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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
//! `mlist_to_hlist`: turning a formula into boxes.
//!
//! `tex.web` §719-§767, with the subroutines of §704-§717 it rests on. This is
//! the routine that decides everything a reader recognises as mathematics: how
//! far a superscript rises, how thick a fraction bar is and where it sits, how
//! tall a `\left(` grows, and how much room goes between a variable and the
//! operator beside it.
//!
//! It makes two passes (§725). The first turns every noad into an hlist and
//! records the tallest and deepest of them, because a `\left(` cannot be sized
//! until the whole formula's height is known. The second throws the noads away
//! and puts the inter-element spacing of §764 between what is left.
//!
//! The boxes are `crate::node`'s, and they are packaged by `crate::pack`'s
//! `hpack` and `vpack` -- §649 and §668 -- rather than by a second copy of
//! them written here.

use super::font::{CharDim, MathFont, MathFonts, TEXT_SIZE};
use super::noad::*;
use crate::glue::Glue;
use crate::node::{BoxNode, CharNode, GlueNode, GlueSource, Node, RuleNode};
use crate::pack::{hlist_height_depth, hpack, vpack, Spec, Tolerances, NATURAL};

/// `half(x)` (§100), which is not `x/2`: an odd number rounds AWAY from zero
/// upward, and half of every shift in this file is computed with it.
fn half(x: Scaled) -> Scaled {
    match x % 2 == 0 {
        true => x / 2,
        false => (x + 1) / 2,
    }
}

/// `x_over_n(x,n)` (§106): integer division truncating toward zero, which is
/// what `cur_mu` is computed with.
fn x_over_n(x: Scaled, n: i64) -> Scaled {
    match n {
        0 => 0,
        n => x / n,
    }
}

/// `xn_over_d(x,n,d)` (§107): $xn/d$, truncated toward zero.
///
/// §107 does it in two halves to stay inside a 32-bit word; `i128` here
/// computes the same quotient without the split.
fn xn_over_d(x: Scaled, n: i64, d: i64) -> Scaled {
    match d {
        0 => 0,
        d => ((x as i128 * n as i128) / d as i128) as i64,
    }
}

/// The font index a `CharNode` carries: family and size packed into one
/// number, the way `fam_fnt` packs them (§699).
pub fn font_index(fam: usize, size: usize) -> usize {
    fam * 3 + size
}

/// The family and size a font index names.
pub fn font_parts(index: usize) -> (usize, usize) {
    (index / 3, index % 3)
}

/// `\hss` — the glue `rebox` centres with (§715).
fn ss_glue() -> Glue {
    Glue {
        natural: 0,
        stretch: 65536,
        stretch_order: 1,
        shrink: 65536,
        shrink_order: 1,
    }
}

fn kern(width: Scaled) -> Node {
    Node::Kern {
        width,
        explicit: false,
    }
}

/// The state `mlist_to_hlist` keeps in globals (§719): the fonts, the style,
/// the size that style implies, and the math unit at that size.
pub struct Converter<'a> {
    pub fonts: &'a MathFonts,
    style: Style,
    size: usize,
    mu: Scaled,
    /// `mlist_penalties` (§719): whether §767's break penalties are inserted.
    ///
    /// §1194 sets it from the mode -- true for a formula inside a paragraph,
    /// false for a display (§1199) and false for every recursive call (§720).
    penalties: bool,
}

/// Which noad a `r_type` names during the first pass. `left_noad` and
/// `right_noad` are not spacing classes, and §728 tests for them by name.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
enum RType {
    Class(Class),
    Left,
    Right,
}

/// One entry of the list the first pass builds and the second pass reads.
struct Translated {
    /// The spacing class, once the Bin-to-Ord rewrites of §728 have settled.
    class: Option<RType>,
    /// `new_hlist(q)` (§725).
    hlist: Vec<Node>,
    /// A style node, which the second pass obeys and then deletes (§763).
    style: Option<Style>,
    /// The delimiter a `left_noad` or `right_noad` will be grown into once
    /// `max_h` and `max_d` are known (§762).
    delimiter: Option<Delimiter>,
    /// `pen` (§761): what §767 charges for breaking the formula after this
    /// noad. `inf_penalty` for everything but a Bin and a Rel.
    penalty: i64,
}

impl Translated {
    fn plain(node: Node) -> Translated {
        Translated {
            class: None,
            hlist: vec![node],
            style: None,
            delimiter: None,
            penalty: INF_PENALTY,
        }
    }
}

impl<'a> Converter<'a> {
    pub fn new(fonts: &'a MathFonts) -> Converter<'a> {
        let mut c = Converter {
            fonts,
            style: TEXT_STYLE,
            size: TEXT_SIZE,
            mu: 0,
            penalties: false,
        };
        c.set_style(TEXT_STYLE);
        c
    }

    /// `mlist_penalties := #` (§719). A recursive call always turns them off
    /// (§720), which is why this is set on the outermost conversion only.
    pub fn with_penalties(mut self, on: bool) -> Converter<'a> {
        self.penalties = on;
        self
    }

    /// "Set up the values of `cur_size` and `cur_mu`, based on `cur_style`"
    /// (§703).
    fn set_style(&mut self, style: Style) {
        self.style = style;
        self.size = size_of(style);
        self.mu = x_over_n(self.fonts.math_quad(self.size), 18);
    }

    // ── the parameters, at the current size ──────────────────────────────

    fn default_rule_thickness(&self) -> Scaled {
        self.fonts.default_rule_thickness(self.size)
    }
    fn axis_height(&self) -> Scaled {
        self.fonts.axis_height(self.size)
    }
    fn math_x_height(&self) -> Scaled {
        self.fonts.math_x_height(self.size)
    }

    /// `fetch(a)` (§722): the font and the metrics of a `math_char`.
    fn fetch(&self, c: MathChar, size: usize) -> Option<(&'a MathFont, CharDim)> {
        let f = self.fonts.font(c.fam, size)?;
        let m = f.metrics(c.character)?;
        Some((f, m))
    }

    /// A character node, measured in the font the family and size name.
    fn char_node(&self, c: MathChar, size: usize) -> Option<Node> {
        let (f, m) = self.fetch(c, size)?;
        // In the stomach's lists a character is its code in its font; on the
        // PDF path it is the Unicode character the slot draws.
        let (font, character) = match f.node_font {
            Some(id) => (id, char::from(c.character)),
            None => (
                font_index(c.fam, size),
                super::font::unicode(c.fam, c.character)?,
            ),
        };
        Some(Node::Char(CharNode {
            font,
            character,
            width: m.width,
            height: m.height,
            depth: m.depth,
        }))
    }

    /// `char_box(f,c)` (§709): a box holding one character, whose width
    /// INCLUDES the italic correction.
    fn char_box(&self, fam: usize, size: usize, code: u8) -> BoxNode {
        let c = MathChar {
            fam,
            character: code,
        };
        let Some((_, m)) = self.fetch(c, size) else {
            return BoxNode::null();
        };
        let list = match self.char_node(c, size) {
            Some(n) => vec![n],
            None => Vec::new(),
        };
        BoxNode {
            width: m.width + m.italic,
            height: m.height,
            depth: m.depth,
            list,
            ..BoxNode::null()
        }
    }

