mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-01-14 11:26:27 +03:00
Merge pull request #3785 from radarhere/unicode_path
Fixed loading font with non-Unicode path on Windows
This commit is contained in:
commit
e20228a60a
|
@ -7,6 +7,7 @@ import os
|
||||||
import sys
|
import sys
|
||||||
import copy
|
import copy
|
||||||
import re
|
import re
|
||||||
|
import shutil
|
||||||
import distutils.version
|
import distutils.version
|
||||||
|
|
||||||
FONT_PATH = "Tests/fonts/FreeMono.ttf"
|
FONT_PATH = "Tests/fonts/FreeMono.ttf"
|
||||||
|
@ -131,6 +132,15 @@ class TestImageFont(PillowTestCase):
|
||||||
with open(FONT_PATH, 'rb') as f:
|
with open(FONT_PATH, 'rb') as f:
|
||||||
self._render(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):
|
def _render(self, font):
|
||||||
txt = "Hello World!"
|
txt = "Hello World!"
|
||||||
ttf = ImageFont.truetype(font, FONT_SIZE,
|
ttf = ImageFont.truetype(font, FONT_SIZE,
|
||||||
|
|
|
@ -140,13 +140,26 @@ class FreeTypeFont(object):
|
||||||
|
|
||||||
self.layout_engine = layout_engine
|
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 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,
|
self.font = core.getfont(font, size, index, encoding,
|
||||||
layout_engine=layout_engine)
|
layout_engine=layout_engine)
|
||||||
else:
|
else:
|
||||||
self.font_bytes = font.read()
|
load_from_bytes(font)
|
||||||
self.font = core.getfont(
|
|
||||||
"", size, index, encoding, self.font_bytes, layout_engine)
|
|
||||||
|
|
||||||
def _multiline_split(self, text):
|
def _multiline_split(self, text):
|
||||||
split_character = "\n" if isinstance(text, str) else b"\n"
|
split_character = "\n" if isinstance(text, str) else b"\n"
|
||||||
|
|
Loading…
Reference in New Issue
Block a user