Skip to content

Commit b3d9bd9

Browse files
committed
Test ImageFont.ImageFont, in case freetype2 is not supported
1 parent e36e670 commit b3d9bd9

File tree

1 file changed

+53
-26
lines changed

1 file changed

+53
-26
lines changed

Tests/test_imagetext.py

Lines changed: 53 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import pytest
44

5-
from PIL import Image, ImageDraw, ImageFont, ImageText
5+
from PIL import Image, ImageDraw, ImageFont, ImageText, features
66

77
from .helper import assert_image_similar_tofile, skip_unless_feature
88

@@ -20,42 +20,69 @@ def layout_engine(request: pytest.FixtureRequest) -> ImageFont.Layout:
2020
return request.param
2121

2222

23-
@pytest.fixture(scope="module")
24-
def font(layout_engine: ImageFont.Layout) -> ImageFont.FreeTypeFont:
25-
return ImageFont.truetype(FONT_PATH, 20, layout_engine=layout_engine)
26-
27-
28-
def test_get_length(font: ImageFont.FreeTypeFont) -> None:
29-
assert ImageText.Text("A", font).get_length() == 12
30-
assert ImageText.Text("AB", font).get_length() == 24
31-
assert ImageText.Text("M", font).get_length() == 12
32-
assert ImageText.Text("y", font).get_length() == 12
33-
assert ImageText.Text("a", font).get_length() == 12
23+
@pytest.fixture(
24+
scope="module",
25+
params=[
26+
None,
27+
pytest.param(ImageFont.Layout.BASIC, marks=skip_unless_feature("freetype2")),
28+
pytest.param(ImageFont.Layout.RAQM, marks=skip_unless_feature("raqm")),
29+
],
30+
)
31+
def font(
32+
request: pytest.FixtureRequest,
33+
) -> ImageFont.ImageFont | ImageFont.FreeTypeFont:
34+
layout_engine = request.param
35+
if layout_engine is None:
36+
return ImageFont.load_default_imagefont()
37+
else:
38+
return ImageFont.truetype(FONT_PATH, 20, layout_engine=layout_engine)
39+
40+
41+
def test_get_length(font: ImageFont.ImageFont | ImageFont.FreeTypeFont) -> None:
42+
factor = 1 if isinstance(font, ImageFont.ImageFont) else 2
43+
assert ImageText.Text("A", font).get_length() == 6 * factor
44+
assert ImageText.Text("AB", font).get_length() == 12 * factor
45+
assert ImageText.Text("M", font).get_length() == 6 * factor
46+
assert ImageText.Text("y", font).get_length() == 6 * factor
47+
assert ImageText.Text("a", font).get_length() == 6 * factor
3448

3549
text = ImageText.Text("\n", font)
3650
with pytest.raises(ValueError, match="can't measure length of multiline text"):
3751
text.get_length()
3852

3953

40-
def test_get_bbox(font: ImageFont.FreeTypeFont) -> None:
41-
assert ImageText.Text("A", font).get_bbox() == (0, 4, 12, 16)
42-
assert ImageText.Text("AB", font).get_bbox() == (0, 4, 24, 16)
43-
assert ImageText.Text("M", font).get_bbox() == (0, 4, 12, 16)
44-
assert ImageText.Text("y", font).get_bbox() == (0, 7, 12, 20)
45-
assert ImageText.Text("a", font).get_bbox() == (0, 7, 12, 16)
54+
@pytest.mark.parametrize(
55+
"text, expected",
56+
(
57+
("A", (0, 4, 12, 16)),
58+
("AB", (0, 4, 24, 16)),
59+
("M", (0, 4, 12, 16)),
60+
("y", (0, 7, 12, 20)),
61+
("a", (0, 7, 12, 16)),
62+
),
63+
)
64+
def test_get_bbox(
65+
font: ImageFont.ImageFont | ImageFont.FreeTypeFont,
66+
text: str,
67+
expected: tuple[int, int, int, int],
68+
) -> None:
69+
if isinstance(font, ImageFont.ImageFont):
70+
expected = (0, 0, expected[2] // 2, 11)
71+
assert ImageText.Text(text, font).get_bbox() == expected
4672

4773

4874
def test_standard_embedded_color(layout_engine: ImageFont.Layout) -> None:
49-
font = ImageFont.truetype(FONT_PATH, 40, layout_engine=layout_engine)
50-
text = ImageText.Text("Hello World!", font)
51-
text.embed_color()
52-
assert text.get_length() == 288
75+
if features.check_module("freetype2"):
76+
font = ImageFont.truetype(FONT_PATH, 40, layout_engine=layout_engine)
77+
text = ImageText.Text("Hello World!", font)
78+
text.embed_color()
79+
assert text.get_length() == 288
5380

54-
im = Image.new("RGB", (300, 64), "white")
55-
draw = ImageDraw.Draw(im)
56-
draw.text((10, 10), text, "#fa6")
81+
im = Image.new("RGB", (300, 64), "white")
82+
draw = ImageDraw.Draw(im)
83+
draw.text((10, 10), text, "#fa6")
5784

58-
assert_image_similar_tofile(im, "Tests/images/standard_embedded.png", 3.1)
85+
assert_image_similar_tofile(im, "Tests/images/standard_embedded.png", 3.1)
5986

6087
text = ImageText.Text("", mode="1")
6188
with pytest.raises(

0 commit comments

Comments
 (0)