mathml_latex/definitions/
builtin.rs

1use super::*;
2
3impl Default for LaTeXEngine {
4    fn default() -> Self {
5        Self {
6            functions: Default::default(),
7            operators: Default::default(),
8            spaces: Default::default(),
9            letters: Default::default(),
10        }
11    }
12}
13
14impl LaTeXEngine {
15    pub fn builtin() -> Self {
16        let mut empty = Self::default();
17        empty.add_builtin_operators();
18        empty.add_builtin_functions();
19        empty.add_builtin_letters();
20        empty.add_builtin_space();
21        empty
22    }
23}
24
25impl LaTeXEngine {
26    pub fn get_function(&self, name: &str) -> Option<&str> {
27        Some(self.functions.get(name)?.as_str())
28    }
29    pub fn add_function<K, V>(&mut self, key: K, value: V)
30    where
31        K: ToString,
32        V: ToString,
33    {
34        self.functions.insert(key.to_string(), value.to_string());
35    }
36    pub fn mut_functions<I>(&mut self) -> &mut BTreeMap<String, String> {
37        &mut self.functions
38    }
39    fn add_builtin_functions(&mut self) {
40        macro_rules! add_function {
41            ($($name:literal => $symbol:literal),* $(,)?) => {
42                $(
43                    self.functions.insert($name.to_string(), $symbol.to_string());
44                )*
45            };
46        }
47        add_function! {
48            "sin" => "sin",
49            "cos" => "cos",
50            "tan" => "tan",
51            "csc" => "csc",
52            "sec" => "sec",
53            "cot" => "cot",
54            "arcsin" => "arcsin",
55            "arccos" => "arccos",
56            "arctan" => "arctan",
57            "sinh" => "sinh",
58            "cosh" => "cosh",
59            "tanh" => "tanh",
60            "coth" => "coth",
61            "exp" => "exp",
62            "ln" => "ln",
63            "log" => "log",
64            "erf" => "erf",
65            "erfc" => "erfc",
66            "arg" => "arg",
67            "gcd" => "gcd",
68            "lcm" => "lcm",
69            "min" => "min",
70            "max" => "max",
71        }
72    }
73}
74
75impl LaTeXEngine {
76    pub fn get_operator(&self, name: &str) -> Option<&str> {
77        Some(self.operators.get(name)?.as_str())
78    }
79    fn add_builtin_operators(&mut self) {
80        macro_rules! add_operator {
81            ($($name:literal => $symbol:literal),* $(,)?) => {
82                $(
83                    self.operators.insert($name.to_string(), $symbol.to_string());
84                )*
85            };
86        }
87
88        add_operator! {
89            "S" => "§",
90            "P" => "¶",
91            "%" => "%",
92            "_" => "_",
93            "&" => "&",
94            "#" => "#",
95            "$" => "$",
96            "times"  => "×",
97            "oplus"  => "⊕",
98            "otimes" => "⊗",
99            "odot"   => "⊙",
100            "cup"    => "∪",
101            "cap"    => "∩",
102            "sqcup"  => "⊔",
103            "sqcap"  => "⊓",
104            "vee"    => "∨",
105            "wedge"  => "∧",
106            "setminus" => "∖",
107            "uplus"  => "⊎",
108            "amalg"  => "⨿",
109            "bigcup" => "⋃",
110            "bigcap" => "⋂",
111        }
112    }
113}
114
115impl LaTeXEngine {
116    pub fn get_letters(&self, name: &str) -> Option<&str> {
117        Some(self.letters.get(name)?.as_str())
118    }
119    fn add_builtin_letters(&mut self) {
120        macro_rules! add_letter {
121            ($($name:literal => $symbol:literal),* $(,)?) => {
122                $(
123                    self.letters.insert($name.to_string(), $symbol.to_string());
124                )*
125            };
126        }
127
128        add_letter! {
129            "Alpha"  => "Α",
130            "alpha"  => "α",
131            "Beta"   => "Β",
132            "beta"   => "β",
133            "Gamma"  => "Γ",
134            "gamma"  => "γ",
135            "Delta"  => "Δ",
136            "delta"  => "δ",
137            "Epsilon"  => "Ε",
138            "epsilon"  => "ε",
139            "Zeta"  => "Ζ",
140            "zeta"  => "ζ",
141            "Eta"  => "Η",
142            "eta"  => "η",
143            "Theta"  => "Θ",
144            "theta"  => "θ",
145            "Iota"  => "Ι",
146            "iota"  => "ι",
147            "Kappa"  => "Κ",
148            "kappa"  => "κ",
149            "Lambda"  => "Λ",
150            "lambda"  => "λ",
151            "Mu"  => "Μ",
152            "mu"  => "μ",
153            "Nu"  => "Ν",
154            "nu"  => "ν",
155            "Xi"  => "Ξ",
156            "xi"  => "ξ",
157            "Omicron"  => "Ο",
158            "omicron"  => "ο",
159            "Pi"  => "Π",
160            "pi"  => "π",
161            "Rho"  => "Ρ",
162            "rho"  => "ρ",
163            "Sigma"  => "Σ",
164            "sigma"  => "σ",
165            "Tau"  => "Τ",
166            "tau"  => "τ",
167            "Upsilon"  => "Υ",
168            "upsilon"  => "υ",
169            "Phi"  => "Φ",
170            "phi"  => "φ",
171            "Chi"  => "Χ",
172            "chi"  => "χ",
173            "Psi"  => "Ψ",
174            "psi"  => "ψ",
175            "Omega"  => "Ω",
176            "omega"  => "ω",
177            //
178            "aleph" => "ℵ",
179            "beth" => "ℶ",
180            "gimel" => "ℷ",
181            "daleth" => "ℸ",
182            "A" => "Å",
183            "a" => "å",
184            "AE" => "Æ",
185            "ae" => "æ",
186            "DH" => "Ð",
187            "dh" => "ð",
188            "dj" => "đ",
189            "L" => "Ł",
190            "l" => "ł",
191            "NG" => "Ŋ",
192            "ng" => "ŋ",
193            "O" => "Ø",
194            "o" => "ø",
195            "OE" => "Œ",
196            "oe" => "œ",
197            "ss" => "ß",
198            "TH" => "Þ",
199            "th" => "þ",
200            "imath" => "ı",
201            "jmath" => "ȷ",
202            "ell" => "ℓ",
203            "hbar" => "ℏ",
204            "hslash" => "ℏ",
205            "infty" => "∞",
206            "mho" => "℧",
207            "Finv" => "Ⅎ",
208            "Re" => "ℜ",
209            "Im" => "ℑ",
210            "wp" => "℘",
211            "alef" => "ℵ",
212            "alefsym" => "ℵ",
213            "real" => "ℜ",
214            "partial" => "∂",
215            "prime" => "′",
216            "emptyset" => "∅",
217            "clubs" => "♣",
218        }
219    }
220}
221
222impl LaTeXEngine {
223    pub fn get_space(&self, name: &str) -> Option<f32> {
224        Some(*self.spaces.get(name)?)
225    }
226    pub fn add_space(&mut self, name: &str, value: f32) {
227        self.spaces.insert(name.to_string(), value);
228    }
229    fn add_builtin_space(&mut self) {
230        self.spaces.insert("!".to_string(), -3.0 / 18.0);
231        self.spaces.insert(",".to_string(), 3.0 / 18.0);
232        self.spaces.insert(":".to_string(), 4.0 / 18.0);
233        self.spaces.insert(";".to_string(), 5.0 / 18.0);
234        self.spaces.insert(" ".to_string(), 1.0);
235        self.spaces.insert("quad".to_string(), 1.0);
236        self.spaces.insert("qquad".to_string(), 2.0);
237    }
238}