aboutsummaryrefslogtreecommitdiffstats
path: root/components/gfx/platform/windows/font_list.rs
blob: 5d1f076d6f99cae41ffb93df297803449f6f1395 (plain) (blame)
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use std::hash::Hash;
use std::sync::Arc;

use dwrote::{Font, FontCollection, FontDescriptor, FontStretch, FontStyle};
use malloc_size_of_derive::MallocSizeOf;
use serde::{Deserialize, Serialize};
use style::values::computed::{FontStyle as StyleFontStyle, FontWeight as StyleFontWeight};
use style::values::specified::font::FontStretchKeyword;
use ucd::{Codepoint, UnicodeBlock};

use crate::font_template::{FontTemplate, FontTemplateDescriptor};
use crate::text::util::unicode_plane;

pub static SANS_SERIF_FONT_FAMILY: &str = "Arial";

pub fn system_default_family(_: &str) -> Option<String> {
    Some("Verdana".to_owned())
}

pub fn for_each_available_family<F>(mut callback: F)
where
    F: FnMut(String),
{
    let system_fc = FontCollection::system();
    for family in system_fc.families_iter() {
        callback(family.name());
    }
}

/// An identifier for a local font on a Windows system.
#[derive(Clone, Debug, Deserialize, MallocSizeOf, PartialEq, Serialize)]
pub struct LocalFontIdentifier {
    /// The FontDescriptor of this font.
    #[ignore_malloc_size_of = "dwrote does not support MallocSizeOf"]
    pub font_descriptor: Arc<FontDescriptor>,
}

impl LocalFontIdentifier {
    pub fn index(&self) -> u32 {
        FontCollection::system()
            .get_font_from_descriptor(&self.font_descriptor)
            .map_or(0, |font| font.create_font_face().get_index())
    }

    pub(crate) fn read_data_from_file(&self) -> Vec<u8> {
        let font = FontCollection::system()
            .get_font_from_descriptor(&self.font_descriptor)
            .unwrap();
        let face = font.create_font_face();
        let files = face.get_files();
        assert!(!files.is_empty());
        files[0].get_font_file_bytes()
    }
}

impl Eq for LocalFontIdentifier {}

impl Hash for LocalFontIdentifier {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.font_descriptor.family_name.hash(state);
        self.font_descriptor.weight.to_u32().hash(state);
        self.font_descriptor.stretch.to_u32().hash(state);
        self.font_descriptor.style.to_u32().hash(state);
    }
}

pub fn for_each_variation<F>(family_name: &str, mut callback: F)
where
    F: FnMut(FontTemplate),
{
    let system_fc = FontCollection::system();
    if let Some(family) = system_fc.get_font_family_by_name(family_name) {
        let count = family.get_font_count();
        for i in 0..count {
            let font = family.get_font(i);
            let template_descriptor = (&font).into();
            let local_font_identifier = LocalFontIdentifier {
                font_descriptor: Arc::new(font.to_descriptor()),
            };
            callback(FontTemplate::new_for_local_font(
                local_font_identifier,
                template_descriptor,
            ))
        }
    }
}

