From 39ec56c6eaa015dde2280ab11e3fa5193f9ad9fb Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Tue, 28 Nov 2023 12:56:32 +0200 Subject: [PATCH 1/2] Improve error message when creating TrueType fonts of invalid size Co-authored-by: Andrew Murray <3112309+radarhere@users.noreply.github.com> --- Tests/test_imagefont.py | 6 ++++++ src/PIL/ImageFont.py | 5 +++++ 2 files changed, 11 insertions(+) diff --git a/Tests/test_imagefont.py b/Tests/test_imagefont.py index db0df047f77..d71d14a34d5 100644 --- a/Tests/test_imagefont.py +++ b/Tests/test_imagefont.py @@ -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(layout_engine, size): + with pytest.raises(ValueError): + ImageFont.truetype(FONT_PATH, size, layout_engine=layout_engine) diff --git a/src/PIL/ImageFont.py b/src/PIL/ImageFont.py index c2956213519..0331a5c458c 100644 --- a/src/PIL/ImageFont.py +++ b/src/PIL/ImageFont.py @@ -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 font size is not greater than zero. """ + if size <= 0: + msg = "font size must be greater than 0" + raise ValueError(msg) + def freetype(font): return FreeTypeFont(font, size, index, encoding, layout_engine) From 36e0b5312a0d9e984dbe21b144e703440ac4efdf Mon Sep 17 00:00:00 2001 From: Aarni Koskela Date: Wed, 29 Nov 2023 09:21:51 +0200 Subject: [PATCH 2/2] Update Tests/test_imagefont.py Co-authored-by: Andrew Murray <3112309+radarhere@users.noreply.github.com> --- Tests/test_imagefont.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/test_imagefont.py b/Tests/test_imagefont.py index d71d14a34d5..e21bf8cbf5c 100644 --- a/Tests/test_imagefont.py +++ b/Tests/test_imagefont.py @@ -1074,6 +1074,6 @@ def test_raqm_missing_warning(monkeypatch): @pytest.mark.parametrize("size", [-1, 0]) -def test_invalid_truetype_sizes_raise(layout_engine, size): +def test_invalid_truetype_sizes_raise_valueerror(layout_engine, size): with pytest.raises(ValueError): ImageFont.truetype(FONT_PATH, size, layout_engine=layout_engine)