jubarte-render 0.19.0

The render product of the jubarte engine: DOCX to PDF and PNG, with Word-faithful layout
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
// SPDX-FileCopyrightText: 2026 Jandira Technologies, LLC
// SPDX-FileCopyrightText: 2023 Ismael Shakverdiev
//
// SPDX-License-Identifier: Apache-2.0 AND LicenseRef-markdown2pdf-MIT

//! OMML bar fractions, radicals, and stretchy delimiters as a box.
//!
//! The fraction, radical, and delimiter arithmetic follows markdown2pdf
//! (MIT, Ismael Shakverdiev, 2023). Constants come from the font's MATH
//! table. Two numbers do not: a delimiter is at least 0.9 em tall (MATH
//! has no minimum), and a superscript inside one of these boxes keeps
//! today's 0.65 size and +0.35 em shift.

use super::math_font::{MathFont, Stretch};
use crate::namespaces::{M, W};
use crate::xmllinq::{Dom, NodeId};

/// One positioned glyph. `y` grows up from the box baseline.
/// `text` is the searchable character; empty on an assembly extender.
#[derive(Debug, Clone)]
pub(crate) struct MathGlyph {
    pub text: String,
    pub gid: u16,
    pub x: f32,
    pub y: f32,
    pub size: f32,
}

/// A horizontal rule. `y_top` is its top edge, up from the baseline.
#[derive(Debug, Clone)]
pub(crate) struct MathRule {
    pub x: f32,
    pub y_top: f32,
    pub w: f32,
    pub thickness: f32,
}

/// A laid-out equation: width, ascent, descent, glyphs, and rules.
#[derive(Debug, Clone, Default)]
pub(crate) struct MathBox {
    pub width: f32,
    pub ascent: f32,
    pub descent: f32,
    pub glyphs: Vec<MathGlyph>,
    pub rules: Vec<MathRule>,
}

/// Lay out `node` (`m:oMath`, or one `m:f` / `m:rad` / `m:d`).
/// `display` is true when an `m:oMathPara` ancestor selects display style.
pub(crate) fn layout_node(
    dom: &Dom,
    node: NodeId,
    font: &MathFont,
    size: f32,
    display: bool,
) -> MathBox {
    layout(dom, node, font, size, display, 0)
}

/// Lay out the first `m:oMath` child of `omml`. `display` selects the
/// display MATH constants (an `m:oMathPara` ancestor in the document).
/// The collector calls [`layout_node`] on the element it already holds.
#[cfg(test)]
pub(crate) fn layout_omml(font: &MathFont, size: f32, display: bool, omml: &str) -> MathBox {
    let xml = format!(
        r#"<?xml version="1.0"?>
<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math"><w:body><w:p><m:oMath>{omml}</m:oMath></w:p></w:body></w:document>"#
    );
    let mut dom = Dom::new();
    let doc = dom.parse_xdocument(&xml);
    let Some(root) = dom.root(doc) else {
        return MathBox::default();
    };
    let Some(math) = dom
        .descendants(root, Some(&M::name("oMath")))
        .into_iter()
        .next()
    else {
        return MathBox::default();
    };
    layout_node(&dom, math, font, size, display)
}

const MAX_DEPTH: u32 = 64;

fn layout(
    dom: &Dom,
    node: NodeId,
    font: &MathFont,
    size: f32,
    display: bool,
    depth: u32,
) -> MathBox {
    if depth > MAX_DEPTH {
        return MathBox::default();
    }
    let next = depth + 1;
    if is(dom, node, "f") {
        return fraction(dom, node, font, size, display, next);
    }
    if is(dom, node, "rad") {
        return radical(dom, node, font, size, display, next);
    }
    if is(dom, node, "d") {
        return delimiter(dom, node, font, size, display, next);
    }
    if is(dom, node, "sSup") || is(dom, node, "sSub") || is(dom, node, "sSubSup") {
        return scripts(dom, node, font, size, display, next, false);
    }
    if is(dom, node, "sPre") {
        return scripts(dom, node, font, size, display, next, true);
    }
    if is(dom, node, "nary") {
        return nary(dom, node, font, size, display, next);
    }
    if is(dom, node, "r") || dom.name_is(node, &W::r()) {
        return math_run(dom, node, font, size);
    }
    row_children(dom, node, font, size, display, next)
}

fn is(dom: &Dom, node: NodeId, local: &str) -> bool {
    dom.name_is(node, &M::name(local))
}

fn is_property(dom: &Dom, node: NodeId) -> bool {
    dom.name(node)
        .is_some_and(|n| n.local_name().ends_with("Pr"))
}

fn child(dom: &Dom, node: NodeId, local: &str) -> Option<NodeId> {
    dom.element(node, &M::name(local))
}

fn val_of(dom: &Dom, node: NodeId) -> Option<&str> {
    dom.attribute(node, &M::name("val"))
        .or_else(|| super::attr_any(dom, node, "val"))
}

/// A present on/off element is on unless its value is 0, off, or false.
fn flag_on(dom: &Dom, parent: NodeId, local: &str) -> bool {
    let Some(el) = child(dom, parent, local) else {
        return false;
    };
    !matches!(val_of(dom, el), Some("0" | "off" | "false"))
}

fn row_children(
    dom: &Dom,
    node: NodeId,
    font: &MathFont,
    size: f32,
    display: bool,
    depth: u32,
) -> MathBox {
    let parts = dom
        .elements(node, None)
        .into_iter()
        .filter(|&c| !is_property(dom, c))
        .map(|c| layout(dom, c, font, size, display, depth))
        .collect();
    hstack(parts)
}

fn hstack(parts: Vec<MathBox>) -> MathBox {
    let mut out = MathBox::default();
    let mut x = 0.0;
    for part in parts {
        let w = part.width;
        out.ascent = out.ascent.max(part.ascent);
        out.descent = out.descent.max(part.descent);
        append(&mut out, part, x, 0.0);
        x += w;
    }
    out.width = x;
    out
}

fn append(out: &mut MathBox, mut part: MathBox, x: f32, y: f32) {
    for g in &mut part.glyphs {
        g.x += x;
        g.y += y;
    }
    for r in &mut part.rules {
        r.x += x;
        r.y_top += y;
    }
    out.glyphs.append(&mut part.glyphs);
    out.rules.append(&mut part.rules);
}

/// Ascent and descent after a baseline shift of `dy` (+up).
fn shifted(part: &MathBox, dy: f32) -> (f32, f32) {
    ((part.ascent + dy).max(0.0), (part.descent - dy).max(0.0))
}

fn fraction(
    dom: &Dom,
    node: NodeId,
    font: &MathFont,
    size: f32,
    display: bool,
    depth: u32,
) -> MathBox {
    let kind = child(dom, node, "fPr")
        .and_then(|pr| child(dom, pr, "type"))
        .and_then(|t| val_of(dom, t))
        .unwrap_or("bar");
    if kind.eq_ignore_ascii_case("noBar") {
        return stacked(dom, node, font, size, display, depth);
    }
    if kind.eq_ignore_ascii_case("lin") || kind.eq_ignore_ascii_case("skw") {
        return row_children(dom, node, font, size, display, depth);
    }
    let c = font.constants();
    let rule = font.scale(c.fraction_rule_thickness, size);
    let axis = font.scale(c.axis_height, size);
    let (shift_up, shift_down, gap_up, gap_down) = if display {
        (
            c.fraction_num_display_shift_up,
            c.fraction_denom_display_shift_down,
            c.fraction_num_display_gap_min,
            c.fraction_denom_display_gap_min,
        )
    } else {
        (
            c.fraction_num_shift_up,
            c.fraction_denom_shift_down,
            c.fraction_num_gap_min,
            c.fraction_denom_gap_min,
        )
    };
    let op = if display {
        size
    } else {
        size * c.script_percent
    };
    let num = child(dom, node, "num")
        .map(|n| layout(dom, n, font, op, display, depth))
        .unwrap_or_default();
    let den = child(dom, node, "den")
        .map(|n| layout(dom, n, font, op, display, depth))
        .unwrap_or_default();
    let mut nu = font.scale(shift_up, size);
    let mut de = font.scale(shift_down, size);
    let gn = font.scale(gap_up, size);
    let gd = font.scale(gap_down, size);
    let bar_top = axis + rule / 2.0;
    let bar_bot = axis - rule / 2.0;
    if nu - num.descent < bar_top + gn {
        nu = bar_top + gn + num.descent;
    }
    if den.ascent - de > bar_bot - gd {
        de = den.ascent - bar_bot + gd;
    }
    // One rule-thickness of side padding, from the font. Not TeX's 0.12 em.
    let pad = rule;
    let content = num.width.max(den.width);
    let num_w = num.width;
    let den_w = den.width;
    let mut out = MathBox::default();
    append(&mut out, num, pad + (content - num_w) / 2.0, nu);
    append(&mut out, den, pad + (content - den_w) / 2.0, -de);
    out.rules.push(MathRule {
        x: 0.0,
        y_top: bar_top,
        w: content + 2.0 * pad,
        thickness: rule,
    });
    out.width = content + 2.0 * pad;
    fit(&mut out, font);
    out
}

/// noBar, nested: today's stack. Numerator +0.45 em, denominator −0.40 em,
/// both at 0.65 of the enclosing size.
fn stacked(
    dom: &Dom,
    node: NodeId,
    font: &MathFont,
    size: f32,
    display: bool,
    depth: u32,
) -> MathBox {
    let op = size * 0.65;
    let num = child(dom, node, "num")
        .map(|n| layout(dom, n, font, op, display, depth))
        .unwrap_or_default();
    let den = child(dom, node, "den")
        .map(|n| layout(dom, n, font, op, display, depth))
        .unwrap_or_default();
    let up = size * 0.45;
    let down = size * 0.40;
    let content = num.width.max(den.width);
    let num_w = num.width;
    let den_w = den.width;
    let mut out = MathBox::default();
    append(&mut out, num, (content - num_w) / 2.0, up);
    append(&mut out, den, (content - den_w) / 2.0, -down);
    out.width = content;
    fit(&mut out, font);
    out
}

