Merge pull request #5518 from icebreakerone/master

Select the correct font variant where there are options in social plugin
This commit is contained in:
Martin Donath 2023-05-16 16:23:33 +02:00 committed by GitHub
commit 0f4aed3d57
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 24 deletions

View File

@ -28,7 +28,6 @@ import requests
import sys import sys
from collections import defaultdict from collections import defaultdict
from glob import glob
from hashlib import md5 from hashlib import md5
from io import BytesIO from io import BytesIO
from mkdocs.commands.build import DuplicateFilter from mkdocs.commands.build import DuplicateFilter
@ -449,19 +448,31 @@ class SocialPlugin(BasePlugin[SocialPluginConfig]):
else: else:
name = "Roboto" name = "Roboto"
# Retrieve font files, if not already done # Google fonts can return varients like OpenSans_Condensed-Regular.ttf so
files = glob(f"{self.cache}/**/*.[ot]tf") # we only use the font requested e.g. OpenSans-Regular.ttf
files = [os.path.relpath(file, self.cache) for file in files] font_filename_base = name.replace(' ', '')
files = [file for file in files if file.endswith(".ttf") or file.endswith(".otf")] or ( filename_regex = re.escape(font_filename_base)+r"-(\w+)\.[ot]tf$"
self._load_font_from_google(name)
)
# Map available font weights to file paths
font = dict() font = dict()
# Check for cached files - note these may be in subfolders
for currentpath, folders, files in os.walk(self.cache):
for file in files: for file in files:
match = re.search(r"-(\w+)\.[ot]tf$", file) # Map available font weights to file paths
fname = os.path.join(currentpath, file)
match = re.search(filename_regex, fname)
if match: if match:
font[match.group(1)] = os.path.join(self.cache, file) font[match.group(1)] = fname
# If none found, fetch from Google and try again
if len(font) == 0:
self._load_font_from_google(name)
for currentpath, folders, files in os.walk(self.cache):
for file in files:
# 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 available font weights with fallback
return defaultdict(lambda: font["Regular"], font) return defaultdict(lambda: font["Regular"], font)

View File

@ -28,7 +28,6 @@ import requests
import sys import sys
from collections import defaultdict from collections import defaultdict
from glob import glob
from hashlib import md5 from hashlib import md5
from io import BytesIO from io import BytesIO
from mkdocs.commands.build import DuplicateFilter from mkdocs.commands.build import DuplicateFilter
@ -449,19 +448,31 @@ class SocialPlugin(BasePlugin[SocialPluginConfig]):
else: else:
name = "Roboto" name = "Roboto"
# Retrieve font files, if not already done # Google fonts can return varients like OpenSans_Condensed-Regular.ttf so
files = glob(f"{self.cache}/**/*.[ot]tf") # we only use the font requested e.g. OpenSans-Regular.ttf
files = [os.path.relpath(file, self.cache) for file in files] font_filename_base = name.replace(' ', '')
files = [file for file in files if file.endswith(".ttf") or file.endswith(".otf")] or ( filename_regex = re.escape(font_filename_base)+r"-(\w+)\.[ot]tf$"
self._load_font_from_google(name)
)
# Map available font weights to file paths
font = dict() font = dict()
# Check for cached files - note these may be in subfolders
for currentpath, folders, files in os.walk(self.cache):
for file in files: for file in files:
match = re.search(r"-(\w+)\.[ot]tf$", file) # Map available font weights to file paths
fname = os.path.join(currentpath, file)
match = re.search(filename_regex, fname)
if match: if match:
font[match.group(1)] = os.path.join(self.cache, file) font[match.group(1)] = fname
# If none found, fetch from Google and try again
if len(font) == 0:
self._load_font_from_google(name)
for currentpath, folders, files in os.walk(self.cache):
for file in files:
# 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 available font weights with fallback
return defaultdict(lambda: font["Regular"], font) return defaultdict(lambda: font["Regular"], font)