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
//! Superscript and subscript function implementations for KaTeX Rust
//!
//! This module handles the rendering of superscript and subscript expressions,
//! following TeXbook rules 18(a-f) for precise positioning and styling.
//!
//! Migrated from KaTeX's supsub.js.

use crate::build_common::{
    VListChild, VListElem, VListElemAndShift, VListParam, make_span, make_v_list,
};
use crate::build_html::{DomType, Side};
use crate::define_function::HtmlBuilder;
use crate::dom_tree::HtmlDomNode;
use crate::functions::{accent, horiz_brace, op, operatorname};
use crate::mathml_tree::{MathDomNode, MathNode, MathNodeType};
use crate::options::Options;
use crate::parser::parse_node::{AnyParseNode, NodeType, ParseNode, ParseNodeOp, ParseNodeSupSub};
use crate::style::DISPLAY;
use crate::types::{ParseError, ParseErrorKind};
use crate::units::make_em;
use crate::{ClassList, KatexContext, build_html, build_mathml};

/// HTML builder delegate for supsub nodes
///
/// Sometimes groups perform special rules when they have superscripts or
/// subscripts attached to them. This function lets the `supsub` group know that
/// its inner element should handle the superscripts and subscripts instead of
/// handling them itself.
fn html_builder_delegate(group: &ParseNodeSupSub, options: &Options) -> Option<HtmlBuilder> {
    let base = group.base.as_deref()?;

    match base {
        AnyParseNode::Op(op_node) => {
            // Operators handle supsubs differently when they have limits
            // (e.g. `\displaystyle\sum_2^3`)
            let delegate = op_node.limits()
                && (options.style.size == DISPLAY.size || op_node.always_handle_sup_sub());
            if delegate {
                return Some(op::html_builder);
            }
        }
        AnyParseNode::OperatorName(op_name) => {
            // Operator names handle supsubs differently when they have limits
            let delegate = op_name.always_handle_sup_sub
                && (op_name.limits || options.style.size == DISPLAY.size);
            if delegate {
                return Some(operatorname::html_builder);
            }
        }
        AnyParseNode::Accent(accent) => {
            // Accents handle supsubs when base is a character box
            if accent.base.is_character_box().unwrap_or(false) {
                return Some(accent::html_builder);
            }
        }
        AnyParseNode::HorizBrace(hbrace) => {
            // Horizontal braces handle supsubs based on position
            let is_sup = group.sub.is_none();
            if is_sup == hbrace.is_over {
                return Some(horiz_brace::html_builder);
            }
        }
        _ => {}
    }

    None
}

