Text layout and mathematics
Paragraphs and measurement
layout_paragraph(library, paragraph) → TextLayout
Shapes every span, wraps at legal Unicode break opportunities and returns an immutable TextLayout. library.layout(paragraph) calls it with the library as its first argument.
TextMeasurement(
advance,
logical_bounds,
ink_bounds,
baselines,
horizontal_overflow,
vertical_overflow,
)
Summarises the logical and ink bounds, the baselines, the line count and the overflow of a layout, without drawing. layout.measure() returns one.
LineLayout(
runs,
baseline,
advance,
logical_bounds,
ink_bounds,
horizontal_overflow=0.0,
source_range=None,
explicitly_terminated=False,
reading_order=(),
)
One laid-out line with its runs, baseline, advance, bounds, source range and whether it ended at an explicit line break.
TextStyle carries the font families, size, weight, slant, language, direction, spacing and OpenType features. A paragraph may mix styles and scripts. Set max_width to wrap at Unicode line-break opportunities {Unicode Consortium} (2026), and leave it unset for one unconstrained line. Line construction and spacing explains how a line is built.
Structured mathematics
from glyphkit import FontLibrary, FontSource
from glyphkit.math import MathStyle, parse_math
library = FontLibrary.from_sources((FontSource.path("fonts/STIXTwoMath-Regular.otf"),))
formula = parse_math(r"U(x_1, x_2) = x_1^\alpha x_2^{1-\alpha}")
box = library.layout_math(formula, style=MathStyle(18, ("STIX Two Math",)))
print(box.bounds)
parse_math(source, *, max_length=100000, max_depth=256, max_matrix_cells=10000) → MathNode
The LaTeX-like source text.
Largest input in code points.
Deepest nesting of groups.
Most cells in matrices.
Parses a defined LaTeX-like grammar into a tree of symbols, scripts, fractions, roots, delimiters, matrices, accents and limits. The grammar is on the Math grammar page.
FontLibrary.layout_math(expression, *, style) → MathBox
The tree returned by parse_math.
Size, math fonts and overrides.
Lays an expression out with the OpenType MATH constants of the first font in style.families. The font must have a MATH table.
MathStyle(
size,
families,
level='display',
script_scale=None,
script_script_scale=None,
constants=...,
letter_slant='normal',
)
The size of the whole expression in layout units.
Math font family first, then fallbacks. The first needs an OpenType MATH table.
"display", "text", "script" or "scriptscript".
Replaces ScriptPercentScaleDown, as a fraction in (0, 1].
Replaces ScriptScriptPercentScaleDown.
Overrides of other MATH constants by name, in em.
Draw alphabetic symbols with the italic or oblique face of the family.
Controls the math families, the size, the script level, the slant of letters, the script scales and overrides of OpenType MATH constants.
MathBox(
kind,
bounds,
baseline,
text_layout=None,
children=(),
rules=(),
origin=(0.0, 0.0),
source_range=None,
glyph_id=None,
)
One node of the laid-out tree. It has a kind, bounds, a baseline, the origin of each child relative to its parent, glyph layouts in text_layout, and drawable rules.
This is a defined subset of LaTeX-like input, not a TeX interpreter. The font-specific constants and glyph variants come from the OpenType MATH table {Microsoft} (2025). MathStyle can override the script scales, named math constants and the slant used for letters, which helps with fonts whose MATH table is incomplete.
A Cobb–Douglas utility function laid out with OpenType MATH metrics and exact glyph outlines. Dashed boxes are the script nodes of the box tree.
A renderer draws the tree by walking it: it adds the origin of each child to its parent's position, draws each text_layout with the outlines of its glyphs, and draws each rule as a line from start to end with its thickness. The script that draws the figure above does exactly this in about forty lines.