mkgraphic 0.4.4

A Rust port of the cycfi/elements GUI framework
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
//! `MathNode` -> `MathBox`: TeX's box model (TeXbook ch. 11 / Appendix
//! G). Every box's `width`/`height`/`depth` are measured relative to its
//! *own* baseline (`height` = extent above, `depth` = extent below) --
//! composition means placing a child box's baseline at an offset from
//! its parent's, not stacking top-left rectangles the way the rest of
//! this crate's layout usually works.
//!
//! **Known simplification**: box `height`/`depth` for a single glyph use
//! the *font's* real ascent/descent (`Canvas::font_metrics`), not that
//! specific glyph's own ink extents -- this crate has no per-glyph
//! bounding-box API, and adding one is out of scope here. This means,
//! e.g., a lone `x` (no descender) and a lone `g` (has one) report
//! identical `height`/`depth` today. Visually close enough for chat-reply
//! math; a real fix would read each glyph's outline bbox via
//! `ttf_parser::Face::glyph_bounding_box`.

use crate::support::canvas::Canvas;
use crate::support::font::Font;

use super::ast::{BigOpKind, DelimiterKind, MathNode};
use super::style::{FontParams, MathStyle};

#[derive(Debug, Clone)]
pub struct MathBox {
    pub width: f32,
    pub height: f32,
    pub depth: f32,
    pub content: MathBoxContent,
}

#[derive(Debug, Clone)]
pub enum MathBoxContent {
    /// One or more glyphs in a single font/size -- drawn via the same
    /// `Canvas::fill_text` every plain text run already uses.
    Glyphs {
        text: String,
        font: Font,
        font_size: f32,
    },
    /// Already-laid-out children, each already positioned relative to
    /// this box's own baseline.
    Hlist(Vec<PlacedBox>),
    /// Non-glyph geometry -- fraction bars (Phase 3), radical hooks
    /// (Phase 4), delimiter paths (Phase 5).
    Rule(RuleKind),
}

#[derive(Debug, Clone)]
pub struct PlacedBox {
    pub x_offset: f32,
    /// Positive = this child's baseline sits *below* the parent's own
    /// baseline (e.g. a subscript or a fraction's denominator); negative
    /// = raised above it (a superscript, a fraction's numerator).
    pub baseline_shift: f32,
    pub math_box: MathBox,
}

#[derive(Debug, Clone)]
pub enum RuleKind {
    HorizontalBar {
        width: f32,
        thickness: f32,
    },
    /// A radical (√) sign: hook rises `total_height` above this rule
    /// box's own baseline, vinculum spans `radicand_width` to the right
    /// of the hook (see `super::radical`).
    Radical {
        total_height: f32,
        radicand_width: f32,
        thickness: f32,
    },
    /// A `\left`/`\right` growing delimiter (see `super::delimiter`).
    /// `is_open` mirrors the shape horizontally for the closing side --
    /// most of these shapes (`(`/`)`, brackets, braces, floor/ceil,
    /// angles) aren't symmetric, so which side they're drawn on matters.
    Delimiter {
        kind: DelimiterKind,
        thickness: f32,
        is_open: bool,
    },
}

/// Lays out `node` at `font_size` in the given [`MathStyle`], using
/// `canvas` for real glyph widths/metrics (matching `wrap_runs`'s own
/// existing `&mut Canvas` need for the same reason).
pub fn layout_math(
    node: &MathNode,
    style: MathStyle,
    font_size: f32,
    canvas: &mut Canvas,
) -> MathBox {
    match node {
        MathNode::Symbol { glyph, .. } => layout_symbol(*glyph, style, font_size, canvas),
        MathNode::Row(items) => layout_row(items, style, font_size, canvas),
        MathNode::Text(text) => layout_text(text, style, font_size, canvas),
        MathNode::Script { base, sup, sub } => layout_script(
            base,
            sup.as_deref(),
            sub.as_deref(),
            style,
            font_size,
            canvas,
        ),
        MathNode::Frac { num, den } => layout_frac(num, den, style, font_size, canvas),
        MathNode::Sqrt(radicand) => layout_sqrt(radicand, style, font_size, canvas),
        MathNode::Delimited { open, body, close } => {
            layout_delimited(*open, body, *close, style, font_size, canvas)
        }
        MathNode::BigOp { kind, lower, upper } => layout_bigop(
            *kind,
            lower.as_deref(),
            upper.as_deref(),
            style,
            font_size,
            canvas,
        ),
    }
}

/// Approximate x-height -- half the font's real ascent, since `Canvas`
/// has no direct x-height accessor (see this module's caching sibling,
/// `Canvas::font_metrics`, whose own doc comment notes the same gap).
/// Used only for Rule 18's "ink must clear N% of x-height" checks, where
/// an approximation is acceptable -- those checks are a minimum-clearance
/// safety net, not a precise typographic measurement.
fn approx_x_height(style: MathStyle, font_size: f32, canvas: &mut Canvas) -> f32 {
    canvas.font_size(font_size * style.size_scale());
    canvas.font_metrics().ascent * 0.5
}

