mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-01-25 00:34:14 +03:00
add getbbox and getlength to basic ImageFont and update related tests
This commit is contained in:
parent
406fe59242
commit
c854bf8d1c
|
@ -1232,9 +1232,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("")
|
draw.textbbox((0, 0), "")
|
||||||
draw.textsize("\n")
|
draw.textbbox((0, 0), "\n")
|
||||||
draw.textsize("test\n")
|
draw.textbbox((0, 0), "test\n")
|
||||||
|
draw.textlength("")
|
||||||
|
|
||||||
|
|
||||||
@skip_unless_feature("freetype2")
|
@skip_unless_feature("freetype2")
|
||||||
|
@ -1245,8 +1246,25 @@ def test_textsize_stroke():
|
||||||
font = ImageFont.truetype("Tests/fonts/FreeMono.ttf", 20)
|
font = ImageFont.truetype("Tests/fonts/FreeMono.ttf", 20)
|
||||||
|
|
||||||
# Act / Assert
|
# Act / Assert
|
||||||
assert draw.textsize("A", font, stroke_width=2) == (16, 20)
|
assert draw.textbbox((2, 2), "A", font, stroke_width=2) == (0, 4, 16, 20)
|
||||||
assert draw.multiline_textsize("ABC\nAaaa", font, stroke_width=2) == (52, 44)
|
assert draw.textbbox((2, 2), "A", font, stroke_width=4) == (-2, 2, 18, 22)
|
||||||
|
assert draw.textbbox((2, 2), "ABC\nAaaa", font, stroke_width=2) == (0, 4, 52, 44)
|
||||||
|
assert draw.textbbox((2, 2), "ABC\nAaaa", font, stroke_width=4) == (-2, 2, 54, 50)
|
||||||
|
|
||||||
|
|
||||||
|
def test_textsize_deprecation():
|
||||||
|
im = Image.new("RGB", (W, H))
|
||||||
|
draw = ImageDraw.Draw(im)
|
||||||
|
|
||||||
|
with pytest.warns(DeprecationWarning) as log:
|
||||||
|
draw.textsize("Hello")
|
||||||
|
assert len(log) == 1
|
||||||
|
with pytest.warns(DeprecationWarning) as log:
|
||||||
|
draw.textsize("Hello\nWorld")
|
||||||
|
assert len(log) == 1
|
||||||
|
with pytest.warns(DeprecationWarning) as log:
|
||||||
|
draw.multiline_textsize("Hello\nWorld")
|
||||||
|
assert len(log) == 1
|
||||||
|
|
||||||
|
|
||||||
@skip_unless_feature("freetype2")
|
@skip_unless_feature("freetype2")
|
||||||
|
|
|
@ -720,8 +720,11 @@ class TestImageFont:
|
||||||
im = Image.new("RGB", (200, 200))
|
im = Image.new("RGB", (200, 200))
|
||||||
d = ImageDraw.Draw(im)
|
d = ImageDraw.Draw(im)
|
||||||
default_font = ImageFont.load_default()
|
default_font = ImageFont.load_default()
|
||||||
with pytest.raises(ValueError):
|
with pytest.warns(DeprecationWarning) as log:
|
||||||
d.textbbox((0, 0), "test", font=default_font)
|
width, height = d.textsize("test", font=default_font)
|
||||||
|
assert len(log) == 1
|
||||||
|
assert d.textlength("test", font=default_font) == width
|
||||||
|
assert d.textbbox((0, 0), "test", font=default_font) == (0, 0, width, height)
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
"anchor, left, top",
|
"anchor, left, top",
|
||||||
|
|
|
@ -709,10 +709,6 @@ class ImageDraw:
|
||||||
|
|
||||||
if font is None:
|
if font is None:
|
||||||
font = self.getfont()
|
font = self.getfont()
|
||||||
from . import ImageFont
|
|
||||||
|
|
||||||
if not isinstance(font, ImageFont.FreeTypeFont):
|
|
||||||
raise ValueError("Only supported for TrueType fonts")
|
|
||||||
mode = "RGBA" if embedded_color else self.fontmode
|
mode = "RGBA" if embedded_color else self.fontmode
|
||||||
bbox = font.getbbox(
|
bbox = font.getbbox(
|
||||||
text, mode, direction, features, language, stroke_width, anchor
|
text, mode, direction, features, language, stroke_width, anchor
|
||||||
|
|
|
@ -167,6 +167,33 @@ class ImageFont:
|
||||||
"""
|
"""
|
||||||
return self.font.getmask(text, mode)
|
return self.font.getmask(text, mode)
|
||||||
|
|
||||||
|
def getbbox(self, text, *args, **kwargs):
|
||||||
|
"""
|
||||||
|
Returns bounding box (in pixels) of given text.
|
||||||
|
|
||||||
|
.. versionadded:: 9.2.0
|
||||||
|
|
||||||
|
:param text: Text to render.
|
||||||
|
:param mode: Used by some graphics drivers to indicate what mode the
|
||||||
|
driver prefers; if empty, the renderer may return either
|
||||||
|
mode. Note that the mode is always a string, to simplify
|
||||||
|
C-level implementations.
|
||||||
|
|
||||||
|
:return: ``(left, top, right, bottom)`` bounding box
|
||||||
|
"""
|
||||||
|
width, height = self.font.getsize(text)
|
||||||
|
return 0, 0, width, height
|
||||||
|
|
||||||
|
def getlength(self, text, *args, **kwargs):
|
||||||
|
"""
|
||||||
|
Returns length (in pixels) of given text.
|
||||||
|
This is the amount by which following text should be offset.
|
||||||
|
|
||||||
|
.. versionadded:: 9.2.0
|
||||||
|
"""
|
||||||
|
width, height = self.font.getsize(text)
|
||||||
|
return width
|
||||||
|
|
||||||
|
|
||||||
##
|
##
|
||||||
# Wrapper for FreeType fonts. Application code should use the
|
# Wrapper for FreeType fonts. Application code should use the
|
||||||
|
|
Loading…
Reference in New Issue
Block a user