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
use crate::font::{Family, LoadError};

/// A font source that can find and load system fonts.
#[allow(missing_debug_implementations)]
pub struct Source {
    raw: font_kit::source::SystemSource,
}

impl Source {
    /// Creates a new [`Source`].
    ///
    /// [`Source`]: struct.Source.html
    pub fn new() -> Self {
        Source {
            raw: font_kit::source::SystemSource::new(),
        }
    }

    /// Finds and loads a font matching the set of provided family priorities.
    ///
    /// [`Source`]: struct.Source.html
    pub fn load(&self, families: &[Family]) -> Result<Vec<u8>, LoadError> {
        let font = self.raw.select_best_match(
            families,
            &font_kit::properties::Properties::default(),
        )?;

        match font {
            font_kit::handle::Handle::Path { path, .. } => {
                use std::io::Read;

                let mut buf = Vec::new();
                let mut reader = std::fs::File::open(path).expect("Read font");
                let _ = reader.read_to_end(&mut buf);

                Ok(buf)
            }
            font_kit::handle::Handle::Memory { bytes, .. } => {
                Ok(bytes.as_ref().clone())
            }
        }
    }
}