From a517a9942db6fa90d30e7b3bd7e3385e06c41330 Mon Sep 17 00:00:00 2001 From: Chris Pointon Date: Tue, 25 Apr 2023 16:29:30 +0100 Subject: [PATCH 1/2] Make font loading walk subdirectories --- src/plugins/social/plugin.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/plugins/social/plugin.py b/src/plugins/social/plugin.py index 06b43714e..d9f017308 100644 --- a/src/plugins/social/plugin.py +++ b/src/plugins/social/plugin.py @@ -393,14 +393,22 @@ class SocialPlugin(BasePlugin[SocialPluginConfig]): name = "Roboto" # Retrieve font files, if not already done - files = os.listdir(self.cache) - files = [file for file in files if file.endswith(".ttf") or file.endswith(".otf")] or ( + all_files=[] + # Check for cached files - note these may be in subfolders + for currentpath, folders, files in os.walk(self.cache): + for file in files: + all_files.append(os.path.join(currentpath, file)) + + # If none found, fetch from Google and try again + if len(all_files) == 0: self._load_font_from_google(name) - ) + for currentpath, folders, files in os.walk(self.cache): + for file in files: + all_files.append(os.path.join(currentpath, file)) # Map available font weights to file paths font = dict() - for file in files: + for file in all_files: match = re.search(r"-(\w+)\.[ot]tf$", file) if match: font[match.group(1)] = os.path.join(self.cache, file) From 7a77935c7a8580e6245da2d9edf053f83b618d3c Mon Sep 17 00:00:00 2001 From: Chris Pointon Date: Tue, 25 Apr 2023 17:57:05 +0100 Subject: [PATCH 2/2] Improve font matching for fonts with different families --- src/plugins/social/plugin.py | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/src/plugins/social/plugin.py b/src/plugins/social/plugin.py index d9f017308..62bdb6301 100644 --- a/src/plugins/social/plugin.py +++ b/src/plugins/social/plugin.py @@ -392,26 +392,31 @@ class SocialPlugin(BasePlugin[SocialPluginConfig]): else: name = "Roboto" - # Retrieve font files, if not already done - all_files=[] + # Google fonts can return varients like OpenSane_Condensed-Regulat.ttf so + # we only use the font requested e.g. OpenSans-Regular.ttf + font_filename_base = name.replace(' ', '') + filename_regex = re.escape(font_filename_base)+r"-(\w+)\.[ot]tf$" + + font = dict() # Check for cached files - note these may be in subfolders - for currentpath, folders, files in os.walk(self.cache): + for currentpath, folders, files in os.walk("./.cache/"): for file in files: - all_files.append(os.path.join(currentpath, file)) + # Map available font weights to file paths + fname = os.path.join(currentpath, file) + match = re.search(filename_regex, fname) + if match: + font[match.group(1)] = fname # If none found, fetch from Google and try again - if len(all_files) == 0: + if len(font) == 0: self._load_font_from_google(name) - for currentpath, folders, files in os.walk(self.cache): + for currentpath, folders, files in os.walk("./.cache/"): for file in files: - all_files.append(os.path.join(currentpath, file)) - - # Map available font weights to file paths - font = dict() - for file in all_files: - match = re.search(r"-(\w+)\.[ot]tf$", file) - if match: - font[match.group(1)] = os.path.join(self.cache, file) + # Map available font weights to file paths + fname = os.path.join(currentpath, file) + match = re.search(filename_regex, fname) + if match: + font[match.group(1)] = fname # Return available font weights with fallback return defaultdict(lambda: font["Regular"], font)