![]() |
The font database and font selection | ![]() |
When a document asks for the font "pagella" in bold italic, or for a font which is not installed at all, or for a Chinese character in a document typeset in Computer Modern, TeXmacs has to decide which physical font file will actually be used. This decision is taken by the font selection machinery, which relies on a font database describing the fonts that are available on the system: their family and style names, the files that contain them, a list of features (sans serif, monospaced, calligraphic, etc.) and a list of automatically computed characteristics (supported scripts, slant, x-height, stroke widths, etc.).
This chapter describes the implementation of the database, of the selection algorithm and of the user interface built on top of them. It assumes that the reader is familiar with the general overview in TeXmacs fonts, which explains what a font is and lists the concrete font classes; the present chapter does not repeat that material. The smart fonts, which combine several physical fonts in order to render arbitrary characters, as well as emulated, virtual and “poor man's” synthetic fonts, are documented in the chapter on smart fonts. Here we only describe the interface between smart fonts and the database, that is, how a smart font asks the database for the closest font able to render a given character. How fonts are selected from the user's point of view, and which files make up the database, is described at a higher level in the reference chapter fonts, from selection to glyph; the mathematical fonts shipped with TeXmacs are presented in the section on mathematical fonts of the user manual, and the way the database registers the OpenType math fonts in math font profiles, shipped fonts and the database.
The central difficulty of font selection is that two unrelated naming schemes have to be reconciled.
Inside documents, fonts are described by the environment variables font (the family, such as roman, pagella or TeX Gyre Pagella), font-family (the variant: rm, ss, tt, …), font-series (the weight: medium, bold, …) and font-shape (right, italic, slanted, small-caps, …), together with the size. Notice the unfortunate terminology: the environment variable font-family holds what the C++ code calls the variant, while the C++ family is the value of font.
Font files declare a family name (such as DejaVu Sans Mono) and a style name (such as Bold Oblique) in their name table. The database is indexed by such pairs (family, style).
Between both, the selection code uses a third, purely internal representation: the logical font, which is an array of strings whose first element is a master family name and whose other elements are normalized features, for instance ["DejaVu", "mono", "sansserif", "bold"]. Both internal and physical descriptions are translated into logical fonts, and the selection consists of finding the physical font whose logical description is closest to the requested one.
From bottom to top, the implementation consists of the following layers.
The files
Plugins/Freetype/tt_tools.cpp
and
Plugins/Freetype/tt_file.cpp
read TrueType and OpenType files (including .ttc collections) without going through FreeType, extract the family and style names (tt_font_name), and locate font files on disk (tt_font_path, tt_font_find, tt_font_exists; a name is looked for as .otf, .ttf and .ttc before .pfb).
tt_tools.cpp
also reads the OpenType layout tables MATH, GSUB and GPOS, which serve the fonts themselves rather than the database (see the OpenType layout tables). The file
Plugins/Freetype/tt_analyze.cpp
renders a few glyphs with FreeType in order to compute the characteristics of a font (tt_analyze).
The file
Graphics/Fonts/font_database.cpp
maintains the global hash tables font_table, font_features, font_variants, font_characteristics and font_substitutions, loads and saves them as Scheme files, builds them by scanning the disk, and answers elementary queries (font_database_families, font_database_styles, font_database_search, font_database_characteristics, …).
The file
Graphics/Fonts/font_select.cpp
translates physical fonts into logical fonts (logical_font, logical_font_exact), defines a distance between logical fonts and implements the search for the closest physical font (search_font). The file
Graphics/Fonts/font_guess.cpp
derives features from characteristics (guessed_features) and defines a finer “guessed” distance used to break ties (guessed_distance).
The file
Graphics/Fonts/font_translate.cpp
converts between the internal scheme and logical fonts (logical_font with four arguments, get_family, get_variant, get_series, get_shape), upgrades old family names (upgrade_family_name) and implements find_closest and closest_font, which return the closest available font for a given internal description, possibly decorated with synthetic bold, italic, small capitals or blackboard bold.
The file
Graphics/Fonts/find_font.cpp
constructs the actual font objects from font names, either through rewriting rules declared in Scheme (the “old” mechanism) or, when no rule exists for a family, through the database.
The typesetter calls smart_font from edit_env_rep::update_font (
Typeset/Env/env_semantics.cpp
) whenever a font related environment variable changes. Smart fonts call closest_font repeatedly, for the main font and for each character that the main font cannot render.
Most database and selection routines are exported to Scheme (see
Scheme/Glue/build-glue-basic.scm
). The font selector dialog and side tool in
progs/fonts/font-new-widgets.scm
are written entirely in terms of these routines.
The following sequence summarizes what happens when the typesetter encounters the markup <with|font|pagella|font-series|bold|…>. The details of each step are given in the subsequent sections.
edit_env_rep::update_font calls smart_font ("pagella", "rm", "bold", "right", sz, dpi).
When the preference "new style fonts" is enabled (the default), smart_font_bis applies a few family name fixes (here tex_gyre_fix rewrites pagella into TeX Gyre Pagella; the last one, profile_fix, replaces a text family by the math font of its profile in math shapes, and conversely, and registers a profiled font which is installed but missing from the database) and calls closest_font on the main family in order to construct the base font of a new smart font.
find_closest translates the request into the logical font ["TeX Gyre Pagella", "bold"], applies the substitutions from
font-substitutions.scm
, and calls search_font, which returns the physical font ("TeX Gyre Pagella", "Bold"). This pair is translated back into the internal description ("TeX Gyre Pagella", "rm", "bold", "right").
find_font finds no Scheme rewriting rule for the family TeX Gyre Pagella, so it asks the database for the files of that font (font_database_search) and constructs unicode_font ("texgyrepagella-bold", sz, dpi).
Later, when the smart font meets a character that is not supported by this font, it calls closest_font again with an increasing attempt number and a variant that mentions the Unicode range of the character, so that the database returns successively other fonts which do support that range.
Graphics/Fonts/font.hpp
Declarations of all public database and selection routines (sections “Font database” and “Font selection” at the end of the file) and of FONT_ATTEMPTS.
Graphics/Fonts/font_database.cpp
The database tables, their loading, saving, filtering and building.
Graphics/Fonts/font_select.cpp
Features, logical fonts, distances, search_font, patch_font, apply_substitutions.
Graphics/Fonts/font_guess.cpp
Features guessed from characteristics and guessed distances.
Graphics/Fonts/font_translate.cpp
Translation from and to the internal naming scheme, find_closest, closest_font.
Graphics/Fonts/find_font.cpp
Font rules and the construction of fonts from names (find_font).
Graphics/Fonts/smart_font.cpp
Smart fonts (smart_font, main_family, get_unicode_range), see smart fonts.
Graphics/Fonts/math_font_profiles.cpp
The profiles of the named OpenType math fonts (math_font_profile_attr, math_family_for_text, text_family_for_math), declared from Scheme in
progs/fonts/fonts-opentype.scm
; see math font profiles.
Plugins/Freetype/tt_file.cpp,
tt_tools.cpp,
tt_analyze.cpp
Locating, parsing and analyzing font files.
The global database shipped with TeXmacs (in the source tree:
src/TeXmacs/fonts
).
progs/fonts/
Font rules for the old mechanism (fonts-*.scm), the old font menus (
font-old-menu.scm
), the font selector (
font-new-widgets.scm
), tools for sampling and comparing fonts (
font-sample.scm
), the profiles and menus of the OpenType math fonts (
fonts-opentype.scm
,
font-short-menu.scm
), the OpenType features (
font-features.scm
) and the font inspector (
font-debug.scm
, see inspecting the font system).