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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
#[cfg(not(any(target_arch = "wasm32", target_os = "android")))]
pub use desktop::*;

#[cfg(target_arch = "wasm32")]
pub use wasm::*;

#[cfg(target_os = "android")]
pub use android::*;

#[cfg(not(any(target_arch = "wasm32", target_os = "android")))]
mod desktop {
    use std::{borrow::Cow, path::Path, sync::Arc};

    use zng_var::ResponseVar;

    use crate::{FontDataRef, FontLoadingError, FontName, FontStretch, FontStyle, FontWeight, GlyphLoadingError};

    pub fn system_all() -> ResponseVar<Vec<FontName>> {
        zng_task::wait_respond(|| {
            font_kit::source::SystemSource::new()
                .all_families()
                .unwrap_or_default()
                .into_iter()
                .map(FontName::from)
                .collect()
        })
    }

    pub fn best(
        font_name: &FontName,
        style: FontStyle,
        weight: FontWeight,
        stretch: FontStretch,
    ) -> Result<Option<(FontDataRef, u32)>, FontLoadingError> {
        if font_name == "Ubuntu" {
            if let Ok(Some(h)) = workaround_ubuntu(style, weight, stretch) {
                return Ok(Some(h));
            }
        }

        let family_name = font_kit::family_name::FamilyName::from(font_name.clone());
        match font_kit::source::SystemSource::new().select_best_match(
            &[family_name],
            &font_kit::properties::Properties {
                style: style.into(),
                weight: weight.into(),
                stretch: stretch.into(),
            },
        ) {
            Ok(handle) => {
                let r = load_handle(&handle)?;
                Ok(Some(r))
            }
            Err(font_kit::error::SelectionError::NotFound) => {
                tracing::debug!(target: "font_loading", "system font not found\nquery: {:?}", (font_name, style, weight, stretch));
                Ok(None)
            }
            Err(font_kit::error::SelectionError::CannotAccessSource { reason }) => Err(FontLoadingError::Io(Arc::new(
                std::io::Error::new(std::io::ErrorKind::Other, reason.unwrap_or_default()),
            ))),
        }
    }

    // see https://github.com/servo/font-kit/issues/245
    fn workaround_ubuntu(
        style: FontStyle,
        weight: FontWeight,
        stretch: FontStretch,
    ) -> Result<Option<(FontDataRef, u32)>, FontLoadingError> {
        let source = font_kit::source::SystemSource::new();
        let ubuntu = match source.select_family_by_name("Ubuntu") {
            Ok(u) => u,
            Err(e) => {
                return match e {
                    font_kit::error::SelectionError::NotFound => Ok(None),
                    font_kit::error::SelectionError::CannotAccessSource { reason } => Err(FontLoadingError::Io(Arc::new(
                        std::io::Error::new(std::io::ErrorKind::Other, reason.unwrap_or_default()),
                    ))),
                }
            }
        };
        for handle in ubuntu.fonts() {
            let font = handle.load()?;
            let name = match font.postscript_name() {
                Some(n) => n,
                None => continue,
            };

            // Ubuntu-ExtraBold
            // Ubuntu-Condensed
            // Ubuntu-CondensedLight
            // Ubuntu-CondensedBold
            // Ubuntu-CondensedMedium
            // Ubuntu-CondensedExtraBold
            // UbuntuItalic-CondensedLightItalic
            // UbuntuItalic-CondensedItalic
            // UbuntuItalic-CondensedMediumItalic
            // UbuntuItalic-CondensedBoldItalic
            // UbuntuItalic-CondensedExtraBoldItalic
            // Ubuntu-Italic
            // UbuntuItalic-ThinItalic
            // UbuntuItalic-LightItalic
            // UbuntuItalic-Italic
            // UbuntuItalic-MediumItalic
            // UbuntuItalic-BoldItalic
            // UbuntuItalic-ExtraBoldItalic
            // UbuntuItalic-CondensedThinItalic
            // Ubuntu-Thin
            // Ubuntu-Regular
            // Ubuntu-Light
            // Ubuntu-Bold
            // Ubuntu-Medium
            // Ubuntu-CondensedThin

            if (style == FontStyle::Italic) != name.contains("Italic") {
                continue;
            }

            if (FontWeight::MEDIUM..FontWeight::SEMIBOLD).contains(&weight) != name.contains("Medium") {
                continue;
            }
            if (weight >= FontWeight::EXTRA_BOLD) != name.contains("ExtraBold") {
                continue;
            }
            if (FontWeight::SEMIBOLD..FontWeight::EXTRA_BOLD).contains(&weight) != name.contains("Bold") {
                continue;
            }

            if (FontWeight::EXTRA_LIGHT..FontWeight::LIGHT).contains(&weight) != name.contains("Light") {
                continue;
            }
            if (weight < FontWeight::EXTRA_LIGHT) != name.contains("Thin") {
                continue;
            }

            if (stretch <= FontStretch::CONDENSED) != name.contains("Condensed") {
                continue;
            }

            return Ok(Some(load_handle(handle)?));
        }
        Ok(None)
    }

