Since 7.25.0, every glyph of a symbolic simple TrueType font without /Encoding and /ToUnicode reads as U+FFFD — the font Ghostscript's pdfwrite writes for any embedded TrueType subset (7.20.0 to 7.24.2 read it)
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 48/100
Research direction
The report names no source files or tests in docling-parse; it provides a Python reproducer using DoclingPdfParser and a PDF font with /Flags 4 and no /Encoding or /ToUnicode. Start by running that reproducer against the affected release and compare its output with 7.24.2. Done means the ASCII text decodes correctly for this symbolic font while the reported cases and versions are checked for regressions.
Written by the indexing model from the issue text.
Description
What happens
Since 7.25.0, text set in a simple TrueType font whose descriptor is flagged symbolic and whose font dictionary has neither /Encoding nor /ToUnicode comes out as U+FFFD, glyph for glyph, in the char, word and textline cells. 7.27.0, the current release, still does (checked with the script below). 7.20.0 to 7.24.2 read the same font; so does pdfium (and so does 7.26.0 with DecodeConfig(keep_glyphs=True), which prints GLYPH<67>GLYPH<111>GLYPH<110>… for Contents: the codes are the ASCII ones).
One font dictionary, four ways (the script below builds the PDF in memory):
| font | 7.20.0, 7.24.2 | 7.25.0, 7.26.0, 7.27.0 |
|---|---|---|
symbolic (/Flags 4), no /Encoding |
Report 2020, 1,234.5 kg |
������ ����, ������� �� |
symbolic (/Flags 4), /Encoding /WinAnsiEncoding |
Report 2020, 1,234.5 kg |
Report 2020, 1,234.5 kg |
nonsymbolic (/Flags 32), no /Encoding |
Report 2020, 1,234.5 kg |
Report 2020, 1,234.5 kg |
nonsymbolic (/Flags 32), /Encoding /WinAnsiEncoding |
Report 2020, 1,234.5 kg |
Report 2020, 1,234.5 kg |
On a real file the versions split the same way: 7.20.0, 7.21.0, 7.22.0, 7.22.1, 7.23.0, 7.24.0 and 7.24.2 read the first four pages of a normalized report without a U+FFFD, 7.25.0 and 7.26.0 with 108.
Why it matters: this is what Ghostscript writes
The first row is not an exotic font. It is what Ghostscript's pdfwrite device makes of an embedded TrueType subset: a font that came in as /Subtype /TrueType, nonsymbolic (/Flags 32), with /Encoding /WinAnsiEncoding and no /ToUnicode goes out as /Subtype /TrueType with no /Encoding, no /ToUnicode and a symbolic descriptor (-sDEVICE=pdfwrite -dPDFSETTINGS=/default -dCompatibilityLevel=1.7). The codes in the content stream are unchanged. The source font is a perfectly ordinary one: InDesign writes it for Latin faces (Avenir, Century Gothic), Word for the digits and Latin letters it sets in a CJK face (ABCDEE+PMingLiU, Microsoft JhengHei). Any pipeline that normalizes its PDFs through Ghostscript before parsing them — ours does — hands docling-parse this font for every one of them.
Ghostscript will not stop writing it, and no setting of it helps:
- It is by design, and the same in 10.06.0 and 10.08.0 (the current release; on six of the files below the two write the same kinds of font and docling-parse reads the same number of U+FFFD from each).
devices/vector/gdevpdtw.c: "TrueType fonts are always written as symbolic, and so they do not have an Encoding entry (SVN revision 11735, bugs #690744, #691036, #691319). In this circumstance, write the ToUnicode map to get a searchable PDF." — but it has a ToUnicode map to write only when the source font had one (pdfi_decode_glyphinpdf/pdf_font.creads the font's own/ToUnicodeand nothing else). A source font that does carry a/ToUnicodekeeps it through pdfwrite and reads fine in 7.26.0. -dSubsetFonts=falsekeeps the/Encodingof a font pdfwrite does not subset, which helps the Latin faces only: a TrueType font with more than 4,096 glyphs is subset whatever the flag says (MAX_NO_SUBSET_GLYPHSingdevpdtb.c), and that is every CJK face. On six files (first six pages each) the flag took two from 971 and 215 U+FFFD to 0 and left the other three at 620, 376 and 6,607.-dPDFSETTINGS=/prepresschanges nothing.
What that looks like on a set of 44 PDFs in Traditional Chinese (annual and sustainability reports, fund fact sheets, insurance manuals, bank notices), each parsed after gs -sDEVICE=pdfwrite: 39 of the 44 are affected. The textline cells hold 256,019 U+FFFD in 7.26.0 against 1,884 in 7.20.0, and 2,179 pages have a run of three or more — the pages a pipeline with a garbled-text check sends to OCR instead of reading their text layer.
Five of the reports (Chinese text in CID fonts, digits and English in Avenir):
| U+FFFD in the textline cells after pdfwrite | 7.20.0 | 7.26.0 |
|---|---|---|
| report 1 (106 pages) | 0 | 5,213 on 99 pages |
| report 2 (132 pages) | 987 | 1,189 |
| report 3 (119 pages) | 485 | 32,354 on 117 pages |
| report 4 (105 pages) | 0 | 15 |
| report 5 (86 pages) | 0 | 3,050 on 86 pages |
(The 987 and 485 are CID fonts whose own /ToUnicode maps to U+FFFD; no engine reads those.) In the first 30 pages of report 1, 1,694 of the 1,901 characters set in fonts of this kind are U+FFFD in 7.26.0 and one is in 7.24.2. What is lost is every year, amount, percentage and English word of the document: 2020年企業社會責任報告書 reads ����年企業社會責任報告書, a table of contents reads �� 董事長的話.
A space set in such a font is an unnamed glyph in the char cells too, while the word and textline cells have a blank there — so the three cell kinds disagree on how many U+FFFD a line holds.
Reproduce
docling-parse only, default DecodeConfig and ContentConfig. The font is not embedded, which a text decode does not need.
from importlib.metadata import version
from io import BytesIO
from docling_parse.pdf_parser import DoclingPdfParser
def build(flags: int, encoding: bytes) -> bytes:
content = b"BT /F1 12 Tf 1 0 0 1 20 100 Tm (Report 2020) Tj ET\nBT /F1 12 Tf 1 0 0 1 20 80 Tm (1,234.5 kg) Tj ET"
widths = b" ".join(b"600" for _ in range(32, 127))
objects = [
b"<< /Type /Catalog /Pages 2 0 R >>",
b"<< /Type /Pages /Kids [3 0 R] /Count 1 >>",
b"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 200 130] /Resources << /Font << /F1 5 0 R >> >> /Contents 4 0 R >>",
b"<< /Length %d >>\nstream\n" % len(content) + content + b"\nendstream",
b"<< /Type /Font /Subtype /TrueType /BaseFont /ABCDEF+Avenir " + encoding
+ b" /FirstChar 32 /LastChar 126 /Widths [" + widths + b"] /FontDescriptor 6 0 R >>",
b"<< /Type /FontDescriptor /FontName /ABCDEF+Avenir /Flags %d /FontBBox [0 -200 1000 900] "
b"/ItalicAngle 0 /Ascent 900 /Descent -200 /CapHeight 700 /StemV 80 >>" % flags,
]
pdf, offsets = b"%PDF-1.7\n", []
for number, obj in enumerate(objects, start=1):
offsets.append(len(pdf))
pdf += b"%d 0 obj\n" % number + obj + b"\nendobj\n"
xref = len(pdf)
pdf += b"xref\n0 %d\n0000000000 65535 f \n" % (len(objects) + 1)
pdf += b"".join(b"%010d 00000 n \n" % offset for offset in offsets)
pdf += b"trailer\n<< /Size %d /Root 1 0 R >>\nstartxref\n%d\n%%%%EOF\n" % (len(objects) + 1, xref)
return pdf
print("docling-parse", version("docling-parse"))
for label, flags, encoding in [
("symbolic (Flags 4), no /Encoding", 4, b""),
("symbolic (Flags 4), /WinAnsiEncoding", 4, b"/Encoding /WinAnsiEncoding"),
("nonsymbolic (Flags 32), no /Encoding", 32, b""),
("nonsymbolic (Flags 32), /WinAnsiEncoding", 32, b"/Encoding /WinAnsiEncoding"),
]:
doc = DoclingPdfParser(loglevel="fatal").load(path_or_stream=BytesIO(build(flags, encoding)))
print(f" {label:42} {[cell.text for cell in doc.get_page(1).textline_cells]}")
doc.unload()
docling-parse 7.24.2
symbolic (Flags 4), no /Encoding ['Report 2020', '1,234.5 kg']
symbolic (Flags 4), /WinAnsiEncoding ['Report 2020', '1,234.5 kg']
nonsymbolic (Flags 32), no /Encoding ['Report 2020', '1,234.5 kg']
nonsymbolic (Flags 32), /WinAnsiEncoding ['Report 2020', '1,234.5 kg']
docling-parse 7.26.0
symbolic (Flags 4), no /Encoding ['������ ����', '������� ��']
symbolic (Flags 4), /WinAnsiEncoding ['Report 2020', '1,234.5 kg']
nonsymbolic (Flags 32), no /Encoding ['Report 2020', '1,234.5 kg']
nonsymbolic (Flags 32), /WinAnsiEncoding ['Report 2020', '1,234.5 kg']
docling-parse 7.27.0
symbolic (Flags 4), no /Encoding ['������ ����', '������� ��']
symbolic (Flags 4), /WinAnsiEncoding ['Report 2020', '1,234.5 kg']
nonsymbolic (Flags 32), no /Encoding ['Report 2020', '1,234.5 kg']
nonsymbolic (Flags 32), /WinAnsiEncoding ['Report 2020', '1,234.5 kg']
To see it on a file of your own: take a PDF with an embedded TrueType subset that has /Encoding /WinAnsiEncoding and no /ToUnicode (a Word document with digits in a Chinese paragraph, saved as PDF, has one), run gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -sOutputFile=out.pdf in.pdf, and parse out.pdf. The original parses without a U+FFFD in every version; out.pdf does in 7.20.0 to 7.24.2 and not since 7.25.0.
Where it seems to come from
7.25.0 is the release of #431 ("Font and ToUnicode decoding fixes"). 7.26.0 added #370 and #453, which follow a base encoding a font-specific font declares; this is the font that declares none. Before 7.25.0 such a font's codes were read through a default encoding — 7.20.0 behaves as if that were StandardEncoding: codes up to 0x7E come out right, 0xA9 as ', 0xAE as fi, and what StandardEncoding leaves undefined is dropped — which was right for all of the ASCII range and is, I think, what the PDF reference's fallback for a symbolic TrueType font without a usable cmap amounts to in practice. pdfium reads the ASCII range of these fonts as ASCII; above it, it read some of them as WinAnsi and others as MacRoman.
What I would expect
For a simple font that gives no way to Unicode (no /ToUnicode, no /Encoding, no usable glyph names), codes 0x20 to 0x7E read as ASCII, as 7.24.2 did: every candidate encoding of such a font agrees on that range. Above 0x7E there is no such agreement, and U+FFFD there is fair.
Environment
docling-parse 7.20.0 to 7.27.0 from PyPI (manylinux x86_64 wheels), Python 3.12, Linux. Ghostscript 10.06.0 and 10.08.0. pypdfium2 5.10.1 for what is said of pdfium.
- Dominant language
- C++
- Stars
- 352
- Forks
- 99
- Avg merge
- 1d 21h
- Merged PRs (30d)
- 37
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from docling-project/docling-parse
-
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
docling-project/docling-parse#448 ·
Maintainers usually reply within 1 day
-
Difficulty 3/5 1-2 days Newbie friendliness 74/100
docling-project/docling-parse#492 ·
Maintainers usually reply within 1 day
-
Difficulty 4/5 3-5 days Newbie friendliness 42/100
docling-project/docling-parse#487 ·
Maintainers usually reply within 1 day
-
7.25.0 splits letter-spaced CJK runs of two to four characters into one word per character (`股本重組` -> `股 本 重 組`): `infer_unspaced_gap_profile()` needs four gaps before it sees trackingPossibly taken @jjohnnystwsss claimed this 4 days ago. Open
Difficulty 4/5 3-5 days Newbie friendliness 55/100
docling-project/docling-parse#483 · 4 comments ·
Maintainers usually reply within 1 day
-
Difficulty 3/5 1-2 days Newbie friendliness 55/100
docling-project/docling-parse#473 ·
Maintainers usually reply within 1 day
All issues in docling-project/docling-parse
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 69/100
Maintainers usually reply within 2 days
-
Missing repro Platform: Android
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
software-mansion/react-native-reanimated#11554 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
Niko1221/Strata#7211 · 3 comments ·
Maintainers usually reply within 5 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 64/100
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
sleepyviper1213/redis#52 ·