![]() |
Scheme interface, font selector and debugging | ![]() |
The following user preferences influence the font database and the font selection:
Default "on". When it changes, notify-new-fonts in
progs/texmacs/texmacs/tm-server.scm
calls set-new-fonts, which sets the C++ flag new_fonts. When the flag is off, smart_font reduces to find_font and only the Scheme rewriting rules are used. Menus test the flag with new-fonts?: when it is on, Format\(\rightarrow\)Font and Document\(\rightarrow\)Font open the font selector instead of the old font menus of
progs/fonts/font-old-menu.scm
. The preference can be toggled in the experimental section of the preferences dialog.
When on, the font selector shows the customization widgets (effects, variants, mathematics) directly; otherwise they are available through an Advanced button.
A list of directories added to the font path (tt_extend_font_path, tt_font_path).
Override the default fonts used for sys-chinese, sys-japanese and sys-korean.
The environment variable TEXMACS_FONT_PATH also extends the font path.
The directory
progs/fonts
contains:
fonts-ec.scm,
fonts-adobe.scm,
fonts-x.scm,
fonts-math.scm,
fonts-foreign.scm,
fonts-misc.scm,
fonts-composite.scm,
fonts-truetype.scm
The rewriting rules of the old mechanism (set-font-rules), loaded at boot time. They remain in use for TeX fonts (the master roman) and for all fonts when the new style fonts are disabled.
font-old-menu.scm
The menus text-font-menu, math-font-menu and prog-font-menu, which list fonts by their old names. Loaded lazily.
font-new-widgets.scm
The font selector (dialog and side tool). Loaded lazily through open-font-selector, open-document-font-selector and open-document-other-font-selector.
font-sample.scm
Utilities which build tables of characters and font samples, used by the font selector and by maintainers for comparing fonts.
The commands scan-disk-for-fonts and clear-font-cache, bound to Tools\(\rightarrow\)Fonts\(\rightarrow\)Scan disk for fonts and Tools\(\rightarrow\)Fonts\(\rightarrow\)Clear font cache, are defined in
progs/texmacs/texmacs/tm-tools.scm
.
The routines below are exported in
Scheme/Glue/build-glue-basic.scm
. Arrays of strings are passed as lists of strings, and logical fonts as lists whose first element is the family or master.
(tt-exists? name)
Both call tt_font_exists: does a font file with base name name (without extension) exist in the font path?
(tt-font-name u)
(tt-dump u)
Return the (family style) pairs of the font file at the URL u; print its tables and names; compute the characteristics of the font with base name name (see font characteristics).
(font-database-build-local)
(font-database-extend-local u)
(font-database-build u)
Scan the whole font path, respectively import the fonts at u, and save the local database; the last two functions are the individual steps (add the fonts at u to the in-memory table, compute missing or all characteristics) and do not save anything.
(font-database-build-global)
(font-database-insert-global u)
(font-database-save-local-delta)
Rebuild the global database from the font path, respectively add the fonts at u to it; save the entries of the local database which are not in the global one, and list their families.
(font-database-load)
(font-database-save)
Load the database if necessary; save the local database; keep only the entries of the in-memory table whose files exist.
(font-database-families)
(font-database-styles family)
(font-database-search family style)
(font-database-characteristics family style)
The sorted list of installed families; the sorted styles of a family; the file names of a physical font (subfonts of collections are named file.n.ttf); its characteristics; the substitution rules for a family.
(font-family->master family)
(font-master->families master)
(font-master-features master)
(font-family-features family)
(font-family-strict-features family)
Glue for family_to_master, master_to_families, master_features, family_features, family_strict_features and style_features.
(font-guessed-features family style)
(font-family-guessed-features family pure?)
(font-guessed-distance fam1 sty1 fam2 sty2)
(font-master-guessed-distance master1 master2)
Features guessed from the characteristics and the distances based on them (guessed_features, guessed_distance, characteristic_distance).
(logical-font-public family style)
(logical-font-exact family style)
The logical font of a physical font (logical_font with two arguments), its exact version (logical_font_exact), and the logical font of an internal description (logical_font with four arguments). Example: (logical-font-private "pagella" "rm" "bold" "italic") returns ("TeX Gyre Pagella" "bold" "italic").
(logical-font-family lf)
(logical-font-variant lf)
(logical-font-series lf)
The values of the environment variables font, font-family, font-series and font-shape corresponding to the logical font lf.
(logical-font-search lf)
(logical-font-search-exact lf)
(logical-font-substitute lf)
The closest physical font (search_font with attempt 1, search_font_exact); the logical font after substitutions (apply_substitutions); the logical font in which the given features (in user interface spelling, such as "Small Capitals") replace the features of the same kind (patch_font).
(search-font-families features)
The installed families having at least one style with the given features, and the styles of a family with these features. A style is accepted if, for each requested feature, the asymmetric distance to the exact logical font of the style is zero or smaller than the distance to a font without any feature: for instance, a Black style is accepted when Bold is asked for, but not conversely.
The main family of a family list such as "bold=Fira Sans,TeX Gyre Pagella" (main_family).
The OpenType support added a few routines to the same glue file: font-logical-search (the four argument font_database_search, from a family, variant, series and shape), font-available-features (the GSUB features of a font, ot_font_features, used by the feature menus of
progs/fonts/font-features.scm
), the profile routines math-font-profile-set, math-font-profile, math-font-profile-families, math-font-profile-attr, math-family-for-text and text-family-for-math, set-hand-tuned-math-fonts and hand-tuned-math-fonts?, which switch off the hand-tuned tables of the fonts that also have a MATH table, and font-debug-info, font-debug-info-of and font-debug-report, behind the font inspector. They are described in math font profiles, OpenType features and inspecting the font system.
The font selector is implemented in
progs/fonts/font-new-widgets.scm
. It exists in two forms: a dialog (font-selector) and a side tool (font-tool), the latter being used when side tools are enabled (side-tools?). The public entry points are:
Changes the font at the cursor position (Format\(\rightarrow\)Font in the compressed menus). The getter is get-env and the setter make-multi-with, which inserts or modifies a with tag.
Changes the document wide font (Document\(\rightarrow\)Font). The getter is get-init and the setter init-multi, which modifies the initial environment.
Changes a style parameter of type font, such as the font used for titles: the environment variables are prefixed by prefix (for instance font-series becomes prefix followed by font-series). It is called from the Other entry of the parameter menus in
progs/generic/generic-menu.scm
.
All widgets receive a list specs of the form (getter setter global? [window]). The getter reads environment variables ("font", "font-family", …), the setter applies a flat list of variable/value pairs, and global? tells whether the document wide font is being edited. The current choices are stored in the hash table selector-table, indexed by specs, a variable and the buffer of the window, so that several selectors can coexist. The variables are:
the font variables :family, :style and :size, whose initial values are computed by initial-font-data from the current environment: the internal description is translated into a logical font (logical-font-private) and then into the physical font which is shown as selected (logical-font-search-exact);
the filter variables :weight, :slant, :stretch, :serif, :spacing, :case, :device, :category and :glyphs, whose initial value is "Any";
the customization variables "bold", "italic", "smallcaps", "sansserif", "typewriter", "math", "greek", "bbb", "cal", "frak" (alternative families for these purposes) and "embold", "embbb", "slant", "hmagnify", "vmagnify", "hextended", "vextended" (font effects), whose initial values are parsed from the current values of font and font-effects (initial-customize-get);
the variable :features, a comma separated list of OpenType features for font-features (selector-features), initialized from the document.
The families listed in the family column are (selected-families specs), that is, search-font-families applied to the non "Any" filters (selected-properties); the styles listed for the selected family are given by search-font-styles. The user interface strings ("Sans Serif", "Math Symbols", …) are decoded on the C++ side by decode_feature.
The selected font is (selector-get-font specs): the logical font of the chosen family and style (logical-font-public), patched with the filter properties (logical-font-patch). Hence, choosing the style Regular together with the filter Weight: Bold asks for a bold font even if the chosen family has no bold style.
The new values of the environment variables are obtained with logical-font-family, logical-font-variant, logical-font-series and logical-font-shape. The value of font is completed by the customizations (logical-font-family*), which yields family lists such as "bold=Fira Sans,TeX Gyre Pagella" that are interpreted by the smart fonts; the effects are assembled into a string such as "hmagnify=1.1,bold=2" for font-effects (selector-font-effects). The OpenType features column (a Features tab in the side tool) lists the features of the physical font which the dialog will really use (selector-font-features-available, through font-logical-search and font-features-of-file), and its toggles fill :features, which goes into font-features; see OpenType features.
selector-get-changes compares these values with the current ones and returns the list of changed variables. Each modification of a choice (selector-set) immediately applies the changes through the setter (selector-notify) and refreshes the dependent widgets. The Ok button of the dialog closes it with the remaining changes, and Reset (document fonts only) resets all font variables to their defaults (selector-restore).
The sample area typesets a sample text (set-font-sample-kind selects standard text, mathematics, the current selection, or tables of Unicode characters built by build-character-table) in a with tag with the new variables. When the physical font that will actually be used differs from the requested one, the widget selector-font-simulate-widget shows both (“Requested” and “Replaced by”); the latter is computed with logical-font-search, that is, by the selection algorithm itself.
The Import button (and Import font in the More tab of the side tool) calls font-import, which calls font-database-extend-local on the chosen file and refreshes the lists. The side tool also offers Scan disk for more fonts and Clear font cache.
For a user, a font becomes available as soon as it is in the local database:
Put the file into one of the directories of the font path (for instance $TEXMACS_HOME_PATH/fonts/truetype), or use the Import button of the font selector, which also adds the directory to the font path.
Run Tools\(\rightarrow\)Fonts\(\rightarrow\)Scan disk for fonts (unless the font was imported). The new families appear in the font selector immediately.
If the font gets an unexpected family or style name, inspect it with tt-dump and tt-font-name. If its features are wrong (for instance a sans serif font not recognized as such), they can be corrected in $TEXMACS_HOME_PATH/fonts/font-features.scm; the changes are taken into account at the next start, and they are lost after an upgrade or a cache clearance.
For fonts which are common enough to be known by all installations, the global database has to be extended. A possible workflow is:
Install the fonts and scan the disk.
Run (font-database-save-local-delta), then (font-test) from
font-sample.scm
in a Scheme session: it returns a table showing, for each new font, its name, the weights, slants, stretches and other properties that the selection algorithm associates to it, and some sample text. This makes it easy to spot wrongly classified fonts.
Run (font-database-insert-global (url "dir")) from a development tree (the files in $TEXMACS_PATH/fonts are overwritten), review the guessed features in font-features.bis.scm, merge them by hand into
font-features.scm
(fixing masters and adding categories such as Calligraphic or Handwritten, which cannot be guessed), and restart TeXmacs.
If appropriate, add substitution rules to
font-substitutions.scm
, and a short alias to upgrade_family_name in
Graphics/Fonts/font_translate.cpp
(as for pagella or dejavu).
Mathematical fonts usually also need microtypographic adjustments, which live in the files Plugins/Freetype/adjust_*.cpp, and possibly special cases in the smart fonts (tex_gyre_fix, math_fix, …); see smart fonts and mathematical typesetting. An OpenType math font needs neither: its MATH table provides the layout, and a profile declared with define-math-font-profile in
progs/fonts/fonts-opentype.scm
gives its text, sans serif and typewriter companions and its place in the menus; see math font profiles, shipped fonts and the database.
The database is built around TrueType and OpenType files. Supporting another format which FreeType can read requires changes at several places, which must remain consistent:
font_database_build and font_database_collect (in
font_database.cpp
) decide by extension which files are scanned, respectively kept when filtering the global database;
tt_font_name must be able to extract the family and style names, or a separate reader must be called by font_database_build;
font_database_build_characteristics and font_database_search map locations to font names by removing known extensions;
tt_font_find_sub (in
tt_file.cpp
) decides which extensions are tried when a font name is looked up, and in which order; currently .otf, .ttf, .ttc, .pfb and .dfont, so that .pfb and .dfont files can be used by name but are never scanned. When the order changes, the prefix of the keys of font_cache.scm in tt_font_find has to change too, or the answers cached by an older version keep being used;
the font is finally constructed by unicode_font in the database fallback of find_font_bis, and analyzed by tt_analyze through tt_font and tt_font_metric; both load the file through FreeType.
The glue functions make it easy to replay each step of the selection:
(use-modules (fonts font-sample))
(font-database-styles "TeX Gyre Pagella")
(font-database-characteristics "TeX Gyre Pagella" "Regular")
(logical-font-private "dejavu" "rm" "medium" "small-caps")
(logical-font-substitute (logical-font-private "SimSun" "ss" "medium" "right"))
(logical-font-search (logical-font-private "Garamond" "rm" "medium" "right"))
(font-database-search "DejaVu Serif" "Book")
(show-closest-fonts '("TeX Gyre Pagella" "Regular")) |
The module
font-sample.scm
is only loaded together with the font selector, hence the first line. The last function returns a table of the 25 installed fonts which are closest to the given one according to the guessed distance, together with their characteristics and samples. Remember that the internal caches of search_font, find_closest and font::instances keep old answers: after a change of the database, the selection in the current session may not reflect it.
The database code reports on the standard output:
A family used by a document or style is not installed; it is printed once per name, and followed by TeXmacs] warning, missing font, loading global substitution list the first time.
While deriving the local database from the global one, a file was identified by name and subfont index but not by size.
The global database is merged into the local one at startup (see loading).
Summary of a disk scan and of the computation of the missing characteristics. The progress of each file (Process …, Analyzing …, | Processing …) is only printed when the debug option verbose is on.
A profiled OpenType math font, or its text companion, was installed but missing from the database, and has been added to the local database.
Finer traces can be obtained by uncommenting the cout statements which are already present in search_font, search_font_among, find_closest and the four argument font_database_search.
In order to make sure that a problem is not caused by stale data, quit TeXmacs and remove $TEXMACS_HOME_PATH/fonts/font-*.scm, $TEXMACS_HOME_PATH/fonts/shipped-stamp.scm, $TEXMACS_HOME_PATH/fonts/unpacked and $TEXMACS_HOME_PATH/system/cache/font_cache.scm. At the next start, the local database is derived again from the global one, and the disk has to be scanned again if needed. The command Tools\(\rightarrow\)Fonts\(\rightarrow\)Clear font cache does part of this from within TeXmacs, but a restart is still necessary.
The environment variable font-family contains the variant (rm, ss, tt), while the C++ family is the value of font. Similarly, the style of a physical font combines what TeXmacs calls the series and the shape.
The local database only contains fonts of the global database which are found on disk, plus the fonts found by explicit scans or imports, plus the profiled OpenType math fonts registered when a document asked for them. A newly installed font is invisible until the disk is scanned, and scanned fonts are forgotten after an upgrade or a cache clearance. The fonts of a newer global database, on the other hand, are merged automatically at the next start.
Fonts outside the hard coded font path are never found, unless they are imported or the path is extended with TEXMACS_FONT_PATH. On Unix systems other than macOS, the hard coded path only names TeX Live 2020 to 2022 (see the font path).
Negative answers are cached in font_cache.scm: a font file which was looked up before being installed may remain “missing” until the font cache is cleared.
Subfonts extracted from .ttc collections into fonts/unpacked are not updated when the collection changes.
Rule heads are case sensitive. The old lower case names (pagella, dejavu) go through the Scheme rules when the new style fonts are disabled, whereas the database names (TeX Gyre Pagella, DejaVu) go through the database. When adding rules, make sure not to shadow database families.
The search itself only uses the names and the features of the database, not the characteristics. A style whose weight or slant is expressed by an unusual word (in another language, for instance) is not recognized as such: it may not be selected for a bold or italic request, and a synthetic emphasis (-poorbf, -poorit) is then applied to another style. Adding features or substitution rules fixes this.
Substitution rules are keyed by the family name after upgrade_family_name, and a rule is silently ignored when its target family is not installed.
Only the English names (name identifiers 1 and 2) of a font file are read. A font without English name records gets an empty family name.
The guessed features and distances depend on the rendering of a few glyphs. Decorative or symbol fonts may be misclassified; the features in
font-features.scm
always take precedence over guesses.
The following problems were noticed while writing this documentation:
In
progs/fonts/font-new-widgets.scm
, open-document-other-font-selector calls (open-document-other-font-selector prefix-window) when side tools are disabled; the intended call is (open-document-other-font-selector-window prefix).
In
Graphics/Fonts/smart_font.cpp
, at attempts \(> 1\) of smart_font_rep::resolve, the test v == "rm" is made on an empty string, where variant == "rm" was probably meant; this is harmless, since rm is dropped by variant_features anyway. In the same file, in_unicode_range, which returns a bool, returns "" (that is, true) for strings that cannot be decoded.
In
Plugins/Freetype/tt_analyze.cpp
, the fallback definition of characteristic_distance used without FreeType returns an int, whereas the header declares a double.
The save functions of
font_database.cpp
remove $TEXMACS_PATH/system/cache/file_cache, whereas the caches of
data_cache.cpp
live in $TEXMACS_HOME_PATH/system/cache.