tinymist_world/font/
info.rs1#[allow(dead_code)]
3pub fn typst_typographic_family(mut family: &str) -> &str {
4 const SEPARATORS: [char; 3] = [' ', '-', '_'];
6
7 const MODIFIERS: &[&str] = &[
9 "extra", "ext", "ex", "x", "semi", "sem", "sm", "demi", "dem", "ultra",
10 ];
11
12 #[rustfmt::skip]
14 const SUFFIXES: &[&str] = &[
15 "normal", "italic", "oblique", "slanted",
16 "thin", "th", "hairline", "light", "lt", "regular", "medium", "med",
17 "md", "bold", "bd", "demi", "extb", "black", "blk", "bk", "heavy",
18 "narrow", "condensed", "cond", "cn", "cd", "compressed", "expanded", "exp"
19 ];
20
21 let mut extra = [].as_slice();
22 let newcm = family.starts_with("NewCM") || family.starts_with("NewComputerModern");
23 if newcm {
24 extra = &["book"];
25 }
26
27 family = family.trim().trim_start_matches('.');
29
30 let lower = family.to_ascii_lowercase();
32 let mut len = usize::MAX;
33 let mut trimmed = lower.as_str();
34
35 while trimmed.len() < len {
37 len = trimmed.len();
38
39 let mut t = trimmed;
41 let mut shortened = false;
42 while let Some(s) = SUFFIXES.iter().chain(extra).find_map(|s| t.strip_suffix(s)) {
43 shortened = true;
44 t = s;
45 }
46
47 if !shortened {
48 break;
49 }
50
51 if let Some(s) = t.strip_suffix(SEPARATORS) {
53 trimmed = s;
54 t = s;
55 }
56
57 if let Some(t) = MODIFIERS.iter().find_map(|s| t.strip_suffix(s))
60 && let Some(stripped) = t.strip_suffix(SEPARATORS)
61 {
62 trimmed = stripped;
63 }
64 }
65
66 family = &family[..len];
68
69 if newcm {
70 family = family.trim_end_matches("10");
71 }
72
73 match family {
75 "Noto Sans Symbols2" => "Noto Sans Symbols 2",
76 "NewComputerModern" => "New Computer Modern",
77 "NewComputerModernMono" => "New Computer Modern Mono",
78 "NewComputerModernSans" => "New Computer Modern Sans",
79 "NewComputerModernMath" => "New Computer Modern Math",
80 "NewCMUncial" | "NewComputerModernUncial" => "New Computer Modern Uncial",
81 other => other,
82 }
83}