Handle use_text_flow more consistently

As noted in #912, `use_text_flow` was not being handled consistently, as
characters and words were being re-sorted without checking first if this
parameter was set to `True`.
This commit is contained in:
Jeremy Singer-Vine
2023-07-01 18:36:59 -04:00
parent ae676aeacd
commit b1db5b840f
+16 -6
View File
@@ -145,6 +145,7 @@ class WordMap:
x_shift: T_num = 0,
y_shift: T_num = 0,
y_tolerance: T_num = DEFAULT_Y_TOLERANCE,
use_text_flow: bool = False,
presorted: bool = False,
expand_ligatures: bool = True,
) -> TextMap:
@@ -213,17 +214,19 @@ class WordMap:
num_newlines = 0
words_sorted = (
words_sorted_doctop = (
self.tuples
if presorted
else sorted(self.tuples, key=lambda x: (x[0]["doctop"], x[0]["x0"]))
if presorted or use_text_flow
else sorted(self.tuples, key=lambda x: float(x[0]["doctop"]))
)
first_word = words_sorted[0][0]
first_word = words_sorted_doctop[0][0]
doctop_start = first_word["doctop"] - first_word["top"]
for i, ws in enumerate(
cluster_objects(words_sorted, lambda x: float(x[0]["doctop"]), y_tolerance)
cluster_objects(
words_sorted_doctop, lambda x: float(x[0]["doctop"]), y_tolerance
)
):
y_dist = (
(ws[0][0]["doctop"] - (doctop_start + y_shift)) / y_density
@@ -245,7 +248,14 @@ class WordMap:
num_newlines += num_newlines_prepend
line_len = 0
for word, chars in sorted(ws, key=lambda x: float(x[0]["x0"])):
line_words_sorted_x0 = (
ws
if presorted or use_text_flow
else sorted(ws, key=lambda x: float(x[0]["x0"]))
)
for word, chars in line_words_sorted_x0:
x_dist = (word["x0"] - x_shift) / x_density if layout else 0
num_spaces_prepend = max(min(1, line_len), round(x_dist) - line_len)
_textmap += [(" ", None)] * num_spaces_prepend