Commit f4a2fef5 authored by Michal Čihař's avatar Michal Čihař

Cache fonts as we use most of them frequently

parent e26113d1
...@@ -117,6 +117,9 @@ BASE_CHARS = frozenset(( ...@@ -117,6 +117,9 @@ BASE_CHARS = frozenset((
0xd, 0xa, 0xd, 0xa,
)) ))
# Cache of open fonts
FONT_CACHE = {}
def is_cjk(text): def is_cjk(text):
''' '''
...@@ -129,14 +132,17 @@ def get_font(size, bold=False, cjk=False): ...@@ -129,14 +132,17 @@ def get_font(size, bold=False, cjk=False):
''' '''
Returns PIL font object matching parameters. Returns PIL font object matching parameters.
''' '''
if cjk: cache_key = '%d-%s-%s' % (size, bold, cjk)
name = 'DroidSansFallback.ttf' if cache_key not in FONT_CACHE:
elif bold: if cjk:
name = 'DroidSans-Bold.ttf' name = 'DroidSansFallback.ttf'
else: elif bold:
name = 'DroidSans.ttf' name = 'DroidSans-Bold.ttf'
else:
name = 'DroidSans.ttf'
return ImageFont.truetype( FONT_CACHE[cache_key] = ImageFont.truetype(
os.path.join(appsettings.TTF_PATH, name), os.path.join(appsettings.TTF_PATH, name),
size size
) )
return FONT_CACHE[cache_key]
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment