![]() |
The character resolution algorithm | ![]() |
When a smart font meets a character c for which its smart map has no entry yet, it calls smart_font_rep::resolve (string c). This routine decides once and for all (for the given family, variant, series and shape) which subfont will render c, registers the decision with sm->add_char and returns the subfont number. The algorithm is a long sequence of rules; the first rule which applies wins. It is helpful to distinguish
special rules for mathematics, applied only if math_kind != 0;
the main search loop over the families of the family list and over increasing attempts, implemented by the auxiliary routines resolve (c, fam, attempt) and resolve_rubber (c, fam, attempt);
the final fallbacks: mathematical alphabets, standard virtual fonts, the old TeX fonts and the error font.
The number of attempts is bounded by FONT_ATTEMPTS (20, see
Graphics/Fonts/font.hpp
). Attempt 1 means “the font requested by the user”; the subsequent attempts ask the font database for less and less close matches (see font selection).
If the smart font is in one of the mathematical shapes (math_kind != 0), the following rules are tried in this order:
Upright letters <up-x>: if the main font supports substitute_upright (c), use subfont up (the main font with REWRITE_UPRIGHT).
Upright Greek <upalpha> or <up-alpha>: if the main font supports the plain Greek letter, use upright-greek.
Greek letters (is_greek), unless the shape is mathupright or the family list contains more than one unconditional family (use_italic_greek, a documented hack, which is overridden when the main font is an untuned OpenType math font, ot_math): if the main font has the mathematical italic Greek letter of the U+1D6E2 block, use italic-greek (main font with REWRITE_ITALIC_GREEK); otherwise use italic-math, the italic variant of the family.
<imath>, <jmath>, <ell>: italic-math.
The primes ' and ‘, if no family in the list provides <#2B9> resp. <backprime> (is_italic_prime): italic-math.
Characters with a special rewriting (is_special): the subfont special with REWRITE_SPECIAL. Single ASCII characters in typewriter variants (-tt) are excluded, and big operators <big-…> are excluded when the main family is a TeX Gyre … Math font or when the family list contains mathlarge= or mathbigop=, since these provide true big operators.
Characters defined in emu-bracket.vfn (find_in_emu_bracket), unless the main font is italic: ("virtual" "emu-bracket").
<langle> and <rangle>, if the main font is not italic and has a slash: the subfont emu-bracket with REWRITE_EMULATE, which draws <emu-langle> from two halves of a slash.
Independently of math_kind, for the main family roman in shape mathupright and one of the variants rm, ss, tt, single non-letter characters are sent to italic-roman.
for (int attempt= 1; attempt <= FONT_ATTEMPTS; attempt++) {
if (attempt > 1 && substitute_math_letter (c, math_kind) != "") break;
for (int i= 0; i < N(a); i++) {
if (ot_math && is_rubber (c) &&
(starts (c, "<wide-") || starts (c, "<rubber-"))) {
int nr= resolve_rubber (c, a[i], attempt);
if (nr >= 0) return nr;
}
int nr= resolve (c, a[i], attempt);
if (nr >= 0) return nr;
if (is_rubber (c)) {
nr= resolve_rubber (c, a[i], attempt);
if (nr >= 0) return nr;
}
if (starts (c, "<wide-")) {
if (fn[SUBFONT_MAIN]->supports (c))
return sm->add_char (tuple ("main"), c);
if (series == "bold")
return sm->add_char (tuple ("poor-bold"), c);
}
}
} |
Here a is the family list split at the commas. The outer loop is over attempts and the inner loop over families, so all families of the list are tried with the exact request before any of them is tried with a less close match. Unicode mathematical alphanumeric symbols only get one attempt, since they have a dedicated fallback (step 3). An OpenType math font stretches its own wide accents, braces and arrows: for such a main font, <wide-…> and <rubber-…> are tried as rubber characters before the emulations which resolve would find.
resolve (c, fam, attempt) returns a subfont number or -1:
If fam is a conditional entry conditions=family, the conditions are checked as described in family lists; if they fail the routine returns -1. The family is normalized with tex_gyre_fix and kepler_fix. In the shape mathitalic with math_kind != 0, Greek letters, bold letters <b-…> and dotless letters are looked up in a whole smart font for family (("subfont" family)), so that the mathematical rules of step 1 apply to that font too.
For a single letter in italic shape, the suffix Math of TeX Gyre and Stix families is removed: math fonts are only used for symbols.
Attempt 1.
The pseudo families cal, cal*, Bbb, Bbb**** only accept upper case letters, cal** and Bbb* only letters.
If fam is the main family and the main font supports c, use main. For another family, use closest_font (fam, variant, series, rshape, sz, dpi, 1) if it supports c; the subfont is (fam variant series rshape "1").
Legacy TeX fonts: Greek characters for the family roman go to greek; for the families of is_math_family the subfont math (with REWRITE_MATH) is tried; for roman and cyrillic, multi-byte characters are tried in cyrillic (with REWRITE_CYRILLIC).
The ideographic space <#3000> is ignored.
Double struck letters <bbb-X> (strings of length 7), when the main family is not a TeX Gyre font and the font has the plain letter X: a poor-bbb subfont. The pen width and height are taken from the database characteristics of the font (get_up_pen_width, get_up_pen_height), rescaled by the x-height and bounded below by a quarter of the line width wline.
<it-…>: the subfont it with REWRITE_ITALIC.
If fam is the main family and the main font is not italic, the emulation virtual fonts are tried in the order emu-fundamental, emu-greek, emu-operators, emu-relations, emu-orderings, emu-setrels, emu-arrows (emu_font_names). The first one which defines c (virtually_defined) and actually supports it with the glyphs of the main font is used, as a subfont ("emulate" name). In this way a symbol missing in the main font is preferably constructed from glyphs of the main font, before looking for it in other fonts.
There is one exception: when the main font has a MATH table (math type MATH_TYPE_OPENTYPE or MATH_TYPE_TEX_GYRE) and the construction could only be exported as a bitmap (virtual_font_draws_vectors is false), the symbol is taken from STIX Two Math, which is shipped with TeXmacs, if that font has it (resolve_shipped_math, subfont shipped-math). The document then looks the same on every system, as with an emulation, but exports as vectors. Only the few symbols which no font has, such as <triangleup>, are still exported as bitmaps.
Attempts \(k > 1\). The routine looks for a font for the Unicode range of c (get_unicode_range): the variant becomes variant-range (for instance rm-greek or rm-mathsymbols), which the font selection translates into a requirement on the supported scripts, and closest_font (fam, v, series, rshape, sz, dpi, k-1) is tried. If it supports c, the subfont (fam v series rshape "k-1") is used.
Characters recognized by is_rubber, that is <left-…>, <mid-…>, <right-…>, <large-…>, and also the wide accents <wide-…> and the stretched arrows <rubber-…>, are handled by resolve_rubber (c, fam, attempt). It extracts the delimiter name (for instance ( from <left-(-3>). Null delimiters (. and <nobracket>) are sent to ignore. When the global has_poor_rubber (
Graphics/Fonts/font.cpp
) is set, some delimiters are replaced by a simpler goal whose presence in the font is enough to build the delimiter: <mid> for <sqrt> and double bars, / for angle brackets, [ and ] for floors, ceilings and double brackets. The goal is resolved like an ordinary character in the main family of the entry, giving a subfont \(k\); the delimiter is then rendered by the subfont ("rubber" k), which is rubber_font (fn[k]), if that font supports it. A long arrow whose long form the font lacks (<rubber-longrightarrow>, for instance) is built on the plain arrow. Italic main fonts never provide rubber characters.
The rubber_font wrapper (in
Graphics/Fonts/font.cpp
) caches one extensible font per base font and lets the base font choose it with the virtual method make_rubber_font. A Unicode font with a MATH table returns a rubber_unicode_font which takes the size variants and the assemblies of the table (see stretchable glyphs); a smart font whose main font is an OpenType math font hands the job to it. The default font_rep::make_rubber_font chooses rubber_stix_font for Stix (unless the hand-tuned tables are switched off, hand_tuned_math_fonts), the base font itself if its name mentions mathlarge= or mathrubber= (the font is then expected to handle rubber characters itself), poor_rubber_font for other Unicode fonts when has_poor_rubber holds (the default), and rubber_unicode_font otherwise. See emulated fonts for poor_rubber_font.
Wide accents <wide-…> which are not resolved in a family are first tried as rubber characters (above), which only succeeds when the extensible font of the family supports them, as the rubber_unicode_font of a font with a MATH table does; they are then taken from the main font if it supports them, and otherwise, in bold series, from a poor-bold subfont (an emulated bold version of the medium font).
If the main loop failed, the following fallbacks are tried:
Mathematical alphanumeric symbols (U+1D400 to U+1D7FF and the letter-like symbols U+2100 to U+213F): substitute_math_letter (c, math_kind) returns the name of an alphabet subfont (bold-math, italic-math, cal, frak, bbb, ss, tt, …), which renders the corresponding plain letter thanks to REWRITE_LETTERS. Script, fraktur and double struck letters are only substituted in mathupright mode (math_kind == 2), and no substitution takes place outside mathematics.
If c is defined in one of the traditional virtual fonts tradi-long, tradi-negate, tradi-misc (find_in_virtual), the subfont ("virtual" name). These virtual fonts are built on the smart font itself, so their components (arrows, bars, relations) are resolved recursively by the same algorithm.
In mathematics, characters which have no Unicode code point (unicode_provides is false) and are not delimiters go to other, a smart font for the family roman in shape mathitalic, that is the old TeX math fonts.
Otherwise the character is drawn in red with the error font.
Consider the family list mathlarge=TeX Gyre Pagella,Linux Libertine in mathematical mode, and the string x<leq><big-sum-2><alpha>.
x is an isolated letter, hence it is sent directly to the italic subfont italic_nr without being resolved. (For an untuned OpenType math font, this subfont is ot-italic, which draws <#1D465> from the main font.)
<leq>: step 1 does not apply; in the main loop the conditional entry mathlarge=TeX Gyre Pagella fails (not a large symbol), and the main family Linux Libertine is tried. If the main font has the symbol, the subfont is main. If not, the emulation fonts are tried; then, with attempt 2, the closest font with variant rm-mathsymbols.
<big-sum-2>: is_special would rewrite it into <sum>, but this rule is disabled because the family list contains mathlarge=. In the main loop, the condition mathlarge holds (big operator), so TeX Gyre Pagella is tried; tex_gyre_fix turns it into TeX Gyre Pagella Math for the medium math shape, whose Unicode font supports the big operators natively (see tex_gyre_native in
Plugins/Freetype/unicode_font.cpp
).
<alpha>: step 1 sends it to italic-greek if the main font has <#1D6FC> (mathematical italic alpha), and to italic-math (Linux Libertine Italic) otherwise.
The results are memorized in the smart map, so the next occurrence of these characters, in any size, costs a table lookup.