Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get the axis ranges from .glyphs files #42

Merged
merged 3 commits into from
Dec 12, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 30 additions & 18 deletions glyphs-reader/src/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,24 @@ fn custom_param<'a>(
None
}

fn glyphs_v2_field_name_and_default(nth_axis: usize) -> Result<(&'static str, f64), Error> {
// Per https://github.com/googlefonts/fontmake-rs/pull/42#pullrequestreview-1211619812
// the field to use is based on the order in axes NOT the tag.
// That is, whatever the first axis is, it's value is in the weightValue field. Long sigh.
// Defaults per https://github.com/googlefonts/fontmake-rs/pull/42#discussion_r1044415236.
Ok(match nth_axis {
0 => ("weightValue", 400_f64),
rsheeter marked this conversation as resolved.
Show resolved Hide resolved
1 => ("widthValue", 100_f64),
2 => ("customValue", 0_f64),
_ => {
return Err(Error::StructuralError(format!(
"We don't know what field to use for axis {}",
nth_axis
)))
}
})
}

impl Font {
fn parse_codepoints(&mut self, radix: u32) {
for glyph in self.glyphs.iter_mut() {
Expand Down Expand Up @@ -391,31 +409,25 @@ impl Font {
});
}

if axes.len() > 3 {
return Err(Error::StructuralError(
"We only understand 0..3 axes for Glyphs v2".into(),
));
}

// v2 stores values for axes in specific fields, find them and put them into place
// "Axis position related properties (e.g. weightValue, widthValue, customValue) have been replaced by the axesValues list which is indexed in parallel with the toplevel axes list."
for master in self.font_master.iter_mut() {
let mut axis_values = Vec::new();
for axis in axes.iter() {
let field_name = match axis.tag.as_str() {
"wght" => "weightValue",
"wdth" => "widthValue",
"XXXX" => "customValue",
_ => {
return Err(Error::StructuralError(format!(
"No v2 field known for '{}'",
axis.tag
)))
}
};
for idx in 0..axes.len() {
// Per https://github.com/googlefonts/fontmake-rs/pull/42#pullrequestreview-1211619812
// the field to use is based on the order in axes NOT the tag.
// That is, whatever the first axis is, it's value is in the weightValue field. Long sigh.
let (field_name, default_value) = glyphs_v2_field_name_and_default(idx)?;
let value = master
.other_stuff
.remove(field_name)
.ok_or_else(|| {
Error::StructuralError(format!(
"Missing '{}' in\n{:#?}",
field_name, master.other_stuff
))
})?
.unwrap_or_else(|| Plist::Float(default_value.into()))
.as_f64()
.ok_or_else(|| {
Error::StructuralError(format!(
Expand Down
4 changes: 0 additions & 4 deletions resources/testdata/glyphs2/WghtVar_ImplicitAxes.glyphs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ capHeight = 0;
descender = -200;
id = m01;
weightValue = 400;
anthrotype marked this conversation as resolved.
Show resolved Hide resolved
widthValue = 100;
customValue = 0;
xHeight = 0;
},
{
Expand All @@ -31,8 +29,6 @@ descender = -200;
id = "E09E0C54-128D-4FEA-B209-1B70BEFE300B";
weight = Bold;
weightValue = 700;
widthValue = 100;
customValue = 0;
xHeight = 500;
}
);
Expand Down