Skip to content

Commit f2100d5

Browse files
authored
write addfont example (#24866)
* write addfont example
1 parent 84c5335 commit f2100d5

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

lib/matplotlib/font_manager.py

+29
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
system font path that matches the specified `FontProperties`
88
instance. The `FontManager` also handles Adobe Font Metrics
99
(AFM) font files for use by the PostScript backend.
10+
The `FontManager.addfont` function adds a custom font from a file without
11+
installing it into your operating system.
1012
1113
The design is based on the `W3C Cascading Style Sheet, Level 1 (CSS1)
1214
font specification <http://www.w3.org/TR/1998/REC-CSS2-19980512/>`_.
@@ -980,6 +982,27 @@ class FontManager:
980982
method does a nearest neighbor search to find the font that most closely
981983
matches the specification. If no good enough match is found, the default
982984
font is returned.
985+
986+
Fonts added with the `FontManager.addfont` method will not persist in the
987+
cache; therefore, `addfont` will need to be called every time Matplotlib is
988+
imported. This method should only be used if and when a font cannot be
989+
installed on your operating system by other means.
990+
991+
Notes
992+
-----
993+
The `FontManager.addfont` method must be called on the global `FontManager`
994+
instance.
995+
996+
Example usage::
997+
998+
import matplotlib.pyplot as plt
999+
from matplotlib import font_manager
1000+
1001+
font_dirs = ["/resources/fonts"] # The path to the custom font file.
1002+
font_files = font_manager.findSystemFonts(fontpaths=font_dirs)
1003+
1004+
for font_file in font_files:
1005+
font_manager.fontManager.addfont(font_file)
9831006
"""
9841007
# Increment this version number whenever the font cache data
9851008
# format or behavior has changed and requires an existing font
@@ -1030,6 +1053,12 @@ def addfont(self, path):
10301053
Parameters
10311054
----------
10321055
path : str or path-like
1056+
1057+
Notes
1058+
-----
1059+
This method is useful for adding a custom font without installing it in
1060+
your operating system. See the `FontManager` singleton instance for
1061+
usage and caveats about this function.
10331062
"""
10341063
# Convert to string in case of a path as
10351064
# afmFontProperty and FT2Font expect this

0 commit comments

Comments
 (0)