GNU TeXmacsUser manualReferenceAboutEnglish
Stretchable glyphs: variants and assemblies

Delimiters, radicals, big operators, wide accents, braces and long arrows have to be drawn at a size which depends on what they surround. A font with an OpenType MATH table says how: its MathVariants subtable lists, for each such glyph, a series of pre-drawn variants of increasing size and, for most of them, a glyph assembly, a recipe which builds an arbitrarily large version out of parts (a top, a bottom, a middle and repeated extenders). This page describes how TeXmacs uses these data: how the typesetter names the stretched characters, how the rubber font of an OpenType math font answers those names from the table, how an assembly becomes a virtual glyph, and how this relates to the older emulation which every other font goes through. How the table is read is the subject of the OpenType layout tables, and the constants which place the stretched glyphs in a formula are discussed in mathematics from the MATH table.

1.The names of stretchable characters

The typesetter never asks a font for a delimiter of a given height directly. It asks for characters with a name of the form <head-root-n>, where head says what kind of character is wanted (left, right, mid, large, big, wide or rubber), root is the name of the base character ((, langle, sum, hat, overbrace, longrightarrow, …), and n is a size number: <left-(-2> is the third size of an opening parenthesis, sizes counting from 0. Big operators are numbered from 1 (there is no <big-sum-0>): <big-sum-1> is the text size and <big-sum-2> the display size. These names are stable across fonts; each font decides which glyph a size number means.

Traditionally the typesetter found the size it needed by probing: get_delimiter and get_wide in

Typeset/Boxes/Basic/text_boxes.cpp

ask the font for the extents of size 0, 1, 2, … until one is tall or wide enough. Fonts which know their sizes now short-circuit this search through two virtual methods of font_rep (

Graphics/Fonts/font.hpp

), which return false by default:

bool get_rubber_variant (string s, SI height, string& r)

bool get_wide_variant (string s, SI width, string& r)
(answer a size search directly)

Given a name without size number, such as <left-(>, return in r the name of the smallest variant whose advance reaches height (respectively width). get_delimiter and get_wide call them first and fall back to probing when they return false.

The smart_font_rep and feature_font_rep wrappers forward these calls to the subfont which draws the character (smart_font_rep::rubber_subfont), so they reach the rubber font of the math font whatever lies in between.

2.Which rubber font a font gets

Every font has a rubber font, obtained with rubber_font (base) (

Graphics/Fonts/font.cpp

), which caches the result of the virtual method base->make_rubber_font (base). The method decides how the stretched characters of that font are drawn:

unicode_font_rep::make_rubber_font

returns rubber_unicode_font (this, ot_face) when the font has a MATH table; otherwise it calls the default below.

smart_font_rep::make_rubber_font

returns the smart font itself for families given with mathlarge= or mathrubber= (they are routed character by character), the rubber font of the main subfont when that subfont has a MATH table, and the default otherwise.

font_rep::make_rubber_font

the default: the hand-tuned rubber_stix_font for the STIX fonts (unless the hand tuning has been switched off), the font itself for mathlarge= and mathrubber= families, the emulation poor_rubber_font for any other Unicode font (the global has_poor_rubber is true), and the font itself otherwise.

So a font with a MATH table always goes through rubber_unicode_font_rep, and the emulation of poor_rubber.cpp remains what ordinary text fonts use.

3.The rubber font of an OpenType math font

rubber_unicode_font_rep (

Plugins/Freetype/rubber_unicode_font.cpp

) keeps the tt_face of the font (math_face, whose math_table is the parsed MATH table) and seven subfonts, created on first use by get_font:

0

the base font itself, which draws the pre-drawn variants under their glyph names <@XXXX> (four hexadecimal digits of the glyph id);

1 to 4

the legacy subfonts of fonts without a table (the base magnified by \(\sqrt{1 / 2}\), \(\sqrt{2}\) and 2, and rubber_assemble_font for the Unicode bracket pieces);

5

the default rubber font, font_rep::make_rubber_font (base), for the characters the table says nothing about;

6

a virtual font named opentype_virtual[base], whose definitions are the assemblies built from the table (see below).

The subfont and the rewritten name of a character are computed once by search_font_cached and stored in the hash maps mapper and rewriter. With a table present, the decision is made by search_font_sub_opentype:

  1. parse_variant splits the name into head, root and size number; the root may itself contain dashes (<wide-var-rightarrow-2>), so the size is the last token and the head the first. Big operators are shifted down by one, since their numbering starts at 1.

  2. variant_glyph turns the root into a glyph id. For the heads wide and rubber the static table of wide_code_point gives the code point which carries the horizontal variants: the combining accents (hat is U+0302, vect U+20D7), the braces U+23DC to U+23DF, and the plain arrows for the long ones (longrightarrow is U+2192), since fonts stretch those and not the spacing modifier letters or the long arrows into which TeXmacs would otherwise translate the names. Other roots are converted from their TeXmacs name with strict_cork_to_utf8.

  3. A name of a made to measure assembly (next section) is answered at once by make_measured, with subfont 6.

  4. If the glyph has vertical or horizontal variants and the size number is within range, the name is rewritten to the glyph name of that variant and subfont 0 draws it. For the display size of a big operator (<big-op-2>), the variant is instead the smallest one which reaches displayOperatorMinHeight, capped at two em by DISPLAY_OPERATOR_MAX_EM, because some fonts declare absurdly large values.

  5. Beyond the last variant, if the glyph has an assembly, every size up to MAX_ASSEMBLY_REPS (64) further sizes is defined at once in the virtual font, and subfont 6 draws it.

  6. Otherwise the legacy search search_font_sub is tried, and when it has nothing either, subfont 5, the default rubber font. A delimiter of which the font has no larger size (Fira Math has none for the slashes) is thus stretched by the emulation beyond the base glyph of the font.

The two size searches of the typesetter are answered in the same spirit. get_rubber_variant compares the target height, converted into design units, with the advances of the vertical variants which the table parser stored (ver_glyph_variants_adv) and returns the first variant which reaches it. Beyond the largest variant, a glyph without assembly gets the largest variant, and a glyph with an assembly gets a made to measure name. get_wide_variant does the same with the horizontal variants and the width.

4.Assemblies as virtual glyphs

An assembly is a list of part records: a glyph id, the lengths of its start and end connectors, its full advance, and a flag which marks the extenders. The specification lets consecutive parts overlap by at least minConnectorOverlap, and by at most the shorter of the two connectors, and repeats the extenders as often as needed. TeXmacs does not draw assemblies with special code: it writes each size as a definition of the glyph algebra of virtual fonts, and lets the virtual font machinery draw it, on the screen as a bitmap and in PDF as the vector outlines of the parts. An assembly therefore exports as the font's own glyphs.

part_records

reads the part records of the assembly and measures the ink of every part in the rendered font (its bottom for a vertical assembly, its left edge for a horizontal one). It also repairs parts which declare no advance: KpMath 0.35, which TeXmacs ships, declares neither advance nor connectors for the bottom part of its right parenthesis. Such a part gets the length of its ink and the shortest connector of the properly declared parts.

assembled_length

computes, in design units, the length of an assembly whose extenders are repeated a given number of times, with the overlaps of the specification.

assemble

writes the definition: a join of the parts, each a leaf @XXXX placed at an offset in units of the em. Parts are placed one by one, at the distance the table prescribes between the near edges of their ink, rather than glued to the stack built so far: many fonts (KpMath, XCharter, STIX, New Computer Modern, …) draw their parts at origins which differ from one part to the next, and gluing measured the ink of the tallest part instead of the one which receives the next. With a target length, the overlaps are enlarged uniformly, up to the connector lengths, so that the assembly shrinks towards the target.

The numbered sizes are defined all at once, the first time a size beyond the variants is asked for: the first assembled size is the smallest number of repetitions whose length exceeds the largest pre-drawn variant, so that sizes keep growing with the size number, and 64 sizes are then defined. This matters because adding a definition invalidates the virtual font: add_virtual_glyph appends the definition to the translator virt and, if subfont 6 already exists, removes it from font::instances, font_metric::instances and font_glyphs::instances (all three are sized after the number of definitions) so that it is rebuilt.

Made to measure assemblies.
Beyond the numbered sizes, the size searches of the typesetter return names such as <left-(-h2500> (a height of 2.5 em) or <wide-overbrace-w4200> (a width of 4.2 em): the last token is h or w followed by the size in thousandths of an em (per_em). make_measured builds the definition with the smallest number of repetitions which reaches the target and shrinks the overlaps towards it. The size is in the name, and in em rather than in pixels, because the screen draws with a magnified copy of the font, whose virtual font starts empty: the copy receives the name which the unmagnified font answered and must be able to rebuild the same glyph from it.
Virtual font primitives.
Two additions to
Graphics/Fonts/virtual_font.cpp

came with this work: hor-take, the horizontal mirror of ver-take, which repeats a column of a glyph over a given length, in both the bitmap compiler (compile_bis) and the vector path (draw_tree), and an optional third argument of glue*, an extra horizontal shift.

5.The emulation behind the table

Fonts without a table, and the characters a table does not cover, still go through the emulation of

Graphics/Fonts/poor_rubber.cpp

and the emu-* virtual fonts, described in emulated fonts. Two changes concern the OpenType path. First, poor_rubber_font_rep now treats a font with a MATH table as having its own big operators, whatever its name (big_flag). Second, the slashes, which cannot be extended by repeating a straight middle part, keep being stretched (mostly vertically) in sixteen further sizes from SLASH_BASE on, so that a large / is a slash and not a magnified glyph.

In the smart font, smart_font_rep::resolve_rubber builds the stretched character from the base character of the font; a long arrow whose long form the font lacks stretches the plain arrow.

6.Symbols taken from STIX Two Math

An OpenType math font may lack a symbol which TeXmacs can emulate, as a construction over the glyphs the font has. Some of these constructions work on pixels (intersect, exclude, bar-*, flood-fill, unserif, …): virtual_font_rep::supported (t, true) tells which operators the vector path can draw, and virtual_font_draws_vectors (fn, s) applies it to one character, following an extended virtual font to the one it extends. A PDF export embeds such a glyph as a bitmap, in a Type 3 font.

To avoid that, smart_font_rep::resolve, when the main font has a MATH table (has_math_table: the math type is MATH_TYPE_OPENTYPE or MATH_TYPE_TEX_GYRE) and the emulation of a character would not draw vectors, takes the character from STIX Two Math instead (resolve_shipped_math, SHIPPED_MATH_FONT), which is shipped with TeXmacs. Using a shipped font rather than whatever the system provides keeps the document the same everywhere, which is also what an emulation achieves. The emulation stays when STIX Two Math is not installed, when it is the main font itself, or when it lacks the character.

Which emulated glyphs of a document become bitmaps is reported by the font report of the font inspector.

7.Source files

Plugins/Freetype/rubber_unicode_font.cpp

The rubber font of Unicode fonts, and its OpenType path: variants, assemblies, made to measure sizes.

Graphics/Fonts/font.cpp
,
font.hpp

rubber_font, the default make_rubber_font and the hooks get_rubber_variant, get_wide_variant.

Graphics/Fonts/smart_font.cpp

make_rubber_font and the forwarding hooks of the smart font, resolve_rubber, and the fallback to STIX Two Math (resolve_shipped_math).

Graphics/Fonts/virtual_font.cpp

The glyph algebra which draws the assemblies, hor-take, virtual_font_draws_vectors.

Graphics/Fonts/poor_rubber.cpp

The emulation of stretched characters for fonts without a table.

Typeset/Boxes/Basic/text_boxes.cpp

get_delimiter and get_wide, the size searches of the typesetter.

8.Open problems

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