    /// `fraction_rule(t)` (§704): a rule `t` thick, running the width of
    /// whatever holds it.
    fn fraction_rule(&self, t: Scaled) -> Node {
        Node::Rule(RuleNode {
            width: crate::node::NULL_FLAG,
            height: t,
            depth: 0,
        })
    }

    /// `overbar(b,k,t)` (§705): `b`, a kern of `k`, a rule of `t`, and `t`
    /// more of space above it.
    fn overbar(&self, b: BoxNode, k: Scaled, t: Scaled) -> BoxNode {
        let list = vec![kern(t), self.fraction_rule(t), kern(k), Node::Box(b)];
        vpack(list, NATURAL, Tolerances::plain()).node
    }

    /// `rebox(b,w)` (§715): the same box, centred in a box `w` wide.
    fn rebox(&self, mut b: BoxNode, w: Scaled) -> BoxNode {
        if b.width == w || b.list.is_empty() {
            b.width = w;
            return b;
        }
        if b.vertical {
            b = hpack(vec![Node::Box(b)], NATURAL, Tolerances::plain(), None).node;
        }
        let mut list = std::mem::take(&mut b.list);
        // §715: a one-character box carries that character's italic
        // correction in its width, and centring it would centre the
        // correction too. A compensating kern puts it back where it belongs.
        if list.len() == 1 {
            if let Node::Char(c) = list[0] {
                if c.width != b.width {
                    list.push(kern(b.width - c.width));
                }
            }
        }
        let mut whole = vec![Node::Glue(GlueNode::new(ss_glue()))];
        whole.append(&mut list);
        whole.push(Node::Glue(GlueNode::new(ss_glue())));
        hpack(whole, Spec::Exactly(w), Tolerances::plain(), None).node
    }

    /// `clean_box(p,s)` (§720): a noad field, boxed in a given style, with a
    /// `shift_amount` of zero.
    fn clean_box(&mut self, field: &Field, style: Style) -> BoxNode {
        match field {
            // §720's `sub_box` case: the box goes to `found` as it stands,
            // shift and all. It is NOT simply handed back -- a box with a
            // shift is repackaged below, which is what turns `\sum`'s
            // centring on the axis into height and depth the limits above it
            // can be measured against.
            Field::Box(b) => Self::found(vec![Node::Box(b.as_ref().clone())]),
            Field::Char(c) => {
                self.clean_list(&[Noad::Atom(Atom::new(Class::Ord, Field::Char(*c)))], style)
            }
            Field::List(l) => self.clean_list(l, style),
            Field::Literal(c) => Self::found(vec![self.literal_node(*c, size_of(style))]),
            Field::Empty => Self::found(Vec::new()),
        }
    }

    /// [`Self::clean_box`] of a field holding the mlist `list`, for a caller
    /// that has the list and not such a field: a fraction's numerator and
    /// denominator, a root's index.
    fn clean_list(&mut self, list: &[Noad], style: Style) -> BoxNode {
        // §720: `mlist_penalties := false` for the whole of a recursive
        // conversion. A formula cannot break inside a subscript, a numerator
        // or anything else that is about to be packed into one box.
        let penalties = std::mem::replace(&mut self.penalties, false);
        let save = self.style;
        let q = self.convert(list, style);
        self.set_style(save);
        self.penalties = penalties;
        Self::found(q)
    }

    /// §720's `found`: a list that is already ONE unshifted box is clean as
    /// it stands, and anything else is packaged.
    fn found(q: Vec<Node>) -> BoxNode {
        let mut x = match <[Node; 1]>::try_from(q) {
            Ok([Node::Box(b)]) if b.shift_amount == 0 => b,
            Ok(one) => hpack(one.into(), NATURAL, Tolerances::plain(), None).node,
            Err(q) => hpack(q, NATURAL, Tolerances::plain(), None).node,
        };
        // §721: a character followed by nothing but its italic correction does
        // not need the correction, because the box is about to be measured
        // rather than set beside anything.
        if x.list.len() == 2 && x.list[0].is_char() && matches!(x.list[1], Node::Kern { .. }) {
            x.list.truncate(1);
        }
        x
    }

    /// A character with no Computer Modern slot, measured on the text
    /// family's quad. See `Field::Literal`.
    fn literal_node(&self, c: char, size: usize) -> Node {
        let quad = self.fonts.math_quad(size);
        Node::Char(CharNode {
            font: font_index(0, size),
            character: c,
            width: half(quad),
            height: self.fonts.math_x_height(size),
            depth: 0,
        })
    }

    // ── var_delimiter, §706-§714 ─────────────────────────────────────────

    /// `var_delimiter(d,s,v)` (§706): the smallest variant of `d` whose height
    /// plus depth is at least `v`, centred on the axis of size `s`.
    fn var_delimiter(&self, d: Delimiter, s: usize, v: Scaled) -> BoxNode {
        let mut found: Option<(usize, usize, u8)> = None;
        let mut w: Scaled = 0;
        let mut done = false;
        // §706: the small variant first, then the large one.
        for (fam, chr) in [(d.small_fam, d.small_char), (d.large_fam, d.large_char)] {
            if done {
                break;
            }
            if fam == 0 && chr == 0 {
                continue;
            }
            // §707: the sizes are tried from `s` downward -- the current size
            // first, then the larger fonts of the same family.
            for size in (0..=s.min(2)).rev() {
                if done {
                    break;
                }
                let Some(g) = self.fonts.font(fam, size) else {
                    continue;
                };
                // §708: walk the chain of ever-larger variants.
                let mut y = chr;
                for _ in 0..=256 {
                    let Some(m) = g.metrics(y) else { break };
                    if g.recipe(y).is_some() {
                        found = Some((fam, size, y));
                        done = true;
                        break;
                    }
                    let u = m.height + m.depth;
                    if u > w {
                        found = Some((fam, size, y));
                        w = u;
                        if u >= v {
                            done = true;
                            break;
                        }
                    }
                    match g.next_larger(y) {
                        Some(next) => y = next,
                        None => break,
                    }
                }
            }
        }
        let mut b = match found {
            Some((fam, size, code)) => {
                match self.fonts.font(fam, size).and_then(|f| f.recipe(code)) {
                    Some(r) => self.extensible(fam, size, r, v),
                    None => self.char_box(fam, size, code),
                }
            }
            // §706: no delimiter at all is a box of `\nulldelimiterspace`.
            None => BoxNode {
                width: self.fonts.params.null_delimiter_space,
                ..BoxNode::null()
            },
        };
        b.shift_amount = half(b.height - b.depth) - self.fonts.axis_height(s);
        b
    }

