Skip to content

Invert the font cmap once when subsetting - #183

Merged
danmolitor merged 1 commit into
mainfrom
fix/subset-cmap-inversion
Oct 6, 2026
Merged

danmolitor merged 1 commit into
mainfrom
fix/subset-cmap-inversion

Conversation

@danmolitor

Copy link
Copy Markdown
Owner

Addresses #182. Thanks @captyoungkyu for the precise diagnosis and patch; this is the same approach.

Font subsetting found each glyph's codepoint by scanning 0..=0xFFFF per glyph. Hangul starts at U+AC00, and a glyph with no codepoint (a ligature or shaped form) scanned the whole plane, so serialization time grew with the number of distinct Korean syllables. The glyph-to-codepoint map is now built in one pass over the font's Unicode cmap, keeping each glyph's lowest BMP codepoint as the scan did, so subset fonts are unchanged.

Measured with Noto Sans KR, one page of Hangul (native CLI, before / after):

Distinct syllables before after output
100 720 ms 262 ms byte-identical
1,000 4,662 ms 85 ms byte-identical
  • New test: the one-pass map against the full-plane scan, for every glyph in six fonts (Noto Sans, Noto Emoji, Noto Naskh Arabic, Noto Sans Devanagari, Noto Sans Hebrew, Liberation Sans). Checked that it catches a wrong mapping (keeping the highest codepoint instead fails it). .notdef is excluded: a format 4 table's final segment maps U+FFFF to it, and the subsetter never gives it a cmap entry.
  • A timing test would be flaky in CI, so the before/after above is measured rather than asserted.
  • Engine (671), visual, html and package tests pass; the byte wall is identical; veraPDF passes PDF/UA-1 and every PDF/A level. Gallery manifest regenerated (hashes only).

The subsetter found each glyph's codepoint by scanning 0..=0xFFFF per
glyph. Hangul starts at U+AC00 and a glyph with no codepoint scanned the
whole plane, so a Korean document with 1,000 distinct syllables spent
about 4.7 s writing its font. The glyph-to-codepoint map is now built in
one pass over the cmap, keeping each glyph's lowest BMP codepoint as the
scan did: 85 ms, byte-identical output.

The test checks the one-pass map against the full-plane scan for every
glyph in six fonts (Noto Sans, Noto Emoji, Noto Naskh Arabic, Noto Sans
Devanagari, Noto Sans Hebrew, Liberation Sans).

Addresses #182.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@captyoungkyu

Copy link
Copy Markdown

Thanks for the quick turnaround and the thorough verification!

I'll test it on our invoices once it's released.

@danmolitor
danmolitor merged commit 58a2f74 into main Oct 6, 2026
13 checks passed
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.

2 participants