Skip to content

Commit 11d599c

Browse files
committed
Added documentation
1 parent 4b2d481 commit 11d599c

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

docs/releasenotes/12.1.0.rst

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,31 @@ TODO
4141
API additions
4242
=============
4343

44+
ImageText.Text.wrap
45+
^^^^^^^^^^^^^^^^^^^
46+
47+
:py:meth:`.ImageText.Text.wrap` has been added, to wrap text to fit within a given
48+
width::
49+
50+
from PIL import ImageText
51+
text = ImageText.Text("Hello World!")
52+
text.wrap(50)
53+
print(text.text) # "Hello\nWorld!"
54+
55+
or within a certain width and height, returning a new :py:class:`.ImageText.Text`
56+
instance if the text does not fit::
57+
58+
text = ImageText.Text("Text does not fit within height")
59+
print(text.wrap(50, 25).text == " within height")
60+
print(text.text) # "Text does\nnot fit"
61+
62+
or scaling, optionally with a font size limit::
63+
64+
text.wrap(50, 15, "shrink")
65+
text.wrap(50, 15, ("shrink", 7))
66+
text.wrap(58, 10, "grow")
67+
text.wrap(50, 50, ("grow", 12))
68+
4469
Specify window in ImageGrab on macOS
4570
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4671

src/PIL/ImageText.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,19 @@ def wrap(
178178
height: int | None = None,
179179
scaling: str | tuple[str, int] | None = None,
180180
) -> Text[AnyStr] | None:
181+
"""
182+
Wrap text to fit within a given width.
183+
184+
:param width: The width to fit within.
185+
:param height: An optional height limit. Any text that does not fit within this
186+
will be returned as a new :py:class:`.Text` object.
187+
:param scaling: An optional directive to scale the text, either "grow" as much
188+
as possible within the given dimensions, or "shrink" until it
189+
fits. It can also be a tuple of (direction, limit), with an
190+
integer limit to stop scaling at.
191+
192+
:returns: An :py:class:`.Text` object, or None.
193+
"""
181194
if isinstance(self.font, ImageFont.TransposedFont):
182195
msg = "TransposedFont not supported"
183196
raise ValueError(msg)

0 commit comments

Comments
 (0)