From 253f9cd20a849b5de23eeb69235d344f073b676b Mon Sep 17 00:00:00 2001 From: ellacroi <57907121+ellacroix@users.noreply.github.com> Date: Thu, 6 Jul 2023 14:45:43 +0200 Subject: [PATCH] Update fill_textbox to better respect rect.width The function norm_words in fill_textbox had a bug in its last loop, appending n+1 characters when actually measuring width of n characters. It led to a bug in fill_texbox when you tried to write a single word mostly composed of "wide" letters (M,m, W, w...), causing the written text to exceed the given rect. The fix was just to replace n+1 by n. --- fitz/utils.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/fitz/utils.py b/fitz/utils.py index acd028cf2..f91712d18 100644 --- a/fitz/utils.py +++ b/fitz/utils.py @@ -4356,10 +4356,10 @@ def norm_words(width, words): while n > 0: wl = sum(wl_lst[:n]) if wl <= width: - nwords.append(w[: n + 1]) + nwords.append(w[:n]) word_lengths.append(wl) - w = w[n + 1 :] - wl_lst = wl_lst[n + 1 :] + w = w[n:] + wl_lst = wl_lst[n:] n = len(wl_lst) else: n -= 1