katex-rs 0.2.4

A Rust implementation of KaTeX - Fast math typesetting for anywhere, more than just the web.
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
//! Delimiter handling for KaTeX
//!
//! This module provides functions for creating and sizing delimiters in
//! mathematical expressions, including parentheses, brackets, braces, and other
//! mathematical symbols. It handles the complex logic for choosing appropriate
//! delimiter sizes and styles based on the content they surround.

use alloc::borrow::Cow;

use phf::{Set, phf_set};

use crate::namespace::KeyMap;

use crate::build_common::{
    VListChild, VListElem, VListKern, VListParam, lookup_symbol, make_span, make_svg_span,
    make_symbol, make_v_list,
};
use crate::dom_tree::{DomSpan, HtmlDomNode, PathNode, SvgChildNode, SvgNode, SymbolNode};
use crate::options::Options;
use crate::style::{SCRIPT, SCRIPTSCRIPT, Style, TEXT};
use crate::svg_geometry::{inner_path, sqrt_path, tall_delim};
use crate::symbols::Mode;
use crate::types::ClassList;
use crate::types::{CssProperty, ParseError, ParseErrorKind};
use crate::units::make_em;
use crate::{CharacterMetrics, KatexContext};

const SIZE_TO_MAX_HEIGHT: [f64; 5] = [0.0, 1.2, 1.8, 2.4, 3.0];

/// Size to max height mapping for delimiter sizing
pub fn size_to_max_height<T>(size: T) -> f64
where
    T: Into<usize>,
{
    let size = size.into().min(SIZE_TO_MAX_HEIGHT.len() - 1); // Clamp size
    SIZE_TO_MAX_HEIGHT[size]
}

/// Padding above the surd vinculum in SVG units
const VB_PAD: f64 = 80.0;

/// Padding above the surd in em units
const EM_PAD: f64 = 0.08;

/// Lap overlap in ems for stacked delimiters
const LAP_IN_EMS: f64 = 0.008;

