mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-05-05 00:13:42 +03:00
add textbbox and textlength to ImageDraw2 and update tests
This commit is contained in:
parent
f34a6460ef
commit
1bf87556ef
|
@ -1239,7 +1239,7 @@ def test_textsize_empty_string():
|
||||||
|
|
||||||
|
|
||||||
@skip_unless_feature("freetype2")
|
@skip_unless_feature("freetype2")
|
||||||
def test_textsize_stroke():
|
def test_textbbox_stroke():
|
||||||
# Arrange
|
# Arrange
|
||||||
im = Image.new("RGB", (W, H))
|
im = Image.new("RGB", (W, H))
|
||||||
draw = ImageDraw.Draw(im)
|
draw = ImageDraw.Draw(im)
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
import os.path
|
import os.path
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
from PIL import Image, ImageDraw, ImageDraw2
|
from PIL import Image, ImageDraw, ImageDraw2
|
||||||
|
|
||||||
from .helper import (
|
from .helper import (
|
||||||
|
@ -205,7 +207,9 @@ def test_textsize():
|
||||||
font = ImageDraw2.Font("white", FONT_PATH)
|
font = ImageDraw2.Font("white", FONT_PATH)
|
||||||
|
|
||||||
# Act
|
# Act
|
||||||
|
with pytest.warns(DeprecationWarning) as log:
|
||||||
size = draw.textsize("ImageDraw2", font)
|
size = draw.textsize("ImageDraw2", font)
|
||||||
|
assert len(log) == 1
|
||||||
|
|
||||||
# Assert
|
# Assert
|
||||||
assert size[1] == 12
|
assert size[1] == 12
|
||||||
|
@ -221,9 +225,10 @@ def test_textsize_empty_string():
|
||||||
# Act
|
# Act
|
||||||
# Should not cause 'SystemError: <built-in method getsize of
|
# Should not cause 'SystemError: <built-in method getsize of
|
||||||
# ImagingFont object at 0x...> returned NULL without setting an error'
|
# ImagingFont object at 0x...> returned NULL without setting an error'
|
||||||
draw.textsize("", font)
|
draw.textbbox((0, 0), "", font)
|
||||||
draw.textsize("\n", font)
|
draw.textbbox((0, 0), "\n", font)
|
||||||
draw.textsize("test\n", font)
|
draw.textbbox((0, 0), "test\n", font)
|
||||||
|
draw.textlength("", font)
|
||||||
|
|
||||||
|
|
||||||
@skip_unless_feature("freetype2")
|
@skip_unless_feature("freetype2")
|
||||||
|
|
|
@ -25,6 +25,7 @@
|
||||||
|
|
||||||
|
|
||||||
from . import Image, ImageColor, ImageDraw, ImageFont, ImagePath
|
from . import Image, ImageColor, ImageDraw, ImageFont, ImagePath
|
||||||
|
from ._deprecate import deprecate
|
||||||
|
|
||||||
|
|
||||||
class Pen:
|
class Pen:
|
||||||
|
@ -176,4 +177,27 @@ class Draw:
|
||||||
|
|
||||||
.. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.textsize`
|
.. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.textsize`
|
||||||
"""
|
"""
|
||||||
return self.draw.textsize(text, font=font.font)
|
deprecate("textsize", 10, "textbbox or textlength")
|
||||||
|
return self.draw.textsize(text, font=font.font, __internal__=True)
|
||||||
|
|
||||||
|
def textbbox(self, xy, text, font):
|
||||||
|
"""
|
||||||
|
Returns bounding box (in pixels) of given text.
|
||||||
|
|
||||||
|
:return: ``(left, top, right, bottom)`` bounding box
|
||||||
|
|
||||||
|
.. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.textbbox`
|
||||||
|
"""
|
||||||
|
if self.transform:
|
||||||
|
xy = ImagePath.Path(xy)
|
||||||
|
xy.transform(self.transform)
|
||||||
|
return self.draw.textbbox(xy, text, font=font.font)
|
||||||
|
|
||||||
|
def textlength(self, text, font):
|
||||||
|
"""
|
||||||
|
Returns length (in pixels) of given text.
|
||||||
|
This is the amount by which following text should be offset.
|
||||||
|
|
||||||
|
.. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.textlength`
|
||||||
|
"""
|
||||||
|
return self.draw.textlength(text, font=font.font)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user