texrs 0.6.8

A TeX engine: Knuth's mouth and expander, lowered for a bytecode VM
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
//! Where every glyph of a set formula lands, and how that reaches the page.
//!
//! `mlist_to_hlist` leaves a tree of boxes; a page wants coordinates. This is
//! `hlist_out` and `vlist_out` (`tex.web` §619-§640) reduced to the part a
//! formula needs: no leaders, no `\special`, no DVI -- a walk that turns the
//! tree into glyphs and rules positioned relative to the formula's own
//! baseline.
//!
//! The result is carried to the typesetter as text, because that is how
//! everything else the lowerer decides reaches it: the colour of a run, the
//! face it is set in and where a page breaks all travel the document's own
//! character stream as markers. A formula travels the same way, as the spec of
//! a marker whose visible text is the formula spelled out for a reader who
//! asked for `--text` rather than for a page.

use super::mlist::font_parts;
use super::noad::{Atom, Field, Noad, Scaled};
use crate::node::{BoxNode, Node, RuleNode};
use crate::pack::Setter;

/// One glyph of a set formula, positioned from the formula's own reference
/// point: `x` to the right, `y` UP from the baseline.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Glyph {
    pub ch: char,
    pub x: Scaled,
    pub y: Scaled,
    /// The size of the font it is set in, in scaled points -- text, script or
    /// scriptscript, which is the whole visible difference between `$x^2$` and
    /// `$x2$`.
    pub size: Scaled,
}

/// A rule: a fraction bar, an overline, or the bar of a radical. `y` is its
/// BOTTOM edge, up from the baseline.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Rule {
    pub x: Scaled,
    pub y: Scaled,
    pub width: Scaled,
    pub height: Scaled,
}

/// A formula, set: its dimensions and everything drawn inside it.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct Setting {
    pub width: Scaled,
    pub height: Scaled,
    pub depth: Scaled,
    pub glyphs: Vec<Glyph>,
    pub rules: Vec<Rule>,
}

/// Walk a set box into glyphs and rules.
pub fn flatten(b: &BoxNode, size_of_font: &dyn Fn(usize) -> Scaled) -> Setting {
    let mut out = Setting {
        width: b.width,
        height: b.height,
        depth: b.depth,
        ..Setting::default()
    };
    match b.vertical {
        true => vlist(b, 0, 0, &mut out, size_of_font),
        false => hlist(b, 0, 0, &mut out, size_of_font),
    }
    out
}

/// `hlist_out` (§619-§628): left to right along one baseline.
fn hlist(
    b: &BoxNode,
    left: Scaled,
    baseline: Scaled,
    out: &mut Setting,
    size_of_font: &dyn Fn(usize) -> Scaled,
) {
    let mut setter = Setter::new(b);
    let mut x = left;
    for node in &b.list {
        match node {
            Node::Char(c) | Node::Ligature(c, _) => {
                out.glyphs.push(Glyph {
                    ch: c.character,
                    x,
                    y: baseline,
                    size: size_of_font(c.font),
                });
                x += c.width;
            }
            Node::Box(inner) => {
                // §623: a box inside an hlist is displaced DOWNWARD by its
                // shift amount.
                let base = baseline - inner.shift_amount;
                match inner.vertical {
                    true => vlist(inner, x, base, out, size_of_font),
                    false => hlist(inner, x, base, out, size_of_font),
                }
                x += inner.width;
            }
            Node::Rule(r) => {
                // §624: a running height or depth is the enclosing box's.
                let (h, d) = running_vertical(r, b);
                let w = match RuleNode::is_running(r.width) {
                    true => 0,
                    false => r.width,
                };
                if h + d > 0 && w > 0 {
                    out.rules.push(Rule {
                        x,
                        y: baseline - d,
                        width: w,
                        height: h + d,
                    });
                }
                x += w;
            }
            Node::Glue(g) => x += setter.glue(&g.spec),
            Node::Kern { width, .. } | Node::Math(width) => x += width,
            _ => {}
        }
    }
}

/// `vlist_out` (§629-§637): down the page from the top of the box.
fn vlist(
    b: &BoxNode,
    left: Scaled,
    baseline: Scaled,
    out: &mut Setting,
    size_of_font: &dyn Fn(usize) -> Scaled,
) {
    let mut setter = Setter::new(b);
    // §630: the walk starts at the TOP of the box, which is its height above
    // the baseline it was placed on.
    let mut v = baseline + b.height;
    for node in &b.list {
        match node {
            Node::Box(inner) => {
                // §632: down by the box's height, set it there, then down by
                // its depth. A shift in a vlist moves it to the RIGHT.
                v -= inner.height;
                let x = left + inner.shift_amount;
                match inner.vertical {
                    true => vlist(inner, x, v, out, size_of_font),
                    false => hlist(inner, x, v, out, size_of_font),
                }
                v -= inner.depth;
            }
            Node::Rule(r) => {
                let h = match RuleNode::is_running(r.height) {
                    true => 0,
                    false => r.height,
                };
                let d = match RuleNode::is_running(r.depth) {
                    true => 0,
                    false => r.depth,
                };
                // §633: a running WIDTH is the enclosing box's, which is how a
                // fraction bar comes out exactly as wide as the fraction.
                let w = match RuleNode::is_running(r.width) {
                    true => b.width,
                    false => r.width,
                };
                v -= h + d;
                if h + d > 0 && w > 0 {
                    out.rules.push(Rule {
                        x: left,
                        y: v,
                        width: w,
                        height: h + d,
                    });
                }
            }
            Node::Glue(g) => v -= setter.glue(&g.spec),
            Node::Kern { width, .. } => v -= width,
            _ => {}
        }
    }
}

/// A rule's height and depth inside an hlist, with `null_flag` resolved
/// against the box holding it (§624).
fn running_vertical(r: &RuleNode, container: &BoxNode) -> (Scaled, Scaled) {
    let h = match RuleNode::is_running(r.height) {
        true => container.height,
        false => r.height,
    };
    let d = match RuleNode::is_running(r.depth) {
        true => container.depth,
        false => r.depth,
    };
    (h, d)
}

/// The size, in scaled points, of the font a `CharNode` names.
pub fn font_size(fonts: &super::font::MathFonts, index: usize) -> Scaled {
    let (fam, size) = font_parts(index);
    (fonts.at(fam, size) * 65536.0).round() as Scaled
}

/// The formula spelled out for a reader.
///
/// `--text` prints the document's words, and a formula is words: `$x^2+1$`
/// should read as `x^2+1` and not vanish, and not come out as the markup that
/// produced it either. Superscript and subscript digits have Unicode
/// spellings and are used where they exist; a fraction reads as a division and
/// a radical as a root, which is how anyone would read them aloud.
///
/// No character of this is a space, because the line breaker splits a
/// paragraph on spaces and a formula is one word.
pub fn plain(list: &[Noad]) -> String {
    let mut out = String::new();
    plain_into(&mut out, list);
    out
}

/// [`plain`], appended to `out`. The whole spelling is built in the one
/// string, rather than in a string per field and per script joined into it.
pub fn plain_into(out: &mut String, list: &[Noad]) {
    for item in list {
        match item {
            Noad::Atom(a) => {
                field_into(out, &a.nucleus);
                scripts_into(out, a);
            }
            Noad::Fraction(f) => {
                plain_into(out, &f.numerator);
                out.push('/');
                plain_into(out, &f.denominator);
            }
            Noad::Radical(r) => {
                // `\sqrt[3]{x}` reads as a cube root, which is where its index
                // goes when it is written rather than set: in front of the
                // sign and raised, the way `∛` is one character.
                if let Some(index) = &r.index {
                    script_into(out, true, |out| plain_into(out, index));
                }
                out.push('√');
                field_into(out, &r.nucleus.nucleus);
            }
            Noad::Over(a) | Noad::Under(a) | Noad::VCenter(a) => field_into(out, &a.nucleus),
            // An accent reads as the letter with the mark on it, which is what
            // a reader would say: `\hat x` is `x̂`, one letter and one
            // combining character.
            Noad::Accent(a) => {
                field_into(out, &a.atom.nucleus);
                if let Some(mark) = combining(a.accent) {
                    out.push(mark);
                }
                scripts_into(out, &a.atom);
            }
            // `\mathchoice`'s text list: a formula printed for a reader is not
            // in any style, and the text one is what `$...$` in a paragraph
            // would have chosen (§731).
            Noad::Choice(c) => plain_into(out, &c.text),
            Noad::Left(_) | Noad::Right(_) | Noad::Style(_) => {}
            Noad::Glue(_) | Noad::Kern(_) | Noad::MuGlue(_) | Noad::MuKern(_) => {}
            Noad::Node(_) => {}
        }
    }
}

