Skip to content
Merged
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
34 changes: 34 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,10 @@ fn parse(data: &[u8], index: u32) -> Result<Face<'_>> {
records.push(r.read::<TableRecord>().ok_or(MalformedFont)?);
}

// `Face::table` binary-searches by tag, but fonts in the wild violate
// the spec's requirement that the directory be sorted.
records.sort_by_key(|record| record.tag);

Ok(Face { data, records })
}

Expand Down Expand Up @@ -693,6 +697,36 @@ mod tests {
assert_eq!(result, Err(Error::CFFError));
}

#[test]
fn unsorted_table_directory_is_searchable() {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you make sure that this test fails before this commit? AFAIK it might depend on where exactly the binary search starts right?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. With the sort reverted, the test fails (table(Tag::HEAD) returns None). It's also pivot-independent because the fixture is a swapped pair, so searching for one of the two tags always gets steered into an empty range, and one of the two asserts always fails.

// Two table records deliberately stored in non-alphabetical order
// (`head` before `cmap`): the directory violates the spec's sort
// requirement, but both tables must still be found.
let mut font = Vec::new();
font.extend(0x00010000u32.to_be_bytes());
font.extend(2u16.to_be_bytes());
font.extend(0u16.to_be_bytes());
font.extend(0u16.to_be_bytes());
font.extend(0u16.to_be_bytes());
// Directory (2 records à 16 bytes; data starts at 12 + 32 = 44).
font.extend(*b"head");
font.extend(0u32.to_be_bytes());
font.extend(44u32.to_be_bytes());
font.extend(4u32.to_be_bytes());
font.extend(*b"cmap");
font.extend(0u32.to_be_bytes());
font.extend(48u32.to_be_bytes());
font.extend(4u32.to_be_bytes());
// Table payloads.
font.extend([1, 2, 3, 4]);
font.extend([5, 6, 7, 8]);

let face = parse(&font, 0).unwrap();

assert_eq!(face.table(Tag::HEAD), Some([1, 2, 3, 4].as_slice()));
assert_eq!(face.table(Tag::CMAP), Some([5, 6, 7, 8].as_slice()));
}

#[test]
fn table_record_with_invalid_bounds_returns_none() {
let face = Face {
Expand Down
Loading