    /// "Construct an extensible character in a new box `b`" (§713-§714).
    ///
    /// The recipe is four character codes -- top, mid, bot, rep -- and the
    /// delimiter is built by stacking as few copies of the repeatable piece as
    /// will reach `v`, in pairs when there is a middle piece.
    fn extensible(&self, fam: usize, size: usize, r: [u8; 4], v: Scaled) -> BoxNode {
        let (top, mid, bot, rep) = (r[0], r[1], r[2], r[3]);
        let Some(f) = self.fonts.font(fam, size) else {
            return BoxNode::null();
        };
        // §714: the width is the repeatable module's, italic correction
        // included, and the height starts as the fixed pieces' total.
        let u = f.height_plus_depth(rep);
        let width = f.metrics(rep).map(|m| m.width + m.italic).unwrap_or(0);
        let mut w: Scaled = 0;
        for c in [bot, mid, top] {
            if c != 0 {
                w += f.height_plus_depth(c);
            }
        }
        let mut n = 0;
        if u > 0 {
            while w < v {
                w += u;
                n += 1;
                if mid != 0 {
                    w += u;
                }
            }
        }
        // §713: stacked from the bottom up, so the list is built bottom-first
        // and reversed at the end -- `stack_into_box` puts each new piece on
        // TOP of what is already there.
        let mut stacked: Vec<BoxNode> = Vec::new();
        if bot != 0 {
            stacked.push(self.char_box(fam, size, bot));
        }
        for _ in 0..n {
            stacked.push(self.char_box(fam, size, rep));
        }
        if mid != 0 {
            stacked.push(self.char_box(fam, size, mid));
            for _ in 0..n {
                stacked.push(self.char_box(fam, size, rep));
            }
        }
        if top != 0 {
            stacked.push(self.char_box(fam, size, top));
        }
        // The topmost piece is the last one stacked, and §711 makes the box's
        // height that piece's height alone.
        let height = stacked.last().map(|b| b.height).unwrap_or(0);
        stacked.reverse();
        let list: Vec<Node> = stacked.into_iter().map(Node::Box).collect();
        BoxNode {
            vertical: true,
            width,
            height,
            depth: w - height,
            list,
            ..BoxNode::null()
        }
    }

    // ── the make_* procedures, §734-§759 ─────────────────────────────────

    /// `make_over(q)` (§734).
    fn make_over(&mut self, a: &mut Atom) {
        let t = self.default_rule_thickness();
        let b = self.clean_box(&a.nucleus, cramped_style(self.style));
        a.nucleus = Field::Box(Box::new(self.overbar(b, 3 * t, t)));
    }

    /// `make_under(q)` (§735).
    fn make_under(&mut self, a: &mut Atom) {
        let t = self.default_rule_thickness();
        let x = self.clean_box(&a.nucleus, self.style);
        let x_height = x.height;
        let list = vec![Node::Box(x), kern(3 * t), self.fraction_rule(t)];
        let mut y = vpack(list, NATURAL, Tolerances::plain()).node;
        let delta = y.height + y.depth + t;
        y.height = x_height;
        y.depth = delta - y.height;
        a.nucleus = Field::Box(Box::new(y));
    }

    /// `make_radical(q)` (§737).
    fn make_radical(&mut self, r: &mut Radical) {
        let t = self.default_rule_thickness();
        let x = self.clean_box(&r.nucleus.nucleus, cramped_style(self.style));
        // §737: the clearance is larger in display style.
        let mut clr = match self.style < TEXT_STYLE {
            true => t + self.math_x_height().abs() / 4,
            false => t + t.abs() / 4,
        };
        let mut y = self.var_delimiter(r.left_delimiter, self.size, x.height + x.depth + clr + t);
        let delta = y.depth - (x.height + x.depth + clr);
        if delta > 0 {
            clr += half(delta);
        }
        y.shift_amount = -(x.height + clr);
        let y_height = y.height;
        let bar = self.overbar(x, clr, y_height);
        let list = vec![Node::Box(y), Node::Box(bar)];
        r.nucleus.nucleus = Field::Box(Box::new(
            hpack(list, NATURAL, Tolerances::plain(), None).node,
        ));
    }

    /// `\root n \of {x}`, which is what `\sqrt[n]{x}` means
    /// (plain.tex:1018-1022).
    ///
    /// There is no noad for it: plain.tex sets the index in an hbox of its own
    /// at `\scriptscriptstyle`, sets the radical in a second box, and then
    /// puts the two beside each other with three explicit amounts --
    ///
    /// ```text
    /// \def\root#1\of{\setbox\rootbox
    ///   \hbox{$\m@th\scriptscriptstyle{#1}$}\mathpalette\r@@t}
    /// \def\r@@t#1#2{\setbox\z@\hbox{$\m@th#1\sqrt{#2}$}\dimen@\ht\z@
    ///   \advance\dimen@-\dp\z@
    ///   \mkern5mu\raise.6\dimen@\copy\rootbox \mkern-10mu\box\z@}
    /// ```
    ///
    /// -- five `mu` in front of the index, six tenths of the radical's height
    /// less its depth to raise it by, and ten `mu` back so the index sits
    /// INSIDE the radical's opening. `\mathpalette` is what makes the radical
    /// itself come out in the surrounding style (plain.tex:1015-1016), which
    /// is the style this is already being converted in.
    fn make_root(&mut self, r: &mut Radical, index: &[Noad]) {
        let z = match &r.nucleus.nucleus {
            Field::Box(b) => b.as_ref().clone(),
            _ => return,
        };
        let mut root = self.clean_list(index, SCRIPT_SCRIPT_STYLE);
        // `\dimen@\ht\z@ \advance\dimen@-\dp\z@`, then `\raise.6\dimen@`.
        // §453 reads `.6` as a coefficient in $2^{16}$ths -- `round_decimals`
        // of one digit 6 is 39322 -- and multiplies with `xn_over_d` (§455).
        // A raise is a NEGATIVE shift, because §623 displaces a box in an
        // hlist downward by its shift amount.
        let dimen = z.height - z.depth;
        root.shift_amount = -xn_over_d(dimen, 39322, 65536);
        let list = vec![
            kern(self.mu_mult(5 * 65536)),
            Node::Box(root),
            kern(self.mu_mult(-10 * 65536)),
            Node::Box(z),
        ];
        r.nucleus.nucleus = Field::Box(Box::new(
            hpack(list, NATURAL, Tolerances::plain(), None).node,
        ));
    }

    /// `make_vcenter(q)` (§736): the box, centred on the axis.
    ///
    /// §736 insists the nucleus is a vlist -- `\vcenter` is `\vbox`'s sibling
    /// and §1167 leaves one behind -- so a nucleus that is still an mlist is
    /// packaged into one first, which is what `\vcenter{...}` written with a
    /// formula inside it asks for.
    fn make_vcenter(&mut self, a: &mut Atom) {
        let mut v = match &a.nucleus {
            Field::Box(b) if b.vertical => b.as_ref().clone(),
            other => {
                let inner = self.clean_box(other, self.style);
                vpack(vec![Node::Box(inner)], NATURAL, Tolerances::plain()).node
            }
        };
        let delta = v.height + v.depth;
        v.height = self.axis_height() + half(delta);
        v.depth = delta - v.height;
        a.nucleus = Field::Box(Box::new(v));
    }

