fix: drop kashida glyphs that a ToUnicode CMap reads as U+FFFD
Problem
Some producers (InDesign) give the kashida (tatweel) glyph the ToUnicode destination U+FFFD. In a justified Arabic line every stretch then prints a replacement character inside a word (الش���خصية), and the pages count as garbled for the OCR gate. In the Saudi Personal Data Protection Law PDF (64 pages, fonts Muna / MunaBlack, Type0 Identity-H) CID 0xCA is glyph00202, a flat stroke on the baseline, and its entry is <00CA> <FFFD>. The output had 3,128 � on 146 lines and 60 pages listed as needing OCR.
Related to, but not part of, firecrawl/firecrawl#5931.
Fix
For each Type0 font, the codes whose ToUnicode destination is U+FFFD are collected from the CMap's entries (not by scanning the code space). A code is treated as a kashida, and dropped from the text before decoding, only when:
- the CMap reads at least 8 Arabic letters, and
- the glyph that code selects in that font's embedded sfnt program (through the font's own CIDToGIDMap) is a kashida: its box reaches both edges of its advance, sits on the baseline, is at most 0.2 em high, at least 0.1 em wide, and wider than tall.
The kashida is dropped, not read as U+0640, because a justification stretch is not part of the logical text. Any other U+FFFD entry still counts as a decode failure, and the OCR gate is unchanged. CMaps are never copied or changed, so fonts that share a ToUnicode stream are each judged by their own glyphs.
Bounds: the font program is read with the existing 8 MiB decompression limit, at most 256 codes are measured per font and 64 font identities per document, each glyph is measured once, widths are computed in i32, and nothing runs in fast mode.
Result on that PDF: 0 � (was 3,128), and the pages listed as needing OCR drop from 60 to 3. The 45 fixture PDFs give identical pdf2md output.
Known limitations
Only a font's primary ToUnicode CMap is read (not its subset remap), bare-CFF Type0 programs are not measured, and simple fonts with /Differences glyph names are not covered (the title page of that PDF still reads الûشخüصية).
Testing
11 tests in src/extractor/kashida_tests.rs: the kashida is dropped, other U+FFFD entries stay failures, flat glyphs that are not kashidas stay, Latin fonts are untouched, fonts sharing a ToUnicode stream are repaired each by their own glyphs and CIDToGIDMap, 65,000 replacement codes are bounded, and extreme glyph coordinates do not overflow.
Not run
- The pdf-evals
bench.pysuite: that sibling repo was not available. - The pinned 1.98.0 toolchain: I used 1.92.0.
cargo clippy -- -D warningsfails with 3nonminimal_boolerrors that already exist on main (src/extractor/clip_boundaries.rs:78, reported twice, andsrc/markdown/convert.rs:1543). There are no new warnings from this change.
Summary by cubic
Stops justified Arabic text from showing a replacement character () for every kashida stretch. Some producers, like InDesign, give the kashida (tatweel) glyph the ToUnicode destination U+FFFD, so lines like الشخصية were printed garbled and their pages flagged for OCR.
For each Type0 font, codes that its ToUnicode CMap reads as U+FFFD are now dropped from the text before decoding — but only when the CMap reads at least 8 Arabic letters and the glyph that code selects in the font's own embedded program is a kashida: a flat stroke on the baseline spanning its advance. Any other U+FFFD entry still counts as a decode failure, so the OCR gate is unchanged. CMaps are never modified, so fonts sharing a ToUnicode stream are each judged by their own glyphs and CIDToGIDMap. A run of nothing but kashidas is no text and not a space.
On the Saudi Personal Data Protection Law PDF this removes all 3,128 `` characters and cuts the pages listed for OCR from 60 to 3; the 45 fixture PDFs produce identical output.
Known limitations
- Only the primary ToUnicode CMap is read, not a renumbered subset remap or a stream written inline in the font dictionary.
- Bare-CFF Type0 programs aren't measured, and simple fonts using
/Differencesglyph names aren't covered.
Testing
- 20 tests in
src/extractor/kashida_tests.rscover the dropped kashida, preserved non-kashida U+FFFD entries, shared ToUnicode streams, kashida-only runs, the 256-code bound, glyph coordinates at the i16 limits, odd-length strings (read by the byte, then in pairs when no byte reads), and range members overridden by a direct entry or read by two ranges sharing a start; a leftover unread byte after dropped kashida pairs still counts as unmapped coverage. - The kashida lookup is skipped in fast page-detection mode.
cargo clippy -- -D warningsshows 3 pre-existingnonminimal_boolerrors onmain; no new warnings from this change.