Skip to content

Commit 2be4767

Browse files
authored
Merge pull request #15068 from anntzer/addfont
Add FontManager.addfont to register fonts at specific paths.
2 parents 08ac6e9 + 567b8ac commit 2be4767

File tree

2 files changed

+53
-5
lines changed

2 files changed

+53
-5
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
API changes
2+
```````````
3+
4+
``font_manager.createFontList`` is deprecated. `.font_manager.FontManager.addfont`
5+
is now available to register a font at a given path.

lib/matplotlib/font_manager.py

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,7 @@ def afmFontProperty(fontpath, font):
502502
return FontEntry(fontpath, name, style, variant, weight, stretch, size)
503503

504504

505+
@cbook.deprecated("3.2", alternative="FontManager.addfont")
505506
def createFontList(fontfiles, fontext='ttf'):
506507
"""
507508
A function to create a font lookup list. The default is to create
@@ -966,12 +967,54 @@ def __init__(self, size=None, weight='normal'):
966967
'ttf': 'DejaVu Sans',
967968
'afm': 'Helvetica'}
968969

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)
971976

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)
9751018

9761019
@property
9771020
def defaultFont(self):

0 commit comments

Comments
 (0)