Skip to content

write addfont example #24866

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jan 31, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions lib/matplotlib/font_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
system font path that matches the specified `FontProperties`
instance. The `FontManager` also handles Adobe Font Metrics
(AFM) font files for use by the PostScript backend.
The `FontManager.addfont` function adds a custom font from a file without
installing it into your operating system.

The design is based on the `W3C Cascading Style Sheet, Level 1 (CSS1)
font specification <http://www.w3.org/TR/1998/REC-CSS2-19980512/>`_.
Expand Down Expand Up @@ -980,6 +982,27 @@ class FontManager:
method does a nearest neighbor search to find the font that most closely
matches the specification. If no good enough match is found, the default
font is returned.

Fonts added with the `FontManager.addfont` method will not persist in the
cache; therefore, `addfont` will need to be called every time Matplotlib is
imported. This method should only be used if and when a font cannot be
installed on your operating system by other means.

Notes
-----
The `FontManager.addfont` method must be called on the global `FontManager`
instance.

Example usage::

import matplotlib.pyplot as plt
from matplotlib import font_manager

font_dirs = ["/resources/fonts"] # The path to the custom font file.
font_files = font_manager.findSystemFonts(fontpaths=font_dirs)

for font_file in font_files:
font_manager.fontManager.addfont(font_file)
"""
# Increment this version number whenever the font cache data
# format or behavior has changed and requires an existing font
Expand Down Expand Up @@ -1030,6 +1053,12 @@ def addfont(self, path):
Parameters
----------
path : str or path-like

Notes
-----
This method is useful for adding a custom font without installing it in
your operating system. See the `FontManager` singleton instance for
usage and caveats about this function.
"""
# Convert to string in case of a path as
# afmFontProperty and FT2Font expect this
Expand Down