Merge pull request #3785 from radarhere/unicode_path

Fixed loading font with non-Unicode path on Windows
This commit is contained in:
Hugo 2019-05-04 15:55:50 +03:00 committed by GitHub
commit e20228a60a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 3 deletions

View File

@ -7,6 +7,7 @@ import os
import sys
import copy
import re
import shutil
import distutils.version
FONT_PATH = "Tests/fonts/FreeMono.ttf"
@ -131,6 +132,15 @@ class TestImageFont(PillowTestCase):
with open(FONT_PATH, 'rb') as f:
self._render(f)
def test_non_unicode_path(self):
try:
tempfile = self.tempfile("temp_"+chr(128)+".ttf")
except UnicodeEncodeError:
self.skipTest("Unicode path could not be created")
shutil.copy(FONT_PATH, tempfile)
ImageFont.truetype(tempfile, FONT_SIZE)
def _render(self, font):
txt = "Hello World!"
ttf = ImageFont.truetype(font, FONT_SIZE,

View File

@ -140,13 +140,26 @@ class FreeTypeFont(object):
self.layout_engine = layout_engine
def load_from_bytes(f):
self.font_bytes = f.read()
self.font = core.getfont(
"", size, index, encoding, self.font_bytes, layout_engine)
if isPath(font):
if sys.platform == "win32":
font_bytes_path = font if isinstance(font, bytes) else font.encode()
try:
font_bytes_path.decode('ascii')
except UnicodeDecodeError:
# FreeType cannot load fonts with non-ASCII characters on Windows
# So load it into memory first
with open(font, 'rb') as f:
load_from_bytes(f)
return
self.font = core.getfont(font, size, index, encoding,
layout_engine=layout_engine)
else:
self.font_bytes = font.read()
self.font = core.getfont(
"", size, index, encoding, self.font_bytes, layout_engine)
load_from_bytes(font)
def _multiline_split(self, text):
split_character = "\n" if isinstance(text, str) else b"\n"