// Based on gfxWindowsPlatform::GetCommonFallbackFonts() in Gecko
pub fn fallback_font_families(codepoint: Option<char>) -> Vec<&'static str> {
    let mut families = vec!["Arial"];

    if let Some(codepoint) = codepoint {
        match unicode_plane(codepoint) {
            // https://en.wikipedia.org/wiki/Plane_(Unicode)#Basic_Multilingual_Plane
            0 => {
                if let Some(block) = codepoint.block() {
                    match block {
                        UnicodeBlock::CyrillicSupplement |
                        UnicodeBlock::Armenian |
                        UnicodeBlock::Hebrew => {
                            families.push("Estrangelo Edessa");
                            families.push("Cambria");
                        },

                        UnicodeBlock::Arabic | UnicodeBlock::ArabicSupplement => {
                            families.push("Microsoft Uighur");
                        },

                        UnicodeBlock::Syriac => {
                            families.push("Estrangelo Edessa");
                        },

                        UnicodeBlock::Thaana => {
                            families.push("MV Boli");
                        },

                        UnicodeBlock::NKo => {
                            families.push("Ebrima");
                        },

                        UnicodeBlock::Devanagari | UnicodeBlock::Bengali => {
                            families.push("Nirmala UI");
                            families.push("Utsaah");
                            families.push("Aparajita");
                        },

                        UnicodeBlock::Gurmukhi |
                        UnicodeBlock::Gujarati |
                        UnicodeBlock::Oriya |
                        UnicodeBlock::Tamil |
                        UnicodeBlock::Telugu |
                        UnicodeBlock::Kannada |
                        UnicodeBlock::Malayalam |
                        UnicodeBlock::Sinhala |
                        UnicodeBlock::Lepcha |
                        UnicodeBlock::OlChiki |
                        UnicodeBlock::CyrillicExtendedC |
                        UnicodeBlock::SundaneseSupplement |
                        UnicodeBlock::VedicExtensions => {
                            families.push("Nirmala UI");
                        },

                        UnicodeBlock::Thai => {
                            families.push("Leelawadee UI");
                        },

                        UnicodeBlock::Lao => {
                            families.push("Lao UI");
                        },

                        UnicodeBlock::Myanmar |
                        UnicodeBlock::MyanmarExtendedA |
                        UnicodeBlock::MyanmarExtendedB => {
                            families.push("Myanmar Text");
                        },

                        UnicodeBlock::HangulJamo |
                        UnicodeBlock::HangulJamoExtendedA |
                        UnicodeBlock::HangulSyllables |
                        UnicodeBlock::HangulJamoExtendedB |
                        UnicodeBlock::HangulCompatibilityJamo => {
                            families.push("Malgun Gothic");
                        },

                        UnicodeBlock::Ethiopic |
                        UnicodeBlock::EthiopicSupplement |
                        UnicodeBlock::EthiopicExtended |
                        UnicodeBlock::EthiopicExtendedA => {
                            families.push("Nyala");
                        },

                        UnicodeBlock::Cherokee => {
                            families.push("Plantagenet Cherokee");
                        },

                        UnicodeBlock::UnifiedCanadianAboriginalSyllabics |
                        UnicodeBlock::UnifiedCanadianAboriginalSyllabicsExtended => {
                            families.push("Euphemia");
                            families.push("Segoe UI");
                        },

                        UnicodeBlock::Khmer | UnicodeBlock::KhmerSymbols => {
                            families.push("Khmer UI");
                            families.push("Leelawadee UI");
                        },

                        UnicodeBlock::Mongolian => {
                            families.push("Mongolian Baiti");
                        },

                        UnicodeBlock::TaiLe => {
                            families.push("Microsoft Tai Le");
                        },

                        UnicodeBlock::NewTaiLue => {
                            families.push("Microsoft New Tai Lue");
                        },

                        UnicodeBlock::Buginese |
                        UnicodeBlock::TaiTham |
                        UnicodeBlock::CombiningDiacriticalMarksExtended => {
                            families.push("Leelawadee UI");
                        },

                        UnicodeBlock::GeneralPunctuation |
                        UnicodeBlock::SuperscriptsandSubscripts |
                        UnicodeBlock::CurrencySymbols |
                        UnicodeBlock::CombiningDiacriticalMarksforSymbols |
                        UnicodeBlock::LetterlikeSymbols |
                        UnicodeBlock::NumberForms |
                        UnicodeBlock::Arrows |
                        UnicodeBlock::MathematicalOperators |
                        UnicodeBlock::MiscellaneousTechnical |
                        UnicodeBlock::ControlPictures |
                        UnicodeBlock::OpticalCharacterRecognition |
                        UnicodeBlock::EnclosedAlphanumerics |
                        UnicodeBlock::BoxDrawing |
                        UnicodeBlock::BlockElements |
                        UnicodeBlock::GeometricShapes |
                        UnicodeBlock::MiscellaneousSymbols |
                        UnicodeBlock::Dingbats |
                        UnicodeBlock::MiscellaneousMathematicalSymbolsA |
                        UnicodeBlock::SupplementalArrowsA |
                        UnicodeBlock::SupplementalArrowsB |
                        UnicodeBlock::MiscellaneousMathematicalSymbolsB |
                        UnicodeBlock::SupplementalMathematicalOperators |
                        UnicodeBlock::MiscellaneousSymbolsandArrows |
                        UnicodeBlock::Glagolitic |
                        UnicodeBlock::LatinExtendedC |
                        UnicodeBlock::Coptic => {
                            families.push("Segoe UI");
                            families.push("Segoe UI Symbol");
                            families.push("Cambria");
                            families.push("Meiryo");
                            families.push("Lucida Sans Unicode");
                            families.push("Ebrima");
                        },

                        UnicodeBlock::GeorgianSupplement |
                        UnicodeBlock::Tifinagh |
                        UnicodeBlock::CyrillicExtendedA |
                        UnicodeBlock::SupplementalPunctuation |
                        UnicodeBlock::CJKRadicalsSupplement |
                        UnicodeBlock::KangxiRadicals |
                        UnicodeBlock::IdeographicDescriptionCharacters => {
                            families.push("Segoe UI");
                            families.push("Segoe UI Symbol");
                            families.push("Meiryo");
                        },

                        UnicodeBlock::BraillePatterns => {
                            families.push("Segoe UI Symbol");
                        },

                        UnicodeBlock::CJKSymbolsandPunctuation |
                        UnicodeBlock::Hiragana |
                        UnicodeBlock::Katakana |
                        UnicodeBlock::Bopomofo |
                        UnicodeBlock::Kanbun |
                        UnicodeBlock::BopomofoExtended |
                        UnicodeBlock::CJKStrokes |
                        UnicodeBlock::KatakanaPhoneticExtensions |
                        UnicodeBlock::CJKUnifiedIdeographs => {
                            families.push("Microsoft YaHei");
                            families.push("Yu Gothic");
                        },

                        UnicodeBlock::EnclosedCJKLettersandMonths => {
                            families.push("Malgun Gothic");
                        },

                        UnicodeBlock::YijingHexagramSymbols => {
                            families.push("Segoe UI Symbol");
                        },

                        UnicodeBlock::YiSyllables | UnicodeBlock::YiRadicals => {
                            families.push("Microsoft Yi Baiti");
                            families.push("Segoe UI");
                        },

                        UnicodeBlock::Vai |
                        UnicodeBlock::CyrillicExtendedB |
                        UnicodeBlock::Bamum |
                        UnicodeBlock::ModifierToneLetters |
                        UnicodeBlock::LatinExtendedD => {
                            families.push("Ebrima");
                            families.push("Segoe UI");
                            families.push("Cambria Math");
                        },

                        UnicodeBlock::SylotiNagri |
                        UnicodeBlock::CommonIndicNumberForms |
                        UnicodeBlock::Phagspa |
                        UnicodeBlock::Saurashtra |
                        UnicodeBlock::DevanagariExtended => {
                            families.push("Microsoft PhagsPa");
                            families.push("Nirmala UI");
                        },

                        UnicodeBlock::KayahLi | UnicodeBlock::Rejang | UnicodeBlock::Javanese => {
                            families.push("Malgun Gothic");
                            families.push("Javanese Text");
                            families.push("Leelawadee UI");
                        },

                        UnicodeBlock::AlphabeticPresentationForms => {
                            families.push("Microsoft Uighur");
                            families.push("Gabriola");
                            families.push("Sylfaen");
                        },

                        UnicodeBlock::ArabicPresentationFormsA |
                        UnicodeBlock::ArabicPresentationFormsB => {
                            families.push("Traditional Arabic");
                            families.push("Arabic Typesetting");
                        },

                        UnicodeBlock::VariationSelectors |
                        UnicodeBlock::VerticalForms |
                        UnicodeBlock::CombiningHalfMarks |
                        UnicodeBlock::CJKCompatibilityForms |
                        UnicodeBlock::SmallFormVariants |
                        UnicodeBlock::HalfwidthandFullwidthForms |
                        UnicodeBlock::Specials => {
                            families.push("Microsoft JhengHei");
                        },

                        _ => {},
                    }
                }
            },

            // https://en.wikipedia.org/wiki/Plane_(Unicode)#Supplementary_Multilingual_Plane
            1 => {
                families.push("Segoe UI Symbol");
                families.push("Ebrima");
                families.push("Nirmala UI");
                families.push("Cambria Math");
            },

            _ => {},
        }
    }

    families.push("Arial Unicode MS");
    families
}

impl From<&Font> for FontTemplateDescriptor {
    fn from(font: &Font) -> Self {
        let style = match font.style() {
            FontStyle::Normal => StyleFontStyle::NORMAL,
            FontStyle::Oblique => StyleFontStyle::OBLIQUE,
            FontStyle::Italic => StyleFontStyle::ITALIC,
        };
        let weight = StyleFontWeight::from_float(font.weight().to_u32() as f32);
        let stretch = match font.stretch() {
            FontStretch::Undefined => FontStretchKeyword::Normal,
            FontStretch::UltraCondensed => FontStretchKeyword::UltraCondensed,
            FontStretch::ExtraCondensed => FontStretchKeyword::ExtraCondensed,
            FontStretch::Condensed => FontStretchKeyword::Condensed,
            FontStretch::SemiCondensed => FontStretchKeyword::SemiCondensed,
            FontStretch::Normal => FontStretchKeyword::Normal,
            FontStretch::SemiExpanded => FontStretchKeyword::SemiExpanded,
            FontStretch::Expanded => FontStretchKeyword::Expanded,
            FontStretch::ExtraExpanded => FontStretchKeyword::ExtraExpanded,
            FontStretch::UltraExpanded => FontStretchKeyword::UltraExpanded,
        }
        .compute();
        FontTemplateDescriptor::new(weight, stretch, style)
    }
}