fn radical(
    dom: &Dom,
    node: NodeId,
    font: &MathFont,
    size: f32,
    display: bool,
    depth: u32,
) -> MathBox {
    let c = font.constants();
    let rule = font.scale(c.radical_rule_thickness, size);
    let gap = if display {
        font.scale(c.radical_display_vertical_gap, size)
    } else {
        font.scale(c.radical_vertical_gap, size)
    };
    let extra = font.scale(c.radical_extra_ascender, size);
    let hide = child(dom, node, "radPr").is_some_and(|pr| flag_on(dom, pr, "degHide"));
    let body = child(dom, node, "e")
        .map(|n| layout(dom, n, font, size, display, depth))
        .unwrap_or_default();
    let need = body.ascent + body.descent + gap + rule;
    let sign = stretch_glyph(font, '\u{221A}', need, size, -body.descent, true);
    // A stretched radical can be an assembly. Its first glyph is the
    // bottom piece; the rule must meet the top of the entire sign.
    let sign_top = sign.ascent;
    let vinculum = sign_top.max(body.ascent + gap);
    let deg = if hide {
        None
    } else {
        child(dom, node, "deg")
            .map(|n| layout(dom, n, font, size * c.script_script_percent, display, depth))
    };
    let (kb, ka, deg_w) = if let Some(d) = &deg {
        (
            font.scale(c.radical_kern_before_degree, size),
            font.scale(c.radical_kern_after_degree, size),
            d.width,
        )
    } else {
        (0.0, 0.0, 0.0)
    };
    let left = kb + deg_w + ka;
    let mut out = MathBox::default();
    if let Some(d) = deg {
        let raise = (c.radical_degree_bottom_raise_percent * (vinculum + rule)).max(0.0);
        append(&mut out, d, kb, raise);
    }
    let sign_w = sign.width;
    let body_w = body.width;
    append(&mut out, sign, left, 0.0);
    append(&mut out, body, left + sign_w, 0.0);
    out.rules.push(MathRule {
        x: left + sign_w,
        y_top: vinculum + rule,
        w: body_w + rule,
        thickness: rule,
    });
    out.width = left + sign_w + body_w + rule;
    fit(&mut out, font);
    // The extra ascender is clearance above the rule, not ink.
    out.ascent = out.ascent.max(vinculum + rule + extra);
    out
}

fn delimiter(
    dom: &Dom,
    node: NodeId,
    font: &MathFont,
    size: f32,
    display: bool,
    depth: u32,
) -> MathBox {
    let pr = child(dom, node, "dPr");
    let beg = fence_char(dom, pr, "begChr", '(');
    let sep = fence_char(dom, pr, "sepChr", '|');
    let end = fence_char(dom, pr, "endChr", ')');
    let grow = pr.is_none_or(|p| child(dom, p, "grow").is_none_or(|_| flag_on(dom, p, "grow")));
    let items: Vec<MathBox> = dom
        .elements(node, Some(&M::name("e")))
        .into_iter()
        .map(|e| layout(dom, e, font, size, display, depth))
        .collect();
    let content = hstack(items.clone());
    let axis = font.scale(font.constants().axis_height, size);
    let delta = (content.ascent - axis).max(content.descent + axis);
    // MATH does not say how tall a delimiter must be. TeX's rule: twice
    // the larger distance from the axis, and at least 0.9 em.
    let target = (2.0 * delta).max(0.9 * size);
    let mut out = MathBox::default();
    let mut x = 0.0;
    if let Some(ch) = beg {
        let fence = stretch_glyph(font, ch, target, size, axis - target / 2.0, grow);
        let w = fence.width;
        // Center a grown fence on the axis. `stretch_glyph` plants the
        // ink bottom at `bottom_y`; for a fence that bottom is
        // `axis - target/2`, which is the centered position when the
        // ink is `target` tall. A shorter base glyph (grow off, or no
        // variant) is recentered below.
        let fence = center_fence(font, fence, axis);
        append(&mut out, fence, x, 0.0);
        x += w;
    }
    for (i, item) in items.into_iter().enumerate() {
        if i > 0
            && let Some(ch) = sep
        {
            let fence = center_fence(
                font,
                stretch_glyph(font, ch, target, size, axis - target / 2.0, grow),
                axis,
            );
            let w = fence.width;
            append(&mut out, fence, x, 0.0);
            x += w;
        }
        let w = item.width;
        append(&mut out, item, x, 0.0);
        x += w;
    }
    if let Some(ch) = end {
        let fence = center_fence(
            font,
            stretch_glyph(font, ch, target, size, axis - target / 2.0, grow),
            axis,
        );
        let w = fence.width;
        append(&mut out, fence, x, 0.0);
        x += w;
    }
    out.width = x;
    fit(&mut out, font);
    out
}

fn scripts(
    dom: &Dom,
    node: NodeId,
    font: &MathFont,
    size: f32,
    display: bool,
    depth: u32,
    pre: bool,
) -> MathBox {
    let script_size = size * 0.65;
    let base = child(dom, node, "e")
        .map(|n| layout(dom, n, font, size, display, depth))
        .unwrap_or_default();
    let sub = child(dom, node, "sub").map(|n| layout(dom, n, font, script_size, display, depth));
    let sup = child(dom, node, "sup").map(|n| layout(dom, n, font, script_size, display, depth));
    let sub_w = sub.as_ref().map(|s| s.width).unwrap_or(0.0);
    let sup_w = sup.as_ref().map(|s| s.width).unwrap_or(0.0);
    let script_w = sub_w.max(sup_w);
    let base_w = base.width;
    let mut out = MathBox::default();
    let (base_x, script_x) = if pre { (script_w, 0.0) } else { (0.0, base_w) };
    append(&mut out, base, base_x, 0.0);
    if let Some(sub) = sub {
        append(&mut out, sub, script_x, -size * 0.15);
    }
    if let Some(sup) = sup {
        append(&mut out, sup, script_x, size * 0.35);
    }
    out.width = base_w + script_w;
    fit(&mut out, font);
    out
}

