Skip to content

Commit

Permalink
Improve error message when creating TrueType fonts of invalid size
Browse files Browse the repository at this point in the history
Fixes #7583
  • Loading branch information
akx committed Nov 28, 2023
1 parent a1d2297 commit 4ea5289
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Tests/test_imagefont.py
Expand Up @@ -1071,3 +1071,9 @@ def test_raqm_missing_warning(monkeypatch):
"Raqm layout was requested, but Raqm is not available. "
"Falling back to basic layout."
)


@pytest.mark.parametrize("size", [-1, 0])
def test_invalid_truetype_sizes_raise(size, layout_engine):
with pytest.raises(ValueError):
ImageFont.truetype(FONT_PATH, size, layout_engine=layout_engine)
5 changes: 5 additions & 0 deletions src/PIL/ImageFont.py
Expand Up @@ -788,8 +788,13 @@ def truetype(font=None, size=10, index=0, encoding="", layout_engine=None):
.. versionadded:: 4.2.0
:return: A font object.
:exception OSError: If the file could not be read.
:exception ValueError: If the size given is invalid.
"""

if size <= 0:
msg = "font size must be positive"
raise ValueError(msg)

def freetype(font):
return FreeTypeFont(font, size, index, encoding, layout_engine)

Expand Down

0 comments on commit 4ea5289

Please sign in to comment.