Repository navigation
fix(font): validate ToUnicode against embedded TrueType cmap - #16
AhmedMaherTohmay wants to merge 1 commit into
Conversation
Read FontFile2 and CIDToGIDMap so composite fonts can provide renderer-backed glyph identity when a PDF's ToUnicode map is wrong or absent. Override only single-character mappings with a genuine NFKC disagreement, preserve multi-character ligatures and correctly encoded Persian yeh, and mark heavily disputed pages degraded. Drop the observed narrow alef filler only when a usable cmap has already demonstrated that the producer's map is contradictory. Harden TrueType cmap parsing and CID map bounds, add focused unit and real-document regressions, and record the evidence in the findings log.
|
Hi @AhmedMaherTohmay I am really sorry, I want be able to merge this pull request, as I mentioned in the issue, that I am gonna work on this issue, as it's would affect the whole pipeline of the project. From the first point I've assumed that all Arabic Pdf, is Type0 Font, and with you file we should now handel TrueType font, this will affect almost all files in the project, and that why I say I want to work on it, but I really appriciate your effort, and the document you provide change a lot. I would invite you to build the comman pdf folder that would be our base benchmark for upgrading this project, I will also share all my test files I've used it to build the system. |
|
No worries at all. |
|
Thanks @AhmedMaherTohmay I really appreciate you support, I will mention you as soon as possible when gonna work on the test data. Thanks again brother |
Linked issue or discussion
Closes #13
What this changes
Some digitally born Arabic PDFs contain a valid-looking but incorrect
/ToUnicodemap. Because the mapped characters are valid Unicode, the previous pipeline could return plausible-looking but incorrect text without detecting the corruption.This PR adds an independent source of glyph identity for composite TrueType fonts:
/FontFile2programs;/CIDToGIDMapfor CID-to-glyph resolution;cmapformats 0, 4, 6, and 12;/ToUnicodemappings with the embedded font identity;degraded;The implementation includes focused unit tests and named real-document regression tests.
Output changes
Yes. This fixes silent corruption in Arabic PDFs whose
/ToUnicodemaps disagree with their embedded TrueType fonts.doc3.pdf, page 1Before:
After:
The corrected output also includes:
A page with widespread disagreement is now reported as
degraded, reflecting that its text was recovered by rejecting many of the PDF producer’s own/ToUnicodeclaims.doc3.pdfdemonstrate the issue and can be provided for corpus verification if their redistribution is permitted.Testing
/CIDToGIDMapbounds./ToUnicodemappings and filler glyph.Corpus request
This behavior was discovered using real Arabic government PDFs. Since font behavior varies significantly between PDF producers, expanding the corpus with more redistributable Arabic PDFs would make these decisions safer.
Useful additions would include:
/ToUnicodemaps;A broader corpus will help ensure that a fix for one producer does not silently damage another.
Checklist
approvedlabelfix/13-tounicode-font-arbitrationcargo testpassescargo clippy --all-targetsandcargo fmt --checkare clean