    fn load_handle(handle: &font_kit::handle::Handle) -> Result<(FontDataRef, u32), FontLoadingError> {
        match handle {
            font_kit::handle::Handle::Path { path, font_index } => {
                let mut path = Cow::Borrowed(path);
                // try replacing type1 fonts with OpenType
                // RustyBuzz does not support type1 (neither does Harfbuzz, it is obsolete)
                //
                // Example case from default Ubuntu fonts:
                // /usr/share/fonts/type1/urw-base35/Z003-MediumItalic.t1
                // /usr/share/fonts/opentype/urw-base35/Z003-MediumItalic.otf
                if let Ok(base) = path.strip_prefix("/usr/share/fonts/type1/") {
                    if let Some(name) = base.file_name() {
                        if let Some(name) = name.to_str() {
                            if name.ends_with(".t1") {
                                let rep = Path::new("/usr/share/fonts/opentype/").join(base.with_extension("otf"));
                                if rep.exists() {
                                    tracing::debug!("replaced `{name}` with .otf of same name");
                                    path = Cow::Owned(rep);
                                }
                            }
                        }
                    }
                }

                let bytes = std::fs::read(&*path)?;

                Ok((FontDataRef(Arc::new(bytes)), *font_index))
            }
            font_kit::handle::Handle::Memory { bytes, font_index } => Ok((FontDataRef(bytes.clone()), *font_index)),
        }
    }

    impl From<font_kit::error::FontLoadingError> for FontLoadingError {
        fn from(ve: font_kit::error::FontLoadingError) -> Self {
            match ve {
                font_kit::error::FontLoadingError::UnknownFormat => Self::UnknownFormat,
                font_kit::error::FontLoadingError::NoSuchFontInCollection => Self::NoSuchFontInCollection,
                font_kit::error::FontLoadingError::Parse => Self::Parse(ttf_parser::FaceParsingError::MalformedFont),
                font_kit::error::FontLoadingError::NoFilesystem => Self::NoFilesystem,
                font_kit::error::FontLoadingError::Io(e) => Self::Io(Arc::new(e)),
            }
        }
    }

    impl From<FontName> for font_kit::family_name::FamilyName {
        fn from(font_name: FontName) -> Self {
            use font_kit::family_name::FamilyName::*;
            match font_name.name() {
                "serif" => Serif,
                "sans-serif" => SansSerif,
                "monospace" => Monospace,
                "cursive" => Cursive,
                "fantasy" => Fantasy,
                _ => Title(font_name.txt.into()),
            }
        }
    }

    impl From<FontStretch> for font_kit::properties::Stretch {
        fn from(value: FontStretch) -> Self {
            font_kit::properties::Stretch(value.0)
        }
    }

