Call get_font lazily

This commit is contained in:
Edgar Geier 2023-03-14 00:39:50 +01:00
parent f86acd325c
commit d297a6a48a
No known key found for this signature in database
GPG key ID: B022ECD3278A265C
5 changed files with 97 additions and 99 deletions

View file

@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT OR Apache-2.0
pub(crate) mod fallback;
use fontdb::FaceInfo;
use alloc::sync::Arc;
pub use self::system::*;
mod system;
@ -12,8 +12,9 @@ pub struct Font(FontInner);
#[ouroboros::self_referencing]
#[allow(dead_code)]
struct FontInner {
info: fontdb::FaceInfo,
#[borrows(info)]
id: fontdb::ID,
data: Arc<dyn AsRef<[u8]> + Send + Sync>,
#[borrows(data)]
#[covariant]
rustybuzz: rustybuzz::Face<'this>,
// workaround, since ouroboros does not work with #[cfg(feature = "swash")]
@ -30,18 +31,18 @@ impl Font {
pub fn new(info: &fontdb::FaceInfo) -> Option<Self> {
#[allow(unused_variables)]
let data = match &info.source {
fontdb::Source::Binary(data) => (**data).as_ref(),
fontdb::Source::Binary(data) => Arc::clone(data),
#[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).as_ref(),
fontdb::Source::SharedFile(_path, data) => Arc::clone(data),
};
Some(Self(
FontInnerTryBuilder {
info: info.clone(),
id: info.id,
swash: {
#[cfg(feature = "swash")]
let swash = {
@ -53,8 +54,9 @@ impl Font {
let swash = ();
swash
},
rustybuzz_builder: |info| {
rustybuzz::Face::from_slice(get_data(info), info.index).ok_or(())
data,
rustybuzz_builder: |data| {
rustybuzz::Face::from_slice((**data).as_ref(), info.index).ok_or(())
},
}
.try_build()
@ -62,36 +64,23 @@ impl Font {
))
}
pub fn info(&self) -> &FaceInfo {
self.0.borrow_info()
pub fn id(&self) -> fontdb::ID {
*self.0.borrow_id()
}
pub fn data(&self) -> &[u8] {
get_data(self.0.borrow_info())
(**self.0.borrow_data()).as_ref()
}
pub fn rustybuzz(&self) -> &rustybuzz::Face {
self.0.borrow_rustybuzz()
}
pub fn name(&self) -> &str {
if let Some((name, _)) = self.info().families.first() {
name
} else {
&self.info().post_script_name
}
}
pub fn contains_family(&self, family: &str) -> bool {
self.info().families.iter().any(|(name, _)| name == family)
}
#[cfg(feature = "swash")]
pub fn as_swash(&self) -> swash::FontRef {
let info = self.0.borrow_info();
let swash = self.0.borrow_swash();
swash::FontRef {
data: get_data(info),
data: self.data(),
offset: swash.0,
key: swash.1,
}
@ -104,16 +93,3 @@ impl Font {
self.0.borrow_swash();
}
}
fn get_data(info: &FaceInfo) -> &[u8] {
match &info.source {
fontdb::Source::Binary(data) => (**data).as_ref(),
#[cfg(feature = "std")]
fontdb::Source::File(path) => {
// This should never happen, because `Font::new` verified the source isn't a file
panic!("Unsupported fontdb Source::File('{}')", path.display());
}
#[cfg(feature = "std")]
fontdb::Source::SharedFile(_path, data) => (**data).as_ref(),
}
}