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
//! Mathematical symbol mappings for KaTeX
//!
//! This module provides comprehensive symbol table management for mathematical
//! typesetting, mapping LaTeX command names to Unicode mathematical symbols and
//! handling symbol categorization, ligature detection, and wide character
//! variants. It supports both math and text modes as defined in KaTeX
//! specifications.
//!
//! # Key Features
//!
//! - Unicode mathematical symbol mapping (e.g., `\alpha` → `α`)
//! - Ligature handling for common sequences (e.g., `--` → `–`)
//! - Wide character variants for mathematical alphanumeric symbols
//! - Symbol categorization by atom types and font styles
//! - Support for both math and text rendering modes
//!
//! # Examples
//!
//! ```rust
//! use katex::symbols::{Font, Group, Mode, NonAtom, Symbols};
//!
//! let mut symbols = Symbols::new();
//! symbols.define_symbol(
//!     Mode::Math,
//!     Font::Main,
//!     Group::NonAtom(NonAtom::MathOrd),
//!     Some('α'),
//!     "\\alpha",
//!     false,
//! );
//!
//! if let Some(info) = symbols.get_math("\\alpha") {
//!     println!("Unicode: {}", info.replace.unwrap_or('?'));
//! }
//! ```

mod types;
use crate::ParseError;
use crate::namespace::KeyMap;
use crate::types::ParseErrorKind;
use phf::phf_map;
pub use types::{Atom, CharInfo, Font, Group, Mode, NonAtom};

/// Core symbol table for mathematical typesetting
///
/// The `Symbols` struct maintains separate mappings for math and text modes,
/// storing character information including Unicode replacements, font styles,
/// and atom categorization. This enables proper rendering of LaTeX commands
/// in different contexts.
///
/// # Fields
///
/// * `math` - Symbol mappings for mathematical expressions
/// * `text` - Symbol mappings for text content
///
/// # Examples
///
/// ```rust
/// use katex::symbols::Symbols;
///
/// let symbols = Symbols::new();
/// assert!(symbols.get_math("\\custom").is_none());
/// assert!(symbols.get_text("\\custom").is_none());
/// ```
pub struct Symbols {
    /// Symbol mappings for mathematical expressions
    math: KeyMap<String, CharInfo>,
    /// Symbol mappings for text content
    text: KeyMap<String, CharInfo>,
}

include!(concat!(env!("OUT_DIR"), "/generated_symbols_data.rs"));

/// Default implementation for Symbols
///
/// Creates an empty symbol table. For a fully populated table with standard
/// mathematical symbols, use [`create_symbols`] instead.
///
/// # Examples
///
/// ```rust
/// use katex::symbols::Symbols;
///
/// let symbols = Symbols::default();
/// assert!(symbols.get_math("\\custom").is_none());
/// ```
impl Default for Symbols {
    fn default() -> Self {
        Self::new()
    }
}

impl Symbols {
    /// Creates a new empty symbol table
    ///
    /// Initializes empty hash maps for both math and text modes. Use this
    /// when you need to build a custom symbol table or start from scratch.
    /// For standard KaTeX symbols, prefer [`create_symbols`].
    ///
    /// # Returns
    ///
    /// A new `Symbols` instance with empty mappings.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use katex::symbols::Symbols;
    ///
    /// let symbols = Symbols::new();
    /// assert!(symbols.get_math("\\custom").is_none());
    /// assert!(symbols.get_text("\\custom").is_none());
    /// ```
    #[must_use]
    pub fn new() -> Self {
        Self {
            math: KeyMap::default(),
            text: KeyMap::default(),
        }
    }

