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