Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Require font parameter in FreeTypeFont and truetype() #8261

Merged
merged 2 commits into from
Sep 4, 2024

Conversation

radarhere
Copy link
Member

@radarhere radarhere commented Jul 25, 2024

Currently, font is an optional parameter for FreeTypeFont and truetype().

Pillow/src/PIL/ImageFont.py

Lines 198 to 211 in 6dd4b3c

class FreeTypeFont:
"""FreeType font wrapper (requires _imagingft service)"""
font: Font
font_bytes: bytes
def __init__(
self,
font: StrOrBytesPath | BinaryIO | None = None,
size: float = 10,
index: int = 0,
encoding: str = "",
layout_engine: Layout | None = None,
) -> None:

Pillow/src/PIL/ImageFont.py

Lines 758 to 764 in 6dd4b3c

def truetype(
font: StrOrBytesPath | BinaryIO | None = None,
size: float = 10,
index: int = 0,
encoding: str = "",
layout_engine: Layout | None = None,
) -> FreeTypeFont:

However, if it is None, then an error is raised.

>>> from PIL import ImageFont
>>> ImageFont.FreeTypeFont()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "PIL/ImageFont.py", line 261, in __init__
    load_from_bytes(font)
  File "PIL/ImageFont.py", line 240, in load_from_bytes
    self.font_bytes = f.read()
AttributeError: 'NoneType' object has no attribute 'read'
>>> from PIL import ImageFont
>>> ImageFont.truetype()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "PIL/ImageFont.py", line 834, in truetype
    return freetype(font)
  File "PIL/ImageFont.py", line 831, in freetype
    return FreeTypeFont(font, size, index, encoding, layout_engine)
  File "PIL/ImageFont.py", line 261, in __init__
    load_from_bytes(font)
  File "PIL/ImageFont.py", line 240, in load_from_bytes
    self.font_bytes = f.read()
AttributeError: 'NoneType' object has no attribute 'read'

This PR removes the defaults to make it required instead.

@radarhere radarhere changed the title Require font parameter in FreeTypeFont Require font parameter in FreeTypeFont and truetype() Jul 25, 2024
@nulano nulano added the Cleanup label Jul 25, 2024
@hugovk hugovk merged commit 1dffc09 into python-pillow:main Sep 4, 2024
50 of 51 checks passed
@radarhere radarhere deleted the imagefont branch September 4, 2024 21:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants