Skip to content

Commit

Permalink
Added ImageFont.load_default_imagefont()
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed May 27, 2024
1 parent bbe1eff commit 930c423
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 157 deletions.
47 changes: 25 additions & 22 deletions Tests/test_imagefontpil.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,51 +9,54 @@

from .helper import assert_image_equal_tofile

original_core = ImageFont.core


def setup_module() -> None:
if features.check_module("freetype2"):
ImageFont.core = _util.DeferredError(ImportError)


def teardown_module() -> None:
ImageFont.core = original_core
fonts = [ImageFont.load_default_imagefont()]
if not features.check_module("freetype2"):
default_font = ImageFont.load_default()
if isinstance(default_font, ImageFont.ImageFont):
fonts.append(default_font)

Check warning on line 16 in Tests/test_imagefontpil.py

View check run for this annotation

Codecov / codecov/patch

Tests/test_imagefontpil.py#L14-L16

Added lines #L14 - L16 were not covered by tests


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

# Act
default_font = ImageFont.load_default()
draw.text((10, 10), txt, font=default_font)
draw.text((10, 10), txt, font=font)

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


def test_size_without_freetype() -> None:
with pytest.raises(ImportError):
ImageFont.load_default(size=14)
def test_without_freetype() -> None:
original_core = ImageFont.core
if features.check_module("freetype2"):
ImageFont.core = _util.DeferredError(ImportError)
try:
assert isinstance(ImageFont.load_default(), ImageFont.ImageFont)

with pytest.raises(ImportError):
ImageFont.load_default(size=14)
finally:
ImageFont.core = original_core


def test_unicode() -> None:
@pytest.mark.parametrize("font", fonts)
def test_unicode(font: ImageFont.ImageFont) -> None:
# should not segfault, should return UnicodeDecodeError
# issue #2826
font = ImageFont.load_default()
with pytest.raises(UnicodeEncodeError):
font.getbbox("’")


def test_textbbox() -> None:
@pytest.mark.parametrize("font", fonts)
def test_textbbox(font: ImageFont.ImageFont) -> None:
im = Image.new("RGB", (200, 200))
d = ImageDraw.Draw(im)
default_font = ImageFont.load_default()
assert d.textlength("test", font=default_font) == 24
assert d.textbbox((0, 0), "test", font=default_font) == (0, 0, 24, 11)
assert d.textlength("test", font=font) == 24
assert d.textbbox((0, 0), "test", font=font) == (0, 0, 24, 11)


def test_decompression_bomb() -> None:
Expand Down
1 change: 1 addition & 0 deletions docs/reference/ImageFont.rst
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ Functions
.. autofunction:: PIL.ImageFont.load_path
.. autofunction:: PIL.ImageFont.truetype
.. autofunction:: PIL.ImageFont.load_default
.. autofunction:: PIL.ImageFont.load_default_imagefont

Methods
-------
Expand Down
Loading

0 comments on commit 930c423

Please sign in to comment.