Skip to content

[PDFBOX-6279] Add ToUnicode entries for glyphs produced by GSUB substitutions - #537

Open
fcalafat wants to merge 1 commit into
apache:trunkfrom
fcalafat:fix-ligature-tounicode
Open

fcalafat wants to merge 1 commit into
apache:trunkfrom
fcalafat:fix-ligature-tounicode

Conversation

@fcalafat

@fcalafat fcalafat commented Oct 9, 2026 •

Copy link
Copy Markdown

When showText() applies GSUB substitutions, a ligature glyph (like "er" or "nd" in a script font) has no cmap entry, so PDCIDFontType2Embedder writes no ToUnicode entry for it and text extraction drops those letters: "Registered" is extracted as "Registed".

Fix:

  • When a glyph has no cmap entry, look it up in the font's GSUB table and write the text of the glyphs it replaces. (pdfbox/src/main/java/org/apache/pdfbox/pdmodel/font/PDCIDFontType2Embedder.java)
  • Only substitutions that produce one glyph are used. When one glyph becomes several, the text can't be split between them.
  • Only the GSUB features that the GSUB workers apply are read (ccmp, liga, clig, calt, locl and the Indic ones). FiraCode's aalt lists the empty exclam.spacer glyph as an alternate of "!", which added two "!" to the text extracted in GlyphLayoutLigaturesAndKerningTest.

Testing:

  • Added two tests that write text, extract it, and compare: Devanagari conjuncts with Lohit-Devanagari.ttf, and Latin letters with a combining accent with the IPA Gothic font already downloaded for the tests. Both fail without the fix. (pdfbox/src/test/java/org/apache/pdfbox/pdmodel/font/TestFontEmbedding.java)
  • The full build passes on trunk, including GlyphLayoutLigaturesAndKerningTest in pdfbox-layout-awt.

…tutions

When showText() applies GSUB substitutions, ligature glyphs such as the
"er" and "nd" of a script font, Devanagari conjuncts, and letters with
a combining accent are drawn as one glyph that has no cmap entry. The
embedder built the ToUnicode map from the cmap only, so these glyphs
got no entry and text extraction dropped their letters: "Registered"
came out as "Registed".

The embedder now maps such a glyph to the text of the glyphs it
replaces, read from the font's GSUB table. Only the features that the
GSUB workers apply are read, and only substitutions that produce one
glyph.
@fcalafat
fcalafat changed the base branch from 3.0 to trunk October 20, 2026 12:53
@fcalafat
fcalafat force-pushed the fix-ligature-tounicode branch from dd7ba17 to d8967d4 Compare October 20, 2026 12:53
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.

1 participant