Kiva Font Management

Kiva’s 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.

Note

Some Kiva backends don’t use the FontManager to resolve fonts but instead use internal methods. For example the QPainter backend uses Qt’s font management system to resolve font definitons.

Overview

The basic function of FontManager is to provide a findfont() 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:

  1. Scans the file system for files with known font file extensions (.ttf, .ttc, .otf, .afm) [scan_system_fonts()]

  2. Examines all the font files identified in the first step and extracts their metadata using fonttools. [create_font_database()]

  3. The FontManager is then ready to be used. Font instances call findfont() on the global FontManager singleton 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 (FontEntry, FontDatabase, etc.) change. The FontManager singleton is loaded on-demand by the first call to default_font_manager().

Font Resolution

The findfont() method uses a somewhat complex process for finding the best match to a given font query.

  1. If a directory keyword argument was passed, only fonts whose files are children of directory will be checked. If none match, a default font is returned.

  2. If a directory is 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.

  3. A score is computed for each font using the scoring functions [score_family(), score_size(), score_stretch(), score_style(), score_variant(), and score_weight()]

  4. If the score meets a certain threshold, the matching font is returned. Otherwise a default is returned.

  5. 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.

Adding Custom Fonts

Because font resolution relies heavily on the system that the code is running on, font matching may not always result in a good match for the desired font. Kiva ships with a fallback font in case no matching font can be found (this frequently happens on headless servers with no GUI libraries installed). However developers may want to ensure that the fonts that they want are always available by including the font files in the resources that are packaged with their application.

Kiva provides add_application_fonts() as a mechanism to register additional fonts with the application font resolution system. This function should be called early in the application start-up process with a list of paths of font files to add to the system. These fonts will also be added to the Qt and/or Wx font databases as well, as appropriate.

Typical usage might look something like the following:

from importlib.resources import files
from kiva.fonttools.api import add_application_fonts

font_file_1 = files(my_package.resources) / "my_font_1.ttf"
font_file_2 = files(my_package.resources) / "my_font_2.ttf"

add_application_fonts([font_file_1, font_file_2])

Note

The font files need to be actual files on the filesystem, they can’t be stored in zip files.