Allow truetype() to search for extensions other than .ttf

This commit is contained in:
Andrew Murray 2015-02-27 14:48:07 +11:00
parent d25a8d7e96
commit 84ec2af495
2 changed files with 20 additions and 20 deletions

View File

@ -261,38 +261,34 @@ 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): for dir in dirs:
return FreeTypeFont(filepath, size, index, encoding) for walkroot, walkdir, walkfilenames in os.walk(dir):
for walkfilename in walkfilenames:
if (ext and walkfilename == ttf_filename) or (not ext and os.path.splitext(walkfilename)[0] == ttf_filename):
fontpath = os.path.join(walkroot, walkfilename)
return FreeTypeFont(fontpath, size, index, encoding)
raise raise

View File

@ -269,7 +269,11 @@ try:
#correctness. #correctness.
with SimplePatcher(sys, 'platform', 'darwin'): with SimplePatcher(sys, 'platform', 'darwin'):
fake_font_path = '/System/Library/Fonts/Arial.ttf' fake_font_path = '/System/Library/Fonts/Arial.ttf'
with SimplePatcher(os.path, 'exists', lambda x: x == fake_font_path): def fake_walker(path):
if path == '/System/Library/Fonts/':
return [(path, [], ['Arial.ttf'], )]
return [(path, [], ['some_random_font.ttf'], )]
with SimplePatcher(os, 'walk', fake_walker):
self._test_fake_loading_font(fake_font_path) self._test_fake_loading_font(fake_font_path)