Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Tests/test_imagetext.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,14 @@ def test_wrap_shrink() -> None:
assert text.wrap(50, 50, "shrink") is None
assert isinstance(text.font, ImageFont.FreeTypeFont)
assert text.font.size == 10
assert text.text == "Hello\nWorld!"

text = ImageText.Text("Hello World!")
with pytest.raises(ValueError, match="Text could not be scaled"):
text.wrap(50, 15, ("shrink", 9))

assert text.wrap(50, 15, "shrink") is None
assert isinstance(text.font, ImageFont.FreeTypeFont)
assert text.font.size == 8

text = ImageText.Text("Hello World!")
Expand Down
22 changes: 10 additions & 12 deletions src/PIL/ImageText.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,18 +219,16 @@ def wrap(
font = self.font
wrap = _Wrap(self, width, height, font)
if scaling == "shrink":
if not wrap.remaining_text:
return None

size = math.ceil(font.size)
while wrap.remaining_text:
if size == max(limit, 1):
msg = "Text could not be scaled"
raise ValueError(msg)
size -= 1
font = self.font.font_variant(size=size)
wrap = _Wrap(self, width, height, font)
self.font = font
if wrap.remaining_text:
size = math.ceil(font.size)
while wrap.remaining_text:
if size == max(limit, 1):
msg = "Text could not be scaled"
raise ValueError(msg)
size -= 1
font = self.font.font_variant(size=size)
wrap = _Wrap(self, width, height, font)
self.font = font
else:
if wrap.remaining_text:
msg = "Text could not be scaled"
Expand Down
Loading