fn layout_symbol(glyph: char, style: MathStyle, font_size: f32, canvas: &mut Canvas) -> MathBox {
    let scaled_size = font_size * style.size_scale();
    // TeX's math-italic convention: letters (Latin and Greek alike --
    // both are `char::is_alphabetic`) render italic; digits, operators,
    // and punctuation stay upright.
    let font = if glyph.is_alphabetic() {
        Font::sans_serif().italic()
    } else {
        Font::sans_serif()
    };
    canvas.font(font.clone());
    canvas.font_size(scaled_size);
    let text = glyph.to_string();
    let width = canvas.text_width(&text);
    let metrics = canvas.font_metrics();
    MathBox {
        width,
        height: metrics.ascent,
        depth: metrics.descent,
        content: MathBoxContent::Glyphs {
            text,
            font,
            font_size: scaled_size,
        },
    }
}

fn layout_text(text: &str, style: MathStyle, font_size: f32, canvas: &mut Canvas) -> MathBox {
    let scaled_size = font_size * style.size_scale();
    let font = Font::sans_serif();
    canvas.font(font.clone());
    canvas.font_size(scaled_size);
    let width = canvas.text_width(text);
    let metrics = canvas.font_metrics();
    MathBox {
        width,
        height: metrics.ascent,
        depth: metrics.descent,
        content: MathBoxContent::Glyphs {
            text: text.to_string(),
            font,
            font_size: scaled_size,
        },
    }
}

/// Concatenates atoms left to right with one small fixed gap between
/// each pair -- real TeX Rule 20 varies this gap by the adjacent atoms'
/// `AtomClass` pairing (e.g. more space around a binary operator than
/// around ordinary/ordinary); deferred for v1, see `AtomClass`'s own doc
/// comment for why that's a documented simplification, not an oversight.
fn layout_row(
    items: &[MathNode],
    style: MathStyle,
    font_size: f32,
    canvas: &mut Canvas,
) -> MathBox {
    let params = FontParams::for_size(font_size * style.size_scale());
    let gap = params.default_rule_thickness * 3.0;

    let mut children = Vec::with_capacity(items.len());
    let mut x = 0.0f32;
    let mut height = 0.0f32;
    let mut depth = 0.0f32;

    for (i, item) in items.iter().enumerate() {
        if i > 0 {
            x += gap;
        }
        let child_box = layout_math(item, style, font_size, canvas);
        height = height.max(child_box.height);
        depth = depth.max(child_box.depth);
        let width = child_box.width;
        children.push(PlacedBox {
            x_offset: x,
            baseline_shift: 0.0,
            math_box: child_box,
        });
        x += width;
    }

    MathBox {
        width: x,
        height,
        depth,
        content: MathBoxContent::Hlist(children),
    }
}

/// Rule 18: superscript/subscript placement. Both may be present at
/// once (`x_i^2`); when they are, both are placed at the same
/// horizontal offset (stacked vertically after the base), and their
/// shifts are additionally constrained to keep a minimum gap between
/// them.
fn layout_script(
    base: &MathNode,
    sup: Option<&MathNode>,
    sub: Option<&MathNode>,
    style: MathStyle,
    font_size: f32,
    canvas: &mut Canvas,
) -> MathBox {
    let params = FontParams::for_size(font_size);
    let base_box = layout_math(base, style, font_size, canvas);
    let x_height = approx_x_height(style, font_size, canvas);

    let sup_box = sup.map(|s| layout_math(s, style.sup_style(), font_size, canvas));
    let sub_box = sub.map(|s| layout_math(s, style.sub_style(), font_size, canvas));

    let mut sup_shift = 0.0f32;
    if let Some(sup_box) = &sup_box {
        let base_shift = if style.is_cramped() {
            params.sup3
        } else if style.is_display() {
            params.sup1
        } else {
            params.sup2
        };
        let clearance = base_box.height - params.sup_drop;
        let ink_min = sup_box.depth + x_height * 0.25;
        sup_shift = base_shift.max(clearance).max(ink_min);
    }

    let mut sub_shift = 0.0f32;
    if let Some(sub_box) = &sub_box {
        let base_shift = if style.is_display() {
            params.sub1
        } else {
            params.sub2
        };
        let clearance = base_box.depth + params.sub_drop;
        let ink_min = sub_box.height + x_height * 0.8;
        sub_shift = base_shift.max(clearance).max(ink_min);
    }

    if let (Some(sup_box), Some(sub_box)) = (&sup_box, &sub_box) {
        let gap = (sup_shift - sup_box.depth) - (sub_box.height - sub_shift);
        let min_gap = 4.0 * params.default_rule_thickness;
        if gap < min_gap {
            let push = (min_gap - gap) / 2.0;
            sup_shift += push;
            sub_shift += push;
        }
    }

    let script_width = match (&sup_box, &sub_box) {
        (Some(s), Some(b)) => s.width.max(b.width),
        (Some(s), None) => s.width,
        (None, Some(b)) => b.width,
        (None, None) => 0.0,
    };

    let mut height = base_box.height;
    let mut depth = base_box.depth;
    let base_width = base_box.width;
    let mut children = vec![PlacedBox {
        x_offset: 0.0,
        baseline_shift: 0.0,
        math_box: base_box,
    }];

    if let Some(sup_box) = sup_box {
        height = height.max(sup_shift + sup_box.height);
        depth = depth.max((sup_box.depth - sup_shift).max(0.0));
        children.push(PlacedBox {
            x_offset: base_width,
            baseline_shift: -sup_shift,
            math_box: sup_box,
        });
    }
    if let Some(sub_box) = sub_box {
        depth = depth.max(sub_shift + sub_box.depth);
        height = height.max((sub_box.height - sub_shift).max(0.0));
        children.push(PlacedBox {
            x_offset: base_width,
            baseline_shift: sub_shift,
            math_box: sub_box,
        });
    }

    MathBox {
        width: base_width + script_width,
        height,
        depth,
        content: MathBoxContent::Hlist(children),
    }
}

/// Rule 15: fraction layout. The bar is centered on the axis (not the
/// baseline -- math notation's horizontal "center line," per
/// `FontParams::axis_height`); numerator/denominator get a minimum
/// clearance from it, symmetrically pushed apart if their natural
/// (style-appropriate) shift would violate that minimum.
fn layout_frac(
    num: &MathNode,
    den: &MathNode,
    style: MathStyle,
    font_size: f32,
    canvas: &mut Canvas,
) -> MathBox {
    let params = FontParams::for_size(font_size);
    let num_box = layout_math(num, style.numerator_style(), font_size, canvas);
    let den_box = layout_math(den, style.denominator_style(), font_size, canvas);
    let thickness = params.default_rule_thickness;

    let (num_shift_ideal, den_shift_ideal, min_gap) = if style.is_display() {
        (params.num1, params.denom1, 3.0 * thickness)
    } else {
        (params.num2, params.denom2, 1.0 * thickness)
    };

    // The bar is centered on the axis, `axis_height` *above* the
    // baseline -- not at the baseline itself -- so numerator/denominator
    // clearance is measured against the bar's own top/bottom edge, each
    // independently (this is two separate minimum-clearance checks, not
    // one shared gap split across both sides).
    let bar_top = params.axis_height + thickness / 2.0;
    let bar_bottom = params.axis_height - thickness / 2.0;

    let num_clearance = (num_shift_ideal - num_box.depth) - bar_top;
    let num_shift = if num_clearance < min_gap {
        num_shift_ideal + (min_gap - num_clearance)
    } else {
        num_shift_ideal
    };

    let den_clearance = bar_bottom - (den_box.height - den_shift_ideal);
    let den_shift = if den_clearance < min_gap {
        den_shift_ideal + (min_gap - den_clearance)
    } else {
        den_shift_ideal
    };

    let width = num_box.width.max(den_box.width);
    let bar = MathBox {
        width,
        height: thickness / 2.0,
        depth: thickness / 2.0,
        content: MathBoxContent::Rule(RuleKind::HorizontalBar { width, thickness }),
    };

    let num_width = num_box.width;
    let den_width = den_box.width;
    let children = vec![
        PlacedBox {
            x_offset: (width - num_width) / 2.0,
            baseline_shift: -num_shift,
            math_box: num_box,
        },
        PlacedBox {
            x_offset: 0.0,
            baseline_shift: -params.axis_height,
            math_box: bar,
        },
        PlacedBox {
            x_offset: (width - den_width) / 2.0,
            baseline_shift: den_shift,
            math_box: den_box,
        },
    ];

    let height = num_shift + children[0].math_box.height;
    let depth = den_shift + children[2].math_box.depth;
    MathBox {
        width,
        height,
        depth,
        content: MathBoxContent::Hlist(children),
    }
}

