Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate ImageFont.getsize and related functions #6381

Merged
merged 20 commits into from Jul 1, 2022
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
406fe59
deprecate font.getsize and related functions
nulano May 25, 2022
c854bf8
add getbbox and getlength to basic ImageFont and update related tests
nulano May 25, 2022
f34a646
update test_font_pcf to use getbbox
nulano May 25, 2022
1bf8755
add textbbox and textlength to ImageDraw2 and update tests
nulano May 25, 2022
e215834
update test_imagefont to use textbbox
nulano Jun 20, 2022
93acbcf
add getbbox and getlength to TransposedFont with tests
nulano Jun 20, 2022
a7baa31
use getbbox instead of getsize in fuzzers.py
nulano Jun 20, 2022
0d91d13
Merge branch 'main' into deprecate-getsize
radarhere Jun 30, 2022
f57a9d6
update TransposedFont.getlength error message
nulano Jun 30, 2022
303ec1a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 30, 2022
838b1f1
add replacement for getsize_multiline to deprecation warning
nulano Jun 30, 2022
3c0b876
Added documentation and release notes
radarhere Jun 30, 2022
9957e0b
link to ImageDraw2 in deprecations and release notes
nulano Jun 30, 2022
7691231
Fix heading in deprecations.rst
nulano Jun 30, 2022
65020e7
Documented deprecation in individual methods
radarhere Jun 30, 2022
ad5271d
Document replacements for individual deprecated font methods
nulano Jun 30, 2022
a37c21e
document planned removal date for ImageFont deprecations and release …
nulano Jun 30, 2022
74e0b95
test {ImageFont,TransposedFont}.getsize() deprecation
nulano Jul 1, 2022
729fe6f
Updated indentation
radarhere Jul 1, 2022
8a6050e
Replaced __internal__ argument with warning filters
radarhere Jul 1, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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")
8 changes: 6 additions & 2 deletions Tests/test_font_pcf.py
Expand Up @@ -68,12 +68,16 @@ 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
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