Skip to main content

Crate formulary

Crate formulary 

Source
Expand description

§formulary

Native MathML Core layout in Rust. Parse presentation MathML, lay it out using the font’s OpenType MATH table, and get back a resolution-independent Layout — glyph ids and rules with baseline metrics — that you rasterize through your own text pipeline.

No webview, no rasterization in the core, no I/O: you bring the font bytes (any font with a MATH table, e.g. STIX Two Math or New Computer Modern).

Parsing follows MathML Core’s error recovery: unknown or structurally invalid elements render as mrow fallbacks and are reported in MathRoot::warnings; only malformed XML is a hard error.

use formulary::{parse, layout, LayoutOptions, MathFont};

let font_data = std::fs::read("fonts/STIXTwoMath-Regular.otf").unwrap();
let font = MathFont::new(&font_data, 0).unwrap();
let tree = parse("<math><mi>x</mi><mo>+</mo><mn>2</mn></math>").unwrap();
let laid = layout(&tree, &font, &LayoutOptions { font_size: 16.0 });
assert!(laid.width > 0.0);

Re-exports§

pub use ast::Color;
pub use ast::DisplayMode;
pub use ast::MathRoot;
pub use ast::Warning;

Modules§

ast
Typed MathML element tree.
svg
SVG emission — the debugging window and golden-test format.

Structs§

GlyphId
A glyph index in the math font — the same 16-bit id the font’s own tables use; 0 is .notdef.
Layout
A laid-out formula: extents plus a flat display list.
LayoutOptions
Caller-supplied layout parameters.
MathFont
A font validated to contain an OpenType MATH table.

Enums§

FontError
Errors constructing a MathFont.
Item
One drawable in the display list.
ParseError
Errors that prevent producing a tree at all. Everything else is recovered from and reported via MathRoot::warnings.

Functions§

layout
Lay out a parsed formula against font at options.font_size.
parse
Parse a MathML fragment whose root element is <math>.