mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-01-13 18:56:17 +03:00
Merge pull request #1117 from radarhere/master
Allow truetype() to search for extensions other than .ttf
This commit is contained in:
commit
c91768f7bf
|
@ -261,38 +261,43 @@ def truetype(font=None, size=10, index=0, encoding="", filename=None):
|
||||||
try:
|
try:
|
||||||
return FreeTypeFont(font, size, index, encoding)
|
return FreeTypeFont(font, size, index, encoding)
|
||||||
except IOError:
|
except IOError:
|
||||||
if font.endswith(".ttf"):
|
ttf_filename = os.path.basename(font)
|
||||||
ttf_filename = font
|
|
||||||
else:
|
dirs = []
|
||||||
ttf_filename = "%s.ttf" % font
|
|
||||||
if sys.platform == "win32":
|
if sys.platform == "win32":
|
||||||
# check the windows font repository
|
# check the windows font repository
|
||||||
# NOTE: must use uppercase WINDIR, to work around bugs in
|
# NOTE: must use uppercase WINDIR, to work around bugs in
|
||||||
# 1.5.2's os.environ.get()
|
# 1.5.2's os.environ.get()
|
||||||
windir = os.environ.get("WINDIR")
|
windir = os.environ.get("WINDIR")
|
||||||
if windir:
|
if windir:
|
||||||
filename = os.path.join(windir, "fonts", font)
|
dirs.append(os.path.join(windir, "fonts"))
|
||||||
return FreeTypeFont(filename, size, index, encoding)
|
|
||||||
elif sys.platform in ('linux', 'linux2'):
|
elif sys.platform in ('linux', 'linux2'):
|
||||||
lindirs = os.environ.get("XDG_DATA_DIRS", "")
|
lindirs = os.environ.get("XDG_DATA_DIRS", "")
|
||||||
if not lindirs:
|
if not lindirs:
|
||||||
# According to the freedesktop spec, XDG_DATA_DIRS should
|
# According to the freedesktop spec, XDG_DATA_DIRS should
|
||||||
# default to /usr/share
|
# default to /usr/share
|
||||||
lindirs = '/usr/share'
|
lindirs = '/usr/share'
|
||||||
lindirs = lindirs.split(":")
|
dirs += [os.path.join(lindir, "fonts") for lindir in lindirs.split(":")]
|
||||||
for lindir in lindirs:
|
|
||||||
parentpath = os.path.join(lindir, "fonts")
|
|
||||||
for walkroot, walkdir, walkfilenames in os.walk(parentpath):
|
|
||||||
if ttf_filename in walkfilenames:
|
|
||||||
filepath = os.path.join(walkroot, ttf_filename)
|
|
||||||
return FreeTypeFont(filepath, size, index, encoding)
|
|
||||||
elif sys.platform == 'darwin':
|
elif sys.platform == 'darwin':
|
||||||
macdirs = ['/Library/Fonts/', '/System/Library/Fonts/',
|
dirs += ['/Library/Fonts', '/System/Library/Fonts',
|
||||||
os.path.expanduser('~/Library/Fonts/')]
|
os.path.expanduser('~/Library/Fonts')]
|
||||||
for macdir in macdirs:
|
|
||||||
filepath = os.path.join(macdir, ttf_filename)
|
ext = os.path.splitext(ttf_filename)[1]
|
||||||
if os.path.exists(filepath):
|
firstFontWithADifferentExtension = None
|
||||||
return FreeTypeFont(filepath, size, index, encoding)
|
for dir in dirs:
|
||||||
|
for walkroot, walkdir, walkfilenames in os.walk(dir):
|
||||||
|
for walkfilename in walkfilenames:
|
||||||
|
if ext and walkfilename == ttf_filename:
|
||||||
|
fontpath = os.path.join(walkroot, walkfilename)
|
||||||
|
return FreeTypeFont(fontpath, size, index, encoding)
|
||||||
|
elif not ext and os.path.splitext(walkfilename)[0] == ttf_filename:
|
||||||
|
fontpath = os.path.join(walkroot, walkfilename)
|
||||||
|
if os.path.splitext(fontpath)[1] == '.ttf':
|
||||||
|
return FreeTypeFont(fontpath, size, index, encoding)
|
||||||
|
if not ext and firstFontWithADifferentExtension == None:
|
||||||
|
firstFontWithADifferentExtension = fontpath
|
||||||
|
if firstFontWithADifferentExtension:
|
||||||
|
return FreeTypeFont(firstFontWithADifferentExtension, size, index, encoding)
|
||||||
raise
|
raise
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -233,7 +233,7 @@ try:
|
||||||
# Assert
|
# Assert
|
||||||
self.assert_image_equal(im, target_img)
|
self.assert_image_equal(im, target_img)
|
||||||
|
|
||||||
def _test_fake_loading_font(self, path_to_fake):
|
def _test_fake_loading_font(self, path_to_fake, fontname):
|
||||||
#Make a copy of FreeTypeFont so we can patch the original
|
#Make a copy of FreeTypeFont so we can patch the original
|
||||||
free_type_font = copy.deepcopy(ImageFont.FreeTypeFont)
|
free_type_font = copy.deepcopy(ImageFont.FreeTypeFont)
|
||||||
with SimplePatcher(ImageFont, '_FreeTypeFont', free_type_font):
|
with SimplePatcher(ImageFont, '_FreeTypeFont', free_type_font):
|
||||||
|
@ -242,7 +242,7 @@ try:
|
||||||
return ImageFont._FreeTypeFont(FONT_PATH, size, index, encoding)
|
return ImageFont._FreeTypeFont(FONT_PATH, size, index, encoding)
|
||||||
return ImageFont._FreeTypeFont(filepath, size, index, encoding)
|
return ImageFont._FreeTypeFont(filepath, size, index, encoding)
|
||||||
with SimplePatcher(ImageFont, 'FreeTypeFont', loadable_font):
|
with SimplePatcher(ImageFont, 'FreeTypeFont', loadable_font):
|
||||||
font = ImageFont.truetype('Arial')
|
font = ImageFont.truetype(fontname)
|
||||||
#Make sure it's loaded
|
#Make sure it's loaded
|
||||||
name = font.getname()
|
name = font.getname()
|
||||||
self.assertEqual(('FreeMono', 'Regular'), name)
|
self.assertEqual(('FreeMono', 'Regular'), name)
|
||||||
|
@ -252,25 +252,43 @@ try:
|
||||||
def test_find_linux_font(self):
|
def test_find_linux_font(self):
|
||||||
#A lot of mocking here - this is more for hitting code and catching
|
#A lot of mocking here - this is more for hitting code and catching
|
||||||
#syntax like errors
|
#syntax like errors
|
||||||
|
fontDirectory = '/usr/local/share/fonts'
|
||||||
with SimplePatcher(sys, 'platform', 'linux'):
|
with SimplePatcher(sys, 'platform', 'linux'):
|
||||||
patched_env = copy.deepcopy(os.environ)
|
patched_env = copy.deepcopy(os.environ)
|
||||||
patched_env['XDG_DATA_DIRS'] = '/usr/share/:/usr/local/share/'
|
patched_env['XDG_DATA_DIRS'] = '/usr/share/:/usr/local/share/'
|
||||||
with SimplePatcher(os, 'environ', patched_env):
|
with SimplePatcher(os, 'environ', patched_env):
|
||||||
def fake_walker(path):
|
def fake_walker(path):
|
||||||
if path == '/usr/local/share/fonts':
|
if path == fontDirectory:
|
||||||
return [(path, [], ['Arial.ttf'], )]
|
return [(path, [], ['Arial.ttf', 'Single.otf', 'Duplicate.otf', 'Duplicate.ttf'], )]
|
||||||
return [(path, [], ['some_random_font.ttf'], )]
|
return [(path, [], ['some_random_font.ttf'], )]
|
||||||
with SimplePatcher(os, 'walk', fake_walker):
|
with SimplePatcher(os, 'walk', fake_walker):
|
||||||
self._test_fake_loading_font('/usr/local/share/fonts/Arial.ttf')
|
# Test that the font loads both with and without the extension
|
||||||
|
self._test_fake_loading_font(fontDirectory+'/Arial.ttf', 'Arial.ttf')
|
||||||
|
self._test_fake_loading_font(fontDirectory+'/Arial.ttf', 'Arial')
|
||||||
|
|
||||||
|
# Test that non-ttf fonts can be found without the extension
|
||||||
|
self._test_fake_loading_font(fontDirectory+'/Single.otf', 'Single')
|
||||||
|
|
||||||
|
# Test that ttf fonts are preferred if the extension is not specified
|
||||||
|
self._test_fake_loading_font(fontDirectory+'/Duplicate.ttf', 'Duplicate')
|
||||||
|
|
||||||
@unittest.skipIf(sys.platform.startswith('win32'), "requires Unix or MacOS")
|
@unittest.skipIf(sys.platform.startswith('win32'), "requires Unix or MacOS")
|
||||||
def test_find_osx_font(self):
|
def test_find_osx_font(self):
|
||||||
#Like the linux test, more cover hitting code rather than testing
|
#Like the linux test, more cover hitting code rather than testing
|
||||||
#correctness.
|
#correctness.
|
||||||
|
fontDirectory = '/System/Library/Fonts'
|
||||||
with SimplePatcher(sys, 'platform', 'darwin'):
|
with SimplePatcher(sys, 'platform', 'darwin'):
|
||||||
fake_font_path = '/System/Library/Fonts/Arial.ttf'
|
def fake_walker(path):
|
||||||
with SimplePatcher(os.path, 'exists', lambda x: x == fake_font_path):
|
if path == fontDirectory:
|
||||||
self._test_fake_loading_font(fake_font_path)
|
return [(path, [], ['Arial.ttf', 'Single.otf', 'Duplicate.otf', 'Duplicate.ttf'], )]
|
||||||
|
return [(path, [], ['some_random_font.ttf'], )]
|
||||||
|
with SimplePatcher(os, 'walk', fake_walker):
|
||||||
|
self._test_fake_loading_font(fontDirectory+'/Arial.ttf', 'Arial.ttf')
|
||||||
|
self._test_fake_loading_font(fontDirectory+'/Arial.ttf', 'Arial')
|
||||||
|
|
||||||
|
self._test_fake_loading_font(fontDirectory+'/Single.otf', 'Single')
|
||||||
|
|
||||||
|
self._test_fake_loading_font(fontDirectory+'/Duplicate.ttf', 'Duplicate')
|
||||||
|
|
||||||
|
|
||||||
except ImportError:
|
except ImportError:
|
||||||
|
|
Loading…
Reference in New Issue
Block a user