1use crate::{Class, Font};
28
29const LARGE_OPS: &[char] = &[
31 '\u{2211}', '\u{220F}', '\u{2210}', '\u{22C0}', '\u{22C1}', '\u{22C2}', '\u{22C3}', '\u{2A00}',
32 '\u{2A01}', '\u{2A02}', '\u{2A03}', '\u{2A04}', '\u{2A05}', '\u{2A06}',
33];
34
35const INTEGRALS: &[char] = &[
38 '\u{222B}', '\u{222C}', '\u{222D}', '\u{222E}', '\u{222F}', '\u{2230}', '\u{2232}', '\u{2233}',
39];
40
41const BINARY: &[char] = &[
42 '+', '-', '*', '\u{2212}', '\u{00B1}', '\u{2213}', '\u{00D7}', '\u{00F7}', '\u{22C5}',
43 '\u{2217}', '\u{00B7}', '\u{2218}', '\u{2216}', '\u{2227}', '\u{2228}', '\u{2229}', '\u{222A}',
44 '\u{2295}', '\u{2296}', '\u{2297}', '\u{2299}', '\u{2216}',
45];
46
47const RELATIONS: &[char] = &[
48 '=', '<', '>', ':', '\u{2264}', '\u{2265}', '\u{2260}', '\u{2248}', '\u{2261}', '\u{2208}',
49 '\u{2209}', '\u{2282}', '\u{2283}', '\u{2286}', '\u{2287}', '\u{2192}', '\u{2190}', '\u{2194}',
50 '\u{21D2}', '\u{21D4}', '\u{21A6}', '\u{2191}', '\u{2193}', '\u{27F9}', '\u{27FA}', '\u{223C}',
51 '\u{221D}', '\u{2245}', '\u{225C}', '\u{2323}', '\u{2254}', '\u{2255}', '\u{220B}', '\u{2225}',
52 '\u{2223}', '\u{2224}', '\u{22A5}', '\u{224D}', '\u{226A}', '\u{226B}', '\u{227D}',
53];
54
55const OPENERS: &[char] = &[
56 '(', '[', '{', '\u{27E8}', '\u{230A}', '\u{2308}', '\u{27E6}', '\u{301A}',
57];
58const CLOSERS: &[char] = &[
59 ')', ']', '}', '\u{27E9}', '\u{230B}', '\u{2309}', '\u{27E7}', '\u{301B}',
60];
61const PUNCTUATION: &[char] = &[',', ';'];
62
63const MARKUP_ORDINARY: &[char] = &[
66 '\u{2A00}', '\u{2A03}', '\u{2A04}', '\u{2A05}', '\u{2A06}', '\u{2229}', '\u{222A}', '\u{2191}',
67 '\u{2193}', '\u{2194}', '\u{21A6}', '\u{27F9}', '\u{27FA}', '\u{220B}', '\u{2225}', '\u{2223}',
68 '\u{2224}', '\u{22A5}', '\u{224D}', '\u{226A}', '\u{226B}',
69];
70
71const MARKUP_BINARY: &[char] = &['/'];
74
75const MARKUP_PUNCTUATION: &[char] = &[':'];
78
79#[derive(Clone, Copy, Debug, PartialEq, Eq)]
81pub enum Convention {
82 MathType,
84 Markup,
86}
87
88const fn holds(table: &[char], ch: char) -> bool {
93 let mut index = 0;
94 while index < table.len() {
95 if table[index] == ch {
96 return true;
97 }
98 index += 1;
99 }
100 false
101}
102
103const fn is_large_op(ch: char) -> bool {
105 holds(LARGE_OPS, ch) || holds(INTEGRALS, ch)
106}
107
108pub const fn is_integral_sign(ch: char) -> bool {
110 holds(INTEGRALS, ch)
111}
112
113const fn mathtype_class(ch: char) -> Class {
115 if is_large_op(ch) {
116 Class::Op
117 } else if holds(OPENERS, ch) {
118 Class::Open
119 } else if holds(CLOSERS, ch) {
120 Class::Close
121 } else if holds(PUNCTUATION, ch) {
122 Class::Punct
123 } else if holds(BINARY, ch) {
124 Class::Bin
125 } else if holds(RELATIONS, ch) {
126 Class::Rel
127 } else {
128 Class::Ord
129 }
130}
131
132pub const fn class_of_char(ch: char) -> Class {
137 class_of_char_with(ch, Convention::MathType)
138}
139
140pub const fn class_of_char_with(ch: char, convention: Convention) -> Class {
142 match convention {
143 Convention::MathType => mathtype_class(ch),
144 Convention::Markup => {
145 if holds(MARKUP_ORDINARY, ch) {
146 Class::Ord
147 } else if holds(MARKUP_BINARY, ch) {
148 Class::Bin
149 } else if holds(MARKUP_PUNCTUATION, ch) {
150 Class::Punct
151 } else {
152 mathtype_class(ch)
153 }
154 }
155 }
156}
157
158pub const fn is_large_op_with(ch: char, convention: Convention) -> bool {
160 match convention {
161 Convention::MathType => is_large_op(ch),
162 Convention::Markup => is_large_op(ch) && !holds(MARKUP_ORDINARY, ch),
163 }
164}
165
166pub fn is_symbol_face(ch: char) -> bool {
175 if ch.is_ascii() {
176 return false;
177 }
178 is_large_op(ch)
179 || matches!(class_of_char(ch), Class::Bin | Class::Rel)
180 || matches!(
181 ch,
182 '\u{2200}'
183 | '\u{2203}'
184 | '\u{2205}'
185 | '\u{221E}'
186 | '\u{2202}'
187 | '\u{2207}'
188 | '\u{210F}'
189 | '\u{2113}'
190 | '\u{2135}'
191 | '\u{2118}'
192 | '\u{2220}'
193 | '\u{22EF}'
194 )
195}
196
197pub fn symbol_position(ch: char) -> Option<(Font, u8)> {
210 symbol_code(ch)
211 .map(|code| (Font::Symbol, code))
212 .or_else(|| extra_code(ch).map(|code| (Font::Extra, code)))
213}
214
215pub fn euclid_math_two_position(ch: char) -> Option<u8> {
222 match ch {
223 '\u{227D}' => Some(0xB1),
224 _ => None,
225 }
226}
227
228pub fn symbol_code(ch: char) -> Option<u8> {
244 if ch.is_ascii() {
245 return Some(ch as u8);
246 }
247 let index = SYMBOL_CODES
248 .binary_search_by_key(&ch, |(character, _)| *character)
249 .ok()?;
250 Some(SYMBOL_CODES[index].1)
251}
252
253pub const SYMBOL_RADICAL: char = '\u{F0D6}';
259
260pub fn symbol_private_use_code(ch: char) -> Option<u8> {
269 let code = u32::from(ch);
270 (code == u32::from(SYMBOL_RADICAL) || (0xF0E6..=0xF0FE).contains(&code)).then_some(code as u8)
271}
272
273pub fn extra_code(ch: char) -> Option<u8> {
283 let index = EXTRA_CODES
284 .binary_search_by_key(&ch, |(character, _)| *character)
285 .ok()?;
286 Some(EXTRA_CODES[index].1)
287}
288
289pub fn symbol_positions() -> &'static [(char, u8)] {
294 SYMBOL_CODES
295}
296
297pub fn extra_positions() -> &'static [(char, u8)] {
301 EXTRA_CODES
302}
303
304const SYMBOL_CODES: &[(char, u8)] = &[
309 ('\u{00AC}', 0xD8),
310 ('\u{00B0}', 0xB0),
311 ('\u{00B1}', 0xB1),
312 ('\u{00B5}', 0x6D),
313 ('\u{00D7}', 0xB4),
314 ('\u{00F7}', 0xB8),
315 ('\u{0192}', 0xA6),
316 ('\u{0391}', 0x41),
317 ('\u{0392}', 0x42),
318 ('\u{0393}', 0x47),
319 ('\u{0394}', 0x44),
320 ('\u{0395}', 0x45),
321 ('\u{0396}', 0x5A),
322 ('\u{0397}', 0x48),
323 ('\u{0398}', 0x51),
324 ('\u{0399}', 0x49),
325 ('\u{039A}', 0x4B),
326 ('\u{039B}', 0x4C),
327 ('\u{039C}', 0x4D),
328 ('\u{039D}', 0x4E),
329 ('\u{039E}', 0x58),
330 ('\u{039F}', 0x4F),
331 ('\u{03A0}', 0x50),
332 ('\u{03A1}', 0x52),
333 ('\u{03A3}', 0x53),
334 ('\u{03A4}', 0x54),
335 ('\u{03A5}', 0x55),
336 ('\u{03A6}', 0x46),
337 ('\u{03A7}', 0x43),
338 ('\u{03A8}', 0x59),
339 ('\u{03A9}', 0x57),
340 ('\u{03B1}', 0x61),
341 ('\u{03B2}', 0x62),
342 ('\u{03B3}', 0x67),
343 ('\u{03B4}', 0x64),
344 ('\u{03B5}', 0x65),
345 ('\u{03B6}', 0x7A),
346 ('\u{03B7}', 0x68),
347 ('\u{03B8}', 0x71),
348 ('\u{03B9}', 0x69),
349 ('\u{03BA}', 0x6B),
350 ('\u{03BB}', 0x6C),
351 ('\u{03BC}', 0x6D),
352 ('\u{03BD}', 0x6E),
353 ('\u{03BE}', 0x78),
354 ('\u{03BF}', 0x6F),
355 ('\u{03C0}', 0x70),
356 ('\u{03C1}', 0x72),
357 ('\u{03C2}', 0x56),
358 ('\u{03C3}', 0x73),
359 ('\u{03C4}', 0x74),
360 ('\u{03C5}', 0x75),
361 ('\u{03C6}', 0x6A),
362 ('\u{03C7}', 0x63),
363 ('\u{03C8}', 0x79),
364 ('\u{03C9}', 0x77),
365 ('\u{03D1}', 0x4A),
366 ('\u{03D2}', 0xA1),
367 ('\u{03D5}', 0x66),
368 ('\u{03D6}', 0x76),
369 ('\u{2022}', 0xB7),
370 ('\u{2026}', 0xBC),
371 ('\u{2032}', 0xA2),
372 ('\u{2033}', 0xB2),
373 ('\u{2044}', 0xA4),
374 ('\u{20AC}', 0xA0),
375 ('\u{2111}', 0xC1),
376 ('\u{2118}', 0xC3),
377 ('\u{211C}', 0xC2),
378 ('\u{2126}', 0x57),
379 ('\u{2135}', 0xC0),
380 ('\u{2190}', 0xAC),
381 ('\u{2191}', 0xAD),
382 ('\u{2192}', 0xAE),
383 ('\u{2193}', 0xAF),
384 ('\u{2194}', 0xAB),
385 ('\u{21B5}', 0xBF),
386 ('\u{21D0}', 0xDC),
387 ('\u{21D1}', 0xDD),
388 ('\u{21D2}', 0xDE),
389 ('\u{21D3}', 0xDF),
390 ('\u{21D4}', 0xDB),
391 ('\u{2200}', 0x22),
392 ('\u{2202}', 0xB6),
393 ('\u{2203}', 0x24),
394 ('\u{2205}', 0xC6),
395 ('\u{2206}', 0x44),
396 ('\u{2207}', 0xD1),
397 ('\u{2208}', 0xCE),
398 ('\u{2209}', 0xCF),
399 ('\u{220B}', 0x27),
400 ('\u{220F}', 0xD5),
401 ('\u{2211}', 0xE5),
402 ('\u{2212}', 0x2D),
403 ('\u{2217}', 0x2A),
404 ('\u{221A}', 0xD6),
405 ('\u{221D}', 0xB5),
406 ('\u{221E}', 0xA5),
407 ('\u{2220}', 0xD0),
408 ('\u{2223}', 0x7C),
409 ('\u{2227}', 0xD9),
410 ('\u{2228}', 0xDA),
411 ('\u{2229}', 0xC7),
412 ('\u{222A}', 0xC8),
413 ('\u{222B}', 0xF2),
414 ('\u{2234}', 0x5C),
415 ('\u{223C}', 0x7E),
416 ('\u{2245}', 0x40),
417 ('\u{2248}', 0xBB),
418 ('\u{2260}', 0xB9),
419 ('\u{2261}', 0xBA),
420 ('\u{2264}', 0xA3),
421 ('\u{2265}', 0xB3),
422 ('\u{2282}', 0xCC),
423 ('\u{2283}', 0xC9),
424 ('\u{2284}', 0xCB),
425 ('\u{2286}', 0xCD),
426 ('\u{2287}', 0xCA),
427 ('\u{2295}', 0xC5),
428 ('\u{2297}', 0xC4),
429 ('\u{22A5}', 0x5E),
430 ('\u{22C5}', 0xD7),
431 ('\u{2320}', 0xF3),
432 ('\u{2321}', 0xF5),
433 ('\u{2329}', 0xE1),
434 ('\u{232A}', 0xF1),
435 ('\u{25CA}', 0xE0),
436 ('\u{2660}', 0xAA),
437 ('\u{2663}', 0xA7),
438 ('\u{2665}', 0xA9),
439 ('\u{2666}', 0xA8),
440];
441
442const EXTRA_CODES: &[(char, u8)] = &[
447 ('\u{019B}', 0x44),
448 ('\u{0300}', 0x23),
449 ('\u{0302}', 0x24),
450 ('\u{0303}', 0x25),
451 ('\u{0307}', 0x26),
452 ('\u{2026}', 0x4B),
453 ('\u{2035}', 0x60),
454 ('\u{20D0}', 0x77),
455 ('\u{20D1}', 0x76),
456 ('\u{20D6}', 0x73),
457 ('\u{20D7}', 0x72),
458 ('\u{20E1}', 0x74),
459 ('\u{2102}', 0xA3),
460 ('\u{210F}', 0x68),
461 ('\u{2113}', 0x6C),
462 ('\u{2115}', 0xA5),
463 ('\u{211A}', 0xA4),
464 ('\u{211D}', 0xA1),
465 ('\u{2124}', 0xA2),
466 ('\u{2195}', 0x62),
467 ('\u{21A6}', 0x61),
468 ('\u{21D5}', 0x63),
469 ('\u{2210}', 0x43),
470 ('\u{2213}', 0x6D),
471 ('\u{2218}', 0x6F),
472 ('\u{2229}', 0x49),
473 ('\u{222A}', 0x55),
474 ('\u{2235}', 0x51),
475 ('\u{225C}', 0x40),
476 ('\u{227A}', 0x70),
477 ('\u{227B}', 0x66),
478 ('\u{22C2}', 0x49),
479 ('\u{22C3}', 0x55),
480 ('\u{22EE}', 0x4D),
481 ('\u{22EF}', 0x4C),
482 ('\u{22F0}', 0x4E),
483 ('\u{22F1}', 0x4F),
484 ('\u{25B9}', 0x3E),
485 ('\u{25C3}', 0x3C),
486 ('\u{FE36}', 0x7B),
487 ('\u{FE38}', 0x7D),
488];
489
490pub fn is_cjk(ch: char) -> bool {
496 matches!(
497 ch as u32,
498 0x1100..=0x11FF | 0x2E80..=0x2FDF | 0x2FF0..=0x2FFF | 0x3000..=0x30FF | 0x3100..=0x312F | 0x3130..=0x318F | 0x3190..=0x319F | 0x31A0..=0x31BF | 0x31C0..=0x31EF | 0x31F0..=0x31FF | 0x3200..=0x33FF | 0x3400..=0x4DBF | 0x4E00..=0x9FFF | 0xA960..=0xA97F | 0xAC00..=0xD7AF | 0xD7B0..=0xD7FF | 0xF900..=0xFAFF | 0xFE10..=0xFE1F | 0xFE30..=0xFE4F | 0xFE50..=0xFE6F | 0xFF00..=0xFFEF | 0x1B000..=0x1B16F | 0x1F200..=0x1F2FF | 0x20000..=0x3FFFF )
523}
524
525const EXTRA_LETTERS: [(char, char, Font); 23] = [
535 ('\u{2102}', 'C', Font::DoubleStruck),
536 ('\u{210A}', 'g', Font::Script),
537 ('\u{210B}', 'H', Font::Script),
538 ('\u{210C}', 'H', Font::Fraktur),
539 ('\u{210D}', 'H', Font::DoubleStruck),
540 ('\u{2110}', 'I', Font::Script),
541 ('\u{2111}', 'I', Font::Fraktur),
542 ('\u{2112}', 'L', Font::Script),
543 ('\u{2115}', 'N', Font::DoubleStruck),
544 ('\u{2119}', 'P', Font::DoubleStruck),
545 ('\u{211A}', 'Q', Font::DoubleStruck),
546 ('\u{211B}', 'R', Font::Script),
547 ('\u{211C}', 'R', Font::Fraktur),
548 ('\u{211D}', 'R', Font::DoubleStruck),
549 ('\u{2124}', 'Z', Font::DoubleStruck),
550 ('\u{2128}', 'Z', Font::Fraktur),
551 ('\u{212C}', 'B', Font::Script),
552 ('\u{212D}', 'C', Font::Fraktur),
553 ('\u{212F}', 'e', Font::Script),
554 ('\u{2130}', 'E', Font::Script),
555 ('\u{2131}', 'F', Font::Script),
556 ('\u{2133}', 'M', Font::Script),
557 ('\u{2134}', 'o', Font::Script),
558];
559
560pub fn extra_letter(ch: char) -> Option<(char, Font)> {
562 let index = EXTRA_LETTERS
563 .binary_search_by_key(&ch, |(symbol, _, _)| *symbol)
564 .ok()?;
565 let (_, letter, font) = EXTRA_LETTERS[index];
566 Some((letter, font))
567}
568
569pub fn extra_symbol(letter: char, font: Font) -> Option<char> {
571 EXTRA_LETTERS
572 .iter()
573 .find(|(_, saved, face)| *saved == letter && *face == font)
574 .map(|(symbol, _, _)| *symbol)
575}
576
577pub fn is_extra_face(ch: char) -> bool {
579 extra_letter(ch).is_some()
580}
581
582#[cfg(test)]
583mod tests {
584 use super::{
585 Convention, SYMBOL_RADICAL, class_of_char, class_of_char_with, extra_code, is_cjk,
586 is_integral_sign, is_large_op_with, is_symbol_face, symbol_code, symbol_position,
587 symbol_private_use_code,
588 };
589 use crate::{Class, Font};
590
591 #[test]
592 fn cjk_letters_and_punctuation_take_the_cjk_face() {
593 for ch in [
594 '\u{4E3A}',
595 '\u{4E2D}',
596 '\u{3001}',
597 '\u{3002}',
598 '\u{300C}',
599 '\u{3042}',
600 '\u{30A2}',
601 '\u{2FF0}',
602 '\u{31B0}',
603 '\u{31F0}',
604 '\u{FF08}',
605 '\u{FF11}',
606 '\u{FA0E}',
607 '\u{1B000}',
608 '\u{1F200}',
609 '\u{20000}',
610 '\u{1100}',
611 '\u{3131}',
612 '\u{AC00}',
613 '\u{3190}',
614 '\u{FE10}',
615 '\u{FE50}',
616 ] {
617 assert!(is_cjk(ch), "{ch:?} is drawn from the CJK face");
618 }
619 }
620
621 #[test]
622 fn latin_letters_greek_and_math_symbols_stay_out_of_the_cjk_face() {
623 for ch in [
624 'A', 'x', '0', '\u{03B1}', '\u{2211}', '\u{2192}', '\u{00E9}',
625 ] {
626 assert!(!is_cjk(ch), "{ch:?} is not drawn from the CJK face");
627 }
628 }
629
630 #[test]
631 fn operators_relations_and_large_operators_take_the_symbol_face() {
632 for ch in [
633 '\u{2212}', '\u{2211}', '\u{2208}', '\u{2264}', '\u{00B1}', '\u{00D7}', '\u{222B}',
634 '\u{220F}',
635 ] {
636 assert!(is_symbol_face(ch), "{ch:?} is drawn from the Symbol face");
637 }
638 }
639
640 #[test]
641 fn ordinary_symbols_the_text_face_lacks_take_the_symbol_face() {
642 for ch in [
643 '\u{2205}', '\u{221E}', '\u{2202}', '\u{2207}', '\u{2200}', '\u{2203}',
644 ] {
645 assert!(is_symbol_face(ch), "{ch:?} is drawn from the Symbol face");
646 }
647 }
648
649 #[test]
650 fn ascii_and_variable_letters_stay_in_the_text_face() {
651 for ch in [
652 '-', '+', '=', 'x', 'A', '0', '\u{03B1}', '\u{03C6}', '\u{221A}',
653 ] {
654 assert!(!is_symbol_face(ch), "{ch:?} is drawn from the text face");
655 }
656 }
657
658 #[test]
659 fn the_symbol_table_names_the_legacy_positions() {
660 assert_eq!(symbol_code('A'), Some(0x41));
661 assert_eq!(symbol_code('\u{03B1}'), Some(0x61));
662 assert_eq!(symbol_code('\u{0394}'), Some(0x44));
663 assert_eq!(symbol_code('\u{03BC}'), Some(0x6D));
664 assert_eq!(symbol_code('\u{03A9}'), Some(0x57));
665 assert_eq!(symbol_code('\u{03C2}'), Some(0x56));
666 assert_eq!(
667 symbol_code('\u{03C6}'),
668 Some(0x6A),
669 "the loopy phi takes the position the Symbol font draws it at"
670 );
671 assert_eq!(
672 symbol_code('\u{03D5}'),
673 Some(0x66),
674 "the straight phi takes the position the Symbol font draws it at"
675 );
676 assert_eq!(symbol_code('\u{2211}'), Some(0xE5));
677 assert_eq!(symbol_code('\u{222B}'), Some(0xF2));
678 assert_eq!(symbol_code('\u{2264}'), Some(0xA3));
679 assert_eq!(symbol_code('\u{1D465}'), None);
680 }
681
682 #[test]
683 fn integral_signs_are_told_apart_from_other_large_operators() {
684 for ch in "\u{222B}\u{222C}\u{222D}\u{222E}\u{222F}\u{2230}\u{2232}\u{2233}".chars() {
685 assert!(is_integral_sign(ch) && is_large_op_with(ch, Convention::MathType));
686 }
687 assert!(!is_integral_sign('\u{2211}'));
688 assert!(is_large_op_with('\u{2211}', Convention::MathType));
689 }
690
691 #[test]
692 fn private_use_codepoints_name_symbol_positions() {
693 assert_eq!(symbol_private_use_code(SYMBOL_RADICAL), Some(0xD6));
694 assert_eq!(symbol_private_use_code('\u{F0E6}'), Some(0xE6));
695 assert_eq!(symbol_private_use_code('\u{F0F0}'), Some(0xF0));
696 assert_eq!(symbol_private_use_code('\u{F0FE}'), Some(0xFE));
697 assert_eq!(symbol_private_use_code('\u{F0D5}'), None);
699 assert_eq!(symbol_private_use_code('\u{F0E5}'), None);
700 assert_eq!(symbol_private_use_code('\u{F0FF}'), None);
701 assert_eq!(symbol_private_use_code('\u{F000}'), None);
703 assert_eq!(symbol_private_use_code('\u{F041}'), None);
704 assert_eq!(symbol_private_use_code('\u{F0B3}'), None);
705 }
706
707 #[test]
708 fn a_bare_character_classifies_by_the_latex_conventions() {
709 assert_eq!(class_of_char('\u{2211}'), Class::Op);
710 assert_eq!(class_of_char('\u{2212}'), Class::Bin);
711 assert_eq!(class_of_char('\u{2208}'), Class::Rel);
712 assert_eq!(class_of_char('('), Class::Open);
713 assert_eq!(class_of_char(']'), Class::Close);
714 assert_eq!(class_of_char(','), Class::Punct);
715 assert_eq!(class_of_char('x'), Class::Ord);
716 assert_eq!(class_of_char('0'), Class::Ord);
717 }
718
719 #[test]
720 fn the_succeeds_or_equal_relation_is_a_relation() {
721 assert_eq!(class_of_char('\u{227D}'), Class::Rel);
722 assert!(
723 is_symbol_face('\u{227D}'),
724 "MathType draws the relation from a face other than the text face"
725 );
726 }
727
728 #[test]
729 fn the_euclid_math_two_encoding_names_the_succeeds_or_equal_position() {
730 use super::euclid_math_two_position;
731 assert_eq!(euclid_math_two_position('\u{227D}'), Some(0xB1));
732 assert_eq!(euclid_math_two_position('\u{227C}'), None);
733 assert_eq!(euclid_math_two_position('Q'), None);
734 }
735
736 #[test]
737 fn the_markup_convention_takes_the_slash_as_binary_and_the_colon_as_punctuation() {
738 assert_eq!(class_of_char_with('/', Convention::MathType), Class::Ord);
739 assert_eq!(class_of_char_with('/', Convention::Markup), Class::Bin);
740 assert_eq!(class_of_char_with(':', Convention::MathType), Class::Rel);
741 assert_eq!(class_of_char_with(':', Convention::Markup), Class::Punct);
742 }
743
744 #[test]
745 fn the_markup_convention_leaves_the_arrows_and_the_extra_relations_ordinary() {
746 for ch in [
747 '\u{2191}', '\u{2193}', '\u{2194}', '\u{21A6}', '\u{27F9}', '\u{27FA}', '\u{220B}',
748 '\u{2225}', '\u{2223}', '\u{2224}', '\u{22A5}', '\u{224D}', '\u{226A}', '\u{226B}',
749 ] {
750 assert_eq!(class_of_char_with(ch, Convention::MathType), Class::Rel);
751 assert_eq!(
752 class_of_char_with(ch, Convention::Markup),
753 Class::Ord,
754 "{ch:?} is ordinary in the markup convention"
755 );
756 }
757 }
758
759 #[test]
760 fn the_markup_convention_leaves_the_union_intersection_and_the_circled_operators_ordinary() {
761 for ch in ['\u{2229}', '\u{222A}'] {
762 assert_eq!(class_of_char_with(ch, Convention::MathType), Class::Bin);
763 assert_eq!(class_of_char_with(ch, Convention::Markup), Class::Ord);
764 }
765 for ch in ['\u{2A00}', '\u{2A03}', '\u{2A04}', '\u{2A05}', '\u{2A06}'] {
766 assert!(is_large_op_with(ch, Convention::MathType), "{ch:?}");
767 assert!(!is_large_op_with(ch, Convention::Markup), "{ch:?}");
768 assert_eq!(class_of_char_with(ch, Convention::Markup), Class::Ord);
769 }
770 }
771
772 #[test]
773 fn both_conventions_agree_on_the_shared_operators_and_the_delimiters() {
774 for ch in [
775 '\u{2211}', '\u{220F}', '\u{2210}', '\u{222B}', '\u{2230}', '\u{22C0}', '\u{22C1}',
776 '\u{22C2}', '\u{22C3}', '\u{2A01}', '\u{2A02}',
777 ] {
778 assert!(is_large_op_with(ch, Convention::MathType), "{ch:?}");
779 assert!(is_large_op_with(ch, Convention::Markup), "{ch:?}");
780 }
781 for (ch, class) in [
782 ('(', Class::Open),
783 (')', Class::Close),
784 ('[', Class::Open),
785 (']', Class::Close),
786 ('{', Class::Open),
787 ('}', Class::Close),
788 ('\u{27E8}', Class::Open),
789 ('\u{27E9}', Class::Close),
790 ('+', Class::Bin),
791 ('=', Class::Rel),
792 (',', Class::Punct),
793 (';', Class::Punct),
794 ('x', Class::Ord),
795 ('0', Class::Ord),
796 ] {
797 assert_eq!(class_of_char_with(ch, Convention::MathType), class);
798 assert_eq!(class_of_char_with(ch, Convention::Markup), class);
799 }
800 }
801
802 #[test]
803 fn the_letterlike_table_is_sorted_for_the_binary_search() {
804 assert!(
805 super::EXTRA_LETTERS
806 .windows(2)
807 .all(|pair| pair[0].0 < pair[1].0),
808 "the table is searched by character, so it must stay sorted"
809 );
810 }
811
812 #[test]
813 fn letterlike_symbols_name_the_letter_and_face_math_type_saved_them_for() {
814 use super::extra_letter;
815 assert_eq!(
816 extra_letter('\u{211D}'),
817 Some(('R', crate::Font::DoubleStruck))
818 );
819 assert_eq!(extra_letter('\u{212C}'), Some(('B', crate::Font::Script)));
820 assert_eq!(extra_letter('\u{212D}'), Some(('C', crate::Font::Fraktur)));
821 assert_eq!(extra_letter('R'), None);
822 assert_eq!(
823 extra_letter('\u{1D54D}'),
824 None,
825 "the block has no BMP character"
826 );
827 }
828
829 #[test]
830 fn the_extra_face_covers_the_letterlike_symbols_in_the_bmp() {
831 use super::is_extra_face;
832 for ch in ['\u{211D}', '\u{212C}', '\u{212D}', '\u{2102}', '\u{2134}'] {
833 assert!(is_extra_face(ch), "{ch:?} is saved in the extra face");
834 }
835 for ch in ['R', '\u{1D54D}', '\u{2113}', '\u{210F}'] {
836 assert!(!is_extra_face(ch), "{ch:?} is not saved in the extra face");
837 }
838 }
839
840 #[test]
841 fn the_symbol_encoding_names_the_legacy_positions() {
842 assert_eq!(symbol_code('\u{03B1}'), Some(0x61));
843 assert_eq!(symbol_code('\u{0394}'), Some(0x44));
844 assert_eq!(symbol_code('\u{03BC}'), Some(0x6D));
845 assert_eq!(symbol_code('\u{03A9}'), Some(0x57));
846 assert_eq!(symbol_code('\u{03C2}'), Some(0x56));
847 assert_eq!(symbol_code('\u{2211}'), Some(0xE5));
848 assert_eq!(symbol_code('\u{222B}'), Some(0xF2));
849 assert_eq!(symbol_code('\u{2264}'), Some(0xA3));
850 assert_eq!(
851 symbol_code('\u{2223}'),
852 Some(0x7C),
853 "the relation takes the bar glyph's position"
854 );
855 assert_eq!(symbol_code('A'), Some(0x41), "ASCII sits at its own code");
856 assert_eq!(symbol_code('\u{1D465}'), None);
857 }
858
859 #[test]
860 fn the_extra_encoding_names_the_math_type_positions() {
861 assert_eq!(extra_code('\u{019B}'), Some(0x44));
862 assert_eq!(extra_code('\u{2113}'), Some(0x6C));
863 assert_eq!(extra_code('\u{210F}'), Some(0x68));
864 assert_eq!(extra_code('\u{2213}'), Some(0x6D));
865 assert_eq!(extra_code('\u{20D7}'), Some(0x72));
866 assert_eq!(extra_code('\u{211D}'), Some(0xA1));
867 assert_eq!(extra_code('\u{2115}'), Some(0xA5));
868 assert_eq!(extra_code('\u{22C3}'), Some(0x55));
869 assert_eq!(extra_code('\u{22C2}'), Some(0x49));
870 assert_eq!(extra_code('\u{225C}'), Some(0x40));
871 assert_eq!(extra_code('\u{22EF}'), Some(0x4C));
872 assert_eq!(extra_code('\u{212C}'), None, "the face has no script B");
873 assert_eq!(extra_code('\u{1D465}'), None);
874 }
875
876 #[test]
877 fn a_symbol_takes_the_symbol_face_unless_only_the_extra_encoding_places_it() {
878 assert_eq!(symbol_position('\u{2211}'), Some((Font::Symbol, 0xE5)));
879 assert_eq!(symbol_position('\u{2229}'), Some((Font::Symbol, 0xC7)));
880 assert_eq!(symbol_position('\u{2113}'), Some((Font::Extra, 0x6C)));
881 assert_eq!(symbol_position('\u{210F}'), Some((Font::Extra, 0x68)));
882 assert_eq!(symbol_position('\u{2213}'), Some((Font::Extra, 0x6D)));
883 assert_eq!(symbol_position('\u{22C3}'), Some((Font::Extra, 0x55)));
884 assert_eq!(symbol_position('\u{22C2}'), Some((Font::Extra, 0x49)));
885 assert_eq!(symbol_position('\u{225C}'), Some((Font::Extra, 0x40)));
886 assert_eq!(symbol_position('\u{22EF}'), Some((Font::Extra, 0x4C)));
887 assert_eq!(
888 symbol_position('\u{222A}'),
889 Some((Font::Symbol, 0xC8)),
890 "a character the Symbol encoding places stays in that face"
891 );
892 assert_eq!(
893 symbol_position('\u{2223}'),
894 Some((Font::Symbol, 0x7C)),
895 "the relation takes the Symbol face's bar"
896 );
897 assert_eq!(symbol_position('\u{2225}'), None);
898 }
899}