/// The combining character that spells one of plain.tex's math accents
/// (plain.tex:939-950), for a reader who asked for `--text`.
///
/// The accent's own slot says nothing here: `cmr10`'s `"16` is a macron and
/// `cmex10`'s `"62` is a wide circumflex, and neither is the character a
/// reader wants beside the letter. Unicode's combining marks are.
fn combining(accent: super::noad::MathChar) -> Option<char> {
    match (accent.fam, accent.character) {
        (0, 0x12) => Some('\u{300}'),
        (0, 0x13) => Some('\u{301}'),
        (0, 0x14) => Some('\u{30C}'),
        (0, 0x15) => Some('\u{306}'),
        (0, 0x16) => Some('\u{304}'),
        (0, 0x5E) => Some('\u{302}'),
        (0, 0x5F) => Some('\u{307}'),
        (0, 0x7E) => Some('\u{303}'),
        (0, 0x7F) => Some('\u{308}'),
        (1, 0x7E) => Some('\u{20D7}'),
        (3, 0x62) => Some('\u{302}'),
        (3, 0x65) => Some('\u{303}'),
        _ => None,
    }
}

fn field_into(out: &mut String, f: &Field) {
    match f {
        Field::Empty | Field::Box(_) => {}
        Field::Char(c) => out.extend(super::font::unicode(c.fam, c.character)),
        Field::Literal(c) => out.push(*c),
        Field::List(l) => plain_into(out, l),
    }
}

/// An atom's superscript and then its subscript, each where it has one.
fn scripts_into(out: &mut String, a: &Atom) {
    if !a.supscr.is_empty() {
        script_into(out, true, |out| field_into(out, &a.supscr));
    }
    if !a.subscr.is_empty() {
        script_into(out, false, |out| field_into(out, &a.subscr));
    }
}

/// A script, which `spell` appends to `out`, in Unicode's own superscript or
/// subscript digits where they exist, and after a `^` or a `_` where they do
/// not.
///
/// The script is spelled in place and rewritten there: a run of digits by
/// their raised or lowered forms, anything else by the mark put in front.
fn script_into(out: &mut String, superscript: bool, spell: impl FnOnce(&mut String)) {
    const SUPER: [char; 10] = ['⁰', '¹', '²', '³', '⁴', '⁵', '⁶', '⁷', '⁸', '⁹'];
    const SUB: [char; 10] = ['₀', '₁', '₂', '₃', '₄', '₅', '₆', '₇', '₈', '₉'];
    let start = out.len();
    spell(out);
    let end = out.len();
    let digits = end > start && out.as_bytes()[start..].iter().all(u8::is_ascii_digit);
    if !digits {
        let mark = match superscript {
            true => '^',
            false => '_',
        };
        out.insert(start, mark);
        return;
    }
    let table = match superscript {
        true => &SUPER,
        false => &SUB,
    };
    for at in start..end {
        let digit = out.as_bytes()[at] - b'0';
        out.push(table[usize::from(digit)]);
    }
    out.replace_range(start..end, "");
}

/// The setting, as the characters a marker's spec carries.
///
/// Integers in scaled points throughout, so nothing is lost to a decimal
/// spelling and back: `g` is a glyph and `r` a rule, and the three numbers in
/// front are the formula's own width, height and depth.
pub fn encode(s: &Setting) -> String {
    // Room for what a typical piece spells, so the spec is rarely regrown.
    let mut out = String::with_capacity(24 + 32 * (s.glyphs.len() + s.rules.len()));
    push_numbers(&mut out, "", &[s.width, s.height, s.depth], ';');
    for g in &s.glyphs {
        let ch = Scaled::from(u32::from(g.ch));
        push_numbers(&mut out, ";g", &[g.x, g.y, g.size, ch], ',');
    }
    for r in &s.rules {
        push_numbers(&mut out, ";r", &[r.x, r.y, r.width, r.height], ',');
    }
    out
}