    /// Defines a symbol in the specified rendering mode
    ///
    /// Adds a new symbol mapping to the appropriate mode's hash map,
    /// associating a LaTeX command name with its character properties and
    /// Unicode replacement. This is the primary method for populating the
    /// symbol table.
    ///
    /// # Parameters
    ///
    /// * `mode` - The rendering mode ([`Mode::Math`] or [`Mode::Text`])
    /// * `font` - The font style for the symbol
    /// * `group` - The atom group categorization
    /// * `replace` - Optional Unicode character replacement string
    /// * `name` - The LaTeX command name (e.g., `"\\alpha"`)
    /// * `accept_unicode_char` - Whether to also map the Unicode character back
    ///   to the symbol
    ///
    /// # Examples
    ///
    /// ```rust
    /// use katex::symbols::{Font, Group, Mode, NonAtom, Symbols};
    ///
    /// let mut symbols = Symbols::new();
    /// symbols.define_symbol(
    ///     Mode::Math,
    ///     Font::Main,
    ///     Group::NonAtom(NonAtom::MathOrd),
    ///     Some('α'),
    ///     "\\alpha",
    ///     true,
    /// );
    /// ```
    ///
    /// # See Also
    ///
    /// - [`create_symbols`] for populating with standard symbols
    /// - [`CharInfo`] for symbol property details
    pub fn define_symbol(
        &mut self,
        mode: Mode,
        font: Font,
        group: Group,
        replace: Option<char>,
        name: &str,
        accept_unicode_char: bool,
    ) {
        let char_info = CharInfo {
            font,
            group,
            replace,
        };

        let table = match mode {
            Mode::Math => &mut self.math,
            Mode::Text => &mut self.text,
        };

        table.insert(name.to_owned(), char_info.clone());

        if accept_unicode_char && let Some(s) = replace {
            table.insert(s.to_string(), char_info);
        }
    }

    /// Retrieves character information for a symbol in math mode
    ///
    /// Looks up the symbol by its LaTeX command name in the math mode mappings.
    ///
    /// # Parameters
    ///
    /// * `name` - The LaTeX command name (e.g., `"\\alpha"`)
    ///
    /// # Returns
    ///
    /// `Some(&CharInfo)` if the symbol exists in math mode, `None` otherwise.
    ///
    /// # Examples
    ///
    /// ```
    /// use katex::symbols::create_symbols;
    ///
    /// let symbols = create_symbols();
    /// if let Some(info) = symbols.get_math("\\alpha") {
    ///     println!("Unicode: {}", info.replace.unwrap_or('?'));
    /// }
    /// ```
    #[must_use]
    pub fn get_math(&self, name: &str) -> Option<&CharInfo> {
        self.math
            .get(name)
            .or_else(|| POPULATE_MATH_SYMBOLS_MAP.get(name))
    }

    /// Retrieves character information for a symbol in text mode
    ///
    /// Looks up the symbol by its LaTeX command name in the text mode mappings.
    ///
    /// # Parameters
    ///
    /// * `name` - The LaTeX command name (e.g., `"\\#"` for hash symbol)
    ///
    /// # Returns
    ///
    /// `Some(&CharInfo)` if the symbol exists in text mode, `None` otherwise.
    ///
    /// # Examples
    ///
    /// ```
    /// use katex::symbols::create_symbols;
    ///
    /// let symbols = create_symbols();
    /// if let Some(info) = symbols.get_text("\\#") {
    ///     println!("Unicode: {}", info.replace.unwrap_or('?'));
    /// }
    /// ```
    #[must_use]
    pub fn get_text(&self, name: &str) -> Option<&CharInfo> {
        self.text
            .get(name)
            .or_else(|| POPULATE_TEXT_SYMBOLS_MAP.get(name))
    }

    /// Retrieves character information for a symbol in the specified mode
    ///
    /// A unified lookup method that delegates to the appropriate mode-specific
    /// method.
    ///
    /// # Parameters
    ///
    /// * `mode` - The rendering mode to search in
    /// * `name` - The LaTeX command name
    ///
    /// # Returns
    ///
    /// `Some(&CharInfo)` if the symbol exists in the specified mode, `None`
    /// otherwise.
    ///
    /// # Examples
    ///
    /// ```
    /// use katex::symbols::{Mode, create_symbols};
    ///
    /// let symbols = create_symbols();
    /// if let Some(info) = symbols.get(Mode::Math, "\\alpha") {
    ///     println!("Found in math mode: {:?}", info.replace);
    /// }
    /// ```
    #[must_use]
    pub fn get(&self, mode: Mode, name: &str) -> Option<&CharInfo> {
        match mode {
            Mode::Math => self.get_math(name),
            Mode::Text => self.get_text(name),
        }
    }

