Skip to content

table: a blank drawn under a glyph does not split the word (#5327) - #5328

Open
asgeirtj wants to merge 1 commit into
pymupdf:mainfrom
asgeirtj:table-blank-under-glyph
Open

asgeirtj wants to merge 1 commit into
pymupdf:mainfrom
asgeirtj:table-blank-under-glyph

Conversation

@asgeirtj

@asgeirtj asgeirtj commented Dec 3, 2026

Copy link
Copy Markdown

Fixes #5327.

Table.extract() returns 6 00.000 for a cell holding 600.000 when the cell is padded with spaces and the last space of the run lies under the first digit. With the run under the whole number it returns 6 0 0 . 0 0 0.

Cause. Since 47e52ae (2.1) the line-based finder builds its chars without TEXT_ACCURATE_BBOXES when layout is off, so blanks are in the char list. WordExtractor orders the chars of a line by where they start and ends a word at every blank. A blank lying under a glyph is ordered after that glyph, so the word ends there.

Fix. WordExtractor.iter_chars_to_words() skips a blank when the glyphs next to it (the nearest one before it and those that reach into it after it) cover more than half of its extent along the line. The gap between the glyphs then decides about the word break through char_begins_new_word(), as it did when blanks were not in the list. An ordinary word space lies between its neighbours, is covered by neither and still ends the word. The check follows char_begins_new_word() for non-upright chars.

Tests. test_5327() builds the page in memory and covers three placements: the run ending under the first digit, the run under the whole number, and a blank between two words drawn as separate spans. It uses find_tables(use_layout=False); with layout on, the layout model finds no table on that small page.

  • pymupdf-lite 2.2 wheel with this table.py: tests/test_tables.py 58 passed (PYMUPDF_TEST_USE_LAYOUT=0). On the unpatched file test_5327 fails with ['Pillows', '6 00.000'].
  • pymupdf 2.2 with layout on (Python 3.13): test_5327 passes with and without the change, since blanks are not in the char list there.
  • 401 real PDFs (court rulings, 455 tables, 21,668 cells) extracted with both files: 15 cells differ, all in the table the bug came from, and each differs only by the spaces removed.

@asgeirtj

asgeirtj commented Dec 3, 2026

Copy link
Copy Markdown
Author

I have read the CLA Document and I hereby sign the CLA

@asgeirtj

asgeirtj commented Dec 3, 2026

Copy link
Copy Markdown
Author

Two additions: a picture of the case, and a second possible fix that I measured against this one.

The real table. It is from a public court ruling, a PDF written by Microsoft Word 2016. Red boxes are the blanks Word pads the cell with, blue boxes are the glyphs, both as page.get_text("rawdict") reports them. In the first three rows the last blank lies on the first digit; in the fourth the run ends where the number starts, and that cell was always extracted correctly.

padding blanks lying under the first digit

The bug is older than 2.1. Same repro as in #5327:

Version TEXT_ACCURATE_BBOXES in the table text flags Cell
1.24.14, 1.25.5, 1.26.4, 1.26.5 no 6 00.000
1.26.6, 1.27.2, 1.28.2, 2.0 yes (since 1c60c20) 600.000
2.1, 2.2 with layout off no (since 47e52ae) 6 00.000

So WordExtractor has always split a word at a blank lying under a glyph. The flag kept blanks out of the char list from 1.26.6 to 2.0 and that hid it.

The other fix: textpage_flags() always returns the flag. I ran it on the 2.2 wheel (layout off) next to this PR:

tests/test_tables.py 21,668 cells of 455 tables in 401 PDFs Time for those PDFs
2.2 57 passed, test_5327 fails reference 175 s
2.2 + this PR 58 passed 15 cells differ, all in the table above, each by the removed blank not timed
2.2 + flag always on 58 passed the same 15 cells; no cell differs from this PR's output 172 s

Both give the same result on everything I have. I kept the PR in WordExtractor because 47e52ae took the flag out on purpose for the non-layout path and I do not know what depends on that; the change here works with either setting and is a no-op when the flag is on. If you prefer the one-line flag change, I will switch the PR to it and keep the test.

@ilgeunben ilgeunben left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks — automated signature comparison of the changed source files with the docs (source parsed, not executed) (no AI).

Introduced by this PR:

  1. `` – WordExtractor: new public class `WordExtractor` (1 methods: blank_under_glyphs) is not documented
    Suggested docs change:
    docs/<new page>.rst (and add it to a toctree)
    .. class:: WordExtractor()
    
       * New in v<next>
    
       .. method:: blank_under_glyphs(chars, i)
       
          :arg chars: TODO
          :arg i: TODO
       
          * New in v<next>

)

Word processors pad table cells with runs of spaces, and such a run can
reach under the text of the cell. Since 2.1 the line-based finder builds
its chars without TEXT_ACCURATE_BBOXES when layout is off, so these blanks
are in the char list. WordExtractor orders the chars of a line by where
they start and ends a word at every blank, so a blank lying under the
first digit of "600.000" ended the word after that digit and
Table.extract() returned "6 00.000". With the run under the whole number
it returned "6 0 0 . 0 0 0".

WordExtractor now skips a blank when the glyphs next to it cover more
than half of its extent along the line. The gap between the glyphs then
decides about the word break, as it did when blanks were not in the list.
An ordinary word space lies between its neighbours and is not affected.
@asgeirtj

asgeirtj commented Dec 3, 2026

Copy link
Copy Markdown
Author

Thanks for the check. WordExtractor is not new: it is on main at src/table.py line 746, and like the rest of the table internals it has no docs page. What this PR added to it is one helper method, called only from iter_chars_to_words.

To leave no public name behind, I renamed it to _blank_under_glyphs and force-pushed (still one commit; tests/test_tables.py: 58 passed). With that, this PR introduces nothing that needs documenting.

@asgeirtj
asgeirtj force-pushed the table-blank-under-glyph branch from 80a335b to 8abcd2c Compare December 3, 2026 09:24
@asgeirtj

asgeirtj commented Dec 3, 2026

Copy link
Copy Markdown
Author

A note on the red CLA check, since it is not specific to this PR: the CLA Assistant workflow has failed at startup on every run since 2026-12-02 22:52 UTC, including the two runs on the jules branch (22:52 and 09:15). It last succeeded at 22:29.

The run page gives the reason (run 47075738997, Annotations):

GitHub restricts pull_request_target on public repositories by default. To allow the event trigger, configure an Actions policy. Learn more: https://gh.io/securely-using-pull_request_target#default-policy-for-pull_request_target

.github/workflows/cla.yml has not changed since 3 November, so this is the new GitHub default taking effect for the repository, and it needs an Actions policy set by a repository admin. Until then no CLA signature is recorded; mine is in the comment above and I am happy to re-post it once the workflow runs again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2.1: Table.extract() splits a number where padding spaces lie under it ("600.000" comes back as "6 00.000") — regression from 2.0

2 participants