/// `lead`, then `numbers` in decimal with `between` between them: one piece
/// of an [`encode`]d spec.
fn push_numbers(out: &mut String, lead: &str, numbers: &[Scaled], between: char) {
    out.push_str(lead);
    for (at, n) in numbers.iter().enumerate() {
        if at > 0 {
            out.push(between);
        }
        push_decimal(out, *n);
    }
}

/// `n` in decimal, as `{}` spells it, without `core::fmt`'s machinery: a
/// formula-heavy document spells millions of these.
fn push_decimal(out: &mut String, n: Scaled) {
    if n < 0 {
        out.push('-');
    }
    let mut digits = [0u8; 20];
    let mut at = digits.len();
    let mut rest = n.unsigned_abs();
    loop {
        at -= 1;
        digits[at] = b'0' + (rest % 10) as u8;
        rest /= 10;
        if rest == 0 {
            break;
        }
    }
    out.extend(digits[at..].iter().map(|&d| char::from(d)));
}

/// The other half of [`encode`].
pub fn decode(text: &str) -> Option<Setting> {
    let mut parts = text.split(';');
    let mut out = Setting {
        width: parts.next()?.parse().ok()?,
        height: parts.next()?.parse().ok()?,
        depth: parts.next()?.parse().ok()?,
        ..Setting::default()
    };
    for part in parts {
        let (kind, rest) = part.split_at(part.char_indices().nth(1)?.0);
        let n: Vec<i64> = rest.split(',').filter_map(|v| v.parse().ok()).collect();
        if n.len() != 4 {
            return None;
        }
        match kind {
            "g" => out.glyphs.push(Glyph {
                x: n[0],
                y: n[1],
                size: n[2],
                ch: char::from_u32(n[3] as u32)?,
            }),
            "r" => out.rules.push(Rule {
                x: n[0],
                y: n[1],
                width: n[2],
                height: n[3],
            }),
            _ => return None,
        }
    }
    Some(out)
}

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

    #[test]
    fn a_setting_survives_the_round_trip_through_a_marker() {
        let s = Setting {
            width: 123_456,
            height: -7,
            depth: 65536,
            glyphs: vec![Glyph {
                ch: '∑',
                x: 10,
                y: -20,
                size: 655_360,
            }],
            rules: vec![Rule {
                x: 1,
                y: 2,
                width: 3,
                height: 4,
            }],
        };
        assert_eq!(decode(&encode(&s)).as_ref(), Some(&s));
    }

    /// The spec spells its numbers by hand; it must spell them as `{}` does,
    /// at the edges of the range and at every change in the digit count.
    #[test]
    fn a_number_is_spelled_as_display_spells_it() {
        let mut values = vec![
            0,
            1,
            -1,
            9,
            10,
            -10,
            65536,
            -65536,
            Scaled::MAX,
            Scaled::MIN,
        ];
        let mut power: Scaled = 1;
        while let Some(next) = power.checked_mul(10) {
            values.extend([power - 1, power, -power, -(power - 1)]);
            power = next;
        }
        for n in values {
            let mut spelled = String::new();
            push_decimal(&mut spelled, n);
            assert_eq!(spelled, n.to_string());
        }
    }

    /// The spec travels inside a marker, so it may hold no character the
    /// marker machinery reads and no space the line breaker would split on.
    #[test]
    fn an_encoded_setting_holds_no_control_character_and_no_space() {
        let s = Setting {
            width: -1,
            height: 0,
            depth: 0,
            glyphs: vec![Glyph {
                ch: 'x',
                x: -5,
                y: 0,
                size: 1,
            }],
            rules: Vec::new(),
        };
        let text = encode(&s);
        assert!(
            !text.chars().any(|c| c.is_control() || c == ' '),
            "the spec carries {text:?}"
        );
    }
}