Skip to content

Commit 7a77935

Browse files
committed
Improve font matching for fonts with different families
1 parent a517a99 commit 7a77935

File tree

1 file changed

+19
-14
lines changed

1 file changed

+19
-14
lines changed

src/plugins/social/plugin.py

+19-14
Original file line numberDiff line numberDiff line change
@@ -392,26 +392,31 @@ def _load_font(self, config):
392392
else:
393393
name = "Roboto"
394394

395-
# Retrieve font files, if not already done
396-
all_files=[]
395+
# Google fonts can return varients like OpenSane_Condensed-Regulat.ttf so
396+
# we only use the font requested e.g. OpenSans-Regular.ttf
397+
font_filename_base = name.replace(' ', '')
398+
filename_regex = re.escape(font_filename_base)+r"-(\w+)\.[ot]tf$"
399+
400+
font = dict()
397401
# Check for cached files - note these may be in subfolders
398-
for currentpath, folders, files in os.walk(self.cache):
402+
for currentpath, folders, files in os.walk("./.cache/"):
399403
for file in files:
400-
all_files.append(os.path.join(currentpath, file))
404+
# Map available font weights to file paths
405+
fname = os.path.join(currentpath, file)
406+
match = re.search(filename_regex, fname)
407+
if match:
408+
font[match.group(1)] = fname
401409

402410
# If none found, fetch from Google and try again
403-
if len(all_files) == 0:
411+
if len(font) == 0:
404412
self._load_font_from_google(name)
405-
for currentpath, folders, files in os.walk(self.cache):
413+
for currentpath, folders, files in os.walk("./.cache/"):
406414
for file in files:
407-
all_files.append(os.path.join(currentpath, file))
408-
409-
# Map available font weights to file paths
410-
font = dict()
411-
for file in all_files:
412-
match = re.search(r"-(\w+)\.[ot]tf$", file)
413-
if match:
414-
font[match.group(1)] = os.path.join(self.cache, file)
415+
# Map available font weights to file paths
416+
fname = os.path.join(currentpath, file)
417+
match = re.search(filename_regex, fname)
418+
if match:
419+
font[match.group(1)] = fname
415420

416421
# Return available font weights with fallback
417422
return defaultdict(lambda: font["Regular"], font)

0 commit comments

Comments
 (0)