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

[glypsh-reader] fix ParseIntError when custom param has unquoted string #626

Merged
merged 2 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
31 changes: 19 additions & 12 deletions glyphs-reader/src/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ impl CustomParameters {
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
enum CustomParameterValue {
Int(i64),
Float(OrderedFloat<f64>),
String(String),
Axes(Vec<Axis>),
AxesMappings(Vec<AxisMapping>),
Expand Down Expand Up @@ -393,22 +394,16 @@ impl FromPlist for CustomParameters {
"value" => {
let peek = tokenizer.peek()?;
match peek {
// VendorID is a string but doesn't feel it needs quotes
Token::Atom(..) if name == Some(String::from("vendorID")) => {
let token = tokenizer.lex()?;
let Token::Atom(val) = token else {
return Err(Error::UnexpectedDataType {
expected: "Atom",
found: token.name(),
});
};
value = Some(CustomParameterValue::String(val.to_string()));
}
Token::Atom(..) => {
let Token::Atom(val) = tokenizer.lex()? else {
panic!("That shouldn't happen");
};
value = Some(CustomParameterValue::Int(val.parse().unwrap()));
value = match Plist::parse(val)? {
Plist::Integer(i) => Some(CustomParameterValue::Int(i)),
Plist::Float(f) => Some(CustomParameterValue::Float(f)),
Plist::String(s) => Some(CustomParameterValue::String(s)),
_ => panic!("atom has to be int, float, or string"),
};
}
Token::OpenBrace if name == Some(String::from("Axis Mappings")) => {
let mappings: Vec<AxisMapping> = tokenizer
Expand Down Expand Up @@ -2401,6 +2396,18 @@ mod tests {
assert_eq!(2, font.default_master_idx);
}

#[test]
fn vf_origin_unquoted_string() {
// the 'Variable Font Origin' custom parameter has the value `m01`,
// un un-quoted plist string, which happens to be the default master.id
// that Glyphs.app assigns to the predefined 'Regular' master that any
// "New Font" comes with when it is first crated.
// We just test that we do not crash attempting to parse the unquoted
// string as an integer.
let font = Font::load(&glyphs3_dir().join("CustomOrigin.glyphs")).unwrap();
assert_eq!(1, font.default_master_idx);
}

#[test]
fn glyph_order_default_is_file_order() {
let font = Font::load(&glyphs3_dir().join("WghtVar.glyphs")).unwrap();
Expand Down
54 changes: 54 additions & 0 deletions resources/testdata/glyphs3/CustomOrigin.glyphs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
.appVersion = "3227";
.formatVersion = 3;
axes = (
{
name = Weight;
tag = wght;
}
);
customParameters = (
{
name = "Variable Font Origin";
value = m01;
}
);
date = "2023-12-05 15:07:25 +0000";
familyName = "WghtVar Custom Origin";
fontMaster = (
{
axesValues = (
100
);
id = "09676F4A-DA0D-41A4-A56F-3296CE3BD503";
name = "Master 1";
},
{
axesValues = (
400
);
id = m01;
name = "Master 2";
}
);
glyphs = (
{
glyphname = space;
lastChange = "2023-12-05 15:14:26 +0000";
layers = (
{
layerId = m01;
width = 200;
},
{
layerId = "09676F4A-DA0D-41A4-A56F-3296CE3BD503";
width = 300;
}
);
unicode = 32;
}
);
unitsPerEm = 1000;
versionMajor = 1;
versionMinor = 0;
}