Skip to main content

mt_core/
faces.rs

1//! The character-to-class conventions the formats follow.
2//!
3//! MTEF records the typeface of every character but not its atom class, Office Math records
4//! neither, and MathML records the class only where it differs from its own defaults. Every
5//! reader therefore derives a character's class from the character itself, and the writer of a
6//! format that has defaults needs the same answer to know when it has to write the class out.
7//! The preview also needs it, because it draws a character with the face MathType drew it with.
8//!
9//! Two conventions cover that work, and each format names the one it follows:
10//!
11//! * [`Convention::MathType`] is the class MathType's own output implies, taken from the TeX
12//!   classes. The MTEF reader and writer and the preview use it.
13//! * [`Convention::Markup`] is the default class MathML's writer and reader share. Office Math
14//!   follows it too, since it records no class at all. MathML spaces `mo` by its operator
15//!   dictionary rather than by the TeX classes, so the two conventions disagree about `/`, which
16//!   is a binary operator in the markup convention and an ordinary character in MathType, about
17//!   `:`, which is punctuation in the markup convention and a relation in MathType, and about the
18//!   characters MathType classes as operators or relations while the markup convention leaves
19//!   them ordinary. Those are the circled n-ary operators 2A00 and 2A03 to 2A06, the union and
20//!   intersection signs 2229 and 222A, the arrows 2191, 2193, 2194, 21A6, 27F9 and 27FA, and the
21//!   relations 220B, 2223, 2224, 2225, 22A5, 224D, 226A and 226B.
22//!
23//! What a format adds on top of the class stays in that format's crate: the `fence`, `separators`
24//! and class markers `mt-mathml` writes on `mo`, the delimiters each markup format spells a
25//! `Delim` as, and the operator spacing both formats leave to the renderer.
26
27use crate::{Class, Font};
28
29/// Characters that take limits when they carry scripts.
30const 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
35/// The integral signs, the large operators whose limits MathType sets against the ends of the
36/// sign instead of at the script positions.
37const 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
63/// Characters the MathType convention classes as operators or relations and the markup convention
64/// leaves ordinary. The module documentation lists them by group.
65const 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
71/// The character the markup convention spaces as a binary operator while the MathType convention
72/// leaves it ordinary.
73const MARKUP_BINARY: &[char] = &['/'];
74
75/// The character the markup convention spaces as punctuation while the MathType convention
76/// classes it as a relation.
77const MARKUP_PUNCTUATION: &[char] = &[':'];
78
79/// The character-classification convention a format follows.
80#[derive(Clone, Copy, Debug, PartialEq, Eq)]
81pub enum Convention {
82    /// The class MathType's own output implies, which follows the TeX classes.
83    MathType,
84    /// The default class MathML's writer and reader share, which Office Math follows as well.
85    Markup,
86}
87
88/// Whether `table` holds `ch`.
89///
90/// A scan rather than `str::contains`, so that [`class_of_char`] can run in a const context,
91/// where the palette data needs it.
92const 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
103/// Whether the character is a large operator in the MathType convention.
104const fn is_large_op(ch: char) -> bool {
105    holds(LARGE_OPS, ch) || holds(INTEGRALS, ch)
106}
107
108/// Whether the character is an integral sign.
109pub const fn is_integral_sign(ch: char) -> bool {
110    holds(INTEGRALS, ch)
111}
112
113/// The atom class the bare character carries in the MathType convention.
114const 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
132/// The atom class a bare character carries in the MathType convention.
133///
134/// A character that is not in one of the tables is an ordinary atom, so a variable and a digit
135/// classify the same way.
136pub const fn class_of_char(ch: char) -> Class {
137    class_of_char_with(ch, Convention::MathType)
138}
139
140/// The atom class a bare character carries in `convention`.
141pub 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
158/// Whether the character takes limits when it carries scripts in `convention`.
159pub 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
166/// Whether MathType draws the character from a symbol-encoded face: operators, relations and the
167/// ordinary symbols the text face has no glyph for.
168///
169/// The face is the Symbol face when the Adobe Symbol encoding places the character, and the
170/// extra face when the Symbol encoding does not but the MT Extra encoding does, which is where
171/// MathType saves `ℓ` and `ℏ`. A character neither encoding places stays in the Symbol face
172/// without a position. A relation the Euclid Math Two face places is the exception: the writer
173/// takes that face, and [`euclid_math_two_position`] gives its position.
174pub 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
197/// The symbol-encoded face MathType writes `ch` in, and the position in that face.
198///
199/// The Symbol face comes first: MathType draws a symbol there when the Adobe Symbol encoding
200/// gives it a position. A character the Symbol encoding does not place goes to the extra face at
201/// the position the MT Extra encoding gives it, which is where MathType saves `ℓ` and `ℏ`.
202/// Returns `None` when neither encoding places the character. A relation the Euclid Math Two
203/// face places also returns `None` here: [`euclid_math_two_position`] gives the position the
204/// writer and the preview take instead.
205///
206/// The caller asks this for a character [`is_symbol_face`] accepts. An ASCII character has a
207/// position here because its own code is the position, but the text face draws it, so the face
208/// returned for ASCII describes the encoding rather than the face the character is written in.
209pub 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
215/// The position MathType draws `ch` at in the Euclid Math Two face, when that face carries it.
216///
217/// Euclid Math Two carries relations the Symbol and MT Extra encodings do not place, at the
218/// positions of its own `EuclidMath2` encoding. Its glyph `succeedsequal` at `B1` draws the
219/// succeeds-or-equal relation, which MathType's AMS TeX translator spells `\succcurlyeq` for
220/// MTCode `0x227D`.
221pub fn euclid_math_two_position(ch: char) -> Option<u8> {
222    match ch {
223        '\u{227D}' => Some(0xB1),
224        _ => None,
225    }
226}
227
228/// The code position `ch` takes in the Symbol face's legacy encoding.
229///
230/// The code-to-name table of `StandardSymbolsPS.afm` gives the positions of the characters in the
231/// face. MathType's Symbol face and its lower- and upper-case Greek styles draw from that font, so
232/// an equation stream names a glyph's position in the character record and a preview translates
233/// the character before it looks the glyph up.
234///
235/// The two phis are the exception. The Adobe Glyph List maps their names `phi` and `phi1` to
236/// U+03C6 and U+03D5, but the face draws the straight phi `ϕ` at `66` and the loopy phi `φ` at
237/// `6A`, the reverse of those names, as the face's own outlines show. MathType draws and saves by
238/// position, so the table follows the drawn shapes. U+2223 `∣` is the other exception: the
239/// encoding lists no such character, but its `bar` at `7C` draws the same vertical line, and
240/// MathType finds a Symbol-face glyph only by the position the record names. An ASCII character
241/// sits at its own code, which the encoding does not list. Characters outside the table have no
242/// position in the encoding.
243pub 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
253/// The radical glyph MathType names in the Symbol face at its legacy position `D6`.
254///
255/// The face carries this one root glyph. MathType draws every radical that outgrows the base
256/// glyph with it, and the face has no taller radical or size variant, so taller content stretches
257/// this shape vertically.
258pub const SYMBOL_RADICAL: char = '\u{F0D6}';
259
260/// The legacy position of a private-use codepoint MathType names a Symbol glyph by.
261///
262/// The face carries its radical at `U+F0D6` and its extended delimiter glyphs at `U+F0E6`
263/// through `U+F0FE`. Those codepoints name the glyphs in the face's cmap, which is how a preview
264/// names a piece, and their low byte is the position in the face's legacy encoding. An equation
265/// stream records the position byte itself, not the private-use codepoint. The private-use ranges
266/// MathType uses for styled letters, `U+F000` through `U+F033` and `U+F080` through `U+F0B3`, are
267/// not positions in the face and return `None`.
268pub 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
273/// The code position `ch` takes in MT Extra's legacy encoding.
274///
275/// MT Extra holds the marks MathType's other faces lack, at the positions of the MathType Extra
276/// encoding recorded in Mozilla's MathML font notes:
277/// <https://www-archive.mozilla.org/projects/mathml/fonts/encoding/mtextra.html>. The notes
278/// describe the reduced set Windows installs by default. The face MathType ships holds more,
279/// among them the letterlike symbols MathType saves there, so the positions the face's own glyph
280/// names give are in the table as well: `Rbb`, `Zbb`, `Cbb`, `Qbb` and `Nbb` sit at `A1` through
281/// `A5`. Characters outside the table have no position in the encoding.
282pub 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
289/// The characters the Symbol encoding places, with their positions, sorted by character.
290///
291/// A caller that browses or writes the face checks it carries a glyph at each position the
292/// encoding names.
293pub fn symbol_positions() -> &'static [(char, u8)] {
294    SYMBOL_CODES
295}
296
297/// The characters MT Extra places, with their positions, sorted by character.
298///
299/// A caller that loads the face checks it carries a glyph at each position the encoding names.
300pub fn extra_positions() -> &'static [(char, u8)] {
301    EXTRA_CODES
302}
303
304/// Unicode character and its code position in the Symbol font's legacy encoding.
305///
306/// Sorted by character, for a binary search. The documentation of [`symbol_code`] gives the
307/// source.
308const 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
442/// Unicode character and its code position in MT Extra's legacy encoding.
443///
444/// Sorted by character, for a binary search. The documentation of [`extra_code`] gives the
445/// source.
446const 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
490/// Whether MathType draws the character from the CJK face.
491///
492/// The CJK face covers the writing systems of Chinese, Japanese and Korean: their punctuation,
493/// kana and ideographs, including the compatibility and extension blocks. MathType declares that
494/// face in every stream it writes and selects it with typeface 12.
495pub fn is_cjk(ch: char) -> bool {
496    matches!(
497        ch as u32,
498        0x1100..=0x11FF      // hangul jamo
499        | 0x2E80..=0x2FDF    // CJK radicals and Kangxi radicals
500        | 0x2FF0..=0x2FFF    // ideographic description characters
501        | 0x3000..=0x30FF    // CJK punctuation, hiragana and katakana
502        | 0x3100..=0x312F    // bopomofo
503        | 0x3130..=0x318F    // hangul compatibility jamo
504        | 0x3190..=0x319F    // kanbun
505        | 0x31A0..=0x31BF    // bopomofo extended
506        | 0x31C0..=0x31EF    // CJK strokes
507        | 0x31F0..=0x31FF    // katakana phonetic extensions
508        | 0x3200..=0x33FF    // enclosed CJK letters and months, compatibility
509        | 0x3400..=0x4DBF    // CJK unified ideographs extension A
510        | 0x4E00..=0x9FFF    // CJK unified ideographs
511        | 0xA960..=0xA97F    // hangul jamo extended-A
512        | 0xAC00..=0xD7AF    // hangul syllables
513        | 0xD7B0..=0xD7FF    // hangul jamo extended-B
514        | 0xF900..=0xFAFF    // CJK compatibility ideographs
515        | 0xFE10..=0xFE1F    // vertical forms
516        | 0xFE30..=0xFE4F    // CJK compatibility forms
517        | 0xFE50..=0xFE6F    // small form variants
518        | 0xFF00..=0xFFEF    // halfwidth and fullwidth forms
519        | 0x1B000..=0x1B16F  // kana supplement and extended
520        | 0x1F200..=0x1F2FF  // enclosed ideographic supplement
521        | 0x20000..=0x3FFFF  // CJK unified ideographs extensions B and later
522    )
523}
524
525/// Letterlike symbols in the BMP, with the letter and face each one stands for.
526///
527/// MathType spells a styled letter whose Unicode block has a BMP character as that character, in
528/// the face its standard faces do not cover, i.e. the extra face. A tree keeps the plain letter in
529/// the styled face instead, so this table is the way between the two: the reader recovers the
530/// letter and face from the saved character, the writer recovers the saved character from them,
531/// and the preview asks which characters MathType saved in the extra face at all.
532///
533/// Sorted by character, for a binary search.
534const 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
560/// The letter and face a letterlike symbol in the BMP stands for.
561pub 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
569/// The letterlike symbol in the BMP MathType saves a styled letter as, when Unicode has one.
570pub 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
577/// Whether MathType saves the character in the extra face.
578pub 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        // The radical is the only position below the extended delimiter range.
698        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        // The styled-letter ranges are not positions in the face.
702        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}