Skip to content

Commit 5652113

Browse files
authored
Merge pull request #8086 from radarhere/load_default_imagefont
2 parents 3fdaecb + 2f85bf1 commit 5652113

File tree

3 files changed

+167
-163
lines changed

3 files changed

+167
-163
lines changed

Tests/test_imagefontpil.py

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,51 +9,57 @@
99

1010
from .helper import assert_image_equal_tofile
1111

12-
original_core = ImageFont.core
13-
14-
15-
def setup_module() -> None:
16-
if features.check_module("freetype2"):
17-
ImageFont.core = _util.DeferredError(ImportError("Disabled for testing"))
18-
19-
20-
def teardown_module() -> None:
21-
ImageFont.core = original_core
12+
fonts = [ImageFont.load_default_imagefont()]
13+
if not features.check_module("freetype2"):
14+
default_font = ImageFont.load_default()
15+
if isinstance(default_font, ImageFont.ImageFont):
16+
fonts.append(default_font)
2217

2318

24-
def test_default_font() -> None:
19+
@pytest.mark.parametrize("font", fonts)
20+
def test_default_font(font: ImageFont.ImageFont) -> None:
2521
# Arrange
2622
txt = 'This is a "better than nothing" default font.'
2723
im = Image.new(mode="RGB", size=(300, 100))
2824
draw = ImageDraw.Draw(im)
2925

3026
# Act
31-
default_font = ImageFont.load_default()
32-
draw.text((10, 10), txt, font=default_font)
27+
draw.text((10, 10), txt, font=font)
3328

3429
# Assert
3530
assert_image_equal_tofile(im, "Tests/images/default_font.png")
3631

3732

38-
def test_size_without_freetype() -> None:
39-
with pytest.raises(ImportError):
40-
ImageFont.load_default(size=14)
33+
def test_without_freetype() -> None:
34+
original_core = ImageFont.core
35+
if features.check_module("freetype2"):
36+
ImageFont.core = _util.DeferredError(ImportError("Disabled for testing"))
37+
try:
38+
with pytest.raises(ImportError):
39+
ImageFont.truetype("Tests/fonts/FreeMono.ttf")
40+
41+
assert isinstance(ImageFont.load_default(), ImageFont.ImageFont)
42+
43+
with pytest.raises(ImportError):
44+
ImageFont.load_default(size=14)
45+
finally:
46+
ImageFont.core = original_core
4147

4248

43-
def test_unicode() -> None:
49+
@pytest.mark.parametrize("font", fonts)
50+
def test_unicode(font: ImageFont.ImageFont) -> None:
4451
# should not segfault, should return UnicodeDecodeError
4552
# issue #2826
46-
font = ImageFont.load_default()
4753
with pytest.raises(UnicodeEncodeError):
4854
font.getbbox("’")
4955

5056

51-
def test_textbbox() -> None:
57+
@pytest.mark.parametrize("font", fonts)
58+
def test_textbbox(font: ImageFont.ImageFont) -> None:
5259
im = Image.new("RGB", (200, 200))
5360
d = ImageDraw.Draw(im)
54-
default_font = ImageFont.load_default()
55-
assert d.textlength("test", font=default_font) == 24
56-
assert d.textbbox((0, 0), "test", font=default_font) == (0, 0, 24, 11)
61+
assert d.textlength("test", font=font) == 24
62+
assert d.textbbox((0, 0), "test", font=font) == (0, 0, 24, 11)
5763

5864

5965
def test_decompression_bomb() -> None:
@@ -76,8 +82,3 @@ def test_oom() -> None:
7682
font = ImageFont.ImageFont()
7783
font._load_pilfont_data(fp, Image.new("L", (1, 1)))
7884
font.getmask("A" * 1_000_000)
79-
80-
81-
def test_freetypefont_without_freetype() -> None:
82-
with pytest.raises(ImportError):
83-
ImageFont.truetype("Tests/fonts/FreeMono.ttf")

docs/reference/ImageFont.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ Functions
5353
.. autofunction:: PIL.ImageFont.load_path
5454
.. autofunction:: PIL.ImageFont.truetype
5555
.. autofunction:: PIL.ImageFont.load_default
56+
.. autofunction:: PIL.ImageFont.load_default_imagefont
5657

5758
Methods
5859
-------

0 commit comments

Comments
 (0)