update test_imagefont to use textbbox

This commit is contained in:
nulano 2022-06-20 01:20:56 +01:00
parent 1bf87556ef
commit e2158344a0
No known key found for this signature in database
GPG Key ID: B650CDF63B705766
3 changed files with 89 additions and 41 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.5 KiB

After

Width:  |  Height:  |  Size: 1.5 KiB

View File

@ -94,7 +94,7 @@ class TestImageFont:
def _render(self, font):
txt = "Hello World!"
ttf = ImageFont.truetype(font, FONT_SIZE, layout_engine=self.LAYOUT_ENGINE)
ttf.getsize(txt)
ttf.getbbox(txt)
img = Image.new("RGB", (256, 64), "white")
d = ImageDraw.Draw(img)
@ -135,15 +135,15 @@ class TestImageFont:
target = "Tests/images/transparent_background_text_L.png"
assert_image_similar_tofile(im.convert("L"), target, 0.01)
def test_textsize_equal(self):
def test_textbbox_equal(self):
im = Image.new(mode="RGB", size=(300, 100))
draw = ImageDraw.Draw(im)
ttf = self.get_font()
txt = "Hello World!"
size = draw.textsize(txt, ttf)
bbox = draw.textbbox((10, 10), txt, ttf)
draw.text((10, 10), txt, font=ttf)
draw.rectangle((10, 10, 10 + size[0], 10 + size[1]))
draw.rectangle(bbox)
assert_image_similar_tofile(
im, "Tests/images/rectangle_surrounding_text.png", 2.5
@ -184,7 +184,7 @@ class TestImageFont:
im = Image.new(mode="RGB", size=(300, 100))
draw = ImageDraw.Draw(im)
ttf = self.get_font()
line_spacing = draw.textsize("A", font=ttf)[1] + 4
line_spacing = ttf.getbbox("A")[3] + 4
lines = TEST_TEXT.split("\n")
y = 0
for line in lines:
@ -245,19 +245,39 @@ class TestImageFont:
im = Image.new(mode="RGB", size=(300, 100))
draw = ImageDraw.Draw(im)
# Test that textsize() correctly connects to multiline_textsize()
assert draw.textsize(TEST_TEXT, font=ttf) == draw.multiline_textsize(
TEST_TEXT, font=ttf
with pytest.warns(DeprecationWarning) as log:
# Test that textsize() correctly connects to multiline_textsize()
assert draw.textsize(TEST_TEXT, font=ttf) == draw.multiline_textsize(
TEST_TEXT, font=ttf
)
# Test that multiline_textsize corresponds to ImageFont.textsize()
# for single line text
assert ttf.getsize("A") == draw.multiline_textsize("A", font=ttf)
# Test that textsize() can pass on additional arguments
# to multiline_textsize()
draw.textsize(TEST_TEXT, font=ttf, spacing=4)
draw.textsize(TEST_TEXT, ttf, 4)
assert len(log) == 6
def test_multiline_bbox(self):
ttf = self.get_font()
im = Image.new(mode="RGB", size=(300, 100))
draw = ImageDraw.Draw(im)
# Test that textbbox() correctly connects to multiline_textbbox()
assert draw.textbbox((0, 0), TEST_TEXT, font=ttf) == draw.multiline_textbbox(
(0, 0), TEST_TEXT, font=ttf
)
# Test that multiline_textsize corresponds to ImageFont.textsize()
# Test that multiline_textbbox corresponds to ImageFont.textbbox()
# for single line text
assert ttf.getsize("A") == draw.multiline_textsize("A", font=ttf)
assert ttf.getbbox("A") == draw.multiline_textbbox((0, 0), "A", font=ttf)
# Test that textsize() can pass on additional arguments
# to multiline_textsize()
draw.textsize(TEST_TEXT, font=ttf, spacing=4)
draw.textsize(TEST_TEXT, ttf, 4)
# Test that textbbox() can pass on additional arguments
# to multiline_textbbox()
draw.textbbox((0, 0), TEST_TEXT, font=ttf, spacing=4)
def test_multiline_width(self):
ttf = self.get_font()
@ -265,9 +285,15 @@ class TestImageFont:
draw = ImageDraw.Draw(im)
assert (
draw.textsize("longest line", font=ttf)[0]
== draw.multiline_textsize("longest line\nline", font=ttf)[0]
draw.textbbox((0, 0), "longest line", font=ttf)[2]
== draw.multiline_textbbox((0, 0), "longest line\nline", font=ttf)[2]
)
with pytest.warns(DeprecationWarning) as log:
assert (
draw.textsize("longest line", font=ttf)[0]
== draw.multiline_textsize("longest line\nline", font=ttf)[0]
)
assert len(log) == 2
def test_multiline_spacing(self):
ttf = self.get_font()
@ -373,9 +399,11 @@ class TestImageFont:
text = "offset this"
# Act
offset = font.getoffset(text)
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(self):
@ -417,11 +445,11 @@ class TestImageFont:
# Assert
assert_image_equal_tofile(im, "Tests/images/default_font.png")
def test_getsize_empty(self):
def test_getbbox_empty(self):
# issue #2614
font = self.get_font()
# should not crash.
assert (0, 0) == font.getsize("")
assert (0, 0, 0, 0) == font.getbbox("")
def test_render_empty(self):
# issue 2666
@ -438,7 +466,7 @@ class TestImageFont:
# issue #2826
font = ImageFont.load_default()
with pytest.raises(UnicodeEncodeError):
font.getsize("")
font.getbbox("")
def test_unicode_extended(self):
# issue #3777
@ -563,17 +591,29 @@ class TestImageFont:
assert t.font.x_ppem == 20
assert t.font.y_ppem == 20
assert t.font.glyphs == 4177
assert t.getsize("A") == (12, 16)
assert t.getsize("AB") == (24, 16)
assert t.getsize("M") == (12, 16)
assert t.getsize("y") == (12, 20)
assert t.getsize("a") == (12, 16)
assert t.getsize_multiline("A") == (12, 16)
assert t.getsize_multiline("AB") == (24, 16)
assert t.getsize_multiline("a") == (12, 16)
assert t.getsize_multiline("ABC\n") == (36, 36)
assert t.getsize_multiline("ABC\nA") == (36, 36)
assert t.getsize_multiline("ABC\nAaaa") == (48, 36)
assert t.getbbox("A") == (0, 4, 12, 16)
assert t.getbbox("AB") == (0, 4, 24, 16)
assert t.getbbox("M") == (0, 4, 12, 16)
assert t.getbbox("y") == (0, 7, 12, 20)
assert t.getbbox("a") == (0, 7, 12, 16)
assert t.getlength("A") == 12
assert t.getlength("AB") == 24
assert t.getlength("M") == 12
assert t.getlength("y") == 12
assert t.getlength("a") == 12
with pytest.warns(DeprecationWarning) as log:
assert t.getsize("A") == (12, 16)
assert t.getsize("AB") == (24, 16)
assert t.getsize("M") == (12, 16)
assert t.getsize("y") == (12, 20)
assert t.getsize("a") == (12, 16)
assert t.getsize_multiline("A") == (12, 16)
assert t.getsize_multiline("AB") == (24, 16)
assert t.getsize_multiline("a") == (12, 16)
assert t.getsize_multiline("ABC\n") == (36, 36)
assert t.getsize_multiline("ABC\nA") == (36, 36)
assert t.getsize_multiline("ABC\nAaaa") == (48, 36)
assert len(log) == 11
def test_getsize_stroke(self):
# Arrange
@ -581,14 +621,22 @@ class TestImageFont:
# Act / Assert
for stroke_width in [0, 2]:
assert t.getsize("A", stroke_width=stroke_width) == (
12 + stroke_width * 2,
16 + stroke_width * 2,
)
assert t.getsize_multiline("ABC\nAaaa", stroke_width=stroke_width) == (
48 + stroke_width * 2,
36 + stroke_width * 4,
assert t.getbbox("A", stroke_width=stroke_width) == (
0 - stroke_width,
4 - stroke_width,
12 + stroke_width,
16 + stroke_width,
)
with pytest.warns(DeprecationWarning) as log:
assert t.getsize("A", stroke_width=stroke_width) == (
12 + stroke_width * 2,
16 + stroke_width * 2,
)
assert t.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(self):
# Arrange
@ -871,7 +919,7 @@ class TestImageFont:
def test_standard_embedded_color(self):
txt = "Hello World!"
ttf = ImageFont.truetype(FONT_PATH, 40, layout_engine=self.LAYOUT_ENGINE)
ttf.getsize(txt)
ttf.getbbox(txt)
im = Image.new("RGB", (300, 64), "white")
d = ImageDraw.Draw(im)

View File

@ -140,8 +140,8 @@ def test_ligature_features():
target = "Tests/images/test_ligature_features.png"
assert_image_similar_tofile(im, target, 0.5)
liga_size = ttf.getsize("fi", features=["-liga"])
assert liga_size == (13, 19)
liga_bbox = ttf.getbbox("fi", features=["-liga"])
assert liga_bbox == (0, 4, 13, 19)
def test_kerning_features():