Skip to content

Commit 0e40a51

Browse files
committedMar 25, 2025
src/ tests/: avoid infinite loop in fill_textbox() if word is too long.
Addresses #4400.
1 parent 574c742 commit 0e40a51

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed
 

‎src/utils.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -4610,12 +4610,12 @@ def output_justify(start, line):
46104610
words = line.split(" ") # the words in the line
46114611

46124612
# cut in parts any words that are longer than rect width
4613-
words, word_lengths = norm_words(std_width, words)
4613+
words, word_lengths = norm_words(width, words)
46144614

46154615
n = len(words)
46164616
while True:
46174617
line0 = " ".join(words[:n])
4618-
wl = sum(word_lengths[:n]) + space_len * (len(word_lengths[:n]) - 1)
4618+
wl = sum(word_lengths[:n]) + space_len * (n - 1)
46194619
if wl <= width:
46204620
new_lines.append((line0, wl))
46214621
words = words[n:]
@@ -4627,6 +4627,7 @@ def output_justify(start, line):
46274627

46284628
if len(words) == 0:
46294629
break
4630+
assert n
46304631

46314632
# -------------------------------------------------------------------------
46324633
# List of lines created. Each item is (text, tl), where 'tl' is the PDF

‎tests/test_general.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1669,4 +1669,4 @@ def test_3886():
16691669
rms_0 = gentle_compare.pixmaps_rms(pixmap, pixmap_clean0)
16701670
rms_1 = gentle_compare.pixmaps_rms(pixmap, pixmap_clean1)
16711671
print(f'test_3886(): {rms_0=} {rms_1=}')
1672-
1672+

‎tests/test_textbox.py

+9
Original file line numberDiff line numberDiff line change
@@ -280,3 +280,12 @@ def test_3916():
280280
page = doc.new_page()
281281
spare_height, scale = page.insert_htmlbox(rect, "Hello, World!", scale_low=0.5)
282282
assert spare_height == -1
283+
284+
285+
def test_4400():
286+
with pymupdf.open() as document:
287+
page = document.new_page()
288+
writer = pymupdf.TextWriter(page.rect)
289+
text = '111111111'
290+
print(f'Calling writer.fill_textbox().', flush=1)
291+
writer.fill_textbox(rect=pymupdf.Rect(0, 0, 100, 20), pos=(80, 0), text=text, fontsize=8)

0 commit comments

Comments
 (0)