pdf/ps: Stop retaining glyph indices in font subsets#31975
Merged
Conversation
Member
Author
|
Note, I have updated the test image here that had the most significant file size changes so we aren't carrying them around needlessly. Note however, it was not necessary to do so as this is just removing unused cruft and tests passed without the new images. |
When passing `retain_gids=True` to the subsetter, it keeps all glyphs until the last one used, in order to preserve the order. For unused glyphs, the data is removed, but metadata about them remains. For example, in DejaVu Sans, the 'fl' ligature is glyph index 5040; this means you get >5000 unused (but empty) glyphs even when that is the only character used from the font subset. If a file uses multiple subsets, each one could contribute an extra copy of the common lower end of the glyph table (e.g., the Latin alphabet, punctuation, etc.) In PostScript, glyphs are referenced by their name and so the glyph index is unimportant. In PDF, the same is true when embedding Type 3 fonts. When embedding Type 42 fonts in PDF, all text is output as character codes, which have their own mapping to glyph indices in each font. Since we already have to output this information, we just need to adjust it slightly to have the glyph index in the subsetted font, and then we can drop the `retain_gids` option. Depending on which is the last glyph in a subset, how many glyphs are in a font, and how many subsets are necessary, this can have a significant impact on the resulting file size. As an example from our test baseline images: - `test_backend_pdf/multi_font_type42.pdf`: 154239 -> 75304 bytes (>50% saved) - `test_backend_pdf/ttc_type42.pdf`: 185702 -> 9803 bytes (almost 95% saved) - `test_backend_ps/multi_font_type42.eps`: 761648 -> 198585 bytes (almost 75% saved) - `test_backend_ps/type42_without_prep.eps`: 1538967 -> 15978 bytes (almost 99% saved)
This is the first version to save the glyph mapping from original to subset font.
2ff2593 to
f6d83e4
Compare
tacaswell
approved these changes
Jul 9, 2026
tacaswell
left a comment
Member
There was a problem hiding this comment.
Given the age of fonttools 4.28.2 I think it is OK to bump the version in 3.11.1.
ksunden
approved these changes
Jul 10, 2026
QuLogic
pushed a commit
that referenced
this pull request
Jul 15, 2026
QuLogic
added a commit
that referenced
this pull request
Jul 15, 2026
…975-on-v3.11.x Backport PR #31975 on branch v3.11.x (pdf/ps: Stop retaining glyph indices in font subsets)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR summary
When passing
retain_gids=Trueto the subsetter, it keeps all glyphs until the last one used, in order to preserve the order. For unused glyphs, the data is removed, but metadata about them remains.For example, in DejaVu Sans, the 'fl' ligature is glyph index 5040; this means you get >5000 unused (but empty) glyphs even when that is the only character used from the font subset. If a file uses multiple subsets, each one could contribute an extra copy of the common lower end of the glyph table (e.g., the Latin alphabet, punctuation, etc.)
In PostScript, glyphs are referenced by their name and so the glyph index is unimportant. In PDF, the same is true when embedding Type 3 fonts. When embedding Type 42 fonts in PDF, all text is output as character codes, which have their own mapping to glyph indices in each font. Since we already have to output this information, we just need to adjust it slightly to have the glyph index in the subsetted font, and then we can drop the
retain_gidsoption.Depending on which is the last glyph in a subset, how many glyphs are in a font, and how many subsets are necessary, this can have a significant impact on the resulting file size. As an example from our test baseline images:
test_backend_pdf/multi_font_type42.pdf: 154239 -> 75304 bytes (>50% saved)test_backend_pdf/ttc_type42.pdf: 185702 -> 9803 bytes (almost 95% saved)test_backend_ps/multi_font_type42.eps: 761648 -> 198585 bytes (almost 75% saved)test_backend_ps/type42_without_prep.eps: 1538967 -> 15978 bytes (almost 99% saved)Fixes #31955
AI Disclosure
None
PR checklist