    /// Checks if a symbol exists in the specified mode
    #[must_use]
    pub fn contains(&self, mode: Mode, name: &str) -> bool {
        match mode {
            Mode::Math => {
                self.math.contains_key(name) || POPULATE_MATH_SYMBOLS_MAP.contains_key(name)
            }
            Mode::Text => {
                self.text.contains_key(name) || POPULATE_TEXT_SYMBOLS_MAP.contains_key(name)
            }
        }
    }
}

/// Checks if a string represents a known ligature sequence
///
/// Ligatures are special character combinations that are replaced with single
/// Unicode characters for better typography (e.g., `--` becomes an en-dash).
///
/// # Parameters
///
/// * `s` - The string to check for ligature patterns
///
/// # Returns
///
/// `true` if the string is a recognized ligature, `false` otherwise.
///
/// # Examples
///
/// ```
/// use katex::symbols::is_ligature;
///
/// assert!(is_ligature("--"));
/// assert!(is_ligature("``"));
/// assert!(!is_ligature("not a ligature"));
/// ```
///
/// # See Also
///
/// - [`LIGATURES`] for the complete list of supported ligatures
#[must_use]
pub fn is_ligature(s: &str) -> bool {
    LIGATURES.contains_key(s)
}

/// Creates a wide Unicode character from UTF-16 surrogate pair
///
/// Mathematical alphanumeric symbols in Unicode are often represented using
/// surrogate pairs in UTF-16 encoding. This function combines the high and low
/// surrogates to form the complete Unicode scalar value.
///
/// # Parameters
///
/// * `high_surrogate` - The high surrogate value (typically 0xD835 for math
///   symbols)
/// * `low_surrogate` - The low surrogate value for the specific character
///
/// # Error Handling
///
/// Returns an empty string if the surrogate pair doesn't form a valid Unicode
/// scalar value.
///
/// # See Also
///
/// - [`generate_wide_char_variants`] for creating multiple variants
/// - Unicode Mathematical Alphanumeric Symbols block (U+1D400–U+1D7FF)
///
/// # Safety
///
/// Safe only within the valid range of surrogate pairs for mathematical
#[must_use]
const fn create_wide_char(high: u16, low: u16) -> char {
    let cp = 0x10000 + (((high as u32) - 0xD800) << 10) + ((low as u32) - 0xDC00);
    // SAFETY: The calculation ensures cp is a valid Unicode scalar value
    unsafe { char::from_u32_unchecked(cp) }
}

/// Static mapping of ligature sequences to their Unicode replacements
///
/// This compile-time hash map contains common typographical ligatures used in
/// mathematical typesetting. Ligatures improve text appearance by replacing
/// multiple characters with a single, properly designed glyph.
///
/// # Supported Ligatures
///
/// | Sequence | Unicode | Description     |
/// |----------|---------|-----------------|
/// | `--`     | `–`     | En dash         |
/// | `---`    | `—`     | Em dash         |
/// | `` ``    | `"`     | Left double quote |
/// | `''`     | `"`     | Right double quote |
///
/// # Examples
///
/// ```
/// use katex::symbols::LIGATURES;
///
/// assert_eq!(LIGATURES.get("--"), Some(&"–"));
/// assert_eq!(LIGATURES.get("``"), Some(&"\u{201c}"));
/// ```
///
/// # See Also
///
/// - [`is_ligature`] for checking if a string is a ligature
pub const LIGATURES: phf::Map<&str, &str> = phf_map!(
    "--" => "\u{2013}",
    "---" => "\u{2014}",
    "``" => "\u{201c}",
    "''" => "\u{201d}",
);

/// Conversion from string to Group enum
///
/// Parses a string representation of a symbol group into the corresponding
/// [`Group`] enum variant. This is used when loading symbol definitions
/// from configuration files or data sources.
///
/// # Parameters
///
/// * `s` - String slice representing the group name
///
/// # Returns
///
/// The corresponding [`Group`] enum variant.
///
/// # Error Handling
///
/// Panics with an error message if the string doesn't match any known group.
/// This ensures data integrity when loading from external sources.
///
/// # See Also
///
/// - [`Group`] enum for the complete type definition
/// - [`Atom`] and [`NonAtom`] for group classifications
impl TryFrom<&str> for Group {
    type Error = ParseError;
    fn try_from(s: &str) -> Result<Self, ParseError> {
        match s {
            "bin" => Ok(Self::Atom(Atom::Bin)),
            "close" => Ok(Self::Atom(Atom::Close)),
            "inner" => Ok(Self::Atom(Atom::Inner)),
            "open" => Ok(Self::Atom(Atom::Open)),
            "punct" => Ok(Self::Atom(Atom::Punct)),
            "rel" => Ok(Self::Atom(Atom::Rel)),
            "accent" | "accent-token" => Ok(Self::NonAtom(NonAtom::AccentToken)),
            "mathord" => Ok(Self::NonAtom(NonAtom::MathOrd)),
            "op" | "op-token" => Ok(Self::NonAtom(NonAtom::OpToken)),
            "spacing" => Ok(Self::NonAtom(NonAtom::Spacing)),
            "textord" => Ok(Self::NonAtom(NonAtom::TextOrd)),
            _ => Err(ParseError::new(ParseErrorKind::InvalidGroup {
                group: s.to_owned(),
            })),
        }
    }
}

