cellix-doc 0.4.3

Documents for Cellix: typeset math, HTML export, LaTeX structure and PDF pages
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
//! Typeset math, from pure Rust and fonts in the binary, so it works over SSH on a server
//! with no TeX. A formula that won't parse is an error here and its source on the page.

use std::cell::OnceCell;
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
use std::sync::Arc;

use latex_rust::{
    Dim, MathBox, MathFont, MathStyle, PngOptions, SvgOptions, latex_to_svg,
    layout_with_em_size_pt, parse, render_png,
};

/// A typeset formula as coverage, so it can take the colour of the text around it.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Formula {
    pub id: u64,
    pub width: u32,
    pub height: u32,
    /// Rows of pixels above the baseline.
    pub baseline: u32,
    pub alpha: Arc<Vec<u8>>,
}

thread_local! {
    static FONT: OnceCell<Option<MathFont>> = const { OnceCell::new() };
}

fn with_font<T>(run: impl FnOnce(&MathFont) -> Result<T, String>) -> Result<T, String> {
    FONT.with(|cell| {
        let font = cell.get_or_init(|| MathFont::stix_two_math().ok());
        run(font.as_ref().ok_or("the math font didn't load")?)
    })
}

/// Environments that only number or wrap math, which the page has no use for.
const WRAPPERS: [&str; 4] = ["equation", "equation*", "displaymath", "math"];

/// Commands the engine doesn't have, as what it does. Longest names first where one is the
/// start of another. The page is a preview, and a near match beats losing a whole display.
const SUBSTITUTES: [(&str, &str); 24] = [
    // In `\operatorname` a thin space prints as a comma, so these stay in math.
    ("\\operatorname*{", "\\mathrm{"),
    ("\\operatorname{arg\\,", "\\mathrm{arg\\,"),
    ("\\argmin", "\\mathrm{arg\\,min}"),
    ("\\argmax", "\\mathrm{arg\\,max}"),
    ("\\bm{", "\\mathbf{"),
    ("\\boldsymbol{", "\\mathbf{"),
    ("\\mathds{", "\\mathbb{"),
    ("\\coloneqq", ":="),
    ("\\eqqcolon", "=:"),
    ("\\varinjlim", "\\lim"),
    ("\\varprojlim", "\\lim"),
    ("\\xmapsto", "\\xrightarrow"),
    ("\\llbracket", "[\\!["),
    ("\\rrbracket", "]\\!]"),
    ("\\bmod", "\\operatorname{mod}"),
    ("\\qed", "\\square"),
    ("\\middle", ""),
    ("\\displaystyle", ""),
    ("\\textstyle", ""),
    ("\\nonumber", ""),
    ("\\notag", ""),
    ("{dcases}", "{cases}"),
    ("{rcases}", "{cases}"),
    ("{smallmatrix}", "{matrix}"),
];

/// Something unseen that reaches a script's depth below a row's own limits.
const ROW_DEPTH: &str = "\\vphantom{\\underset{\\underset{x}{x}}{X}}";

/// Operators whose limits stand under them in a display. The engine only does this for `\lim`.
const UNDER: [&str; 8] = [
    "\\max",
    "\\min",
    "\\sup",
    "\\inf",
    "\\limsup",
    "\\liminf",
    "\\mathrm{arg\\,min}",
    "\\mathrm{arg\\,max}",
];

/// A script at the start of `text`: its text, and how far it runs.
fn script(text: &str) -> Option<(&str, usize)> {
    let mut chars = text.char_indices();
    match chars.next()?.1 {
        '{' => {
            let mut depth = 0;
            for (at, c) in text.char_indices() {
                depth += match c {
                    '{' => 1,
                    '}' => -1,
                    _ => 0,
                };
                if depth == 0 {
                    return Some((&text[1..at], at + 1));
                }
            }
            None
        }
        '\\' => {
            let end = chars
                .find(|(_, c)| !c.is_ascii_alphabetic())
                .map_or(text.len(), |(at, _)| at);
            Some((&text[..end], end))
        }
        c => Some((&text[..c.len_utf8()], c.len_utf8())),
    }
}

/// Big operators, whose limits the engine only stacks by itself in a display.
const BIG: [&str; 7] = [
    "\\sum", "\\prod", "\\coprod", "\\int", "\\bigcup", "\\bigcap", "\\oint",
];

