oxedyne/fe2o3/fe2o3_austenite/src/math/style.rs
12.2 KiB, 1 run
created by r1870400018:63558, 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 (the `codex` crate's `styling` module, version 0.3, as Typst 0.15.1 uses it). |
| 3 | // Modified for Austenite: ported to Hematite's types, IR and error handling. |
| 4 | //! Maths alphabets: which Mathematical Alphanumeric Symbol a letter becomes under `bold`, `italic` and |
| 5 | //! the variant functions (`bb`, `cal`, `frak`, ...), ported from the `codex` crate's `styling` module |
| 6 | //! (0.3, Apache-2.0) that Typst 0.15 uses. Only the styles `select` can return are carried. |
| 7 | |
| 8 | /// How big a maths expression is set: display and text at full size, scripts smaller. |
| 9 | #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 10 | pub enum MathSize { |
| 11 | ScriptScript, |
| 12 | Script, |
| 13 | Text, |
| 14 | Display, |
| 15 | } |
| 16 | |
| 17 | impl MathSize { |
| 18 | pub fn name(self) -> &'static str { |
| 19 | match self { |
| 20 | MathSize::ScriptScript => "script-script", |
| 21 | MathSize::Script => "script", |
| 22 | MathSize::Text => "text", |
| 23 | MathSize::Display => "display", |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | pub fn from_name(s: &str) -> Option<Self> { |
| 28 | Some(match s { |
| 29 | "script-script" => MathSize::ScriptScript, |
| 30 | "script" => MathSize::Script, |
| 31 | "text" => MathSize::Text, |
| 32 | "display" => MathSize::Display, |
| 33 | _ => return None, |
| 34 | }) |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | /// The alphabet a variant function selects. |
| 39 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] |
| 40 | pub enum MathVariant { |
| 41 | Plain, |
| 42 | Fraktur, |
| 43 | SansSerif, |
| 44 | Monospace, |
| 45 | DoubleStruck, |
| 46 | Chancery, |
| 47 | Roundhand, |
| 48 | } |
| 49 | |
| 50 | impl MathVariant { |
| 51 | pub fn name(self) -> &'static str { |
| 52 | match self { |
| 53 | MathVariant::Plain => "plain", |
| 54 | MathVariant::Fraktur => "fraktur", |
| 55 | MathVariant::SansSerif => "sans-serif", |
| 56 | MathVariant::Monospace => "monospace", |
| 57 | MathVariant::DoubleStruck => "double-struck", |
| 58 | MathVariant::Chancery => "chancery", |
| 59 | MathVariant::Roundhand => "roundhand", |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | pub fn from_name(s: &str) -> Option<Self> { |
| 64 | Some(match s { |
| 65 | "plain" => MathVariant::Plain, |
| 66 | "fraktur" => MathVariant::Fraktur, |
| 67 | "sans-serif" => MathVariant::SansSerif, |
| 68 | "monospace" => MathVariant::Monospace, |
| 69 | "double-struck" => MathVariant::DoubleStruck, |
| 70 | "chancery" => MathVariant::Chancery, |
| 71 | "roundhand" => MathVariant::Roundhand, |
| 72 | _ => return None, |
| 73 | }) |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 78 | pub enum MathStyle { |
| 79 | Plain, |
| 80 | Bold, |
| 81 | Italic, |
| 82 | BoldItalic, |
| 83 | Fraktur, |
| 84 | BoldFraktur, |
| 85 | SansSerif, |
| 86 | SansSerifBold, |
| 87 | SansSerifItalic, |
| 88 | SansSerifBoldItalic, |
| 89 | Monospace, |
| 90 | DoubleStruck, |
| 91 | DoubleStruckItalic, |
| 92 | Chancery, |
| 93 | BoldChancery, |
| 94 | Roundhand, |
| 95 | BoldRoundhand, |
| 96 | Hebrew, |
| 97 | } |
| 98 | |
| 99 | impl MathStyle { |
| 100 | /// The style a character takes under a variant, boldness and italic setting (`None`: automatic, |
| 101 | /// italic for Latin and lower-case Greek letters). |
| 102 | pub fn select(c: char, variant: Option<MathVariant>, bold: bool, italic: Option<bool>) -> MathStyle { |
| 103 | use MathVariant as Mv; |
| 104 | match (variant.unwrap_or(Mv::Plain), bold, italic) { |
| 105 | (Mv::SansSerif, false, Some(false)) if is_latin(c) => MathStyle::SansSerif, |
| 106 | (Mv::SansSerif, false, _) if is_latin(c) => MathStyle::SansSerifItalic, |
| 107 | (Mv::SansSerif, true, Some(false)) if is_latin(c) => MathStyle::SansSerifBold, |
| 108 | (Mv::SansSerif, true, _) if is_latin(c) => MathStyle::SansSerifBoldItalic, |
| 109 | (Mv::SansSerif, false, _) if is_digit(c) => MathStyle::SansSerif, |
| 110 | (Mv::SansSerif, true, _) if is_digit(c) => MathStyle::SansSerifBold, |
| 111 | (Mv::SansSerif, _, Some(false)) if is_greek(c) => MathStyle::SansSerifBold, |
| 112 | (Mv::SansSerif, _, Some(true)) if is_greek(c) => MathStyle::SansSerifBoldItalic, |
| 113 | (Mv::SansSerif, _, None) if is_upper_greek(c) => MathStyle::SansSerifBold, |
| 114 | (Mv::SansSerif, _, None) if is_lower_greek(c) => MathStyle::SansSerifBoldItalic, |
| 115 | (Mv::Fraktur, false, _) if is_latin(c) => MathStyle::Fraktur, |
| 116 | (Mv::Fraktur, true, _) if is_latin(c) => MathStyle::BoldFraktur, |
| 117 | (Mv::Monospace, _, _) if is_digit(c) || is_latin(c) => MathStyle::Monospace, |
| 118 | (Mv::DoubleStruck, _, Some(true)) if matches!(c, 'D' | 'd' | 'e' | 'i' | 'j') |
| 119 | => MathStyle::DoubleStruckItalic, |
| 120 | (Mv::DoubleStruck, _, _) if is_digit(c) || is_latin(c) |
| 121 | || matches!(c, '\u{2211}' | '\u{393}' | '\u{3a0}' | '\u{3b3}' | '\u{3c0}') |
| 122 | => MathStyle::DoubleStruck, |
| 123 | (Mv::Chancery, false, _) if is_latin(c) => MathStyle::Chancery, |
| 124 | (Mv::Chancery, true, _) if is_latin(c) => MathStyle::BoldChancery, |
| 125 | (Mv::Roundhand, false, _) if is_latin(c) => MathStyle::Roundhand, |
| 126 | (Mv::Roundhand, true, _) if is_latin(c) => MathStyle::BoldRoundhand, |
| 127 | (_, false, Some(true)) if is_latin(c) || is_greek(c) => MathStyle::Italic, |
| 128 | (_, false, None) if is_latin(c) || is_lower_greek(c) => MathStyle::Italic, |
| 129 | (_, true, Some(false)) if is_latin(c) || is_greek(c) => MathStyle::Bold, |
| 130 | (_, true, Some(true)) if is_latin(c) || is_greek(c) => MathStyle::BoldItalic, |
| 131 | (_, true, None) if is_latin(c) || is_lower_greek(c) => MathStyle::BoldItalic, |
| 132 | (_, true, None) if is_upper_greek(c) => MathStyle::Bold, |
| 133 | (_, true, _) if is_digit(c) || matches!(c, '\u{3dc}' | '\u{3dd}') => MathStyle::Bold, |
| 134 | (_, _, Some(true) | None) if matches!(c, '\u{131}' | '\u{237}' | '\u{127}') => MathStyle::Italic, |
| 135 | (_, _, Some(true) | None) if is_hebrew(c) => MathStyle::Hebrew, |
| 136 | _ => MathStyle::Plain, |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | /// Appends the styled form of `c` to `out`: one character, or two where a variation selector picks |
| 142 | /// the chancery or roundhand script. |
| 143 | pub fn push_styled(out: &mut String, c: char, style: MathStyle) { |
| 144 | let (a, b) = match style { |
| 145 | MathStyle::Plain => (c, None), |
| 146 | MathStyle::Bold => (to_bold(c), None), |
| 147 | MathStyle::Italic => (to_italic(c), None), |
| 148 | MathStyle::BoldItalic => (to_bold_italic(c), None), |
| 149 | MathStyle::Fraktur => (to_fraktur(c), None), |
| 150 | MathStyle::BoldFraktur => (to_bold_fraktur(c), None), |
| 151 | MathStyle::SansSerif => (to_sans_serif(c), None), |
| 152 | MathStyle::SansSerifBold => (to_sans_serif_bold(c), None), |
| 153 | MathStyle::SansSerifItalic => (to_sans_serif_italic(c), None), |
| 154 | MathStyle::SansSerifBoldItalic => (to_sans_serif_bold_italic(c), None), |
| 155 | MathStyle::Monospace => (to_monospace(c), None), |
| 156 | MathStyle::DoubleStruck => (to_double_struck(c), None), |
| 157 | MathStyle::DoubleStruckItalic => (to_double_struck_italic(c), None), |
| 158 | MathStyle::Chancery => (to_script(c), is_latin(c).then_some(VS1)), |
| 159 | MathStyle::BoldChancery => (to_bold_script(c), is_latin(c).then_some(VS1)), |
| 160 | MathStyle::Roundhand => (to_script(c), is_latin(c).then_some(VS2)), |
| 161 | MathStyle::BoldRoundhand => (to_bold_script(c), is_latin(c).then_some(VS2)), |
| 162 | MathStyle::Hebrew => (to_hebrew(c), None), |
| 163 | }; |
| 164 | out.push(a); |
| 165 | if let Some(b) = b { |
| 166 | out.push(b); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | /// A string restyled character by character, as maths text and symbols are. |
| 171 | pub fn styled(text: &str, variant: Option<MathVariant>, bold: bool, italic: Option<bool>) -> String { |
| 172 | let mut out = String::with_capacity(text.len()); |
| 173 | for c in text.chars() { |
| 174 | push_styled(&mut out, c, MathStyle::select(c, variant, bold, italic)); |
| 175 | } |
| 176 | out |
| 177 | } |
| 178 | |
| 179 | const VS1: char = '\u{fe00}'; |
| 180 | const VS2: char = '\u{fe01}'; |
| 181 | |
| 182 | fn is_digit(c: char) -> bool { c.is_ascii_digit() } |
| 183 | fn is_latin(c: char) -> bool { c.is_ascii_alphabetic() } |
| 184 | fn is_greek(c: char) -> bool { is_upper_greek(c) || is_lower_greek(c) } |
| 185 | fn is_upper_greek(c: char) -> bool { matches!(c, '\u{391}'..='\u{3a9}' | '\u{2207}' | '\u{3f4}') } |
| 186 | fn is_hebrew(c: char) -> bool { matches!(c, '\u{5d0}'..='\u{5d3}') } |
| 187 | |
| 188 | fn is_lower_greek(c: char) -> bool { |
| 189 | matches!(c, '\u{3b1}'..='\u{3c9}' | '\u{2202}' | '\u{3f5}' | '\u{3d1}' | '\u{3f0}' | '\u{3d5}' | '\u{3f1}' | '\u{3d6}') |
| 190 | } |
| 191 | |
| 192 | // Every delta lands inside the Unicode scalar range by construction; an impossible sum keeps the letter. |
| 193 | fn shift(c: char, delta: u32) -> char { |
| 194 | char::from_u32(c as u32 + delta).unwrap_or(c) |
| 195 | } |
| 196 | |
| 197 | fn to_bold(c: char) -> char { |
| 198 | let d = match c { |
| 199 | 'A'..='Z' => 0x1D3BF, |
| 200 | 'a'..='z' => 0x1D3B9, |
| 201 | '\u{391}'..='\u{3a1}' => 0x1D317, |
| 202 | '\u{3f4}' => 0x1D2C5, |
| 203 | '\u{3a3}'..='\u{3a9}' => 0x1D317, |
| 204 | '\u{2207}' => 0x1B4BA, |
| 205 | '\u{3b1}'..='\u{3c9}' => 0x1D311, |
| 206 | '\u{2202}' => 0x1B4D9, |
| 207 | '\u{3f5}' => 0x1D2E7, |
| 208 | '\u{3d1}' => 0x1D30C, |
| 209 | '\u{3f0}' => 0x1D2EE, |
| 210 | '\u{3d5}' => 0x1D30A, |
| 211 | '\u{3f1}' => 0x1D2EF, |
| 212 | '\u{3d6}' => 0x1D30B, |
| 213 | '\u{3dc}'..='\u{3dd}' => 0x1D3EE, |
| 214 | '0'..='9' => 0x1D79E, |
| 215 | _ => return c, |
| 216 | }; |
| 217 | shift(c, d) |
| 218 | } |
| 219 | |
| 220 | fn to_italic(c: char) -> char { |
| 221 | let d = match c { |
| 222 | 'h' => 0x20A6, |
| 223 | '\u{127}' => 0x1FE8, |
| 224 | 'A'..='Z' => 0x1D3F3, |
| 225 | 'a'..='z' => 0x1D3ED, |
| 226 | '\u{131}' => 0x1D573, |
| 227 | '\u{237}' => 0x1D46E, |
| 228 | '\u{391}'..='\u{3a1}' => 0x1D351, |
| 229 | '\u{3f4}' => 0x1D2FF, |
| 230 | '\u{3a3}'..='\u{3a9}' => 0x1D351, |
| 231 | '\u{2207}' => 0x1B4F4, |
| 232 | '\u{3b1}'..='\u{3c9}' => 0x1D34B, |
| 233 | '\u{2202}' => 0x1B513, |
| 234 | '\u{3f5}' => 0x1D321, |
| 235 | '\u{3d1}' => 0x1D346, |
| 236 | '\u{3f0}' => 0x1D328, |
| 237 | '\u{3d5}' => 0x1D344, |
| 238 | '\u{3f1}' => 0x1D329, |
| 239 | '\u{3d6}' => 0x1D345, |
| 240 | _ => return c, |
| 241 | }; |
| 242 | shift(c, d) |
| 243 | } |
| 244 | |
| 245 | fn to_bold_italic(c: char) -> char { |
| 246 | let d = match c { |
| 247 | 'A'..='Z' => 0x1D427, |
| 248 | 'a'..='z' => 0x1D421, |
| 249 | '\u{391}'..='\u{3a1}' => 0x1D38B, |
| 250 | '\u{3f4}' => 0x1D339, |
| 251 | '\u{3a3}'..='\u{3a9}' => 0x1D38B, |
| 252 | '\u{2207}' => 0x1B52E, |
| 253 | '\u{3b1}'..='\u{3c9}' => 0x1D385, |
| 254 | '\u{2202}' => 0x1B54D, |
| 255 | '\u{3f5}' => 0x1D35B, |
| 256 | '\u{3d1}' => 0x1D380, |
| 257 | '\u{3f0}' => 0x1D362, |
| 258 | '\u{3d5}' => 0x1D37E, |
| 259 | '\u{3f1}' => 0x1D363, |
| 260 | '\u{3d6}' => 0x1D37F, |
| 261 | _ => return c, |
| 262 | }; |
| 263 | shift(c, d) |
| 264 | } |
| 265 | |
| 266 | fn to_script(c: char) -> char { |
| 267 | let d = match c { |
| 268 | 'g' => 0x20A3, |
| 269 | 'H' => 0x20C3, |
| 270 | 'I' => 0x20C7, |
| 271 | 'L' => 0x20C6, |
| 272 | 'R' => 0x20C9, |
| 273 | 'B' => 0x20EA, |
| 274 | 'e' => 0x20CA, |
| 275 | 'E'..='F' => 0x20EB, |
| 276 | 'M' => 0x20E6, |
| 277 | 'o' => 0x20C5, |
| 278 | 'A'..='Z' => 0x1D45B, |
| 279 | 'a'..='z' => 0x1D455, |
| 280 | _ => return c, |
| 281 | }; |
| 282 | shift(c, d) |
| 283 | } |
| 284 | |
| 285 | fn to_bold_script(c: char) -> char { |
| 286 | let d = match c { |
| 287 | 'A'..='Z' => 0x1D48F, |
| 288 | 'a'..='z' => 0x1D489, |
| 289 | _ => return c, |
| 290 | }; |
| 291 | shift(c, d) |
| 292 | } |
| 293 | |
| 294 | fn to_fraktur(c: char) -> char { |
| 295 | let d = match c { |
| 296 | 'H' => 0x20C4, |
| 297 | 'I' => 0x20C8, |
| 298 | 'R' => 0x20CA, |
| 299 | 'Z' => 0x20CE, |
| 300 | 'C' => 0x20EA, |
| 301 | 'A'..='Z' => 0x1D4C3, |
| 302 | 'a'..='z' => 0x1D4BD, |
| 303 | _ => return c, |
| 304 | }; |
| 305 | shift(c, d) |
| 306 | } |
| 307 | |
| 308 | fn to_bold_fraktur(c: char) -> char { |
| 309 | let d = match c { |
| 310 | 'A'..='Z' => 0x1D52B, |
| 311 | 'a'..='z' => 0x1D525, |
| 312 | _ => return c, |
| 313 | }; |
| 314 | shift(c, d) |
| 315 | } |
| 316 | |
| 317 | fn to_sans_serif(c: char) -> char { |
| 318 | let d = match c { |
| 319 | 'A'..='Z' => 0x1D55F, |
| 320 | 'a'..='z' => 0x1D559, |
| 321 | '0'..='9' => 0x1D7B2, |
| 322 | _ => return c, |
| 323 | }; |
| 324 | shift(c, d) |
| 325 | } |
| 326 | |
| 327 | fn to_sans_serif_bold(c: char) -> char { |
| 328 | let d = match c { |
| 329 | 'A'..='Z' => 0x1D593, |
| 330 | 'a'..='z' => 0x1D58D, |
| 331 | '\u{391}'..='\u{3a1}' => 0x1D3C5, |
| 332 | '\u{3f4}' => 0x1D373, |
| 333 | '\u{3a3}'..='\u{3a9}' => 0x1D3C5, |
| 334 | '\u{2207}' => 0x1B568, |
| 335 | '\u{3b1}'..='\u{3c9}' => 0x1D3BF, |
| 336 | '\u{2202}' => 0x1B587, |
| 337 | '\u{3f5}' => 0x1D395, |
| 338 | '\u{3d1}' => 0x1D3BA, |
| 339 | '\u{3f0}' => 0x1D39C, |
| 340 | '\u{3d5}' => 0x1D3B8, |
| 341 | '\u{3f1}' => 0x1D39D, |
| 342 | '\u{3d6}' => 0x1D3B9, |
| 343 | '0'..='9' => 0x1D7BC, |
| 344 | _ => return c, |
| 345 | }; |
| 346 | shift(c, d) |
| 347 | } |
| 348 | |
| 349 | fn to_sans_serif_italic(c: char) -> char { |
| 350 | let d = match c { |
| 351 | 'A'..='Z' => 0x1D5C7, |
| 352 | 'a'..='z' => 0x1D5C1, |
| 353 | _ => return c, |
| 354 | }; |
| 355 | shift(c, d) |
| 356 | } |
| 357 | |
| 358 | fn to_sans_serif_bold_italic(c: char) -> char { |
| 359 | let d = match c { |
| 360 | 'A'..='Z' => 0x1D5FB, |
| 361 | 'a'..='z' => 0x1D5F5, |
| 362 | '\u{391}'..='\u{3a1}' => 0x1D3FF, |
| 363 | '\u{3f4}' => 0x1D3AD, |
| 364 | '\u{3a3}'..='\u{3a9}' => 0x1D3FF, |
| 365 | '\u{2207}' => 0x1B5A2, |
| 366 | '\u{3b1}'..='\u{3c9}' => 0x1D3F9, |
| 367 | '\u{2202}' => 0x1B5C1, |
| 368 | '\u{3f5}' => 0x1D3CF, |
| 369 | '\u{3d1}' => 0x1D3F4, |
| 370 | '\u{3f0}' => 0x1D3D6, |
| 371 | '\u{3d5}' => 0x1D3F2, |
| 372 | '\u{3f1}' => 0x1D3D7, |
| 373 | '\u{3d6}' => 0x1D3F3, |
| 374 | _ => return c, |
| 375 | }; |
| 376 | shift(c, d) |
| 377 | } |
| 378 | |
| 379 | fn to_monospace(c: char) -> char { |
| 380 | let d = match c { |
| 381 | 'A'..='Z' => 0x1D62F, |
| 382 | 'a'..='z' => 0x1D629, |
| 383 | '0'..='9' => 0x1D7C6, |
| 384 | _ => return c, |
| 385 | }; |
| 386 | shift(c, d) |
| 387 | } |
| 388 | |
| 389 | fn to_double_struck(c: char) -> char { |
| 390 | let d = match c { |
| 391 | 'C' => 0x20BF, |
| 392 | 'H' => 0x20C5, |
| 393 | 'N' => 0x20C7, |
| 394 | 'P'..='Q' => 0x20C9, |
| 395 | 'R' => 0x20CB, |
| 396 | 'Z' => 0x20CA, |
| 397 | '\u{3c0}' => 0x1D7C, |
| 398 | '\u{3b3}' => 0x1D8A, |
| 399 | '\u{393}' => 0x1DAB, |
| 400 | '\u{3a0}' => 0x1D9F, |
| 401 | '\u{2211}' => return '\u{2140}', |
| 402 | 'A'..='Z' => 0x1D4F7, |
| 403 | 'a'..='z' => 0x1D4F1, |
| 404 | '0'..='9' => 0x1D7A8, |
| 405 | _ => return c, |
| 406 | }; |
| 407 | shift(c, d) |
| 408 | } |
| 409 | |
| 410 | fn to_double_struck_italic(c: char) -> char { |
| 411 | let d = match c { |
| 412 | 'D' => 0x2101, |
| 413 | 'd'..='e' => 0x20E2, |
| 414 | 'i'..='j' => 0x20DF, |
| 415 | _ => return c, |
| 416 | }; |
| 417 | shift(c, d) |
| 418 | } |
| 419 | |
| 420 | fn to_hebrew(c: char) -> char { |
| 421 | match c { |
| 422 | '\u{5d0}'..='\u{5d3}' => shift(c, 0x1B65), |
| 423 | _ => c, |
| 424 | } |
| 425 | } |