/// Delimiter types for sizing sequences
#[derive(Debug, Clone)]
pub enum DelimiterType {
    /// Small delimiter using a specific style
    Small(&'static Style),
    /// Large delimiter using a specific size index
    Large(usize),
    /// Stacked delimiter for very tall expressions
    Stack,
}

/// Result of delimiter sizing traversal
#[derive(Debug)]
pub struct DelimiterSizing {
    /// The type of delimiter to use
    pub delimiter_type: DelimiterType,
    /// The calculated height of the delimiter
    pub height: f64,
}

/// Result of sqrt image creation
#[derive(Debug)]
pub struct SqrtImageResult {
    /// The span containing the SVG image of the sqrt symbol
    pub span: DomSpan,
    /// The width of the rule (vinculum) in ems
    pub rule_width: f64,
    /// The advance width of the sqrt symbol in ems
    pub advance_width: f64,
}

/// Get the metrics for a given symbol and font, after transformation
fn get_metrics(
    ctx: &KatexContext,
    symbol: &str,
    font: &str,
    mode: Mode,
) -> Result<CharacterMetrics, ParseError> {
    let replace = ctx.symbols.get_math(symbol).and_then(|s| s.replace);
    let mut buf = [0u8; 4];
    let symbol = replace.map_or(symbol, |s| s.encode_utf8(&mut buf));
    let look_up = lookup_symbol(ctx, symbol, font, mode)?;

    if let Some(look_up) = look_up
        && let Some(metrics) = look_up.metrics
    {
        Ok(metrics)
    } else {
        Err(ParseError::new(ParseErrorKind::UnsupportedSymbolFont {
            symbol: symbol.to_owned(),
            font: font.to_owned(),
        }))
    }
}

/// Puts a delimiter span in a given style, and adds appropriate height, depth,
/// and maxFontSizes.
fn style_wrap(
    delim: HtmlDomNode,
    to_style: &'static Style,
    options: &Options,
    classes: ClassList,
) -> DomSpan {
    let new_options = options.having_base_style(Some(to_style));
    let mut combined_classes = classes;
    combined_classes.extend(new_options.sizing_classes(options));
    let mut span = make_span(combined_classes, vec![delim], Some(options), None);

    let multiplier = new_options.size_multiplier / options.size_multiplier;
    span.height *= multiplier;
    span.depth *= multiplier;
    span.max_font_size = new_options.size_multiplier;

    span
}

/// Makes a small delimiter. This is a delimiter that comes in the Main-Regular
/// font, but is restyled to either be in textstyle, scriptstyle, or
/// scriptscriptstyle.
pub fn make_small_delim<T: Into<ClassList>>(
    ctx: &KatexContext,
    delim: &str,
    style: &'static Style,
    center: bool,
    options: &Options,
    mode: Mode,
    classes: T,
) -> Result<DomSpan, ParseError> {
    let classes = classes.into();
    let text = make_symbol(
        ctx,
        delim,
        "Main-Regular",
        mode,
        Some(options),
        classes.clone(),
    )?;
    let mut span = style_wrap(text.into(), style, options, classes);

    if center {
        span = center_span(span, options, TEXT);
    }

    Ok(span)
}

/// Builds a symbol in the given font size (note size is an integer)
fn mathrm_size(
    ctx: &KatexContext,
    value: &str,
    size: usize,
    mode: Mode,
    options: &Options,
) -> Result<SymbolNode, ParseError> {
    let font_name = format!("Size{size}-Regular");
    make_symbol(
        ctx,
        value,
        &font_name,
        mode,
        Some(options),
        ClassList::Empty,
    )
}

/// Makes a large delimiter. This is a delimiter that comes in the Size1, Size2,
/// Size3, or Size4 fonts. It is always rendered in textstyle.
pub fn make_large_delim<T: Into<ClassList>>(
    ctx: &KatexContext,
    delim: &str,
    size: usize,
    center: bool,
    options: &Options,
    mode: Mode,
    classes: T,
) -> Result<DomSpan, ParseError> {
    let classes = classes.into();
    let inner = mathrm_size(ctx, delim, size, mode, options)?;
    let mut span = style_wrap(
        make_span(
            vec![
                Cow::Borrowed("delimsizing"),
                Cow::Owned(format!("size{size}")),
            ],
            vec![inner.into()],
            Some(options),
            None,
        )
        .into(),
        TEXT,
        options,
        classes,
    );

    if center {
        span = center_span(span, options, TEXT);
    }

    Ok(span)
}

/// Center a span around the axis
fn center_span(mut span: DomSpan, options: &Options, style: &'static Style) -> DomSpan {
    let new_options = options.having_base_style(Some(style));
    let shift = (1.0 - options.size_multiplier / new_options.size_multiplier)
        * options.font_metrics().axis_height;

    // Apply the shift to center the span around the axis
    // In KaTeX.js, this modifies the span's style to apply vertical positioning
    // We'll create a modified span with the vertical shift applied
    span.classes.push("delimcenter");
    span.height -= shift;
    span.depth += shift;
    span.style.insert(CssProperty::Top, make_em(shift));
    span
}

/// Make a span from a font glyph with the given offset and in the given font.
/// This is used in makeStackedDelim to make the stacking pieces for the
/// delimiter.
fn make_glyph_span(
    ctx: &KatexContext,
    symbol: &str,
    font: &str,
    mode: Mode,
) -> Result<VListChild, ParseError> {
    let size_classes = if font == "Size1-Regular" {
        &["delimsizinginner", "delim-size1"]
    } else {
        &["delimsizinginner", "delim-size4"]
    };

    let corner = make_span(
        ClassList::Const(size_classes),
        vec![
            make_span(
                ClassList::Empty,
                vec![make_symbol(ctx, symbol, font, mode, None, ClassList::Empty)?.into()],
                None,
                None,
            )
            .into(),
        ],
        None,
        None,
    );

    // Wrap in the appropriate tag that VList uses
    Ok(VListElem::builder().elem(corner.into()).build().into())
}

/// Create a span with inline SVG for the inner part of a tall stacked
/// delimiter.
fn make_inner(
    ctx: &KatexContext,
    ch: &str,
    height: f64,
    options: &Options,
) -> Result<VListChild, ParseError> {
    let Some(first_char) = ch.chars().next() else {
        return Err(ParseError::new(ParseErrorKind::EmptyDelimiterCharacter));
    };
    // Get font metrics data

    let width = if let Some(metric) = ctx
        .font_metrics
        .get_metric("Size4-Regular", first_char as u32)?
    {
        metric.width
    } else {
        ctx.font_metrics
            .get_metric("Size1-Regular", first_char as u32)?
            .map_or(0.0, |metrics| metrics.width)
    };

    let path = PathNode {
        path_name: "inner".to_owned(),
        alternate: Some(inner_path(ch, (1000.0 * height).round())),
    };

    let mut svg_attributes = KeyMap::default();
    svg_attributes.extend([
        ("width".to_owned(), make_em(width)),
        ("height".to_owned(), make_em(height)),
        ("style".to_owned(), format!("width:{}", make_em(width))),
        (
            "viewBox".to_owned(),
            format!("0 0 {} {}", (1000.0 * width), (1000.0 * height).round()),
        ),
        ("preserveAspectRatio".to_owned(), "xMinYMin".to_owned()),
    ]);

    let svg_children = vec![SvgChildNode::Path(path)];

    let svg_node = SvgNode::builder()
        .children(svg_children)
        .attributes(svg_attributes)
        .build();
    let mut span = make_svg_span(ClassList::Empty, vec![svg_node], options);

    // Set span properties to match JavaScript version
    span.height = height;
    span.style.insert(CssProperty::Height, make_em(height));
    span.style.insert(CssProperty::Width, make_em(width));

    Ok(VListElem::builder().elem(span.into()).build().into())
}

/// Make a stacked delimiter out of a given delimiter, with the total height at
/// least `height_total`. This routine is mentioned on page 442 of the TeXbook.
pub fn make_stacked_delim<T: Into<ClassList>>(
    ctx: &KatexContext,
    delim: &str,
    height_total: f64,
    center: bool,
    options: &Options,
    mode: Mode,
    classes: T,
) -> Result<DomSpan, ParseError> {
    let classes = classes.into();
    // There are four parts, the top, an optional middle, a repeated part, and a
    // bottom.
    let mut top = delim.to_owned();
    let mut middle = None;
    let mut repeat = delim.to_owned();
    let mut bottom = delim.to_owned();
    let mut svg_label = String::new();
    let mut view_box_width = 0.0;
    let mut font = "Size1-Regular".to_owned();

    // Set the parts and font based on the symbol
    match delim {
        "\\uparrow" => {
            "\u{23d0}".clone_into(&mut repeat);
            "\u{23d0}".clone_into(&mut bottom);
        }
        "\\Uparrow" => {
            "\u{2016}".clone_into(&mut repeat);
            "\u{2016}".clone_into(&mut bottom);
        }
        "\\downarrow" => {
            "\u{23d0}".clone_into(&mut top);
            "\u{23d0}".clone_into(&mut repeat);
        }
        "\\Downarrow" => {
            "\u{2016}".clone_into(&mut top);
            "\u{2016}".clone_into(&mut repeat);
        }
        "\\updownarrow" => {
            "\\uparrow".clone_into(&mut top);
            "\u{23d0}".clone_into(&mut repeat);
            "\\downarrow".clone_into(&mut bottom);
        }
        "\\Updownarrow" => {
            "\\Uparrow".clone_into(&mut top);
            "\u{2016}".clone_into(&mut repeat);
            "\\Downarrow".clone_into(&mut bottom);
        }
        "|" | "\\lvert" | "\\rvert" | "\\vert" => {
            "\u{2223}".clone_into(&mut repeat);
            "vert".clone_into(&mut svg_label);
            view_box_width = 333.0;
        }
        "\\|" | "\\lVert" | "\\rVert" | "\\Vert" => {
            "\u{2225}".clone_into(&mut repeat);
            "doublevert".clone_into(&mut svg_label);
            view_box_width = 556.0;
        }
        "[" | "\\lbrack" => {
            "\u{23a1}".clone_into(&mut top);
            "\u{23a2}".clone_into(&mut repeat);
            "\u{23a3}".clone_into(&mut bottom);
            "Size4-Regular".clone_into(&mut font);
            "lbrack".clone_into(&mut svg_label);
            view_box_width = 667.0;
        }
        "]" | "\\rbrack" => {
            "\u{23a4}".clone_into(&mut top);
            "\u{23a5}".clone_into(&mut repeat);
            "\u{23a6}".clone_into(&mut bottom);
            "Size4-Regular".clone_into(&mut font);
            "rbrack".clone_into(&mut svg_label);
            view_box_width = 667.0;
        }
        "\\lfloor" | "\u{230a}" => {
            "\u{23a2}".clone_into(&mut top);
            "\u{23a2}".clone_into(&mut repeat);
            "\u{23a3}".clone_into(&mut bottom);
            "Size4-Regular".clone_into(&mut font);
            "lfloor".clone_into(&mut svg_label);
            view_box_width = 667.0;
        }
        "\\lceil" | "\u{2308}" => {
            "\u{23a1}".clone_into(&mut top);
            "\u{23a2}".clone_into(&mut repeat);
            "\u{23a2}".clone_into(&mut bottom);
            "Size4-Regular".clone_into(&mut font);
            "lceil".clone_into(&mut svg_label);
            view_box_width = 667.0;
        }
        "\\rfloor" | "\u{230b}" => {
            "\u{23a5}".clone_into(&mut top);
            "\u{23a5}".clone_into(&mut repeat);
            "\u{23a6}".clone_into(&mut bottom);
            "Size4-Regular".clone_into(&mut font);
            "rfloor".clone_into(&mut svg_label);
            view_box_width = 667.0;
        }
        "\\rceil" | "\u{2309}" => {
            "\u{23a4}".clone_into(&mut top);
            "\u{23a5}".clone_into(&mut repeat);
            "\u{23a5}".clone_into(&mut bottom);
            "Size4-Regular".clone_into(&mut font);
            "rceil".clone_into(&mut svg_label);
            view_box_width = 667.0;
        }
        "(" | "\\lparen" => {
            "\u{239b}".clone_into(&mut top);
            "\u{239c}".clone_into(&mut repeat);
            "\u{239d}".clone_into(&mut bottom);
            "Size4-Regular".clone_into(&mut font);
            "lparen".clone_into(&mut svg_label);
            view_box_width = 875.0;
        }
        ")" | "\\rparen" => {
            "\u{239e}".clone_into(&mut top);
            "\u{239f}".clone_into(&mut repeat);
            "\u{23a0}".clone_into(&mut bottom);
            "Size4-Regular".clone_into(&mut font);
            "rparen".clone_into(&mut svg_label);
            view_box_width = 875.0;
        }
        "\\{" | "\\lbrace" => {
            "\u{23a7}".clone_into(&mut top);
            middle = Some("\u{23a8}".to_owned());
            "\u{23a9}".clone_into(&mut bottom);
            "\u{23aa}".clone_into(&mut repeat);
            "Size4-Regular".clone_into(&mut font);
        }
        "\\}" | "\\rbrace" => {
            "\u{23ab}".clone_into(&mut top);
            middle = Some("\u{23ac}".to_owned());
            "\u{23ad}".clone_into(&mut bottom);
            "\u{23aa}".clone_into(&mut repeat);
            "Size4-Regular".clone_into(&mut font);
        }
        "\\lgroup" | "\u{27ee}" => {
            "\u{23a7}".clone_into(&mut top);
            "\u{23a9}".clone_into(&mut bottom);
            "\u{23aa}".clone_into(&mut repeat);
            "Size4-Regular".clone_into(&mut font);
        }
        "\\rgroup" | "\u{27ef}" => {
            "\u{23ab}".clone_into(&mut top);
            "\u{23ad}".clone_into(&mut bottom);
            "\u{23aa}".clone_into(&mut repeat);
            "Size4-Regular".clone_into(&mut font);
        }
        "\\lmoustache" | "\u{23b0}" => {
            "\u{23a7}".clone_into(&mut top);
            "\u{23ad}".clone_into(&mut bottom);
            "\u{23aa}".clone_into(&mut repeat);
            "Size4-Regular".clone_into(&mut font);
        }
        "\\rmoustache" | "\u{23b1}" => {
            "\u{23ab}".clone_into(&mut top);
            "\u{23a9}".clone_into(&mut bottom);
            "\u{23aa}".clone_into(&mut repeat);
            "Size4-Regular".clone_into(&mut font);
        }
        _ => {}
    }

    // Get the metrics of the four sections
    let top_metrics = get_metrics(ctx, &top, &font, mode)?;
    let top_height_total = top_metrics.height + top_metrics.depth;

    let repeat_metrics = get_metrics(ctx, &repeat, &font, mode)?;
    let repeat_height_total = repeat_metrics.height + repeat_metrics.depth;

    let bottom_metrics = get_metrics(ctx, &bottom, &font, mode)?;
    let bottom_height_total = bottom_metrics.height + bottom_metrics.depth;

    let mut middle_height_total = 0.0;
    let middle_factor = if let Some(middle_sym) = &middle {
        let middle_metrics = get_metrics(ctx, middle_sym, &font, mode)?;
        middle_height_total = middle_metrics.height + middle_metrics.depth;
        2.0 // repeat symmetrically above and below middle
    } else {
        1.0
    };

    // Calculate the minimal height that the delimiter can have.
    let minimal_height = top_height_total + bottom_height_total + middle_height_total;

    let delta = (height_total - minimal_height) / (middle_factor * repeat_height_total);
    let repeat_count = delta.ceil().max(0.0) as i32;

    // Compute the total height of the delimiter including all the symbols
    let real_height_total =
        (f64::from(repeat_count) * middle_factor).mul_add(repeat_height_total, minimal_height);

    // Calculate the depth
    let axis_height = options.font_metrics().axis_height;
    let adjusted_axis = if center {
        axis_height * options.size_multiplier
    } else {
        axis_height
    };
    let depth = real_height_total / 2.0 - adjusted_axis;

    // Now, we start building the pieces that will go into the vlist
    let mut stack: Vec<VListChild> = Vec::new();

    if svg_label.is_empty() {
        // Stack glyphs
        // Start by adding the bottom symbol
        stack.push(make_glyph_span(ctx, &bottom, &font, mode)?);
        stack.push(VListChild::Kern(VListKern { size: -LAP_IN_EMS })); // overlap

        if middle.is_none() {
            // The middle section will be an SVG. Make it an extra 0.016em tall.
            let inner_height = 2.0f64.mul_add(
                LAP_IN_EMS,
                real_height_total - top_height_total - bottom_height_total,
            );
            stack.push(make_inner(ctx, &repeat, inner_height, options)?);
        } else {
            // When there is a middle bit, we need the middle part and two repeated sections
            let inner_height = 2.0f64.mul_add(
                LAP_IN_EMS,
                (real_height_total - top_height_total - bottom_height_total - middle_height_total)
                    / 2.0,
            );
            stack.push(make_inner(ctx, &repeat, inner_height, options)?);
            // Now insert the middle of the brace.
            stack.push(VListChild::Kern(VListKern { size: -LAP_IN_EMS }));
            if let Some(middle_sym) = &middle {
                stack.push(make_glyph_span(ctx, middle_sym, &font, mode)?);
            }
            stack.push(VListChild::Kern(VListKern { size: -LAP_IN_EMS }));
            stack.push(make_inner(ctx, &repeat, inner_height, options)?);
        }

        // Add the top symbol
        stack.push(VListChild::Kern(VListKern { size: -LAP_IN_EMS }));
        stack.push(make_glyph_span(ctx, &top, &font, mode)?);
    } else {
        // Instead of stacking glyphs, create a single SVG.
        let mid_height = real_height_total - top_height_total - bottom_height_total;
        let view_box_height = (real_height_total * 1000.0).round();
        let path_str = tall_delim(&svg_label, (mid_height * 1000.0).round())?;
        let path = PathNode {
            path_name: svg_label,
            alternate: Some(path_str),
        };
        // const width = (viewBoxWidth / 1000).toFixed(3) + "em";
        let width = format!("{:.3}em", view_box_width / 1000.0);
        let height = format!("{:.3}em", view_box_height / 1000.0);

        let mut svg_attributes = KeyMap::default();
        svg_attributes.extend([
            ("width".to_owned(), width.clone()),
            ("height".to_owned(), height.clone()),
            (
                "viewBox".to_owned(),
                format!("0 0 {view_box_width} {view_box_height}"),
            ),
        ]);
        let svg_node = SvgNode::builder()
            .children(vec![SvgChildNode::Path(path)])
            .attributes(svg_attributes)
            .build();
        let mut wrapper = make_svg_span(ClassList::Empty, vec![svg_node], options);
        wrapper.height = view_box_height / 1000.0;
        wrapper.style.insert(CssProperty::Width, width);
        wrapper.style.insert(CssProperty::Height, height);
        stack.push(VListElem::builder().elem(wrapper.into()).build().into());
    }

    // Finally, build the vlist
    let new_options = options.having_base_style(Some(TEXT));
    let inner = make_v_list(
        VListParam::Bottom {
            position_data: depth,
            children: stack,
        },
        &new_options,
    )?;

    let span = style_wrap(
        make_span(
            ClassList::Const(&["delimsizing", "mult"]),
            vec![inner.into()],
            Some(&new_options),
            None,
        )
        .into(),
        TEXT,
        options,
        classes,
    );

    Ok(span)
}

/// Make a sqrt image of the given height.
pub fn make_sqrt_image(
    ctx: &KatexContext,
    height: f64,
    options: &Options,
) -> Result<SqrtImageResult, ParseError> {
    // Define a newOptions that removes the effect of size changes such as \Huge.
    let new_options = options.having_base_sizing();

    // Pick the desired surd glyph from a sequence of surds.
    let delimiter_type = traverse_sequence(
        ctx,
        "\\surd",
        height * new_options.size_multiplier,
        STACK_LARGE_DELIMITER_SEQUENCE,
        &new_options,
    )?;

    let mut size_multiplier = new_options.size_multiplier;

    // The standard sqrt SVGs each have a 0.04em thick vinculum.
    let extra_vinculum =
        (options.min_rule_thickness - options.font_metrics().sqrt_rule_thickness).max(0.0);

    // Create a span containing an SVG image of a sqrt symbol.
    let span_height;
    let tex_height;
    let view_box_height;
    let advance_width;

    let mut span = match delimiter_type {
        DelimiterType::Small(_style) => {
            // Get an SVG that is derived from glyph U+221A in font KaTeX-Main.
            view_box_height = 1000.0f64.mul_add(extra_vinculum, 1000.0) + VB_PAD;
            if height < 1.0 {
                size_multiplier = 1.0; // mimic a \textfont radical
            } else if height < 1.4 {
                size_multiplier = 0.7; // mimic a \scriptfont radical
            }
            span_height = (1.0 + extra_vinculum + EM_PAD) / size_multiplier;
            tex_height = (1.0 + extra_vinculum) / size_multiplier;
            let span = sqrt_svg(
                "sqrtMain",
                span_height,
                view_box_height as i32,
                extra_vinculum,
                options,
            );

            advance_width = 0.833 / size_multiplier; // from the font.

            span
        }
        DelimiterType::Large(size) => {
            // These SVGs come from fonts: KaTeX_Size1, _Size2, etc.
            view_box_height = (1000.0 + VB_PAD) * SIZE_TO_MAX_HEIGHT[*size];
            tex_height = (SIZE_TO_MAX_HEIGHT[*size] + extra_vinculum) / size_multiplier;
            span_height = (SIZE_TO_MAX_HEIGHT[*size] + extra_vinculum + EM_PAD) / size_multiplier;
            let mut span = sqrt_svg(
                &format!("sqrtSize{size}"),
                span_height,
                view_box_height as i32,
                extra_vinculum,
                options,
            );
            span.style
                .insert(CssProperty::MinWidth, "1.02em".to_owned());
            advance_width = 1.0 / size_multiplier; // 1.0 from the font.

            span
        }
        DelimiterType::Stack => {
            // Tall sqrt. In TeX, this would be stacked using multiple glyphs.
            span_height = height + extra_vinculum + EM_PAD;
            tex_height = height + extra_vinculum;
            view_box_height = 1000.0f64.mul_add(height, extra_vinculum).round();
            let mut span = sqrt_svg(
                "sqrtTall",
                span_height,
                view_box_height as i32,
                extra_vinculum,
                options,
            );
            span.style
                .insert(CssProperty::MinWidth, "0.742em".to_owned());
            advance_width = 1.056;
            span
        }
    };

    span.height = tex_height;
    span.style.insert(CssProperty::Height, make_em(span_height));
    // MinWidth is already set in sqrt_svg

    Ok(SqrtImageResult {
        span,
        rule_width: (options.font_metrics().sqrt_rule_thickness + extra_vinculum) * size_multiplier,
        advance_width,
    })
}

/// Generate SVG path for square root symbols
fn sqrt_svg(
    sqrt_name: &str,
    height: f64,
    view_box_height: i32,
    extra_vinculum: f64,
    options: &Options,
) -> DomSpan {
    let path = sqrt_path(
        sqrt_name,
        1000.0 * extra_vinculum,
        f64::from(view_box_height),
    );
    let path_node = PathNode {
        path_name: sqrt_name.to_owned(),
        alternate: Some(path),
    };

    let svg_children = vec![SvgChildNode::Path(path_node)];
    let mut svg_attributes = KeyMap::default();
    svg_attributes.extend([
        ("width".to_owned(), "400em".to_owned()),
        ("height".to_owned(), make_em(height)),
        (
            "viewBox".to_owned(),
            format!("0 0 400000 {view_box_height}"),
        ),
        (
            "preserveAspectRatio".to_owned(),
            "xMinYMin slice".to_owned(),
        ),
    ]);
    let svg_node = SvgNode::builder()
        .children(svg_children)
        .attributes(svg_attributes)
        .build();
    let mut svg = make_svg_span("hide-tail", vec![svg_node], options);

    // Set style properties
    svg.style
        .insert(CssProperty::MinWidth, "0.853em".to_owned());
    svg.style.insert(CssProperty::Height, make_em(height));

    svg
}

/// Used to create a delimiter of a specific size, where `size` is 1, 2, 3, or
/// 4.
pub fn sized_delim<T: Into<ClassList>>(
    ctx: &KatexContext,
    delim: &str,
    size: usize,
    options: &Options,
    mode: Mode,
    classes: T,
) -> Result<DomSpan, ParseError> {
    let classes = classes.into();
    // < and > turn into \langle and \rangle in delimiters
    let delim = match delim {
        "<" | "\\lt" | "\u{27e8}" => "\\langle",
        ">" | "\\gt" | "\u{27e9}" => "\\rangle",
        _ => delim,
    };

    // Sized delimiters are never centered.
    if STACK_LARGE_DELIMITERS.contains(delim) || STACK_NEVER_DELIMITERS.contains(delim) {
        make_large_delim(ctx, delim, size, false, options, mode, classes)
    } else if STACK_ALWAYS_DELIMITERS.contains(delim) {
        make_stacked_delim(
            ctx,
            delim,
            SIZE_TO_MAX_HEIGHT[size],
            false,
            options,
            mode,
            classes,
        )
    } else {
        Err(ParseError::new(ParseErrorKind::IllegalDelimiter {
            delim: delim.to_owned(),
        }))
    }
}

/// Traverse a sequence of types of delimiters to decide what kind of delimiter
/// should be used to create a delimiter of the given height+depth.
fn traverse_sequence<'a>(
    ctx: &KatexContext,
    delim: &str,
    height: f64,
    sequence: &'a [DelimiterType],
    options: &Options,
) -> Result<&'a DelimiterType, ParseError> {
    // Here, we choose the index we should start at in the sequences. In smaller
    // sizes (which correspond to larger numbers in style.size) we start earlier
    // in the sequence. Thus, scriptscript starts at index 3-3=0, script starts
    // at index 3-2=1, text starts at 3-1=2, and display starts at min(2,3-0)=2
    let start = (3 - options.style.size).min(2);
    for delim_type in sequence.iter().skip(start) {
        if matches!(delim_type, DelimiterType::Stack) {
            // This is always the last delimiter, so we just break the loop now.
            break;
        }

        let font = delim_type_to_font(delim_type);
        let metrics = get_metrics(ctx, delim, &font, Mode::Math)?;
        let mut height_depth = metrics.height + metrics.depth;

        // Small delimiters are scaled down versions of the same font, so we
        // account for the style change size.
        if let DelimiterType::Small(style) = &delim_type {
            let new_options = options.having_base_style(Some(style));
            height_depth *= new_options.size_multiplier;
        }

        // Check if the delimiter at this size works for the given height.
        if height_depth > height {
            return Ok(delim_type);
        }
    }

    // If we reached the end of the sequence, return the last sequence element.
    Ok(&sequence[sequence.len() - 1])
}

/// Get the font used in a delimiter based on what kind of delimiter it is.
fn delim_type_to_font(delimiter_type: &DelimiterType) -> String {
    match delimiter_type {
        DelimiterType::Small(_) => "Main-Regular".to_owned(),
        DelimiterType::Large(size) => format!("Size{size}-Regular"),
        DelimiterType::Stack => "Size4-Regular".to_owned(),
    }
}

/// Make a delimiter of a given height+depth, with optional centering. Here, we
/// traverse the sequences, and create a delimiter that the sequence tells us
/// to.
pub fn custom_sized_delim<T: Into<ClassList>>(
    ctx: &KatexContext,
    delim: &str,
    height: f64,
    center: bool,
    options: &Options,
    mode: Mode,
    classes: T,
) -> Result<DomSpan, ParseError> {
    let classes = classes.into();
    let delim = match delim {
        "<" | "\\lt" | "\u{27e8}" => "\\langle",
        ">" | "\\gt" | "\u{27e9}" => "\\rangle",
        _ => delim,
    };

    // Decide what sequence to use
    let sequence = if STACK_NEVER_DELIMITERS.contains(delim) {
        STACK_NEVER_DELIMITER_SEQUENCE
    } else if STACK_LARGE_DELIMITERS.contains(delim) {
        STACK_LARGE_DELIMITER_SEQUENCE
    } else {
        STACK_ALWAYS_DELIMITER_SEQUENCE
    };

    // Look through the sequence
    let delimiter_type = traverse_sequence(ctx, delim, height, sequence, options)?;

    // Get the delimiter from font glyphs.
    match delimiter_type {
        DelimiterType::Small(style) => {
            make_small_delim(ctx, delim, style, center, options, mode, classes)
        }
        DelimiterType::Large(size) => {
            make_large_delim(ctx, delim, *size, center, options, mode, classes)
        }
        DelimiterType::Stack => {
            make_stacked_delim(ctx, delim, height, center, options, mode, classes)
        }
    }
}

/// Make a delimiter for use with `\left` and `\right`, given a height and depth
/// of an expression that the delimiters surround.
pub fn left_right_delim<T: Into<ClassList>>(
    ctx: &KatexContext,
    delim: &str,
    height: f64,
    depth: f64,
    options: &Options,
    mode: Mode,
    classes: T,
) -> Result<DomSpan, ParseError> {
    let classes = classes.into();
    // We always center \left/\right delimiters, so the axis is always shifted
    let axis_height = options.font_metrics().axis_height * options.size_multiplier;

    // Taken from TeX source, tex.web, function make_left_right
    let delimiter_factor = 901.0;
    let delimiter_extend = 5.0 / options.font_metrics().pt_per_em;

    let max_dist_from_axis = (height - axis_height).max(depth + axis_height);

    let total_height = (max_dist_from_axis / 500.0 * delimiter_factor)
        .max(2.0f64.mul_add(max_dist_from_axis, -delimiter_extend));

    // Finally, we defer to `makeCustomSizedDelim` with our calculated total
    // height
    custom_sized_delim(ctx, delim, total_height, true, options, mode, classes)
}

// Delimiter classification arrays

const STACK_LARGE_DELIMITERS: Set<&str> = phf_set!(
    "(", "\\lparen", ")", "\\rparen", "[", "\\lbrack", "]", "\\rbrack", "\\{", "\\lbrace", "\\}",
    "\\rbrace", "\\lfloor", "\\rfloor", "\u{230a}", "\u{230b}", "\\lceil", "\\rceil", "\u{2308}",
    "\u{2309}", "\\surd",
);

const STACK_ALWAYS_DELIMITERS: Set<&str> = phf_set!(
    "\\uparrow",
    "\\downarrow",
    "\\updownarrow",
    "\\Uparrow",
    "\\Downarrow",
    "\\Updownarrow",
    "|",
    "\\|",
    "\\vert",
    "\\Vert",
    "\\lvert",
    "\\rvert",
    "\\lVert",
    "\\rVert",
    "\\lgroup",
    "\\rgroup",
    "\u{27ee}",
    "\u{27ef}",
    "\\lmoustache",
    "\\rmoustache",
    "\u{23b0}",
    "\u{23b1}",
);

const STACK_NEVER_DELIMITERS: Set<&str> = phf_set!(
    "<",
    ">",
    "\\langle",
    "\\rangle",
    "/",
    "\\backslash",
    "\\lt",
    "\\gt",
);

const STACK_NEVER_DELIMITER_SEQUENCE: &[DelimiterType] = &[
    DelimiterType::Small(SCRIPTSCRIPT),
    DelimiterType::Small(SCRIPT),
    DelimiterType::Small(TEXT),
    DelimiterType::Large(1),
    DelimiterType::Large(2),
    DelimiterType::Large(3),
    DelimiterType::Large(4),
];

const STACK_ALWAYS_DELIMITER_SEQUENCE: &[DelimiterType] = &[
    DelimiterType::Small(SCRIPTSCRIPT),
    DelimiterType::Small(SCRIPT),
    DelimiterType::Small(TEXT),
    DelimiterType::Stack,
];

const STACK_LARGE_DELIMITER_SEQUENCE: &[DelimiterType] = &[
    DelimiterType::Small(SCRIPTSCRIPT),
    DelimiterType::Small(SCRIPT),
    DelimiterType::Small(TEXT),
    DelimiterType::Large(1),
    DelimiterType::Large(2),
    DelimiterType::Large(3),
    DelimiterType::Large(4),
    DelimiterType::Stack,
];