    impl From<FontStyle> for font_kit::properties::Style {
        fn from(value: FontStyle) -> Self {
            use font_kit::properties::Style::*;
            match value {
                FontStyle::Normal => Normal,
                FontStyle::Italic => Italic,
                FontStyle::Oblique => Oblique,
            }
        }
    }

    impl From<FontWeight> for font_kit::properties::Weight {
        fn from(value: FontWeight) -> Self {
            font_kit::properties::Weight(value.0)
        }
    }

    impl From<font_kit::error::GlyphLoadingError> for GlyphLoadingError {
        fn from(value: font_kit::error::GlyphLoadingError) -> Self {
            use GlyphLoadingError::*;
            match value {
                font_kit::error::GlyphLoadingError::NoSuchGlyph => NoSuchGlyph,
                font_kit::error::GlyphLoadingError::PlatformError => PlatformError,
            }
        }
    }
}

#[cfg(target_arch = "wasm32")]
mod wasm {
    use zng_var::ResponseVar;

    use crate::{FontDataRef, FontLoadingError, FontName, FontStretch, FontStyle, FontWeight};

    pub fn system_all() -> ResponseVar<Vec<FontName>> {
        zng_var::response_done_var(vec![])
    }

    pub fn best(
        font_name: &FontName,
        style: FontStyle,
        weight: FontWeight,
        stretch: FontStretch,
    ) -> Result<Option<(FontDataRef, u32)>, FontLoadingError> {
        let _ = (font_name, style, weight, stretch);
        Err(FontLoadingError::NoFilesystem)
    }
}

#[cfg(target_os = "android")]
mod android {
    // font-kit does not cross compile for Android because of a dependency,
    // so we reimplement/copy some of their code here.

    use std::{borrow::Cow, path::PathBuf, sync::Arc};

    use zng_var::ResponseVar;

    use crate::{FontDataRef, FontLoadingError, FontName, FontStretch, FontStyle, FontWeight};

    pub fn system_all() -> ResponseVar<Vec<FontName>> {
        zng_task::wait_respond(|| {
            let mut prev = None;
            cached_system_all()
                .iter()
                .flat_map(|(k, _)| {
                    if prev == Some(k) {
                        None
                    } else {
                        prev = Some(k);
                        Some(k)
                    }
                })
                .cloned()
                .collect()
        })
    }

    fn cached_system_all() -> parking_lot::MappedRwLockReadGuard<'static, Vec<(FontName, PathBuf)>> {
        let lock = SYSTEM_ALL.read();
        if !lock.is_empty() {
            return lock;
        }

