Do not justify last line

This commit is contained in:
Andrew Murray 2025-04-17 19:36:52 +10:00
parent cccc07269a
commit b955cee725
4 changed files with 8 additions and 5 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.5 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.4 KiB

View File

@ -267,16 +267,19 @@ def test_render_multiline_text_align(
assert_image_similar_tofile(im, f"Tests/images/multiline_text{ext}.png", 0.01)
def test_render_multiline_text_align_justify_single_word(
def test_render_multiline_text_align_justify_last_line(
font: ImageFont.FreeTypeFont,
) -> None:
im = Image.new("RGB", (185, 65))
im = Image.new("RGB", (280, 60))
draw = ImageDraw.Draw(im)
draw.multiline_text(
(0, 0), "hey you\nyou are awesome\nthis", font=font, align="justify"
(0, 0),
"hey you you are awesome\nthis\nlooks awkward",
font=font,
align="justify",
)
assert_image_equal_tofile(im, "Tests/images/multiline_text_justify_single_word.png")
assert_image_equal_tofile(im, "Tests/images/multiline_text_justify_last_line.png")
def test_unknown_align(font: ImageFont.FreeTypeFont) -> None:

View File

@ -770,7 +770,7 @@ class ImageDraw:
msg = 'align must be "left", "center", "right" or "justify"'
raise ValueError(msg)
if align == "justify" and width_difference != 0:
if align == "justify" and width_difference != 0 and idx != len(lines) - 1:
words = line.split(" " if isinstance(text, str) else b" ")
if len(words) > 1:
word_widths = [