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
// SPDX-License-Identifier: MIT OR Apache-2.0

use core::ops::Deref;

pub(crate) mod fallback;

pub use self::matches::*;
mod matches;

pub use self::system::*;
mod system;

pub struct Font<'a> {
    pub info: &'a fontdb::FaceInfo,
    pub data: &'a [u8],
    pub rustybuzz: rustybuzz::Face<'a>,
    #[cfg(feature = "swash")]
    pub swash: (u32, swash::CacheKey),
}

impl<'a> Font<'a> {
    pub fn new(info: &'a fontdb::FaceInfo) -> Option<Self> {
        let data = match &info.source {
            fontdb::Source::Binary(data) => data.deref().as_ref(),
            #[cfg(feature = "std")]
            fontdb::Source::File(path) => {
                log::warn!("Unsupported fontdb Source::File('{}')", path.display());
                return None;
            }
            #[cfg(feature = "std")]
            fontdb::Source::SharedFile(_path, data) => data.deref().as_ref(),
        };

        Some(Self {
            info,
            data,
            rustybuzz: rustybuzz::Face::from_slice(data, info.index)?,
            #[cfg(feature = "swash")]
            swash: {
                let swash = swash::FontRef::from_index(data, info.index as usize)?;
                (swash.offset, swash.key)
            },
        })
    }

    #[cfg(feature = "swash")]
    pub fn as_swash(&self) -> swash::FontRef {
        swash::FontRef {
            data: self.data,
            offset: self.swash.0,
            key: self.swash.1,
        }
    }
}