![]() |
Extending, debugging and pitfalls | ![]() |
Suppose that a symbol <foo> is drawn in red although it could be built from other symbols. No recompilation is needed for this, only a definition in a .vfn file.
Choose the file. If the symbol should be drawn in the style of the current text font, from the glyphs of that font, put it in the appropriate emu-*.vfn file (relations in emu-relations.vfn, arrows in emu-arrows.vfn, and so on); these fonts are used in extend mode on top of the main font and of emu-fundamental, so the definition can use all symbols of the main font and all the building blocks of emu-fundamental.vfn. If the symbol is a traditional combination which should work with any font, put it in one of the tradi-*.vfn files; these are built on the smart font itself and are only tried after all physical fonts.
Write the definition, as a new entry (foo expression). Use names of more than one character. Prefer the operators which can be drawn as vector graphics (see the reference); use bitmap explicitly when a vector rendering would be wrong. When the definition depends on peculiarities of particular fonts, guard the alternatives with or and font, as in emu-fundamental.vfn:
(minus* (or (font (scale (hor-crop _) (hor-crop +) 1 *)
Baskerville meyne_textur Papyrus)
(scale - + 1 *))) |
Test the symbol with several main fonts, at several magnifications, in text and in mathematics, both on the screen and in a PDF export (the screen uses the bitmap path, printing the vector path). Since translators and fonts are cached, restart TeXmacs after modifying a .vfn file. A personal copy of a file in $TEXMACS_HOME_PATH/fonts/virtual overrides the system one, which is convenient for experiments.
A new .vfn file is not used automatically: the list of emulation fonts is hard-coded in emu_font_names and the list of traditional fonts in initialize_virtual, both in
Graphics/Fonts/smart_font.cpp
. The order of these lists is the order in which the files are searched.
To render a class of characters in a new way:
If the subfont needs the characters in another encoding, add a constant REWRITE_NAME and a case in the static function rewrite.
Choose a specification tree for the subfont, for instance ("my-kind" parameters), and add a branch to smart_font_rep::initialize_font which creates the font. Use smart_font_bis for a variant of the same family, closest_font together with adjusted_dpi for another family, and wrap the result with adjust_subfont if it is not a smart font, so that the horizontal resolution is respected.
Add a rule at the right place of smart_font_rep::resolve (string c) or of resolve (c, fam, attempt), which registers the decision with sm->add_font (key, rewr) and sm->add_char (key, c). Check that the subfont supports the (rewritten) character before registering it, otherwise the character will be invisible rather than red.
The decision must only depend on the family, variant, series and shape of the smart font and on the character, never on its size or resolution, since the smart map is shared by all sizes.
Ranges are defined in get_unicode_range (int code). A new range name can then be used in conditional family entries (myrange=Some Font) and is used automatically in the attempts \(k > 1\), which request the variant variant-myrange from the font database; for this to be useful, the font database must know which fonts cover the range (see the font selection).
To emulate a variant which the font database may lack (for instance a condensed shape), proceed as follows:
Implement the emulated font, following the pattern of the existing ones: a font_rep subclass with a base font, transformed metrics in get_extents and get_xpositions, transformed glyph tables in index_glyph and get_glyph, a draw_fixed which draws through ren->draw on the screen and, if possible, through renderer transformations on printers, a magnify and a constructor function with a unique resource name. Declare it in
Graphics/Fonts/font.hpp
and add the file to the build.
In find_closest (
Graphics/Fonts/font_translate.cpp
), append a new suffix (like -poorbf) to the series, shape or variant when the requested logical feature is not provided by the font which was found.
In find_font (family, variant, series, shape, sz, dpi) (
Graphics/Fonts/find_font.cpp
), recognize the suffix, find the font without it, wrap it with the new emulation, and store the result in font::instances under the full name.
If the emulation should also be available on demand, add an effect to apply_effects (see font effects).
A symbol drawn in red went through the whole resolution algorithm without finding a font: it is rendered by the error font. Check that the name of the symbol is right, that some installed font has it (the font database must be up to date, see font database), or define it virtually.
The font inspector, Tools\(\rightarrow\)Fonts\(\rightarrow\)Font inspector, answers this question without recompiling: it reports the route, the subfont, the file and the rewriting of the character at the cursor, read from the smart map (smart_font_debug_info), and can colour every glyph by its route (debug switch fonts) or list all the characters of a document by route in a font report; see inspecting the font system. For finer questions,
smart_font.cpp
contains many commented out traces, such as //cout << "Found " << c << " in " … in resolve and //cout << "Font " << nr … at the end of initialize_font. Enabling them temporarily is the fastest way to understand a resolution. Remember that decisions are cached in the smart map for the whole session.
Font related messages are written to the debug_fonts stream (channel debug-fonts, visible in the debugging console). The loading of encodings and virtual fonts is reported when the std debug flag is set, for instance with (debug-set "std" #t).
build-character-table in
progs/fonts/font-sample.scm
builds, as Scheme markup, a table with all characters between two code points, for instance (insert (stree->tree (build-character-table #x2190 #x21FF))). Inserting such tables with various values of font shows at a glance which characters are native, emulated or missing.
Switching off the preference "new style fonts" makes smart_font fall back on the rule based find_font. Similarly, (set-hand-tuned-math-fonts #f) typesets the fonts which have both hand-tuned tables and a MATH table (TeX Gyre, Stix) from their table only.
The warning no bitmap available for … comes from the default font_rep::get_glyph: a virtual or emulated font was built on a font which cannot export glyphs. Setting get_glyph_fatal in
Graphics/Fonts/font.cpp
turns these warnings into failures, which gives a backtrace.
"invalid virtual character" (with the offending tree) means that compile_bis met an unknown construct; "bad virtual font format" that a .vfn file does not start with virtual-font; "substitution font loop detected" that initialize_font produced the smart font itself as a subfont.
Smart fonts support everything. smart_font_rep::supports always returns true. In a plain mode virtual font whose base is a smart font (the tradi-* and emu-bracket subfonts), all atoms are therefore considered supported, and (or g1 g2) always selects \(g_1\); only the font test discriminates. Alternatives are meaningful in extend mode virtual fonts on a physical font (the emu-* fonts).
Unknown constructs. An unknown or misspelled operator makes supported return false, which silently disables the glyph in the emu-* fonts (the emulation is only used if it is supported). In the tradi-* fonts, which are selected by name only, the same mistake reaches compile_bis and aborts with "invalid virtual character".
Variables shadow glyphs. with replaces every atom equal to the variable in its body, so a variable named w hides the letter w in the body.
One-character names. A one-byte string is interpreted by get_char as a code (position in the file), not as a name, so entries with one-character names cannot be requested.
Codes of parameterized glyphs. The legacy encoding “code byte followed by a parameter” (used by poor_rubber_font and math_font) only works for entries whose position in the file is below 256 and different from the code of < (60), since the string would otherwise be taken for a symbol name.
Shared smart maps. Since the smart map is shared by all sizes, a subfont which supports a character at one size is assumed to support it at all sizes. Conversely, a smart font may find subfont numbers in the map which it has not initialized yet; always call initialize_font before using fn[nr].
Rewritten runs. Rewriting can change the length of a run (for instance <bbb-A> becomes A). The cursor positions inside such a run are approximated by the position of its start.
Bitmaps in PDF. Glyphs using bitmap-only operators, and bold, blackboard bold, extended, distorted and blurred emulations, are embedded as bitmap (Type 3) fonts. Their quality depends on the resolution of the font. When the main font has a MATH table, a symbol whose emulation would be such a bitmap is taken from STIX Two Math instead, if that font has it; the font report of the inspector says which emulated glyphs are still exported as bitmaps.
The magic slant. The automatic italic emulation uses the slant 0.25001, which
Typeset/Concat/concat_math.cpp
recognizes; do not change one without the other.
Accumulating effects. add-font-effect appends to font-effects, so nested embold tags embolden repeatedly.
The following problems were noticed in the code while writing this documentation:
smart_font.cpp
, resolve (c, fam, attempt), attempts \(k > 1\): the test v == "rm" is made on the still empty string v instead of variant, so the variant rm-range is always requested, never just range.
smart_font.cpp
, in_unicode_range: for an empty conversion it executes return ""; in a function returning bool, which yields true.
virtual_font.cpp
, compile_bis for scale: the @ arguments are processed after the magnifications have been computed and have no effect, so (scale g r @ 1) only scales vertically.
virtual_font.cpp
, fscale: the vertical deepen is applied to the original glyph instead of the result of widen.
virtual_font.cpp
, rot-right: the logical height is computed as ey->x2- ex->x1 (with ex->x1 just set to zero) instead of ey->x2- ey->x1 as for rot-left.
virtual_font.cpp
: deepen and widen are declared vector-capable in supported but are not handled by draw_tree; a glyph using them on vector components would be missing on printers. (The only current use applies them to a circle, which forces the bitmap path.)
poor_effected.cpp
, get_extents: the top of the ink box is merged with min instead of max.
poor_extended.cpp
, get_xpositions: the loops rescale xpos[0..N(s)-1] but not the final position xpos[N(s)].
math_font.cpp
: operator != on fonts returns fn1.rep == fn2.rep.
charmap.cpp
, join_charmap_rep::child: calls child (i-sum) instead of child (ch-sum); harmless as long as all joined charmaps have arity one.
smart_font.cpp
, register_profiled_font: a profiled math font which is missing from the database is registered with font_database_extend_local, which also adds its directory to the preference "imported fonts"; merely opening a document in such a font therefore changes the user preferences and saves the local database.
smart_font.cpp
: is_rubber now also accepts the stretched arrows <rubber-…>, but the pseudo ranges mathrubber and mathlarge of in_unicode_range do not, so that an entry mathrubber=family does not apply to them.
Plugins/Freetype/tt_file.cpp
, tt_font_path: texlive_font_dirs, which finds the TeX Live font directories of any year, is only used on macOS; the other Unix systems still list TeX Live 2020 to 2022 by name.