/// Rule 11: radical layout. The radicand keeps the expression's own
/// baseline (no vertical shift -- this is what makes `\sqrt{x}` sit at
/// the same baseline as surrounding text); the radical sign itself rises
/// above it by the radicand's height plus a clearance gap plus the bar's
/// own thickness, so the vinculum floats just above the tallest content
/// it covers.
fn layout_sqrt(
    radicand: &MathNode,
    style: MathStyle,
    font_size: f32,
    canvas: &mut Canvas,
) -> MathBox {
    let params = FontParams::for_size(font_size);
    // The radicand is cramped (Rule 11): no extra headroom is reserved
    // above it the way an uncramped style would imply, since the
    // radical sign itself is what visually "covers" it.
    let radicand_box = layout_math(radicand, style.cramped(), font_size, canvas);
    let thickness = params.default_rule_thickness;
    let clearance = thickness + params.axis_height.abs() / 4.0;
    let total_height = radicand_box.height + clearance + thickness;

    let tick = super::radical::tick_width(total_height);
    let width = tick + radicand_box.width;
    let radicand_width = radicand_box.width;
    let depth = radicand_box.depth;

    let sign = MathBox {
        width,
        height: total_height,
        depth: 0.0,
        content: MathBoxContent::Rule(RuleKind::Radical {
            total_height,
            radicand_width,
            thickness,
        }),
    };

    let children = vec![
        PlacedBox {
            x_offset: 0.0,
            baseline_shift: 0.0,
            math_box: sign,
        },
        PlacedBox {
            x_offset: tick,
            baseline_shift: 0.0,
            math_box: radicand_box,
        },
    ];
    MathBox {
        width,
        height: total_height,
        depth,
        content: MathBoxContent::Hlist(children),
    }
}

/// `\left`/`\right`: the body keeps its own natural style (unlike a
/// radicand, a delimited body isn't cramped); each present delimiter is
/// sized to the body's full height+depth (plus a small clearance) and
/// centered on the math axis, which is what makes a tall stretched
/// paren look correctly balanced around the middle of its content rather
/// than around the baseline.
fn layout_delimited(
    open: Option<DelimiterKind>,
    body: &MathNode,
    close: Option<DelimiterKind>,
    style: MathStyle,
    font_size: f32,
    canvas: &mut Canvas,
) -> MathBox {
    let params = FontParams::for_size(font_size);
    let body_box = layout_math(body, style, font_size, canvas);
    let thickness = params.default_rule_thickness;

    let target_height = body_box.height + body_box.depth + 2.0 * thickness;
    let half = target_height / 2.0;
    let height_above_axis = half + params.axis_height;
    let depth_below_axis = (half - params.axis_height).max(0.0);

    let delimiter_box = |kind: DelimiterKind, is_open: bool| MathBox {
        width: super::delimiter::delimiter_width(target_height),
        height: height_above_axis,
        depth: depth_below_axis,
        content: MathBoxContent::Rule(RuleKind::Delimiter {
            kind,
            thickness,
            is_open,
        }),
    };

    let mut children = Vec::with_capacity(3);
    let mut x = 0.0f32;
    let mut height = body_box.height;
    let mut depth = body_box.depth;

    if let Some(kind) = open {
        let b = delimiter_box(kind, true);
        height = height.max(b.height);
        depth = depth.max(b.depth);
        x += b.width;
        children.push(PlacedBox {
            x_offset: 0.0,
            baseline_shift: 0.0,
            math_box: b,
        });
    }
    let body_width = body_box.width;
    children.push(PlacedBox {
        x_offset: x,
        baseline_shift: 0.0,
        math_box: body_box,
    });
    x += body_width;
    if let Some(kind) = close {
        let b = delimiter_box(kind, false);
        height = height.max(b.height);
        depth = depth.max(b.depth);
        children.push(PlacedBox {
            x_offset: x,
            baseline_shift: 0.0,
            math_box: b,
        });
        x += super::delimiter::delimiter_width(target_height);
    }

    MathBox {
        width: x,
        height,
        depth,
        content: MathBoxContent::Hlist(children),
    }
}

/// Rule 13: big-operator limits. In display style, `\sum`/`\prod`/etc.
/// (per [`BigOpKind::display_limits`] -- `\int`/`\oint` are the
/// exception) stack their limits directly above/below the operator
/// glyph, centered on its width. Every other case (text style, or
/// `\int`/`\oint` even in display style) falls back to ordinary corner
/// scripts, using the same placement math as Rule 18.
fn layout_bigop(
    kind: BigOpKind,
    lower: Option<&MathNode>,
    upper: Option<&MathNode>,
    style: MathStyle,
    font_size: f32,
    canvas: &mut Canvas,
) -> MathBox {
    let params = FontParams::for_size(font_size);
    let scaled_size = font_size * style.size_scale();
    let op_text = kind.glyph().to_string();
    let op_font = Font::sans_serif();
    canvas.font(op_font.clone());
    canvas.font_size(scaled_size);
    let op_width = canvas.text_width(&op_text);
    let op_metrics = canvas.font_metrics();
    let op_box = MathBox {
        width: op_width,
        height: op_metrics.ascent,
        depth: op_metrics.descent,
        content: MathBoxContent::Glyphs {
            text: op_text,
            font: op_font,
            font_size: scaled_size,
        },
    };

    if style.is_display() && kind.display_limits() {
        layout_bigop_stacked(op_box, lower, upper, style, font_size, &params, canvas)
    } else {
        layout_bigop_corner_scripts(op_box, lower, upper, style, font_size, &params, canvas)
    }
}