    /// `make_math_accent(q)` (§738-§742).
    ///
    /// §738: "Slants are not considered when placing accents in math mode. The
    /// accenter is centered over the accentee, and the accent width is treated
    /// as zero with respect to the size of the final box."
    fn make_math_accent(&mut self, acc: &mut Accent) {
        let Some((f, _)) = self.fetch(acc.accent, self.size) else {
            // §739 does nothing at all when the accent character is not in the
            // font: `if char_exists(cur_i)` guards the whole procedure.
            return;
        };
        let mut c = acc.accent.character;
        // §742: how far to skew the accent to the right -- the kern between
        // the accented character and its font's `\skewchar`.
        let s = self.skew(&acc.atom);
        let mut x = self.clean_box(&acc.atom.nucleus, cramped_style(self.style));
        let w = x.width;
        let mut h = x.height;
        // §741: walk the charlist for the widest accent that still fits over
        // the nucleus. The chain stops at the first variant WIDER than the
        // accentee, so the accent never overhangs what it accents.
        while let Some(y) = f.next_larger(c) {
            let Some(m) = f.metrics(y) else { break };
            if m.width > w {
                break;
            }
            c = y;
        }
        // §739: the accent is lowered onto a nucleus shorter than the
        // x-height, and no further than the x-height for a taller one -- which
        // is what puts `\hat x` and `\hat A` at the same height above their
        // letters rather than at the same height above the baseline.
        let x_height = f.param(5);
        let mut delta = h.min(x_height);
        // §743: a script on an accented noad belongs to the WHOLE accented
        // thing, so the nucleus and its two scripts are boxed together first
        // and the accent then goes over that box.
        if (!acc.atom.supscr.is_empty() || !acc.atom.subscr.is_empty())
            && matches!(acc.atom.nucleus, Field::Char(_))
        {
            let inner = Noad::Atom(Atom {
                class: ClassOrOrd(Class::Ord),
                nucleus: std::mem::take(&mut acc.atom.nucleus),
                supscr: std::mem::take(&mut acc.atom.supscr),
                subscr: std::mem::take(&mut acc.atom.subscr),
                limits: acc.atom.limits,
            });
            acc.atom.nucleus = Field::List(vec![inner]);
            x = self.clean_box(&acc.atom.nucleus, self.style);
            delta += x.height - h;
            h = x.height;
        }
        // §739: the accent box, centred over the accentee and skewed, with a
        // width of zero so that it costs the final box nothing.
        let mut y = self.char_box(acc.accent.fam, self.size, c);
        y.shift_amount = s + half(w - y.width);
        y.width = 0;
        let x_width = x.width;
        let list = vec![Node::Box(y), kern(-delta), Node::Box(x)];
        let mut y = vpack(list, NATURAL, Tolerances::plain()).node;
        y.width = x_width;
        // §740: an accent that ends up shorter than what it accents does not
        // shrink the nucleus; the box keeps the nucleus's own height.
        if y.height < h {
            let gap = h - y.height;
            y.list.insert(0, kern(gap));
            y.height = h;
        }
        acc.atom.nucleus = Field::Box(Box::new(y));
    }

    /// "Compute the amount of skew" (§742).
    ///
    /// The lig/kern program of the accented character is searched for a step
    /// whose next character is the font's `\skewchar`; its kern is how far
    /// right the accent moves. `cmmi10`'s skew character is `'177`
    /// (plain.tex:474), which is why `\hat f` sits further right than `\hat x`.
    fn skew(&self, a: &Atom) -> Scaled {
        let Field::Char(c) = a.nucleus else {
            return 0;
        };
        let Some(f) = self.fonts.font(c.fam, self.size) else {
            return 0;
        };
        let Ok(skew_char) = u8::try_from(f.skew_char) else {
            return 0;
        };
        match f.tfm.step(c.character, skew_char) {
            Some(crate::tfm::Step::Kern { by, .. }) => f.sp(by),
            _ => 0,
        }
    }

    /// `make_fraction(q)` (§743-§748). Unlike the others it produces the
    /// noad's whole hlist rather than a nucleus.
    fn make_fraction(&mut self, f: &Fraction) -> Vec<Node> {
        let thickness = f.thickness.unwrap_or_else(|| self.default_rule_thickness());
        // §744: equal-width numerator and denominator, and the default shifts.
        let mut x = self.clean_list(&f.numerator, num_style(self.style));
        let mut z = self.clean_list(&f.denominator, denom_style(self.style));
        match x.width < z.width {
            true => x = self.rebox(x, z.width),
            false => z = self.rebox(z, x.width),
        }
        let (mut shift_up, mut shift_down) = match self.style < TEXT_STYLE {
            true => (self.fonts.num1(self.size), self.fonts.denom1(self.size)),
            false => (
                match thickness != 0 {
                    true => self.fonts.num2(self.size),
                    false => self.fonts.num3(self.size),
                },
                self.fonts.denom2(self.size),
            ),
        };
        let mut delta = 0;
        if thickness == 0 {
            // §745: no rule, so the clearance is stated outright.
            let clr = match self.style < TEXT_STYLE {
                true => 7 * self.default_rule_thickness(),
                false => 3 * self.default_rule_thickness(),
            };
            let d = half(clr - ((shift_up - x.depth) - (z.height - shift_down)));
            if d > 0 {
                shift_up += d;
                shift_down += d;
            }
        } else {
            // §746: with a rule, the clearance depends on its thickness and
            // both halves are measured from the axis.
            let clr = match self.style < TEXT_STYLE {
                true => 3 * thickness,
                false => thickness,
            };
            delta = half(thickness);
            let delta1 = clr - ((shift_up - x.depth) - (self.axis_height() + delta));
            let delta2 = clr - ((self.axis_height() - delta) - (z.height - shift_down));
            if delta1 > 0 {
                shift_up += delta1;
            }
            if delta2 > 0 {
                shift_down += delta2;
            }
        }
        // §747: the vlist, built top down.
        let height = shift_up + x.height;
        let depth = z.depth + shift_down;
        let width = x.width;
        let x_depth = x.depth;
        let mut list: Vec<Node> = vec![Node::Box(x)];
        match thickness == 0 {
            true => list.push(kern((shift_up - x_depth) - (z.height - shift_down))),
            false => {
                list.push(kern((shift_up - x_depth) - (self.axis_height() + delta)));
                list.push(self.fraction_rule(thickness));
                list.push(kern((self.axis_height() - delta) - (z.height - shift_down)));
            }
        }
        list.push(Node::Box(z));
        let v = BoxNode {
            vertical: true,
            width,
            height,
            depth,
            list,
            ..BoxNode::null()
        };
        // §748: the delimiters either side, sized by `delim1` or `delim2`.
        let size = match self.style < TEXT_STYLE {
            true => self.fonts.delim1(self.size),
            false => self.fonts.delim2(self.size),
        };
        let left = self.var_delimiter(f.left_delimiter, self.size, size);
        let right = self.var_delimiter(f.right_delimiter, self.size, size);
        let whole = vec![Node::Box(left), Node::Box(v), Node::Box(right)];
        vec![Node::Box(
            hpack(whole, NATURAL, Tolerances::plain(), None).node,
        )]
    }

