1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
use std::{fmt::Display, str::FromStr};
/// Describes a generic font family.
#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]
pub enum Generic {
/// Glyphs have finishing strokes, flared or tapering ends, or have actual
/// serifed endings.
Serif = 0,
/// Glyphs have stroke endings that are plain.
SansSerif = 1,
/// All glyphs have the same fixed width.
Monospace = 2,
/// Glyphs in cursive fonts generally have either joining strokes or other
/// cursive characteristics beyond those of italic typefaces. The glyphs
/// are partially or completely connected, and the result looks more like
/// handwritten pen or brush writing than printed letter work.
Cursive = 3,
/// Fantasy fonts are primarily decorative fonts that contain playful
/// representations of characters
Fantasy = 4,
/// Glyphs are taken from the default user interface font on a given
/// platform.
SystemUi = 5,
/// The default user interface serif font.
UiSerif = 6,
/// The default user interface sans-serif font.
UiSansSerif = 7,
/// The default user interface monospace font.
UiMonospace = 8,
/// The default user interface font that has rounded features.
UiRounded = 9,
/// Fonts that are specifically designed to render emoji.
Emoji = 10,
/// This is for the particular stylistic concerns of representing
/// mathematics: superscript and subscript, brackets that cross several
/// lines, nesting expressions, and double struck glyphs with distinct
/// meanings.
Math = 11,
/// A particular style of Chinese characters that are between serif-style
/// Song and cursive-style Kai forms. This style is often used for
/// government documents.
FangSong = 12,
}
impl Default for Generic {
fn default() -> Self {
Self::SystemUi
}
}
// Generic Family Map
impl FromStr for Generic {
type Err = &'static str;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(match s.trim() {
"serif" => Self::Serif,
"sans-serif" => Self::SansSerif,
"monospace" => Self::Monospace,
"cursive" => Self::Cursive,
"fantasy" => Self::Fantasy,
"system-ui" | "system" | "" => Self::SystemUi,
"ui-serif" => Self::UiSerif,
"ui-sans-serif" => Self::UiSansSerif,
"ui-monospace" => Self::UiMonospace,
"ui-rounded" => Self::UiRounded,
"emoji" => Self::Emoji,
"math" => Self::Math,
"fangsong" => Self::FangSong,
_ => return Err("fail"),
})
}
}
impl Display for Generic {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
let name = match self {
Self::Serif => "serif",
Self::SansSerif => "sans-serif",
Self::Monospace => "monospace",
Self::Cursive => "cursive",
Self::Fantasy => "fantasy",
Self::SystemUi => "system-ui",
Self::UiSerif => "ui-serif",
Self::UiSansSerif => "ui-sans-serif",
Self::UiMonospace => "ui-monospace",
Self::UiRounded => "ui-rounded",
Self::Emoji => "emoji",
Self::Math => "math",
Self::FangSong => "fangsong",
};
write!(f, "{}", name)
}
}
impl Generic {
#[cfg(target_os = "android")]
pub fn system(&self) -> &'static str {
// fonts.xml have alias
match *self {
Self::Serif => "serif",
Self::SansSerif => "sans-serif",
Self::Monospace => "monospace",
Self::Cursive => "cursive",
Self::Fantasy => "cursive", // no fantasy in fonts.xml
Self::SystemUi => "sans-serif",
Self::Emoji => "sans-serif",
_ => "sans-serif",
}
}
#[cfg(target_os = "linux")]
pub fn system(&self) -> &'static str {
// fonts.conf have alias
match *self {
Self::Serif => "serif",
Self::SansSerif => "sans-serif",
Self::Monospace => "monospace",
Self::Cursive => "cursive",
Self::Fantasy => "fantasy",
Self::SystemUi => "system-ui",
Self::UiSerif => "serif",
Self::UiSansSerif => "sans-serif",
Self::UiMonospace => "monospace",
Self::Emoji => "emoji",
Self::Math => "math",
_ => "sans-serif",
}
}
#[cfg(any(target_os = "ios", target_os = "macos"))]
pub fn system(&self) -> &'static str {
// from fontique
// const DEFAULT_GENERIC_FAMILIES: &[(GenericFamily, &[&str])] = &[
// (GenericFamily::Serif, &["Times", "Times New Roman"]),
// (GenericFamily::SansSerif, &["Helvetica"]),
// (GenericFamily::Monospace, &["Courier", "Courier New"]),
// (GenericFamily::Cursive, &["Apple Chancery"]),
// (GenericFamily::Fantasy, &["Papyrus"]),
// (GenericFamily::SystemUi, &["System Font", ".SF NS"]),
// (GenericFamily::Emoji, &["Apple Color Emoji"]),
// (GenericFamily::Math, &["STIX Two Math"]),
// ];
// from godot
// String OS_IOS::_get_default_fontname(const String &p_font_name) const {
// String font_name = p_font_name;
// if (font_name.to_lower() == "sans-serif") {
// font_name = "Helvetica";
// } else if (font_name.to_lower() == "serif") {
// font_name = "Times";
// } else if (font_name.to_lower() == "monospace") {
// font_name = "Courier";
// } else if (font_name.to_lower() == "fantasy") {
// font_name = "Papyrus";
// } else if (font_name.to_lower() == "cursive") {
// font_name = "Apple Chancery";
// };
// return font_name;
// }
match *self {
Generic::Serif => "Times New Roman",
Generic::SansSerif => "Helvetica",
Generic::Monospace => "Courier New",
Generic::Cursive => "Apple Chancery",
Generic::Fantasy => "Papyrus",
Generic::SystemUi => "System Font",
Generic::Emoji => "Apple Color Emoji",
Generic::Math => "STIX Two Math",
_ => "System Font",
}
}
#[cfg(target_os = "windows")]
pub fn system(&self) -> &'static str {
// https://learn.microsoft.com/en-us/windows/win32/uxguide/vis-fonts
// const DEFAULT_GENERIC_FAMILIES: &[(GenericFamily, &[&str])] = &[
// (GenericFamily::Serif, &["Times New Roman"]),
// (GenericFamily::SansSerif, &["Arial"]),
// (GenericFamily::Monospace, &["Consolas"]),
// (GenericFamily::Cursive, &["Comic Sans MS"]),
// (GenericFamily::Fantasy, &["Impact"]),
// (GenericFamily::SystemUi, &["Segoe UI"]),
// (GenericFamily::Emoji, &["Segoe UI Emoji"]),
// (GenericFamily::Math, &["Cambria Math"]),
// (GenericFamily::FangSong, &["FangSong"]),
// ];
// String OS_Windows::_get_default_fontname(const String &p_font_name) const {
// String font_name = p_font_name;
// if (font_name.to_lower() == "sans-serif") {
// font_name = "Arial";
// } else if (font_name.to_lower() == "serif") {
// font_name = "Times New Roman";
// } else if (font_name.to_lower() == "monospace") {
// font_name = "Courier New";
// } else if (font_name.to_lower() == "cursive") {
// font_name = "Comic Sans MS";
// } else if (font_name.to_lower() == "fantasy") {
// font_name = "Gabriola";
// }
// return font_name;
// }
match *self {
Generic::Serif => "Times New Roman",
Generic::SansSerif => "Arial",
Generic::Monospace => "Courier New",
Generic::Cursive => "Comic Sans MS",
Generic::Fantasy => "Gabriola",
Generic::SystemUi => "Segoe UI",
Generic::Emoji => "Segoe UI Emoji",
Generic::Math => "Cambria Math",
Generic::FangSong => "FangSong",
_ => "Segoe UI",
}
}
#[cfg(target_arch = "wasm32")]
pub fn system(&self) -> &'static str {
match *self {
Self::Serif => "serif",
Self::SansSerif => "sans-serif",
Self::Monospace => "monospace",
Self::Cursive => "cursive",
Self::Fantasy => "fantasy",
Self::SystemUi => "system-ui",
Self::UiSerif => "ui-serif",
Self::UiSansSerif => "ui-sans-serif",
Self::UiMonospace => "ui-monospace",
Self::UiRounded => "ui-rounded",
Self::Emoji => "emoji",
Self::Math => "math",
Self::FangSong => "fangsong",
}
}
}