/// HTML builder for supsub nodes
///
/// Super scripts and subscripts are handled in the TeXbook on page
/// 445-446, rules 18(a-f).
fn html_builder(
    node: &ParseNode,
    options: &Options,
    ctx: &KatexContext,
) -> Result<HtmlDomNode, ParseError> {
    let ParseNode::SupSub(group) = node else {
        return Err(ParseError::new(ParseErrorKind::ExpectedNode {
            node: NodeType::SupSub,
        }));
    };

    // Here is where we defer to the inner group if it should handle
    // superscripts and subscripts itself.
    if let Some(delegate_builder) = html_builder_delegate(group, options) {
        return delegate_builder(node, options, ctx);
    }

    let value_base = group.base.as_deref();
    let value_super = group.sup.as_deref();
    let value_sub = group.sub.as_deref();

    let base_html = if let Some(base) = value_base {
        build_html::build_group(ctx, base, options, None)?
    } else {
        // When base is None, create an empty span
        make_span(ClassList::Empty, vec![], Some(options), None).into()
    };

    let mut super_m = None;
    let mut sub_m = None;

    let metrics = options.font_metrics();

    // Rule 18a
    let mut super_shift = 0.0;
    let mut sub_shift = 0.0;

    let is_character_box = value_base.is_some_and(|b| b.is_character_box().unwrap_or(false));

    if let Some(sup_group) = value_super {
        let new_options = options.having_style(options.style.sup());
        super_m = Some(build_html::build_group(
            ctx,
            sup_group,
            &new_options,
            Some(options),
        )?);
        if !is_character_box {
            super_shift = base_html.height()
                - new_options.font_metrics().sup_drop * new_options.size_multiplier
                    / options.size_multiplier;
        }
    }

    if let Some(sub_group) = value_sub {
        let new_options = options.having_style(options.style.sub());
        sub_m = Some(build_html::build_group(
            ctx,
            sub_group,
            &new_options,
            Some(options),
        )?);
        if !is_character_box {
            sub_shift = base_html.depth()
                + new_options.font_metrics().sub_drop * new_options.size_multiplier
                    / options.size_multiplier;
        }
    }

    // Rule 18c
    let min_sup_shift = if options.style == DISPLAY {
        metrics.sup1
    } else if options.style.cramped {
        metrics.sup3
    } else {
        metrics.sup2
    };

    // scriptspace is a font-size-independent size, so scale it
    // appropriately for use as the marginRight.
    let multiplier = options.size_multiplier;
    let margin_right = make_em((0.5 / metrics.pt_per_em) / multiplier);

    let mut margin_left = None;
    if sub_m.is_some() {
        // Subscripts shouldn't be shifted by the base's italic correction.
        // Account for that by shifting the subscript back the appropriate
        // amount. Note we only do this when the base is a single symbol.
        let is_oiint = if let Some(AnyParseNode::Op(op_node)) = value_base
            && let Some(name) = op_node.name()
        {
            matches!(name, "\\oiint" | "\\oiiint")
        } else {
            false
        };

        if matches!(base_html, HtmlDomNode::Symbol(_)) || is_oiint {
            match &base_html {
                HtmlDomNode::Symbol(sym) => {
                    margin_left = Some(make_em(-sym.italic));
                }
                HtmlDomNode::DomSpan(span) => {
                    if let Some(italic) = span.italic {
                        margin_left = Some(make_em(-italic));
                    }
                }
                _ => {}
            }
        }
    }

    let supsub = match (super_m.take(), sub_m.take(), margin_left) {
        (Some(super_elem), Some(sub_elem), margin_left) => {
            // Both superscript and subscript
            let sup_depth = super_elem.depth();
            let sub_height = sub_elem.height();

            super_shift = super_shift
                .max(min_sup_shift)
                .max(0.25f64.mul_add(metrics.x_height, sup_depth));
            sub_shift = sub_shift.max(metrics.sub2);

            let rule_width = metrics.default_rule_thickness;

            // Rule 18e
            let max_width = 4.0 * rule_width;
            if (super_shift - sup_depth) - (sub_height - sub_shift) < max_width {
                sub_shift = max_width - (super_shift - sup_depth) + sub_height;
                let psi = 0.8f64.mul_add(metrics.x_height, -(super_shift - sup_depth));
                if psi > 0.0 {
                    super_shift += psi;
                    sub_shift -= psi;
                }
            }

            make_v_list(
                VListParam::IndividualShift {
                    children: vec![
                        VListElemAndShift {
                            elem: sub_elem,
                            shift: sub_shift,
                            margin_left,
                            margin_right: Some(margin_right.clone()),
                            wrapper_classes: None,
                            wrapper_style: None,
                        },
                        VListElemAndShift {
                            elem: super_elem,
                            shift: -super_shift,
                            margin_left: None,
                            margin_right: Some(margin_right),
                            wrapper_classes: None,
                            wrapper_style: None,
                        },
                    ],
                },
                options,
            )?
        }
        (None, Some(sub_elem), margin_left) => {
            // Rule 18b
            let sub_height = sub_elem.height();
            sub_shift = sub_shift
                .max(metrics.sub1)
                .max(0.8f64.mul_add(-metrics.x_height, sub_height));

            make_v_list(
                VListParam::Shift {
                    position_data: sub_shift,
                    children: vec![VListChild::Elem(Box::new(VListElem {
                        elem: sub_elem,
                        shift: None,
                        margin_left,
                        margin_right: Some(margin_right),
                        wrapper_classes: None,
                        wrapper_style: None,
                    }))],
                },
                options,
            )?
        }
        (Some(sup_elem), None, _) => {
            // Rule 18c, d
            let sup_depth = sup_elem.depth();
            super_shift = super_shift
                .max(min_sup_shift)
                .max(0.25f64.mul_add(metrics.x_height, sup_depth));

            make_v_list(
                VListParam::Shift {
                    position_data: -super_shift,
                    children: vec![VListChild::Elem(Box::new(VListElem {
                        elem: sup_elem,
                        shift: None,
                        margin_left: None,
                        margin_right: Some(margin_right),
                        wrapper_classes: None,
                        wrapper_style: None,
                    }))],
                },
                options,
            )?
        }
        (None, None, _) => {
            return Err(ParseError::new(ParseErrorKind::SupSubMissingSupOrSub));
        }
    };

    // Wrap the supsub vlist in a span.msupsub to reset text-align.
    let mclass =
        build_html::get_type_of_dom_tree(&base_html, Some(Side::Right)).unwrap_or(DomType::Mord);
    Ok(make_span(
        mclass.as_str(),
        vec![
            base_html,
            make_span("msupsub", vec![supsub.into()], None, None).into(),
        ],
        Some(options),
        None,
    )
    .into())
}

