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: &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}\u{222B}\u{222C}\u{222D}\u{222E}\u{222F}\u{2230}";
31
32const 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}";
33
34const RELATIONS: &str = "=<>:\u{2264}\u{2265}\u{2260}\u{2248}\u{2261}\u{2208}\u{2209}\u{2282}\u{2283}\u{2286}\u{2287}\
35\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}\
36\u{2323}\u{2254}\u{2255}\u{220B}\u{2225}\u{2223}\u{2224}\u{22A5}\u{224D}\u{226A}\u{226B}\
37\u{227D}";
38
39const OPENERS: &str = "([{\u{27E8}\u{230A}\u{2308}\u{27E6}\u{301A}";
40const CLOSERS: &str = ")]}\u{27E9}\u{230B}\u{2309}\u{27E7}\u{301B}";
41const PUNCTUATION: &str = ",;";
42
43/// Characters the MathType convention classes as operators or relations and the markup convention
44/// leaves ordinary. The module documentation lists them by group.
45const 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}";
46
47/// The character the markup convention spaces as a binary operator while the MathType convention
48/// leaves it ordinary.
49const MARKUP_BINARY: &str = "/";
50
51/// The character the markup convention spaces as punctuation while the MathType convention
52/// classes it as a relation.
53const MARKUP_PUNCTUATION: &str = ":";
54
55/// The character-classification convention a format follows.
56#[derive(Clone, Copy, Debug, PartialEq, Eq)]
57pub enum Convention {
58    /// The class MathType's own output implies, which follows the TeX classes.
59    MathType,
60    /// The default class MathML's writer and reader share, which Office Math follows as well.
61    Markup,
62}
63
64/// Whether the character is a large operator in the MathType convention.
65fn is_large_op(ch: char) -> bool {
66    LARGE_OPS.contains(ch)
67}
68
69/// The atom class the bare character carries in the MathType convention.
70fn mathtype_class(ch: char) -> Class {
71    if is_large_op(ch) {
72        Class::Op
73    } else if OPENERS.contains(ch) {
74        Class::Open
75    } else if CLOSERS.contains(ch) {
76        Class::Close
77    } else if PUNCTUATION.contains(ch) {
78        Class::Punct
79    } else if BINARY.contains(ch) {
80        Class::Bin
81    } else if RELATIONS.contains(ch) {
82        Class::Rel
83    } else {
84        Class::Ord
85    }
86}
87
88/// The atom class a bare character carries in the MathType convention.
89///
90/// A character that is not in one of the tables is an ordinary atom, so a variable and a digit
91/// classify the same way.
92pub fn class_of_char(ch: char) -> Class {
93    class_of_char_with(ch, Convention::MathType)
94}
95
96/// The atom class a bare character carries in `convention`.
97pub fn class_of_char_with(ch: char, convention: Convention) -> Class {
98    match convention {
99        Convention::MathType => mathtype_class(ch),
100        Convention::Markup => {
101            if MARKUP_ORDINARY.contains(ch) {
102                Class::Ord
103            } else if MARKUP_BINARY.contains(ch) {
104                Class::Bin
105            } else if MARKUP_PUNCTUATION.contains(ch) {
106                Class::Punct
107            } else {
108                mathtype_class(ch)
109            }
110        }
111    }
112}
113
114/// Whether the character takes limits when it carries scripts in `convention`.
115pub fn is_large_op_with(ch: char, convention: Convention) -> bool {
116    match convention {
117        Convention::MathType => is_large_op(ch),
118        Convention::Markup => is_large_op(ch) && !MARKUP_ORDINARY.contains(ch),
119    }
120}
121
122/// Whether MathType draws the character from a symbol-encoded face: operators, relations and the
123/// ordinary symbols the text face has no glyph for.
124///
125/// The face is the Symbol face when the Adobe Symbol encoding places the character, and the
126/// extra face when the Symbol encoding does not but the MT Extra encoding does, which is where
127/// MathType saves `ℓ` and `ℏ`. A character neither encoding places stays in the Symbol face
128/// without a position. A relation the Euclid Math Two face places is the exception: the writer
129/// takes that face, and [`euclid_math_two_position`] gives its position.
130pub fn is_symbol_face(ch: char) -> bool {
131    if ch.is_ascii() {
132        return false;
133    }
134    is_large_op(ch)
135        || matches!(class_of_char(ch), Class::Bin | Class::Rel)
136        || matches!(
137            ch,
138            '\u{2200}'
139                | '\u{2203}'
140                | '\u{2205}'
141                | '\u{221E}'
142                | '\u{2202}'
143                | '\u{2207}'
144                | '\u{210F}'
145                | '\u{2113}'
146                | '\u{2135}'
147                | '\u{2118}'
148                | '\u{2220}'
149                | '\u{22EF}'
150        )
151}
152
153/// The symbol-encoded face MathType writes `ch` in, and the position in that face.
154///
155/// The Symbol face comes first: MathType draws a symbol there when the Adobe Symbol encoding
156/// gives it a position. A character the Symbol encoding does not place goes to the extra face at
157/// the position the MT Extra encoding gives it, which is where MathType saves `ℓ` and `ℏ`.
158/// Returns `None` when neither encoding places the character. A relation the Euclid Math Two
159/// face places also returns `None` here: [`euclid_math_two_position`] gives the position the
160/// writer and the preview take instead.
161///
162/// The caller asks this for a character [`is_symbol_face`] accepts. An ASCII character has a
163/// position here because its own code is the position, but the text face draws it, so the face
164/// returned for ASCII describes the encoding rather than the face the character is written in.
165pub fn symbol_position(ch: char) -> Option<(Font, u8)> {
166    symbol_code(ch)
167        .map(|code| (Font::Symbol, code))
168        .or_else(|| extra_code(ch).map(|code| (Font::Extra, code)))
169}
170
171/// The position MathType draws `ch` at in the Euclid Math Two face, when that face carries it.
172///
173/// Euclid Math Two carries relations the Symbol and MT Extra encodings do not place, at the
174/// positions of its own `EuclidMath2` encoding. Its glyph `succeedsequal` at `B1` draws the
175/// succeeds-or-equal relation, which MathType's AMS TeX translator spells `\succcurlyeq` for
176/// MTCode `0x227D`.
177pub fn euclid_math_two_position(ch: char) -> Option<u8> {
178    match ch {
179        '\u{227D}' => Some(0xB1),
180        _ => None,
181    }
182}
183
184/// The code position `ch` takes in the Symbol face's legacy encoding.
185///
186/// The code-to-name table of `StandardSymbolsPS.afm` gives the positions of the characters in the
187/// face. MathType's Symbol face and its lower- and upper-case Greek styles draw from that font, so
188/// an equation stream names a glyph's position in the character record and a preview translates
189/// the character before it looks the glyph up.
190///
191/// The two phis are the exception. The Adobe Glyph List maps their names `phi` and `phi1` to
192/// U+03C6 and U+03D5, but the face draws the straight phi `ϕ` at `66` and the loopy phi `φ` at
193/// `6A`, the reverse of those names, as the face's own outlines show. MathType draws and saves by
194/// position, so the table follows the drawn shapes. U+2223 `∣` is the other exception: the
195/// encoding lists no such character, but its `bar` at `7C` draws the same vertical line, and
196/// MathType finds a Symbol-face glyph only by the position the record names. An ASCII character
197/// sits at its own code, which the encoding does not list. Characters outside the table have no
198/// position in the encoding.
199pub fn symbol_code(ch: char) -> Option<u8> {
200    if ch.is_ascii() {
201        return Some(ch as u8);
202    }
203    let index = SYMBOL_CODES
204        .binary_search_by_key(&ch, |(character, _)| *character)
205        .ok()?;
206    Some(SYMBOL_CODES[index].1)
207}
208
209/// The radical glyph MathType names in the Symbol face at its legacy position `D6`.
210///
211/// The face carries this one root glyph. MathType draws every radical that outgrows the base
212/// glyph with it, and the face has no taller radical or size variant, so taller content stretches
213/// this shape vertically.
214pub const SYMBOL_RADICAL: char = '\u{F0D6}';
215
216/// The legacy position of a private-use codepoint MathType names a Symbol glyph by.
217///
218/// The face carries its radical at `U+F0D6` and its extended delimiter glyphs at `U+F0E6`
219/// through `U+F0FE`. Those codepoints name the glyphs in the face's cmap, which is how a preview
220/// names a piece, and their low byte is the position in the face's legacy encoding. An equation
221/// stream records the position byte itself, not the private-use codepoint. The private-use ranges
222/// MathType uses for styled letters, `U+F000` through `U+F033` and `U+F080` through `U+F0B3`, are
223/// not positions in the face and return `None`.
224pub fn symbol_private_use_code(ch: char) -> Option<u8> {
225    let code = u32::from(ch);
226    (code == u32::from(SYMBOL_RADICAL) || (0xF0E6..=0xF0FE).contains(&code)).then_some(code as u8)
227}
228
229/// The code position `ch` takes in MT Extra's legacy encoding.
230///
231/// MT Extra holds the marks MathType's other faces lack, at the positions of the MathType Extra
232/// encoding recorded in Mozilla's MathML font notes:
233/// <https://www-archive.mozilla.org/projects/mathml/fonts/encoding/mtextra.html>. The notes
234/// describe the reduced set Windows installs by default. The face MathType ships holds more,
235/// among them the letterlike symbols MathType saves there, so the positions the face's own glyph
236/// names give are in the table as well: `Rbb`, `Zbb`, `Cbb`, `Qbb` and `Nbb` sit at `A1` through
237/// `A5`. Characters outside the table have no position in the encoding.
238pub fn extra_code(ch: char) -> Option<u8> {
239    let index = EXTRA_CODES
240        .binary_search_by_key(&ch, |(character, _)| *character)
241        .ok()?;
242    Some(EXTRA_CODES[index].1)
243}
244
245/// The characters MT Extra places, with their positions, sorted by character.
246///
247/// A caller that loads the face checks it carries a glyph at each position the encoding names.
248pub fn extra_positions() -> &'static [(char, u8)] {
249    EXTRA_CODES
250}
251
252/// Unicode character and its code position in the Symbol font's legacy encoding.
253///
254/// Sorted by character, for a binary search. The documentation of [`symbol_code`] gives the
255/// source.
256const SYMBOL_CODES: &[(char, u8)] = &[
257    ('\u{00AC}', 0xD8),
258    ('\u{00B0}', 0xB0),
259    ('\u{00B1}', 0xB1),
260    ('\u{00B5}', 0x6D),
261    ('\u{00D7}', 0xB4),
262    ('\u{00F7}', 0xB8),
263    ('\u{0192}', 0xA6),
264    ('\u{0391}', 0x41),
265    ('\u{0392}', 0x42),
266    ('\u{0393}', 0x47),
267    ('\u{0394}', 0x44),
268    ('\u{0395}', 0x45),
269    ('\u{0396}', 0x5A),
270    ('\u{0397}', 0x48),
271    ('\u{0398}', 0x51),
272    ('\u{0399}', 0x49),
273    ('\u{039A}', 0x4B),
274    ('\u{039B}', 0x4C),
275    ('\u{039C}', 0x4D),
276    ('\u{039D}', 0x4E),
277    ('\u{039E}', 0x58),
278    ('\u{039F}', 0x4F),
279    ('\u{03A0}', 0x50),
280    ('\u{03A1}', 0x52),
281    ('\u{03A3}', 0x53),
282    ('\u{03A4}', 0x54),
283    ('\u{03A5}', 0x55),
284    ('\u{03A6}', 0x46),
285    ('\u{03A7}', 0x43),
286    ('\u{03A8}', 0x59),
287    ('\u{03A9}', 0x57),
288    ('\u{03B1}', 0x61),
289    ('\u{03B2}', 0x62),
290    ('\u{03B3}', 0x67),
291    ('\u{03B4}', 0x64),
292    ('\u{03B5}', 0x65),
293    ('\u{03B6}', 0x7A),
294    ('\u{03B7}', 0x68),
295    ('\u{03B8}', 0x71),
296    ('\u{03B9}', 0x69),
297    ('\u{03BA}', 0x6B),
298    ('\u{03BB}', 0x6C),
299    ('\u{03BC}', 0x6D),
300    ('\u{03BD}', 0x6E),
301    ('\u{03BE}', 0x78),
302    ('\u{03BF}', 0x6F),
303    ('\u{03C0}', 0x70),
304    ('\u{03C1}', 0x72),
305    ('\u{03C2}', 0x56),
306    ('\u{03C3}', 0x73),
307    ('\u{03C4}', 0x74),
308    ('\u{03C5}', 0x75),
309    ('\u{03C6}', 0x6A),
310    ('\u{03C7}', 0x63),
311    ('\u{03C8}', 0x79),
312    ('\u{03C9}', 0x77),
313    ('\u{03D1}', 0x4A),
314    ('\u{03D2}', 0xA1),
315    ('\u{03D5}', 0x66),
316    ('\u{03D6}', 0x76),
317    ('\u{2022}', 0xB7),
318    ('\u{2026}', 0xBC),
319    ('\u{2032}', 0xA2),
320    ('\u{2033}', 0xB2),
321    ('\u{2044}', 0xA4),
322    ('\u{20AC}', 0xA0),
323    ('\u{2111}', 0xC1),
324    ('\u{2118}', 0xC3),
325    ('\u{211C}', 0xC2),
326    ('\u{2126}', 0x57),
327    ('\u{2135}', 0xC0),
328    ('\u{2190}', 0xAC),
329    ('\u{2191}', 0xAD),
330    ('\u{2192}', 0xAE),
331    ('\u{2193}', 0xAF),
332    ('\u{2194}', 0xAB),
333    ('\u{21B5}', 0xBF),
334    ('\u{21D0}', 0xDC),
335    ('\u{21D1}', 0xDD),
336    ('\u{21D2}', 0xDE),
337    ('\u{21D3}', 0xDF),
338    ('\u{21D4}', 0xDB),
339    ('\u{2200}', 0x22),
340    ('\u{2202}', 0xB6),
341    ('\u{2203}', 0x24),
342    ('\u{2205}', 0xC6),
343    ('\u{2206}', 0x44),
344    ('\u{2207}', 0xD1),
345    ('\u{2208}', 0xCE),
346    ('\u{2209}', 0xCF),
347    ('\u{220B}', 0x27),
348    ('\u{220F}', 0xD5),
349    ('\u{2211}', 0xE5),
350    ('\u{2212}', 0x2D),
351    ('\u{2217}', 0x2A),
352    ('\u{221A}', 0xD6),
353    ('\u{221D}', 0xB5),
354    ('\u{221E}', 0xA5),
355    ('\u{2220}', 0xD0),
356    ('\u{2223}', 0x7C),
357    ('\u{2227}', 0xD9),
358    ('\u{2228}', 0xDA),
359    ('\u{2229}', 0xC7),
360    ('\u{222A}', 0xC8),
361    ('\u{222B}', 0xF2),
362    ('\u{2234}', 0x5C),
363    ('\u{223C}', 0x7E),
364    ('\u{2245}', 0x40),
365    ('\u{2248}', 0xBB),
366    ('\u{2260}', 0xB9),
367    ('\u{2261}', 0xBA),
368    ('\u{2264}', 0xA3),
369    ('\u{2265}', 0xB3),
370    ('\u{2282}', 0xCC),
371    ('\u{2283}', 0xC9),
372    ('\u{2284}', 0xCB),
373    ('\u{2286}', 0xCD),
374    ('\u{2287}', 0xCA),
375    ('\u{2295}', 0xC5),
376    ('\u{2297}', 0xC4),
377    ('\u{22A5}', 0x5E),
378    ('\u{22C5}', 0xD7),
379    ('\u{2320}', 0xF3),
380    ('\u{2321}', 0xF5),
381    ('\u{2329}', 0xE1),
382    ('\u{232A}', 0xF1),
383    ('\u{25CA}', 0xE0),
384    ('\u{2660}', 0xAA),
385    ('\u{2663}', 0xA7),
386    ('\u{2665}', 0xA9),
387    ('\u{2666}', 0xA8),
388];
389
390/// Unicode character and its code position in MT Extra's legacy encoding.
391///
392/// Sorted by character, for a binary search. The documentation of [`extra_code`] gives the
393/// source.
394const EXTRA_CODES: &[(char, u8)] = &[
395    ('\u{019B}', 0x44),
396    ('\u{0300}', 0x23),
397    ('\u{0302}', 0x24),
398    ('\u{0303}', 0x25),
399    ('\u{0307}', 0x26),
400    ('\u{2026}', 0x4B),
401    ('\u{2035}', 0x60),
402    ('\u{20D0}', 0x77),
403    ('\u{20D1}', 0x76),
404    ('\u{20D6}', 0x73),
405    ('\u{20D7}', 0x72),
406    ('\u{20E1}', 0x74),
407    ('\u{2102}', 0xA3),
408    ('\u{210F}', 0x68),
409    ('\u{2113}', 0x6C),
410    ('\u{2115}', 0xA5),
411    ('\u{211A}', 0xA4),
412    ('\u{211D}', 0xA1),
413    ('\u{2124}', 0xA2),
414    ('\u{2195}', 0x62),
415    ('\u{21A6}', 0x61),
416    ('\u{21D5}', 0x63),
417    ('\u{2210}', 0x43),
418    ('\u{2213}', 0x6D),
419    ('\u{2218}', 0x6F),
420    ('\u{2229}', 0x49),
421    ('\u{222A}', 0x55),
422    ('\u{2235}', 0x51),
423    ('\u{225C}', 0x40),
424    ('\u{227A}', 0x70),
425    ('\u{227B}', 0x66),
426    ('\u{22C2}', 0x49),
427    ('\u{22C3}', 0x55),
428    ('\u{22EE}', 0x4D),
429    ('\u{22EF}', 0x4C),
430    ('\u{22F0}', 0x4E),
431    ('\u{22F1}', 0x4F),
432    ('\u{25B9}', 0x3E),
433    ('\u{25C3}', 0x3C),
434    ('\u{FE36}', 0x7B),
435    ('\u{FE38}', 0x7D),
436];
437
438/// Whether MathType draws the character from the CJK face.
439///
440/// The CJK face covers the writing systems of Chinese, Japanese and Korean: their punctuation,
441/// kana and ideographs, including the compatibility and extension blocks. MathType declares that
442/// face in every stream it writes and selects it with typeface 12.
443pub fn is_cjk(ch: char) -> bool {
444    matches!(
445        ch as u32,
446        0x1100..=0x11FF      // hangul jamo
447        | 0x2E80..=0x2FDF    // CJK radicals and Kangxi radicals
448        | 0x2FF0..=0x2FFF    // ideographic description characters
449        | 0x3000..=0x30FF    // CJK punctuation, hiragana and katakana
450        | 0x3100..=0x312F    // bopomofo
451        | 0x3130..=0x318F    // hangul compatibility jamo
452        | 0x3190..=0x319F    // kanbun
453        | 0x31A0..=0x31BF    // bopomofo extended
454        | 0x31C0..=0x31EF    // CJK strokes
455        | 0x31F0..=0x31FF    // katakana phonetic extensions
456        | 0x3200..=0x33FF    // enclosed CJK letters and months, compatibility
457        | 0x3400..=0x4DBF    // CJK unified ideographs extension A
458        | 0x4E00..=0x9FFF    // CJK unified ideographs
459        | 0xA960..=0xA97F    // hangul jamo extended-A
460        | 0xAC00..=0xD7AF    // hangul syllables
461        | 0xD7B0..=0xD7FF    // hangul jamo extended-B
462        | 0xF900..=0xFAFF    // CJK compatibility ideographs
463        | 0xFE10..=0xFE1F    // vertical forms
464        | 0xFE30..=0xFE4F    // CJK compatibility forms
465        | 0xFE50..=0xFE6F    // small form variants
466        | 0xFF00..=0xFFEF    // halfwidth and fullwidth forms
467        | 0x1B000..=0x1B16F  // kana supplement and extended
468        | 0x1F200..=0x1F2FF  // enclosed ideographic supplement
469        | 0x20000..=0x3FFFF  // CJK unified ideographs extensions B and later
470    )
471}
472
473/// Letterlike symbols in the BMP, with the letter and face each one stands for.
474///
475/// MathType spells a styled letter whose Unicode block has a BMP character as that character, in
476/// the face its standard faces do not cover, i.e. the extra face. A tree keeps the plain letter in
477/// the styled face instead, so this table is the way between the two: the reader recovers the
478/// letter and face from the saved character, the writer recovers the saved character from them,
479/// and the preview asks which characters MathType saved in the extra face at all.
480///
481/// Sorted by character, for a binary search.
482const EXTRA_LETTERS: [(char, char, Font); 23] = [
483    ('\u{2102}', 'C', Font::DoubleStruck),
484    ('\u{210A}', 'g', Font::Script),
485    ('\u{210B}', 'H', Font::Script),
486    ('\u{210C}', 'H', Font::Fraktur),
487    ('\u{210D}', 'H', Font::DoubleStruck),
488    ('\u{2110}', 'I', Font::Script),
489    ('\u{2111}', 'I', Font::Fraktur),
490    ('\u{2112}', 'L', Font::Script),
491    ('\u{2115}', 'N', Font::DoubleStruck),
492    ('\u{2119}', 'P', Font::DoubleStruck),
493    ('\u{211A}', 'Q', Font::DoubleStruck),
494    ('\u{211B}', 'R', Font::Script),
495    ('\u{211C}', 'R', Font::Fraktur),
496    ('\u{211D}', 'R', Font::DoubleStruck),
497    ('\u{2124}', 'Z', Font::DoubleStruck),
498    ('\u{2128}', 'Z', Font::Fraktur),
499    ('\u{212C}', 'B', Font::Script),
500    ('\u{212D}', 'C', Font::Fraktur),
501    ('\u{212F}', 'e', Font::Script),
502    ('\u{2130}', 'E', Font::Script),
503    ('\u{2131}', 'F', Font::Script),
504    ('\u{2133}', 'M', Font::Script),
505    ('\u{2134}', 'o', Font::Script),
506];
507
508/// The letter and face a letterlike symbol in the BMP stands for.
509pub fn extra_letter(ch: char) -> Option<(char, Font)> {
510    let index = EXTRA_LETTERS
511        .binary_search_by_key(&ch, |(symbol, _, _)| *symbol)
512        .ok()?;
513    let (_, letter, font) = EXTRA_LETTERS[index];
514    Some((letter, font))
515}
516
517/// The letterlike symbol in the BMP MathType saves a styled letter as, when Unicode has one.
518pub fn extra_symbol(letter: char, font: Font) -> Option<char> {
519    EXTRA_LETTERS
520        .iter()
521        .find(|(_, saved, face)| *saved == letter && *face == font)
522        .map(|(symbol, _, _)| *symbol)
523}
524
525/// Whether MathType saves the character in the extra face.
526pub fn is_extra_face(ch: char) -> bool {
527    extra_letter(ch).is_some()
528}
529
530#[cfg(test)]
531mod tests {
532    use super::{
533        Convention, SYMBOL_RADICAL, class_of_char, class_of_char_with, extra_code, is_cjk,
534        is_large_op_with, is_symbol_face, symbol_code, symbol_position, symbol_private_use_code,
535    };
536    use crate::{Class, Font};
537
538    #[test]
539    fn cjk_letters_and_punctuation_take_the_cjk_face() {
540        for ch in [
541            '\u{4E3A}',
542            '\u{4E2D}',
543            '\u{3001}',
544            '\u{3002}',
545            '\u{300C}',
546            '\u{3042}',
547            '\u{30A2}',
548            '\u{2FF0}',
549            '\u{31B0}',
550            '\u{31F0}',
551            '\u{FF08}',
552            '\u{FF11}',
553            '\u{FA0E}',
554            '\u{1B000}',
555            '\u{1F200}',
556            '\u{20000}',
557            '\u{1100}',
558            '\u{3131}',
559            '\u{AC00}',
560            '\u{3190}',
561            '\u{FE10}',
562            '\u{FE50}',
563        ] {
564            assert!(is_cjk(ch), "{ch:?} is drawn from the CJK face");
565        }
566    }
567
568    #[test]
569    fn latin_letters_greek_and_math_symbols_stay_out_of_the_cjk_face() {
570        for ch in [
571            'A', 'x', '0', '\u{03B1}', '\u{2211}', '\u{2192}', '\u{00E9}',
572        ] {
573            assert!(!is_cjk(ch), "{ch:?} is not drawn from the CJK face");
574        }
575    }
576
577    #[test]
578    fn operators_relations_and_large_operators_take_the_symbol_face() {
579        for ch in [
580            '\u{2212}', '\u{2211}', '\u{2208}', '\u{2264}', '\u{00B1}', '\u{00D7}', '\u{222B}',
581            '\u{220F}',
582        ] {
583            assert!(is_symbol_face(ch), "{ch:?} is drawn from the Symbol face");
584        }
585    }
586
587    #[test]
588    fn ordinary_symbols_the_text_face_lacks_take_the_symbol_face() {
589        for ch in [
590            '\u{2205}', '\u{221E}', '\u{2202}', '\u{2207}', '\u{2200}', '\u{2203}',
591        ] {
592            assert!(is_symbol_face(ch), "{ch:?} is drawn from the Symbol face");
593        }
594    }
595
596    #[test]
597    fn ascii_and_variable_letters_stay_in_the_text_face() {
598        for ch in [
599            '-', '+', '=', 'x', 'A', '0', '\u{03B1}', '\u{03C6}', '\u{221A}',
600        ] {
601            assert!(!is_symbol_face(ch), "{ch:?} is drawn from the text face");
602        }
603    }
604
605    #[test]
606    fn the_symbol_table_names_the_legacy_positions() {
607        assert_eq!(symbol_code('A'), Some(0x41));
608        assert_eq!(symbol_code('\u{03B1}'), Some(0x61));
609        assert_eq!(symbol_code('\u{0394}'), Some(0x44));
610        assert_eq!(symbol_code('\u{03BC}'), Some(0x6D));
611        assert_eq!(symbol_code('\u{03A9}'), Some(0x57));
612        assert_eq!(symbol_code('\u{03C2}'), Some(0x56));
613        assert_eq!(
614            symbol_code('\u{03C6}'),
615            Some(0x6A),
616            "the loopy phi takes the position the Symbol font draws it at"
617        );
618        assert_eq!(
619            symbol_code('\u{03D5}'),
620            Some(0x66),
621            "the straight phi takes the position the Symbol font draws it at"
622        );
623        assert_eq!(symbol_code('\u{2211}'), Some(0xE5));
624        assert_eq!(symbol_code('\u{222B}'), Some(0xF2));
625        assert_eq!(symbol_code('\u{2264}'), Some(0xA3));
626        assert_eq!(symbol_code('\u{1D465}'), None);
627    }
628
629    #[test]
630    fn private_use_codepoints_name_symbol_positions() {
631        assert_eq!(symbol_private_use_code(SYMBOL_RADICAL), Some(0xD6));
632        assert_eq!(symbol_private_use_code('\u{F0E6}'), Some(0xE6));
633        assert_eq!(symbol_private_use_code('\u{F0F0}'), Some(0xF0));
634        assert_eq!(symbol_private_use_code('\u{F0FE}'), Some(0xFE));
635        // The radical is the only position below the extended delimiter range.
636        assert_eq!(symbol_private_use_code('\u{F0D5}'), None);
637        assert_eq!(symbol_private_use_code('\u{F0E5}'), None);
638        assert_eq!(symbol_private_use_code('\u{F0FF}'), None);
639        // The styled-letter ranges are not positions in the face.
640        assert_eq!(symbol_private_use_code('\u{F000}'), None);
641        assert_eq!(symbol_private_use_code('\u{F041}'), None);
642        assert_eq!(symbol_private_use_code('\u{F0B3}'), None);
643    }
644
645    #[test]
646    fn a_bare_character_classifies_by_the_latex_conventions() {
647        assert_eq!(class_of_char('\u{2211}'), Class::Op);
648        assert_eq!(class_of_char('\u{2212}'), Class::Bin);
649        assert_eq!(class_of_char('\u{2208}'), Class::Rel);
650        assert_eq!(class_of_char('('), Class::Open);
651        assert_eq!(class_of_char(']'), Class::Close);
652        assert_eq!(class_of_char(','), Class::Punct);
653        assert_eq!(class_of_char('x'), Class::Ord);
654        assert_eq!(class_of_char('0'), Class::Ord);
655    }
656
657    #[test]
658    fn the_succeeds_or_equal_relation_is_a_relation() {
659        assert_eq!(class_of_char('\u{227D}'), Class::Rel);
660        assert!(
661            is_symbol_face('\u{227D}'),
662            "MathType draws the relation from a face other than the text face"
663        );
664    }
665
666    #[test]
667    fn the_euclid_math_two_encoding_names_the_succeeds_or_equal_position() {
668        use super::euclid_math_two_position;
669        assert_eq!(euclid_math_two_position('\u{227D}'), Some(0xB1));
670        assert_eq!(euclid_math_two_position('\u{227C}'), None);
671        assert_eq!(euclid_math_two_position('Q'), None);
672    }
673
674    #[test]
675    fn the_markup_convention_takes_the_slash_as_binary_and_the_colon_as_punctuation() {
676        assert_eq!(class_of_char_with('/', Convention::MathType), Class::Ord);
677        assert_eq!(class_of_char_with('/', Convention::Markup), Class::Bin);
678        assert_eq!(class_of_char_with(':', Convention::MathType), Class::Rel);
679        assert_eq!(class_of_char_with(':', Convention::Markup), Class::Punct);
680    }
681
682    #[test]
683    fn the_markup_convention_leaves_the_arrows_and_the_extra_relations_ordinary() {
684        for ch in [
685            '\u{2191}', '\u{2193}', '\u{2194}', '\u{21A6}', '\u{27F9}', '\u{27FA}', '\u{220B}',
686            '\u{2225}', '\u{2223}', '\u{2224}', '\u{22A5}', '\u{224D}', '\u{226A}', '\u{226B}',
687        ] {
688            assert_eq!(class_of_char_with(ch, Convention::MathType), Class::Rel);
689            assert_eq!(
690                class_of_char_with(ch, Convention::Markup),
691                Class::Ord,
692                "{ch:?} is ordinary in the markup convention"
693            );
694        }
695    }
696
697    #[test]
698    fn the_markup_convention_leaves_the_union_intersection_and_the_circled_operators_ordinary() {
699        for ch in ['\u{2229}', '\u{222A}'] {
700            assert_eq!(class_of_char_with(ch, Convention::MathType), Class::Bin);
701            assert_eq!(class_of_char_with(ch, Convention::Markup), Class::Ord);
702        }
703        for ch in ['\u{2A00}', '\u{2A03}', '\u{2A04}', '\u{2A05}', '\u{2A06}'] {
704            assert!(is_large_op_with(ch, Convention::MathType), "{ch:?}");
705            assert!(!is_large_op_with(ch, Convention::Markup), "{ch:?}");
706            assert_eq!(class_of_char_with(ch, Convention::Markup), Class::Ord);
707        }
708    }
709
710    #[test]
711    fn both_conventions_agree_on_the_shared_operators_and_the_delimiters() {
712        for ch in [
713            '\u{2211}', '\u{220F}', '\u{2210}', '\u{222B}', '\u{2230}', '\u{22C0}', '\u{22C1}',
714            '\u{22C2}', '\u{22C3}', '\u{2A01}', '\u{2A02}',
715        ] {
716            assert!(is_large_op_with(ch, Convention::MathType), "{ch:?}");
717            assert!(is_large_op_with(ch, Convention::Markup), "{ch:?}");
718        }
719        for (ch, class) in [
720            ('(', Class::Open),
721            (')', Class::Close),
722            ('[', Class::Open),
723            (']', Class::Close),
724            ('{', Class::Open),
725            ('}', Class::Close),
726            ('\u{27E8}', Class::Open),
727            ('\u{27E9}', Class::Close),
728            ('+', Class::Bin),
729            ('=', Class::Rel),
730            (',', Class::Punct),
731            (';', Class::Punct),
732            ('x', Class::Ord),
733            ('0', Class::Ord),
734        ] {
735            assert_eq!(class_of_char_with(ch, Convention::MathType), class);
736            assert_eq!(class_of_char_with(ch, Convention::Markup), class);
737        }
738    }
739
740    #[test]
741    fn the_letterlike_table_is_sorted_for_the_binary_search() {
742        assert!(
743            super::EXTRA_LETTERS
744                .windows(2)
745                .all(|pair| pair[0].0 < pair[1].0),
746            "the table is searched by character, so it must stay sorted"
747        );
748    }
749
750    #[test]
751    fn letterlike_symbols_name_the_letter_and_face_math_type_saved_them_for() {
752        use super::extra_letter;
753        assert_eq!(
754            extra_letter('\u{211D}'),
755            Some(('R', crate::Font::DoubleStruck))
756        );
757        assert_eq!(extra_letter('\u{212C}'), Some(('B', crate::Font::Script)));
758        assert_eq!(extra_letter('\u{212D}'), Some(('C', crate::Font::Fraktur)));
759        assert_eq!(extra_letter('R'), None);
760        assert_eq!(
761            extra_letter('\u{1D54D}'),
762            None,
763            "the block has no BMP character"
764        );
765    }
766
767    #[test]
768    fn the_extra_face_covers_the_letterlike_symbols_in_the_bmp() {
769        use super::is_extra_face;
770        for ch in ['\u{211D}', '\u{212C}', '\u{212D}', '\u{2102}', '\u{2134}'] {
771            assert!(is_extra_face(ch), "{ch:?} is saved in the extra face");
772        }
773        for ch in ['R', '\u{1D54D}', '\u{2113}', '\u{210F}'] {
774            assert!(!is_extra_face(ch), "{ch:?} is not saved in the extra face");
775        }
776    }
777
778    #[test]
779    fn the_symbol_encoding_names_the_legacy_positions() {
780        assert_eq!(symbol_code('\u{03B1}'), Some(0x61));
781        assert_eq!(symbol_code('\u{0394}'), Some(0x44));
782        assert_eq!(symbol_code('\u{03BC}'), Some(0x6D));
783        assert_eq!(symbol_code('\u{03A9}'), Some(0x57));
784        assert_eq!(symbol_code('\u{03C2}'), Some(0x56));
785        assert_eq!(symbol_code('\u{2211}'), Some(0xE5));
786        assert_eq!(symbol_code('\u{222B}'), Some(0xF2));
787        assert_eq!(symbol_code('\u{2264}'), Some(0xA3));
788        assert_eq!(
789            symbol_code('\u{2223}'),
790            Some(0x7C),
791            "the relation takes the bar glyph's position"
792        );
793        assert_eq!(symbol_code('A'), Some(0x41), "ASCII sits at its own code");
794        assert_eq!(symbol_code('\u{1D465}'), None);
795    }
796
797    #[test]
798    fn the_extra_encoding_names_the_math_type_positions() {
799        assert_eq!(extra_code('\u{019B}'), Some(0x44));
800        assert_eq!(extra_code('\u{2113}'), Some(0x6C));
801        assert_eq!(extra_code('\u{210F}'), Some(0x68));
802        assert_eq!(extra_code('\u{2213}'), Some(0x6D));
803        assert_eq!(extra_code('\u{20D7}'), Some(0x72));
804        assert_eq!(extra_code('\u{211D}'), Some(0xA1));
805        assert_eq!(extra_code('\u{2115}'), Some(0xA5));
806        assert_eq!(extra_code('\u{22C3}'), Some(0x55));
807        assert_eq!(extra_code('\u{22C2}'), Some(0x49));
808        assert_eq!(extra_code('\u{225C}'), Some(0x40));
809        assert_eq!(extra_code('\u{22EF}'), Some(0x4C));
810        assert_eq!(extra_code('\u{212C}'), None, "the face has no script B");
811        assert_eq!(extra_code('\u{1D465}'), None);
812    }
813
814    #[test]
815    fn a_symbol_takes_the_symbol_face_unless_only_the_extra_encoding_places_it() {
816        assert_eq!(symbol_position('\u{2211}'), Some((Font::Symbol, 0xE5)));
817        assert_eq!(symbol_position('\u{2229}'), Some((Font::Symbol, 0xC7)));
818        assert_eq!(symbol_position('\u{2113}'), Some((Font::Extra, 0x6C)));
819        assert_eq!(symbol_position('\u{210F}'), Some((Font::Extra, 0x68)));
820        assert_eq!(symbol_position('\u{2213}'), Some((Font::Extra, 0x6D)));
821        assert_eq!(symbol_position('\u{22C3}'), Some((Font::Extra, 0x55)));
822        assert_eq!(symbol_position('\u{22C2}'), Some((Font::Extra, 0x49)));
823        assert_eq!(symbol_position('\u{225C}'), Some((Font::Extra, 0x40)));
824        assert_eq!(symbol_position('\u{22EF}'), Some((Font::Extra, 0x4C)));
825        assert_eq!(
826            symbol_position('\u{222A}'),
827            Some((Font::Symbol, 0xC8)),
828            "a character the Symbol encoding places stays in that face"
829        );
830        assert_eq!(
831            symbol_position('\u{2223}'),
832            Some((Font::Symbol, 0x7C)),
833            "the relation takes the Symbol face's bar"
834        );
835        assert_eq!(symbol_position('\u{2225}'), None);
836    }
837}