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.
Experimental support is included for using fontconfig on Unix variant platforms (Linux, OS X, Solaris). To enable it, set the constant USE_FONTCONFIG in this file to True. Fontconfig has the advantage that it is the standard way to look up fonts on X11 platforms, so if a font is installed, it is much more likely to be found.
Bases: object
A class for storing Font properties. It is used when populating the font lookup dictionary.
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.
Search the font list for the font that most closely matches the FontProperties prop.
findfont() 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 Vera Sans) is returned.
directory, is specified, will only return fonts from the given directory (or subdirectory of that directory).
The result is cached, so subsequent lookups don’t have to perform the O(n) nearest neighbor search.
If fallback_to_default is True, will fallback to the default font family (usually “Bitstream Vera Sans” or “Helvetica”) if the first lookup hard-fails.
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.
Returns a match score between the list of font families in families and the font family name family2.
An exact match anywhere in the list returns 0.0.
A match by generic font name will return 0.1.
No match will return 1.0.
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.
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.
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.
Returns a match score between variant1 and variant2.
An exact match returns 0.0, otherwise 1.0.
Returns a match score between weight1 and weight2.
The result is the absolute value of the difference between the CSS numeric values of weight1 and weight2, normalized between 0.0 and 1.0.
Set the default font weight. The initial value is ‘normal’.
Update the font dictionary with new font files. Currently not implemented.
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:
- family: A list of font names in decreasing order of priority. The items may include a generic font family name, either ‘serif’, ‘sans-serif’, ‘cursive’, ‘fantasy’, or ‘monospace’. In that case, the actual font to be used will be looked up from the associated rcParam in matplotlibrc.
- style: Either ‘normal’, ‘italic’ or ‘oblique’.
- variant: Either ‘normal’ or ‘small-caps’.
- stretch: A numeric value in the range 0-1000 or one of ‘ultra-condensed’, ‘extra-condensed’, ‘condensed’, ‘semi-condensed’, ‘normal’, ‘semi-expanded’, ‘expanded’, ‘extra-expanded’ or ‘ultra-expanded’
- weight: 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’
- 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
The default font property for TrueType fonts (as specified in the default matplotlibrc file) 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. See the documentation on fontconfig patterns. 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.
Return a deep 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.
See the documentation on fontconfig patterns.
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.
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 text.usetex 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.
See the documentation on fontconfig patterns.
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.
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 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’
Get list of font files on OS X - ignores font suffix by default.
Bases: object
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.
A function for populating a FontKey instance by extracting information from the AFM font file.
font is a class:AFM instance.
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.
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.
Grab a list of all the fonts that are being tracked by fontconfig by making a system call to fc-list. This is an easy way to grab all of the fonts the user wants to be made available to applications, without needing knowing where all of them reside.
Return a list of file extensions extensions that are synonyms for the given file extension fileext.
Returns True if 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.
Return a list of all fonts matching any of the extensions, possibly upper-cased, found recursively under the directory.
Equivalent to pickle.dump(data, open(filename, ‘w’)) but closes the file to prevent filehandle leakage.
Equivalent to pickle.load(open(filename, ‘r’)) but closes the file to prevent filehandle leakage.
A function for populating the FontKey by extracting information from the TrueType font file.
font is a FT2Font instance.
flatten a ttfdict to all the filenames it contains
Return the weight property as a numeric value. String values are converted to their corresponding numeric value.
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.
Search for fonts in the specified font directory, or use the system directories if none given. A list of TrueType font filenames are returned by default, or AFM fonts if fontext == ‘afm’.
A module for parsing and generating fontconfig patterns.
See the fontconfig pattern specification for more information.
Bases: object
A simple pyparsing-based parser for fontconfig-style patterns.
See the fontconfig pattern specification for more information.
Parse the given fontconfig pattern and return a dictionary of key/value pairs useful for initializing a font_manager.FontProperties object.
sub(repl, string[, count = 0]) –> newstring Return the string obtained by replacing the leftmost non-overlapping occurrences of pattern in string by the replacement repl.
sub(repl, string[, count = 0]) –> newstring Return the string obtained by replacing the leftmost non-overlapping occurrences of pattern in string by the replacement repl.
Given a dictionary of key/value pairs, generates a fontconfig pattern string.
sub(repl, string[, count = 0]) –> newstring Return the string obtained by replacing the leftmost non-overlapping occurrences of pattern in string by the replacement repl.
sub(repl, string[, count = 0]) –> newstring Return the string obtained by replacing the leftmost non-overlapping occurrences of pattern in string by the replacement repl.