Skip to content

Commit

Permalink
If font is file-like object, do not re-read from object to get variant
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Apr 21, 2022
1 parent de1ba37 commit a40c7a6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
5 changes: 4 additions & 1 deletion Tests/test_imagefont.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,12 @@ def _font_as_bytes(self):
return font_bytes

def test_font_with_filelike(self):
ImageFont.truetype(
ttf = ImageFont.truetype(
self._font_as_bytes(), FONT_SIZE, layout_engine=self.LAYOUT_ENGINE
)
ttf_copy = ttf.font_variant()
assert ttf_copy.font_bytes == ttf.font_bytes

self._render(self._font_as_bytes())
# Usage note: making two fonts from the same buffer fails.
# shared_bytes = self._font_as_bytes()
Expand Down
7 changes: 6 additions & 1 deletion src/PIL/ImageFont.py
Original file line number Diff line number Diff line change
Expand Up @@ -711,8 +711,13 @@ def font_variant(
:return: A FreeTypeFont object.
"""
if font is None:
try:
font = BytesIO(self.font_bytes)
except AttributeError:
font = self.path
return FreeTypeFont(
font=self.path if font is None else font,
font=font,
size=self.size if size is None else size,
index=self.index if index is None else index,
encoding=self.encoding if encoding is None else encoding,
Expand Down

0 comments on commit a40c7a6

Please sign in to comment.