/// MathML builder for supsub nodes
fn mathml_builder(
    node: &ParseNode,
    options: &Options,
    ctx: &KatexContext,
) -> Result<MathDomNode, ParseError> {
    let ParseNode::SupSub(group) = node else {
        return Err(ParseError::new(ParseErrorKind::ExpectedNode {
            node: NodeType::SupSub,
        }));
    };

    // Is the inner group a relevant horizontal brace?
    let mut is_brace = false;
    let mut is_over = false;
    let is_sup;

    if let Some(base) = group.base.as_deref()
        && let AnyParseNode::HorizBrace(hbrace) = base
    {
        is_sup = group.sup.is_some();
        if is_sup == hbrace.is_over {
            is_brace = true;
            is_over = hbrace.is_over;
        }
    }

    let mut children = if let Some(base) = group.base.as_deref() {
        let mut base_clone = base.clone();
        match &mut base_clone {
            AnyParseNode::Op(op_node) => match op_node {
                ParseNodeOp::Symbol {
                    parent_is_sup_sub, ..
                }
                | ParseNodeOp::Body {
                    parent_is_sup_sub, ..
                } => {
                    *parent_is_sup_sub = true;
                }
            },
            AnyParseNode::OperatorName(op_name) => {
                op_name.parent_is_sup_sub = true;
            }
            _ => {}
        }
        vec![build_mathml::build_group(ctx, &base_clone, options)?]
    } else {
        vec![
            MathNode::builder()
                .node_type(MathNodeType::Mrow)
                .build()
                .into(),
        ]
    };

    if let Some(sub) = &group.sub {
        children.push(build_mathml::build_group(ctx, sub, options)?);
    }

    if let Some(sup) = &group.sup {
        children.push(build_mathml::build_group(ctx, sup, options)?);
    }

    let node_type = if is_brace {
        if is_over {
            MathNodeType::Mover
        } else {
            MathNodeType::Munder
        }
    } else if group.sub.is_none() {
        let base = group.base.as_deref();
        if let Some(base) = base
            && let AnyParseNode::Op(op_node) = base
        {
            if op_node.limits() && (options.style == DISPLAY || op_node.always_handle_sup_sub()) {
                MathNodeType::Mover
            } else {
                MathNodeType::Msup
            }
        } else if let Some(base) = base
            && let AnyParseNode::OperatorName(op_name) = base
        {
            if op_name.always_handle_sup_sub && (op_name.limits || options.style == DISPLAY) {
                MathNodeType::Mover
            } else {
                MathNodeType::Msup
            }
        } else {
            MathNodeType::Msup
        }
    } else if group.sup.is_none() {
        let base = group.base.as_deref();
        if let Some(base) = base
            && let AnyParseNode::Op(op_node) = base
        {
            if op_node.limits() && (options.style == DISPLAY || op_node.always_handle_sup_sub()) {
                MathNodeType::Munder
            } else {
                MathNodeType::Msub
            }
        } else if let Some(base) = base
            && let AnyParseNode::OperatorName(op_name) = base
        {
            if op_name.always_handle_sup_sub && (op_name.limits || options.style == DISPLAY) {
                MathNodeType::Munder
            } else {
                MathNodeType::Msub
            }
        } else {
            MathNodeType::Msub
        }
    } else {
        let base = group.base.as_deref();
        if let Some(base) = base
            && let AnyParseNode::Op(op_node) = base
        {
            if op_node.limits() && options.style == DISPLAY {
                MathNodeType::Munderover
            } else {
                MathNodeType::Msubsup
            }
        } else if let Some(base) = base
            && let AnyParseNode::OperatorName(op_name) = base
        {
            if op_name.always_handle_sup_sub && (options.style == DISPLAY || op_name.limits) {
                MathNodeType::Munderover
            } else {
                MathNodeType::Msubsup
            }
        } else {
            MathNodeType::Msubsup
        }
    };

    Ok(MathNode::builder()
        .node_type(node_type)
        .children(children)
        .build()
        .into())
}

/// Registers supsub functions in the KaTeX context
pub fn define_supsub(ctx: &mut KatexContext) {
    ctx.define_function_builders(NodeType::SupSub, Some(html_builder), Some(mathml_builder));
}