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.
class matplotlib.font_manager.FontEntry(fname='', name='', style='normal', variant='normal', weight='normal', stretch='normal', size='medium')
[source]
Bases: object
A class for storing Font properties. It is used when populating the font lookup dictionary.
class matplotlib.font_manager.FontManager(size=None, weight='normal')
[source]
Bases: object
On import, the FontManager
singleton instance creates a list of TrueType fonts based on the font properties: name, style, variant, weight, stretch, and size. The findfont()
method does a nearest neighbor search to find the font that most closely matches the specification. If no good enough match is found, a default font is returned.
afmfiles
defaultFont
findfont(self, prop, fontext='ttf', directory=None, fallback_to_default=True, rebuild_if_missing=True)
[source]
Find a font that most closely matches the given font properties.
Parameters: |
|
---|---|
Returns: |
|
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.
static get_default_size()
[source]
Return the default font size.
get_default_weight(self)
[source]
Return the default font weight.
score_family(self, families, family2)
[source]
Returns 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.
score_size(self, size1, size2)
[source]
Returns 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.
score_stretch(self, stretch1, stretch2)
[source]
Returns 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.
score_style(self, style1, style2)
[source]
Returns 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.
score_variant(self, variant1, variant2)
[source]
Returns a match score between variant1 and variant2.
An exact match returns 0.0, otherwise 1.0.
score_weight(self, weight1, weight2)
[source]
Returns 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_default_weight(self, weight)
[source]
Set the default font weight. The initial value is 'normal'.
ttffiles
class matplotlib.font_manager.FontProperties(family=None, style=None, variant=None, weight=None, stretch=None, size=None, fname=None)
[source]
Bases: object
A class for storing and manipulating font properties.
The font properties are those described in the W3C Cascading Style Sheet, Level 1 font specification. The six properties are:
The default font property for TrueType fonts (as specified in the default rcParams) is:
sans-serif, normal, normal, normal, normal, scalable.
Alternatively, a font may be specified using an absolute path to a .ttf file, by using the fname kwarg.
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 require fontconfig to be installed. 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.
copy(self)
[source]
Return a copy of self.
get_family(self)
[source]
Return a list of font names that comprise the font family.
get_file(self)
[source]
Return the filename of the associated font.
get_fontconfig_pattern(self)
[source]
Get a fontconfig pattern suitable for looking up the font as specified with fontconfig's fc-match
utility.
This support does not require fontconfig to be installed or support for it to be enabled. We are merely borrowing its pattern syntax for use here.
get_name(self)
[source]
Return the name of the font that best matches the font properties.
get_size(self)
[source]
Return the font size.
get_size_in_points(self)
[source]
get_slant(self)
Return the font style. Values are: 'normal', 'italic' or 'oblique'.
get_stretch(self)
[source]
Return the font stretch or width. Options are: 'ultra-condensed', 'extra-condensed', 'condensed', 'semi-condensed', 'normal', 'semi-expanded', 'expanded', 'extra-expanded', 'ultra-expanded'.
get_style(self)
[source]
Return the font style. Values are: 'normal', 'italic' or 'oblique'.
get_variant(self)
[source]
Return the font variant. Values are: 'normal' or 'small-caps'.
get_weight(self)
[source]
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'
set_family(self, family)
[source]
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 text.usetex
is True
.
set_file(self, file)
[source]
Set the filename of the fontfile to use. In this case, all other properties will be ignored.
set_fontconfig_pattern(self, pattern)
[source]
Set the properties by parsing a fontconfig pattern.
This support does not require fontconfig to be installed or support for it to be enabled. We are merely borrowing its pattern syntax for use here.
set_name(self, family)
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 text.usetex
is True
.
set_size(self, size)
[source]
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_slant(self, style)
Set the font style. Values are: 'normal', 'italic' or 'oblique'.
set_stretch(self, stretch)
[source]
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_style(self, style)
[source]
Set the font style. Values are: 'normal', 'italic' or 'oblique'.
set_variant(self, variant)
[source]
Set the font variant. Values are: 'normal' or 'small-caps'.
set_weight(self, weight)
[source]
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'
class matplotlib.font_manager.JSONEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)
[source]
Bases: json.encoder.JSONEncoder
Constructor for JSONEncoder, with sensible defaults.
If skipkeys is false, then it is a TypeError to attempt encoding of keys that are not str, int, float or None. If skipkeys is True, such items are simply skipped.
If ensure_ascii is true, the output is guaranteed to be str objects with all incoming non-ASCII characters escaped. If ensure_ascii is false, the output can contain non-ASCII characters.
If check_circular is true, then lists, dicts, and custom encoded objects will be checked for circular references during encoding to prevent an infinite recursion (which would cause an OverflowError). Otherwise, no such check takes place.
If allow_nan is true, then NaN, Infinity, and -Infinity will be encoded as such. This behavior is not JSON specification compliant, but is consistent with most JavaScript based encoders and decoders. Otherwise, it will be a ValueError to encode such floats.
If sort_keys is true, then the output of dictionaries will be sorted by key; this is useful for regression tests to ensure that JSON serializations can be compared on a day-to-day basis.
If indent is a non-negative integer, then JSON array elements and object members will be pretty-printed with that indent level. An indent level of 0 will only insert newlines. None is the most compact representation.
If specified, separators should be an (item_separator, key_separator) tuple. The default is (', ', ': ') if indent is None
and (',', ': ') otherwise. To get the most compact JSON representation, you should specify (',', ':') to eliminate whitespace.
If specified, default is a function that gets called for objects that can't otherwise be serialized. It should return a JSON encodable version of the object or raise a TypeError
.
default(self, o)
[source]
Implement this method in a subclass such that it returns a serializable object for o
, or calls the base implementation (to raise a TypeError
).
For example, to support arbitrary iterators, you could implement default like this:
def default(self, o): try: iterable = iter(o) except TypeError: pass else: return list(iterable) # Let the base class default method raise the TypeError return JSONEncoder.default(self, o)
matplotlib.font_manager.OSXInstalledFonts(directories=None, fontext='ttf')
[source]
[Deprecated] Get list of font files on OS X.
Deprecated since version 3.1.
class matplotlib.font_manager.TempCache(**kwargs)
[source]
Bases: object
[Deprecated] A class to store temporary caches that are (a) not saved to disk and (b) invalidated whenever certain font-related rcParams---namely the family lookup lists---are changed or the font cache is reloaded. This avoids the expensive linear search through all fonts every time a font is looked up.
Deprecated since version 3.0.
get(self, prop)
[source]
invalidating_rcparams = ('font.serif', 'font.sans-serif', 'font.cursive', 'font.fantasy', 'font.monospace')
make_rcparams_key(self)
[source]
set(self, prop, value)
[source]
matplotlib.font_manager.afmFontProperty(fontpath, font)
[source]
Extract information from an AFM font file.
Parameters: |
|
---|---|
Returns: |
|
matplotlib.font_manager.createFontList(fontfiles, fontext='ttf')
[source]
A function to create a font lookup list. The default is to create a list of TrueType fonts. An AFM font list can optionally be created.
matplotlib.font_manager.findSystemFonts(fontpaths=None, fontext='ttf')
[source]
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.
matplotlib.font_manager.get_font(filename, hinting_factor=None)
[source]
matplotlib.font_manager.get_fontconfig_fonts(fontext='ttf')
[source]
List the font filenames known to fc-list
having the given extension.
matplotlib.font_manager.get_fontext_synonyms(fontext)
[source]
Return a list of file extensions extensions that are synonyms for the given file extension fileext.
matplotlib.font_manager.is_opentype_cff_font(filename)
[source]
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.
matplotlib.font_manager.json_dump(data, filename)
[source]
Dumps a data structure as JSON in the named file.
Handles FontManager and its fields. 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).
matplotlib.font_manager.json_load(filename)
[source]
Loads a data structure as JSON from the named file.
Handles FontManager and its fields. Relative file paths are interpreted as being relative to the Matplotlib data path, and transformed into absolute paths.
matplotlib.font_manager.list_fonts(directory, extensions)
[source]
Return a list of all fonts matching any of the extensions, found recursively under the directory.
matplotlib.font_manager.ttfFontProperty(font)
[source]
Extract information from a TrueType font file.
Parameters: |
|
---|---|
Returns: |
|
matplotlib.font_manager.win32FontDirectory()
[source]
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.
matplotlib.font_manager.win32InstalledFonts(directory=None, fontext='ttf')
[source]
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'.
© 2012–2018 Matplotlib Development Team. All rights reserved.
Licensed under the Matplotlib License Agreement.
https://matplotlib.org/3.1.1/api/font_manager_api.html