Kiva Font Management¶
FontManager code originated in the Matplotlib code base. Until
version 5.1.0 it was largely unchanged. From 5.1.0 forward, it has been
refactored to emphasize that it’s an internal detail which should be avoided by
3rd party code.
This document aims to describe the structure of the code circa version 5.1.0.
The basic function of
FontManager is to provide a
method which can be called by the
Font class to resolve a font name
or font file location on the system where the code is running.
To accomplish this, it:
Scans the file system for files with known font file extensions (
Examines all the font files identified in the first step and extracts their metadata using fonttools. [
FontManageris then ready to be used.
findfont()on the global
FontManagersingleton as needed.
Because scanning a system for available fonts is quite an expensive operation,
FontManager stores a
pickled copy of its global
singleton in a cache file. And because
pickle is notoriously brittle, the
font manager has a
__version__ attribute must be incremented any
time the attributes or their classes (
etc.) change. The
FontManager singleton is loaded on-demand by the
first call to
findfont() method uses a somewhat complex process for finding the
best match to a given font query.
directorykeyword argument was passed, only fonts whose files are children of
directorywill be checked. If none match, a default font is returned.
directoryis not specified, the search is first narrowed by the font family (or families) designated by the query. This is possible because the match scoring algorithm gives very bad scores to fonts whose family does not match the query.
A score is computed for each font using the scoring functions [
If the score meets a certain threshold, the matching font is returned. Otherwise a default is returned.
If the query was successful, it is added to a local query cache which will avoid the scoring process if a matching query is later performed again.