Store font as peniko::Font behind feature flag (#375)

* Allow exposing Peniko font from cosmic text

* .

* .

* .

* .
This commit is contained in:
Taj Pereira 2025-04-05 03:49:45 +10:30 committed by GitHub
parent 6598c7cae8
commit 695745ac4f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 27 additions and 1 deletions

View file

@ -40,11 +40,16 @@ version = "0.3.13"
default-features = false
features = ["hardcoded-data"]
[dependencies.peniko]
version = "0.3.1"
optional = true
[features]
default = ["std", "swash", "fontconfig"]
fontconfig = ["fontdb/fontconfig", "std"]
monospace_fallback = []
no_std = ["rustybuzz/libm", "hashbrown", "dep:libm"]
peniko = ["dep:peniko"]
shape-run-cache = []
std = [
"fontdb/memmap",

View file

@ -2,6 +2,9 @@
// re-export ttf_parser
pub use ttf_parser;
// re-export peniko::Font;
#[cfg(feature = "peniko")]
pub use peniko::Font as PenikoFont;
use core::fmt;
@ -38,7 +41,10 @@ pub struct Font {
#[cfg(feature = "swash")]
swash: (u32, swash::CacheKey),
rustybuzz: OwnedFace,
#[cfg(not(feature = "peniko"))]
data: Arc<dyn AsRef<[u8]> + Send + Sync>,
#[cfg(feature = "peniko")]
data: peniko::Font,
id: fontdb::ID,
monospace_fallback: Option<FontMonospaceFallback>,
}
@ -73,13 +79,25 @@ impl Font {
}
pub fn data(&self) -> &[u8] {
(*self.data).as_ref()
#[cfg(not(feature = "peniko"))]
{
(*self.data).as_ref()
}
#[cfg(feature = "peniko")]
{
self.data.data.data()
}
}
pub fn rustybuzz(&self) -> &RustybuzzFace<'_> {
self.rustybuzz.borrow_dependent()
}
#[cfg(feature = "peniko")]
pub fn as_peniko(&self) -> PenikoFont {
self.data.clone()
}
#[cfg(feature = "swash")]
pub fn as_swash(&self) -> swash::FontRef<'_> {
let swash = &self.swash;
@ -171,7 +189,10 @@ impl Font {
RustybuzzFace::from_slice((**data).as_ref(), info.index).ok_or(())
})
.ok()?,
#[cfg(not(feature = "peniko"))]
data,
#[cfg(feature = "peniko")]
data: peniko::Font::new(peniko::Blob::new(data), info.index),
})
}
}