@@ -502,6 +502,7 @@ def afmFontProperty(fontpath, font):
502
502
return FontEntry (fontpath , name , style , variant , weight , stretch , size )
503
503
504
504
505
+ @cbook .deprecated ("3.2" , alternative = "FontManager.addfont" )
505
506
def createFontList (fontfiles , fontext = 'ttf' ):
506
507
"""
507
508
A function to create a font lookup list. The default is to create
@@ -966,12 +967,54 @@ def __init__(self, size=None, weight='normal'):
966
967
'ttf' : 'DejaVu Sans' ,
967
968
'afm' : 'Helvetica' }
968
969
969
- ttffiles = findSystemFonts (paths ) + findSystemFonts ()
970
- self .ttflist = createFontList (ttffiles )
970
+ self .afmlist = []
971
+ self .ttflist = []
972
+ for fontext in ["afm" , "ttf" ]:
973
+ for path in [* findSystemFonts (paths , fontext = fontext ),
974
+ * findSystemFonts (fontext = fontext )]:
975
+ self .addfont (path )
971
976
972
- afmfiles = (findSystemFonts (paths , fontext = 'afm' )
973
- + findSystemFonts (fontext = 'afm' ))
974
- self .afmlist = createFontList (afmfiles , fontext = 'afm' )
977
+ def addfont (self , path ):
978
+ """
979
+ Cache the properties of the font at *path* to make it available to the
980
+ `FontManager`. The type of font is inferred from the path suffix.
981
+
982
+ Parameters
983
+ ----------
984
+ path : str or path-like
985
+ """
986
+ if Path (path ).suffix .lower () == ".afm" :
987
+ try :
988
+ with open (path , "rb" ) as fh :
989
+ font = afm .AFM (fh )
990
+ except EnvironmentError :
991
+ _log .info ("Could not open font file %s" , path )
992
+ return
993
+ except RuntimeError :
994
+ _log .info ("Could not parse font file %s" , path )
995
+ return
996
+ try :
997
+ prop = afmFontProperty (path , font )
998
+ except KeyError as exc :
999
+ _log .info ("Could not extract properties for %s: %s" , path , exc )
1000
+ return
1001
+ self .afmlist .append (prop )
1002
+ else :
1003
+ try :
1004
+ font = ft2font .FT2Font (path )
1005
+ except (OSError , RuntimeError ) as exc :
1006
+ _log .info ("Could not open font file %s: %s" , path , exc )
1007
+ return
1008
+ except UnicodeError :
1009
+ _log .info ("Cannot handle unicode filenames" )
1010
+ return
1011
+ try :
1012
+ prop = ttfFontProperty (font )
1013
+ except (KeyError , RuntimeError , ValueError ,
1014
+ NotImplementedError ) as exc :
1015
+ _log .info ("Could not extract properties for %s: %s" , path , exc )
1016
+ return
1017
+ self .ttflist .append (prop )
975
1018
976
1019
@property
977
1020
def defaultFont (self ):
0 commit comments