Repository navigation
fix: preserve zero-width characters and ligatures in table cell extraction - #5209
Open
ManojDevarajulu wants to merge 2 commits into
Open
ManojDevarajulu wants to merge 2 commits into
ManojDevarajulu wants to merge 2 commits into
Conversation
When quad corrections are unset (e.g. in the table layout path or when importing pymupdf4llm), expanded ligatures and combining marks receive zero-width bounding boxes. In extract_cells, the 50% bbox area overlap check evaluates to 0 > 0 (False), silently dropping the second character of ligatures (e.g. 'fine' -> 'fne'). This commit: - Pre-allocates cell_rect once per extract_cells call. - Falls back to testing character origin inside cell_rect when bbox is empty. - Adds a regression test in tests/test_5185.py covering zero-width ligature retention and out-of-cell exclusion. Fixes pymupdf#5185
Contributor
|
All contributors have signed the CLA ✍️ ✅ |
Author
|
I have read the CLA Document and I hereby sign the CLA |
Author
|
recheck |
Author
|
Hey @ToukoUrsin @JorjMcKie, just checking in on this one whenever you get some time to take a look. Let me know if anything needs changing! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #5185.
Supersedes #5186 (adds automated regression test, avoids repeated object allocation in the character loop).
Problem
When quad corrections are unset (e.g. in the table layout path or when importing
pymupdf4llm, which callsTOOLS.unset_quad_corrections(True)), expanded ligatures (fi,fl,ff,tt) and combining marks receive a zero-width bounding box for subsequent characters.In
extract_cells, character inclusion is evaluated using:For zero-area bounding boxes (
abs(bbox) == 0.0),0.0 > 0.0evaluates toFalse. As a result, the second character of ligatures is silently dropped from table cells (fine-tuning->fne-tuning,figures->fgures,different->diferent).Note: Upstream main now triggers this even without
pymupdf4llmbecause the latest source unsets quad corrections in the table layout path.Solution
bbox.is_empty(width or height <= 0), test whether the character'soriginpoint lies inside the cell.cell_rect = pymupdf.Rect(cell)once perextract_cellscall instead of re-instantiatingpymupdf.Rect(cell)repeatedly inside the character loop.tests/test_5185.pyverifying that zero-width ligature characters inside the cell are preserved ("fine") and zero-width characters outside the cell are excluded.Verification
tests/test_5185.py: passed.2203.07436v4.pdf, p.44): restored 5xfine-tuning(previously extracted asfne-tuning).tests/test_tables.py): 46 passed.