Skip to content

Creating TrueType fonts with size=0 fails with Pillow 10+ differently than with Pillow 9 #7583

@akx

Description

@akx

What did you do?

(Pillow) ~/b/Pillow (main) $ docker run -it -v "./Arial Bold.ttf:/a.ttf:ro" python:3.11 bash
root@e1ff41aff78e:/# pip install 'pillow<10'
Successfully installed pillow-9.5.0
root@e1ff41aff78e:/# python
Python 3.11.6 (main, Nov  1 2023, 10:08:51) [GCC 12.2.0] on linux
>>> from PIL import ImageFont
>>> ImageFont.truetype("/a.ttf", size=0)
<PIL.ImageFont.FreeTypeFont object at 0xffff9ef7d990>
root@e1ff41aff78e:/# pip install 'pillow>=10'
Successfully installed pillow-10.1.0
root@e1ff41aff78e:/# python
Python 3.11.6 (main, Nov  1 2023, 10:08:51) [GCC 12.2.0] on linux
>>> from PIL import ImageFont
>>> ImageFont.truetype("/a.ttf", size=0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.11/site-packages/PIL/ImageFont.py", line 797, in truetype
    return freetype(font)
           ^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/PIL/ImageFont.py", line 794, in freetype
    return FreeTypeFont(font, size, index, encoding, layout_engine)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/PIL/ImageFont.py", line 226, in __init__
    self.font = core.getfont(
                ^^^^^^^^^^^^^
OSError: invalid ppem value
>>>

What did you expect to happen? / What actually happened?

Font creation for a zero or negative size should fail alright, but maybe with a less cryptic error. (size=-1 yields invalid argument.)

With Pillow 9.x, font construction succeeds, but e.g. getbbox() or attempting to draw raises invalid argument.

What are your OS, Python and Pillow versions?

  • OS: Linux, various
  • Python: various
  • Pillow: see above for variants

See also

Downstream bug: WhyNotHugo/python-barcode#217

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions