Skip to content

Commit

Permalink
feat: set variations in ttf-parser
Browse files Browse the repository at this point in the history
  • Loading branch information
zimond committed Sep 21, 2023
1 parent 293f57d commit d85d02c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
14 changes: 12 additions & 2 deletions src/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ impl Font {
Variant::Index(i) => i,
_ => 0,
};
let face = StaticFaceTryBuilder {
let mut face = StaticFaceTryBuilder {
buffer: buffer.clone(),
face_builder: |buf| Face::parse(buf, index),
}
Expand Down Expand Up @@ -350,7 +350,12 @@ impl Font {
stretch: face.borrow_face().width().to_number() as u32,
family: ascii_name.unwrap_or_else(|| names[0].name.clone()),
};
if let Variant::Instance { coords, names, .. } = &variant {
if let Variant::Instance {
coords,
names,
axes,
} = &variant
{
let width_axis_index = axes
.iter()
.position(|axis| axis.tag == Tag::from_bytes(b"wdth"));
Expand All @@ -364,6 +369,11 @@ impl Font {
key.weight = value.0 as u32;
}
key.family = names[0].postscript.name.clone();
face.with_face_mut(|face| {
for (coord, axis) in coords.iter().zip(axes.iter()) {
face.set_variation(axis.tag, coord.0);
}
});
}
let font = Font {
names,
Expand Down
11 changes: 10 additions & 1 deletion tests/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ pub fn test_variable_font_loading() -> Result<(), Error> {
let _ = fontkit.add_font_from_buffer(buf)?;
let mut key = FontKey::default();
key.family = "AlimamaFangYuanTiVF-Medium-Round".into();
assert!(fontkit.query(&key).is_some());
let bitmap_1 = fontkit
.query(&key)
.and_then(|font| font.bitmap('G', 10.0, 0.0))
.map(|g| g.bitmap.iter().filter(|p| **p > 0).count());
key.family = "AlimamaFangYuanTiVF-Thin-Round".into();
let bitmap_2 = fontkit
.query(&key)
.and_then(|font| font.bitmap('G', 10.0, 0.0))
.map(|g| g.bitmap.iter().filter(|p| **p > 0).count());
assert!(bitmap_1 > bitmap_2);
Ok(())
}

0 comments on commit d85d02c

Please sign in to comment.