![]() |
Mathematics from the MATH table | ![]() |
A font with an OpenType MATH table says how formulas should be laid out in it: where the axis is, how far a fraction moves its numerator up, how close a subscript may come to a superscript, where an accent attaches to a letter, how much a script may cut into the corner of its base. This page describes how TeXmacs turns those data into the parameters of its fonts, and where the typesetter uses them. The parsing of the table is described in the OpenType layout tables, the stretchable glyphs (size variants and assemblies) in stretchable glyphs: variants and assemblies, and the way a document arrives at such a font in math font profiles, shipped fonts and the database. For the user's point of view, see how mathematical fonts work.
The design rule is that the hand-tuned tables win. TeXmacs had per-font corrections long before it read MATH tables: the adjust_*.cpp tables and the STIX, TeX Gyre, Linux Libertine, Linux Biolinum, Fira Sans and Papyrus branches of
unicode_font.cpp
, tuned against the layout of TeXmacs itself. The table is layered under them: it fills what they leave open, and they keep precedence wherever they say something.
Graphics/Fonts/font.hpp,
font.cpp
The parameters of font_rep, the virtual hooks with their neutral defaults, copy_math_pars, and the switch hand_tuned_math_fonts.
Plugins/Freetype/unicode_font.cpp
Activation (init_ot_math), the conversion from design units, and the implementations of the hooks for fonts with a table.
Typeset/Boxes/Composite/script_boxes.cpp
Scripts and limits.
Typeset/Boxes/Composite/math_boxes.cpp
Fractions, radicals, wide accents, over- and underlines.
Typeset/Concat/concat_math.cpp
Radicals and fractions at the level of the concatenator, long arrows with labels, accents over dotless letters, negated relations.
Typeset/Env/env_semantics.cpp
Script sizes and script alternates.
Typeset/Boxes/Basic/text_boxes.cpp, the composite and modifier boxes
The box side of the hooks.
A unicode_font_rep is made for one font file at one size and resolution. Its constructor creates the shared face of the file (tt_face (family)), and if the face carries a MATH table it calls init_ot_math before it looks at the family name. The order of the constructor is therefore:
the generic metrics of every Unicode font (x-height, axis, script positions guessed from the glyphs, rule widths);
init_ot_math when the font has a table, which sets ot_math and overwrites or fills the parameters below;
the per-family branches, guarded by tuned= hand_tuned_math_fonts || !has_ot: a tuned font installs its correction tables (lsub_correct, rsup_correct, above_correct, …) and global corrections;
only if no branch matched and the font has a table, math_type= MATH_TYPE_OPENTYPE.
Two different flags thus come out of the constructor, and the code tests one or the other on purpose:
The font has a MATH table and its parameters were loaded. This is true for the tuned TeX Gyre math fonts too.
The font has a table and no hand-tuned branch claimed it. Glyph-level data which the tuned tables already cover, italic corrections and cut-ins, are only taken from the table for such fonts; the tuned fonts keep MATH_TYPE_STIX, MATH_TYPE_TEX_GYRE or MATH_TYPE_NORMAL.
The switch hand_tuned_math_fonts (
font.cpp
, default true) turns the tuned branches off for fonts which have a table, so that the table-only result can be compared with the tuned one. It is set from the preference hand tuned math fonts (notify-hand-tuned-math-fonts in
tm-server.scm
, which calls set-hand-tuned-math-fonts), and the sample scripts of
tests/opentype
turn it off with TM_HAND_TUNED=off. The same switch also decides whether the STIX rubber font is used (font_rep::make_rubber_font) and the STIX special cases of
concat_math.cpp
. Fonts are cached, so the switch only affects fonts made after it was changed.
init_ot_math converts the constants with design_unit_to_metric (vertical lengths) and design_unit_to_metric_x (horizontal lengths), whose factors init_design_unit_factor computes from units_per_EM, the size and the resolution of the font. It writes two kinds of fields of font_rep.
The axis, from axisHeight, if positive.
The default rule width, from fractionRuleThickness, if positive.
The script positions, from subscriptShiftDown, subscriptTopMax, superscriptBottomMin, superscriptShiftUp and the cramped shift; only when both shifts are positive. ysup_hi_lim has no counterpart in the table and becomes the larger of the superscript shift and the x-height.
font.hpp
), all zero for a font without table:
frac_rule_thickness, frac_num_shift_up, frac_num_disp_shift_up, frac_num_gap_min, frac_num_disp_gap_min, frac_denom_shift_down, frac_denom_disp_shift_down, frac_denom_gap_min, frac_denom_disp_gap_min.
sqrt_ver_gap, sqrt_ver_disp_gap, sqrt_rule_thickness, sqrt_extra_ascender, sqrt_degree_rise_percent, sqrt_kern_before_degree, sqrt_kern_after_degree.
upper_limit_gap_min, upper_limit_baseline_rise_min, lower_limit_gap_min, lower_limit_baseline_drop_min, stretch_stack_top_shift_up, stretch_stack_bottom_shift_down, stretch_stack_gap_above_min, stretch_stack_gap_below_min.
sub_sup_gap_min, sup_drop_max, sub_drop_min, sup_bottom_max_with_sub, space_after_script, script_percent, script_script_percent.
accent_base_height, flattened_accent_base_height, overbar_vertical_gap, overbar_rule_thickness, overbar_extra_ascender, underbar_vertical_gap, underbar_rule_thickness, underbar_extra_descender.
The comments in
font.hpp
name the constant each field comes from. copy_math_pars copies all of them, and ot_math, so that a font made from another one (a smart font from its main font, a magnified or emulated font from its base) inherits them. The typesetter sees the smart font of the current mode (env->fn), whose parameters come from its main subfont or, for mathematics, from the subfont that draws the letters.
The data which belong to one glyph are reached through virtual hooks of font_rep, whose defaults in
font.cpp
answer “unknown”, and are relayed by virtual methods of the boxes, so that a composite box answers for the glyph that represents it.
The script corrections at a height h: the height, relative to the baseline of s, of the edge of the script which faces s (the bottom of a superscript, the top of a subscript). The defaults ignore h and return the older corrections without height. For MATH_TYPE_OPENTYPE, unicode_font_rep answers from the MathKernInfo cut-in at that height (get_ot_kerning, which converts the height to design units and the kern back), plus, on the right side, the italic correction of the last glyph (get_ot_italic_correction): all of it for a superscript, none for a subscript, except on integral signs (is_ot_integral), which take two fifths of it above and lose three fifths below. The corrections without height call the _at versions at the bottom, respectively the top, of the font.
For MATH_TYPE_OPENTYPE, the italic correction of the table when the glyph has one.
Whether the glyph, or the glyph whose size variant it is (get_init_glyphID), is in ExtendedShapeCoverage. Answered whenever ot_math is set.
The top accent attachment of the glyph, when the table has one; answered whenever ot_math is set.
The substitute of the glyph under a GSUB feature, as a native glyph name; see OpenType features in text and formulas.
On the box side,
boxes.hpp
adds lsub_correction_at, lsup_correction_at, rsub_correction_at, rsup_correction_at, extended_shape and top_accent. A text box forwards them to its font, but only a text box of exactly one character can be an ordinary glyph: a longer string answers true to extended_shape and false to top_accent. Concatenations forward the left corrections to their first box and the right ones to their last box; change and modifier boxes forward to their body, and the box of a wide accent to its base, shifting the attachment point by the position of the base.
Each construction tests for the data it needs, using a parameter which is only non zero when it was read from a table, and falls back on its older code otherwise.
script_box_rep (
script_boxes.cpp
) places scripts with ot_script_shifts when fn->ot_math and sub_sup_gap_min > 0. A superscript starts at sup_lo_base of the base; over an extended shape (or a composite box) it may not drop more than sup_drop_max below the top of the base; its bottom stays above ysup_lo_lim. A subscript is placed symmetrically with sub_drop_min and ysub_hi_lim. When both are present and the gap between them is less than sub_sup_gap_min, the subscript moves down, and the superscript up by as much of the difference as keeps its bottom under sup_bottom_max_with_sub, as in TeX. The horizontal corrections are then evaluated with the _at methods, at the height of the edge of the script that faces the base and of the edge of the base that faces the script, which is what the cut-in kerning of the specification asks for, and space_after_script is added after right scripts.
lim_box_rep uses the limit constants when the font has positive lower_limit_gap_min and lower_limit_baseline_drop_min: the gap between the operator and its limits, and the minimal rise and drop of their baselines. A stretched base, flagged by the new argument of limit_box, takes the stretch stack constants instead, which keep a label closer to a long arrow or a wide brace than a limit to an operator; typeset_long_arrow in
concat_math.cpp
passes it, and uses wide_box_covering, which keeps trying the next horizontal variant until the arrow is at least as wide as its labels.
frac_box_rep takes a new argument disp (the display style of the environment) and, when frac_num_gap_min > 0, sets the rule to frac_rule_thickness and places the numerator at the larger of its shift and the minimal gap above the rule, the denominator at the smaller of its shift and the minimal gap below, with the display style constants in display style.
At the level of the concatenator, the radical sign is asked to cover the body plus sqrt_ver_gap, or sqrt_ver_disp_gap in display style, when that constant is positive. In sqrt_box_rep, with sqrt_degree_rise_percent > 0, the rule has the thickness sqrt_rule_thickness and is drawn flush with the top of the sign (half a thickness below it), the box gets sqrt_extra_ascender above, the degree is raised by radicalDegreeBottomRaisePercent of the height of the sign and tucked into it by sqrt_kern_after_degree (a negative value), and sqrt_kern_before_degree keeps it clear of what precedes.
In wide_box_rep (
math_boxes.cpp
), fonts with ot_math which are neither TeX Gyre nor STIX by math type get three new paths:
a bar over or under a base is a rule with the thickness, gap and extra ascender or descender of the table, the gap measured from the ink of the base;
a wide accent is asked from the font with get_wide_variant, and drawn from the variant or assembly it returns (see stretchable glyphs);
for MATH_TYPE_OPENTYPE, a narrow accent is the accent glyph, or its flac substitute over a base taller than flattened_accent_base_height, raised by the excess of the base over accent_base_height, and attached at the top accent attachment points of the base and of the accent when both are known.
The base of an accent which is a single i or j is replaced by its dtls substitute when the font has one (
concat_math.cpp
).
The size of a delimiter, a radical sign or a big operator is chosen by the rubber font of the math font, which knows the variants and the assemblies of the table: get_rubber_variant and get_wide_variant are asked first by the generic code which picks a size. A display operator is the smallest variant at least displayOperatorMinHeight tall, capped at two em (DISPLAY_OPERATOR_MAX_EM in
rubber_unicode_font.cpp
) because some fonts declare much larger values. The details are in stretchable glyphs: variants and assemblies.
For MATH_TYPE_OPENTYPE, a negated relation (neg applied to =, <in>, <subseteq> and about forty others) is set as the precomposed Unicode symbol when the font has it, instead of a stroke drawn through the relation (negated_symbols in
concat_math.cpp
).
edit_env_rep::update_font (
env_semantics.cpp
) builds the font of the current mode with the new make_current_font. At script levels, when the font has ot_math and math-font-sizes is default, the size is taken from scriptPercentScaleDown at the first level and scriptScriptPercentScaleDown beyond, and for MATH_TYPE_OPENTYPE the font is wrapped in a feature_font for the ssty feature (alternate 0 at the first level, 1 beyond), which gives the script size shapes of the font. The features a document asks for with font-features are applied after that (apply_features).
Ten of the 56 constants are parsed and not used anywhere outside the parser:
The plain stacks. above, below and binomials over an ordinary base are limit boxes and use the limit constants; the stack constants would place the top much higher (444 against 111 units in Latin Modern Math), a visible change to every such construct, so they are left out on purpose.
Deliberately ignored: TeXmacs sizes every bracket automatically, and the constant would make the plain parenthesis of (x) jump to a larger variant. TeX and unicode-math ignore it too.
No primitive of TeXmacs draws a skewed fraction.
Not needed by the line breaker.
The device tables are not applied either (the OpenType layout tables).
Test the right flag. ot_math means “the parameters come from a table”, and holds for the tuned TeX Gyre fonts; math_type == MATH_TYPE_OPENTYPE means “the table is the only source”. Code which replaces a tuned behaviour must test the latter, or exclude the tuned math types explicitly as wide_box_rep does.
smart_font_rep has a member of its own named ot_math (
smart_font.cpp
), which means “the main font is an untuned OpenType math font whose letters come from its own alphabets”. It hides the field of font_rep inside the methods of the smart font, while the typesetter, which holds a font handle, reads the field of font_rep, copied by copy_math_pars.
Each construction decides on its own whether the table applies, by testing one constant for being positive (frac_num_gap_min, sqrt_degree_rise_percent, sub_sup_gap_min, the two limit constants, sqrt_ver_gap, the bar thicknesses, accent_base_height). A font with a table in which that constant is zero gets the older layout for that construction only.
The list of integral signs of is_ot_integral is written out by name; <iint> is not in it, so the double integral gets the italic correction of an ordinary glyph.
Linux Libertine carries a stub MATH table in its regular face: it is laid out by its tuned branch while the switch is on, and by the table otherwise.