fn layout_bigop_stacked(
    op_box: MathBox,
    lower: Option<&MathNode>,
    upper: Option<&MathNode>,
    style: MathStyle,
    font_size: f32,
    params: &FontParams,
    canvas: &mut Canvas,
) -> MathBox {
    let upper_box = upper.map(|u| layout_math(u, style.sup_style(), font_size, canvas));
    let lower_box = lower.map(|l| layout_math(l, style.sub_style(), font_size, canvas));

    let width = op_box
        .width
        .max(upper_box.as_ref().map_or(0.0, |b| b.width))
        .max(lower_box.as_ref().map_or(0.0, |b| b.width));

    let mut children = Vec::with_capacity(3);
    let mut height = op_box.height;
    let mut depth = op_box.depth;
    let op_width = op_box.width;
    let op_height = op_box.height;
    let op_depth = op_box.depth;

    if let Some(upper_box) = upper_box {
        let shift = op_height + params.big_op_spacing1 + upper_box.depth;
        height = height.max(shift + upper_box.height);
        let x = (width - upper_box.width) / 2.0;
        children.push(PlacedBox {
            x_offset: x,
            baseline_shift: -shift,
            math_box: upper_box,
        });
    }
    children.push(PlacedBox {
        x_offset: (width - op_width) / 2.0,
        baseline_shift: 0.0,
        math_box: op_box,
    });
    if let Some(lower_box) = lower_box {
        let shift = op_depth + params.big_op_spacing2 + lower_box.height;
        depth = depth.max(shift + lower_box.depth);
        let x = (width - lower_box.width) / 2.0;
        children.push(PlacedBox {
            x_offset: x,
            baseline_shift: shift,
            math_box: lower_box,
        });
    }

    MathBox {
        width,
        height,
        depth,
        content: MathBoxContent::Hlist(children),
    }
}

/// Corner-script placement (text style, or `\int`/`\oint` even in
/// display style) -- the same Rule-18 shift/clearance math
/// `layout_script` uses, applied to the operator glyph as the base
/// instead of a general `MathNode`.
fn layout_bigop_corner_scripts(
    op_box: MathBox,
    lower: Option<&MathNode>,
    upper: Option<&MathNode>,
    style: MathStyle,
    font_size: f32,
    params: &FontParams,
    canvas: &mut Canvas,
) -> MathBox {
    let sup_box = upper.map(|u| layout_math(u, style.sup_style(), font_size, canvas));
    let sub_box = lower.map(|l| layout_math(l, style.sub_style(), font_size, canvas));

    let mut sup_shift = 0.0f32;
    if let Some(sup_box) = &sup_box {
        let base_shift = if style.is_cramped() {
            params.sup3
        } else if style.is_display() {
            params.sup1
        } else {
            params.sup2
        };
        sup_shift = base_shift.max(op_box.height - params.sup_drop);
    }
    let mut sub_shift = 0.0f32;
    if let Some(sub_box) = &sub_box {
        let base_shift = if style.is_display() {
            params.sub1
        } else {
            params.sub2
        };
        sub_shift = base_shift
            .max(op_box.depth + params.sub_drop)
            .max(sub_box.height);
    }

    let script_width = match (&sup_box, &sub_box) {
        (Some(s), Some(b)) => s.width.max(b.width),
        (Some(s), None) => s.width,
        (None, Some(b)) => b.width,
        (None, None) => 0.0,
    };

    let mut height = op_box.height;
    let mut depth = op_box.depth;
    let op_width = op_box.width;
    let mut children = vec![PlacedBox {
        x_offset: 0.0,
        baseline_shift: 0.0,
        math_box: op_box,
    }];

    if let Some(sup_box) = sup_box {
        height = height.max(sup_shift + sup_box.height);
        children.push(PlacedBox {
            x_offset: op_width,
            baseline_shift: -sup_shift,
            math_box: sup_box,
        });
    }
    if let Some(sub_box) = sub_box {
        depth = depth.max(sub_shift + sub_box.depth);
        children.push(PlacedBox {
            x_offset: op_width,
            baseline_shift: sub_shift,
            math_box: sub_box,
        });
    }

    MathBox {
        width: op_width + script_width,
        height,
        depth,
        content: MathBoxContent::Hlist(children),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::support::math::ast::AtomClass;

    fn sym(glyph: char) -> MathNode {
        MathNode::Symbol {
            glyph,
            class: AtomClass::Ord,
        }
    }

    #[test]
    fn a_single_symbol_has_positive_width_and_uses_real_font_metrics() {
        let mut canvas = Canvas::new(200, 200).unwrap();
        let math_box = layout_math(&sym('x'), MathStyle::Text, 20.0, &mut canvas);
        assert!(math_box.width > 0.0);
        assert!(
            math_box.height > 0.0,
            "expected a positive ascent from real font metrics"
        );
    }

    #[test]
    fn a_row_concatenates_children_left_to_right_with_gaps() {
        let mut canvas = Canvas::new(200, 200).unwrap();
        let row = MathNode::Row(vec![sym('x'), sym('y')]);
        let math_box = layout_math(&row, MathStyle::Text, 20.0, &mut canvas);
        let MathBoxContent::Hlist(children) = &math_box.content else {
            panic!("expected Hlist")
        };
        assert_eq!(children.len(), 2);
        assert!(
            children[1].x_offset > children[0].math_box.width,
            "second child should start after the first plus a gap"
        );
        assert!(
            (math_box.width - (children[1].x_offset + children[1].math_box.width)).abs() < 1e-3
        );
    }

    #[test]
    fn superscript_is_raised_above_the_baseline() {
        let mut canvas = Canvas::new(200, 200).unwrap();
        let node = MathNode::Script {
            base: Box::new(sym('x')),
            sup: Some(Box::new(sym('2'))),
            sub: None,
        };
        let math_box = layout_math(&node, MathStyle::Text, 20.0, &mut canvas);
        let MathBoxContent::Hlist(children) = &math_box.content else {
            panic!("expected Hlist")
        };
        assert_eq!(children.len(), 2);
        assert!(
            children[1].baseline_shift < 0.0,
            "superscript should be raised (negative baseline_shift), got {}",
            children[1].baseline_shift
        );
    }

    #[test]
    fn subscript_is_lowered_below_the_baseline() {
        let mut canvas = Canvas::new(200, 200).unwrap();
        let node = MathNode::Script {
            base: Box::new(sym('x')),
            sup: None,
            sub: Some(Box::new(sym('i'))),
        };
        let math_box = layout_math(&node, MathStyle::Text, 20.0, &mut canvas);
        let MathBoxContent::Hlist(children) = &math_box.content else {
            panic!("expected Hlist")
        };
        assert_eq!(children.len(), 2);
        assert!(
            children[1].baseline_shift > 0.0,
            "subscript should be lowered (positive baseline_shift), got {}",
            children[1].baseline_shift
        );
    }

    #[test]
    fn both_scripts_at_once_are_placed_at_the_same_x_offset() {
        let mut canvas = Canvas::new(200, 200).unwrap();
        let node = MathNode::Script {
            base: Box::new(sym('x')),
            sup: Some(Box::new(sym('2'))),
            sub: Some(Box::new(sym('i'))),
        };
        let math_box = layout_math(&node, MathStyle::Text, 20.0, &mut canvas);
        let MathBoxContent::Hlist(children) = &math_box.content else {
            panic!("expected Hlist")
        };
        assert_eq!(children.len(), 3, "expected [base, sup, sub]");
        assert_eq!(
            children[1].x_offset, children[2].x_offset,
            "sup and sub should stack at the same x offset"
        );
        assert!(children[1].baseline_shift < 0.0);
        assert!(children[2].baseline_shift > 0.0);
    }

    #[test]
    fn both_scripts_maintain_a_minimum_gap_between_them() {
        let mut canvas = Canvas::new(200, 200).unwrap();
        let node = MathNode::Script {
            base: Box::new(sym('x')),
            sup: Some(Box::new(sym('2'))),
            sub: Some(Box::new(sym('i'))),
        };
        let math_box = layout_math(&node, MathStyle::Text, 20.0, &mut canvas);
        let MathBoxContent::Hlist(children) = &math_box.content else {
            panic!("expected Hlist")
        };
        let sup = &children[1];
        let sub = &children[2];
        let params = FontParams::for_size(20.0);
        let gap =
            (-sup.baseline_shift - sup.math_box.depth) - (sub.math_box.height - sub.baseline_shift);
        assert!(
            gap >= 4.0 * params.default_rule_thickness - 1e-3,
            "expected at least the Rule-18 minimum gap between sup and sub, got {gap}"
        );
    }

    #[test]
    fn nested_scripts_use_a_smaller_style_and_shrink() {
        let mut canvas = Canvas::new(200, 200).unwrap();
        let plain = layout_math(&sym('x'), MathStyle::Text, 20.0, &mut canvas);
        let node = MathNode::Script {
            base: Box::new(sym('x')),
            sup: Some(Box::new(sym('2'))),
            sub: None,
        };
        let scripted = layout_math(&node, MathStyle::Text, 20.0, &mut canvas);
        let MathBoxContent::Hlist(children) = &scripted.content else {
            panic!("expected Hlist")
        };
        assert!(
            children[1].math_box.width < plain.width,
            "the superscript itself should be smaller than a full-size symbol"
        );
    }

    #[test]
    fn fraction_bar_sits_at_axis_height() {
        let mut canvas = Canvas::new(200, 200).unwrap();
        let node = MathNode::Frac {
            num: Box::new(sym('1')),
            den: Box::new(sym('2')),
        };
        let math_box = layout_math(&node, MathStyle::Display, 20.0, &mut canvas);
        let MathBoxContent::Hlist(children) = &math_box.content else {
            panic!("expected Hlist")
        };
        let bar = &children[1];
        assert!(matches!(
            bar.math_box.content,
            MathBoxContent::Rule(RuleKind::HorizontalBar { .. })
        ));
        let params = FontParams::for_size(20.0);
        assert!(
            (-bar.baseline_shift - params.axis_height).abs() < 1e-4,
            "expected the bar's baseline_shift to be exactly -axis_height, got {}",
            bar.baseline_shift
        );
    }

    #[test]
    fn fraction_enforces_minimum_display_style_gap() {
        let mut canvas = Canvas::new(200, 200).unwrap();
        // A row deep enough that the *ideal* (unadjusted) shifts alone
        // wouldn't leave the required 3x-rule-thickness display-style gap.
        let tall = MathNode::Script {
            base: Box::new(sym('x')),
            sup: Some(Box::new(sym('2'))),
            sub: Some(Box::new(sym('2'))),
        };
        let node = MathNode::Frac {
            num: Box::new(tall.clone()),
            den: Box::new(tall),
        };
        let math_box = layout_math(&node, MathStyle::Display, 20.0, &mut canvas);
        let MathBoxContent::Hlist(children) = &math_box.content else {
            panic!("expected Hlist")
        };
        let (num, bar, den) = (&children[0], &children[1], &children[2]);
        let thickness = FontParams::for_size(20.0).default_rule_thickness;
        let num_bottom = -num.baseline_shift - num.math_box.depth;
        let bar_top = -bar.baseline_shift + thickness / 2.0;
        let den_top = den.baseline_shift - den.math_box.height;
        let bar_bottom = -bar.baseline_shift - thickness / 2.0;
        assert!(
            num_bottom >= bar_top - 1e-3,
            "numerator should clear the bar, got num_bottom={num_bottom} bar_top={bar_top}"
        );
        assert!(
            den_top <= bar_bottom + 1e-3,
            "denominator should clear the bar, got den_top={den_top} bar_bottom={bar_bottom}"
        );
    }

    #[test]
    fn fraction_width_is_the_wider_of_numerator_and_denominator() {
        let mut canvas = Canvas::new(200, 200).unwrap();
        let node = MathNode::Frac {
            num: Box::new(sym('1')),
            den: Box::new(MathNode::Row(vec![sym('1'), sym('0'), sym('0')])),
        };
        let math_box = layout_math(&node, MathStyle::Text, 20.0, &mut canvas);
        let MathBoxContent::Hlist(children) = &math_box.content else {
            panic!("expected Hlist")
        };
        assert!(
            (math_box.width - children[2].math_box.width).abs() < 1e-3,
            "fraction width should match the wider denominator"
        );
    }

    #[test]
    fn radical_top_bar_clears_the_radicand_by_the_rule_11_gap() {
        let mut canvas = Canvas::new(200, 200).unwrap();
        let node = MathNode::Sqrt(Box::new(sym('x')));
        let math_box = layout_math(&node, MathStyle::Text, 20.0, &mut canvas);
        let MathBoxContent::Hlist(children) = &math_box.content else {
            panic!("expected Hlist")
        };
        let (sign, radicand) = (&children[0], &children[1]);
        let MathBoxContent::Rule(RuleKind::Radical {
            total_height,
            thickness,
            ..
        }) = &sign.math_box.content
        else {
            panic!("expected a Radical rule, got {:?}", sign.math_box.content)
        };
        let params = FontParams::for_size(20.0);
        let clearance = *total_height - radicand.math_box.height - thickness;
        let expected_clearance = params.default_rule_thickness + params.axis_height.abs() / 4.0;
        assert!(
            (clearance - expected_clearance).abs() < 1e-3,
            "expected the Rule-11 clearance gap, got {clearance}"
        );
    }

    #[test]
    fn radical_radicand_keeps_the_expressions_own_baseline() {
        let mut canvas = Canvas::new(200, 200).unwrap();
        let node = MathNode::Sqrt(Box::new(sym('x')));
        let math_box = layout_math(&node, MathStyle::Text, 20.0, &mut canvas);
        let MathBoxContent::Hlist(children) = &math_box.content else {
            panic!("expected Hlist")
        };
        assert_eq!(
            children[1].baseline_shift, 0.0,
            "the radicand should sit at the expression's own baseline, not be shifted"
        );
    }

    #[test]
    fn delimiters_grow_to_at_least_the_bodys_full_extent() {
        let mut canvas = Canvas::new(200, 200).unwrap();
        let tall_body = MathNode::Script {
            base: Box::new(sym('x')),
            sup: Some(Box::new(sym('2'))),
            sub: Some(Box::new(sym('2'))),
        };
        let node = MathNode::Delimited {
            open: Some(DelimiterKind::Paren),
            body: Box::new(tall_body),
            close: Some(DelimiterKind::Paren),
        };
        let math_box = layout_math(&node, MathStyle::Text, 20.0, &mut canvas);
        let MathBoxContent::Hlist(children) = &math_box.content else {
            panic!("expected Hlist")
        };
        assert_eq!(children.len(), 3, "expected [open, body, close]");
        let (open_delim, body, close_delim) = (&children[0], &children[1], &children[2]);
        assert!(matches!(
            open_delim.math_box.content,
            MathBoxContent::Rule(RuleKind::Delimiter {
                kind: DelimiterKind::Paren,
                is_open: _,
                ..
            })
        ));
        assert!(matches!(
            close_delim.math_box.content,
            MathBoxContent::Rule(RuleKind::Delimiter {
                kind: DelimiterKind::Paren,
                is_open: _,
                ..
            })
        ));
        assert!(
            open_delim.math_box.height + open_delim.math_box.depth
                >= body.math_box.height + body.math_box.depth,
            "delimiter should be at least as tall as the body it encloses"
        );
        assert!(
            close_delim.x_offset > body.x_offset,
            "close delimiter should be placed after the body"
        );
    }

    #[test]
    fn a_missing_side_produces_no_delimiter_box() {
        let mut canvas = Canvas::new(200, 200).unwrap();
        let node = MathNode::Delimited {
            open: None,
            body: Box::new(sym('x')),
            close: Some(DelimiterKind::Bracket),
        };
        let math_box = layout_math(&node, MathStyle::Text, 20.0, &mut canvas);
        let MathBoxContent::Hlist(children) = &math_box.content else {
            panic!("expected Hlist")
        };
        assert_eq!(
            children.len(),
            2,
            "expected [body, close] only, no open delimiter"
        );
    }

    #[test]
    fn sum_stacks_limits_in_display_style() {
        let mut canvas = Canvas::new(200, 200).unwrap();
        let node = MathNode::BigOp {
            kind: BigOpKind::Sum,
            lower: Some(Box::new(sym('1'))),
            upper: Some(Box::new(sym('n'))),
        };
        let math_box = layout_math(&node, MathStyle::Display, 20.0, &mut canvas);
        let MathBoxContent::Hlist(children) = &math_box.content else {
            panic!("expected Hlist")
        };
        assert_eq!(children.len(), 3, "expected [upper, op, lower] stacked");
        // The upper limit should be directly above the operator (roughly
        // centered, not off to the side), and the lower limit directly
        // below -- i.e. all three roughly share an x-extent, unlike
        // corner scripts which sit entirely to the operator's right.
        assert!(
            children[0].baseline_shift < 0.0,
            "upper limit should be raised above the operator"
        );
        assert!(
            children[2].baseline_shift > 0.0,
            "lower limit should be lowered below the operator"
        );
    }

    #[test]
    fn int_keeps_corner_scripts_even_in_display_style() {
        let mut canvas = Canvas::new(200, 200).unwrap();
        let node = MathNode::BigOp {
            kind: BigOpKind::Int,
            lower: Some(Box::new(sym('0'))),
            upper: Some(Box::new(sym('1'))),
        };
        let math_box = layout_math(&node, MathStyle::Display, 20.0, &mut canvas);
        let MathBoxContent::Hlist(children) = &math_box.content else {
            panic!("expected Hlist")
        };
        assert_eq!(
            children.len(),
            3,
            "expected [op, sup, sub] as corner scripts"
        );
        // Corner scripts both sit at the *same* x_offset, to the right of
        // the operator -- unlike the stacked case, they don't span the
        // operator's own width.
        assert_eq!(children[1].x_offset, children[2].x_offset);
        assert!(
            children[1].x_offset > 0.0,
            "corner scripts should be offset to the right of the operator glyph"
        );
    }

    #[test]
    fn sum_in_text_style_falls_back_to_corner_scripts() {
        let mut canvas = Canvas::new(200, 200).unwrap();
        let node = MathNode::BigOp {
            kind: BigOpKind::Sum,
            lower: Some(Box::new(sym('1'))),
            upper: Some(Box::new(sym('n'))),
        };
        let math_box = layout_math(&node, MathStyle::Text, 20.0, &mut canvas);
        let MathBoxContent::Hlist(children) = &math_box.content else {
            panic!("expected Hlist")
        };
        assert_eq!(
            children[1].x_offset, children[2].x_offset,
            "text-style limits should be corner scripts, not stacked"
        );
    }
}