oxedyne/fe2o3/fe2o3_austenite/src/math/class.rs
12.0 KiB, 1 run
created by r1870400018:63540, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Original work copyright (c) the Typst project authors (typst-utils `default_math_class`, with the Unicode maths classes as the `unicode-math-class` crate tabulates them, version 0.15.1). |
| 3 | // Modified for Austenite: ported to Hematite's types, IR and error handling. |
| 4 | //! Unicode maths classes (UTR #25, `MathClass-15.txt`, as the `unicode-math-class` crate tabulates it) |
| 5 | //! with Typst 0.15's overrides from `typst_utils::default_math_class`. The class decides a character's |
| 6 | //! spacing, its default limits and whether it may stretch as a delimiter. |
| 7 | //! |
| 8 | //! The table belongs in `fe2o3_text::unicode` beside the other character properties; it sits here until |
| 9 | //! that lookup exists. |
| 10 | |
| 11 | /// A character's maths class. |
| 12 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] |
| 13 | pub enum MathClass { |
| 14 | Normal, |
| 15 | Alphabetic, |
| 16 | Binary, |
| 17 | Closing, |
| 18 | Diacritic, |
| 19 | Fence, |
| 20 | GlyphPart, |
| 21 | Large, |
| 22 | Opening, |
| 23 | Punctuation, |
| 24 | Relation, |
| 25 | Space, |
| 26 | Unary, |
| 27 | Vary, |
| 28 | Special, |
| 29 | } |
| 30 | |
| 31 | impl MathClass { |
| 32 | /// The class by its Typst name, as `math.class("relation", ..)` takes it. |
| 33 | pub fn from_name(s: &str) -> Option<Self> { |
| 34 | Some(match s { |
| 35 | "normal" => MathClass::Normal, |
| 36 | "punctuation" => MathClass::Punctuation, |
| 37 | "opening" => MathClass::Opening, |
| 38 | "closing" => MathClass::Closing, |
| 39 | "fence" => MathClass::Fence, |
| 40 | "large" => MathClass::Large, |
| 41 | "relation" => MathClass::Relation, |
| 42 | "unary" => MathClass::Unary, |
| 43 | "binary" => MathClass::Binary, |
| 44 | "vary" => MathClass::Vary, |
| 45 | _ => return None, |
| 46 | }) |
| 47 | } |
| 48 | |
| 49 | pub fn name(self) -> &'static str { |
| 50 | match self { |
| 51 | MathClass::Normal => "normal", |
| 52 | MathClass::Alphabetic => "alphabetic", |
| 53 | MathClass::Binary => "binary", |
| 54 | MathClass::Closing => "closing", |
| 55 | MathClass::Diacritic => "diacritic", |
| 56 | MathClass::Fence => "fence", |
| 57 | MathClass::GlyphPart => "glyph-part", |
| 58 | MathClass::Large => "large", |
| 59 | MathClass::Opening => "opening", |
| 60 | MathClass::Punctuation => "punctuation", |
| 61 | MathClass::Relation => "relation", |
| 62 | MathClass::Space => "space", |
| 63 | MathClass::Unary => "unary", |
| 64 | MathClass::Vary => "vary", |
| 65 | MathClass::Special => "special", |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | /// The class Typst gives a character: the UTR #25 class with Typst's spacing overrides. |
| 71 | pub fn default_math_class(c: char) -> Option<MathClass> { |
| 72 | match c { |
| 73 | ':' => Some(MathClass::Relation), |
| 74 | '\u{22ef}' | '\u{22f1}' | '\u{22f0}' | '\u{22ee}' => Some(MathClass::Normal), |
| 75 | '.' | '/' => Some(MathClass::Normal), |
| 76 | '\u{22a5}' => Some(MathClass::Normal), |
| 77 | '\u{214b}' => Some(MathClass::Binary), |
| 78 | '\u{23b0}' | '\u{27c5}' => Some(MathClass::Opening), |
| 79 | '\u{23b1}' | '\u{27c6}' => Some(MathClass::Closing), |
| 80 | '\u{27c7}' => Some(MathClass::Binary), |
| 81 | '\u{060c}' => Some(MathClass::Punctuation), |
| 82 | c => unicode_class(c), |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | /// The UTR #25 class alone. |
| 87 | pub fn unicode_class(c: char) -> Option<MathClass> { |
| 88 | let cp = c as u32; |
| 89 | let i = match CLASSES.binary_search_by(|&(lo, hi, _)| { |
| 90 | if hi < cp { |
| 91 | std::cmp::Ordering::Less |
| 92 | } else if lo > cp { |
| 93 | std::cmp::Ordering::Greater |
| 94 | } else { |
| 95 | std::cmp::Ordering::Equal |
| 96 | } |
| 97 | }) { |
| 98 | Ok(i) => i, |
| 99 | Err(_) => return None, |
| 100 | }; |
| 101 | CLASSES.get(i).map(|e| e.2) |
| 102 | } |
| 103 | |
| 104 | use MathClass::{ |
| 105 | Alphabetic as A, |
| 106 | Binary as B, |
| 107 | Closing as C, |
| 108 | Diacritic as D, |
| 109 | Fence as F, |
| 110 | GlyphPart as G, |
| 111 | Large as L, |
| 112 | Normal as N, |
| 113 | Opening as O, |
| 114 | Punctuation as P, |
| 115 | Relation as R, |
| 116 | Space as S, |
| 117 | Unary as U, |
| 118 | Vary as V, |
| 119 | Special as X, |
| 120 | }; |
| 121 | |
| 122 | // Inclusive code point ranges and their class, sorted, 402 runs of the 2736 classified characters. |
| 123 | static CLASSES: &[(u32, u32, MathClass)] = &[ |
| 124 | (0x0020, 0x0020, S), (0x0021, 0x0021, N), (0x0023, 0x0026, N), (0x0028, 0x0028, O), |
| 125 | (0x0029, 0x0029, C), (0x002a, 0x002a, N), (0x002b, 0x002b, V), (0x002c, 0x002c, P), |
| 126 | (0x002d, 0x002d, N), (0x002e, 0x002e, P), (0x002f, 0x002f, B), (0x0030, 0x0039, N), |
| 127 | (0x003a, 0x003b, P), (0x003c, 0x003e, R), (0x003f, 0x003f, P), (0x0040, 0x0040, N), |
| 128 | (0x0041, 0x005a, A), (0x005b, 0x005b, O), (0x005c, 0x005c, B), (0x005d, 0x005d, C), |
| 129 | (0x005e, 0x005f, N), (0x0060, 0x0060, D), (0x0061, 0x007a, A), (0x007b, 0x007b, O), |
| 130 | (0x007c, 0x007c, F), (0x007d, 0x007d, C), (0x007e, 0x007e, N), (0x00a0, 0x00a0, S), |
| 131 | (0x00a1, 0x00a1, P), (0x00a2, 0x00a7, N), (0x00a8, 0x00a8, D), (0x00ac, 0x00ac, U), |
| 132 | (0x00af, 0x00af, D), (0x00b0, 0x00b0, N), (0x00b1, 0x00b1, V), (0x00b2, 0x00b6, N), |
| 133 | (0x00b7, 0x00b7, B), (0x00b9, 0x00b9, N), (0x00bc, 0x00be, N), (0x00bf, 0x00bf, P), |
| 134 | (0x00d7, 0x00d7, B), (0x00f7, 0x00f7, B), (0x0131, 0x0131, A), (0x0237, 0x0237, A), |
| 135 | (0x02c6, 0x02c7, D), (0x02d8, 0x02da, D), (0x02dc, 0x02dc, D), (0x0300, 0x0308, D), |
| 136 | (0x030a, 0x030a, D), (0x030c, 0x030c, D), (0x0311, 0x0311, D), (0x0323, 0x0323, D), |
| 137 | (0x032c, 0x0333, D), (0x0338, 0x0338, D), (0x033a, 0x033a, D), (0x033f, 0x033f, D), |
| 138 | (0x0346, 0x0346, D), (0x0391, 0x03a1, A), (0x03a3, 0x03a4, A), (0x03a6, 0x03a9, A), |
| 139 | (0x03b1, 0x03c1, A), (0x03c3, 0x03c9, A), (0x03d0, 0x03d2, A), (0x03d5, 0x03d6, A), |
| 140 | (0x03d8, 0x03d9, N), (0x03da, 0x03dd, A), (0x03e0, 0x03e1, A), (0x03f0, 0x03f1, A), |
| 141 | (0x03f4, 0x03f5, A), (0x03f6, 0x03f6, N), (0x0428, 0x0428, A), (0x0606, 0x0607, L), |
| 142 | (0x0608, 0x0608, A), (0x2000, 0x2007, S), (0x2009, 0x200b, S), (0x2010, 0x2010, P), |
| 143 | (0x2012, 0x2014, P), (0x2016, 0x2016, F), (0x2020, 0x2021, R), (0x2022, 0x2022, B), |
| 144 | (0x2026, 0x2026, N), (0x2032, 0x2037, N), (0x203b, 0x203c, N), (0x2040, 0x2040, B), |
| 145 | (0x2044, 0x2044, B), (0x204e, 0x204e, B), (0x204f, 0x2050, R), (0x2051, 0x2052, N), |
| 146 | (0x2057, 0x2057, N), (0x205f, 0x205f, S), (0x2061, 0x2062, B), (0x2063, 0x2063, P), |
| 147 | (0x2064, 0x2064, X), (0x207a, 0x207e, N), (0x208a, 0x208e, N), (0x20d0, 0x20d2, D), |
| 148 | (0x20d3, 0x20d3, X), (0x20d4, 0x20df, D), (0x20e1, 0x20e1, D), (0x20e4, 0x20ef, D), |
| 149 | (0x2102, 0x2102, A), (0x2107, 0x2107, N), (0x210a, 0x210d, A), (0x210e, 0x210f, N), |
| 150 | (0x2110, 0x2113, A), (0x2115, 0x2115, A), (0x2118, 0x211d, A), (0x2124, 0x2124, A), |
| 151 | (0x2126, 0x2127, N), (0x2128, 0x2128, A), (0x2129, 0x2129, N), (0x212b, 0x212d, A), |
| 152 | (0x212f, 0x2131, A), (0x2132, 0x2132, N), (0x2133, 0x2138, A), (0x213c, 0x213d, A), |
| 153 | (0x213e, 0x213e, N), (0x213f, 0x213f, A), (0x2140, 0x2140, L), (0x2141, 0x2149, N), |
| 154 | (0x214b, 0x214b, N), (0x2190, 0x21b3, R), (0x21b6, 0x21b7, R), (0x21ba, 0x21ff, R), |
| 155 | (0x2200, 0x2201, U), (0x2202, 0x2202, N), (0x2203, 0x2204, U), (0x2205, 0x2205, N), |
| 156 | (0x2206, 0x2207, U), (0x2208, 0x220d, R), (0x220e, 0x220e, N), (0x220f, 0x2211, L), |
| 157 | (0x2212, 0x2213, V), (0x2214, 0x2219, B), (0x221a, 0x221c, L), (0x221d, 0x221d, R), |
| 158 | (0x221e, 0x2222, N), (0x2223, 0x2226, R), (0x2227, 0x222a, B), (0x222b, 0x2233, L), |
| 159 | (0x2234, 0x2237, R), (0x2238, 0x2238, B), (0x2239, 0x223d, R), (0x223e, 0x223e, B), |
| 160 | (0x223f, 0x223f, N), (0x2240, 0x2240, B), (0x2241, 0x228b, R), (0x228c, 0x228e, B), |
| 161 | (0x228f, 0x2292, R), (0x2293, 0x22a1, B), (0x22a2, 0x22a3, R), (0x22a4, 0x22a4, N), |
| 162 | (0x22a5, 0x22b8, R), (0x22b9, 0x22bd, B), (0x22be, 0x22bf, N), (0x22c0, 0x22c3, L), |
| 163 | (0x22c4, 0x22c7, B), (0x22c8, 0x22c8, R), (0x22c9, 0x22cc, B), (0x22cd, 0x22cd, R), |
| 164 | (0x22ce, 0x22cf, B), (0x22d0, 0x22d1, R), (0x22d2, 0x22d3, B), (0x22d4, 0x22ff, R), |
| 165 | (0x2300, 0x2300, N), (0x2302, 0x2302, N), (0x2305, 0x2306, B), (0x2308, 0x2308, O), |
| 166 | (0x2309, 0x2309, C), (0x230a, 0x230a, O), (0x230b, 0x230b, C), (0x2310, 0x2311, N), |
| 167 | (0x2319, 0x2319, N), (0x231c, 0x231c, O), (0x231d, 0x231d, C), (0x231e, 0x231e, O), |
| 168 | (0x231f, 0x231f, C), (0x2320, 0x2321, G), (0x2322, 0x2323, R), (0x2336, 0x2336, N), |
| 169 | (0x233d, 0x233d, B), (0x233f, 0x233f, R), (0x237c, 0x237c, R), (0x2394, 0x2394, N), |
| 170 | (0x239b, 0x23af, G), (0x23b0, 0x23b1, R), (0x23b2, 0x23b3, G), (0x23b4, 0x23b6, N), |
| 171 | (0x23b7, 0x23b7, G), (0x23d0, 0x23d0, G), (0x23dc, 0x23e7, N), (0x24c8, 0x24c8, N), |
| 172 | (0x25a0, 0x25a1, N), (0x25aa, 0x25ab, N), (0x25ad, 0x25b1, N), (0x25b2, 0x25b9, B), |
| 173 | (0x25bc, 0x25c5, B), (0x25c6, 0x25c9, N), (0x25ca, 0x25cb, B), (0x25ce, 0x25d3, N), |
| 174 | (0x25d6, 0x25d7, N), (0x25e2, 0x25e5, N), (0x25e6, 0x25e6, B), (0x25e7, 0x25ea, N), |
| 175 | (0x25eb, 0x25ec, B), (0x25ef, 0x25ef, N), (0x25f8, 0x25ff, B), (0x2605, 0x2606, B), |
| 176 | (0x2609, 0x2609, N), (0x260c, 0x260c, N), (0x263d, 0x2644, N), (0x2646, 0x2649, N), |
| 177 | (0x2660, 0x2667, N), (0x2669, 0x2669, N), (0x266d, 0x266f, N), (0x2680, 0x2689, N), |
| 178 | (0x26aa, 0x26ac, N), (0x26b2, 0x26b2, N), (0x2713, 0x2713, N), (0x2717, 0x2717, N), |
| 179 | (0x2720, 0x2720, N), (0x272a, 0x272a, N), (0x2736, 0x2736, N), (0x2772, 0x2772, O), |
| 180 | (0x2773, 0x2773, C), (0x27c0, 0x27c1, N), (0x27c2, 0x27cb, R), (0x27cc, 0x27cc, L), |
| 181 | (0x27cd, 0x27cd, R), (0x27ce, 0x27cf, B), (0x27d0, 0x27d0, N), (0x27d1, 0x27d1, B), |
| 182 | (0x27d2, 0x27d4, R), (0x27d5, 0x27d9, L), (0x27da, 0x27df, R), (0x27e0, 0x27e5, B), |
| 183 | (0x27e6, 0x27e6, O), (0x27e7, 0x27e7, C), (0x27e8, 0x27e8, O), (0x27e9, 0x27e9, C), |
| 184 | (0x27ea, 0x27ea, O), (0x27eb, 0x27eb, C), (0x27ec, 0x27ec, O), (0x27ed, 0x27ed, C), |
| 185 | (0x27ee, 0x27ee, O), (0x27ef, 0x27ef, C), (0x27f0, 0x27ff, R), (0x2900, 0x297f, R), |
| 186 | (0x2980, 0x2980, F), (0x2981, 0x2981, N), (0x2982, 0x2982, F), (0x2983, 0x2983, O), |
| 187 | (0x2984, 0x2984, C), (0x2985, 0x2985, O), (0x2986, 0x2986, C), (0x2987, 0x2987, O), |
| 188 | (0x2988, 0x2988, C), (0x2989, 0x2989, O), (0x298a, 0x298a, C), (0x298b, 0x298b, O), |
| 189 | (0x298c, 0x298c, C), (0x298d, 0x298d, O), (0x298e, 0x298e, C), (0x298f, 0x298f, O), |
| 190 | (0x2990, 0x2990, C), (0x2991, 0x2991, O), (0x2992, 0x2992, C), (0x2993, 0x2993, O), |
| 191 | (0x2994, 0x2994, C), (0x2995, 0x2995, O), (0x2996, 0x2996, C), (0x2997, 0x2997, O), |
| 192 | (0x2998, 0x2998, C), (0x2999, 0x299a, F), (0x299b, 0x29b5, N), (0x29b6, 0x29b9, B), |
| 193 | (0x29ba, 0x29bf, N), (0x29c0, 0x29c1, B), (0x29c2, 0x29c3, N), (0x29c4, 0x29c8, B), |
| 194 | (0x29c9, 0x29cd, N), (0x29ce, 0x29d5, R), (0x29d6, 0x29d7, B), (0x29d8, 0x29d8, O), |
| 195 | (0x29d9, 0x29d9, C), (0x29da, 0x29da, O), (0x29db, 0x29db, C), (0x29dc, 0x29de, N), |
| 196 | (0x29df, 0x29df, R), (0x29e0, 0x29e0, N), (0x29e1, 0x29e1, R), (0x29e2, 0x29e2, B), |
| 197 | (0x29e3, 0x29e6, R), (0x29e7, 0x29ea, N), (0x29eb, 0x29eb, B), (0x29ec, 0x29f3, N), |
| 198 | (0x29f4, 0x29f4, R), (0x29f5, 0x29f7, B), (0x29f8, 0x29f9, L), (0x29fa, 0x29fb, B), |
| 199 | (0x29fc, 0x29fc, O), (0x29fd, 0x29fd, C), (0x29fe, 0x29ff, B), (0x2a00, 0x2a21, L), |
| 200 | (0x2a22, 0x2a58, B), (0x2a59, 0x2a59, R), (0x2a5a, 0x2a65, B), (0x2a66, 0x2a70, R), |
| 201 | (0x2a71, 0x2a72, B), (0x2a73, 0x2ae0, R), (0x2ae1, 0x2ae1, N), (0x2ae2, 0x2af0, R), |
| 202 | (0x2af1, 0x2af1, N), (0x2af2, 0x2af3, R), (0x2af4, 0x2af6, B), (0x2af7, 0x2afa, R), |
| 203 | (0x2afb, 0x2afb, B), (0x2afc, 0x2afc, L), (0x2afd, 0x2afe, B), (0x2aff, 0x2aff, L), |
| 204 | (0x2b00, 0x2b11, R), (0x2b12, 0x2b19, N), (0x2b1b, 0x2b2f, N), (0x2b30, 0x2b4c, R), |
| 205 | (0x2b50, 0x2b54, N), (0x2b95, 0x2b95, R), (0x2bc2, 0x2bc8, N), (0x2bca, 0x2bcb, N), |
| 206 | (0x3008, 0x3009, X), (0x301a, 0x301b, X), (0x306e, 0x306e, N), (0xfb29, 0xfb29, X), |
| 207 | (0xfe00, 0xfe00, D), (0xfe61, 0xfe66, X), (0xfe68, 0xfe68, X), (0xff0b, 0xff0b, X), |
| 208 | (0xff1c, 0xff1e, X), (0xff3c, 0xff3c, X), (0xff3e, 0xff3e, X), (0xff5c, 0xff5c, X), |
| 209 | (0xff5e, 0xff5e, X), (0xffe2, 0xffe2, X), (0xffe9, 0xffec, X), (0x1d400, 0x1d454, A), |
| 210 | (0x1d456, 0x1d49c, A), (0x1d49e, 0x1d49f, A), (0x1d4a2, 0x1d4a2, A), (0x1d4a5, 0x1d4a6, A), |
| 211 | (0x1d4a9, 0x1d4ac, A), (0x1d4ae, 0x1d4b9, A), (0x1d4bb, 0x1d4bb, A), (0x1d4bd, 0x1d4c3, A), |
| 212 | (0x1d4c5, 0x1d505, A), (0x1d507, 0x1d50a, A), (0x1d50d, 0x1d514, A), (0x1d516, 0x1d51c, A), |
| 213 | (0x1d51e, 0x1d539, A), (0x1d53b, 0x1d53e, A), (0x1d540, 0x1d544, A), (0x1d546, 0x1d546, A), |
| 214 | (0x1d54a, 0x1d550, A), (0x1d552, 0x1d6a5, A), (0x1d6a8, 0x1d7cb, A), (0x1d7ce, 0x1d7ff, N), |
| 215 | (0x1ee00, 0x1ee03, A), (0x1ee05, 0x1ee1f, A), (0x1ee21, 0x1ee22, A), (0x1ee24, 0x1ee24, A), |
| 216 | (0x1ee27, 0x1ee27, A), (0x1ee29, 0x1ee32, A), (0x1ee34, 0x1ee37, A), (0x1ee39, 0x1ee39, A), |
| 217 | (0x1ee3b, 0x1ee3b, A), (0x1ee42, 0x1ee42, A), (0x1ee47, 0x1ee47, A), (0x1ee49, 0x1ee49, A), |
| 218 | (0x1ee4b, 0x1ee4b, A), (0x1ee4d, 0x1ee4f, A), (0x1ee51, 0x1ee52, A), (0x1ee54, 0x1ee54, A), |
| 219 | (0x1ee57, 0x1ee57, A), (0x1ee59, 0x1ee59, A), (0x1ee5b, 0x1ee5b, A), (0x1ee5d, 0x1ee5d, A), |
| 220 | (0x1ee5f, 0x1ee5f, A), (0x1ee61, 0x1ee62, A), (0x1ee64, 0x1ee64, A), (0x1ee67, 0x1ee6a, A), |
| 221 | (0x1ee6c, 0x1ee72, A), (0x1ee74, 0x1ee77, A), (0x1ee79, 0x1ee7c, A), (0x1ee7e, 0x1ee7e, A), |
| 222 | (0x1ee80, 0x1ee89, A), (0x1ee8b, 0x1ee9b, A), (0x1eea1, 0x1eea3, A), (0x1eea5, 0x1eea9, A), |
| 223 | (0x1eeab, 0x1eebb, A), (0x1eef0, 0x1eef1, L), (0x1f784, 0x1f784, N), (0x1f78c, 0x1f78d, N), |
| 224 | (0x1f797, 0x1f799, N), (0x1f79d, 0x1f79f, N), |
| 225 | ]; |