/// Sets `\sum\limits_{a}^{b}` as a stack, since the engine ignores the word for these.
fn limits_asked_for(text: &str) -> String {
    let mut out = String::with_capacity(text.len());
    let mut rest = text;
    while let Some(at) = rest.find('\\') {
        out.push_str(&rest[..at]);
        rest = &rest[at..];
        let asked = BIG.iter().find_map(|name| {
            let after = rest.strip_prefix(*name)?.trim_start();
            Some((*name, after.strip_prefix("\\limits")?))
        });
        let Some((name, mut tail)) = asked else {
            out.push('\\');
            rest = &rest[1..];
            continue;
        };
        let (mut under, mut over) = (None, None);
        for _ in 0..2 {
            let trimmed = tail.trim_start();
            let slot = match trimmed.chars().next() {
                Some('_') => &mut under,
                Some('^') => &mut over,
                _ => break,
            };
            let body = trimmed[1..].trim_start();
            let Some((text, used)) = script(body) else {
                break;
            };
            *slot = Some(text);
            tail = &body[used..];
        }
        let mut stack = name.to_string();
        if let Some(over) = over {
            stack = format!("\\overset{{{over}}}{{{stack}}}");
        }
        if let Some(under) = under {
            stack = format!("\\underset{{{under}}}{{{stack}}}");
        }
        out.push_str(&stack);
        rest = tail;
    }
    out.push_str(rest);
    out
}

/// Sets `\max_{x}` as a stack, so its limit stands under it the way TeX puts it.
fn limits_under(text: &str) -> String {
    let mut out = String::with_capacity(text.len());
    let mut rest = text;
    while let Some(at) = rest.find('\\') {
        out.push_str(&rest[..at]);
        rest = &rest[at..];
        let found = UNDER.iter().find(|name| {
            rest.strip_prefix(**name)
                .is_some_and(|after| !after.starts_with(|c: char| c.is_ascii_alphabetic()))
        });
        let stacked = found.and_then(|name| {
            let after = rest[name.len()..].trim_start();
            let after = after.strip_prefix("\\limits").unwrap_or(after).trim_start();
            let (under, used) = script(after.strip_prefix('_')?.trim_start())?;
            let spaces = after.len() - 1 - after[1..].trim_start().len();
            let tail = &after[1 + spaces + used..];
            Some((format!("\\underset{{{under}}}{{{name}}}"), tail))
        });
        match stacked {
            Some((stack, tail)) => {
                out.push_str(&stack);
                rest = tail;
            }
            None => {
                let step = found.map_or(1, |name| name.len());
                out.push_str(&rest[..step]);
                rest = &rest[step..];
            }
        }
    }
    out.push_str(rest);
    out
}

/// Takes `\name{...}` out of `text`, with `keep` deciding what stands in its place.
fn strip_command(text: &mut String, name: &str, keep: bool) {
    let open = format!("{name}{{");
    while let Some(at) = text.find(&open) {
        let start = at + open.len();
        let mut depth = 1;
        let mut end = None;
        for (i, c) in text[start..].char_indices() {
            match c {
                '{' => depth += 1,
                '}' => depth -= 1,
                _ => {}
            }
            if depth == 0 {
                end = Some(start + i);
                break;
            }
        }
        let Some(end) = end else {
            return;
        };
        let inner = text[start..end].to_string();
        text.replace_range(at..=end, if keep { &inner } else { "" });
    }
}

/// What the math engine reads: `\label` has no picture, and `equation` only numbers.
fn prepared(source: &str, display: bool) -> String {
    let mut text = source.trim().to_string();
    for name in WRAPPERS {
        let (open, close) = (format!("\\begin{{{name}}}"), format!("\\end{{{name}}}"));
        if let Some(inner) = text
            .strip_prefix(&open)
            .and_then(|t| t.strip_suffix(&close))
        {
            text = inner.trim().to_string();
        }
    }
    // Numbers belong in the margin, where the page puts them.
    strip_command(&mut text, "\\label", false);
    strip_command(&mut text, "\\tag", false);
    strip_command(&mut text, "\\tag*", false);
    strip_command(&mut text, "\\smash", true);
    strip_command(&mut text, "\\mathop", true);
    strip_command(&mut text, "\\mathrel", true);
    strip_command(&mut text, "\\mathbin", true);
    for (from, to) in SUBSTITUTES {
        text = text.replace(from, to);
    }
    // Numbered environments number themselves, and restart in every block, so they all go
    // through the one that doesn't.
    for name in [
        "align", "gather", "multline", "alignat", "flalign", "eqnarray",
    ] {
        text = text
            .replace(&format!("{{{name}*}}"), "{aligned}")
            .replace(&format!("{{{name}}}"), "{aligned}");
    }
    let text = limits_asked_for(&text.replace("{gathered}", "{aligned}"));
    let text = if display { limits_under(&text) } else { text };
    if text.contains("{aligned}") {
        roomy_rows(&text)
    } else {
        text
    }
}

/// Gives a row with limits under it more depth, so they clear the next row's. The engine has
/// no `\\\\[gap]`, but a row is as deep as whatever's in it, seen or not.
fn roomy_rows(text: &str) -> String {
    let rows: Vec<&str> = text.split("\\\\").collect();
    let last = rows.len().saturating_sub(1);
    let spaced = rows.iter().enumerate().map(|(index, row)| {
        if index < last && row.contains("\\underset") {
            format!("{row}{ROW_DEPTH}")
        } else {
            (*row).to_string()
        }
    });
    spaced.collect::<Vec<_>>().join("\\\\")
}

