summaryrefslogtreecommitdiff
path: root/src/loading/mem.rs
blob: 9ff02d2d7fd25d750453cb8c5307c48ef84c6a53 (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
use std::borrow::Cow;
use std::collections::HashMap;
use std::io;
use std::path::{Path, PathBuf};
use std::rc::Rc;

use super::{FileHash, Loader};
use crate::font::FaceInfo;
use crate::util::PathExt;

/// Loads fonts and files from an in-memory storage.
#[derive(Default)]
pub struct MemLoader {
    faces: Vec<FaceInfo>,
    files: HashMap<PathBuf, Cow<'static, [u8]>>,
}

impl MemLoader {
    /// Create a new from-memory loader.
    pub fn new() -> Self {
        Self { faces: vec![], files: HashMap::new() }
    }

    /// Builder-style variant of [`insert`](Self::insert).
    pub fn with<P, D>(mut self, path: P, data: D) -> Self
    where
        P: AsRef<Path>,
        D: Into<Cow<'static, [u8]>>,
    {
        self.insert(path, data);
        self
    }

    /// Builder-style method to wrap the loader in an [`Rc`] to make it usable
    /// with the [`Context`](crate::Context).
    pub fn wrap(self) -> Rc<Self> {
        Rc::new(self)
    }

    /// Insert a path-file mapping. If the data forms a font, then that font
    /// will be available for layouting.
    ///
    /// The data can either be owned or referenced, but the latter only if its
    /// lifetime is `'static`.
    pub fn insert<P, D>(&mut self, path: P, data: D)
    where
        P: AsRef<Path>,
        D: Into<Cow<'static, [u8]>>,
    {
        let path = path.as_ref().normalize();
        let data = data.into();
        self.faces.extend(FaceInfo::parse(&path, &data));
        self.files.insert(path, data);
    }
}

impl Loader for MemLoader {
    fn faces(&self) -> &[FaceInfo] {
        &self.faces
    }

    fn resolve(&self, path: &Path) -> io::Result<FileHash> {
        let norm = path.normalize();
        if self.files.contains_key(&norm) {
            Ok(FileHash(fxhash::hash64(&norm)))
        } else {
            Err(io::ErrorKind::NotFound.into())
        }
    }

    fn load(&self, path: &Path) -> io::Result<Vec<u8>> {
        self.files
            .get(&path.normalize())
            .map(|cow| cow.clone().into_owned())
            .ok_or_else(|| io::ErrorKind::NotFound.into())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::font::FontVariant;

    #[test]
    fn test_recognize_and_load_font() {
        let data = include_bytes!("../../fonts/PTSans-Regular.ttf");
        let path = Path::new("PTSans.ttf");
        let loader = MemLoader::new().with(path, &data[..]);

        // Test that the found was found.
        let info = &loader.faces[0];
        assert_eq!(info.path, path);
        assert_eq!(info.index, 0);
        assert_eq!(info.family, "PT Sans");
        assert_eq!(info.variant, FontVariant::default());
        assert_eq!(loader.faces.len(), 1);

        // Test that the file can be loaded.
        assert_eq!(
            loader.load(Path::new("directory/../PTSans.ttf")).unwrap(),
            data
        );
    }
}