    /// `make_op(q)` (§749-§751). Returns the italic correction, which §754
    /// uses as the offset between a subscript and a superscript, and fills in
    /// the noad's hlist itself when the limits go above and below.
    fn make_op(&mut self, a: &mut Atom) -> (Scaled, Option<Vec<Node>>) {
        if a.limits == Limits::Normal && self.style < TEXT_STYLE {
            a.limits = Limits::Above;
        }
        let mut delta = 0;
        if let Field::Char(c) = a.nucleus {
            let mut c = c;
            // §749: in display style an operator is replaced by the next
            // larger variant, which is how `\sum` grows.
            if self.style < TEXT_STYLE {
                if let Some(f) = self.fonts.font(c.fam, self.size) {
                    if let Some(larger) = f.next_larger(c.character) {
                        if f.metrics(larger).is_some() {
                            c.character = larger;
                        }
                    }
                }
            }
            if let Some((_, m)) = self.fetch(c, self.size) {
                delta = m.italic;
            }
            a.nucleus = Field::Char(c);
            let mut x = self.clean_box(&Field::Char(c), self.style);
            if !a.subscr.is_empty() && a.limits != Limits::Above {
                x.width -= delta;
            }
            x.shift_amount = half(x.height - x.depth) - self.axis_height();
            a.nucleus = Field::Box(Box::new(x));
        }
        if a.limits != Limits::Above {
            return (delta, None);
        }
        // §750: a vlist of the superscript, the operator and the subscript,
        // each reboxed to the widest of the three.
        let x = self.clean_box(&a.supscr, sup_style(self.style));
        let y = self.clean_box(&a.nucleus, self.style);
        let z = self.clean_box(&a.subscr, sub_style(self.style));
        let mut width = y.width;
        width = width.max(x.width).max(z.width);
        let mut x = self.rebox(x, width);
        let y = self.rebox(y, width);
        let mut z = self.rebox(z, width);
        x.shift_amount = half(delta);
        z.shift_amount = -x.shift_amount;
        let mut height = y.height;
        let mut depth = y.depth;
        let mut list: Vec<Node> = Vec::new();
        // §751: the kerns above and below, and what they add to the box.
        if a.supscr.is_empty() {
            list.push(Node::Box(y));
        } else {
            let mut shift_up = self.fonts.big_op_spacing3(self.size) - x.depth;
            let floor = self.fonts.big_op_spacing1(self.size);
            if shift_up < floor {
                shift_up = floor;
            }
            height += self.fonts.big_op_spacing5(self.size) + x.height + x.depth + shift_up;
            list.push(kern(self.fonts.big_op_spacing5(self.size)));
            list.push(Node::Box(x));
            list.push(kern(shift_up));
            list.push(Node::Box(y));
        }
        if !a.subscr.is_empty() {
            let mut shift_down = self.fonts.big_op_spacing4(self.size) - z.height;
            let floor = self.fonts.big_op_spacing2(self.size);
            if shift_down < floor {
                shift_down = floor;
            }
            depth += self.fonts.big_op_spacing5(self.size) + z.height + z.depth + shift_down;
            list.push(kern(shift_down));
            list.push(Node::Box(z));
            list.push(kern(self.fonts.big_op_spacing5(self.size)));
        }
        let v = BoxNode {
            vertical: true,
            width,
            height,
            depth,
            list,
            ..BoxNode::null()
        };
        (delta, Some(vec![Node::Box(v)]))
    }

    /// `math_type(nucleus(q)) := math_text_char` (§752): whether this noad's
    /// character is followed immediately by another from the same family, so
    /// that the two are a word rather than two symbols.
    fn text_char(&self, a: &Atom, next: Option<&Noad>) -> bool {
        if !a.subscr.is_empty() || !a.supscr.is_empty() || a.class() != Class::Ord {
            return false;
        }
        let (Field::Char(q), Some(Noad::Atom(p))) = (&a.nucleus, next) else {
            return false;
        };
        if !matches!(
            p.class(),
            Class::Ord
                | Class::Op
                | Class::Bin
                | Class::Rel
                | Class::Open
                | Class::Close
                | Class::Punct
        ) {
            return false;
        }
        matches!(p.nucleus, Field::Char(r) if r.fam == q.fam)
    }

    /// `make_ord(q)` (§752-§753), kerns only.
    ///
    /// The ligature half of §752 has nothing to do in a math font -- Computer
    /// Modern's math families define no ligature programs -- but the KERNS
    /// do: `cmmi10` kerns its italic letters against the punctuation and the
    /// scripts beside them, and dropping those would set `$f(x)$` with the
    /// `f` running into the parenthesis.
    fn make_ord_kern(&self, a: &Atom, next: Option<&Noad>) -> Option<Scaled> {
        if !a.subscr.is_empty() || !a.supscr.is_empty() {
            return None;
        }
        let Field::Char(q) = a.nucleus else {
            return None;
        };
        let Some(Noad::Atom(p)) = next else {
            return None;
        };
        if !matches!(
            p.class(),
            Class::Ord
                | Class::Op
                | Class::Bin
                | Class::Rel
                | Class::Open
                | Class::Close
                | Class::Punct
        ) {
            return None;
        }
        let Field::Char(r) = p.nucleus else {
            return None;
        };
        if r.fam != q.fam {
            return None;
        }
        let f = self.fonts.font(q.fam, self.size)?;
        match f.tfm.step(q.character, r.character) {
            Some(crate::tfm::Step::Kern { by, .. }) => Some(f.sp(by)),
            _ => None,
        }
    }

