Skip to content

Commit

Permalink
Added load_pilfont()
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Oct 18, 2023
1 parent f790878 commit 170ac18
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 25 deletions.
26 changes: 16 additions & 10 deletions Tests/test_imagefontpil.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,48 @@

from .helper import assert_image_equal_tofile

pytestmark = pytest.mark.skipif(
features.check_module("freetype2"),
reason="PILfont superseded if FreeType is supported",
)
funcs = [ImageFont.load_pilfont]
if not features.check_module("freetype2"):
funcs.append(ImageFont.load_default)

Check warning on line 9 in Tests/test_imagefontpil.py

View check run for this annotation

Codecov / codecov/patch

Tests/test_imagefontpil.py#L9

Added line #L9 was not covered by tests


def test_default_font():
@pytest.mark.parametrize("func", funcs)
def test_default_font(func):
# 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()
default_font = func()
draw.text((10, 10), txt, font=default_font)

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


@pytest.mark.skipif(
features.check_module("freetype2"),
reason="PILfont superseded if FreeType is supported",
)
def test_size_without_freetype():
with pytest.raises(ImportError):
ImageFont.load_default(size=14)


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


def test_textbbox():
@pytest.mark.parametrize("func", funcs)
def test_textbbox(func):
im = Image.new("RGB", (200, 200))
d = ImageDraw.Draw(im)
default_font = ImageFont.load_default()
default_font = func()
assert d.textlength("test", font=default_font) == 24
assert d.textbbox((0, 0), "test", font=default_font) == (0, 0, 24, 11)
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_pilfont

Methods
-------
Expand Down
39 changes: 24 additions & 15 deletions src/PIL/ImageFont.py
Original file line number Diff line number Diff line change
Expand Up @@ -1107,12 +1107,21 @@ def load_default(size=None):
layout_engine=Layout.BASIC,
)
else:
f = ImageFont()
f._load_pilfont_data(
# courB08
BytesIO(
base64.b64decode(
b"""
f = load_pilfont()

Check warning on line 1110 in src/PIL/ImageFont.py

View check run for this annotation

Codecov / codecov/patch

src/PIL/ImageFont.py#L1110

Added line #L1110 was not covered by tests
return f


def load_pilfont():
"""Load a "better than nothing" font.
:return: A font object.
"""
f = ImageFont()
f._load_pilfont_data(
# courB08
BytesIO(
base64.b64decode(
b"""
UElMZm9udAo7Ozs7OzsxMDsKREFUQQoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
Expand Down Expand Up @@ -1205,12 +1214,12 @@ def load_default(size=None):
AAAAugAKAMEAEQAGAAD////4AAYAAgDBAAoAyAAUAAYAAP////kABQACAMgACgDOABMABgAA////
+QAGAAIAzgAKANUAEw==
"""
)
),
Image.open(
BytesIO(
base64.b64decode(
b"""
)
),
Image.open(
BytesIO(
base64.b64decode(
b"""
iVBORw0KGgoAAAANSUhEUgAAAx4AAAAUAQAAAAArMtZoAAAEwElEQVR4nABlAJr/AHVE4czCI/4u
Mc4b7vuds/xzjz5/3/7u/n9vMe7vnfH/9++vPn/xyf5zhxzjt8GHw8+2d83u8x27199/nxuQ6Od9
M43/5z2I+9n9ZtmDBwMQECDRQw/eQIQohJXxpBCNVE6QCCAAAAD//wBlAJr/AgALyj1t/wINwq0g
Expand All @@ -1235,8 +1244,8 @@ def load_default(size=None):
Gc/eeW7BwPj5+QGZhANUswMAAAD//2JgqGBgYGBgqEMXlvhMPUsAAAAA//8iYDd1AAAAAP//AwDR
w7IkEbzhVQAAAABJRU5ErkJggg==
"""
)
)
),
)
)
),
)
return f

0 comments on commit 170ac18

Please sign in to comment.