Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 49 additions & 1 deletion src/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -894,6 +894,50 @@ def char_begins_new_word(
or (cy > ay + y)
)

def _blank_under_glyphs(self, chars: list, i: int) -> bool:
"""Whether the blank `chars[i]` is drawn on top of the glyphs next to
it in reading order.

Word processors pad table cells with runs of spaces, and such a run
can reach under the text of the cell. A blank lying under a glyph
separates nothing, but the chars are ordered by where they start, so
it would end the word there ("600.000" read as "6 00.000"). It counts
as lying under the glyphs when they cover more than half of its
extent along the line. An ordinary word space sits between its
neighbours and is covered by neither.

As in char_begins_new_word(), the attributes compared depend on the
upright-ness of the chars.
"""
blank = chars[i]
if blank["upright"]:
lo, hi, line_pos, tolerance = "x0", "x1", _line_top, self.y_tolerance
else:
lo, hi, line_pos, tolerance = "top", "bottom", _line_x0, self.x_tolerance
b0, b1 = blank[lo], blank[hi]
blank_line = line_pos(blank)

def overlap(char) -> float:
if abs(line_pos(char) - blank_line) > tolerance:
return 0
return min(b1, char[hi]) - max(b0, char[lo])

covered = 0
# The nearest glyph before the blank.
j = i - 1
while j >= 0 and chars[j]["text"].isspace():
j -= 1
if j >= 0:
covered += max(overlap(chars[j]), 0)
# The glyphs after it, as far as they reach into the blank.
for char in itertools.islice(chars, i + 1, None):
part = overlap(char)
if part <= 0:
break
if not char["text"].isspace():
covered += part
return covered > (b1 - b0) / 2

def iter_chars_to_words(self, ordered_chars):
current_word: list = []

Expand All @@ -905,9 +949,13 @@ def start_next_word(new_char=None):

current_word = [] if new_char is None else [new_char]

for char in ordered_chars:
ordered_chars = list(ordered_chars)
for i, char in enumerate(ordered_chars):
text = char["text"]

if text.isspace() and self._blank_under_glyphs(ordered_chars, i):
continue # not a separator: the glyphs around it decide

if not self.keep_blank_chars and text.isspace():
yield from start_next_word(None)

Expand Down
42 changes: 42 additions & 0 deletions tests/test_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -2029,3 +2029,45 @@ def test_5198():
else:
assert items == [[['P for interaction', 'Cooking fuel type'], ['0.74', '12.5 mg']]]
assert items2 == ['P', 'for', 'interaction', 'Cooking', 'fuel', 'type', '0.74', '12.5', 'mg']


def test_5327():
"""A blank drawn under a glyph does not split the word in a table cell.

Word processors pad table cells with runs of spaces, and such a run can
reach under the text of the cell. The chars of a cell are ordered by
where they start, so a blank lying under the first digit of "600.000"
came after that digit and the cell was extracted as "6 00.000".
"""
fontsize = 10
blank = pymupdf.get_text_length(" ", fontsize=fontsize)

def cells(*texts):
doc = pymupdf.open()
page = doc.new_page(width=300, height=120)
x0, x1, x2, y0, y1 = 40, 140, 260, 40, 70
for p1, p2 in [
((x0, y0), (x2, y0)),
((x0, y1), (x2, y1)),
((x0, y0), (x0, y1)),
((x1, y0), (x1, y1)),
((x2, y0), (x2, y1)),
]:
page.draw_line(p1, p2)
page.insert_text((60, 60), "Pillows", fontsize=fontsize)
for x, text in texts:
page.insert_text((x, 60), text, fontsize=fontsize)
# The line-based finder: this is about the chars inside the cells.
tables = page.find_tables(use_layout=False).tables
return [cell for table in tables for row in table.extract() for cell in row]

# The last blank of the run starts 0.5 pt inside the "6".
found = cells((200, "600.000"), (200.5 - 7 * blank, " " * 8))
assert "600.000" in found, found
# The run reaches under the whole number.
found = cells((200, "600.000"), (180, " " * 20))
assert "600.000" in found, found
# A blank between two words still separates them.
width = pymupdf.get_text_length("two", fontsize=fontsize)
found = cells((160, "two"), (160 + width, " "), (160 + width + blank, "words"))
assert "two words" in found, found