Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Select the correct font variant where there are options in social plugin #5518

Merged
merged 3 commits into from
May 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 23 additions & 12 deletions material/plugins/social/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import sys

from collections import defaultdict
from glob import glob
from hashlib import md5
from io import BytesIO
from mkdocs.commands.build import DuplicateFilter
Expand Down Expand Up @@ -449,19 +448,31 @@ def _load_font(self, config):
else:
name = "Roboto"

# Retrieve font files, if not already done
files = glob(f"{self.cache}/**/*.[ot]tf")
files = [os.path.relpath(file, self.cache) for file in files]
files = [file for file in files if file.endswith(".ttf") or file.endswith(".otf")] or (
self._load_font_from_google(name)
)
# Google fonts can return varients like OpenSans_Condensed-Regular.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$"

# Map available font weights to file paths
font = dict()
for file in files:
match = re.search(r"-(\w+)\.[ot]tf$", file)
if match:
font[match.group(1)] = os.path.join(self.cache, file)
# Check for cached files - note these may be in subfolders
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

# 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 defaultdict(lambda: font["Regular"], font)
Expand Down
35 changes: 23 additions & 12 deletions src/plugins/social/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import sys

from collections import defaultdict
from glob import glob
from hashlib import md5
from io import BytesIO
from mkdocs.commands.build import DuplicateFilter
Expand Down Expand Up @@ -449,19 +448,31 @@ def _load_font(self, config):
else:
name = "Roboto"

# Retrieve font files, if not already done
files = glob(f"{self.cache}/**/*.[ot]tf")
files = [os.path.relpath(file, self.cache) for file in files]
files = [file for file in files if file.endswith(".ttf") or file.endswith(".otf")] or (
self._load_font_from_google(name)
)
# Google fonts can return varients like OpenSans_Condensed-Regular.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$"

# Map available font weights to file paths
font = dict()
for file in files:
match = re.search(r"-(\w+)\.[ot]tf$", file)
if match:
font[match.group(1)] = os.path.join(self.cache, file)
# Check for cached files - note these may be in subfolders
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

# 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 defaultdict(lambda: font["Regular"], font)
Expand Down