/// Where each row of a display stands in its picture, as shares of its height, from what
/// each row measures alone. None if one of them can't be set by itself.
pub fn row_centres(rows: &[String], em: u32) -> Option<Vec<f32>> {
    let mut heights = Vec::with_capacity(rows.len());
    for row in rows {
        let mut text = row.replace('&', " ");
        for word in [
            "\\begin{align*}",
            "\\begin{align}",
            "\\end{align*}",
            "\\end{align}",
        ] {
            text = text.replace(word, " ");
        }
        for word in [
            "\\begin{gather*}",
            "\\begin{gather}",
            "\\end{gather*}",
            "\\end{gather}",
        ] {
            text = text.replace(word, " ");
        }
        heights.push(render(&text, true, em).ok()?.height as f32);
    }
    let total: f32 = heights.iter().sum();
    let mut done = 0.0;
    let centres = heights.iter().map(|height| {
        let centre = (done + height / 2.0) / total.max(1.0);
        done += height;
        centre
    });
    Some(centres.collect())
}

fn pixels(dim: &Dim, em: u32) -> f64 {
    let (top, bottom) = dim.as_ratio().unwrap_or((0, 1));
    top as f64 / bottom.max(1) as f64 * em as f64
}

fn decode(bytes: &[u8]) -> Result<(u32, u32, Vec<u8>), String> {
    let mut reader = png::Decoder::new(std::io::Cursor::new(bytes))
        .read_info()
        .map_err(|e| e.to_string())?;
    let size = reader
        .output_buffer_size()
        .ok_or("the image is too large")?;
    let mut buffer = vec![0; size];
    let info = reader.next_frame(&mut buffer).map_err(|e| e.to_string())?;
    let channels = info.color_type.samples();
    let alpha = buffer[..info.buffer_size()]
        .chunks_exact(channels)
        .map(|p| p[channels - 1])
        .collect();
    Ok((info.width, info.height, alpha))
}

/// Typesets a formula with `em` pixels to the em.
pub fn render(source: &str, display: bool, em: u32) -> Result<Formula, String> {
    let text = prepared(source, display);
    if text.is_empty() {
        return Err("empty formula".to_string());
    }
    let ast = parse(&text).map_err(|e| e.to_string())?;
    with_font(|font| {
        let style = if display {
            MathStyle::Display
        } else {
            MathStyle::Text
        };
        let size = Dim::from_i64(i64::from(em));
        let tree: MathBox =
            layout_with_em_size_pt(&ast, font, style, &size).map_err(|e| e.to_string())?;
        // At 72 dpi a point is a pixel, so the em size is exact.
        let mut options = PngOptions::new();
        options.font_size_pt = size;
        options.dpi = Dim::from_i64(72);
        let bytes = render_png(&tree, font, &options).map_err(|e| e.to_string())?;
        let (width, height, alpha) = decode(&bytes)?;
        let mut hasher = DefaultHasher::new();
        (&text, display, em).hash(&mut hasher);
        Ok(Formula {
            id: hasher.finish(),
            width,
            height,
            baseline: pixels(&tree.height, em).round() as u32,
            alpha: Arc::new(alpha),
        })
    })
}