fn nary(dom: &Dom, node: NodeId, font: &MathFont, size: f32, display: bool, depth: u32) -> MathBox {
    let chr = child(dom, node, "naryPr")
        .and_then(|pr| child(dom, pr, "chr"))
        .and_then(|c| val_of(dom, c))
        .unwrap_or("")
        .to_string();
    let mut parts = Vec::new();
    if !chr.is_empty() {
        parts.push(text_run(font, &chr, true, size));
    }
    for child_id in dom.elements(node, None) {
        if is(dom, child_id, "naryPr") {
            continue;
        }
        let script = is(dom, child_id, "sub") || is(dom, child_id, "sup");
        let sz = if script { size * 0.65 } else { size };
        let dy = if is(dom, child_id, "sup") {
            size * 0.35
        } else if is(dom, child_id, "sub") {
            -size * 0.15
        } else {
            0.0
        };
        let part = layout(dom, child_id, font, sz, display, depth);
        let (asc, desc) = shifted(&part, dy);
        let w = part.width;
        // Stash the shift by appending into a one-piece box at the right y.
        let mut placed = MathBox::default();
        append(&mut placed, part, 0.0, dy);
        placed.width = w;
        placed.ascent = asc;
        placed.descent = desc;
        parts.push(placed);
    }
    hstack(parts)
}

fn math_run(dom: &Dom, node: NodeId, font: &MathFont, size: f32) -> MathBox {
    let upright = super::math_run_upright(dom, node) != Some(false);
    text_run(font, &text_in(dom, node), upright, size)
}

fn text_in(dom: &Dom, node: NodeId) -> String {
    let mut out = String::new();
    collect_text(dom, node, &mut out);
    out
}

fn collect_text(dom: &Dom, node: NodeId, out: &mut String) {
    if let Some(t) = dom.text_value(node) {
        out.push_str(t);
        return;
    }
    for i in 0..dom.child_count(node) {
        collect_text(dom, dom.child_at(node, i), out);
    }
}

fn text_run(font: &MathFont, text: &str, upright: bool, size: f32) -> MathBox {
    let mapped: String = text.chars().map(super::math_italic).collect();
    // The collector's rule: the whole run stays ASCII when the face
    // lacks any letter it would map to.
    let show_mapped = !upright && super::math_alphanumerics_cover(text, &mapped);
    let mut out = MathBox::default();
    let mut x = 0.0;
    for (shown, orig) in mapped.chars().zip(text.chars()) {
        let shown = if show_mapped { shown } else { orig };
        let gid = font
            .glyph_id(shown)
            .or_else(|| font.glyph_id(orig))
            .unwrap_or(0);
        let adv = font.scale(font.glyph(gid).advance, size);
        out.glyphs.push(MathGlyph {
            text: shown.to_string(),
            gid,
            x,
            y: 0.0,
            size,
        });
        x += adv;
    }
    out.width = x;
    fit(&mut out, font);
    out
}

/// A delimiter character. Absent element → `default`. Empty `m:val` → nothing.
fn fence_char(dom: &Dom, pr: Option<NodeId>, local: &str, default: char) -> Option<char> {
    let Some(pr) = pr else {
        return Some(default);
    };
    let Some(el) = child(dom, pr, local) else {
        return Some(default);
    };
    match val_of(dom, el) {
        Some("") => None,
        Some(v) => v.chars().next(),
        None => Some(default),
    }
}

/// Vertical glyph, ink bottom at `bottom_y`. `grow` false keeps the base
/// glyph. A base glyph with no variants is scaled up to `target` pt.
fn stretch_glyph(
    font: &MathFont,
    ch: char,
    target: f32,
    size: f32,
    bottom_y: f32,
    grow: bool,
) -> MathBox {
    let Some(base) = font.glyph_id(ch) else {
        let mut out = text_run(font, &ch.to_string(), true, size);
        for g in &mut out.glyphs {
            g.y += bottom_y;
        }
        fit(&mut out, font);
        return out;
    };
    let label = ch.to_string();
    if !grow {
        return place_single(font, base, size, bottom_y, &label, false, target);
    }
    let upem = font.units_per_em();
    let target_units = if size > 0.0 {
        target / size * upem
    } else {
        0.0
    };
    match font.stretch_vertical(base, target_units) {
        Stretch::Single(gid) => {
            place_single(font, gid, size, bottom_y, &label, gid == base, target)
        }
        Stretch::Assembly { parts, overlap } => {
            assemble(font, &parts, overlap, target, size, bottom_y, &label)
        }
    }
}

fn place_single(
    font: &MathFont,
    gid: u16,
    size: f32,
    bottom_y: f32,
    label: &str,
    may_scale: bool,
    target: f32,
) -> MathBox {
    let m = font.glyph(gid);
    let natural = font.scale(m.height() + m.depth(), size);
    let draw = if may_scale && natural > 0.0 && natural + 0.01 < target {
        size * (target / natural)
    } else {
        size
    };
    let drawn = font.glyph(gid);
    // Ink bottom at `bottom_y`: baseline sits `-y_min` above that bottom.
    let baseline = bottom_y - font.scale(drawn.y_min, draw);
    let mut out = MathBox::default();
    out.glyphs.push(MathGlyph {
        text: label.to_string(),
        gid,
        x: 0.0,
        y: baseline,
        size: draw,
    });
    out.width = font.scale(drawn.advance, draw);
    fit(&mut out, font);
    out
}

