Skip to content

Let a CFF subset keep the original glyph ids as CIDs - #37

Open
mimbrown wants to merge 1 commit into
typst:mainfrom
mimbrown:cid-mapping
Open

mimbrown wants to merge 1 commit into
typst:mainfrom
mimbrown:cid-mapping

Conversation

@mimbrown

Copy link
Copy Markdown

I have a use case where I'm generating different parts of a PDF in parallel, then merging together. Obviously I don't want to embed multiple subsets of the same font, I want to merge the subsets at the end, but that needs deterministic glyph mappings. Claude created this PR to add an option for specifying the CidMapping; by default it preserves existing behavior, but allows callers to specify they want the original glyph ID to be used as the CID.

A PDF writer that writes content streams before the final subset is
known needs CIDs that don't depend on the subset. TrueType fonts get
that with a CIDToGIDMap; a CFF font's CIDs come from its charset, which
was always written in subset order.

CidMapping::OriginalGlyphId writes the charset so each glyph's CID is
its glyph id in the original font, as one format 2 range per run of
consecutive ids. subset_with_options takes it along with the variation
coordinates. The default and the existing functions are unchanged.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_013STfqkxxaHR6czBMgfbyrk
@yhay81

yhay81 commented Oct 24, 2026

Copy link
Copy Markdown

This would also let tools subset CFF fonts that a PDF already embeds without rewriting its content. A Type0 font whose CIDFontType0 program is not CID-keyed addresses glyphs by CID = glyph ID, so a subset whose charset maps each new glyph back to its original glyph ID keeps every existing code working, which is exactly what this PR does. For a program that is already CID-keyed (CIDFontType0C, or an OpenType CFF table with an ROS), the codes in the PDF are the program's own CIDs, so the matching variant would keep the source charset's CID for each kept glyph instead of its glyph ID. Would you consider that as a second CidMapping value, Original next to GlyphId? I'd be glad to add it with tests on top of this PR. (pylopdf, a Python PDF library on lopdf/hayro/krilla, would use it for Document.subset_fonts.)

🤖 Generated with Claude Code

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