Skip to content

Commit

Permalink
Support setting font feature values (zed-industries#11898)
Browse files Browse the repository at this point in the history
Now (on `macOS` and `Windows`) we can set font feature value:
```rust
  "buffer_font_features": {
    "cv01": true,
    "cv03": 3,
    "cv09": 1,
    "VSAH": 7,
    "VSAJ": 8
  }
```

And one can still use `"cv01": true`.



https://github.com/zed-industries/zed/assets/14981363/3e3fcf4f-abdb-4d9e-a0a6-71dc24a515c2




Release Notes:

- Added font feature values, now you can set font features like `"cv01":
7`.

---------

Co-authored-by: Mikayla <[email protected]>
  • Loading branch information
JunkuiZhang and mikayla-maki authored May 16, 2024
1 parent b6189b0 commit 80caa74
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 242 deletions.
7 changes: 2 additions & 5 deletions crates/gpui/src/platform/mac/open_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,11 @@ pub fn apply_features(font: &mut Font, features: &FontFeatures) {
let native_font = font.native_font();
let mut feature_array =
CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks);
for (tag, enable) in features.tag_value_list() {
if !enable {
continue;
}
for (tag, value) in features.tag_value_list() {
let keys = [kCTFontOpenTypeFeatureTag, kCTFontOpenTypeFeatureValue];
let values = [
CFString::new(&tag).as_CFTypeRef(),
CFNumber::from(1).as_CFTypeRef(),
CFNumber::from(*value as i32).as_CFTypeRef(),
];
let dict = CFDictionaryCreate(
kCFAllocatorDefault,
Expand Down
31 changes: 12 additions & 19 deletions crates/gpui/src/platform/windows/direct_write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1201,26 +1201,26 @@ fn apply_font_features(

// All of these features are enabled by default by DirectWrite.
// If you want to (and can) peek into the source of DirectWrite
let mut feature_liga = make_direct_write_feature("liga", true);
let mut feature_clig = make_direct_write_feature("clig", true);
let mut feature_calt = make_direct_write_feature("calt", true);
let mut feature_liga = make_direct_write_feature("liga", 1);
let mut feature_clig = make_direct_write_feature("clig", 1);
let mut feature_calt = make_direct_write_feature("calt", 1);

for (tag, enable) in tag_values {
if tag == *"liga" && !enable {
for (tag, value) in tag_values {
if tag.as_str() == "liga" && *value == 0 {
feature_liga.parameter = 0;
continue;
}
if tag == *"clig" && !enable {
if tag.as_str() == "clig" && *value == 0 {
feature_clig.parameter = 0;
continue;
}
if tag == *"calt" && !enable {
if tag.as_str() == "calt" && *value == 0 {
feature_calt.parameter = 0;
continue;
}

unsafe {
direct_write_features.AddFontFeature(make_direct_write_feature(&tag, enable))?;
direct_write_features.AddFontFeature(make_direct_write_feature(&tag, *value))?;
}
}
unsafe {
Expand All @@ -1233,18 +1233,11 @@ fn apply_font_features(
}

#[inline]
fn make_direct_write_feature(feature_name: &str, enable: bool) -> DWRITE_FONT_FEATURE {
fn make_direct_write_feature(feature_name: &str, parameter: u32) -> DWRITE_FONT_FEATURE {
let tag = make_direct_write_tag(feature_name);
if enable {
DWRITE_FONT_FEATURE {
nameTag: tag,
parameter: 1,
}
} else {
DWRITE_FONT_FEATURE {
nameTag: tag,
parameter: 0,
}
DWRITE_FONT_FEATURE {
nameTag: tag,
parameter,
}
}

Expand Down
Loading

0 comments on commit 80caa74

Please sign in to comment.