Skip to content

Commit 80a335b

Browse files
committed
table: a blank drawn under a glyph does not split the word (#5327)
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.
1 parent 8000aab commit 80a335b

2 files changed

Lines changed: 91 additions & 1 deletion

File tree

‎src/table.py‎

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -894,6 +894,50 @@ def char_begins_new_word(
894894
or (cy > ay + y)
895895
)
896896

897+
def blank_under_glyphs(self, chars: list, i: int) -> bool:
898+
"""Whether the blank `chars[i]` is drawn on top of the glyphs next to
899+
it in reading order.
900+
901+
Word processors pad table cells with runs of spaces, and such a run
902+
can reach under the text of the cell. A blank lying under a glyph
903+
separates nothing, but the chars are ordered by where they start, so
904+
it would end the word there ("600.000" read as "6 00.000"). It counts
905+
as lying under the glyphs when they cover more than half of its
906+
extent along the line. An ordinary word space sits between its
907+
neighbours and is covered by neither.
908+
909+
As in char_begins_new_word(), the attributes compared depend on the
910+
upright-ness of the chars.
911+
"""
912+
blank = chars[i]
913+
if blank["upright"]:
914+
lo, hi, line_pos, tolerance = "x0", "x1", _line_top, self.y_tolerance
915+
else:
916+
lo, hi, line_pos, tolerance = "top", "bottom", _line_x0, self.x_tolerance
917+
b0, b1 = blank[lo], blank[hi]
918+
blank_line = line_pos(blank)
919+
920+
def overlap(char) -> float:
921+
if abs(line_pos(char) - blank_line) > tolerance:
922+
return 0
923+
return min(b1, char[hi]) - max(b0, char[lo])
924+
925+
covered = 0
926+
# The nearest glyph before the blank.
927+
j = i - 1
928+
while j >= 0 and chars[j]["text"].isspace():
929+
j -= 1
930+
if j >= 0:
931+
covered += max(overlap(chars[j]), 0)
932+
# The glyphs after it, as far as they reach into the blank.
933+
for char in itertools.islice(chars, i + 1, None):
934+
part = overlap(char)
935+
if part <= 0:
936+
break
937+
if not char["text"].isspace():
938+
covered += part
939+
return covered > (b1 - b0) / 2
940+
897941
def iter_chars_to_words(self, ordered_chars):
898942
current_word: list = []
899943

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

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

908-
for char in ordered_chars:
952+
ordered_chars = list(ordered_chars)
953+
for i, char in enumerate(ordered_chars):
909954
text = char["text"]
910955

956+
if text.isspace() and self.blank_under_glyphs(ordered_chars, i):
957+
continue # not a separator: the glyphs around it decide
958+
911959
if not self.keep_blank_chars and text.isspace():
912960
yield from start_next_word(None)
913961

‎tests/test_tables.py‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2029,3 +2029,45 @@ def test_5198():
20292029
else:
20302030
assert items == [[['P for interaction', 'Cooking fuel type'], ['0.74', '12.5 mg']]]
20312031
assert items2 == ['P', 'for', 'interaction', 'Cooking', 'fuel', 'type', '0.74', '12.5', 'mg']
2032+
2033+
2034+
def test_5327():
2035+
"""A blank drawn under a glyph does not split the word in a table cell.
2036+
2037+
Word processors pad table cells with runs of spaces, and such a run can
2038+
reach under the text of the cell. The chars of a cell are ordered by
2039+
where they start, so a blank lying under the first digit of "600.000"
2040+
came after that digit and the cell was extracted as "6 00.000".
2041+
"""
2042+
fontsize = 10
2043+
blank = pymupdf.get_text_length(" ", fontsize=fontsize)
2044+
2045+
def cells(*texts):
2046+
doc = pymupdf.open()
2047+
page = doc.new_page(width=300, height=120)
2048+
x0, x1, x2, y0, y1 = 40, 140, 260, 40, 70
2049+
for p1, p2 in [
2050+
((x0, y0), (x2, y0)),
2051+
((x0, y1), (x2, y1)),
2052+
((x0, y0), (x0, y1)),
2053+
((x1, y0), (x1, y1)),
2054+
((x2, y0), (x2, y1)),
2055+
]:
2056+
page.draw_line(p1, p2)
2057+
page.insert_text((60, 60), "Pillows", fontsize=fontsize)
2058+
for x, text in texts:
2059+
page.insert_text((x, 60), text, fontsize=fontsize)
2060+
# The line-based finder: this is about the chars inside the cells.
2061+
tables = page.find_tables(use_layout=False).tables
2062+
return [cell for table in tables for row in table.extract() for cell in row]
2063+
2064+
# The last blank of the run starts 0.5 pt inside the "6".
2065+
found = cells((200, "600.000"), (200.5 - 7 * blank, " " * 8))
2066+
assert "600.000" in found, found
2067+
# The run reaches under the whole number.
2068+
found = cells((200, "600.000"), (180, " " * 20))
2069+
assert "600.000" in found, found
2070+
# A blank between two words still separates them.
2071+
width = pymupdf.get_text_length("two", fontsize=fontsize)
2072+
found = cells((160, "two"), (160 + width, " "), (160 + width + blank, "words"))
2073+
assert "two words" in found, found

0 commit comments

Comments
 (0)