Make FontSystem not self-referencing

This commit is contained in:
Edgar Geier 2023-03-01 02:50:10 +01:00
parent 0548d7ae59
commit 506a4194be
No known key found for this signature in database
GPG key ID: 7A65B51FD6B75EF5
10 changed files with 206 additions and 175 deletions

View file

@ -4,12 +4,17 @@ use core::ops::Deref;
pub(crate) mod fallback;
pub use self::matches::*;
mod matches;
pub use self::system::*;
mod system;
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
/// Identifies a [`Font`] in a [`FontSystem`]
pub struct FontKey {
pub id: fontdb::ID,
#[cfg(feature = "swash")]
pub swash: (u32, swash::CacheKey),
}
/// A font
pub struct Font<'a> {
pub info: &'a fontdb::FaceInfo,
@ -44,6 +49,36 @@ impl<'a> Font<'a> {
})
}
pub fn from_key(db: &'a fontdb::Database, key: FontKey) -> Option<Self> {
let info = db.face(key.id)?;
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: key.swash,
})
}
pub fn key(&self) -> FontKey {
FontKey {
id: self.info.id,
#[cfg(feature = "swash")]
swash: self.swash,
}
}
#[cfg(feature = "swash")]
pub fn as_swash(&self) -> swash::FontRef {
swash::FontRef {