Skip to content

Commit e5fc2d3

Browse files
committed
Added height argument to wrap()
1 parent fb90f95 commit e5fc2d3

File tree

3 files changed

+196
-104
lines changed

3 files changed

+196
-104
lines changed

Tests/test_imagetext.py

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -83,18 +83,28 @@ def test_stroke() -> None:
8383
)
8484

8585

86-
def test_wrap() -> None:
87-
# No wrap required
88-
text = ImageText.Text("Hello World!")
89-
text.wrap(100)
90-
assert text.text == "Hello World!"
91-
92-
# Wrap word to a new line
93-
text = ImageText.Text("Hello World!")
94-
text.wrap(50)
95-
assert text.text == "Hello\nWorld!"
96-
97-
# Split word across lines
98-
text = ImageText.Text("Hello World!")
99-
text.wrap(25)
100-
assert text.text == "Hello\nWorl\nd!"
86+
@pytest.mark.parametrize(
87+
"text, width, expected",
88+
(
89+
("Hello World!", 100, "Hello World!"), # No wrap required
90+
("Hello World!", 50, "Hello\nWorld!"), # Wrap word to a new line
91+
("Hello World!", 25, "Hello\nWorl\nd!"), # Split word across lines
92+
# Keep multiple spaces within a line
93+
("Keep multiple spaces", 75, "Keep multiple\nspaces"),
94+
),
95+
)
96+
@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()
101+
102+
103+
def test_wrap_height() -> None:
104+
text = ImageText.Text("Text does not fit within height")
105+
assert text.wrap(50, 25).text == " within height"
106+
assert text.text == "Text does\nnot fit"
107+
108+
text = ImageText.Text("Text does not fit singlelongword")
109+
assert text.wrap(50, 25).text == " singlelongword"
110+
assert text.text == "Text does\nnot fit"

src/PIL/ImageDraw.py

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -591,63 +591,62 @@ def getink(fill: _Ink | None) -> int:
591591
else ink
592592
)
593593

594-
for xy, anchor, line in image_text._split(xy, anchor, align):
594+
for line in image_text._split(xy, anchor, align):
595595

596596
def draw_text(ink: int, stroke_width: float = 0) -> None:
597597
mode = self.fontmode
598598
if stroke_width == 0 and embedded_color:
599599
mode = "RGBA"
600-
coord = []
601-
for i in range(2):
602-
coord.append(int(xy[i]))
603-
start = (math.modf(xy[0])[0], math.modf(xy[1])[0])
600+
x = int(line.x)
601+
y = int(line.y)
602+
start = (math.modf(line.x)[0], math.modf(line.y)[0])
604603
try:
605604
mask, offset = image_text.font.getmask2( # type: ignore[union-attr,misc]
606-
line,
605+
line.text,
607606
mode,
608607
direction=direction,
609608
features=features,
610609
language=language,
611610
stroke_width=stroke_width,
612611
stroke_filled=True,
613-
anchor=anchor,
612+
anchor=line.anchor,
614613
ink=ink,
615614
start=start,
616615
*args,
617616
**kwargs,
618617
)
619-
coord = [coord[0] + offset[0], coord[1] + offset[1]]
618+
x += offset[0]
619+
y += offset[1]
620620
except AttributeError:
621621
try:
622622
mask = image_text.font.getmask( # type: ignore[misc]
623-
line,
623+
line.text,
624624
mode,
625625
direction,
626626
features,
627627
language,
628628
stroke_width,
629-
anchor,
629+
line.anchor,
630630
ink,
631631
start=start,
632632
*args,
633633
**kwargs,
634634
)
635635
except TypeError:
636-
mask = image_text.font.getmask(line)
636+
mask = image_text.font.getmask(line.text)
637637
if mode == "RGBA":
638638
# image_text.font.getmask2(mode="RGBA")
639639
# returns color in RGB bands and mask in A
640640
# extract mask and set text alpha
641641
color, mask = mask, mask.getband(3)
642642
ink_alpha = struct.pack("i", ink)[3]
643643
color.fillband(3, ink_alpha)
644-
x, y = coord
645644
if self.im is not None:
646645
self.im.paste(
647646
color, (x, y, x + mask.size[0], y + mask.size[1]), mask
648647
)
649648
else:
650-
self.draw.draw_bitmap(coord, mask, ink)
649+
self.draw.draw_bitmap((x, y), mask, ink)
651650

652651
if stroke_ink is not None:
653652
# Draw stroked text

0 commit comments

Comments
 (0)