Skip to content

Sort table records when parsing the table directory - #29

Merged
laurmaedje merged 1 commit into
typst:mainfrom
dcalvo:sort-table-records
Jul 23, 2026
Merged

laurmaedje merged 1 commit into
typst:mainfrom
dcalvo:sort-table-records

Conversation

@dcalvo

@dcalvo dcalvo commented Jul 21, 2026

Copy link
Copy Markdown
Contributor

parse stores table records in file order, while Face::table looks them up with a binary search that assumes the directory is sorted by tag. The OpenType spec requires this ordering — "Entries in the Table Directory must be sorted in ascending order by tag." — but fonts embedded in PDFs violate it in the wild, and for those fonts the binary search fails to find tables that are physically present, so subsetting errors out or silently drops data. This sorts the records once after parsing (they have no other consumer, so behavior is unchanged for conformant fonts) and adds a regression test with a deliberately unsorted directory.

Comment thread src/lib.rs
}

#[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.

@LaurenzV

Copy link
Copy Markdown
Collaborator

LGTM then, if fine with @laurmaedje. Can confirm that such fonts exist in some PDFs, unfortunately.

@laurmaedje

Copy link
Copy Markdown
Member

Do ttf-parser / skrifa handle such fonts "correctly"?

@LaurenzV

Copy link
Copy Markdown
Collaborator

Skrifa does, I submitted a PR some time ago (googlefonts/fontations#1526). Not sure about ttf-parser

@laurmaedje
laurmaedje merged commit b0adcc1 into typst:main Jul 23, 2026
3 checks passed
@laurmaedje

Copy link
Copy Markdown
Member

Okay, then I'm fine with it. ttf-parser does not matter as much since we migrating away from it anyway. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants