Skip to content

Commit

Permalink
Merge pull request #7080 from radarhere/getsize
Browse files Browse the repository at this point in the history
Remove deprecated ImageFont.getsize and related functions for Pillow 10.0.0
  • Loading branch information
mergify[bot] committed Apr 10, 2023
2 parents a41b9ce + 6c5cc6e commit 0b9997f
Show file tree
Hide file tree
Showing 13 changed files with 114 additions and 609 deletions.
19 changes: 7 additions & 12 deletions Tests/test_deprecate.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@
@pytest.mark.parametrize(
"version, expected",
[
(
10,
"Old thing is deprecated and will be removed in Pillow 10 "
r"\(2023-07-01\)\. Use new thing instead\.",
),
(
11,
"Old thing is deprecated and will be removed in Pillow 11 "
Expand Down Expand Up @@ -57,18 +52,18 @@ def test_old_version(deprecated, plural, expected):

def test_plural():
expected = (
r"Old things are deprecated and will be removed in Pillow 10 \(2023-07-01\)\. "
r"Old things are deprecated and will be removed in Pillow 11 \(2024-10-15\)\. "
r"Use new thing instead\."
)
with pytest.warns(DeprecationWarning, match=expected):
_deprecate.deprecate("Old things", 10, "new thing", plural=True)
_deprecate.deprecate("Old things", 11, "new thing", plural=True)


def test_replacement_and_action():
expected = "Use only one of 'replacement' and 'action'"
with pytest.raises(ValueError, match=expected):
_deprecate.deprecate(
"Old thing", 10, replacement="new thing", action="Upgrade to new thing"
"Old thing", 11, replacement="new thing", action="Upgrade to new thing"
)


Expand All @@ -81,16 +76,16 @@ def test_replacement_and_action():
)
def test_action(action):
expected = (
r"Old thing is deprecated and will be removed in Pillow 10 \(2023-07-01\)\. "
r"Old thing is deprecated and will be removed in Pillow 11 \(2024-10-15\)\. "
r"Upgrade to new thing\."
)
with pytest.warns(DeprecationWarning, match=expected):
_deprecate.deprecate("Old thing", 10, action=action)
_deprecate.deprecate("Old thing", 11, action=action)


def test_no_replacement_or_action():
expected = (
r"Old thing is deprecated and will be removed in Pillow 10 \(2023-07-01\)"
r"Old thing is deprecated and will be removed in Pillow 11 \(2024-10-15\)"
)
with pytest.warns(DeprecationWarning, match=expected):
_deprecate.deprecate("Old thing", 10)
_deprecate.deprecate("Old thing", 11)
3 changes: 0 additions & 3 deletions Tests/test_font_pcf.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,6 @@ def test_textsize(request, tmp_path):
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.getlength(msg) == len(msg) * 10
Expand Down
15 changes: 0 additions & 15 deletions Tests/test_imagedraw.py
Original file line number Diff line number Diff line change
Expand Up @@ -1224,21 +1224,6 @@ def test_textbbox_stroke():
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")
def test_stroke():
for suffix, stroke_fill in {"same": None, "different": "#0f0"}.items():
Expand Down
11 changes: 5 additions & 6 deletions Tests/test_imagedraw2.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import pytest

from PIL import Image, ImageDraw, ImageDraw2
from PIL import Image, ImageDraw, ImageDraw2, features

from .helper import (
assert_image_equal,
Expand Down Expand Up @@ -171,19 +171,18 @@ def test_text():


@skip_unless_feature("freetype2")
def test_textsize():
def test_textbbox():
# Arrange
im = Image.new("RGB", (W, H))
draw = ImageDraw2.Draw(im)
font = ImageDraw2.Font("white", FONT_PATH)

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

# Assert
assert size[1] == 12
right = 72 if features.check_feature("raqm") else 70
assert bbox == (0, 2, right, 12)


@skip_unless_feature("freetype2")
Expand Down
117 changes: 21 additions & 96 deletions Tests/test_imagefont.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,27 +251,6 @@ def test_draw_align(font):
draw.text((100, 40), line, (0, 0, 0), font=font, align="left")


def test_multiline_size(font):
im = Image.new(mode="RGB", size=(300, 100))
draw = ImageDraw.Draw(im)

with pytest.warns(DeprecationWarning) as log:
# Test that textsize() correctly connects to multiline_textsize()
assert draw.textsize(TEST_TEXT, font=font) == draw.multiline_textsize(
TEST_TEXT, font=font
)

# Test that multiline_textsize corresponds to ImageFont.textsize()
# for single line text
assert font.getsize("A") == draw.multiline_textsize("A", font=font)

# Test that textsize() can pass on additional arguments
# to multiline_textsize()
draw.textsize(TEST_TEXT, font=font, spacing=4)
draw.textsize(TEST_TEXT, font, 4)
assert len(log) == 6


def test_multiline_bbox(font):
im = Image.new(mode="RGB", size=(300, 100))
draw = ImageDraw.Draw(im)
Expand All @@ -298,12 +277,6 @@ def test_multiline_width(font):
draw.textbbox((0, 0), "longest line", font=font)[2]
== draw.multiline_textbbox((0, 0), "longest line\nline", font=font)[2]
)
with pytest.warns(DeprecationWarning) as log:
assert (
draw.textsize("longest line", font=font)[0]
== draw.multiline_textsize("longest line\nline", font=font)[0]
)
assert len(log) == 2


def test_multiline_spacing(font):
Expand All @@ -326,29 +299,23 @@ def test_rotated_transposed_font(font, orientation):

# Original font
draw.font = font
with pytest.warns(DeprecationWarning) as log:
box_size_a = draw.textsize(word)
assert box_size_a == font.getsize(word)
assert len(log) == 2
bbox_a = draw.textbbox((10, 10), word)

# Rotated font
draw.font = transposed_font
with pytest.warns(DeprecationWarning) as log:
box_size_b = draw.textsize(word)
assert box_size_b == transposed_font.getsize(word)
assert len(log) == 2
bbox_b = draw.textbbox((20, 20), word)

# Check (w,h) of box a is (h,w) of box b
assert box_size_a[0] == box_size_b[1]
assert box_size_a[1] == box_size_b[0]
# Check (w, h) of box a is (h, w) of box b
assert (
bbox_a[2] - bbox_a[0],
bbox_a[3] - bbox_a[1],
) == (
bbox_b[3] - bbox_b[1],
bbox_b[2] - bbox_b[0],
)

# Check bbox b is (20, 20, 20 + h, 20 + w)
assert bbox_b[0] == 20
assert bbox_b[1] == 20
assert bbox_b[2] == 20 + bbox_a[3] - bbox_a[1]
assert bbox_b[3] == 20 + bbox_a[2] - bbox_a[0]
# Check top left co-ordinates are correct
assert bbox_b[:2] == (20, 20)

# text length is undefined for vertical text
with pytest.raises(ValueError):
Expand All @@ -373,28 +340,25 @@ def test_unrotated_transposed_font(font, orientation):

# Original font
draw.font = font
with pytest.warns(DeprecationWarning) as log:
box_size_a = draw.textsize(word)
assert len(log) == 1
bbox_a = draw.textbbox((10, 10), word)
length_a = draw.textlength(word)

# Rotated font
draw.font = transposed_font
with pytest.warns(DeprecationWarning) as log:
box_size_b = draw.textsize(word)
assert len(log) == 1
bbox_b = draw.textbbox((20, 20), word)
length_b = draw.textlength(word)

# Check boxes a and b are same size
assert box_size_a == box_size_b
assert (
bbox_a[2] - bbox_a[0],
bbox_a[3] - bbox_a[1],
) == (
bbox_b[2] - bbox_b[0],
bbox_b[3] - bbox_b[1],
)

# Check bbox b is (20, 20, 20 + w, 20 + h)
assert bbox_b[0] == 20
assert bbox_b[1] == 20
assert bbox_b[2] == 20 + bbox_a[2] - bbox_a[0]
assert bbox_b[3] == 20 + bbox_a[3] - bbox_a[1]
# Check top left co-ordinates are correct
assert bbox_b[:2] == (20, 20)

assert length_a == length_b

Expand Down Expand Up @@ -447,19 +411,6 @@ def test_free_type_font_get_metrics(font):
assert (ascent, descent) == (16, 4)


def test_free_type_font_get_offset(font):
# Arrange
text = "offset this"

# Act
with pytest.warns(DeprecationWarning) as log:
offset = font.getoffset(text)

# Assert
assert len(log) == 1
assert offset == (0, 3)


def test_free_type_font_get_mask(font):
# Arrange
text = "mask this"
Expand Down Expand Up @@ -618,19 +569,6 @@ def test_imagefont_getters(font):
assert font.getlength("M") == 12
assert font.getlength("y") == 12
assert font.getlength("a") == 12
with pytest.warns(DeprecationWarning) as log:
assert font.getsize("A") == (12, 16)
assert font.getsize("AB") == (24, 16)
assert font.getsize("M") == (12, 16)
assert font.getsize("y") == (12, 20)
assert font.getsize("a") == (12, 16)
assert font.getsize_multiline("A") == (12, 16)
assert font.getsize_multiline("AB") == (24, 16)
assert font.getsize_multiline("a") == (12, 16)
assert font.getsize_multiline("ABC\n") == (36, 36)
assert font.getsize_multiline("ABC\nA") == (36, 36)
assert font.getsize_multiline("ABC\nAaaa") == (48, 36)
assert len(log) == 11


@pytest.mark.parametrize("stroke_width", (0, 2))
Expand All @@ -641,16 +579,6 @@ def test_getsize_stroke(font, stroke_width):
12 + stroke_width,
16 + stroke_width,
)
with pytest.warns(DeprecationWarning) as log:
assert font.getsize("A", stroke_width=stroke_width) == (
12 + stroke_width * 2,
16 + stroke_width * 2,
)
assert font.getsize_multiline("ABC\nAaaa", stroke_width=stroke_width) == (
48 + stroke_width * 2,
36 + stroke_width * 4,
)
assert len(log) == 2


def test_complex_font_settings():
Expand Down Expand Up @@ -781,11 +709,8 @@ def test_textbbox_non_freetypefont():
im = Image.new("RGB", (200, 200))
d = ImageDraw.Draw(im)
default_font = ImageFont.load_default()
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)
assert d.textlength("test", font=default_font) == 24
assert d.textbbox((0, 0), "test", font=default_font) == (0, 0, 24, 11)


@pytest.mark.parametrize(
Expand Down

0 comments on commit 0b9997f

Please sign in to comment.