A module for finding, managing, and using fonts across platforms.
This module provides a single FontManager
instance that can be shared across backends and platforms. The findfont
function returns the best TrueType (TTF) font file in the local or 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 design is based on the W3C Cascading Style Sheet, Level 1 (CSS1) font specification. Future versions may implement the Level 2 or 2.1 specifications.
Bases: object
On import, the FontManager
singleton instance creates a list of ttf and afm fonts and caches their FontProperties
. The FontManager.findfont
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.
Cache the properties of the font at path to make it available to the FontManager
. The type of font is inferred from the path suffix.
Find a font that most closely matches the given font properties.
FontProperties
The font properties to search for. This can be either a FontProperties
object or a string defining a fontconfig patterns.
The extension of the font file:
If given, only search this directory and its subdirectories.
If True, will fallback to the default font family (usually "DejaVu Sans" or "Helvetica") if the first lookup hard-fails.
Whether to rebuild the font cache and search again if the first match appears to point to a nonexisting font (i.e., the font cache contains outdated entries).
The filename of the best matching font.
This performs a nearest neighbor search. Each font is given a similarity score to the target font properties. The first font with the highest score is returned. If no matches below a certain threshold are found, the default font (usually DejaVu Sans) is returned.
The result is cached, so subsequent lookups don't have to perform the O(n) nearest neighbor search.
See the W3C Cascading Style Sheet, Level 1 documentation for a description of the font finding algorithm.
Return the default font size.
Return the default font weight.
Return a match score between the list of font families in families and the font family name family2.
An exact match at the head of the list returns 0.0.
A match further down the list will return between 0 and 1.
No match will return 1.0.
Return a match score between size1 and size2.
If size2 (the size specified in the font file) is 'scalable', this function always returns 0.0, since any font size can be generated.
Otherwise, the result is the absolute distance between size1 and size2, normalized so that the usual range of font sizes (6pt - 72pt) will lie between 0.0 and 1.0.
Return a match score between stretch1 and stretch2.
The result is the absolute value of the difference between the CSS numeric values of stretch1 and stretch2, normalized between 0.0 and 1.0.
Return a match score between style1 and style2.
An exact match returns 0.0.
A match between 'italic' and 'oblique' returns 0.1.
No match returns 1.0.
Return a match score between variant1 and variant2.
An exact match returns 0.0, otherwise 1.0.
Return a match score between weight1 and weight2.
The result is 0.0 if both weight1 and weight 2 are given as strings and have the same value.
Otherwise, the result is the absolute value of the difference between the CSS numeric values of weight1 and weight2, normalized between 0.05 and 1.0.
Set the default font weight. The initial value is 'normal'.
Bases: object
A class for storing and manipulating font properties.
The font properties are the six properties described in the W3C Cascading Style Sheet, Level 1 font specification and math_fontfamily for math fonts:
rcParams["mathtext.fontset"]
(default: 'dejavusans'
). Supported values are the same as the ones supported by rcParams["mathtext.fontset"]
(default: 'dejavusans'
): 'dejavusans', 'dejavuserif', 'cm', 'stix', 'stixsans' and 'custom'.Alternatively, a font may be specified using the absolute path to a font file, by using the fname kwarg. However, in this case, it is typically simpler to just pass the path (as a pathlib.Path
, not a str
) to the font kwarg of the Text
object.
The preferred usage of font sizes is to use the relative values, e.g., 'large', instead of absolute font sizes, e.g., 12. This approach allows all text sizes to be made larger or smaller based on the font manager's default font size.
This class will also accept a fontconfig pattern, if it is the only argument provided. This support does not depend on fontconfig; we are merely borrowing its pattern syntax for use here.
Note that Matplotlib's internal font manager and fontconfig use a different algorithm to lookup fonts, so the results of the same pattern may be different in Matplotlib than in other applications that use fontconfig.
Return a copy of self.
Return a list of font names that comprise the font family.
Return the filename of the associated font.
Get a fontconfig pattern suitable for looking up the font as specified with fontconfig's fc-match
utility.
This support does not depend on fontconfig; we are merely borrowing its pattern syntax for use here.
Return the name of the font family used for math text.
The default font is rcParams["mathtext.fontset"]
(default: 'dejavusans'
).
Return the name of the font that best matches the font properties.
Return the font size.
Return the font style. Values are: 'normal', 'italic' or 'oblique'.
Return the font stretch or width. Options are: 'ultra-condensed', 'extra-condensed', 'condensed', 'semi-condensed', 'normal', 'semi-expanded', 'expanded', 'extra-expanded', 'ultra-expanded'.
Return the font style. Values are: 'normal', 'italic' or 'oblique'.
Return the font variant. Values are: 'normal' or 'small-caps'.
Set the font weight. Options are: A numeric value in the range 0-1000 or one of 'light', 'normal', 'regular', 'book', 'medium', 'roman', 'semibold', 'demibold', 'demi', 'bold', 'heavy', 'extra bold', 'black'
Change the font family. May be either an alias (generic name is CSS parlance), such as: 'serif', 'sans-serif', 'cursive', 'fantasy', or 'monospace', a real font name or a list of real font names. Real font names are not supported when rcParams["text.usetex"]
(default: False
) is True
.
Set the filename of the fontfile to use. In this case, all other properties will be ignored.
Set the properties by parsing a fontconfig pattern.
This support does not depend on fontconfig; we are merely borrowing its pattern syntax for use here.
Set the font family for text in math mode.
If not set explicitly, rcParams["mathtext.fontset"]
(default: 'dejavusans'
) will be used.
The name of the font family.
Available font families are defined in the matplotlibrc.template file here
See also
Change the font family. May be either an alias (generic name is CSS parlance), such as: 'serif', 'sans-serif', 'cursive', 'fantasy', or 'monospace', a real font name or a list of real font names. Real font names are not supported when rcParams["text.usetex"]
(default: False
) is True
.
Set the font size. Either an relative value of 'xx-small', 'x-small', 'small', 'medium', 'large', 'x-large', 'xx-large' or an absolute font size, e.g., 12.
Set the font style. Values are: 'normal', 'italic' or 'oblique'.
Set the font stretch or width. Options are: 'ultra-condensed', 'extra-condensed', 'condensed', 'semi-condensed', 'normal', 'semi-expanded', 'expanded', 'extra-expanded' or 'ultra-expanded', or a numeric value in the range 0-1000.
Set the font style. Values are: 'normal', 'italic' or 'oblique'.
Set the font variant. Values are: 'normal' or 'small-caps'.
Set the font weight. May be either a numeric value in the range 0-1000 or one of 'ultralight', 'light', 'normal', 'regular', 'book', 'medium', 'roman', 'semibold', 'demibold', 'demi', 'bold', 'heavy', 'extra bold', 'black'
Extract information from an AFM font file.
Search for fonts in the specified font paths. If no paths are given, will use a standard set of system paths, as well as the list of fonts tracked by fontconfig if fontconfig is installed and available. A list of TrueType fonts are returned by default with AFM fonts as an option.
Find a font that most closely matches the given font properties.
FontProperties
The font properties to search for. This can be either a FontProperties
object or a string defining a fontconfig patterns.
The extension of the font file:
If given, only search this directory and its subdirectories.
If True, will fallback to the default font family (usually "DejaVu Sans" or "Helvetica") if the first lookup hard-fails.
Whether to rebuild the font cache and search again if the first match appears to point to a nonexisting font (i.e., the font cache contains outdated entries).
The filename of the best matching font.
This performs a nearest neighbor search. Each font is given a similarity score to the target font properties. The first font with the highest score is returned. If no matches below a certain threshold are found, the default font (usually DejaVu Sans) is returned.
The result is cached, so subsequent lookups don't have to perform the O(n) nearest neighbor search.
See the W3C Cascading Style Sheet, Level 1 documentation for a description of the font finding algorithm.
[Deprecated] List font filenames known to fc-list
having the given extension.
Deprecated since version 3.5.
Return a list of file extensions extensions that are synonyms for the given file extension fileext.
Return whether the given font is a Postscript Compact Font Format Font embedded in an OpenType wrapper. Used by the PostScript and PDF backends that can not subset these fonts.
Dump FontManager
data as JSON to the file named filename.
See also
File paths that are children of the Matplotlib data path (typically, fonts shipped with Matplotlib) are stored relative to that data path (to remain valid across virtualenvs).
This function temporarily locks the output file to prevent multiple processes from overwriting one another's output.
Load a FontManager
from the JSON file named filename.
See also
Return a list of all fonts matching any of the extensions, found recursively under the directory.
Extract information from a TrueType font file.
FT2Font
The TrueType font file from which information will be extracted.
FontEntry
The extracted font properties.
Return the user-specified font directory for Win32. This is looked up from the registry key
\\HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\Fonts
If the key is not found, %WINDIR%\Fonts
will be returned.
[Deprecated] Search for fonts in the specified font directory, or use the system directories if none given. Additionally, it is searched for user fonts installed. A list of TrueType font filenames are returned by default, or AFM fonts if fontext == 'afm'.
Deprecated since version 3.5.
A class for storing Font properties.
It is used when populating the font lookup dictionary.
© 2012–2021 Matplotlib Development Team. All rights reserved.
Licensed under the Matplotlib License Agreement.
https://matplotlib.org/3.5.1/api/font_manager_api.html