    /// `make_scripts(q,delta)` (§756-§759): the subscript and superscript,
    /// shifted onto the list the nucleus produced.
    fn make_scripts(&mut self, a: &Atom, hlist: &mut Vec<Node>, delta: Scaled) {
        let mut shift_up: Scaled = 0;
        let mut shift_down: Scaled = 0;
        // §756: a bare character sits on the baseline, so the scripts are
        // shifted from there; anything else is measured. The test is
        // `is_char_node(p)` on the FIRST node -- a character followed by its
        // italic correction is still a character node -- and an empty hlist is
        // measured, which is what gives `${}^2$` its shift.
        if !hlist.first().map(|n| n.is_char()).unwrap_or(false) {
            let (height, depth) = hlist_height_depth(hlist);
            let t = match self.style < SCRIPT_STYLE {
                true => 1,
                false => 2,
            };
            shift_up = height - self.fonts.sup_drop(t);
            shift_down = depth + self.fonts.sub_drop(t);
        }
        let x = if a.supscr.is_empty() {
            // §757: a subscript on its own must not rise above four fifths of
            // the x-height.
            let mut x = self.clean_box(&a.subscr, sub_style(self.style));
            x.width += self.fonts.params.script_space;
            let floor = self.fonts.sub1(self.size);
            if shift_down < floor {
                shift_down = floor;
            }
            let clr = x.height - (self.math_x_height().abs() * 4) / 5;
            if shift_down < clr {
                shift_down = clr;
            }
            x.shift_amount = shift_down;
            x
        } else {
            // §758: a superscript must not descend below a quarter of the
            // x-height.
            let mut x = self.clean_box(&a.supscr, sup_style(self.style));
            x.width += self.fonts.params.script_space;
            let mut clr = match self.style % 2 == 1 {
                true => self.fonts.sup3(self.size),
                false => match self.style < TEXT_STYLE {
                    true => self.fonts.sup1(self.size),
                    false => self.fonts.sup2(self.size),
                },
            };
            if shift_up < clr {
                shift_up = clr;
            }
            clr = x.depth + self.math_x_height().abs() / 4;
            if shift_up < clr {
                shift_up = clr;
            }
            if a.subscr.is_empty() {
                x.shift_amount = -shift_up;
                x
            } else {
                // §759: both present. They must be four rule thicknesses
                // apart, and the superscript sits `delta` to the right.
                let mut y = self.clean_box(&a.subscr, sub_style(self.style));
                y.width += self.fonts.params.script_space;
                let floor = self.fonts.sub2(self.size);
                if shift_down < floor {
                    shift_down = floor;
                }
                let mut clr = 4 * self.default_rule_thickness()
                    - ((shift_up - x.depth) - (y.height - shift_down));
                if clr > 0 {
                    shift_down += clr;
                    clr = (self.math_x_height().abs() * 4) / 5 - (shift_up - x.depth);
                    if clr > 0 {
                        shift_up += clr;
                        shift_down -= clr;
                    }
                }
                x.shift_amount = delta;
                let gap = (shift_up - x.depth) - (y.height - shift_down);
                let list = vec![Node::Box(x), kern(gap), Node::Box(y)];
                let mut v = vpack(list, NATURAL, Tolerances::plain()).node;
                v.shift_amount = shift_down;
                v
            }
        };
        hlist.push(Node::Box(x));
    }

    /// `make_left_right(q,style,max_d,max_h)` (§762): how tall a `\left` or
    /// `\right` delimiter has to be, given the formula it encloses.
    fn make_left_right(
        &mut self,
        d: Delimiter,
        style: Style,
        max_d: Scaled,
        max_h: Scaled,
    ) -> Vec<Node> {
        let size = size_of(style);
        let delta2 = max_d + self.fonts.axis_height(size);
        let mut delta1 = max_h + max_d - delta2;
        if delta2 > delta1 {
            delta1 = delta2;
        }
        let mut delta = (delta1 / 500) * self.fonts.params.delimiter_factor;
        let delta2 = delta1 + delta1 - self.fonts.params.delimiter_shortfall;
        if delta < delta2 {
            delta = delta2;
        }
        vec![Node::Box(self.var_delimiter(d, size, delta))]
    }

    // ── mlist_to_hlist itself, §726-§767 ─────────────────────────────────

