If font is file-like object, do not re-read from object to get variant

This commit is contained in:
Andrew Murray 2022-04-22 07:31:20 +10:00
parent de1ba373e1
commit a40c7a6bea
2 changed files with 10 additions and 2 deletions

View File

@ -65,9 +65,12 @@ class TestImageFont:
return font_bytes return font_bytes
def test_font_with_filelike(self): def test_font_with_filelike(self):
ImageFont.truetype( ttf = ImageFont.truetype(
self._font_as_bytes(), FONT_SIZE, layout_engine=self.LAYOUT_ENGINE 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()) self._render(self._font_as_bytes())
# Usage note: making two fonts from the same buffer fails. # Usage note: making two fonts from the same buffer fails.
# shared_bytes = self._font_as_bytes() # shared_bytes = self._font_as_bytes()

View File

@ -711,8 +711,13 @@ class FreeTypeFont:
:return: A FreeTypeFont object. :return: A FreeTypeFont object.
""" """
if font is None:
try:
font = BytesIO(self.font_bytes)
except AttributeError:
font = self.path
return FreeTypeFont( return FreeTypeFont(
font=self.path if font is None else font, font=font,
size=self.size if size is None else size, size=self.size if size is None else size,
index=self.index if index is None else index, index=self.index if index is None else index,
encoding=self.encoding if encoding is None else encoding, encoding=self.encoding if encoding is None else encoding,