fix: drop kashida glyphs that a ToUnicode CMap reads as U+FFFD

Closed
BE
1d ago
+628-24

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 file output had 3,128 � on 146 lines and 60 pages listed as needing OCR.

Related to, but not part of, firecrawl/firecrawl#5931.

Fix

After the existing control-destination repair, a U+FFFD entry is emptied (the
code reads as nothing) only when:

  • the CMap is an Arabic font's (at least 8 Arabic letters, at least a third of the entries), and
  • the glyph in the embedded sfnt 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. A code that reads as nothing no
longer counts as a contributing code, which keeps the ligature joining
(الموافق) in order. Only the ToUnicode CMap is repaired, not its subset remap.

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.

The title page uses a separate simple CFF font whose û/ü glyphs are not
flat strokes; they are not covered here.

Checks

Run: cargo fmt, cargo test (all pass, 8 new tests), cargo clippy.
Clippy fails only with the 3 nonminimal_bool errors that already exist on main.
Not run: the pdf-evals bench.py suite (repository not available), release
build, SonarCloud.

Summary by Sourcery

Drop justified Arabic kashida glyphs that incorrectly decode as U+FFFD while preserving legitimate text and decode failures.

Bug Fixes:

  • Prevent justified Arabic kashida glyphs mapped to U+FFFD from appearing as replacement characters in extracted text or triggering unnecessary OCR flags.
  • Preserve other U+FFFD decode failures while handling shared ToUnicode maps according to each font's glyph mapping.

Enhancements:

  • Bound and cache per-font kashida detection based on Arabic mappings and embedded TrueType glyph geometry, including CIDToGIDMap handling.
  • Apply kashida removal consistently to page and form XObject text extraction, while leaving fast page-detection mode unchanged.

Documentation:

  • Document the new Arabic kashida handling, bounds, and supported limitations in the unreleased changelog.

Tests:

  • Add coverage for kashida removal, non-Arabic and non-kashida glyphs, shared CMaps, CIDToGIDMap variants, fast mode, overflow-safe measurements, candidate limits, and overlapping range resolution.

Summary by cubic

Fixes justified Arabic text showing a replacement character at every stretch when a kashida (tatweel) glyph's ToUnicode entry reads as U+FFFD, as InDesign writes it. Kashida codes are now found once per font identity, keyed by its ToUnicode stream, program, and CIDToGIDMap, and dropped from the string before it is decoded, so a stream shared across fonts is never changed for the others.

  • The code is dropped only when the CMap is an Arabic font's (range destinations counted too), the code's effective reading is U+FFFD, and the glyph is a flat stroke spanning its advance on the baseline; a code another range maps to real text still stands, and other U+FFFD entries count as decode failures.
  • Work is bounded: at most 256 codes per font and 64 fonts per document are measured, once per glyph, and the fast page-detection mode skips the repair.
  • Glyph boxes are measured in i32 to avoid overflow, with each glyph measured once however many codes read it.
  • On the Saudi Personal Data Protection Law PDF, this removes 3,128 replacement characters and drops OCR-flagged pages from 60 to 3; the 45 fixture PDFs give identical pdf2md output and all new tests pass.

Confidence Score: 5/5 · The PR appears safe to merge; the reported character-loss issue is fixed.RetriggerView in GreptileConfidence Score: 5/5

The PR appears safe to merge; the reported character-loss issue is fixed.

Summary

Drops U+FFFD-mapped Arabic justification strokes using each font's glyphs, without changing shared CMaps.

  • Arabic text drops replacement-mapped kashida stretches before decoding.

Reviews (5) · Last reviewed commit: "fix: keep a code another range maps to t..." · Reviewed by Greptile