1pub const FONT_GLYPH_WIDTH: usize = 8;
4pub const FONT_GLYPH_HEIGHT: usize = 16;
5
6pub const FONT_8X16: [[u8; 16]; 95] = [
8 [
10 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
11 0x00,
12 ],
13 [
15 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x40, 0x40, 0x40, 0x40, 0x40, 0x00, 0x40, 0x40, 0x00,
16 0x00,
17 ],
18 [
20 0x00, 0x00, 0x00, 0x00, 0x00, 0x58, 0x50, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
21 0x00,
22 ],
23 [
25 0x00, 0x00, 0x00, 0x00, 0x00, 0x12, 0x12, 0x7f, 0x24, 0x24, 0xff, 0x24, 0x2c, 0x68, 0x00,
26 0x00,
27 ],
28 [
30 0x00, 0x00, 0x00, 0x00, 0x10, 0x3c, 0x50, 0x50, 0x70, 0x1c, 0x16, 0x16, 0x7c, 0x10, 0x00,
31 0x00,
32 ],
33 [
35 0x00, 0x00, 0x00, 0x00, 0x00, 0x71, 0x51, 0x5a, 0x5f, 0x57, 0x7a, 0x0a, 0x13, 0x11, 0x00,
36 0x00,
37 ],
38 [
40 0x00, 0x00, 0x00, 0x00, 0x00, 0x3c, 0x24, 0x24, 0x38, 0x78, 0x4d, 0x47, 0x47, 0x3d, 0x00,
41 0x00,
42 ],
43 [
45 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x40, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
46 0x00,
47 ],
48 [
50 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x60, 0x40, 0x40, 0xc0, 0xc0, 0xc0, 0x40, 0x40, 0x60,
51 0x20,
52 ],
53 [
55 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x60, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x60,
56 0x40,
57 ],
58 [
60 0x00, 0x00, 0x00, 0x00, 0x10, 0x10, 0x7e, 0x38, 0x28, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00,
61 0x00,
62 ],
63 [
65 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x10, 0x7e, 0x10, 0x10, 0x10, 0x00, 0x00,
66 0x00,
67 ],
68 [
70 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x40,
71 0x40,
72 ],
73 [
75 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x00, 0x00, 0x00, 0x00,
76 0x00,
77 ],
78 [
80 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x40, 0x00,
81 0x00,
82 ],
83 [
85 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x10, 0x10, 0x20, 0x20, 0x60, 0x40, 0x40, 0xc0, 0x00,
86 0x00,
87 ],
88 [
90 0x00, 0x00, 0x00, 0x00, 0x00, 0x38, 0x64, 0x46, 0x46, 0x42, 0x46, 0x46, 0x64, 0x38, 0x00,
91 0x00,
92 ],
93 [
95 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x38, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x00,
96 0x00,
97 ],
98 [
100 0x00, 0x00, 0x00, 0x00, 0x00, 0x38, 0x44, 0x04, 0x04, 0x0c, 0x18, 0x30, 0x60, 0x7e, 0x00,
101 0x00,
102 ],
103 [
105 0x00, 0x00, 0x00, 0x00, 0x00, 0x7c, 0x44, 0x04, 0x0c, 0x38, 0x04, 0x06, 0x06, 0x7c, 0x00,
106 0x00,
107 ],
108 [
110 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x1c, 0x14, 0x24, 0x64, 0x44, 0xfe, 0x04, 0x04, 0x00,
111 0x00,
112 ],
113 [
115 0x00, 0x00, 0x00, 0x00, 0x00, 0x7c, 0x40, 0x40, 0x7c, 0x04, 0x06, 0x06, 0x04, 0x78, 0x00,
116 0x00,
117 ],
118 [
120 0x00, 0x00, 0x00, 0x00, 0x00, 0x1c, 0x20, 0x40, 0x5c, 0x66, 0x42, 0x46, 0x66, 0x3c, 0x00,
121 0x00,
122 ],
123 [
125 0x00, 0x00, 0x00, 0x00, 0x00, 0xfe, 0x06, 0x04, 0x0c, 0x08, 0x18, 0x10, 0x30, 0x20, 0x00,
126 0x00,
127 ],
128 [
130 0x00, 0x00, 0x00, 0x00, 0x00, 0x3c, 0x64, 0x44, 0x6c, 0x38, 0x44, 0x42, 0x46, 0x3c, 0x00,
131 0x00,
132 ],
133 [
135 0x00, 0x00, 0x00, 0x00, 0x00, 0x38, 0x64, 0x46, 0x46, 0x46, 0x3e, 0x04, 0x0c, 0x78, 0x00,
136 0x00,
137 ],
138 [
140 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x40, 0x00, 0x00, 0x00, 0x40, 0x40, 0x00,
141 0x00,
142 ],
143 [
145 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x40, 0x00, 0x00, 0x00, 0x00, 0x40, 0x40,
146 0xc0,
147 ],
148 [
150 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x0c, 0x30, 0x60, 0x38, 0x0e, 0x00, 0x00, 0x00,
151 0x00,
152 ],
153 [
155 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7e, 0x00, 0x00, 0x7e, 0x00, 0x00, 0x00, 0x00,
156 0x00,
157 ],
158 [
160 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x70, 0x1c, 0x06, 0x18, 0x60, 0x00, 0x00, 0x00,
161 0x00,
162 ],
163 [
165 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8, 0x08, 0x08, 0x18, 0x30, 0x20, 0x00, 0x20, 0x20, 0x00,
166 0x00,
167 ],
168 [
170 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x30, 0x6f, 0x49, 0x59, 0x59, 0x5b, 0x4e, 0x30, 0x1f,
171 0x00,
172 ],
173 [
175 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x18, 0x1c, 0x24, 0x26, 0x7e, 0x42, 0xc3, 0x81, 0x00,
176 0x00,
177 ],
178 [
180 0x00, 0x00, 0x00, 0x00, 0x00, 0x7e, 0x62, 0x62, 0x62, 0x7c, 0x63, 0x63, 0x63, 0x7e, 0x00,
181 0x00,
182 ],
183 [
185 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0x30, 0x60, 0x40, 0x40, 0x40, 0x60, 0x30, 0x1e, 0x00,
186 0x00,
187 ],
188 [
190 0x00, 0x00, 0x00, 0x00, 0x00, 0x7e, 0x63, 0x61, 0x61, 0x61, 0x61, 0x61, 0x63, 0x7e, 0x00,
191 0x00,
192 ],
193 [
195 0x00, 0x00, 0x00, 0x00, 0x00, 0x7e, 0x60, 0x60, 0x60, 0x7c, 0x60, 0x60, 0x60, 0x7e, 0x00,
196 0x00,
197 ],
198 [
200 0x00, 0x00, 0x00, 0x00, 0x00, 0x7e, 0x60, 0x60, 0x60, 0x7c, 0x60, 0x60, 0x60, 0x60, 0x00,
201 0x00,
202 ],
203 [
205 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0x30, 0x60, 0x40, 0x47, 0x41, 0x61, 0x31, 0x1f, 0x00,
206 0x00,
207 ],
208 [
210 0x00, 0x00, 0x00, 0x00, 0x00, 0x61, 0x61, 0x61, 0x61, 0x7f, 0x61, 0x61, 0x61, 0x61, 0x00,
211 0x00,
212 ],
213 [
215 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x70, 0x00,
216 0x00,
217 ],
218 [
220 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,
221 0x40,
222 ],
223 [
225 0x00, 0x00, 0x00, 0x00, 0x00, 0x63, 0x66, 0x6c, 0x78, 0x78, 0x68, 0x64, 0x66, 0x63, 0x00,
226 0x00,
227 ],
228 [
230 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x7e, 0x00,
231 0x00,
232 ],
233 [
235 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x70, 0x70, 0x51, 0x59, 0x49, 0x4e, 0x46, 0x46, 0x00,
236 0x00,
237 ],
238 [
240 0x00, 0x00, 0x00, 0x00, 0x00, 0x61, 0x71, 0x71, 0x59, 0x4d, 0x45, 0x47, 0x43, 0x41, 0x00,
241 0x00,
242 ],
243 [
245 0x00, 0x00, 0x00, 0x00, 0x00, 0x1e, 0x21, 0x60, 0x40, 0x40, 0x40, 0x60, 0x21, 0x1e, 0x00,
246 0x00,
247 ],
248 [
250 0x00, 0x00, 0x00, 0x00, 0x00, 0x7c, 0x66, 0x62, 0x66, 0x7c, 0x60, 0x60, 0x60, 0x60, 0x00,
251 0x00,
252 ],
253 [
255 0x00, 0x00, 0x00, 0x00, 0x00, 0x1e, 0x21, 0x60, 0x40, 0x40, 0x40, 0x60, 0x21, 0x1e, 0x02,
256 0x01,
257 ],
258 [
260 0x00, 0x00, 0x00, 0x00, 0x00, 0x7c, 0x66, 0x62, 0x66, 0x7c, 0x6c, 0x66, 0x62, 0x63, 0x00,
261 0x00,
262 ],
263 [
265 0x00, 0x00, 0x00, 0x00, 0x00, 0x3c, 0x40, 0x40, 0x60, 0x38, 0x0c, 0x06, 0x04, 0x78, 0x00,
266 0x00,
267 ],
268 [
270 0x00, 0x00, 0x00, 0x00, 0x00, 0xfe, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00,
271 0x00,
272 ],
273 [
275 0x00, 0x00, 0x00, 0x00, 0x00, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x61, 0x23, 0x1e, 0x00,
276 0x00,
277 ],
278 [
280 0x00, 0x00, 0x00, 0x00, 0x00, 0xc3, 0xc2, 0x42, 0x66, 0x24, 0x2c, 0x3c, 0x18, 0x18, 0x00,
281 0x00,
282 ],
283 [
285 0x00, 0x00, 0x00, 0x00, 0x00, 0xc6, 0xc6, 0x46, 0x4f, 0x69, 0x29, 0x39, 0x30, 0x30, 0x00,
286 0x00,
287 ],
288 [
290 0x00, 0x00, 0x00, 0x00, 0x00, 0xc2, 0x64, 0x2c, 0x38, 0x18, 0x38, 0x24, 0x46, 0xc2, 0x00,
291 0x00,
292 ],
293 [
295 0x00, 0x00, 0x00, 0x00, 0x00, 0xc2, 0x46, 0x64, 0x2c, 0x38, 0x10, 0x10, 0x10, 0x10, 0x00,
296 0x00,
297 ],
298 [
300 0x00, 0x00, 0x00, 0x00, 0x00, 0x7e, 0x04, 0x0c, 0x08, 0x10, 0x30, 0x20, 0x40, 0xfe, 0x00,
301 0x00,
302 ],
303 [
305 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,
306 0x70,
307 ],
308 [
310 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x40, 0x40, 0x60, 0x20, 0x20, 0x10, 0x10, 0x18, 0x00,
311 0x00,
312 ],
313 [
315 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
316 0xe0,
317 ],
318 [
320 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x38, 0x28, 0x24, 0x44, 0x42, 0x00, 0x00, 0x00, 0x00,
321 0x00,
322 ],
323 [
325 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
326 0xfc,
327 ],
328 [
330 0x00, 0x00, 0x00, 0x00, 0x40, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
331 0x00,
332 ],
333 [
335 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3c, 0x04, 0x04, 0x7c, 0x44, 0x4c, 0x7c, 0x00,
336 0x00,
337 ],
338 [
340 0x00, 0x00, 0x00, 0x00, 0x40, 0x40, 0x40, 0x7c, 0x66, 0x42, 0x42, 0x42, 0x66, 0x5c, 0x00,
341 0x00,
342 ],
343 [
345 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3c, 0x60, 0x40, 0x40, 0x40, 0x60, 0x3c, 0x00,
346 0x00,
347 ],
348 [
350 0x00, 0x00, 0x00, 0x00, 0x02, 0x02, 0x02, 0x3e, 0x66, 0x42, 0x42, 0x42, 0x66, 0x3a, 0x00,
351 0x00,
352 ],
353 [
355 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3c, 0x64, 0x46, 0x7e, 0x40, 0x60, 0x3c, 0x00,
356 0x00,
357 ],
358 [
360 0x00, 0x00, 0x00, 0x00, 0x38, 0x60, 0x60, 0xf0, 0x60, 0x60, 0x60, 0x60, 0x60, 0x60, 0x00,
361 0x00,
362 ],
363 [
365 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3a, 0x66, 0x42, 0x42, 0x42, 0x66, 0x3e, 0x02,
366 0x06,
367 ],
368 [
370 0x00, 0x00, 0x00, 0x00, 0x40, 0x40, 0x40, 0x7c, 0x66, 0x42, 0x42, 0x42, 0x42, 0x42, 0x00,
371 0x00,
372 ],
373 [
375 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x00,
376 0x00,
377 ],
378 [
380 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,
381 0x40,
382 ],
383 [
385 0x00, 0x00, 0x00, 0x00, 0x40, 0x40, 0x40, 0x44, 0x48, 0x50, 0x70, 0x58, 0x4c, 0x46, 0x00,
386 0x00,
387 ],
388 [
390 0x00, 0x00, 0x00, 0x00, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x00,
391 0x00,
392 ],
393 [
395 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x5d, 0x66, 0x46, 0x46, 0x46, 0x46, 0x46, 0x00,
396 0x00,
397 ],
398 [
400 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x5c, 0x66, 0x42, 0x42, 0x42, 0x42, 0x42, 0x00,
401 0x00,
402 ],
403 [
405 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3c, 0x66, 0x42, 0x42, 0x42, 0x66, 0x3c, 0x00,
406 0x00,
407 ],
408 [
410 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x5c, 0x66, 0x42, 0x42, 0x42, 0x66, 0x7c, 0x40,
411 0x40,
412 ],
413 [
415 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3a, 0x66, 0x42, 0x42, 0x42, 0x66, 0x3e, 0x02,
416 0x02,
417 ],
418 [
420 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x58, 0x60, 0x40, 0x40, 0x40, 0x40, 0x40, 0x00,
421 0x00,
422 ],
423 [
425 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x78, 0x40, 0x60, 0x38, 0x0c, 0x0c, 0x78, 0x00,
426 0x00,
427 ],
428 [
430 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0xf0, 0x40, 0x40, 0x40, 0x40, 0x60, 0x30, 0x00,
431 0x00,
432 ],
433 [
435 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x42, 0x42, 0x42, 0x42, 0x42, 0x66, 0x3a, 0x00,
436 0x00,
437 ],
438 [
440 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc6, 0xc4, 0x4c, 0x68, 0x28, 0x38, 0x30, 0x00,
441 0x00,
442 ],
443 [
445 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xcc, 0xcc, 0x4e, 0x5a, 0x72, 0x33, 0x31, 0x00,
446 0x00,
447 ],
448 [
450 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x44, 0x6c, 0x38, 0x30, 0x38, 0x6c, 0xc4, 0x00,
451 0x00,
452 ],
453 [
455 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc6, 0x44, 0x4c, 0x68, 0x28, 0x38, 0x10, 0x30,
456 0x20,
457 ],
458 [
460 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7c, 0x08, 0x10, 0x30, 0x20, 0x40, 0xfc, 0x00,
461 0x00,
462 ],
463 [
465 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x20, 0x20, 0x20, 0x20, 0xc0, 0x20, 0x20, 0x20, 0x20,
466 0x18,
467 ],
468 [
470 0x00, 0x00, 0x00, 0x00, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10,
471 0x10,
472 ],
473 [
475 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x20, 0x20, 0x20, 0x20, 0x18, 0x20, 0x20, 0x20, 0x60,
476 0xc0,
477 ],
478 [
480 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x72, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x00,
481 0x00,
482 ],
483];
484
485#[inline]
487pub fn get_glyph(c: char) -> Option<&'static [u8; 16]> {
488 let code = c as u32;
489 if (32..=126).contains(&code) {
490 Some(&FONT_8X16[(code - 32) as usize])
491 } else {
492 None
493 }
494}
495
496pub const TOFU_GLYPH: [u8; 16] = [
498 0x00, 0x00, 0x7e, 0x42, 0x42, 0x5a, 0x5a, 0x42, 0x5a, 0x5a, 0x42, 0x42, 0x7e, 0x00, 0x00, 0x00,
499];
500
501pub fn map_common_unicode(c: char) -> Option<&'static [u8; 16]> {
503 match c {
504 '\u{00A0}' => get_glyph(' '), '\u{2018}' | '\u{2019}' | '\u{201A}' | '\u{201B}' => get_glyph('\''), '\u{201C}' | '\u{201D}' | '\u{201E}' | '\u{201F}' => get_glyph('"'), '\u{2013}' | '\u{2014}' | '\u{2212}' => get_glyph('-'), '\u{2026}' => get_glyph('.'), '\u{2022}' => get_glyph('*'), '\u{00B7}' => get_glyph('.'), '\u{00AB}' => get_glyph('<'), '\u{00BB}' => get_glyph('>'), '\u{00D7}' => get_glyph('x'), '\u{00F7}' => get_glyph('/'), '\u{00B1}' => get_glyph('+'), '\u{00B0}' => get_glyph('o'), _ => None,
518 }
519}
520
521#[inline]
523pub fn get_glyph_or_fallback(c: char) -> &'static [u8; 16] {
524 if let Some(glyph) = get_glyph(c) {
525 glyph
526 } else if let Some(mapped) = map_common_unicode(c) {
527 mapped
528 } else {
529 &TOFU_GLYPH
530 }
531}
532
533#[derive(Debug, Clone, Copy, PartialEq, Eq)]
535pub enum OsPlatform {
536 Linux,
537 MacOS,
538 Windows,
539 Other,
540}
541
542impl OsPlatform {
543 pub const fn current() -> Self {
544 if cfg!(target_os = "macos") {
545 Self::MacOS
546 } else if cfg!(target_os = "windows") {
547 Self::Windows
548 } else if cfg!(target_os = "linux") {
549 Self::Linux
550 } else {
551 Self::Other
552 }
553 }
554}
555
556#[derive(Debug, Clone, Copy, PartialEq, Eq)]
558pub enum GenericFontFamily {
559 SansSerif,
560 Serif,
561 Monospace,
562 Cursive,
563 Fantasy,
564 SystemUi,
565}
566
567#[derive(Debug, Clone)]
569pub struct SystemFontResolver;
570
571impl SystemFontResolver {
572 pub fn default_chain_for_generic(family: GenericFontFamily) -> Vec<String> {
574 Self::chain_for_platform(family, OsPlatform::current())
575 }
576
577 pub fn chain_for_platform(family: GenericFontFamily, platform: OsPlatform) -> Vec<String> {
579 match platform {
580 OsPlatform::MacOS => match family {
581 GenericFontFamily::SansSerif | GenericFontFamily::SystemUi => vec![
582 "-apple-system".into(),
583 "BlinkMacSystemFont".into(),
584 "SF Pro Text".into(),
585 "Helvetica Neue".into(),
586 "Helvetica".into(),
587 "Arial".into(),
588 "PingFang SC".into(),
589 "Hiragino Sans".into(),
590 "Apple Color Emoji".into(),
591 "sans-serif".into(),
592 ],
593 GenericFontFamily::Serif => vec![
594 "New York".into(),
595 "Times New Roman".into(),
596 "Times".into(),
597 "Songti SC".into(),
598 "serif".into(),
599 ],
600 GenericFontFamily::Monospace => vec![
601 "SF Mono".into(),
602 "Menlo".into(),
603 "Monaco".into(),
604 "Courier New".into(),
605 "monospace".into(),
606 ],
607 _ => vec!["-apple-system".into(), "sans-serif".into()],
608 },
609 OsPlatform::Windows => match family {
610 GenericFontFamily::SansSerif | GenericFontFamily::SystemUi => vec![
611 "Segoe UI".into(),
612 "Calibri".into(),
613 "Arial".into(),
614 "Microsoft YaHei".into(),
615 "Meiryo".into(),
616 "Segoe UI Emoji".into(),
617 "Segoe UI Symbol".into(),
618 "sans-serif".into(),
619 ],
620 GenericFontFamily::Serif => vec![
621 "Georgia".into(),
622 "Times New Roman".into(),
623 "Cambria".into(),
624 "SimSun".into(),
625 "serif".into(),
626 ],
627 GenericFontFamily::Monospace => vec![
628 "Consolas".into(),
629 "Cascadia Code".into(),
630 "Courier New".into(),
631 "monospace".into(),
632 ],
633 _ => vec!["Segoe UI".into(), "sans-serif".into()],
634 },
635 OsPlatform::Linux | OsPlatform::Other => match family {
636 GenericFontFamily::SansSerif | GenericFontFamily::SystemUi => vec![
637 "Ubuntu".into(),
638 "DejaVu Sans".into(),
639 "Liberation Sans".into(),
640 "Noto Sans".into(),
641 "Noto Sans CJK SC".into(),
642 "Noto Color Emoji".into(),
643 "sans-serif".into(),
644 ],
645 GenericFontFamily::Serif => vec![
646 "DejaVu Serif".into(),
647 "Liberation Serif".into(),
648 "Noto Serif".into(),
649 "serif".into(),
650 ],
651 GenericFontFamily::Monospace => vec![
652 "Ubuntu Mono".into(),
653 "DejaVu Sans Mono".into(),
654 "Liberation Mono".into(),
655 "Noto Sans Mono".into(),
656 "monospace".into(),
657 ],
658 _ => vec!["Ubuntu".into(), "sans-serif".into()],
659 },
660 }
661 }
662
663 pub fn parse_font_family_cascade(css_value: &str) -> Vec<String> {
665 let mut results = Vec::new();
666 for item in css_value.split(',') {
667 let trimmed = item.trim().trim_matches(|c| c == '\'' || c == '"');
668 if trimmed.is_empty() {
669 continue;
670 }
671 match trimmed.to_ascii_lowercase().as_str() {
672 "sans-serif" => {
673 for f in Self::default_chain_for_generic(GenericFontFamily::SansSerif) {
674 if !results.contains(&f) {
675 results.push(f);
676 }
677 }
678 }
679 "serif" => {
680 for f in Self::default_chain_for_generic(GenericFontFamily::Serif) {
681 if !results.contains(&f) {
682 results.push(f);
683 }
684 }
685 }
686 "monospace" => {
687 for f in Self::default_chain_for_generic(GenericFontFamily::Monospace) {
688 if !results.contains(&f) {
689 results.push(f);
690 }
691 }
692 }
693 "system-ui" | "-apple-system" | "blinkmacsystemfont" => {
694 let token = trimmed.to_string();
695 if !results.contains(&token) {
696 results.push(token);
697 }
698 for f in Self::default_chain_for_generic(GenericFontFamily::SystemUi) {
699 if !results.contains(&f) {
700 results.push(f);
701 }
702 }
703 }
704 _ => {
705 let owned = trimmed.to_string();
706 if !results.contains(&owned) {
707 results.push(owned);
708 }
709 }
710 }
711 }
712
713 if results.is_empty() {
714 results = Self::default_chain_for_generic(GenericFontFamily::SansSerif);
715 }
716 results
717 }
718}
719
720#[derive(Debug, Clone)]
722pub struct FontFallbackChain {
723 pub families: Vec<String>,
724}
725
726impl FontFallbackChain {
727 pub fn new(families: Vec<String>) -> Self {
728 Self { families }
729 }
730
731 pub fn from_css(css_value: &str) -> Self {
732 Self {
733 families: SystemFontResolver::parse_font_family_cascade(css_value),
734 }
735 }
736
737 pub fn primary_font(&self) -> Option<&str> {
738 self.families.first().map(|s| s.as_str())
739 }
740
741 pub fn can_render(&self, c: char) -> bool {
743 get_glyph(c).is_some() || map_common_unicode(c).is_some()
744 }
745}