Skip to content

The animation engine for agents: blazingly fast math and 3D videos with Manim's Python API, on a Rust GPU core.

Python

Integrate Typst text rendering

Sina Atalay2e04cdfe01da
diff --git a/rust/engine/src/text.rs b/rust/engine/src/text.rs
new file mode 100644
index 00000000..f6ef33ae
--- /dev/null
+++ b/rust/engine/src/text.rs
@@ -0,0 +1,260 @@
+//! The face's text (see `chrome`): a line in manimgx's fonts, shaped by rustybuzz, the shaper Typst
+//! sets text with, so its glyphs are the ones Typst would place; each character in the first font
+//! that has it (the line's family first, then the others in their order), the line's runs in their
+//! reading order. And its glyphs' outlines, as the renderer draws a path.
+//!
+//! Typst itself is not here: it is a typesetter of documents, and what the face sets is lines. In
+//! the browser it would be 28 MB of WebAssembly; this is 0.7 MB.
+
+use rustybuzz::{Direction, Face, Feature, UnicodeBuffer, Variation};
+use ttf_parser::{GlyphId, Tag};
+use unicode_segmentation::UnicodeSegmentation;
+
+/// The families the face sets text in.
+#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
+pub(crate) enum Family {
+ Sans,
+ Mono,
+}
+
+impl Family {
+ fn name(self) -> &'static str {
+ match self {
+ Family::Sans => "Noto Sans",
+ Family::Mono => "DejaVu Sans Mono",
+ }
+ }
+}
+
+/// A font: its family, whether it slants, and its faces at the weights the face sets: regular and
+/// bold (a variable font's at 700; another's, the same face).
+struct Font {
+ family: String,
+ italic: bool,
+ regular: Face<'static>,
+ bold: Face<'static>,
+}
+
+/// The fonts the face is set in, in the order a character falls back through them.
+pub(crate) struct Fonts {
+ fonts: Vec<Font>,
+ orders: [Vec<u16>; 2], // where a character of each family is looked for (`order`)
+}
+
+/// A glyph of a line: its font (an index), weight and id; where its origin is (points, y up, from
+/// the line's start on its baseline); its size (points per font unit).
+#[derive(Clone, Copy, Debug, PartialEq)]
+pub(crate) struct Glyph {
+ pub(crate) font: u16,
+ pub(crate) bold: bool,
+ pub(crate) id: u16,
+ pub(crate) x: f32,
+ pub(crate) y: f32,
+ pub(crate) scale: f32,
+}
+
+/// A line set: its glyphs, and its advance (points).
+#[derive(Debug)]
+pub(crate) struct Line {
+ pub(crate) glyphs: Vec<Glyph>,
+ pub(crate) width: f32,
+}
+
+/// A font's family, as its name table gives it (the typographic family first).
+fn family(face: &Face) -> String {
+ let names = face.names();
+ [ttf_parser::name_id::TYPOGRAPHIC_FAMILY, ttf_parser::name_id::FAMILY]
+ .into_iter()
+ .find_map(|id| names.into_iter().filter(|n| n.name_id == id).find_map(|n| n.to_string()))
+ .unwrap_or_default()
+}
+
+impl Fonts {
+ /// The fonts in `files` (each a font file's bytes, kept for as long as the program runs; a
+ /// collection gives each of its fonts), falling back in that order.
+ pub(crate) fn new(files: impl IntoIterator<Item = &'static [u8]>) -> Self {
+ let mut fonts = Vec::new();
+ let wght = Tag::from_bytes(b"wght");
+ for data in files {
+ for index in 0..ttf_parser::fonts_in_collection(data).unwrap_or(1) {
+ let (Some(regular), Some(mut bold)) = (Face::from_slice(data, index), Face::from_slice(data, index)) else { continue };
+ if bold.variation_axes().into_iter().any(|axis| axis.tag == wght) {
+ bold.set_variations(&[Variation { tag: wght, value: 700.0 }]);
+ }
+ fonts.push(Font { family: family(&regular), italic: regular.is_italic(), regular, bold });
+ }
+ }
+ let order = |family: Family| {
+ let mut order: Vec<u16> = (0..fonts.len() as u16).collect();
+ order.sort_by_key(|&i| {
+ let font: &Font = &fonts[usize::from(i)];
+ (font.family != family.name() || font.italic, font.italic, font.family.contains("Emoji"))
+ });
+ order
+ };
+ let orders = [order(Family::Sans), order(Family::Mono)];
+ Self { fonts, orders }
+ }
+
+ fn face(&self, font: u16, bold: bool) -> &Face<'static> {
+ let font = &self.fonts[usize::from(font)];
+ if bold { &font.bold } else { &font.regular }
+ }
+
+ /// The fonts a character of `family` is looked for in: the family's upright ones, then the
+ /// others in their order (upright before slanted, emoji last).
+ fn order(&self, family: Family) -> &[u16] {
+ &self.orders[family as usize]
+ }
+
+ /// `text` (a line) in `family` at `size` points, bold or not, its digits of one width or not:
+ /// its glyphs, placed.
+ pub(crate) fn line(&self, text: &str, family: Family, size: f32, bold: bool, tabular: bool) -> Line {
+ let order = self.order(family);
+ let features = [Feature::new(Tag::from_bytes(b"tnum"), 1, ..)];
+ let features = if tabular { &features[..] } else { &[] };
+ let mut glyphs = Vec::new();
+ let mut x = 0.0;
+ // its runs of one direction, in the order they are read (Unicode's bidirectional
+ // algorithm, the line left to right, as Typst sets an English document's); in each, its
+ // pieces of one font
+ let bidi = unicode_bidi::BidiInfo::new(text, Some(unicode_bidi::Level::ltr()));
+ for paragraph in &bidi.paragraphs {
+ let (levels, runs) = bidi.visual_runs(paragraph, paragraph.range.clone());
+ for run in runs {
+ let rtl = levels[run.start].is_rtl();
+ let mut pieces = self.pieces(&text[run.clone()], order);
+ if rtl {
+ pieces.reverse();
+ }
+ for (font, range) in pieces {
+ let face = self.face(font, bold);
+ let mut buffer = UnicodeBuffer::new();
+ buffer.push_str(&text[run.start + range.start..run.start + range.end]);
+ buffer.set_direction(if rtl { Direction::RightToLeft } else { Direction::LeftToRight });
+ buffer.guess_segment_properties();
+ let shaped = rustybuzz::shape(face, features, buffer);
+ let scale = size / face.units_per_em() as f32;
+ for (info, at) in shaped.glyph_infos().iter().zip(shaped.glyph_positions()) {
+ glyphs.push(Glyph { font, bold, id: info.glyph_id as u16, x: x + at.x_offset as f32 * scale, y: at.y_offset as f32 * scale, scale });
+ x += at.x_advance as f32 * scale;
+ }
+ }
+ }
+ }
+ Line { glyphs, width: x }
+ }
+
+ /// `text`'s pieces of one font: each grapheme in the first font of `order` that has all its
+ /// characters (else the first's, which draws what it lacks as its .notdef).
+ fn pieces(&self, text: &str, order: &[u16]) -> Vec<(u16, std::ops::Range<usize>)> {
+ let mut pieces: Vec<(u16, std::ops::Range<usize>)> = Vec::new();
+ for (at, grapheme) in text.grapheme_indices(true) {
+ let has = |&&font: &&u16| grapheme.chars().all(|c| c.is_control() || self.face(font, false).glyph_index(c).is_some());
+ let font = order.iter().find(has).or(order.first()).copied().unwrap_or(0);
+ match pieces.last_mut() {
+ Some((last, range)) if *last == font => range.end = at + grapheme.len(),
+ _ => pieces.push((font, at..at + grapheme.len())),
+ }
+ }
+ pieces
+ }
+
+ /// A glyph's outline: cubic points (font units, y up), four per curve, each contour closed.
+ pub(crate) fn outline(&self, font: u16, bold: bool, id: u16) -> Vec<[f64; 3]> {
+ let mut pen = Pen { points: Vec::new(), at: [0.0; 2], start: [0.0; 2] };
+ self.face(font, bold).outline_glyph(GlyphId(id), &mut pen);
+ pen.points
+ }
+}
+
+/// A glyph's outline as cubic points: lines and TrueType's quadratics raised to cubics.
+struct Pen {
+ points: Vec<[f64; 3]>,
+ at: [f64; 2],
+ start: [f64; 2],
+}
+
+impl Pen {
+ fn cubic(&mut self, a: [f64; 2], b: [f64; 2], to: [f64; 2]) {
+ let from = self.at;
+ self.points.extend([from, a, b, to].map(|p| [p[0], p[1], 0.0]));
+ self.at = to;
+ }
+
+ fn line(&mut self, to: [f64; 2]) {
+ let from = self.at;
+ let third = |k: f64| [from[0] + k * (to[0] - from[0]), from[1] + k * (to[1] - from[1])];
+ self.cubic(third(1.0 / 3.0), third(2.0 / 3.0), to);
+ }
+}
+
+impl ttf_parser::OutlineBuilder for Pen {
+ fn move_to(&mut self, x: f32, y: f32) {
+ self.at = [f64::from(x), f64::from(y)];
+ self.start = self.at;
+ }
+ fn line_to(&mut self, x: f32, y: f32) {
+ self.line([f64::from(x), f64::from(y)]);
+ }
+ fn quad_to(&mut self, x1: f32, y1: f32, x: f32, y: f32) {
+ let (c, to) = ([f64::from(x1), f64::from(y1)], [f64::from(x), f64::from(y)]);
+ let from = self.at;
+ let third = |p: [f64; 2]| [p[0] + 2.0 / 3.0 * (c[0] - p[0]), p[1] + 2.0 / 3.0 * (c[1] - p[1])];
+ self.cubic(third(from), third(to), to);
+ }
+ fn curve_to(&mut self, x1: f32, y1: f32, x2: f32, y2: f32, x: f32, y: f32) {
+ self.cubic([f64::from(x1), f64::from(y1)], [f64::from(x2), f64::from(y2)], [f64::from(x), f64::from(y)]);
+ }
+ fn close(&mut self) {
+ if self.at != self.start {
+ self.line(self.start);
+ }
+ }
+}
+
+#[cfg(all(test, feature = "typeset"))]
+mod tests {
+ use super::*;
+
+ /// manimgx's fonts, in the repository.
+ const FONTS: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/../../fonts/manimgx-fonts/src/manimgx_fonts");
+
+ /// A line's glyphs, left to right: id, x and y (points, from the first's), points per unit.
+ type Glyphs = Vec<(u16, f32, f32, f32)>;
+
+ fn placed(mut glyphs: Glyphs) -> Glyphs {
+ glyphs.sort_by(|a, b| a.1.total_cmp(&b.1));
+ let (x0, y0) = glyphs.first().map_or((0.0, 0.0), |g| (g.1, g.2));
+ glyphs.into_iter().map(|(id, x, y, s)| (id, x - x0, y - y0, s)).collect()
+ }
+
+ /// `text` set by Typst, at 13 points.
+ fn typst(text: &str, family: Family, bold: bool, tabular: bool) -> Glyphs {
+ let style = format!("font: \"{}\", size: 13pt, weight: \"{}\", number-width: {}", family.name(), if bold { "bold" } else { "regular" }, if tabular { "\"tabular\"" } else { "auto" });
+ let source = format!("#set page(width: auto, height: auto, margin: 0pt)\n#set text({style})\n#\"{}\"", text.replace('\\', "\\\\").replace('"', "\\\""));
+ let (rows, ..) = crate::typeset::typeset(source, &[FONTS.into()], None).expect("Typst sets it");
+ placed(rows.chunks_exact(23).filter(|r| r[0] == 0.0).map(|r| ((r[1] as u64 % 65536) as u16, r[6] as f32, r[7] as f32, r[2] as f32)).collect())
+ }
+
+ #[test]
+ fn a_line_is_set_as_typst_sets_it() {
+ let fonts = Fonts::new(crate::typeset::face_fonts(&[FONTS.into()]));
+ let lines = [
+ ("0:01.2 / 0:04.0", Family::Sans, false, true),
+ ("Line 14 · 0:02.4", Family::Sans, false, true),
+ ("AVATAR Typography: fi fl ffi", Family::Sans, false, false),
+ ("Sound 45% « 10 s » 1.25×", Family::Sans, false, true),
+ ("• مرحبا والنهاية", Family::Sans, false, true),
+ ("Space k", Family::Sans, true, false),
+ (" File \"scene.py\", line 3, in construct", Family::Mono, false, false),
+ ];
+ for (text, family, bold, tabular) in lines {
+ let line = fonts.line(text, family, 13.0, bold, tabular);
+ let ours = placed(line.glyphs.iter().map(|g| (g.id, g.x, g.y, g.scale)).collect());
+ let theirs = typst(text, family, bold, tabular);
+ let same = ours.len() == theirs.len() && ours.iter().zip(&theirs).all(|(o, t)| o.0 == t.0 && (o.1 - t.1).abs() < 1e-3 && (o.2 - t.2).abs() < 1e-3 && (o.3 - t.3).abs() < 1e-6);
+ assert!(same && !ours.is_empty(), "{text:?}:\n ours {ours:?}\n Typst {theirs:?}");
+ }
+ }
+}
diff --git a/rust/engine/src/tone.wgsl b/rust/engine/src/tone.wgsl
index 930a3e64..4375e09d 100644
--- a/rust/engine/src/tone.wgsl
+++ b/rust/engine/src/tone.wgsl
@@ -39,8 +39,3 @@ fn agx(c: vec3<f32>) -> vec3<f32> {
fn toned(c: vec3<f32>, tone: u32) -> vec3<f32> {
return gamma(select(clamp(c, vec3<f32>(0.0), vec3<f32>(1.0)), clamp(agx(c), vec3<f32>(0.0), vec3<f32>(1.0)), tone == 1u));
}
-
-// What a radiance shows as: scaled by the exposure, then shown.
-fn displayed(radiance: vec3<f32>, exposure: f32, tone: u32) -> vec3<f32> {
- return toned(radiance * exposure, tone);
-}
diff --git a/rust/engine/src/typeset.rs b/rust/engine/src/typeset.rs
new file mode 100644
index 00000000..b21f49e4
--- /dev/null
+++ b/rust/engine/src/typeset.rs
@@ -0,0 +1,705 @@
+//! Typst as a library: a document in, its layout out, read straight off Typst's frames. Every
+//! glyph comes with its outline's key, its placement, its paint and the source bytes it draws
+//! (its cluster, through its span); every shape as cubic curves; every labelled group as the
+//! items inside it.
+//!
+//! Coordinates: a glyph's placement maps its outline (font units, y up) to the page in points
+//! with y up (Typst's page is y down; the flip is folded in here). Shapes come in page points,
+//! y up. Python scales points to scene units.
+
+use std::collections::HashMap;
+use std::path::PathBuf;
+use std::sync::{Arc, Mutex, OnceLock};
+
+use typst::diag::{FileError, FileResult, Severity, SourceDiagnostic};
+use typst::foundations::{Bytes, Datetime, Duration};
+use typst::layout::{Abs, Frame, FrameItem, Point, Transform};
+use typst::syntax::{FileId, RootedPath, Source, SyntaxKind, VirtualPath, VirtualRoot};
+use typst::text::{Font, FontBook, FontInstance};
+use typst::utils::LazyHash;
+use typst::visualize::{Curve, CurveItem, FixedStroke, Geometry, Paint};
+use typst::{Library, LibraryExt, World, WorldExt};
+use typst::text::{Coverage, FontAxis, FontFlags, FontInfo, FontVariant};
+use typst_kit::fonts::{FontPath, FontSource, FontStore, embedded, scan, system};
+use typst_layout::PagedDocument;
+use unicode_segmentation::UnicodeSegmentation;
+
+/// One item's row: kind, key, placement (6), fill (4), stroke (4), stroke width, advance, and
+/// where a glyph came from: its node in the source (start, end), the bytes of the node's text its
+/// cluster draws (start, end) and the node's kind (`NODE_*`).
+const ROW: usize = 23;
+/// A glyph's node: none (not from the main source), markup text (its text is its source), a
+/// string literal (its text is the string's value), or another node (an escape, a shorthand…).
+const NODE_NONE: f64 = 0.0;
+const NODE_TEXT: f64 = 1.0;
+const NODE_STR: f64 = 2.0;
+const NODE_OTHER: f64 = 3.0;
+const GLYPH: f64 = 0.0;
+const SHAPE: f64 = 1.0;
+
+/// Fonts and packages for one font setup, kept for the process: loading fonts costs
+/// milliseconds, compiling with them again is incremental.
+struct Setup {
+ library: LazyHash<Library>,
+ fonts: FontStore,
+ packages: Option<PathBuf>,
+ // a package's files, read and parsed once: a source kept is one Typst's memos recognize
+ files: Mutex<HashMap<FileId, FileResult<Bytes>>>,
+ sources: Mutex<HashMap<FileId, FileResult<Source>>>,
+}
+
+/// A setup's fonts: the directories scanned (the caller's and manimgx's own), the packages, and
+/// the system families the document names (lowercase; none: no system font at all).
+type SetupKey = (Vec<String>, Option<String>, Vec<String>);
+
+fn setups() -> &'static Mutex<HashMap<SetupKey, Arc<Setup>>> {
+ static SETUPS: OnceLock<Mutex<HashMap<SetupKey, Arc<Setup>>>> = OnceLock::new();
+ SETUPS.get_or_init(Default::default)
+}
+
+/// The fonts in `font_paths` (the caller's, then the ones manimgx ships) and Typst's own, and of
+/// the system's only the families in `named`: a document sets its text in manimgx's fonts on every
+/// machine, falls back only among them, and reaches a system font only by naming it.
+fn setup(font_paths: &[String], packages: Option<&str>, named: &[String]) -> Arc<Setup> {
+ let key = (font_paths.to_vec(), packages.map(str::to_owned), named.to_vec());
+ let mut all = setups().lock().unwrap();
+ all.entry(key)
+ .or_insert_with(|| {
+ let mut fonts = FontStore::new();
+ let dirs: Vec<PathBuf> = font_paths.iter().map(PathBuf::from).collect();
+ fonts.extend(cached_scan(&dirs, || dirs.iter().flat_map(|dir| scan(dir)).collect()));
+ fonts.extend(embedded());
+ if !named.is_empty() {
+ fonts.extend(
+ cached_scan(&font_directories(), || system().collect())
+ .into_iter()
+ .filter(|(_, info)| named.contains(&info.family.to_lowercase())),
+ );
+ }
+ Arc::new(Setup {
+ library: LazyHash::new(Library::default()),
+ fonts,
+ packages: packages.map(PathBuf::from),
+ files: Mutex::default(),
+ sources: Mutex::default(),
+ })
+ })
+ .clone()
+}
+
+/// A font on disk, mapped into memory when first used (not read whole: a CJK font is 60 MB and a
+/// text touches a few of its pages).
+struct Mapped {
+ path: PathBuf,
+ index: u32,
+}
+
+impl FontSource for Mapped {
+ fn load(&self) -> Option<Font> {
+ let file = std::fs::File::open(&self.path).ok()?;
+ // SAFETY: a font file is not written while mapped (as fontdb and typst-kit assume)
+ let map = unsafe { memmap2::Mmap::map(&file) }.ok()?;
+ Font::new(Bytes::new(map), self.index)
+ }
+}
+
+/// A scanned font as the cache keeps it: where it is, and its info's fields.
+type Cached = (PathBuf, u32, String, FontVariant, FontFlags, Vec<FontAxis>, Coverage);
+/// What made a cached scan, in its file's name: bump it with Typst, or when `Cached` changes.
+const SCAN: &str = "typst-0.15.1";
+
+/// The fonts under `dirs`, as Typst finds them (`find`: typst-kit's scan of them), read from a
+/// cache on disk while no file under them has changed: scanning parses every font (~50 ms for the
+/// system's, ~15 ms for a CJK collection); checking the files' sizes and times costs a few
+/// milliseconds. A font installed or removed changes the fingerprint, so every font stays usable.
+fn cached_scan(
+ dirs: &[PathBuf],
+ find: impl FnOnce() -> Vec<(FontPath, FontInfo)>,
+) -> Vec<(Mapped, FontInfo)> {
+ if dirs.is_empty() {
+ return Vec::new();
+ }
+ let dir = std::env::temp_dir().join("manimgx-fonts");
+ let key = fingerprint(dirs);
+ let cache = dir.join(format!("{SCAN}-{key:016x}.bin"));
+ let entries: Vec<Cached> = match std::fs::read(&cache).ok().and_then(|b| bincode::deserialize(&b).ok()) {
+ Some(entries) => entries,
+ None => {
+ let entries: Vec<Cached> = find()
+ .into_iter()
+ .map(|(path, info)| {
+ let FontInfo { family, variant, flags, axes, coverage } = info;
+ (path.path, path.index, family, variant, flags, axes, coverage)
+ })
+ .collect();
+ if let Ok(bytes) = bincode::serialize(&entries) {
+ // written whole, then renamed: a reader never sees half a file
+ let partial = dir.join(format!("{SCAN}-{key:016x}.{}.tmp", std::process::id()));
+ let _ = std::fs::create_dir_all(&dir)
+ .and_then(|()| std::fs::write(&partial, bytes))
+ .and_then(|()| std::fs::rename(&partial, &cache));
+ }
+ entries
+ }
+ };
+ entries
+ .into_iter()
+ .map(|(path, index, family, variant, flags, axes, coverage)| {
+ (Mapped { path, index }, FontInfo { family, variant, flags, axes, coverage })
+ })
+ .collect()
+}
+
+/// Where fontdb looks for the system's fonts (its `load_system_fonts`), and where typst-kit
+/// looks for Adobe's.
+fn font_directories() -> Vec<PathBuf> {
+ let home = std::env::var_os(if cfg!(windows) { "USERPROFILE" } else { "HOME" }).map(PathBuf::from);
+ let mut dirs: Vec<PathBuf> = Vec::new();
+ if cfg!(target_os = "macos") {
+ dirs.extend(["/Library/Fonts", "/System/Library/Fonts", "/Network/Library/Fonts"].map(PathBuf::from));
+ if let Ok(assets) = std::fs::read_dir("/System/Library/AssetsV2") {
+ dirs.extend(assets.flatten().map(|e| e.path()).filter(|p| {
+ p.file_name().is_some_and(|n| n.to_string_lossy().starts_with("com_apple_MobileAsset_Font"))
+ }));
+ }
+ dirs.extend(home.iter().flat_map(|h| {
+ let adobe = h.join("Library/Application Support/Adobe");
+ [h.join("Library/Fonts"), adobe.join("CoreSync/plugins/livetype/.r"), adobe.join(".User Owned Fonts")]
+ }));
+ } else if cfg!(windows) {
+ let root = std::env::var_os("SYSTEMROOT").map_or_else(|| PathBuf::from("C:\\Windows"), PathBuf::from);
+ dirs.push(root.join("Fonts"));
+ dirs.extend(home.iter().flat_map(|h| {
+ let adobe = h.join("AppData\\Roaming\\Adobe");
+ [
+ h.join("AppData\\Local\\Microsoft\\Windows\\Fonts"),
+ h.join("AppData\\Roaming\\Microsoft\\Windows\\Fonts"),
+ adobe.join("CoreSync\\plugins\\livetype\\r"),
+ adobe.join("User Owned Fonts"),
+ ]
+ }));
+ } else {
+ dirs.extend(["/usr/share/fonts", "/usr/local/share/fonts"].map(PathBuf::from));
+ dirs.extend(home.iter().flat_map(|h| [h.join(".local/share/fonts"), h.join(".fonts")]));
+ }
+ dirs
+}
+
+/// Every file under `dirs`: its path, size and modification time, hashed.
+fn fingerprint(dirs: &[PathBuf]) -> u64 {
+ use std::hash::{Hash, Hasher};
+ let mut hasher = std::hash::DefaultHasher::new();
+ let mut stack: Vec<PathBuf> = dirs.to_vec();
+ while let Some(dir) = stack.pop() {
+ dir.hash(&mut hasher);
+ let Ok(entries) = std::fs::read_dir(&dir) else { continue };
+ let mut entries: Vec<_> = entries.flatten().collect();
+ entries.sort_by_key(|e| e.file_name());
+ for entry in entries {
+ let Ok(meta) = std::fs::metadata(entry.path()) else { continue };
+ if meta.is_dir() {
+ stack.push(entry.path());
+ } else {
+ entry.file_name().hash(&mut hasher);
+ meta.len().hash(&mut hasher);
+ meta.modified().ok().hash(&mut hasher);
+ }
+ }
+ }
+ hasher.finish()
+}
+
+/// A document: its setup and its main source.
+struct Document<'a> {
+ setup: &'a Setup,
+ main: Source,
+}
+
+impl Document<'_> {
+ /// A package's file: mitex's from the engine (`@preview/mitex`, the package the converted
+ /// LaTeX calls, built in), any other from the packages' directory, if there is one.
+ fn read(&self, id: FileId) -> FileResult<Bytes> {
+ let path = id.get();
+ let within = path.vpath().get_without_slash();
+ if let VirtualRoot::Package(spec) = path.root()
+ && (spec.namespace.as_str(), spec.name.as_str()) == ("preview", "mitex")
+ && spec.version.to_string() == mitex_spec_gen::VERSION
+ && let Some((_, bytes)) = mitex_spec_gen::PACKAGE.iter().find(|(p, _)| *p == within)
+ {
+ return Ok(Bytes::new(*bytes));
+ }
+ let (VirtualRoot::Package(spec), Some(root)) = (path.root(), &self.setup.packages) else {
+ return Err(FileError::NotFound(within.into()));
+ };
+ let file = root
+ .join(spec.namespace.as_str())
+ .join(spec.name.as_str())
+ .join(spec.version.to_string())
+ .join(within);
+ std::fs::read(&file)
+ .map(Bytes::new)
+ .map_err(|error| FileError::from_io(error, &file))
+ }
+}
+
+impl World for Document<'_> {
+ fn library(&self) -> &LazyHash<Library> {
+ &self.setup.library
+ }
+ fn book(&self) -> &LazyHash<FontBook> {
+ self.setup.fonts.book()
+ }
+ fn main(&self) -> FileId {
+ self.main.id()
+ }
+ fn source(&self, id: FileId) -> FileResult<Source> {
+ if id == self.main.id() {
+ return Ok(self.main.clone());
+ }
+ let cached = self.setup.sources.lock().unwrap().get(&id).cloned();
+ if let Some(source) = cached {
+ return source;
+ }
+ let source = self.file(id).and_then(|bytes| {
+ let text = std::str::from_utf8(&bytes).map_err(|_| FileError::InvalidUtf8)?;
+ Ok(Source::new(id, text.into()))
+ });
+ self.setup.sources.lock().unwrap().insert(id, source.clone());
+ source
+ }
+ fn file(&self, id: FileId) -> FileResult<Bytes> {
+ let mut files = self.setup.files.lock().unwrap();
+ files.entry(id).or_insert_with(|| self.read(id)).clone()
+ }
+ fn font(&self, index: usize) -> Option<Font> {
+ self.setup.fonts.font(index)
+ }
+ fn today(&self, _: Option<Duration>) -> Option<Datetime> {
+ None
+ }
+}
+
+/// Fonts by the keys glyph outlines are cached under: a font instance's number, times 65536,
+/// plus the glyph's id. Numbers are for the process, so a key names one outline forever.
+fn fonts() -> &'static Mutex<(HashMap<FontInstance, u32>, Vec<FontInstance>)> {
+ static FONTS: OnceLock<Mutex<(HashMap<FontInstance, u32>, Vec<FontInstance>)>> =
+ OnceLock::new();
+ FONTS.get_or_init(Default::default)
+}
+
+fn font_number(font: &FontInstance) -> u32 {
+ let mut fonts = fonts().lock().unwrap();
+ if let Some(&n) = fonts.0.get(font) {
+ return n;
+ }
+ let n = fonts.1.len() as u32;
+ fonts.0.insert(font.clone(), n);
+ fonts.1.push(font.clone());
+ n
+}
+
+fn rgba(paint: &Paint) -> [f64; 4] {
+ // a gradient or a tiling: its first color stands for it (a part has one paint)
+ let color = match paint {
+ Paint::Solid(color) => color.clone(),
+ Paint::Gradient(gradient) => gradient.stops_ref()[0].0.clone(),
+ Paint::Tiling(_) => typst::visualize::Color::BLACK,
+ };
+ let rgb = color.to_rgb();
+ // in 8 bits a channel, as manimgx's colors are
+ [rgb.red, rgb.green, rgb.blue, rgb.alpha].map(|c| (f64::from(c) * 255.0).round() / 255.0)
+}
+
+/// A stroke's color and width (none: [-1; 4], 0).
+fn stroke(stroke: &Option<FixedStroke>) -> ([f64; 4], f64) {
+ stroke.as_ref().map_or(([-1.0; 4], 0.0), |s| (rgba(&s.paint), s.thickness.to_pt()))
+}
+
+/// Page (points, y down) to points with y up.
+fn flip(ts: Transform) -> Transform {
+ Transform::scale(typst::layout::Ratio::one(), -typst::layout::Ratio::one()).pre_concat(ts)
+}
+
+fn affine(ts: Transform) -> [f64; 6] {
+ [ts.sx.get(), ts.ky.get(), ts.kx.get(), ts.sy.get(), ts.tx.to_pt(), ts.ty.to_pt()]
+}
+
+struct Walk<'a> {
+ doc: &'a Document<'a>,
+ rows: Vec<f64>,
+ shapes: Vec<Vec<f64>>,
+ labels: Vec<(String, Vec<usize>)>,
+ open: Vec<usize>, // the labelled groups the walk is inside
+}
+
+impl Walk<'_> {
+ fn item(&mut self, row: [f64; ROW]) {
+ let index = self.rows.len() / ROW;
+ for &group in &self.open {
+ self.labels[group].1.push(index);
+ }
+ self.rows.extend_from_slice(&row);
+ }
+
+ /// Where a glyph came from: its node's source range, the bytes of the node's text its
+ /// cluster draws (`offset`, and `len` more) and the node's kind.
+ fn origin(&self, span: typst::syntax::Span, offset: u16, len: usize) -> [f64; 5] {
+ let node = (span.id() == Some(self.doc.main.id()))
+ .then(|| self.doc.main.find(span))
+ .flatten();
+ let Some(node) = node else {
+ return [-1.0, -1.0, -1.0, -1.0, NODE_NONE];
+ };
+ let kind = match node.kind() {
+ SyntaxKind::Text => NODE_TEXT,
+ SyntaxKind::Str => NODE_STR,
+ _ => NODE_OTHER,
+ };
+ let (range, start) = (node.range(), usize::from(offset));
+ [range.start as f64, range.end as f64, start as f64, (start + len) as f64, kind]
+ }
+
+ fn frame(&mut self, frame: &Frame, ts: Transform) {
+ for (pos, item) in frame.items() {
+ let at = ts.pre_concat(Transform::translate(pos.x, pos.y));
+ match item {
+ FrameItem::Group(group) => {
+ let labelled = group.label.map(|label| {
+ self.labels.push((label.resolve().to_string(), Vec::new()));
+ self.open.push(self.labels.len() - 1);
+ });
+ self.frame(&group.frame, at.pre_concat(group.transform));
+ if labelled.is_some() {
+ self.open.pop();
+ }
+ }
+ FrameItem::Text(text) => {
+ let number = f64::from(font_number(&text.font));
+ let scale = text.size.to_pt() / text.font.units_per_em();
+ let fill = rgba(&text.fill);
+ let (stroke, width) = stroke(&text.stroke);
+ // a run advances along x, or along y (a stretched delimiter's parts)
+ let (mut x, mut y) = (Abs::zero(), Abs::zero());
+ for glyph in &text.glyphs {
+ let origin = Point::new(
+ x + glyph.x_offset.at(text.size),
+ -(y + glyph.y_offset.at(text.size)),
+ );
+ // outline (font units, y up) → page (y down) → y up
+ let placement = flip(at)
+ .pre_concat(Transform::translate(origin.x, origin.y))
+ .pre_concat(Transform::scale(
+ typst::layout::Ratio::new(scale),
+ typst::layout::Ratio::new(-scale),
+ ));
+ let [node_start, node_end, from, to, kind] =
+ self.origin(glyph.span.0, glyph.span.1, glyph.range().len());
+ let [a, b, c, d, e, f] = affine(placement);
+ let advance = glyph.x_advance.get() * text.font.units_per_em();
+ self.item([
+ GLYPH,
+ number * 65536.0 + f64::from(glyph.id),
+ a, b, c, d, e, f,
+ fill[0], fill[1], fill[2], fill[3],
+ stroke[0], stroke[1], stroke[2], stroke[3],
+ width,
+ advance,
+ node_start, node_end, from, to, kind,
+ ]);
+ x += glyph.x_advance.at(text.size);
+ y += glyph.y_advance.at(text.size);
+ }
+ }
+ FrameItem::Shape(shape, _) => {
+ let curve = match &shape.geometry {
+ Geometry::Line(to) => {
+ let mut c = Curve::new();
+ c.move_(Point::zero());
+ c.line(*to);
+ c
+ }
+ Geometry::Rect(size) => Curve::rect(*size),
+ Geometry::Curve(curve) => curve.clone(),
+ };
+ let points = cubics(&curve, flip(at));
+ if points.is_empty() {
+ continue;
+ }
+ let fill = shape.fill.as_ref().map_or([-1.0; 4], rgba);
+ let (stroke, width) = stroke(&shape.stroke);
+ let index = self.shapes.len() as f64;
+ self.shapes.push(points);
+ self.item([
+ SHAPE, index, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0,
+ fill[0], fill[1], fill[2], fill[3],
+ stroke[0], stroke[1], stroke[2], stroke[3],
+ width, 0.0, -1.0, -1.0, -1.0, -1.0, NODE_NONE,
+ ]);
+ }
+ FrameItem::Image(..) | FrameItem::Link(..) | FrameItem::Tag(..) => {}
+ }
+ }
+ }
+}
+
+/// A curve as manimgx's cubic points (4 per curve, x y z), lines raised to cubics.
+fn cubics(curve: &Curve, ts: Transform) -> Vec<f64> {
+ let mut out = Vec::new();
+ let p = |q: Point| {
+ let q = q.transform(ts);
+ [q.x.to_pt(), q.y.to_pt()]
+ };
+ let mut push = |a: [f64; 2], b: [f64; 2], c: [f64; 2], d: [f64; 2]| {
+ for q in [a, b, c, d] {
+ out.extend_from_slice(&[q[0], q[1], 0.0]);
+ }
+ };
+ let (mut pen, mut start) = ([0.0; 2], [0.0; 2]);
+ for item in curve.0.iter() {
+ match *item {
+ CurveItem::Move(to) => {
+ pen = p(to);
+ start = pen;
+ }
+ CurveItem::Line(to) => {
+ let end = p(to);
+ push(pen, lerp(pen, end, 1.0 / 3.0), lerp(pen, end, 2.0 / 3.0), end);
+ pen = end;
+ }
+ CurveItem::Cubic(c1, c2, to) => {
+ let end = p(to);
+ push(pen, p(c1), p(c2), end);
+ pen = end;
+ }
+ CurveItem::Close => {
+ if pen != start {
+ push(pen, lerp(pen, start, 1.0 / 3.0), lerp(pen, start, 2.0 / 3.0), start);
+ }
+ pen = start;
+ }
+ }
+ }
+ out
+}
+
+fn lerp(a: [f64; 2], b: [f64; 2], t: f64) -> [f64; 2] {
+ [a[0] + (b[0] - a[0]) * t, a[1] + (b[1] - a[1]) * t]
+}
+
+fn message(diagnostics: &[SourceDiagnostic], doc: &Document) -> String {
+ diagnostics
+ .iter()
+ .filter(|d| d.severity == Severity::Error)
+ .map(|d| {
+ let at = doc
+ .range(d.span)
+ .map(|r| format!(" (source bytes {}..{})", r.start, r.end))
+ .unwrap_or_default();
+ format!("{}{at}", d.message)
+ })
+ .collect::<Vec<_>>()
+ .join("; ")
+}
+
+pub type Layout = (Vec<f64>, Vec<Vec<f64>>, Vec<(String, Vec<usize>)>, bool);
+
+/// Typeset `source` with the fonts in `font_paths` and Typst's own (a system font only by its
+/// family's name), and the packages under `packages` besides mitex's (the engine's own): its
+/// items (one row of `ROW` floats each, in document order), its shapes' cubic points and its
+/// labelled groups (a label and the items inside, nested ones too).
+pub fn typeset(source: String, font_paths: &[String], packages: Option<&str>) -> Result<Layout, String> {
+ {
+ static COMPILES: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
+ let file = RootedPath::new(VirtualRoot::Project, VirtualPath::new("main.typ").unwrap())
+ .intern();
+ let main = Source::new(file, source);
+ // manimgx's fonts (the caller's, the bundled ones, Typst's own); a system font only
+ // when the document names a family they don't include, and then that family alone
+ let lean = setup(font_paths, packages, &[]);
+ let doc = Document { setup: &lean, main: main.clone() };
+ let warned = typst::compile::<PagedDocument>(&doc);
+ let mut named: Vec<String> = warned
+ .warnings
+ .iter()
+ .filter_map(|w| w.message.strip_prefix("unknown font family: "))
+ .map(str::to_owned)
+ .collect();
+ named.sort();
+ named.dedup();
+ let full;
+ let system = !named.is_empty();
+ let (doc, compiled) = if system {
+ full = setup(font_paths, packages, &named);
+ let doc = Document { setup: &full, main };
+ let compiled = typst::compile::<PagedDocument>(&doc).output;
+ (doc, compiled)
+ } else {
+ (doc, warned.output)
+ };
+ // Typst memoizes across documents; forget what the last few hundred didn't use
+ if COMPILES.fetch_add(1, std::sync::atomic::Ordering::Relaxed) % 256 == 255 {
+ typst::comemo::evict(8);
+ }
+ let document = compiled.map_err(|errors| message(&errors, &doc))?;
+ let page = document.pages().first().ok_or("Typst produced no page")?;
+ let mut walk =
+ Walk { doc: &doc, rows: Vec::new(), shapes: Vec::new(), labels: Vec::new(), open: Vec::new() };
+ walk.frame(&page.frame, Transform::identity());
+ Ok((walk.rows, walk.shapes, walk.labels, system))
+ }
+}
+
+/// The system's fonts' fingerprint (every file under their directories: path, size, time), once
+/// per process: a layout set with system fonts is good while it is unchanged.
+pub fn system_fonts_fingerprint() -> u64 {
+ static FINGERPRINT: OnceLock<u64> = OnceLock::new();
+ *FINGERPRINT.get_or_init(|| fingerprint(&font_directories()))
+}
+
+/// The fonts a player's face is set in (see `text`): the font files in `dirs` (manimgx's), and
+/// Typst's monospace one, DejaVu Sans Mono; each file's bytes, kept (mapped, where files are) for
+/// as long as the program runs.
+pub fn face_fonts(dirs: &[String]) -> Vec<&'static [u8]> {
+ let mut files: Vec<&'static [u8]> = Vec::new();
+ for dir in dirs {
+ let Ok(entries) = std::fs::read_dir(dir) else { continue };
+ let font = |p: &PathBuf| p.extension().and_then(|e| e.to_str()).is_some_and(|e| ["ttf", "otf", "ttc", "otc"].contains(&e.to_ascii_lowercase().as_str()));
+ let mut paths: Vec<PathBuf> = entries.flatten().map(|e| e.path()).filter(font).collect();
+ paths.sort();
+ for path in paths {
+ let Ok(file) = std::fs::File::open(&path) else { continue };
+ // SAFETY: a font file is not written while mapped (as fontdb and typst-kit assume)
+ if let Ok(map) = unsafe { memmap2::Mmap::map(&file) } {
+ files.push(&Box::leak(Box::new(map))[..]);
+ }
+ }
+ }
+ let mono = embedded().find(|(_, info)| info.family == "DejaVu Sans Mono" && info.variant == FontVariant::default());
+ files.extend(mono.map(|(font, _)| &*Box::leak(font.data().to_vec().into_boxed_slice())));
+ files
+}
+
+/// A glyph's outline as a curve (font units): TrueType's quadratics raised to cubics.
+struct Pen {
+ curve: Curve,
+ at: Point,
+}
+
+impl Pen {
+ fn point(x: f32, y: f32) -> Point {
+ Point::new(Abs::pt(f64::from(x)), Abs::pt(f64::from(y)))
+ }
+}
+
+impl ttf_parser::OutlineBuilder for Pen {
+ fn move_to(&mut self, x: f32, y: f32) {
+ self.at = Self::point(x, y);
+ self.curve.move_(self.at);
+ }
+ fn line_to(&mut self, x: f32, y: f32) {
+ self.at = Self::point(x, y);
+ self.curve.line(self.at);
+ }
+ fn quad_to(&mut self, x1: f32, y1: f32, x: f32, y: f32) {
+ let (c, end) = (Self::point(x1, y1), Self::point(x, y));
+ let third = |a: Point, b: Point| a + (b - a) * (2.0 / 3.0);
+ self.curve.cubic(third(self.at, c), third(end, c), end);
+ self.at = end;
+ }
+ fn curve_to(&mut self, x1: f32, y1: f32, x2: f32, y2: f32, x: f32, y: f32) {
+ self.at = Self::point(x, y);
+ self.curve.cubic(Self::point(x1, y1), Self::point(x2, y2), self.at);
+ }
+ fn close(&mut self) {
+ self.curve.close();
+ }
+}
+
+/// Each key's outline as cubic points (font units, y up); empty for a glyph with no outline
+/// (a space) or one Typst would draw as an image (a color emoji).
+pub fn glyph_outlines(keys: &[u64]) -> Vec<Vec<f64>> {
+ let fonts: Vec<(FontInstance, u16)> = {
+ let fonts = fonts().lock().unwrap();
+ keys.iter().map(|k| (fonts.1[(k / 65536) as usize].clone(), (k % 65536) as u16)).collect()
+ };
+ fonts
+ .iter()
+ .map(|(font, id)| {
+ let mut pen = Pen { curve: Curve::new(), at: Point::zero() };
+ font.ttf().outline_glyph(ttf_parser::GlyphId(*id), &mut pen);
+ cubics(&pen.curve, Transform::identity())
+ })
+ .collect()
+}
+
+/// A ligature glyph's caret positions (font units), from the font's GDEF ligature caret list;
+/// empty when the font gives none (then a glyph is divided equally among what it draws).
+pub fn ligature_carets(key: u64) -> Vec<f64> {
+ let font = fonts().lock().unwrap().1[(key / 65536) as usize].clone();
+ carets(font.font().data(), font.font().index(), (key % 65536) as u16).unwrap_or_default()
+}
+
+/// GDEF's LigCaretList: Coverage → LigGlyph → CaretValues (formats 1 and 3: a coordinate;
+/// format 2, a contour point, is skipped).
+fn carets(data: &[u8], index: u32, glyph: u16) -> Option<Vec<f64>> {
+ let face = ttf_parser::RawFace::parse(data, index).ok()?;
+ let gdef = face.table(ttf_parser::Tag::from_bytes(b"GDEF"))?;
+ let u16_at = |d: &[u8], at: usize| Some(u16::from_be_bytes([*d.get(at)?, *d.get(at + 1)?]));
+ let list = usize::from(u16_at(gdef, 8)?);
+ if list == 0 {
+ return None;
+ }
+ let lig = gdef.get(list..)?;
+ let coverage = lig.get(usize::from(u16_at(lig, 0)?)..)?;
+ let at = coverage_index(coverage, glyph)?;
+ let glyph_table = lig.get(usize::from(u16_at(lig, 4 + 2 * at)?)..)?;
+ let count = usize::from(u16_at(glyph_table, 0)?);
+ let mut out = Vec::new();
+ for k in 0..count {
+ let value = glyph_table.get(usize::from(u16_at(glyph_table, 2 + 2 * k)?)..)?;
+ match u16_at(value, 0)? {
+ 1 | 3 => out.push(f64::from(i16::from_be_bytes([value[2], value[3]]))),
+ _ => return None,
+ }
+ }
+ Some(out)
+}
+
+fn coverage_index(coverage: &[u8], glyph: u16) -> Option<usize> {
+ let u16_at = |at: usize| Some(u16::from_be_bytes([*coverage.get(at)?, *coverage.get(at + 1)?]));
+ match u16_at(0)? {
+ 1 => (0..usize::from(u16_at(2)?)).find(|&i| u16_at(4 + 2 * i) == Some(glyph)),
+ 2 => {
+ for i in 0..usize::from(u16_at(2)?) {
+ let (start, end, first) = (u16_at(4 + 6 * i)?, u16_at(6 + 6 * i)?, u16_at(8 + 6 * i)?);
+ if (start..=end).contains(&glyph) {
+ return Some(usize::from(first + (glyph - start)));
+ }
+ }
+ None
+ }
+ _ => None,
+ }
+}
+
+/// Where `text`'s graphemes (user-perceived characters, UAX #29) start, in characters.
+pub fn graphemes(text: &str) -> Vec<usize> {
+ let mut chars = 0;
+ text.graphemes(true)
+ .map(|g| {
+ let at = chars;
+ chars += g.chars().count();
+ at
+ })
+ .collect()
+}
+
+/// LaTeX math as Typst math (mitex's conversion; its default command spec).
+pub fn mitex_math(latex: &str) -> Result<String, String> {
+ mitex::convert_math(latex, None)
+}
+
+/// LaTeX text as Typst markup (mitex's conversion; its default command spec).
+pub fn mitex_text(latex: &str) -> Result<String, String> {
+ mitex::convert_text(latex, None)
+}