Skip to content

Commit 0f4aed3

Browse files
authored
Merge pull request #5518 from icebreakerone/master
Select the correct font variant where there are options in social plugin
2 parents facfa5c + fdf8362 commit 0f4aed3

File tree

2 files changed

+46
-24
lines changed

2 files changed

+46
-24
lines changed

material/plugins/social/plugin.py

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import sys
2929

3030
from collections import defaultdict
31-
from glob import glob
3231
from hashlib import md5
3332
from io import BytesIO
3433
from mkdocs.commands.build import DuplicateFilter
@@ -449,19 +448,31 @@ def _load_font(self, config):
449448
else:
450449
name = "Roboto"
451450

452-
# Retrieve font files, if not already done
453-
files = glob(f"{self.cache}/**/*.[ot]tf")
454-
files = [os.path.relpath(file, self.cache) for file in files]
455-
files = [file for file in files if file.endswith(".ttf") or file.endswith(".otf")] or (
456-
self._load_font_from_google(name)
457-
)
451+
# Google fonts can return varients like OpenSans_Condensed-Regular.ttf so
452+
# we only use the font requested e.g. OpenSans-Regular.ttf
453+
font_filename_base = name.replace(' ', '')
454+
filename_regex = re.escape(font_filename_base)+r"-(\w+)\.[ot]tf$"
458455

459-
# Map available font weights to file paths
460456
font = dict()
461-
for file in files:
462-
match = re.search(r"-(\w+)\.[ot]tf$", file)
463-
if match:
464-
font[match.group(1)] = os.path.join(self.cache, file)
457+
# Check for cached files - note these may be in subfolders
458+
for currentpath, folders, files in os.walk(self.cache):
459+
for file in files:
460+
# Map available font weights to file paths
461+
fname = os.path.join(currentpath, file)
462+
match = re.search(filename_regex, fname)
463+
if match:
464+
font[match.group(1)] = fname
465+
466+
# If none found, fetch from Google and try again
467+
if len(font) == 0:
468+
self._load_font_from_google(name)
469+
for currentpath, folders, files in os.walk(self.cache):
470+
for file in files:
471+
# Map available font weights to file paths
472+
fname = os.path.join(currentpath, file)
473+
match = re.search(filename_regex, fname)
474+
if match:
475+
font[match.group(1)] = fname
465476

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

src/plugins/social/plugin.py

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import sys
2929

3030
from collections import defaultdict
31-
from glob import glob
3231
from hashlib import md5
3332
from io import BytesIO
3433
from mkdocs.commands.build import DuplicateFilter
@@ -449,19 +448,31 @@ def _load_font(self, config):
449448
else:
450449
name = "Roboto"
451450

452-
# Retrieve font files, if not already done
453-
files = glob(f"{self.cache}/**/*.[ot]tf")
454-
files = [os.path.relpath(file, self.cache) for file in files]
455-
files = [file for file in files if file.endswith(".ttf") or file.endswith(".otf")] or (
456-
self._load_font_from_google(name)
457-
)
451+
# Google fonts can return varients like OpenSans_Condensed-Regular.ttf so
452+
# we only use the font requested e.g. OpenSans-Regular.ttf
453+
font_filename_base = name.replace(' ', '')
454+
filename_regex = re.escape(font_filename_base)+r"-(\w+)\.[ot]tf$"
458455

459-
# Map available font weights to file paths
460456
font = dict()
461-
for file in files:
462-
match = re.search(r"-(\w+)\.[ot]tf$", file)
463-
if match:
464-
font[match.group(1)] = os.path.join(self.cache, file)
457+
# Check for cached files - note these may be in subfolders
458+
for currentpath, folders, files in os.walk(self.cache):
459+
for file in files:
460+
# Map available font weights to file paths
461+
fname = os.path.join(currentpath, file)
462+
match = re.search(filename_regex, fname)
463+
if match:
464+
font[match.group(1)] = fname
465+
466+
# If none found, fetch from Google and try again
467+
if len(font) == 0:
468+
self._load_font_from_google(name)
469+
for currentpath, folders, files in os.walk(self.cache):
470+
for file in files:
471+
# Map available font weights to file paths
472+
fname = os.path.join(currentpath, file)
473+
match = re.search(filename_regex, fname)
474+
if match:
475+
font[match.group(1)] = fname
465476

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

0 commit comments

Comments
 (0)