1pub const THIN: f64 = 0.167; pub const MED: f64 = 0.222; pub const THICK: f64 = 0.278; #[derive(Debug, Clone, Default)]
14pub struct OpDictProperties {
15 pub lspace: f64,
16 pub rspace: f64,
17 pub stretchy: bool,
18 pub symmetric: bool,
19 pub largeop: bool,
20 pub movablelimits: bool,
21 pub fence: bool,
22 pub separator: bool,
23}
24
25pub fn role_to_form(role: &str) -> &'static str {
29 match role {
30 "OPEN" => "prefix",
31 "CLOSE" | "VERTBAR" | "PERIOD" | "POSTFIX" => "postfix",
32 "OPFUNCTION" | "TRIGFUNCTION" | "BIGOP" | "SUMOP" | "INTOP" | "LIMITOP" | "OPERATOR"
33 | "DIFFOP" => "prefix",
34 _ => "infix",
35 }
36}
37
38pub fn opdict_lookup(content: &str, role: &str) -> OpDictProperties {
43 let form = role_to_form(role);
44 let category = lookup_category(content, form);
45
46 let mut props = category_to_properties(category);
47
48 if is_fence_operator(content) {
50 props.fence = true;
51 }
52 if is_separator_operator(content) {
53 props.separator = true;
54 }
55
56 props
57}
58
59pub fn opdict_category(content: &str, role: &str) -> &'static str {
61 lookup_category(content, role_to_form(role))
62}
63
64fn lookup_category(content: &str, form: &str) -> &'static str {
68 let chars: Vec<char> = content.chars().collect();
69 let len = chars.len();
70
71 if len > 2 {
73 return "Default";
74 }
75 if len == 0 {
76 return "Default";
77 }
78
79 let code1 = chars[0] as u32;
80
81 if len == 1 && (0x0320..=0x03FF).contains(&code1) {
83 return "Default";
84 }
85
86 let effective_code = if len == 2 {
88 let code2 = chars[1] as u32;
89 if code2 == 0x0338 || code2 == 0x20D2 {
91 code1
92 }
93 else if let Some(idx) = two_ascii_char_index(content) {
95 0x0320 + idx as u32
96 } else {
97 return "Default";
98 }
99 } else {
100 code1
101 };
102
103 if form == "infix" && (effective_code == 0x007C || effective_code == 0x223C) {
105 return "ForceDefault";
106 }
107
108 lookup_content_form(effective_code, form).unwrap_or("Default")
110}
111
112fn two_ascii_char_index(content: &str) -> Option<usize> {
114 const OPS: &[&str] = &[
115 "!!", "!=", "&&", "**", "*=", "++", "+=", "--", "-=", "->", "//", "/=", ":=", "<=", "<>", "==",
116 ">=", "||",
117 ];
118 OPS.iter().position(|&op| op == content)
119}
120
121fn lookup_content_form(code: u32, form: &str) -> Option<&'static str> {
125 match form {
126 "infix" => lookup_infix(code),
127 "prefix" => lookup_prefix(code),
128 "postfix" => lookup_postfix(code),
129 _ => None,
130 }
131}
132
133const INFIX_TABLE: &[(u32, u32, &str)] = &[
135 (0x0025, 0x0025, "C"),
136 (0x002A, 0x002A, "C"),
137 (0x002B, 0x002B, "B"),
138 (0x002C, 0x002C, "M"),
139 (0x002D, 0x002D, "B"),
140 (0x002E, 0x002E, "C"),
141 (0x002F, 0x002F, "K"), (0x003A, 0x003A, "M"),
143 (0x003B, 0x003B, "M"),
144 (0x003F, 0x0040, "C"),
145 (0x005C, 0x005C, "K"),
146 (0x005E, 0x005E, "C"),
147 (0x005F, 0x005F, "K"),
148 (0x00B1, 0x00B1, "B"),
149 (0x00B7, 0x00B7, "C"),
150 (0x00D7, 0x00D7, "C"),
151 (0x00F7, 0x00F7, "B"),
152 (0x0322, 0x0322, "B"),
153 (0x0323, 0x0323, "C"),
154 (0x032E, 0x032E, "C"),
155 (0x2022, 0x2022, "C"),
156 (0x2043, 0x2043, "C"),
157 (0x2044, 0x2044, "B"),
158 (0x2061, 0x2064, "K"),
159 (0x2190, 0x2195, "A"),
160 (0x219A, 0x21AE, "A"),
161 (0x21B0, 0x21B5, "A"),
162 (0x21B9, 0x21B9, "A"),
163 (0x21BC, 0x21D5, "A"),
164 (0x21DA, 0x21F0, "A"),
165 (0x21F3, 0x21FF, "A"),
166 (0x2206, 0x2206, "K"),
167 (0x2212, 0x2216, "B"),
168 (0x2217, 0x2219, "C"),
169 (0x2227, 0x222A, "B"),
170 (0x2236, 0x2236, "B"),
171 (0x2238, 0x2238, "B"),
172 (0x2240, 0x2240, "C"),
173 (0x228C, 0x228E, "B"),
174 (0x2293, 0x2296, "B"),
175 (0x2297, 0x2297, "C"),
176 (0x2298, 0x2298, "B"),
177 (0x2299, 0x229B, "C"),
178 (0x229D, 0x229F, "B"),
179 (0x22A0, 0x22A1, "C"),
180 (0x22BA, 0x22BA, "C"),
181 (0x22BB, 0x22BD, "B"),
182 (0x22C4, 0x22C7, "C"),
183 (0x22C9, 0x22CC, "C"),
184 (0x22CE, 0x22CF, "B"),
185 (0x22D2, 0x22D3, "B"),
186 (0x2305, 0x2306, "C"),
187 (0x2794, 0x2794, "A"),
188 (0x2795, 0x2797, "B"),
189 (0x2799, 0x2799, "A"),
190 (0x279B, 0x27A1, "A"),
191 (0x27A5, 0x27A6, "A"),
192 (0x27A8, 0x27AF, "A"),
193 (0x27B1, 0x27B1, "A"),
194 (0x27B3, 0x27B3, "A"),
195 (0x27B5, 0x27B5, "A"),
196 (0x27B8, 0x27B8, "A"),
197 (0x27BA, 0x27BE, "A"),
198 (0x27CB, 0x27CB, "C"),
199 (0x27CD, 0x27CD, "C"),
200 (0x27F0, 0x27F1, "A"),
201 (0x27F4, 0x27FF, "A"),
202 (0x2900, 0x2920, "A"),
203 (0x2934, 0x2937, "A"),
204 (0x2942, 0x2975, "A"),
205 (0x297C, 0x297F, "A"),
206 (0x29B8, 0x29B8, "B"),
207 (0x29BC, 0x29BC, "B"),
208 (0x29C4, 0x29C5, "B"),
209 (0x29C6, 0x29C8, "C"),
210 (0x29D4, 0x29D7, "C"),
211 (0x29E2, 0x29E2, "C"),
212 (0x29F5, 0x29FB, "B"),
213 (0x2A1D, 0x2A1E, "C"),
214 (0x2A1F, 0x2A2E, "B"),
215 (0x2A2F, 0x2A37, "C"),
216 (0x2A38, 0x2A3A, "B"),
217 (0x2A3B, 0x2A3D, "C"),
218 (0x2A3E, 0x2A3E, "B"),
219 (0x2A3F, 0x2A3F, "C"),
220 (0x2A40, 0x2A4F, "B"),
221 (0x2A50, 0x2A50, "C"),
222 (0x2A51, 0x2A63, "B"),
223 (0x2A64, 0x2A65, "C"),
224 (0x2ADB, 0x2ADB, "B"),
225 (0x2ADC, 0x2ADD, "C"),
226 (0x2AF6, 0x2AF6, "B"),
227 (0x2AFB, 0x2AFB, "B"),
228 (0x2AFD, 0x2AFD, "B"),
229 (0x2AFE, 0x2AFE, "C"),
230 (0x2B04, 0x2B07, "A"),
231 (0x2B0C, 0x2B11, "A"),
232 (0x2B30, 0x2B3E, "A"),
233 (0x2B40, 0x2B4C, "A"),
234 (0x2B60, 0x2B65, "A"),
235 (0x2B6A, 0x2B6D, "A"),
236 (0x2B70, 0x2B73, "A"),
237 (0x2B7A, 0x2B7D, "A"),
238 (0x2B80, 0x2B87, "A"),
239 (0x2B95, 0x2B95, "A"),
240 (0x2BA0, 0x2BAF, "A"),
241 (0x2BB8, 0x2BB8, "A"),
242];
243const PREFIX_TABLE: &[(u32, u32, &str)] = &[
244 (0x0021, 0x0021, "D"),
245 (0x0028, 0x0028, "F"),
246 (0x002B, 0x002B, "D"),
247 (0x002D, 0x002D, "D"),
248 (0x005B, 0x005B, "F"),
249 (0x007B, 0x007B, "F"),
250 (0x007C, 0x007C, "F"),
251 (0x00AC, 0x00AC, "D"),
252 (0x00B1, 0x00B1, "D"),
253 (0x0331, 0x0331, "D"),
254 (0x2016, 0x2016, "F"),
255 (0x2018, 0x2018, "D"),
256 (0x201C, 0x201C, "D"),
257 (0x2145, 0x2146, "L"),
258 (0x2200, 0x2201, "D"),
259 (0x2202, 0x2202, "L"),
260 (0x2203, 0x2204, "D"),
261 (0x2207, 0x2207, "D"),
262 (0x220F, 0x2211, "J"),
263 (0x2212, 0x2213, "D"),
264 (0x221A, 0x221C, "L"),
265 (0x221F, 0x2222, "D"),
266 (0x222B, 0x2233, "H"),
267 (0x2234, 0x2235, "D"),
268 (0x223C, 0x223C, "D"),
269 (0x22BE, 0x22BF, "D"),
270 (0x22C0, 0x22C3, "J"),
271 (0x2308, 0x2308, "F"),
272 (0x230A, 0x230A, "F"),
273 (0x2310, 0x2310, "D"),
274 (0x2319, 0x2319, "D"),
275 (0x2329, 0x2329, "F"),
276 (0x2772, 0x2772, "F"),
277 (0x2795, 0x2796, "D"),
278 (0x27C0, 0x27C0, "D"),
279 (0x27E6, 0x27E6, "F"),
280 (0x27E8, 0x27E8, "F"),
281 (0x27EA, 0x27EA, "F"),
282 (0x27EC, 0x27EC, "F"),
283 (0x27EE, 0x27EE, "F"),
284 (0x2980, 0x2980, "F"),
285 (0x2983, 0x2983, "F"),
286 (0x2985, 0x2985, "F"),
287 (0x2987, 0x2987, "F"),
288 (0x2989, 0x2989, "F"),
289 (0x298B, 0x298B, "F"),
290 (0x298D, 0x298D, "F"),
291 (0x298F, 0x298F, "F"),
292 (0x2991, 0x2991, "F"),
293 (0x2993, 0x2993, "F"),
294 (0x2995, 0x2995, "F"),
295 (0x2997, 0x2997, "F"),
296 (0x2999, 0x2999, "F"),
297 (0x299B, 0x29AF, "D"),
298 (0x29D8, 0x29D8, "F"),
299 (0x29DA, 0x29DA, "F"),
300 (0x29FC, 0x29FC, "F"),
301 (0x2A00, 0x2A0A, "J"),
302 (0x2A0B, 0x2A1C, "H"),
303 (0x2A1D, 0x2A1E, "J"),
304 (0x2AEC, 0x2AED, "D"),
305 (0x2AFC, 0x2AFC, "J"),
306 (0x2AFF, 0x2AFF, "J"),
307];
308const POSTFIX_TABLE: &[(u32, u32, &str)] = &[
309 (0x0021, 0x0022, "E"),
310 (0x0025, 0x0027, "E"),
311 (0x0029, 0x0029, "G"),
312 (0x005D, 0x005D, "G"),
313 (0x005E, 0x005F, "I"),
314 (0x0060, 0x0060, "E"),
315 (0x007C, 0x007C, "G"),
316 (0x007D, 0x007D, "G"),
317 (0x007E, 0x007E, "I"),
318 (0x00A8, 0x00A8, "E"),
319 (0x00AF, 0x00AF, "I"),
320 (0x00B0, 0x00B0, "E"),
321 (0x00B2, 0x00B4, "E"),
322 (0x00B8, 0x00B9, "E"),
323 (0x02C6, 0x02C7, "I"),
324 (0x02C9, 0x02C9, "I"),
325 (0x02CA, 0x02CB, "E"),
326 (0x02CD, 0x02CD, "I"),
327 (0x02D8, 0x02DA, "E"),
328 (0x02DC, 0x02DC, "I"),
329 (0x02DD, 0x02DD, "E"),
330 (0x02F7, 0x02F7, "I"),
331 (0x0302, 0x0302, "I"),
332 (0x0311, 0x0311, "E"),
333 (0x0320, 0x0320, "E"),
334 (0x0325, 0x0325, "E"),
335 (0x0327, 0x0327, "E"),
336 (0x0331, 0x0331, "E"),
337 (0x2016, 0x2016, "G"),
338 (0x2019, 0x201B, "E"),
339 (0x201D, 0x201F, "E"),
340 (0x2032, 0x2037, "E"),
341 (0x203E, 0x203E, "I"),
342 (0x2057, 0x2057, "E"),
343 (0x20DB, 0x20DC, "E"),
344 (0x2309, 0x2309, "G"),
345 (0x230B, 0x230B, "G"),
346 (0x2322, 0x2323, "I"),
347 (0x232A, 0x232A, "G"),
348 (0x23B4, 0x23B5, "I"),
349 (0x23CD, 0x23CD, "E"),
350 (0x23DC, 0x23E1, "I"),
351 (0x2773, 0x2773, "G"),
352 (0x27E7, 0x27E7, "G"),
353 (0x27E9, 0x27E9, "G"),
354 (0x27EB, 0x27EB, "G"),
355 (0x27ED, 0x27ED, "G"),
356 (0x27EF, 0x27EF, "G"),
357 (0x2980, 0x2980, "G"),
358 (0x2984, 0x2984, "G"),
359 (0x2986, 0x2986, "G"),
360 (0x2988, 0x2988, "G"),
361 (0x298A, 0x298A, "G"),
362 (0x298C, 0x298C, "G"),
363 (0x298E, 0x298E, "G"),
364 (0x2990, 0x2990, "G"),
365 (0x2992, 0x2992, "G"),
366 (0x2994, 0x2994, "G"),
367 (0x2996, 0x2996, "G"),
368 (0x2998, 0x2998, "G"),
369 (0x2999, 0x2999, "G"),
370 (0x29D9, 0x29D9, "G"),
371 (0x29DB, 0x29DB, "G"),
372 (0x29FD, 0x29FD, "G"),
373 (0x1EEF0, 0x1EEF1, "I"),
374];
375const FENCE_TABLE: &[(u32, u32)] = &[
376 (0x0028, 0x0029),
377 (0x005B, 0x005B),
378 (0x005D, 0x005D),
379 (0x007B, 0x007D),
380 (0x0331, 0x0331),
381 (0x2016, 0x2016),
382 (0x2018, 0x2019),
383 (0x201C, 0x201D),
384 (0x2308, 0x230B),
385 (0x2329, 0x232A),
386 (0x2772, 0x2773),
387 (0x27E6, 0x27EF),
388 (0x2980, 0x2980),
389 (0x2983, 0x2999),
390 (0x29D8, 0x29DB),
391 (0x29FC, 0x29FD),
392];
393
394fn range_lookup(table: &[(u32, u32, &'static str)], code: u32) -> Option<&'static str> {
396 match table.binary_search_by(|&(a, b, _)| {
397 if code < a {
398 std::cmp::Ordering::Greater
399 } else if code > b {
400 std::cmp::Ordering::Less
401 } else {
402 std::cmp::Ordering::Equal
403 }
404 }) {
405 Ok(i) => Some(table[i].2),
406 Err(_) => None,
407 }
408}
409
410fn lookup_infix(code: u32) -> Option<&'static str> { range_lookup(INFIX_TABLE, code) }
411fn lookup_prefix(code: u32) -> Option<&'static str> { range_lookup(PREFIX_TABLE, code) }
412fn lookup_postfix(code: u32) -> Option<&'static str> { range_lookup(POSTFIX_TABLE, code) }
413
414fn category_to_properties(category: &str) -> OpDictProperties {
418 match category {
419 "A" => OpDictProperties {
420 lspace: THICK,
421 rspace: THICK,
422 stretchy: true,
423 ..Default::default()
424 },
425 "B" => OpDictProperties {
426 lspace: MED,
427 rspace: MED,
428 ..Default::default()
429 },
430 "C" => OpDictProperties {
431 lspace: THIN,
432 rspace: THIN,
433 ..Default::default()
434 },
435 "D" => OpDictProperties {
436 lspace: 0.0,
437 rspace: 0.0,
438 ..Default::default()
439 },
440 "E" => OpDictProperties {
441 lspace: 0.0,
442 rspace: 0.0,
443 ..Default::default()
444 },
445 "F" => OpDictProperties {
446 lspace: 0.0,
447 rspace: 0.0,
448 stretchy: true,
449 symmetric: true,
450 ..Default::default()
451 },
452 "G" => OpDictProperties {
453 lspace: 0.0,
454 rspace: 0.0,
455 stretchy: true,
456 symmetric: true,
457 ..Default::default()
458 },
459 "H" => OpDictProperties {
460 lspace: THIN,
461 rspace: THIN,
462 symmetric: true,
463 largeop: true,
464 ..Default::default()
465 },
466 "I" => OpDictProperties {
467 lspace: 0.0,
468 rspace: 0.0,
469 stretchy: true,
470 ..Default::default()
471 },
472 "J" => OpDictProperties {
473 lspace: THIN,
474 rspace: THIN,
475 symmetric: true,
476 largeop: true,
477 movablelimits: true,
478 ..Default::default()
479 },
480 "K" => OpDictProperties {
481 lspace: 0.0,
482 rspace: 0.0,
483 ..Default::default()
484 },
485 "L" => OpDictProperties {
486 lspace: THIN,
487 rspace: 0.0,
488 ..Default::default()
489 },
490 "M" => OpDictProperties {
491 lspace: 0.0,
492 rspace: THIN,
493 ..Default::default()
494 },
495 _ => OpDictProperties {
496 lspace: THICK,
497 rspace: THICK,
498 ..Default::default()
499 }, }
501}
502
503fn is_fence_operator(content: &str) -> bool {
505 let c = match content.chars().next() {
506 Some(c) => c as u32,
507 None => return false,
508 };
509 FENCE_TABLE.iter().any(|&(a, b)| (a..=b).contains(&c))
510}
511
512fn is_separator_operator(content: &str) -> bool {
514 let c = match content.chars().next() {
515 Some(c) => c as u32,
516 None => return false,
517 };
518 c == 0x002C || c == 0x003B || c == 0x2063
519}
520
521#[cfg(test)]
522mod tests {
523 use super::*;
524
525 #[test]
526 fn test_content_form_table_regressions() {
527 let p = opdict_lookup("\u{2A50}", "BINOP");
529 assert_eq!(p.lspace, THIN);
530 assert_eq!(p.rspace, THIN);
531 let p = opdict_lookup("\u{27A1}", "ARROW");
533 assert_eq!(p.lspace, THICK);
534 let p = opdict_lookup("\u{0331}", "OPEN");
536 assert!(p.fence);
537 }
538
539 #[test]
540 fn test_opdict_lookup_plus() {
541 let props = opdict_lookup("+", "ADDOP");
542 assert!((props.lspace - MED).abs() < 0.001); assert!((props.rspace - MED).abs() < 0.001);
544 assert!(!props.stretchy);
545 }
546
547 #[test]
555 fn prefix_role_zeroes_left_spacing_that_infix_role_keeps() {
556 for glyph in ["\u{2212}", "+"] {
557 let prefix = opdict_lookup(glyph, "OPERATOR");
558 let infix = opdict_lookup(glyph, "ADDOP");
559 assert_eq!(prefix.lspace, 0.0, "{glyph} prefix (OPERATOR) lspace");
560 assert!(
561 (infix.lspace - MED).abs() < 0.001,
562 "{glyph} infix (ADDOP) lspace should be MED"
563 );
564 assert!(
566 infix.lspace > prefix.lspace,
567 "{glyph}: infix must reserve more left space than prefix"
568 );
569 }
570 }
571
572 #[test]
573 fn test_opdict_lookup_open_paren() {
574 let props = opdict_lookup("(", "OPEN");
575 assert!((props.lspace - 0.0).abs() < 0.001); assert!(props.stretchy);
577 assert!(props.symmetric);
578 assert!(props.fence);
579 }
580
581 #[test]
582 fn test_opdict_lookup_integral() {
583 let props = opdict_lookup("\u{222B}", "INTOP");
584 assert!(props.largeop); assert!(props.symmetric);
586 }
587
588 #[test]
589 fn test_opdict_lookup_sum() {
590 let props = opdict_lookup("\u{2211}", "SUMOP");
591 assert!(props.largeop); assert!(props.movablelimits);
593 }
594
595 #[test]
596 fn test_opdict_lookup_comma() {
597 let props = opdict_lookup(",", "PUNCT");
598 assert!(props.separator);
599 assert!((props.rspace - THIN).abs() < 0.001); }
601
602 #[test]
603 fn test_opdict_lookup_invisible_times() {
604 let props = opdict_lookup("\u{2062}", "MULOP");
605 assert!((props.lspace - 0.0).abs() < 0.001); assert!((props.rspace - 0.0).abs() < 0.001);
607 }
608
609 #[test]
610 fn test_opdict_category_k_is_infix_only() {
611 for op in ["/", "\\", "_", "\u{2206}", "\u{2061}", "\u{2062}"] {
612 assert_eq!(opdict_category(op, "MULOP"), "K", "{op:?}");
613 }
614 assert_eq!(opdict_category("/", "OPEN"), "Default");
615 assert_eq!(opdict_category("\u{2215}", "MULOP"), "B");
616 }
617
618 #[test]
619 fn test_two_ascii_char_ops() {
620 assert_eq!(two_ascii_char_index("!="), Some(1));
621 assert_eq!(two_ascii_char_index("->"), Some(9));
622 assert_eq!(two_ascii_char_index("xx"), None);
623 }
624}