Skip to content

Commit

Permalink
Merge pull request #6381 from nulano/deprecate-getsize
Browse files Browse the repository at this point in the history
Deprecate ImageFont.getsize and related functions
  • Loading branch information
mergify[bot] committed Jul 1, 2022
2 parents 79329fb + 8a6050e commit 488589b
Show file tree
Hide file tree
Showing 15 changed files with 408 additions and 100 deletions.
Binary file modified Tests/images/rectangle_surrounding_text.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions Tests/oss-fuzz/fuzzers.py
Expand Up @@ -33,9 +33,9 @@ def fuzz_font(data):
# different font objects.
return

font.getsize_multiline("ABC\nAaaa")
font.getbbox("ABC")
font.getmask("test text")
with Image.new(mode="RGBA", size=(200, 200)) as im:
draw = ImageDraw.Draw(im)
draw.multiline_textsize("ABC\nAaaa", font, stroke_width=2)
draw.multiline_textbbox((10, 10), "ABC\nAaaa", font, stroke_width=2)
draw.text((10, 10), "Test Text", font=font, fill="#000")
11 changes: 9 additions & 2 deletions Tests/test_font_pcf.py
Expand Up @@ -76,12 +76,19 @@ def test_textsize(request, tmp_path):
tempname = save_font(request, tmp_path)
font = ImageFont.load(tempname)
for i in range(255):
(dx, dy) = font.getsize(chr(i))
(ox, oy, dx, dy) = font.getbbox(chr(i))
assert ox == 0
assert oy == 0
assert dy == 20
assert dx in (0, 10)
assert font.getlength(chr(i)) == dx
with pytest.warns(DeprecationWarning) as log:
assert font.getsize(chr(i)) == (dx, dy)
assert len(log) == 1
for i in range(len(message)):
msg = message[: i + 1]
assert font.getsize(msg) == (len(msg) * 10, 20)
assert font.getlength(msg) == len(msg) * 10
assert font.getbbox(msg) == (0, 0, len(msg) * 10, 20)


def _test_high_characters(request, tmp_path, message):
Expand Down
8 changes: 6 additions & 2 deletions Tests/test_font_pcf_charsets.py
Expand Up @@ -101,13 +101,17 @@ def _test_textsize(request, tmp_path, encoding):
tempname = save_font(request, tmp_path, encoding)
font = ImageFont.load(tempname)
for i in range(255):
(dx, dy) = font.getsize(bytearray([i]))
(ox, oy, dx, dy) = font.getbbox(bytearray([i]))
assert ox == 0
assert oy == 0
assert dy == 20
assert dx in (0, 10)
assert font.getlength(bytearray([i])) == dx
message = charsets[encoding]["message"].encode(encoding)
for i in range(len(message)):
msg = message[: i + 1]
assert font.getsize(msg) == (len(msg) * 10, 20)
assert font.getlength(msg) == len(msg) * 10
assert font.getbbox(msg) == (0, 0, len(msg) * 10, 20)


def test_textsize_iso8859_1(request, tmp_path):
Expand Down
30 changes: 24 additions & 6 deletions Tests/test_imagedraw.py
Expand Up @@ -1232,21 +1232,39 @@ 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")
def test_textsize_stroke():
def test_textbbox_stroke():
# Arrange
im = Image.new("RGB", (W, H))
draw = ImageDraw.Draw(im)
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")
Expand Down
13 changes: 9 additions & 4 deletions Tests/test_imagedraw2.py
@@ -1,5 +1,7 @@
import os.path

import pytest

from PIL import Image, ImageDraw, ImageDraw2

from .helper import (
Expand Down Expand Up @@ -205,7 +207,9 @@ def test_textsize():
font = ImageDraw2.Font("white", FONT_PATH)

# Act
size = draw.textsize("ImageDraw2", font)
with pytest.warns(DeprecationWarning) as log:
size = draw.textsize("ImageDraw2", font)
assert len(log) == 1

# Assert
assert size[1] == 12
Expand All @@ -221,9 +225,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("", font)
draw.textsize("\n", font)
draw.textsize("test\n", font)
draw.textbbox((0, 0), "", font)
draw.textbbox((0, 0), "\n", font)
draw.textbbox((0, 0), "test\n", font)
draw.textlength("", font)


@skip_unless_feature("freetype2")
Expand Down

0 comments on commit 488589b

Please sign in to comment.