/// A formula as self-contained SVG, for pages that leave Cellix.
pub fn svg(source: &str, display: bool) -> Result<String, String> {
    let text = prepared(source, display);
    with_font(|font| {
        let mut options = SvgOptions::new();
        options.display = display;
        let svg = latex_to_svg(&text, font, &options).map_err(|e| e.to_string())?;
        // The XML header would land in the middle of a page.
        Ok(svg
            .find("<svg")
            .map_or(svg.clone(), |at| svg[at..].to_string()))
    })
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn limits_stand_under_operators_in_a_display() {
        let beside = render(r"\max_{x \in X} f", false, 30).unwrap();
        let under = render(r"\max_{x \in X} f", true, 30).unwrap();
        assert!(under.height > beside.height + 10);
        let wide = render(r"\argmin_{\theta} f + \min_x g + \max^2", true, 30).unwrap();
        assert!(wide.height > beside.height + 10);
        assert_eq!(
            prepared(r"\max_{x}^2 \maximum", true),
            r"\underset{x}{\max}^2 \maximum"
        );
        assert_eq!(prepared(r"\min\limits_a b", true), r"\underset{a}{\min} b");
    }

    #[test]
    fn a_thin_space_in_an_operators_name_is_not_a_comma() {
        let text = prepared(r"\operatorname{arg\,min} + \operatorname*{arg\,max}", false);
        assert!(!text.contains("operatorname"), "{text}");
    }

    #[test]
    fn rows_of_a_display_keep_clear_of_each_other() {
        let plain = r"\begin{aligned} a &= \mathrm{x}_{y} \\ c &= d \end{aligned}";
        let under = r"\begin{aligned} a &= \max_{y} \\ c &= d \end{aligned}";
        assert!(!prepared(plain, true).contains("vphantom"));
        assert!(prepared(under, true).contains("vphantom"));
        let (low, high) = (
            render(plain, true, 30).unwrap(),
            render(under, true, 30).unwrap(),
        );
        assert!(high.height > low.height + 10);
    }

    #[test]
    fn a_tall_row_takes_its_share_of_the_picture() {
        let rows = vec![
            r"\begin{align} a &= b".to_string(),
            r"\sum_{i=1}^{n} c \end{align}".to_string(),
        ];
        let centres = row_centres(&rows, 30).unwrap();
        assert!(centres[0] < 0.3, "{centres:?}");
        assert!(centres[1] > 0.5, "{centres:?}");
    }

    #[test]
    fn limits_asked_for_in_running_text_stand_over_and_under() {
        assert_eq!(
            prepared(r"\sum\limits_{i=1}^{n} x", false),
            r"\underset{i=1}{\overset{n}{\sum}} x"
        );
        assert_eq!(
            prepared(r"\prod\limits^2_a", false),
            r"\underset{a}{\overset{2}{\prod}}"
        );
        let beside = render(r"\sum_{i=1}^n x", false, 30).unwrap();
        let stacked = render(r"\sum\limits_{i=1}^n x", false, 30).unwrap();
        assert!(stacked.height > beside.height + 8);
    }

    #[test]
    fn asking_for_limits_never_breaks_a_formula() {
        assert!(render(r"\sum\limits_{i=1}^n x", false, 30).is_ok());
        assert!(render(r"\sum\nolimits_{i=1}^n x", true, 30).is_ok());
    }

    #[test]
    fn a_formula_comes_out_as_real_coverage() {
        let formula = render(r"\frac{a}{b} + \sum_{i=1}^{n} x_i^2", false, 22).unwrap();
        assert!(formula.width > 22 && formula.height > 10);
        assert_eq!(
            formula.alpha.len(),
            (formula.width * formula.height) as usize
        );
        assert!(formula.alpha.iter().any(|a| *a > 200), "ink");
        assert!(formula.alpha.contains(&0), "and paper");
        assert!(formula.baseline > 0 && formula.baseline < formula.height);
    }

    #[test]
    fn display_math_stands_taller_than_inline() {
        let text = r"\sum_{i=1}^{n} i";
        let inline = render(text, false, 22).unwrap();
        let display = render(text, true, 22).unwrap();
        assert!(display.height > inline.height);
    }

    #[test]
    fn scientific_environments_typeset() {
        for source in [
            r"\begin{align} a &= b \\ c &= d \end{align}",
            r"\begin{equation} E = mc^2 \label{eq:e} \end{equation}",
            r"\begin{pmatrix} 1 & 0 \\ 0 & 1 \end{pmatrix}",
            r"f(x) = \begin{cases} 0 & x < 0 \\ 1 & x \ge 0 \end{cases}",
            r"\int_0^\infty e^{-x^2}\,dx = \frac{\sqrt{\pi}}{2}",
            r"\nabla \cdot \mathbf{E} = \frac{\rho}{\varepsilon_0}",
        ] {
            render(source, true, 22).unwrap_or_else(|e| panic!("{source}: {e}"));
        }
    }

    #[test]
    fn what_the_engine_lacks_has_a_stand_in() {
        for source in [
            r"\operatorname*{arg\,max}_x f(x)",
            r"\argmin_{\theta} \mathcal{L}(\theta)",
            r"\bm{x}^\top \mathds{1}",
            r"a \coloneqq b",
            r"\begin{dcases} a \\\\ b \end{dcases}",
            r"x \bmod y",
            r"\begin{align} a &= b \tag{2} \\\\ c &= d \notag \end{align}",
        ] {
            render(source, true, 22).unwrap_or_else(|e| panic!("{source}: {e}"));
        }
    }

    #[test]
    fn numbers_are_left_to_the_margin() {
        assert_eq!(prepared(r"E = mc^2 \tag{2}", true).trim(), "E = mc^2");
        assert!(prepared(r"\begin{align} a \end{align}", true).contains("{aligned}"));
    }

    #[test]
    fn nonsense_is_an_error_not_a_picture() {
        assert!(render(r"\frac{", false, 22).is_err());
        assert!(render("  ", false, 22).is_err());
    }

    #[test]
    fn svg_stands_alone() {
        assert!(svg("x^2", false).unwrap().starts_with("<svg"));
    }
}