Skip to content

Commit 39ec56c

Browse files
akxradarhere
andcommitted
Improve error message when creating TrueType fonts of invalid size
Co-authored-by: Andrew Murray <[email protected]>
1 parent 697c24b commit 39ec56c

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

Tests/test_imagefont.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,3 +1071,9 @@ def test_raqm_missing_warning(monkeypatch):
10711071
"Raqm layout was requested, but Raqm is not available. "
10721072
"Falling back to basic layout."
10731073
)
1074+
1075+
1076+
@pytest.mark.parametrize("size", [-1, 0])
1077+
def test_invalid_truetype_sizes_raise(layout_engine, size):
1078+
with pytest.raises(ValueError):
1079+
ImageFont.truetype(FONT_PATH, size, layout_engine=layout_engine)

src/PIL/ImageFont.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,8 +788,13 @@ def truetype(font=None, size=10, index=0, encoding="", layout_engine=None):
788788
.. versionadded:: 4.2.0
789789
:return: A font object.
790790
:exception OSError: If the file could not be read.
791+
:exception ValueError: If the font size is not greater than zero.
791792
"""
792793

794+
if size <= 0:
795+
msg = "font size must be greater than 0"
796+
raise ValueError(msg)
797+
793798
def freetype(font):
794799
return FreeTypeFont(font, size, index, encoding, layout_engine)
795800

0 commit comments

Comments
 (0)