mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-01-24 16:24:11 +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
|
||||
# Should not cause 'SystemError: <built-in method getsize of
|
||||
# ImagingFont object at 0x...> returned NULL without setting an error'
|
||||
draw.textsize("")
|
||||
draw.textsize("\n")
|
||||
draw.textsize("test\n")
|
||||
draw.textbbox((0, 0), "")
|
||||
draw.textbbox((0, 0), "\n")
|
||||
draw.textbbox((0, 0), "test\n")
|
||||
draw.textlength("")
|
||||
|
||||
|
||||
@skip_unless_feature("freetype2")
|
||||
|
@ -1245,8 +1246,25 @@ def test_textsize_stroke():
|
|||
font = ImageFont.truetype("Tests/fonts/FreeMono.ttf", 20)
|
||||
|
||||
# Act / Assert
|
||||
assert draw.textsize("A", font, stroke_width=2) == (16, 20)
|
||||
assert draw.multiline_textsize("ABC\nAaaa", font, stroke_width=2) == (52, 44)
|
||||
assert draw.textbbox((2, 2), "A", font, stroke_width=2) == (0, 4, 16, 20)
|
||||
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")
|
||||
|
|
|
@ -720,8 +720,11 @@ class TestImageFont:
|
|||
im = Image.new("RGB", (200, 200))
|
||||
d = ImageDraw.Draw(im)
|
||||
default_font = ImageFont.load_default()
|
||||
with pytest.raises(ValueError):
|
||||
d.textbbox((0, 0), "test", font=default_font)
|
||||
with pytest.warns(DeprecationWarning) as log:
|
||||
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(
|
||||
"anchor, left, top",
|
||||
|
|
|
@ -709,10 +709,6 @@ class ImageDraw:
|
|||
|
||||
if font is None:
|
||||
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
|
||||
bbox = font.getbbox(
|
||||
text, mode, direction, features, language, stroke_width, anchor
|
||||
|
|
|
@ -167,6 +167,33 @@ class ImageFont:
|
|||
"""
|
||||
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
|
||||
|
|
Loading…
Reference in New Issue
Block a user