/// Creates and populates the global symbol table with standard KaTeX symbols
///
/// This function initializes a complete symbol table containing all standard
/// mathematical symbols, Greek letters, operators, and special characters
/// supported by KaTeX. It loads symbols from JSON data, generates wide
/// character variants, and sets up both math and text mode mappings.
///
/// The returned symbol table includes:
/// - Greek letters (α, β, γ, etc.)
/// - Mathematical operators (=, +, −, etc.)
/// - Relations (≡, ∼, ≅, etc.)
/// - Delimiters ({, }, \[, \], etc.)
/// - Accents (´, `, ˜, etc.)
/// - Wide character variants for letters and numbers
/// - Text symbols for regular typography
///
/// # Returns
///
/// A fully populated [`Symbols`] instance ready for mathematical typesetting.
///
/// # Examples
///
/// ```rust
/// use katex::symbols::create_symbols;
///
/// let symbols = create_symbols();
/// assert!(symbols.get_math("\\alpha").is_some());
/// assert!(symbols.get_text("\\#").is_some());
///
/// // Look up a Greek letter
/// if let Some(alpha) = symbols.get_math("\\alpha") {
///     println!("Alpha: {}", alpha.replace.unwrap_or('?'));
/// }
/// ```
///
/// # Performance
///
/// This function performs significant computation to generate all symbol
/// variants. Therefore, should not be called except context initialization or
/// testing.
///
/// # See Also
///
/// - [`Symbols::new`] for creating an empty table
/// - [`Symbols::define_symbol`] for adding individual symbols
/// - KaTeX symbol documentation for the complete symbol set
#[must_use]
pub fn create_symbols() -> Symbols {
    let mut symbols = Symbols::new();

    // mathTextSymbols loop: numbers and symbols for math mode
    let math_text_symbols = "0123456789/@.\"";
    for ch in math_text_symbols.chars() {
        symbols.define_symbol(
            Mode::Math,
            Font::Main,
            Group::NonAtom(NonAtom::TextOrd),
            Some(ch),
            &ch.to_string(),
            false,
        );
    }

    // !TODO: Fix extractor to include all symbols
    symbols.define_symbol(
        Mode::Text,
        Font::Main,
        Group::NonAtom(NonAtom::AccentToken),
        Some('\u{00a8}'),
        r#"\""#,
        false,
    );

    // textSymbols loop: symbols for text mode
    let text_symbols = "0123456789!@*()-=+\";:?/.,";
    for ch in text_symbols.chars() {
        symbols.define_symbol(
            Mode::Text,
            Font::Main,
            Group::NonAtom(NonAtom::TextOrd),
            Some(ch),
            &ch.to_string(),
            false,
        );
    }

    // letters loop: A-Z a-z for math and text modes
    let letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    for ch in letters.chars() {
        symbols.define_symbol(
            Mode::Math,
            Font::Main,
            Group::NonAtom(NonAtom::MathOrd),
            Some(ch),
            &ch.to_string(),
            false,
        );
        symbols.define_symbol(
            Mode::Text,
            Font::Main,
            Group::NonAtom(NonAtom::TextOrd),
            Some(ch),
            &ch.to_string(),
            false,
        );
    }

    // wideChar letters loop: bold, italic, etc., with conditions for i < 26
    let letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    for (i, ch) in letters.chars().enumerate() {
        // Bold (U+1D400-1D433)
        let wide_char = create_wide_char(0xD835, 0xDC00 + i as u16).to_string();
        symbols.define_symbol(
            Mode::Math,
            Font::Main,
            Group::NonAtom(NonAtom::MathOrd),
            Some(ch),
            &wide_char,
            false,
        );
        symbols.define_symbol(
            Mode::Text,
            Font::Main,
            Group::NonAtom(NonAtom::TextOrd),
            Some(ch),
            &wide_char,
            false,
        );

        // Italic (U+1D434-1D467)
        let wide_char = create_wide_char(0xD835, 0xDC34 + i as u16).to_string();
        symbols.define_symbol(
            Mode::Math,
            Font::Main,
            Group::NonAtom(NonAtom::MathOrd),
            Some(ch),
            &wide_char,
            false,
        );
        symbols.define_symbol(
            Mode::Text,
            Font::Main,
            Group::NonAtom(NonAtom::TextOrd),
            Some(ch),
            &wide_char,
            false,
        );

        // Bold Italic (U+1D468-1D49B)
        let wide_char = create_wide_char(0xD835, 0xDC68 + i as u16).to_string();
        symbols.define_symbol(
            Mode::Math,
            Font::Main,
            Group::NonAtom(NonAtom::MathOrd),
            Some(ch),
            &wide_char,
            false,
        );
        symbols.define_symbol(
            Mode::Text,
            Font::Main,
            Group::NonAtom(NonAtom::TextOrd),
            Some(ch),
            &wide_char,
            false,
        );

        // Fraktur (U+1D504-1D537)
        let wide_char = create_wide_char(0xD835, 0xDD04 + i as u16).to_string();
        symbols.define_symbol(
            Mode::Math,
            Font::Main,
            Group::NonAtom(NonAtom::MathOrd),
            Some(ch),
            &wide_char,
            false,
        );
        symbols.define_symbol(
            Mode::Text,
            Font::Main,
            Group::NonAtom(NonAtom::TextOrd),
            Some(ch),
            &wide_char,
            false,
        );

        // Bold Fraktur (U+1D56C-1D59F)
        let wide_char = create_wide_char(0xD835, 0xDD6C + i as u16).to_string();
        symbols.define_symbol(
            Mode::Math,
            Font::Main,
            Group::NonAtom(NonAtom::MathOrd),
            Some(ch),
            &wide_char,
            false,
        );
        symbols.define_symbol(
            Mode::Text,
            Font::Main,
            Group::NonAtom(NonAtom::TextOrd),
            Some(ch),
            &wide_char,
            false,
        );

        // Sans-serif (U+1D5A0-1D5D3)
        let wide_char = create_wide_char(0xD835, 0xDDA0 + i as u16).to_string();
        symbols.define_symbol(
            Mode::Math,
            Font::Main,
            Group::NonAtom(NonAtom::MathOrd),
            Some(ch),
            &wide_char,
            false,
        );
        symbols.define_symbol(
            Mode::Text,
            Font::Main,
            Group::NonAtom(NonAtom::TextOrd),
            Some(ch),
            &wide_char,
            false,
        );

        // Sans Bold (U+1D5D4-1D607)
        let wide_char = create_wide_char(0xD835, 0xDDD4 + i as u16).to_string();
        symbols.define_symbol(
            Mode::Math,
            Font::Main,
            Group::NonAtom(NonAtom::MathOrd),
            Some(ch),
            &wide_char,
            false,
        );
        symbols.define_symbol(
            Mode::Text,
            Font::Main,
            Group::NonAtom(NonAtom::TextOrd),
            Some(ch),
            &wide_char,
            false,
        );

        // Sans Italic (U+1D608-1D63B)
        let wide_char = create_wide_char(0xD835, 0xDE08 + i as u16).to_string();
        symbols.define_symbol(
            Mode::Math,
            Font::Main,
            Group::NonAtom(NonAtom::MathOrd),
            Some(ch),
            &wide_char,
            false,
        );
        symbols.define_symbol(
            Mode::Text,
            Font::Main,
            Group::NonAtom(NonAtom::TextOrd),
            Some(ch),
            &wide_char,
            false,
        );

        // Monospace (U+1D670-1D6A3)
        let wide_char = create_wide_char(0xD835, 0xDE70 + i as u16).to_string();
        symbols.define_symbol(
            Mode::Math,
            Font::Main,
            Group::NonAtom(NonAtom::MathOrd),
            Some(ch),
            &wide_char,
            false,
        );
        symbols.define_symbol(
            Mode::Text,
            Font::Main,
            Group::NonAtom(NonAtom::TextOrd),
            Some(ch),
            &wide_char,
            false,
        );

        if i < 26 {
            // Double struck (U+1D538-1D56B for A-Z)
            let wide_char = create_wide_char(0xD835, 0xDD38 + i as u16).to_string();
            symbols.define_symbol(
                Mode::Math,
                Font::Main,
                Group::NonAtom(NonAtom::MathOrd),
                Some(ch),
                &wide_char,
                false,
            );
            symbols.define_symbol(
                Mode::Text,
                Font::Main,
                Group::NonAtom(NonAtom::TextOrd),
                Some(ch),
                &wide_char,
                false,
            );

            // Script (U+1D49C-1D4CF for A-Z)
            let wide_char = create_wide_char(0xD835, 0xDC9C + i as u16).to_string();
            symbols.define_symbol(
                Mode::Math,
                Font::Main,
                Group::NonAtom(NonAtom::MathOrd),
                Some(ch),
                &wide_char,
                false,
            );
            symbols.define_symbol(
                Mode::Text,
                Font::Main,
                Group::NonAtom(NonAtom::TextOrd),
                Some(ch),
                &wide_char,
                false,
            );
        }
    }

    // Special case for "k" double struck lower case
    let wide_char = create_wide_char(0xD835, 0xDD5C).to_string();
    symbols.define_symbol(
        Mode::Math,
        Font::Main,
        Group::NonAtom(NonAtom::MathOrd),
        Some('k'),
        &wide_char,
        false,
    );
    symbols.define_symbol(
        Mode::Text,
        Font::Main,
        Group::NonAtom(NonAtom::TextOrd),
        Some('k'),
        &wide_char,
        false,
    );

    // wideChar numbers loop: bold, etc., for 0-9
    for i in 0..10 {
        let ch = char::from(b'0' + i);

        // Bold (U+1D7CE-1D7D7)
        let wide_char = create_wide_char(0xD835, 0xDFCE + u16::from(i)).to_string();
        symbols.define_symbol(
            Mode::Math,
            Font::Main,
            Group::NonAtom(NonAtom::MathOrd),
            Some(ch),
            &wide_char,
            false,
        );
        symbols.define_symbol(
            Mode::Text,
            Font::Main,
            Group::NonAtom(NonAtom::TextOrd),
            Some(ch),
            &wide_char,
            false,
        );

        // Sans-serif (U+1D7E2-1D7EB)
        let wide_char = create_wide_char(0xD835, 0xDFE2 + u16::from(i)).to_string();
        symbols.define_symbol(
            Mode::Math,
            Font::Main,
            Group::NonAtom(NonAtom::MathOrd),
            Some(ch),
            &wide_char,
            false,
        );
        symbols.define_symbol(
            Mode::Text,
            Font::Main,
            Group::NonAtom(NonAtom::TextOrd),
            Some(ch),
            &wide_char,
            false,
        );

        // Bold Sans (U+1D7EC-1D7F5)
        let wide_char = create_wide_char(0xD835, 0xDFEC + u16::from(i)).to_string();
        symbols.define_symbol(
            Mode::Math,
            Font::Main,
            Group::NonAtom(NonAtom::MathOrd),
            Some(ch),
            &wide_char,
            false,
        );
        symbols.define_symbol(
            Mode::Text,
            Font::Main,
            Group::NonAtom(NonAtom::TextOrd),
            Some(ch),
            &wide_char,
            false,
        );

        // Monospace (U+1D7F6-1D7FF)
        let wide_char = create_wide_char(0xD835, 0xDFF6 + u16::from(i)).to_string();
        symbols.define_symbol(
            Mode::Math,
            Font::Main,
            Group::NonAtom(NonAtom::MathOrd),
            Some(ch),
            &wide_char,
            false,
        );
        symbols.define_symbol(
            Mode::Text,
            Font::Main,
            Group::NonAtom(NonAtom::TextOrd),
            Some(ch),
            &wide_char,
            false,
        );
    }

    // extraLatin loop
    let extra_latin = "\u{00d0}\u{00de}\u{00fe}";
    for ch in extra_latin.chars() {
        symbols.define_symbol(
            Mode::Math,
            Font::Main,
            Group::NonAtom(NonAtom::MathOrd),
            Some(ch),
            &ch.to_string(),
            false,
        );
        symbols.define_symbol(
            Mode::Text,
            Font::Main,
            Group::NonAtom(NonAtom::TextOrd),
            Some(ch),
            &ch.to_string(),
            false,
        );
    }

    symbols
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::symbols::{Atom, Font, Group, NonAtom, create_symbols};

    #[test]
    fn test_symbol_creation() {
        let symbols = create_symbols();

        // Test basic symbol lookup
        assert!(
            symbols.get_math("\\equiv").is_some(),
            "Failed to find \\equiv"
        );
        assert!(
            symbols.get_math("\\forall").is_some(),
            "Failed to find \\forall"
        );
        assert!(
            symbols.get_math("\\exists").is_some(),
            "Failed to find \\exists"
        );

        // Test mode-specific symbols
        assert!(
            symbols.get_text("\\#").is_some(),
            "Failed to find \\# in text mode"
        );
        assert!(
            symbols.get_math("\\#").is_some(),
            "Failed to find \\# in math mode"
        );

        // Test Unicode character mapping
        let equiv_info = symbols.get_math("\\equiv").unwrap();
        assert_eq!(equiv_info.replace, Some('\u{2261}'));
    }

    fn get_ligature_replacement(s: &str) -> Option<&'static str> {
        LIGATURES.get(s).copied()
    }

    #[test]
    fn test_ligature_detection() {
        assert!(is_ligature("--"));
        assert!(is_ligature("---"));
        assert!(is_ligature("``"));
        assert!(is_ligature("''"));
        assert!(!is_ligature("notaligature"));

        assert_eq!(get_ligature_replacement("--"), Some("\u{2013}"));
        assert_eq!(get_ligature_replacement("---"), Some("\u{2014}"));
        assert_eq!(get_ligature_replacement("``"), Some("\u{201c}"));
        assert_eq!(get_ligature_replacement("''"), Some("\u{201d}"));
        assert_eq!(get_ligature_replacement("notaligature"), None);
    }

    #[test]
    fn test_symbol_properties() {
        let symbols = create_symbols();

        // Test symbol properties
        let equiv_info = symbols.get_math("\\equiv").unwrap();
        assert_eq!(equiv_info.font, Font::Main);
        assert!(matches!(equiv_info.group, Group::Atom(Atom::Rel)));

        let forall_info = symbols.get_math("\\forall").unwrap();
        assert_eq!(forall_info.font, Font::Main);
        assert!(matches!(
            forall_info.group,
            Group::NonAtom(NonAtom::TextOrd)
        ));
    }

    #[test]
    fn test_key_symbols_retrieval() {
        let symbols = create_symbols();

        // Test Greek letters
        assert!(symbols.get_math("\\alpha").is_some());
        assert!(symbols.get_math("\\beta").is_some());
        assert!(symbols.get_math("\\gamma").is_some());
        assert!(symbols.get_math("\\delta").is_some());

        // Test that alpha has correct properties
        let alpha_info = symbols.get_math("\\alpha").unwrap();
        assert_eq!(alpha_info.font, Font::Main);
        assert!(matches!(alpha_info.group, Group::NonAtom(NonAtom::MathOrd)));
        assert_eq!(alpha_info.replace, Some('\u{03b1}'));

        // Test numbers
        for i in 0..10 {
            let num_str = format!("{i}");
            assert!(symbols.get_math(&num_str).is_some());
            assert!(symbols.get_text(&num_str).is_some());
        }

        // Test letters
        let letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
        for ch in letters.chars() {
            let ch_str = ch.to_string();
            assert!(symbols.get_math(&ch_str).is_some());
            assert!(symbols.get_text(&ch_str).is_some());
        }

        // Test operators
        assert!(symbols.get_math("\\equiv").is_some());
        assert!(symbols.get_math("\\forall").is_some());
        assert!(symbols.get_math("\\exists").is_some());

        // Test accents
        assert!(symbols.get_math("\\acute").is_some());
        assert!(symbols.get_math("\\grave").is_some());
        assert!(symbols.get_math("\\tilde").is_some());
    }
}