    /// `mlist_to_hlist` (§726): the whole conversion, in the given style.
    pub fn convert(&mut self, mlist: &[Noad], style: Style) -> Vec<Node> {
        self.set_style(style);
        // §731 REWRITES the list it is walking: a `choice_node` becomes a
        // style node followed by whichever of its four mlists the current
        // style names, and the rest of the list follows that. So the pass runs
        // over the caller's slice until it meets one, and over a working copy
        // that can be spliced from then on: a formula without `\mathchoice` is
        // never copied.
        let mut work: std::borrow::Cow<[Noad]> = std::borrow::Cow::Borrowed(mlist);
        let mut items: Vec<Translated> = Vec::with_capacity(work.len());
        let mut r_index: Option<usize> = None;
        let mut r_type = RType::Class(Class::Op);
        let mut max_h: Scaled = 0;
        let mut max_d: Scaled = 0;

        let mut at = 0;
        while at < work.len() {
            // §730-§732: the nodes that can appear among noads.
            match &work[at] {
                Noad::Style(s) => {
                    let s = *s;
                    self.set_style(s);
                    items.push(Translated {
                        class: None,
                        hlist: Vec::new(),
                        style: Some(s),
                        delimiter: None,
                        penalty: INF_PENALTY,
                    });
                    at += 1;
                    continue;
                }
                // §731: "Change this node to a style node followed by the
                // correct choice", indexed by `cur_style div 2`.
                Noad::Choice(c) => {
                    let chosen = match self.style / 2 {
                        0 => c.display.clone(),
                        1 => c.text.clone(),
                        2 => c.script.clone(),
                        _ => c.script_script.clone(),
                    };
                    let mut replacement = vec![Noad::Style(self.style)];
                    replacement.extend(chosen);
                    work.to_mut().splice(at..=at, replacement);
                    continue;
                }
                Noad::Glue(w) => {
                    let w = *w;
                    items.push(Translated::plain(Node::Glue(GlueNode::new(Glue::fixed(w)))));
                    at += 1;
                    continue;
                }
                // §732: `\mskip` is written in `mu` and becomes ordinary glue
                // here, where the size it lands in is finally known.
                Noad::MuGlue(g) => {
                    let glue = self.math_glue(g);
                    items.push(Translated::plain(Node::Glue(GlueNode::new(glue))));
                    at += 1;
                    continue;
                }
                Noad::Kern(w) => {
                    let w = *w;
                    items.push(Translated::plain(kern(w)));
                    at += 1;
                    continue;
                }
                // `math_kern(p,cur_mu)` (§717): the same conversion for a
                // `\mkern`.
                // `subtype(p):=explicit` once converted.
                Noad::MuKern(w) => {
                    let width = self.mu_mult(*w);
                    items.push(Translated::plain(Node::Kern {
                        width,
                        explicit: true,
                    }));
                    at += 1;
                    continue;
                }
                Noad::Node(n) => {
                    let mut n = n.clone();
                    match &mut n {
                        // §730: a rule counts towards what a `\left` grows to.
                        Node::Rule(r) => {
                            max_h = max_h.max(r.height);
                            max_d = max_d.max(r.depth);
                        }
                        // §732: an `\mskip` becomes ordinary glue at this size,
                        // and a `\nonscript` deletes the glue or kern after it
                        // when the size is not the text size.
                        Node::Glue(g) if g.source == GlueSource::MuGlue => {
                            let s = g.spec;
                            g.spec = self.math_glue(&MuGlue {
                                natural: s.natural,
                                stretch: s.stretch,
                                shrink: s.shrink,
                                stretch_order: s.stretch_order,
                                shrink_order: s.shrink_order,
                            });
                            g.source = GlueSource::Normal;
                        }
                        Node::Glue(g)
                            if g.source == GlueSource::CondMath
                                && self.size != super::font::TEXT_SIZE =>
                        {
                            let next_is_space = matches!(
                                work.get(at + 1),
                                Some(Noad::Node(Node::Glue(_) | Node::Kern { .. }))
                                    | Some(
                                        Noad::MuGlue(_)
                                            | Noad::MuKern(_)
                                            | Noad::Glue(_)
                                            | Noad::Kern(_)
                                    )
                            );
                            if next_is_space {
                                work.to_mut().remove(at + 1);
                            }
                        }
                        _ => {}
                    }
                    items.push(Translated::plain(n));
                    at += 1;
                    continue;
                }
                _ => {}
            }

            // §728's Bin-to-Ord rewrites, which depend on what came before.
            let mut class = class_of(&work[at]);
            if class == RType::Class(Class::Bin)
                && matches!(
                    r_type,
                    RType::Class(Class::Bin)
                        | RType::Class(Class::Op)
                        | RType::Class(Class::Rel)
                        | RType::Class(Class::Open)
                        | RType::Class(Class::Punct)
                        | RType::Left
                )
            {
                class = RType::Class(Class::Ord);
            }
            if matches!(
                class,
                RType::Class(Class::Rel)
                    | RType::Class(Class::Close)
                    | RType::Class(Class::Punct)
                    | RType::Right
            ) {
                // §729: a Bin with nothing binary after it is an Ord.
                if r_type == RType::Class(Class::Bin) {
                    if let Some(i) = r_index {
                        items[i].class = Some(RType::Class(Class::Ord));
                        items[i].penalty = INF_PENALTY;
                    }
                }
            }

            // The make_* procedures that rewrite a field (§734-§749) work on a
            // copy of their noad; nothing else copies one.
            let item = &work[at];
            let next = work.get(at + 1);
            let (hlist, delimiter) = match item {
                Noad::Left(d) | Noad::Right(d) => (Vec::new(), Some(*d)),
                Noad::Fraction(f) => (self.make_fraction(f), None),
                Noad::Radical(r) => {
                    let mut r = r.clone();
                    self.make_radical(&mut r);
                    // plain.tex's `\root`, which is `\sqrt[n]{x}`: the index
                    // goes on AFTER the radical exists, because it is placed
                    // by the radical's own height.
                    if let Some(index) = r.index.take() {
                        self.make_root(&mut r, &index);
                    }
                    (self.nucleus_and_scripts(&r.nucleus, 0, next), None)
                }
                Noad::Over(a) => {
                    let mut a = a.clone();
                    self.make_over(&mut a);
                    (self.nucleus_and_scripts(&a, 0, next), None)
                }
                Noad::Under(a) => {
                    let mut a = a.clone();
                    self.make_under(&mut a);
                    (self.nucleus_and_scripts(&a, 0, next), None)
                }
                // §733: `accent_noad: make_math_accent(q)`.
                Noad::Accent(acc) => {
                    let mut acc = acc.clone();
                    self.make_math_accent(&mut acc);
                    (self.nucleus_and_scripts(&acc.atom, 0, next), None)
                }
                // §733: `vcenter_noad: make_vcenter(q)`.
                Noad::VCenter(a) => {
                    let mut a = a.clone();
                    self.make_vcenter(&mut a);
                    (self.nucleus_and_scripts(&a, 0, next), None)
                }
                // §733: an Op is the one class with a procedure of its own;
                // `make_ord`'s kerns run inside `nucleus_and_scripts`, and Open
                // and Inner have nothing done to them.
                Noad::Atom(a) if a.class() == Class::Op => {
                    let mut a = a.clone();
                    match self.make_op(&mut a) {
                        (_, Some(limits_box)) => (limits_box, None),
                        (delta, None) => (self.nucleus_and_scripts(&a, delta, next), None),
                    }
                }
                Noad::Atom(a) => (self.nucleus_and_scripts(a, 0, next), None),
                _ => (Vec::new(), None),
            };

            // §727: `check_dimensions` -- the tallest and deepest so far, for
            // the `\left` and `\right` the second pass will size.
            if delimiter.is_none() {
                let (height, depth) = hlist_height_depth(&hlist);
                max_h = max_h.max(height);
                max_d = max_d.max(depth);
            }
            r_index = Some(items.len());
            r_type = class;
            items.push(Translated {
                class: Some(class),
                hlist,
                style: None,
                delimiter,
                // §761: only a Bin and a Rel are breakable, and only in a
                // formula `mlist_penalties` was turned on for.
                penalty: match class {
                    RType::Class(Class::Bin) => self.fonts.params.bin_op_penalty,
                    RType::Class(Class::Rel) => self.fonts.params.rel_penalty,
                    _ => INF_PENALTY,
                },
            });
            at += 1;
        }
        // §726's closing `Convert a final bin_noad to an ord_noad`.
        if r_type == RType::Class(Class::Bin) {
            if let Some(i) = r_index {
                items[i].class = Some(RType::Class(Class::Ord));
                items[i].penalty = INF_PENALTY;
            }
        }
        self.second_pass(items, style, max_h, max_d)
    }

    /// "Convert `nucleus(q)` to an hlist and attach the sub/superscripts"
    /// (§754-§755).
    fn nucleus_and_scripts(
        &mut self,
        a: &Atom,
        mut delta: Scaled,
        next: Option<&Noad>,
    ) -> Vec<Node> {
        let mut p: Vec<Node> = match &a.nucleus {
            Field::Char(c) => {
                // §755: the character, followed by its italic correction when
                // there is no subscript to hold the correction's place.
                match self.fetch(*c, self.size) {
                    Some((f, m)) => {
                        let mut list = Vec::new();
                        if let Some(node) = self.char_node(*c, self.size) {
                            list.push(node);
                        }
                        delta = m.italic;
                        // §752: an Ord whose nucleus is followed immediately
                        // by another character of the SAME family is a
                        // `math_text_char`, and §755 gives it no italic
                        // correction when the font is a TEXT font -- one whose
                        // `space` parameter is not zero. That is what keeps
                        // `\log` from setting as `lo` and a kerned `g`: the
                        // three letters are cmr10's, and cmr10 has a space.
                        if self.text_char(a, next) && f.param(2) != 0 {
                            delta = 0;
                        }
                        if a.subscr.is_empty() && delta != 0 {
                            list.push(kern(delta));
                            delta = 0;
                        }
                        list
                    }
                    None => Vec::new(),
                }
            }
            Field::Empty => Vec::new(),
            Field::Box(b) => vec![Node::Box(b.as_ref().clone())],
            Field::List(l) => {
                // §754: `mlist_penalties := false` here too -- the list is
                // about to become one box, so nothing inside it can break.
                let save = self.style;
                let penalties = std::mem::replace(&mut self.penalties, false);
                let inner = self.convert(l, save);
                self.penalties = penalties;
                self.set_style(save);
                vec![Node::Box(
                    hpack(inner, NATURAL, Tolerances::plain(), None).node,
                )]
            }
            Field::Literal(c) => vec![self.literal_node(*c, self.size)],
        };
        // §752: the kern `make_ord` finds between this character and the next.
        if let Some(by) = self.make_ord_kern(a, next) {
            if by != 0 {
                p.push(kern(by));
            }
        }
        if a.subscr.is_empty() && a.supscr.is_empty() {
            return p;
        }
        self.make_scripts(a, &mut p, delta);
        p
    }

