Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1b20dab

Browse files
committed
Add FontManager.addfont to register fonts at specific paths.
The naming (without underscore) is similar to findfont. I chose to provide an API adding a single path rather than also wrap findSystemFonts given that the later is basically just a recursive glob (``Path(...).glob("**/*.ttf")``). addfont has no deduplication logic but I think that's fine (really ttflist should be ttfset, but that's a bigger API change). findSystemFonts already assumes that the font extension gives the font type so assuming the same in addfont seems fine.
1 parent b34c605 commit 1b20dab

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
@@ -503,6 +503,7 @@ def afmFontProperty(fontpath, font):
503503
return FontEntry(fontpath, name, style, variant, weight, stretch, size)
504504

505505

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

970-
ttffiles = findSystemFonts(paths) + findSystemFonts()
971-
self.ttflist = createFontList(ttffiles)
971+
self.afmlist = []
972+
self.ttflist = []
973+
for fontext in ["afm", "ttf"]:
974+
for path in [*findSystemFonts(paths, fontext=fontext),
975+
*findSystemFonts(fontext=fontext)]:
976+
self.addfont(path)
972977

973-
afmfiles = (findSystemFonts(paths, fontext='afm')
974-
+ findSystemFonts(fontext='afm'))
975-
self.afmlist = createFontList(afmfiles, fontext='afm')
978+
def addfont(self, path):
979+
"""
980+
Cache the properties of the font at *path* to make it available to the
981+
`FontManager`. The type of font is inferred from the path suffix.
982+
983+
Parameters
984+
----------
985+
path : str or path-like
986+
"""
987+
if Path(path).suffix.lower() == ".afm":
988+
try:
989+
with open(path, "rb") as fh:
990+
font = afm.AFM(fh)
991+
except EnvironmentError:
992+
_log.info("Could not open font file %s", path)
993+
return
994+
except RuntimeError:
995+
_log.info("Could not parse font file %s", path)
996+
return
997+
try:
998+
prop = afmFontProperty(path, font)
999+
except KeyError as exc:
1000+
_log.info("Could not extract properties for %s: %s", path, exc)
1001+
return
1002+
self.afmlist.append(prop)
1003+
else:
1004+
try:
1005+
font = ft2font.FT2Font(path)
1006+
except (OSError, RuntimeError) as exc:
1007+
_log.info("Could not open font file %s: %s", path, exc)
1008+
return
1009+
except UnicodeError:
1010+
_log.info("Cannot handle unicode filenames")
1011+
return
1012+
try:
1013+
prop = ttfFontProperty(font)
1014+
except (KeyError, RuntimeError, ValueError,
1015+
NotImplementedError) as exc:
1016+
_log.info("Could not extract properties for %s: %s", path, exc)
1017+
return
1018+
self.ttflist.append(prop)
9761019

9771020
@property
9781021
def defaultFont(self):

0 commit comments

Comments
 (0)