        drop(lock);
        let mut lock = SYSTEM_ALL.write();
        if lock.is_empty() {
            for entry in ["/system/fonts/", "/system/font/", "/data/fonts/", "/system/product/fonts/"]
                .iter()
                .flat_map(std::fs::read_dir)
                .flatten()
                .flatten()
            {
                let entry = entry.path();
                let ext = entry.extension().and_then(|e| e.to_str()).unwrap_or_default().to_ascii_lowercase();
                if ["ttf", "otf"].contains(&ext.as_str()) && entry.is_file() {
                    if let Ok(bytes) = std::fs::read(&entry) {
                        match crate::FontFace::load(FontDataRef(Arc::new(bytes)), 0) {
                            Ok(f) => {
                                lock.push((f.family_name().clone(), entry));
                            }
                            Err(e) => tracing::error!("error parsing '{}', {e}", entry.display()),
                        }
                    }
                }
            }
            lock.sort_by(|a, b| a.0.cmp(&b.0));
        }
        drop(lock);
        SYSTEM_ALL.read()
    }

    pub fn best(
        font_name: &FontName,
        style: FontStyle,
        weight: FontWeight,
        stretch: FontStretch,
    ) -> Result<Option<(FontDataRef, u32)>, FontLoadingError> {
        let lock = cached_system_all();
        let lock = &*lock;

        // special names
        // source: https://android.googlesource.com/platform/frameworks/base/+/master/data/fonts/fonts.xml
        let font_name = match font_name.name() {
            "sans-serif" => Cow::Owned(FontName::new("Roboto")),
            "serif" | "fantasy" => Cow::Owned(FontName::new("Noto Serif")),
            "cursive" => Cow::Owned(FontName::new("Dancing Script")),
            "monospace" => Cow::Owned(FontName::new("Droid Sans Mono")),
            _ => Cow::Borrowed(font_name),
        };
        let font_name = &*font_name;

        let mut start_i = match lock.binary_search_by(|a| a.0.cmp(font_name)) {
            Ok(i) => i,
            Err(_) => {
                tracing::debug!(target: "font_loading", "system font not found\nquery: {:?}", (font_name, style, weight, stretch));
                return Ok(None);
            }
        };
        while start_i > 0 && &lock[start_i - 1].0 == font_name {
            start_i -= 1
        }
        let mut end_i = start_i;
        while end_i + 1 < lock.len() && &lock[end_i + 1].0 == font_name {
            end_i += 1
        }

        let family_len = end_i - start_i;
        let mut options = Vec::with_capacity(family_len);
        let mut candidates = Vec::with_capacity(family_len);

        for (_, path) in &lock[start_i..=end_i] {
            if let Ok(bytes) = std::fs::read(path) {
                if let Ok(f) = ttf_parser::Face::parse(&bytes, 0) {
                    candidates.push(matching::Properties {
                        style: f.style(),
                        weight: f.weight(),
                        stretch: f.width(),
                    });
                    options.push(bytes);
                }
            }
        }

        match matching::find_best_match(
            &candidates,
            &matching::Properties {
                style: style.into(),
                weight: weight.into(),
                stretch: stretch.into(),
            },
        ) {
            Ok(i) => {
                let bytes = options.swap_remove(i);
                Ok(Some((FontDataRef(Arc::new(bytes)), 0)))
            }
            Err(FontLoadingError::NoSuchFontInCollection) => {
                tracing::debug!(target: "font_loading", "system font not found\nquery: {:?}", (font_name, style, weight, stretch));
                Ok(None)
            }
            Err(e) => Err(FontLoadingError::Io(Arc::new(std::io::Error::new(std::io::ErrorKind::Other, e)))),
        }
    }

    zng_app_context::app_local! {
        static SYSTEM_ALL: Vec<(FontName, PathBuf)> = vec![];
    }

    mod matching {
        // font-kit/src/matching.rs
        //
        // Copyright © 2018 The Pathfinder Project Developers.
        //
        // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
        // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
        // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
        // option. This file may not be copied, modified, or distributed
        // except according to those terms.

        //! Determines the closest font matching a description per the CSS Fonts Level 3 specification.

        use ttf_parser::{Style, Weight, Width as Stretch};

        use crate::FontLoadingError;

        pub struct Properties {
            pub style: Style,
            pub weight: Weight,
            pub stretch: Stretch,
        }

        /// This follows CSS Fonts Level 3 § 5.2 [1].
        ///
        /// https://drafts.csswg.org/css-fonts-3/#font-style-matching
        pub fn find_best_match(candidates: &[Properties], query: &Properties) -> Result<usize, FontLoadingError> {
            // Step 4.
            let mut matching_set: Vec<usize> = (0..candidates.len()).collect();
            if matching_set.is_empty() {
                return Err(FontLoadingError::NoSuchFontInCollection);
            }

            // Step 4a (`font-stretch`).
            let matching_stretch = if matching_set.iter().any(|&index| candidates[index].stretch == query.stretch) {
                // Exact match.
                query.stretch
            } else if query.stretch <= Stretch::Normal {
                // Closest width, first checking narrower values and then wider values.
                match matching_set
                    .iter()
                    .filter(|&&index| candidates[index].stretch < query.stretch)
                    .min_by_key(|&&index| query.stretch.to_number() - candidates[index].stretch.to_number())
                {
                    Some(&matching_index) => candidates[matching_index].stretch,
                    None => {
                        let matching_index = *matching_set
                            .iter()
                            .min_by_key(|&&index| candidates[index].stretch.to_number() - query.stretch.to_number())
                            .unwrap();
                        candidates[matching_index].stretch
                    }
                }
            } else {
                // Closest width, first checking wider values and then narrower values.
                match matching_set
                    .iter()
                    .filter(|&&index| candidates[index].stretch > query.stretch)
                    .min_by_key(|&&index| candidates[index].stretch.to_number() - query.stretch.to_number())
                {
                    Some(&matching_index) => candidates[matching_index].stretch,
                    None => {
                        let matching_index = *matching_set
                            .iter()
                            .min_by_key(|&&index| query.stretch.to_number() - candidates[index].stretch.to_number())
                            .unwrap();
                        candidates[matching_index].stretch
                    }
                }
            };
            matching_set.retain(|&index| candidates[index].stretch == matching_stretch);

            // Step 4b (`font-style`).
            let style_preference = match query.style {
                Style::Italic => [Style::Italic, Style::Oblique, Style::Normal],
                Style::Oblique => [Style::Oblique, Style::Italic, Style::Normal],
                Style::Normal => [Style::Normal, Style::Oblique, Style::Italic],
            };
            let matching_style = *style_preference
                .iter()
                .find(|&query_style| matching_set.iter().any(|&index| candidates[index].style == *query_style))
                .unwrap();
            matching_set.retain(|&index| candidates[index].style == matching_style);

            // Step 4c (`font-weight`).
            //
            // The spec doesn't say what to do if the weight is between 400 and 500 exclusive, so we
            // just use 450 as the cutoff.
            let matching_weight = if matching_set.iter().any(|&index| candidates[index].weight == query.weight) {
                query.weight
            } else if query.weight.to_number() >= 400
                && query.weight.to_number() < 450
                && matching_set.iter().any(|&index| candidates[index].weight == Weight::from(500))
            {
                // Check 500 first.
                Weight::from(500)
            } else if query.weight.to_number() >= 450
                && query.weight.to_number() <= 500
                && matching_set.iter().any(|&index| candidates[index].weight.to_number() == 400)
            {
                // Check 400 first.
                Weight::from(400)
            } else if query.weight.to_number() <= 500 {
                // Closest weight, first checking thinner values and then fatter ones.
                match matching_set
                    .iter()
                    .filter(|&&index| candidates[index].weight.to_number() <= query.weight.to_number())
                    .min_by_key(|&&index| query.weight.to_number() - candidates[index].weight.to_number())
                {
                    Some(&matching_index) => candidates[matching_index].weight,
                    None => {
                        let matching_index = *matching_set
                            .iter()
                            .min_by_key(|&&index| candidates[index].weight.to_number() - query.weight.to_number())
                            .unwrap();
                        candidates[matching_index].weight
                    }
                }
            } else {
                // Closest weight, first checking fatter values and then thinner ones.
                match matching_set
                    .iter()
                    .filter(|&&index| candidates[index].weight.to_number() >= query.weight.to_number())
                    .min_by_key(|&&index| candidates[index].weight.to_number() - query.weight.to_number())
                {
                    Some(&matching_index) => candidates[matching_index].weight,
                    None => {
                        let matching_index = *matching_set
                            .iter()
                            .min_by_key(|&&index| query.weight.to_number() - candidates[index].weight.to_number())
                            .unwrap();
                        candidates[matching_index].weight
                    }
                }
            };
            matching_set.retain(|&index| candidates[index].weight == matching_weight);

            // Step 4d concerns `font-size`, but fonts in `font-kit` are unsized, so we ignore that.

            // Return the result.
            matching_set.into_iter().next().ok_or(FontLoadingError::NoSuchFontInCollection)
        }
    }
}