    /// "Make a second pass over the mlist" (§760-§767): the noads go, the
    /// spacing of §764 arrives.
    fn second_pass(
        &mut self,
        mut items: Vec<Translated>,
        style: Style,
        max_h: Scaled,
        max_d: Scaled,
    ) -> Vec<Node> {
        let mut out: Vec<Node> = Vec::new();
        let mut r_type: Option<Class> = None;
        self.set_style(style);
        // Each hlist is moved out as it is reached; the look ahead below reads
        // only the item after, which is still whole.
        for at in 0..items.len() {
            let item = &mut items[at];
            let hlist = std::mem::take(&mut item.hlist);
            let (class, delimiter, penalty) = (item.class, item.delimiter, item.penalty);
            if let Some(s) = item.style {
                self.set_style(s);
                continue;
            }
            let Some(class) = class else {
                out.extend(hlist);
                continue;
            };
            // §761: a `left_noad` or a `right_noad` becomes an Open or a
            // Close, and its delimiter is grown to fit the formula.
            let (t, hlist) = match (class, delimiter) {
                (RType::Left, Some(d)) => {
                    (Class::Open, self.make_left_right(d, style, max_d, max_h))
                }
                (RType::Right, Some(d)) => {
                    (Class::Close, self.make_left_right(d, style, max_d, max_h))
                }
                (RType::Class(c), _) => (c, hlist),
                _ => (Class::Ord, hlist),
            };
            if let Some(r) = r_type {
                if let Some(g) = self.inter_element(r, t) {
                    out.push(Node::Glue(g));
                }
            }
            out.extend(hlist);
            // §767: a penalty after the hlist of a Bin or a Rel, so a long
            // formula can break there and nowhere else -- but never before the
            // end of the formula, and never in front of a Rel, which would put
            // the break where the relation's own penalty already offers one.
            if self.penalties && penalty < INF_PENALTY {
                // §767 reads `type(link(q))` -- the node immediately after,
                // whatever it is -- and inserts nothing when the formula ends
                // there, when a penalty node already stands there, or when a
                // Rel follows, whose own penalty would offer the same break.
                if let Some(following) = items.get(at + 1) {
                    let already = matches!(following.hlist.as_slice(), [Node::Penalty(_)]);
                    if !already && following.class != Some(RType::Class(Class::Rel)) {
                        out.push(Node::Penalty(penalty));
                    }
                }
            }
            r_type = Some(t);
        }
        out
    }

    /// "Append inter-element spacing based on `r_type` and `t`" (§766).
    ///
    /// The glue is `math_glue` (§716) of the parameter §764's digit names,
    /// converted from `mu` to points at the current size's `cur_mu`.
    fn inter_element(&self, r: Class, t: Class) -> Option<GlueNode> {
        let script = self.style >= SCRIPT_STYLE;
        let p = &self.fonts.params;
        let (g, source) = match spacing(r, t) {
            Space::None | Space::Impossible => return None,
            Space::ConditionalThin if script => return None,
            Space::ConditionalThin | Space::Thin => (p.thin_mu_skip, GlueSource::ThinMuSkip),
            Space::ConditionalMedium if script => return None,
            Space::ConditionalMedium => (p.med_mu_skip, GlueSource::MedMuSkip),
            Space::ConditionalThick if script => return None,
            Space::ConditionalThick => (p.thick_mu_skip, GlueSource::ThickMuSkip),
        };
        // §766: `subtype(z):=x+1`, the parameter the glue came from.
        Some(GlueNode::param(self.math_glue(&g), source))
    }

    /// One `mu` amount, in points at the current size (§716).
    ///
    /// §716 splits `cur_mu` into an integer part `n` and a fraction `f` and
    /// computes `n*x + xn_over_d(x,f,"200000)`, which is `x * cur_mu / 65536`
    /// done without a 64-bit multiply. `math_kern` (§717) does the same
    /// arithmetic on a kern's single width.
    fn mu_mult(&self, x: Scaled) -> Scaled {
        let mut n = x_over_n(self.mu, 65536);
        let mut f = self.mu - n * 65536;
        if f < 0 {
            n -= 1;
            f += 65536;
        }
        n * x + xn_over_d(x, f, 65536)
    }

    /// `math_glue(g,m)` (§716): a glue written in `mu`, in points.
    ///
    /// Infinite stretch and shrink are copied as they are: a `fil` is not a
    /// length in mu.
    fn math_glue(&self, g: &MuGlue) -> Glue {
        let finite = |v: Scaled, order: i64| match order {
            0 => self.mu_mult(v),
            _ => v,
        };
        Glue {
            natural: self.mu_mult(g.natural),
            stretch: finite(g.stretch, g.stretch_order),
            stretch_order: g.stretch_order,
            shrink: finite(g.shrink, g.shrink_order),
            shrink_order: g.shrink_order,
        }
    }
}

/// The `type(q)` of a noad, as §761 reads it.
fn class_of(item: &Noad) -> RType {
    match item {
        Noad::Atom(a) => RType::Class(a.class()),
        // §761: a fraction, a radical, an overline, an underline, an accent
        // and a `\vcenter` are all Ord for spacing purposes -- the case in
        // §761 assigns `t` for none of them, so each keeps the `t:=ord_noad`
        // the switch was entered with.
        Noad::Fraction(_)
        | Noad::Radical(_)
        | Noad::Over(_)
        | Noad::Under(_)
        | Noad::Accent(_)
        | Noad::VCenter(_) => RType::Class(Class::Ord),
        Noad::Left(_) => RType::Left,
        Noad::Right(_) => RType::Right,
        _ => RType::Class(Class::Ord),
    }
}

/// The formula, boxed: what `\hbox`ing the result of `mlist_to_hlist` gives.
pub fn set(fonts: &MathFonts, mlist: &[Noad], style: Style) -> BoxNode {
    let list = Converter::new(fonts).convert(mlist, style);
    hpack(list, NATURAL, Tolerances::plain(), None).node
}

/// The formula, boxed, with §767's break penalties in it.
///
/// §1194 turns them on for a formula set inside a paragraph and §1199 leaves
/// them off for a display. They are inert until a formula's hlist reaches the
/// paragraph breaker, which it does not here -- `src/typeset.rs` measures a
/// set formula as ONE word -- so this is the shape of the port rather than a
/// place a line breaks today. See `crate::math`'s note.
pub fn set_with_penalties(fonts: &MathFonts, mlist: &[Noad], style: Style) -> BoxNode {
    let list = Converter::new(fonts)
        .with_penalties(true)
        .convert(mlist, style);
    hpack(list, NATURAL, Tolerances::plain(), None).node
}