math-core-renderer-internal 0.7.0

Internal crate used by math-core
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
use bitflags::bitflags;
#[cfg(feature = "serde")]
use serde::Serialize;

use strum_macros::IntoStaticStr;

use crate::super_char::{SuperChar, VariationSelector};

bitflags! {
    #[repr(transparent)]
    #[derive(Clone, Copy, Debug, PartialEq, Eq)]
    #[cfg_attr(feature = "serde", derive(Serialize))]
    pub struct OpAttrs: u16 {
        const STRETCHY_FALSE = 1;
        const STRETCHY_TRUE = 1 << 1;
        const NO_MOVABLE_LIMITS = 1 << 2;
        const FORCE_MOVABLE_LIMITS = 1 << 3;
        const FORM_PREFIX = 1 << 4;
        const FORM_INFIX = 1 << 5;
        const FORM_POSTFIX = 1 << 6;
        const SYMMETRIC_TRUE = 1 << 7;
        const LARGEOP_TRUE = 1 << 8;
    }
}

impl OpAttrs {
    pub fn write_to(self, s: &mut String) {
        debug_assert!(
            !(self.contains(OpAttrs::STRETCHY_FALSE) && self.contains(OpAttrs::STRETCHY_TRUE)),
            "STRETCHY_FALSE and STRETCHY_TRUE cannot both be set"
        );
        debug_assert!(
            !(self.contains(OpAttrs::NO_MOVABLE_LIMITS)
                && self.contains(OpAttrs::FORCE_MOVABLE_LIMITS)),
            "NO_MOVABLE_LIMITS and FORCE_MOVABLE_LIMITS cannot both be set"
        );
        debug_assert!(
            !(self.contains(OpAttrs::FORM_PREFIX) && self.contains(OpAttrs::FORM_POSTFIX)),
            "FORM_PREFIX and FORM_POSTFIX cannot both be set"
        );
        if self.contains(OpAttrs::STRETCHY_FALSE) {
            s.push_str(r#" stretchy="false""#);
        }
        if self.contains(OpAttrs::STRETCHY_TRUE) {
            s.push_str(r#" stretchy="true""#);
        }
        if self.contains(OpAttrs::NO_MOVABLE_LIMITS) {
            s.push_str(r#" movablelimits="false""#);
        }
        if self.contains(OpAttrs::FORCE_MOVABLE_LIMITS) {
            s.push_str(r#" movablelimits="true""#);
        }
        if self.contains(OpAttrs::FORM_PREFIX) {
            s.push_str(r#" form="prefix""#);
        }
        if self.contains(OpAttrs::FORM_INFIX) {
            s.push_str(r#" form="infix""#);
        }
        if self.contains(OpAttrs::FORM_POSTFIX) {
            s.push_str(r#" form="postfix""#);
        }
        if self.contains(OpAttrs::SYMMETRIC_TRUE) {
            s.push_str(r#" symmetric="true""#);
        }
        if self.contains(OpAttrs::LARGEOP_TRUE) {
            s.push_str(r#" largeop="true""#);
        }
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub enum LetterAttr {
    Default,
    ForcedUpright,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, IntoStaticStr)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub enum Size {
    #[strum(serialize = "1.2em")]
    Scale1 = 1,
    #[strum(serialize = "1.623em")]
    Scale2,
    #[strum(serialize = "2.047em")]
    Scale3,
    #[strum(serialize = "2.470em")]
    Scale4,
}

/// display style
#[derive(Clone, Copy, Debug, PartialEq, Eq, IntoStaticStr)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub enum FracAttr {
    #[strum(serialize = r#" displaystyle="true""#)]
    DisplayStyleTrue = 1,
    #[strum(serialize = r#" displaystyle="false""#)]
    DisplayStyleFalse,
    #[strum(serialize = r#" displaystyle="true" scriptlevel="0" style="padding-top: 0.1667em""#)]
    CFracStyle,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, IntoStaticStr)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub enum Style {
    #[strum(serialize = r#" displaystyle="true" scriptlevel="0""#)]
    Display = 1,
    #[strum(serialize = r#" displaystyle="false" scriptlevel="0""#)]
    Text,
    #[strum(serialize = r#" displaystyle="false" scriptlevel="1""#)]
    Script,
    #[strum(serialize = r#" displaystyle="false" scriptlevel="2""#)]
    ScriptScript,
}

impl Style {
    /// One step smaller, as used for the numerator and denominator of a fraction.
    pub const fn shrink(self) -> Self {
        match self {
            Style::Display => Style::Text,
            Style::Text => Style::Script,
            Style::Script | Style::ScriptScript => Style::ScriptScript,
        }
    }

    /// One step smaller but no bigger than script, as used for sub/superscripts.
    pub const fn scriptify(self) -> Self {
        match self {
            Style::Display | Style::Text => Style::Script,
            Style::Script | Style::ScriptScript => Style::ScriptScript,
        }
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, IntoStaticStr)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub enum MathSpacing {
    #[strum(serialize = "0")]
    Zero = 1,
    /// 3/18 of an em/\quad
    #[strum(serialize = "0.1667em")]
    ThreeMu,
    /// 4/18 of an em/\quad
    #[strum(serialize = "0.2222em")]
    FourMu,
    /// 5/18 of an em/\quad
    #[strum(serialize = "0.2778em")]
    FiveMu,
}

bitflags! {
    #[repr(transparent)]
    #[derive(Clone, Copy, Debug, PartialEq, Eq)]
    #[cfg_attr(feature = "serde", derive(Serialize))]
    pub struct Notation: u8 {
        const HORIZONTAL = 1; // (not used at the moment)
        const UP_DIAGONAL = 1 << 1;
        const DOWN_DIAGONAL = 1 << 2;
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub enum HtmlTextStyle {
    Bold = 1,
    Italic,
    BoldItalic,
    Emphasis,
    Typewriter,
    SmallCaps,
    SansSerif,
    Serif,
    Strikethrough,
    Underline,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, IntoStaticStr)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub enum HtmlTextSize {
    #[strum(serialize = "50%")]
    Size50,
    #[strum(serialize = "60%")]
    Size60,
    #[strum(serialize = "70%")]
    Size70,
    #[strum(serialize = "80%")]
    Size80,
    #[strum(serialize = "90%")]
    Size90,
    #[strum(serialize = "100%")]
    Size100,
    #[strum(serialize = "120%")]
    Size120,
    #[strum(serialize = "140%")]
    Size140,
    #[strum(serialize = "170%")]
    Size170,
    #[strum(serialize = "200%")]
    Size200,
    #[strum(serialize = "250%")]
    Size250,
}

// Transform of unicode characters.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub enum TextTransform {
    Bold = 1,
    BoldFraktur,
    BoldItalic,
    BoldSansSerif,
    BoldScript,
    DoubleStruck,
    Fraktur,
    // Initial,
    Italic,
    // Looped,
    Monospace,
    SansSerif,
    SansSerifBoldItalic,
    SansSerifItalic,
    ScriptChancery,
    ScriptRoundhand,
    // Stretched,
    // Tailed,
}

#[inline]
const fn add_offset(c: char, offset: u32) -> char {
    debug_assert!(char::from_u32(c as u32 + offset).is_some());
    // SAFETY: the offsets are such that the resulting char should be valid.
    unsafe { char::from_u32_unchecked(c as u32 + offset) }
}

impl TextTransform {
    /// If the string does not have length 1, it is passed through unchanged.
    // FIXME maybe we should do better than that?
    #[inline]
    pub const fn transform(self, ts: SuperChar, is_upright: bool) -> SuperChar {
        match ts.try_as_char() {
            Some(c) => self.transform_char(c, is_upright),
            None => ts,
        }
    }

    #[allow(clippy::manual_is_ascii_check)]
    pub const fn transform_char(self, c: char, is_upright: bool) -> SuperChar {
        let tf = if is_upright && matches!(self, TextTransform::BoldItalic) {
            TextTransform::Bold
        } else {
            self
        };
        let mapped = match tf {
            TextTransform::BoldScript => match c {
                'A'..='Z' => Some(add_offset(c, 0x1D48F)),
                'a'..='z' => Some(add_offset(c, 0x1D489)),
                _ => None,
            },
            TextTransform::BoldItalic => match c {
                'A'..='Z' => Some(add_offset(c, 0x1D427)),
                'a'..='z' => Some(add_offset(c, 0x1D421)),
                'Α'..='Ω' => Some(add_offset(c, 0x1D38B)),
                'α'..='ω' => Some(add_offset(c, 0x1D385)),
                'ϴ' => Some('𝜭'),
                '' => Some('𝜵'),
                '' => Some('𝝏'),
                'ϵ' => Some('𝝐'),
                'ϑ' => Some('𝝑'),
                'ϰ' => Some('𝝒'),
                'ϕ' => Some('𝝓'),
                'ϱ' => Some('𝝔'),
                'ϖ' => Some('𝝕'),
                _ => None,
            },
            TextTransform::Bold => match c {
                'A'..='Z' => Some(add_offset(c, 0x1D3BF)),
                'a'..='z' => Some(add_offset(c, 0x1D3B9)),
                'Α'..='Ω' => Some(add_offset(c, 0x1D317)),
                'α'..='ω' => Some(add_offset(c, 0x1D311)),
                'Ϝ'..='ϝ' => Some(add_offset(c, 0x1D3EE)),
                '0'..='9' => Some(add_offset(c, 0x1D79E)),
                'ϴ' => Some('𝚹'),
                '' => Some('𝛁'),
                '' => Some('𝛛'),
                'ϵ' => Some('𝛜'),
                'ϑ' => Some('𝛝'),
                'ϰ' => Some('𝛞'),
                'ϕ' => Some('𝛟'),
                'ϱ' => Some('𝛠'),
                'ϖ' => Some('𝛡'),
                _ => None,
            },
            TextTransform::Fraktur => match c {
                'A'..='B' | 'D'..='G' | 'J'..='Q' | 'S'..='Y' => Some(add_offset(c, 0x1D4C3)),
                'H'..='I' => Some(add_offset(c, 0x20C4)),
                'a'..='z' => Some(add_offset(c, 0x1D4BD)),
                'C' => Some(''),
                'R' => Some(''),
                'Z' => Some(''),
                _ => None,
            },
            TextTransform::ScriptChancery | TextTransform::ScriptRoundhand => match c {
                'A' | 'C'..='D' | 'G' | 'J'..='K' | 'N'..='Q' | 'S'..='Z' => {
                    Some(add_offset(c, 0x1D45B))
                }
                'E'..='F' => Some(add_offset(c, 0x20EB)),
                'a'..='d' | 'f' | 'h'..='n' | 'p'..='z' => Some(add_offset(c, 0x1D455)),
                'B' => Some(''),
                'H' => Some(''),
                'I' => Some(''),
                'L' => Some(''),
                'M' => Some(''),
                'R' => Some(''),
                'e' => Some(''),
                'g' => Some(''),
                'o' => Some(''),
                _ => None,
            },
            TextTransform::Monospace => match c {
                'A'..='Z' => Some(add_offset(c, 0x1D62F)),
                'a'..='z' => Some(add_offset(c, 0x1D629)),
                '0'..='9' => Some(add_offset(c, 0x1D7C6)),
                _ => None,
            },
            TextTransform::SansSerif => match c {
                'A'..='Z' => Some(add_offset(c, 0x1D55F)),
                'a'..='z' => Some(add_offset(c, 0x1D559)),
                '0'..='9' => Some(add_offset(c, 0x1D7B2)),
                _ => None,
            },
            TextTransform::BoldFraktur => match c {
                'A'..='Z' => Some(add_offset(c, 0x1D52B)),
                'a'..='z' => Some(add_offset(c, 0x1D525)),
                _ => None,
            },
            TextTransform::SansSerifBoldItalic => match c {
                'A'..='Z' => Some(add_offset(c, 0x1D5FB)),
                'a'..='z' => Some(add_offset(c, 0x1D5F5)),
                'Α'..='Ω' => Some(add_offset(c, 0x1D3FF)),
                'α'..='ω' => Some(add_offset(c, 0x1D3F9)),
                'ϴ' => Some('𝞡'),
                '' => Some('𝞩'),
                '' => Some('𝟃'),
                'ϵ' => Some('𝟄'),
                'ϑ' => Some('𝟅'),
                'ϰ' => Some('𝟆'),
                'ϕ' => Some('𝟇'),
                'ϱ' => Some('𝟈'),
                'ϖ' => Some('𝟉'),
                _ => None,
            },
            TextTransform::SansSerifItalic => match c {
                'A'..='Z' => Some(add_offset(c, 0x1D5C7)),
                'a'..='z' => Some(add_offset(c, 0x1D5C1)),
                _ => None,
            },
            TextTransform::BoldSansSerif => match c {
                'A'..='Z' => Some(add_offset(c, 0x1D593)),
                'a'..='z' => Some(add_offset(c, 0x1D58D)),
                'Α'..='Ω' => Some(add_offset(c, 0x1D3C5)),
                'α'..='ω' => Some(add_offset(c, 0x1D3BF)),
                '0'..='9' => Some(add_offset(c, 0x1D7BC)),
                'ϴ' => Some('𝝧'),
                '' => Some('𝝯'),
                '' => Some('𝞉'),
                'ϵ' => Some('𝞊'),
                'ϑ' => Some('𝞋'),
                'ϰ' => Some('𝞌'),
                'ϕ' => Some('𝞍'),
                'ϱ' => Some('𝞎'),
                'ϖ' => Some('𝞏'),
                _ => None,
            },
            TextTransform::DoubleStruck => match c {
                'A'..='B' | 'D'..='G' | 'I'..='M' | 'O' | 'S'..='Y' => Some(add_offset(c, 0x1D4F7)),
                'P'..='Q' => Some(add_offset(c, 0x20C9)),
                'a'..='z' => Some(add_offset(c, 0x1D4F1)),
                '0'..='9' => Some(add_offset(c, 0x1D7A8)),
                'C' => Some(''),
                'H' => Some(''),
                'N' => Some(''),
                'R' => Some(''),
                'Z' => Some(''),
                'π' => Some(''),
                'γ' => Some(''),
                'Γ' => Some(''),
                'Π' => Some(''),
                '' => Some(''),
                // FIXME: add Arabic double-struck characters
                _ => None,
            },
            TextTransform::Italic => match c {
                'A'..='Z' => Some(add_offset(c, 0x1D3F3)),
                'a'..='g' | 'i'..='z' => Some(add_offset(c, 0x1D3ED)),
                'Α'..='Ω' => Some(add_offset(c, 0x1D351)),
                'α'..='ω' => Some(add_offset(c, 0x1D34B)),
                'h' => Some(''),
                'ı' => Some('𝚤'),
                'ȷ' => Some('𝚥'),
                'ϴ' => Some('𝛳'),
                '' => Some('𝛻'),
                '' => Some('𝜕'),
                'ϵ' => Some('𝜖'),
                'ϑ' => Some('𝜗'),
                'ϰ' => Some('𝜘'),
                'ϕ' => Some('𝜙'),
                'ϱ' => Some('𝜚'),
                'ϖ' => Some('𝜛'),
                _ => None,
            },
        };

        match mapped {
            Some(mapped_char) => match tf {
                TextTransform::ScriptChancery => {
                    SuperChar::from_char_with_vs(mapped_char, VariationSelector::Vs1)
                }
                TextTransform::ScriptRoundhand => {
                    SuperChar::from_char_with_vs(mapped_char, VariationSelector::Vs2)
                }
                _ => SuperChar::from_char(mapped_char),
            },
            None => SuperChar::from_char(c),
        }
    }
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct RowAttrs {
    // `color: …;` CSS property
    pub color: Option<(u8, u8, u8)>,
    // `style` attribute
    pub style: Option<Style>,
    // `math-shift: compact;` CSS property
    pub math_shift_compact: bool,
}

impl RowAttrs {
    pub const DEFAULT: Self = Self {
        color: None,
        style: None,
        math_shift_compact: false,
    };
}

#[cfg(test)]
mod tests {
    use crate::super_char::{SuperChar, VariationSelector};

    use super::TextTransform;

    #[test]
    fn transform_test() {
        let problems: [(char, TextTransform, SuperChar); _] = [
            ('G', TextTransform::BoldScript, '𝓖'.into()),
            ('H', TextTransform::Italic, '𝐻'.into()),
            ('X', TextTransform::Fraktur, '𝔛'.into()),
            (
                'S',
                TextTransform::ScriptChancery,
                SuperChar::from_char_with_vs('𝒮', VariationSelector::Vs1),
            ),
            (
                'M',
                TextTransform::ScriptRoundhand,
                SuperChar::from_char_with_vs('', VariationSelector::Vs2),
            ),
            ('f', TextTransform::Bold, '𝐟'.into()),
            ('g', TextTransform::Bold, '𝐠'.into()),
            ('o', TextTransform::DoubleStruck, '𝕠'.into()),
            ('D', TextTransform::Monospace, '𝙳'.into()),
            ('x', TextTransform::Monospace, '𝚡'.into()),
            ('2', TextTransform::Monospace, '𝟸'.into()),
            ('U', TextTransform::SansSerif, '𝖴'.into()),
            ('v', TextTransform::SansSerif, '𝗏'.into()),
            ('4', TextTransform::SansSerif, '𝟦'.into()),
            ('A', TextTransform::SansSerifBoldItalic, '𝘼'.into()),
            ('a', TextTransform::SansSerifBoldItalic, '𝙖'.into()),
            ('Α', TextTransform::SansSerifBoldItalic, '𝞐'.into()),
            ('α', TextTransform::SansSerifBoldItalic, '𝞪'.into()),
            ('A', TextTransform::SansSerifItalic, '𝘈'.into()),
            ('a', TextTransform::SansSerifItalic, '𝘢'.into()),
            ('J', TextTransform::BoldSansSerif, '𝗝'.into()),
            ('r', TextTransform::BoldSansSerif, '𝗿'.into()),
            ('Ξ', TextTransform::BoldSansSerif, '𝝣'.into()),
            ('τ', TextTransform::BoldSansSerif, '𝞃'.into()),
        ];
        for (source, transform, target) in problems.into_iter() {
            assert_eq!(
                target,
                transform.transform_char(source, false),
                "executed: {:?}({})",
                transform,
                source
            );
        }
    }
}