Skip to content

Commit

Permalink
Strip BOM from glif xml if present
Browse files Browse the repository at this point in the history
  • Loading branch information
cmyr committed Oct 4, 2024
1 parent 27d7a2a commit 1fe9e3b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/glyph/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ type Version = (u32, u32);
const VERSION_1: Version = (1, 0);
const VERSION_2: Version = (2, 0);

// https://en.wikipedia.org/wiki/Byte_order_mark
const UTF8_BOM: &[u8] = &[0xEF, 0xBB, 0xBF];

pub(crate) struct GlifParser<'names> {
glyph: Glyph,
version: Version,
Expand All @@ -36,6 +39,8 @@ impl<'names> GlifParser<'names> {
xml: &[u8],
names: Option<&'names NameList>,
) -> Result<Glyph, GlifLoadError> {
// optional but allowed for utf-8.
let xml = xml.strip_prefix(UTF8_BOM).unwrap_or(xml);
let mut reader = Reader::from_reader(xml);
let mut buf = Vec::new();
reader.trim_text(true);
Expand Down
7 changes: 7 additions & 0 deletions src/glyph/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -888,3 +888,10 @@ fn deduplicate_unicodes2() {
"#;
assert_eq!(data2, data2_expected);
}

#[test]
fn bom_glif() {
let bytes = include_bytes!("../../testdata/bom_glif.glif");
let glyph = parse_glyph(bytes).expect("initial load failed");
assert_eq!(glyph.lib.get("hi").unwrap().as_string(), Some("hello"));
}
25 changes: 25 additions & 0 deletions testdata/bom_glif.glif
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!-- this includes a byte order mark (https://en.wikipedia.org/wiki/Byte_order_mark)-->
<?xml version="1.0" encoding="UTF-8"?>
<glyph name=".notdef" format="2">
<advance width="600" height="1000"/>
<outline>
<contour>
<point x="145" y="51" type="line"/>
<point x="145" y="663" type="line"/>
<point x="454" y="663" type="line"/>
<point x="454" y="51" type="line"/>
</contour>
<contour>
<point x="94" y="0" type="line"/>
<point x="505" y="0" type="line"/>
<point x="505" y="714" type="line"/>
<point x="94" y="714" type="line"/>
</contour>
</outline>
<lib>
<dict>
<key>hi</key>
<string>hello</string>
</dict>
</lib>
</glyph>

0 comments on commit 1fe9e3b

Please sign in to comment.