From a40c7a6bea437a6b6aa745c6f4752d5f62df684a Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Fri, 22 Apr 2022 07:31:20 +1000 Subject: [PATCH] If font is file-like object, do not re-read from object to get variant --- Tests/test_imagefont.py | 5 ++++- src/PIL/ImageFont.py | 7 ++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/Tests/test_imagefont.py b/Tests/test_imagefont.py index 0e1d1e637ad..0c50303f902 100644 --- a/Tests/test_imagefont.py +++ b/Tests/test_imagefont.py @@ -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() diff --git a/src/PIL/ImageFont.py b/src/PIL/ImageFont.py index 81ac03fe6b5..4799d71fb1c 100644 --- a/src/PIL/ImageFont.py +++ b/src/PIL/ImageFont.py @@ -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,