GNU TeXmacsUser manualReferenceAboutEnglish
Scheme interface, font selector and debugging

1.Preferences

The following user preferences influence the font database and the font selection:

"new style fonts"

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.

"advanced font customization"

When on, the font selector shows the customization widgets (effects, variants, mathematics) directly; otherwise they are available through an Advanced button.

"imported fonts"

A list of directories added to the font path (tt_extend_font_path, tt_font_path).

"default chinese font name", "default japanese font name", "default korean font name"

Override the default fonts used for sys-chinese, sys-japanese and sys-korean.

The environment variable TEXMACS_FONT_PATH also extends the font path.

2.Scheme modules

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

.

3.The glue API

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.

3.1.Font files

(tt-exists? name)

(font-exists-in-tt? name)
(is a font file available?)

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)

(tt-analyze name)
(inspect font files)

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

3.2.The database

(font-database-build-local)

(font-database-extend-local u)

(font-database-build u)

(font-database-build-characteristics force?)
(build the local database)

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)

(font-database-delta-families)
(maintenance of the global database)

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)

(font-database-filter)
(low level operations)

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)

(font-database-substitutions family)
(queries)

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.

3.3.Features and distances

(font-family->master family)

(font-master->families master)

(font-master-features master)

(font-family-features family)

(font-family-strict-features family)

(font-style-features style)
(features)

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)

(characteristic-distance chars1 chars2)
(guesses)

Features guessed from the characteristics and the distances based on them (guessed_features, guessed_distance, characteristic_distance).

3.4.Logical fonts

(logical-font-public family style)

(logical-font-exact family style)

(logical-font-private font variant series shape)
(construct logical fonts)

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)

(logical-font-shape lf)
(back to the internal scheme)

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)

(logical-font-patch lf features)
(search and modify)

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)

(search-font-styles family features)
(filter by properties)

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.

(font-family-main font)
(main family)

The main family of a family list such as "bold=Fira Sans,TeX Gyre Pagella" (main_family).

3.5.The OpenType additions

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.

4.The font selector

4.1.Entry points

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:

(open-font-selector)

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.

(open-document-font-selector)

Changes the document wide font (Document\(\rightarrow\)Font). The getter is get-init and the setter init-multi, which modifies the initial environment.

(open-document-other-font-selector prefix)

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

.

4.2.State

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:

4.3.From the widgets to the database

5.Adding fonts

5.1.Making a new font available

For a user, a font becomes available as soon as it is in the local database:

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

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

5.2.Adding a font to the global database

For fonts which are common enough to be known by all installations, the global database has to be extended. A possible workflow is:

  1. Install the fonts and scan the disk.

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

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

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

5.3.Supporting a new font format

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:

6.Debugging

6.1.Inspecting the selection in a Scheme session

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.

6.2.Console messages and traces

The database code reports on the standard output:

TeXmacs] missing 'X' family (or master)

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.

TeXmacs] approximating font …

While deriving the local database from the global one, a file was identified by name and subfont index but not by size.

TeXmacs] the shipped font database changed, merging it, TeXmacs] the local font database lost entries, merging again

The global database is merged into the local one at startup (see loading).

TeXmacs] scanned n new font file(s), skipped m already known, TeXmacs] analyzed k font style(s)

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.

TeXmacs] registering family, the math font of file

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.

6.3.Starting from a clean state

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.

7.Pitfalls

7.1.Known problems in the current code

The following problems were noticed while writing this documentation:

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with no Invariant Sections, with no Front-Cover Texts, and with no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License".