mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-04-20 17:22:00 +03:00
Do not justify a single word
This commit is contained in:
parent
3d4119521c
commit
cccc07269a
BIN
Tests/images/multiline_text_justify_single_word.png
Normal file
BIN
Tests/images/multiline_text_justify_single_word.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 2.4 KiB |
|
@ -267,6 +267,18 @@ 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(
|
||||
font: ImageFont.FreeTypeFont,
|
||||
) -> None:
|
||||
im = Image.new("RGB", (185, 65))
|
||||
draw = ImageDraw.Draw(im)
|
||||
draw.multiline_text(
|
||||
(0, 0), "hey you\nyou are awesome\nthis", font=font, align="justify"
|
||||
)
|
||||
|
||||
assert_image_equal_tofile(im, "Tests/images/multiline_text_justify_single_word.png")
|
||||
|
||||
|
||||
def test_unknown_align(font: ImageFont.FreeTypeFont) -> None:
|
||||
im = Image.new(mode="RGB", size=(300, 100))
|
||||
draw = ImageDraw.Draw(im)
|
||||
|
|
|
@ -772,24 +772,26 @@ class ImageDraw:
|
|||
|
||||
if align == "justify" and width_difference != 0:
|
||||
words = line.split(" " if isinstance(text, str) else b" ")
|
||||
word_widths = [
|
||||
self.textlength(
|
||||
word,
|
||||
font,
|
||||
direction=direction,
|
||||
features=features,
|
||||
language=language,
|
||||
embedded_color=embedded_color,
|
||||
)
|
||||
for word in words
|
||||
]
|
||||
width_difference = max_width - sum(word_widths)
|
||||
for i, word in enumerate(words):
|
||||
parts.append(((left, top), word))
|
||||
left += word_widths[i] + width_difference / (len(words) - 1)
|
||||
else:
|
||||
parts.append(((left, top), line))
|
||||
if len(words) > 1:
|
||||
word_widths = [
|
||||
self.textlength(
|
||||
word,
|
||||
font,
|
||||
direction=direction,
|
||||
features=features,
|
||||
language=language,
|
||||
embedded_color=embedded_color,
|
||||
)
|
||||
for word in words
|
||||
]
|
||||
width_difference = max_width - sum(word_widths)
|
||||
for i, word in enumerate(words):
|
||||
parts.append(((left, top), word))
|
||||
left += word_widths[i] + width_difference / (len(words) - 1)
|
||||
top += line_spacing
|
||||
continue
|
||||
|
||||
parts.append(((left, top), line))
|
||||
top += line_spacing
|
||||
|
||||
return font, anchor, parts
|
||||
|
|
Loading…
Reference in New Issue
Block a user