Rade Martinović created PDFBOX-6354:
---------------------------------------

             Summary: Ligature glyph without a cmap entry gets no ToUnicode 
mapping, so text extraction loses the letters
                 Key: PDFBOX-6354
                 URL: https://issues.apache.org/jira/browse/PDFBOX-6354
             Project: PDFBox
          Issue Type: Bug
          Components: PDModel
    Affects Versions: 3.0.6 PDFBox
         Environment: PDFBox 3.0.6, FontBox 3.0.6, Java 25, macOS. Also seen 
through openhtmltopdf 1.1.40.
            Reporter: Rade Martinović
         Attachments: LigTestSans.ttf, LigatureToUnicode.java, ligature.pdf, 
make_test_font.py

Since PDFBOX-5599 (3.0.0), {{PDPageContentStream.showText}} applies the font's 
GSUB {{liga}} feature to Latin text. The ToUnicode CMap is still built from the 
font's cmap. Ligature glyphs such as {{f_f}}, {{f_f_i}} and {{f_f_l}} usually 
have no cmap entry, because a ligature is not a character. Such a glyph gets no 
ToUnicode entry, and the letters it stands for are lost when the text is 
extracted. The page looks right.

PDFBox logs it when the font is subset:
{noformat}
WARNING: No Unicode mapping for CID+5041 (5041) in font AAEXBC+LigTestSans
{noformat}

Result for {{Raffael Pfeffer, Offenbach, office, affluent}}:
||Reader||Extracted text||
|PDFTextStripper 3.0.6|{{Ra ael Pfe er, O enbach, o ce, a uent}}|
|pypdf (falls back to the CID)|{{RaᎱael PfeᎱer, OᎱenbach, oᎴce, aᎵuent}}|

The same font with the ligatures mapped to U+FB00-U+FB04 (original DejaVu Sans) 
extracts correctly, because PDFTextStripper expands the ligature code points.

We ran into it with a commercial font (Gilroy) in invoices rendered by 
openhtmltopdf, which uses {{showText}}. A print-and-mail service reads the 
recipient address from the PDF text, and "Raffael Pfeffer, Offenbach" arrived 
as "Ra ael Pfe er, O enbach".

h3. Expected

PDFBox knows the input characters when it substitutes the glyph, so the 
ToUnicode entry for a ligature glyph can be a {{bfchar}} to the source string: 
{{<CID> <00660066>}} for "ff", {{<CID> <006600660069>}} for "ffi". PDF 
32000-1:2008 §9.10.3 allows a ToUnicode destination of several UTF-16 code 
units. Then any reader extracts the plain letters, with no need to understand 
U+FB0x.

If that's not wanted, an option to turn off GSUB substitution for Latin text 
would let callers avoid the problem. Today the only workaround is to remove 
{{liga}} from the font.

h3. Steps to reproduce

Attached: {{LigatureToUnicode.java}}, {{LigTestSans.ttf}}, 
{{make_test_font.py}}, {{ligature.pdf}}.

{{LigTestSans.ttf}} is DejaVu Sans (Bitstream Vera / DejaVu licence, renamed as 
the licence requires) with U+FB00-U+FB06 removed from the cmap, which 
{{make_test_font.py}} does with fontTools. This gives the unmapped ligature 
glyphs that many fonts ship with.

{noformat}
java -cp 
pdfbox-3.0.6.jar:fontbox-3.0.6.jar:pdfbox-io-3.0.6.jar:commons-logging-1.3.x.jar
 LigatureToUnicode.java LigTestSans.ttf

written:   Raffael Pfeffer, Offenbach, office, affluent
extracted: Ra ael Pfe er, O enbach, o ce, a uent
{noformat}

With {{DejaVuSans.ttf}} instead, {{extracted}} equals {{written}}.



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to