/// Stack assembly parts bottom to top. The first glyph keeps `label`;
/// extenders carry an empty string so the text layer has one character.
fn assemble(
    font: &MathFont,
    parts: &[super::math_font::AssemblyPart],
    overlap: f32,
    height: f32,
    size: f32,
    bottom_y: f32,
    label: &str,
) -> MathBox {
    let ov = font.scale(overlap, size);
    let fixed: f32 = parts
        .iter()
        .filter(|p| !p.extender)
        .map(|p| font.scale(p.full_advance, size))
        .sum();
    let n_ext = parts.iter().filter(|p| p.extender).count().max(1) as f32;
    let ext_adv = parts
        .iter()
        .find(|p| p.extender)
        .map(|p| font.scale(p.full_advance, size))
        .unwrap_or(0.0);
    let need = (height - fixed + ov * (parts.len() as f32)).max(0.0);
    let reps = if ext_adv > ov {
        ((need / (ext_adv - ov)).ceil() as usize / n_ext as usize).max(1)
    } else {
        1
    };
    let mut out = MathBox::default();
    let mut y = bottom_y;
    let mut labeled = false;
    for part in parts {
        let count = if part.extender { reps } else { 1 };
        for _ in 0..count {
            let m = font.glyph(part.gid);
            let baseline = y - font.scale(m.y_min, size);
            let text = if labeled {
                String::new()
            } else {
                labeled = true;
                label.to_string()
            };
            out.glyphs.push(MathGlyph {
                text,
                gid: part.gid,
                x: 0.0,
                y: baseline,
                size,
            });
            out.width = out.width.max(font.scale(m.advance, size));
            y += font.scale(part.full_advance, size) - ov;
        }
    }
    fit(&mut out, font);
    out
}

/// A fence planted with its ink bottom at `axis - target/2` is already
/// centered when it reached `target`. A shorter glyph (grow off, or no
/// variant tall enough) is moved so its ink center sits on the axis.
fn center_fence(font: &MathFont, mut fence: MathBox, axis: f32) -> MathBox {
    let Some(g) = fence.glyphs.first() else {
        return fence;
    };
    let m = font.glyph(g.gid);
    let top = g.y + font.scale(m.height(), g.size);
    let bot = g.y - font.scale(m.depth(), g.size);
    // An assembly's center is the center of every part, not the first.
    let (top, bot) = fence.glyphs.iter().fold((top, bot), |(t, b), g| {
        let m = font.glyph(g.gid);
        (
            t.max(g.y + font.scale(m.height(), g.size)),
            b.min(g.y - font.scale(m.depth(), g.size)),
        )
    });
    let mid = (top + bot) / 2.0;
    let dy = axis - mid;
    if dy.abs() > 0.01 {
        for g in &mut fence.glyphs {
            g.y += dy;
        }
    }
    fit(&mut fence, font);
    fence
}

