Skip to content
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
10 changes: 10 additions & 0 deletions src/fontinfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1724,6 +1724,16 @@ mod tests {
assert_eq!(font_info.open_type_os2_vendor_id, Some("LTTR".into()));
}

#[test]
fn fontinfo_empty_guideline_name() {
let path = "testdata/fontinfo_empty_guideline_name.plist";
let font_info: FontInfo = plist::from_file(path).expect("failed to load fontinfo");
let guidelines = font_info.guidelines.expect("guidelines should be present");
assert_eq!(guidelines.len(), 2);
assert_eq!(guidelines[0].name, None);
assert_eq!(guidelines[1].name, Some(Name::new_raw("ascender")));
}

#[test]
fn fontinfo2() {
let path = "testdata/fontinfotest.ufo/fontinfo.plist";
Expand Down
3 changes: 3 additions & 0 deletions src/glyph/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,9 @@ impl GlifParser {
}
angle = Some(angle_value);
}
// The name is optional, and some real-world fonts write it as
// an explicit empty string; treat that as no name.
b"name" if value.is_empty() => (),
b"name" => name = Some(Name::new(&value).map_err(|_| ErrorKind::InvalidName)?),
b"color" => color = Some(value.parse().map_err(|_| ErrorKind::BadColor)?),
b"identifier" => {
Expand Down
13 changes: 13 additions & 0 deletions src/glyph/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,19 @@ fn guidelines() {
assert_eq!(glyph.width, 364.);
}

#[test]
fn empty_guideline_name() {
let data = r#"
<?xml version="1.0" encoding="UTF-8"?>
<glyph name="period" format="2">
<guideline y="495" name=""/>
</glyph>
"#;
let glyph = parse_glyph(data.as_bytes()).unwrap();
assert_eq!(glyph.guidelines.len(), 1);
assert_eq!(glyph.guidelines[0].name, None);
}

#[test]
#[should_panic(expected = "MissingClose")]
fn missing_close() {
Expand Down
33 changes: 32 additions & 1 deletion src/guideline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,24 @@ struct RawGuideline {
x: Option<f64>,
y: Option<f64>,
angle: Option<f64>,
#[serde(default, deserialize_with = "deserialize_optional_name")]
name: Option<Name>,
color: Option<Color>,
identifier: Option<Identifier>,
}

// The name is optional, and some real-world fonts write it as an explicit
// empty string; treat that as no name instead of failing the load.
fn deserialize_optional_name<'de, D>(deserializer: D) -> Result<Option<Name>, D::Error>
where
D: Deserializer<'de>,
{
match Option::<String>::deserialize(deserializer)? {
Some(name) if !name.is_empty() => name.parse::<Name>().map(Some).map_err(de::Error::custom),
_ => Ok(None),
}
}

impl Serialize for Guideline {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
Expand Down Expand Up @@ -171,7 +184,7 @@ impl<'de> Deserialize<'de> for Guideline {
mod tests {
use super::*;

use serde_test::{assert_tokens, Token};
use serde_test::{assert_de_tokens, assert_tokens, Token};

#[test]
fn guideline_parsing() {
Expand Down Expand Up @@ -207,4 +220,22 @@ mod tests {
],
);
}

#[test]
fn empty_guideline_name_is_no_name() {
let expected = Guideline::new(Line::Vertical(495.0), None, None, None);
assert_de_tokens(
&expected,
&[
Token::Struct { name: "RawGuideline", len: 2 },
Token::Str("x"),
Token::Some,
Token::F64(495.0),
Token::Str("name"),
Token::Some,
Token::Str(""),
Token::StructEnd,
],
);
}
}
23 changes: 23 additions & 0 deletions testdata/fontinfo_empty_guideline_name.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>familyName</key>
<string>Test</string>
<key>guidelines</key>
<array>
<dict>
<key>name</key>
<string></string>
<key>y</key>
<integer>495</integer>
</dict>
<dict>
<key>name</key>
<string>ascender</string>
<key>y</key>
<integer>750</integer>
</dict>
</array>
</dict>
</plist>
Loading