@@ -392,26 +392,31 @@ def _load_font(self, config):
392
392
else :
393
393
name = "Roboto"
394
394
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 ()
397
401
# 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/" ):
399
403
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
401
409
402
410
# If none found, fetch from Google and try again
403
- if len (all_files ) == 0 :
411
+ if len (font ) == 0 :
404
412
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/" ):
406
414
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
415
420
416
421
# Return available font weights with fallback
417
422
return defaultdict (lambda : font ["Regular" ], font )
0 commit comments