Skip to content

Commit b955cee

Browse files
committed
Do not justify last line
1 parent cccc072 commit b955cee

File tree

4 files changed

+8
-5
lines changed

4 files changed

+8
-5
lines changed
3.5 KB
Loading
Binary file not shown.

Tests/test_imagefont.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -267,16 +267,19 @@ def test_render_multiline_text_align(
267267
assert_image_similar_tofile(im, f"Tests/images/multiline_text{ext}.png", 0.01)
268268

269269

270-
def test_render_multiline_text_align_justify_single_word(
270+
def test_render_multiline_text_align_justify_last_line(
271271
font: ImageFont.FreeTypeFont,
272272
) -> None:
273-
im = Image.new("RGB", (185, 65))
273+
im = Image.new("RGB", (280, 60))
274274
draw = ImageDraw.Draw(im)
275275
draw.multiline_text(
276-
(0, 0), "hey you\nyou are awesome\nthis", font=font, align="justify"
276+
(0, 0),
277+
"hey you you are awesome\nthis\nlooks awkward",
278+
font=font,
279+
align="justify",
277280
)
278281

279-
assert_image_equal_tofile(im, "Tests/images/multiline_text_justify_single_word.png")
282+
assert_image_equal_tofile(im, "Tests/images/multiline_text_justify_last_line.png")
280283

281284

282285
def test_unknown_align(font: ImageFont.FreeTypeFont) -> None:

src/PIL/ImageDraw.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -770,7 +770,7 @@ def _prepare_multiline_text(
770770
msg = 'align must be "left", "center", "right" or "justify"'
771771
raise ValueError(msg)
772772

773-
if align == "justify" and width_difference != 0:
773+
if align == "justify" and width_difference != 0 and idx != len(lines) - 1:
774774
words = line.split(" " if isinstance(text, str) else b" ")
775775
if len(words) > 1:
776776
word_widths = [

0 commit comments

Comments
 (0)