fn fit(b: &mut MathBox, font: &MathFont) {
    // A negative radical kern pulls the sign left of x = 0. Shift the box
    // so its ink starts at 0; the width is the right edge after that.
    let mut min_x = 0.0_f32;
    for g in &b.glyphs {
        min_x = min_x.min(g.x);
    }
    for r in &b.rules {
        min_x = min_x.min(r.x);
    }
    if min_x < 0.0 {
        for g in &mut b.glyphs {
            g.x -= min_x;
        }
        for r in &mut b.rules {
            r.x -= min_x;
        }
    }
    let mut asc = 0.0_f32;
    let mut desc = 0.0_f32;
    let mut right = 0.0_f32;
    for g in &b.glyphs {
        let m = font.glyph(g.gid);
        let top = g.y + font.scale(m.height(), g.size);
        let bot = g.y - font.scale(m.depth(), g.size);
        asc = asc.max(top);
        desc = desc.max(-bot);
        right = right.max(g.x + font.scale(m.advance, g.size));
    }
    for r in &b.rules {
        asc = asc.max(r.y_top);
        desc = desc.max(-(r.y_top - r.thickness));
        right = right.max(r.x + r.w);
    }
    b.ascent = asc;
    b.descent = desc;
    b.width = b.width.max(right);
}

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

    const SIZE: f32 = 12.0;
    const STIX: &[u8] = include_bytes!("../../../../tests/fixtures/fonts/STIXTwoMath.otf");

    const BAR: &str = r#"<m:f><m:num><m:r><m:t>a</m:t></m:r></m:num><m:den><m:r><m:t>b</m:t></m:r></m:den></m:f>"#;
    const RAD: &str = r#"<m:rad><m:radPr><m:degHide m:val="1"/></m:radPr><m:deg/><m:e><m:r><m:t>x</m:t></m:r></m:e></m:rad>"#;
    const DEG: &str = r#"<m:rad><m:deg><m:r><m:t>3</m:t></m:r></m:deg><m:e><m:r><m:t>x</m:t></m:r></m:e></m:rad>"#;

    fn stix() -> MathFont {
        MathFont::from_bytes(STIX).expect("STIX Two Math")
    }

    fn lay(font: &MathFont, display: bool, omml: &str) -> MathBox {
        layout_omml(font, SIZE, display, omml)
    }

    /// Mathematical italic, the character Word puts in the text layer.
    fn italic(c: char) -> char {
        match c {
            'h' => '\u{210E}',
            'a'..='z' => char::from_u32(0x1D44E + (c as u32 - 'a' as u32)).unwrap(),
            'A'..='Z' => char::from_u32(0x1D434 + (c as u32 - 'A' as u32)).unwrap(),
            _ => c,
        }
    }

    fn glyph(b: &MathBox, ascii: char) -> &MathGlyph {
        let want = italic(ascii);
        b.glyphs
            .iter()
            .find(|g| g.text.contains(want))
            .unwrap_or_else(|| panic!("no {want:?} in {b:?}; layout returned an empty box"))
    }

    fn radical(b: &MathBox) -> &MathGlyph {
        b.glyphs
            .iter()
            .find(|g| g.text.contains('\u{221A}'))
            .unwrap_or_else(|| panic!("no radical sign in {b:?}; layout returned an empty box"))
    }

    fn opening(b: &MathBox) -> &MathGlyph {
        b.glyphs
            .iter()
            .find(|g| g.text.contains('('))
            .expect("opening delimiter")
    }

    fn span(font: &MathFont, g: &MathGlyph) -> (f32, f32) {
        let m = font.glyph(g.gid);
        let top = g.y + font.scale(m.height(), g.size);
        let bot = g.y - font.scale(m.depth(), g.size);
        (top, bot)
    }

    const X: &str = r#"<m:e><m:r><m:t>x</m:t></m:r></m:e>"#;
    const SUB: &str = r#"<m:sub><m:r><m:t>i</m:t></m:r></m:sub>"#;
    const SUP: &str = r#"<m:sup><m:r><m:t>n</m:t></m:r></m:sup>"#;

    #[test]
    fn math_layout_sub_and_superscripts_sit_right_of_the_base() {
        let font = stix();
        let b = lay(
            &font,
            false,
            &format!("<m:sSubSup>{X}{SUB}{SUP}</m:sSubSup>"),
        );
        let (x, i, n) = (glyph(&b, 'x'), glyph(&b, 'i'), glyph(&b, 'n'));
        assert!(i.x > x.x && n.x > x.x, "{b:?}");
        assert!(i.y < x.y && n.y > x.y, "{b:?}");
        assert!(i.size < x.size, "{b:?}");
        let sup_only = lay(&font, false, &format!("<m:sSup>{X}{SUP}</m:sSup>"));
        assert!(glyph(&sup_only, 'n').y > 0.0, "{sup_only:?}");
        let sub_only = lay(&font, false, &format!("<m:sSub>{X}{SUB}</m:sSub>"));
        assert!(glyph(&sub_only, 'i').y < 0.0, "{sub_only:?}");
    }

    #[test]
    fn math_layout_prescripts_sit_left_of_the_base() {
        let font = stix();
        let b = lay(&font, false, &format!("<m:sPre>{SUB}{SUP}{X}</m:sPre>"));
        let (x, i, n) = (glyph(&b, 'x'), glyph(&b, 'i'), glyph(&b, 'n'));
        assert!(i.x < x.x && n.x < x.x, "{b:?}");
        assert!(b.width > 0.0);
        // A script with no base still has a width.
        let bare = lay(&font, false, &format!("<m:sSup>{SUP}</m:sSup>"));
        assert!(bare.width > 0.0, "{bare:?}");
    }

    #[test]
    fn math_layout_nary_puts_its_operator_first_and_limits_around_it() {
        let font = stix();
        let b = lay(
            &font,
            true,
            &format!(r#"<m:nary><m:naryPr><m:chr m:val="∑"/></m:naryPr>{SUB}{SUP}{X}</m:nary>"#),
        );
        let op = b
            .glyphs
            .iter()
            .find(|g| g.text.contains('∑'))
            .expect("operator");
        let (x, i, n) = (glyph(&b, 'x'), glyph(&b, 'i'), glyph(&b, 'n'));
        assert!(op.x < i.x && i.x < x.x, "{b:?}");
        assert!(i.y < 0.0 && n.y > 0.0 && x.y == 0.0, "{b:?}");
        // No `m:chr`: no operator glyph, the parts alone.
        let plain = lay(&font, false, &format!("<m:nary>{X}</m:nary>"));
        assert_eq!(plain.glyphs.len(), 1, "{plain:?}");
    }

    #[test]
    fn math_layout_linear_and_skewed_fractions_lay_out_in_a_row() {
        let font = stix();
        for kind in ["lin", "skw"] {
            let b = lay(
                &font,
                false,
                &format!(
                    r#"<m:f><m:fPr><m:type m:val="{kind}"/></m:fPr><m:num><m:r><m:t>a</m:t></m:r></m:num><m:den><m:r><m:t>b</m:t></m:r></m:den></m:f>"#
                ),
            );
            let (a, bb) = (glyph(&b, 'a'), glyph(&b, 'b'));
            assert_eq!(a.y, bb.y, "{kind}: {b:?}");
            assert!(bb.x > a.x, "{kind}: {b:?}");
            assert!(b.rules.is_empty(), "{kind}: {b:?}");
        }
    }

    #[test]
    fn math_layout_stops_at_its_depth_limit() {
        let font = stix();
        let deep = format!(
            "{}<m:r><m:t>x</m:t></m:r>{}",
            "<m:d><m:e>".repeat(80),
            "</m:e></m:d>".repeat(80)
        );
        let b = lay(&font, false, &deep);
        assert!(
            !b.glyphs.iter().any(|g| g.text.contains(italic('x'))),
            "{b:?}"
        );
        let shallow = format!(
            "{}<m:r><m:t>x</m:t></m:r>{}",
            "<m:d><m:e>".repeat(3),
            "</m:e></m:d>".repeat(3)
        );
        glyph(&lay(&font, false, &shallow), 'x');
    }

    #[test]
    fn math_layout_empty_delimiter_characters_draw_nothing() {
        let font = stix();
        let b = lay(
            &font,
            false,
            &format!(r#"<m:d><m:dPr><m:begChr m:val=""/><m:endChr m:val="]"/></m:dPr>{X}</m:d>"#),
        );
        assert!(!b.glyphs.iter().any(|g| g.text.contains('(')), "{b:?}");
        assert!(b.glyphs.iter().any(|g| g.text.contains(']')), "{b:?}");
        assert!(!b.glyphs.iter().any(|g| g.text.contains(')')), "{b:?}");
    }

    #[test]
    fn math_layout_bar_fraction_puts_the_rule_on_the_axis() {
        let font = stix();
        let b = lay(&font, true, BAR);
        let rule = b.rules.first().expect("bar fraction has a rule");
        let axis = font.scale(font.constants().axis_height, SIZE);
        let center = rule.y_top - rule.thickness / 2.0;
        assert!(
            (center - axis).abs() < 0.05,
            "rule center {center} is not the axis {axis}"
        );
        let num = glyph(&b, 'a');
        let den = glyph(&b, 'b');
        assert!(
            num.y > rule.y_top,
            "numerator baseline {} is not above the rule {}",
            num.y,
            rule.y_top
        );
        assert!(
            den.y < rule.y_top - rule.thickness,
            "denominator baseline {} is not below the rule",
            den.y
        );
        let piece = |g: &MathGlyph| font.scale(font.glyph(g.gid).advance, g.size);
        assert!(
            rule.w + 0.01 >= piece(num),
            "rule narrower than the numerator"
        );
        assert!(
            rule.w + 0.01 >= piece(den),
            "rule narrower than the denominator"
        );
        assert!(
            (num.size - SIZE).abs() < 0.05,
            "display numerator stays at the base size, got {}",
            num.size
        );
    }

    #[test]
    fn math_layout_inline_fraction_shrinks_numerator_and_denominator() {
        let font = stix();
        let b = lay(&font, false, BAR);
        let num = glyph(&b, 'a');
        let want = SIZE * font.constants().script_percent;
        assert!(
            (num.size - want).abs() < 0.05,
            "inline numerator {} is not script size {want}",
            num.size
        );
    }

    #[test]
    fn math_layout_radical_rule_clears_the_body() {
        let font = stix();
        let b = lay(&font, false, RAD);
        let sign = radical(&b);
        let body = glyph(&b, 'x');
        assert!(sign.x < body.x, "radical sign is not left of the body");
        let rule = b.rules.first().expect("radical has a rule");
        let (body_top, _) = span(&font, body);
        let gap = font.scale(font.constants().radical_vertical_gap, SIZE);
        let bottom = rule.y_top - rule.thickness;
        assert!(
            bottom + 0.05 >= body_top + gap,
            "rule bottom {bottom} is under body top {body_top} plus gap {gap}"
        );
    }

    #[test]
    fn math_layout_radical_degree_sits_above_and_left() {
        let font = stix();
        let b = lay(&font, false, DEG);
        let sign = radical(&b);
        let body = glyph(&b, 'x');
        let deg = b
            .glyphs
            .iter()
            .find(|g| g.text.contains('3'))
            .expect("degree");
        // The font's after-kern is negative: the degree tucks into the
        // surd, so its origin can sit to the right of the sign's origin.
        // What Word shows is the degree above the body and left of it,
        // with that kern between the degree's right edge and the sign.
        let deg_w = font.scale(font.glyph(deg.gid).advance, deg.size);
        let ka = font.scale(font.constants().radical_kern_after_degree, SIZE);
        let gap = sign.x - (deg.x + deg_w);
        assert!(
            (gap - ka).abs() < 0.05,
            "gap {gap} from the degree to the radical is not the font's after-kern {ka}"
        );
        assert!(deg.x < body.x, "degree is not left of the radicand");
        assert!(deg.y > body.y, "degree is not above the body baseline");
    }

    #[test]
    fn math_layout_delimiters_grow_to_cover_a_fraction() {
        let font = stix();
        let inner = lay(&font, true, BAR);
        let wrapped = format!("<m:d><m:e>{BAR}</m:e></m:d>");
        let short = lay(
            &font,
            true,
            r#"<m:d><m:e><m:r><m:t>x</m:t></m:r></m:e></m:d>"#,
        );
        let tall = lay(&font, true, &wrapped);
        let short_span = {
            let (top, bot) = span(&font, opening(&short));
            top - bot
        };
        let tall_g = opening(&tall);
        let (top, bot) = span(&font, tall_g);
        assert!(
            top - bot > short_span + 0.5,
            "fence around a fraction ({}) is not taller than the fence around x ({short_span})",
            top - bot
        );
        assert!(
            top + 0.05 >= inner.ascent,
            "fence top {top} does not cover the fraction ascent {}",
            inner.ascent
        );
        assert!(
            bot - 0.05 <= -inner.descent,
            "fence bottom {bot} does not cover the fraction descent {}",
            inner.descent
        );
    }

    #[test]
    fn math_layout_delimiters_cover_a_nobar_stack() {
        // word2007's binomial is m:d around m:f type=noBar. The stack
        // keeps today's shifts. The fences still have to cover it.
        let font = stix();
        let omml = r#"<m:d><m:e><m:f><m:fPr><m:type m:val="noBar"/></m:fPr><m:num><m:r><m:t>n</m:t></m:r></m:num><m:den><m:r><m:t>k</m:t></m:r></m:den></m:f></m:e></m:d>"#;
        let b = lay(&font, true, omml);
        let open = opening(&b);
        let (top, bot) = span(&font, open);
        let num = glyph(&b, 'n');
        let den = glyph(&b, 'k');
        let (num_top, _) = span(&font, num);
        let (_, den_bot) = span(&font, den);
        assert!(
            top + 0.05 >= num_top,
            "fence top {top} does not cover the numerator {num_top}"
        );
        assert!(
            bot - 0.05 <= den_bot,
            "fence bottom {bot} does not cover the denominator {den_bot}"
        );
        assert!(
            b.rules.is_empty(),
            "a noBar stack inside a delimiter has no fraction bar"
        );
    }

    #[test]
    fn math_layout_nests_a_fraction_in_a_radical_in_brackets() {
        let font = stix();
        let omml = format!(
            r#"<m:d><m:e><m:rad><m:radPr><m:degHide m:val="1"/></m:radPr><m:deg/><m:e>{BAR}</m:e></m:rad></m:e></m:d>"#
        );
        let b = lay(&font, true, &omml);
        assert!(
            b.rules.len() >= 2,
            "nesting needs the fraction bar and the radical rule, got {}",
            b.rules.len()
        );
        let sign = radical(&b);
        let num = glyph(&b, 'a');
        let open = b
            .glyphs
            .iter()
            .find(|g| g.text.contains('('))
            .expect("opening bracket");
        let close = b
            .glyphs
            .iter()
            .find(|g| g.text.contains(')'))
            .expect("closing bracket");
        assert!(sign.x < num.x, "radical is not left of the fraction");
        assert!(
            open.x < sign.x,
            "opening bracket is not outside the radical"
        );
        assert!(
            close.x > num.x,
            "closing bracket is not outside the fraction"
        );
        assert!(b.width.is_finite() && b.ascent.is_finite() && b.descent.is_finite());
        assert!(b.glyphs.iter().all(|g| g.x.is_finite() && g.y.is_finite()));
        assert!(b.rules.iter().all(|r| r.x.is_finite() && r.w.is_finite()));
    }

    #[test]
    fn math_layout_superscript_inside_a_radical_uses_todays_script_shift() {
        let font = stix();
        let omml = r#"<m:rad><m:radPr><m:degHide m:val="1"/></m:radPr><m:deg/><m:e><m:sSup><m:e><m:r><m:t>x</m:t></m:r></m:e><m:sup><m:r><m:t>2</m:t></m:r></m:sup></m:sSup></m:e></m:rad>"#;
        let b = lay(&font, true, omml);
        let base = glyph(&b, 'x');
        let sup = b
            .glyphs
            .iter()
            .find(|g| g.text.contains('2'))
            .expect("superscript");
        assert!(
            (sup.size - SIZE * 0.65).abs() < 0.05,
            "script size {} is not 0.65 of the base",
            sup.size
        );
        assert!(
            (sup.y - base.y - SIZE * 0.35).abs() < 0.05,
            "script shift {} is not +0.35 em",
            sup.y - base.y
        );
    }

    #[test]
    fn math_layout_fallback_font_still_stacks_a_bar_fraction() {
        let font = MathFont::fallback(1000.0);
        let b = lay(&font, true, BAR);
        let rule = b.rules.first().expect("fallback fraction has a rule");
        let num = glyph(&b, 'a');
        let den = glyph(&b, 'b');
        assert!(
            num.y > rule.y_top,
            "fallback numerator is not above the bar"
        );
        assert!(
            den.y < rule.y_top - rule.thickness,
            "fallback denominator is not below the bar"
        );
    }
    #[test]
    fn math_layout_radical_rule_reaches_the_top_of_an_assembled_sign() {
        let font = stix();
        let mut body = BAR.to_string();
        for _ in 0..6 {
            body =
                format!("<m:f><m:num>{body}</m:num><m:den><m:r><m:t>b</m:t></m:r></m:den></m:f>");
        }
        let body_box = lay(&font, true, &body);
        let c = font.constants();
        let gap = font.scale(c.radical_display_vertical_gap, SIZE);
        let thickness = font.scale(c.radical_rule_thickness, SIZE);
        let sign = stretch_glyph(
            &font,
            '\u{221A}',
            body_box.ascent + body_box.descent + gap + thickness,
            SIZE,
            -body_box.descent,
            true,
        );
        assert!(sign.glyphs.len() > 1, "test must exercise a glyph assembly");
        let omml = format!(
            "<m:rad><m:radPr><m:degHide m:val=\"1\"/></m:radPr><m:deg/><m:e>{body}</m:e></m:rad>"
        );
        let laid = lay(&font, true, &omml);
        let rule = laid.rules.last().unwrap();
        assert!(
            rule.y_top + 0.05 >= sign.ascent,
            "radical rule {} ends below assembled sign {}",
            rule.y_top,
            sign.ascent
        );
    }
}