Skip to content

Commit fc9671c

Browse files
committed
Added scaling argument to wrap()
1 parent e5fc2d3 commit fc9671c

File tree

3 files changed

+257
-142
lines changed

3 files changed

+257
-142
lines changed

Tests/test_imagetext.py

Lines changed: 98 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,27 +84,116 @@ def test_stroke() -> None:
8484

8585

8686
@pytest.mark.parametrize(
87-
"text, width, expected",
87+
"data, width, expected",
8888
(
8989
("Hello World!", 100, "Hello World!"), # No wrap required
9090
("Hello World!", 50, "Hello\nWorld!"), # Wrap word to a new line
91-
("Hello World!", 25, "Hello\nWorl\nd!"), # Split word across lines
9291
# Keep multiple spaces within a line
9392
("Keep multiple spaces", 75, "Keep multiple\nspaces"),
93+
(" Keep\n leading space", 100, " Keep\n leading space"),
9494
),
9595
)
9696
@pytest.mark.parametrize("string", (True, False))
97-
def test_wrap(text: str, width: int, expected: str, string: bool) -> None:
98-
text = ImageText.Text(text if string else text.encode())
99-
assert text.wrap(width) is None
100-
assert text.text == expected if string else expected.encode()
97+
def test_wrap(data: str, width: int, expected: str, string: bool) -> None:
98+
if string:
99+
text = ImageText.Text(data)
100+
assert text.wrap(width) is None
101+
assert text.text == expected
102+
else:
103+
text_bytes = ImageText.Text(data.encode())
104+
assert text_bytes.wrap(width) is None
105+
assert text_bytes.text == expected.encode()
106+
107+
108+
def test_wrap_long_word() -> None:
109+
text = ImageText.Text("Hello World!")
110+
with pytest.raises(ValueError, match="Word does not fit within line"):
111+
text.wrap(25)
112+
113+
114+
def test_wrap_unsupported(font: ImageFont.FreeTypeFont) -> None:
115+
transposed_font = ImageFont.TransposedFont(font)
116+
text = ImageText.Text("Hello World!", transposed_font)
117+
with pytest.raises(ValueError, match="TransposedFont not supported"):
118+
text.wrap(50)
119+
120+
text = ImageText.Text("Hello World!", direction="ttb")
121+
with pytest.raises(ValueError, match="Only ltr direction supported"):
122+
text.wrap(50)
101123

102124

103125
def test_wrap_height() -> None:
104126
text = ImageText.Text("Text does not fit within height")
105-
assert text.wrap(50, 25).text == " within height"
127+
wrapped = text.wrap(50, 25)
128+
assert wrapped is not None
129+
assert wrapped.text == " within height"
106130
assert text.text == "Text does\nnot fit"
107131

108-
text = ImageText.Text("Text does not fit singlelongword")
109-
assert text.wrap(50, 25).text == " singlelongword"
132+
text = ImageText.Text("Text does not fit\nwithin height")
133+
wrapped = text.wrap(50, 20)
134+
assert wrapped is not None
135+
assert wrapped.text == " not fit\nwithin height"
136+
assert text.text == "Text does"
137+
138+
text = ImageText.Text("Text does not fit\n\nwithin height")
139+
wrapped = text.wrap(50, 25)
140+
assert wrapped is not None
141+
assert wrapped.text == "\nwithin height"
110142
assert text.text == "Text does\nnot fit"
143+
144+
145+
def test_wrap_scaling_unsupported() -> None:
146+
font = ImageFont.load_default_imagefont()
147+
text = ImageText.Text("Hello World!", font)
148+
with pytest.raises(ValueError, match="'scaling' only supports FreeTypeFont"):
149+
text.wrap(50, scaling="shrink")
150+
151+
text = ImageText.Text("Hello World!")
152+
with pytest.raises(ValueError, match="'scaling' requires 'height'"):
153+
text.wrap(50, scaling="shrink")
154+
155+
156+
def test_wrap_shrink() -> None:
157+
# No scaling required
158+
text = ImageText.Text("Hello World!")
159+
assert isinstance(text.font, ImageFont.FreeTypeFont)
160+
assert text.font.size == 10
161+
assert text.wrap(50, 50, "shrink") is None
162+
assert isinstance(text.font, ImageFont.FreeTypeFont)
163+
assert text.font.size == 10
164+
165+
with pytest.raises(ValueError, match="Text could not be scaled"):
166+
text.wrap(50, 15, ("shrink", 9))
167+
168+
assert text.wrap(50, 15, "shrink") is None
169+
assert text.font.size == 8
170+
171+
text = ImageText.Text("Hello World!")
172+
assert text.wrap(50, 15, ("shrink", 7)) is None
173+
assert isinstance(text.font, ImageFont.FreeTypeFont)
174+
assert text.font.size == 8
175+
176+
177+
def test_wrap_grow() -> None:
178+
# No scaling required
179+
text = ImageText.Text("Hello World!")
180+
assert isinstance(text.font, ImageFont.FreeTypeFont)
181+
assert text.font.size == 10
182+
assert text.wrap(58, 10, "grow") is None
183+
assert isinstance(text.font, ImageFont.FreeTypeFont)
184+
assert text.font.size == 10
185+
186+
with pytest.raises(ValueError, match="Text could not be scaled"):
187+
text.wrap(50, 50, ("grow", 12))
188+
189+
assert text.wrap(50, 50, "grow") is None
190+
assert text.font.size == 16
191+
192+
text = ImageText.Text("A\nB")
193+
with pytest.raises(ValueError, match="Text could not be scaled"):
194+
text.wrap(50, 10, "grow")
195+
196+
text = ImageText.Text("Hello World!")
197+
assert text.wrap(50, 50, ("grow", 18)) is None
198+
assert isinstance(text.font, ImageFont.FreeTypeFont)
199+
assert text.font.size == 16

src/PIL/ImageDraw.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ def draw_corners(pieslice: bool) -> None:
538538
def text(
539539
self,
540540
xy: tuple[float, float],
541-
text: AnyStr | ImageText.Text,
541+
text: AnyStr | ImageText.Text[AnyStr],
542542
fill: _Ink | None = None,
543543
font: (
544544
ImageFont.ImageFont

0 commit comments

Comments
 (0)