Skip to content

Commit

Permalink
Merge pull request #5510 from radarhere/textbbox
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk committed Jun 10, 2021
2 parents 61b9065 + 362d504 commit a9b0adc
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
7 changes: 7 additions & 0 deletions Tests/test_imagefont.py
Expand Up @@ -719,6 +719,13 @@ def test_variation_set_by_axes(self):
font.set_variation_by_axes([100])
self._check_text(font, "Tests/images/variation_tiny_axes.png", 32.5)

def test_textbbox_non_freetypefont(self):
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)

@pytest.mark.parametrize(
"anchor, left, left_old, top",
(
Expand Down
4 changes: 3 additions & 1 deletion src/PIL/ImageDraw.py
Expand Up @@ -33,7 +33,7 @@
import math
import numbers

from . import Image, ImageColor
from . import Image, ImageColor, ImageFont

"""
A simple 2D drawing interface for PIL images.
Expand Down Expand Up @@ -646,6 +646,8 @@ def textbbox(

if font is None:
font = self.getfont()
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
Expand Down

0 comments on commit a9b0adc

Please sign in to comment.