Skip to content

Commit

Permalink
Adding support for arbitrary variable font axes
Browse files Browse the repository at this point in the history
  • Loading branch information
bwoods committed Nov 13, 2024
1 parent 151326c commit 839143c
Showing 1 changed file with 17 additions and 22 deletions.
39 changes: 17 additions & 22 deletions composable-views/src/text/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,10 @@ impl<'a> Font<'a> {
Some(FontConfig {
face,
features: Vec::default(),
variations: Vec::default(),
direction: None,
script: Script::from_iso15924_tag(Tag::from_bytes(b"Zzzz")),
language: None,
weight: None,
})
}
}
Expand All @@ -152,10 +152,10 @@ impl<'a> Font<'a> {
pub struct FontConfig<'a> {
face: Face<'a>,
features: Vec<Feature>,
variations: Vec<(Tag, f32)>,
direction: Option<Direction>,
script: Option<Script>,
language: Option<Language>,
weight: Option<f32>,
}

impl<'a> FontConfig<'a> {
Expand Down Expand Up @@ -195,34 +195,29 @@ impl<'a> FontConfig<'a> {
self
}

///
#[inline]
pub fn variation(mut self, tag: &[u8; 4], value: f32) -> Self {
self.variations
.push((Tag::from_bytes(tag), value));

self
}

///
#[inline]
pub fn weight(self, weight: f32) -> Self {
Self {
weight: Some(weight),
..self
}
self.variation(b"wght", weight)
}

/// The final step in building a Font.
#[inline(never)]
pub fn size(mut self, size: f32) -> Font<'a> {
for axis in self.face.variation_axes().into_iter() {
match &axis.tag.to_bytes() {
b"opsz" => {
let opsz = size.clamp(axis.min_value, axis.max_value);
self.face.set_variation(axis.tag, opsz);
}
b"wght" => {
let wght = self
.weight
.map(|w| w.clamp(axis.min_value, axis.max_value))
.unwrap_or(axis.def_value);

self.face.set_variation(axis.tag, wght);
}
_ => {}
}
// Always attempt to match optical sizing
self.face.set_variation(Tag::from_bytes(b"opsz"), size);

for (tag, value) in self.variations {
self.face.set_variation(tag, value);
}

// Using direction.unwrap_or_default() would give an Direction::Invalid
Expand Down

0 comments on commit 839143c

Please sign in to comment.