From ab294950b4a07df7092f64c561127bc9c0ae9afc Mon Sep 17 00:00:00 2001 From: Breval Ferrari Date: Tue, 15 Apr 2025 19:00:52 -0400 Subject: [PATCH] fonts --- bingus/src/fnt.rs | 2 ++ bingus/src/fnt/fontkit.rs | 42 +++++++++++++++++++++++++++++++++++++++ bingus/src/lib.rs | 3 +++ 3 files changed, 47 insertions(+) create mode 100644 bingus/src/fnt.rs create mode 100644 bingus/src/fnt/fontkit.rs diff --git a/bingus/src/fnt.rs b/bingus/src/fnt.rs new file mode 100644 index 0000000..800fe2e --- /dev/null +++ b/bingus/src/fnt.rs @@ -0,0 +1,2 @@ +mod fontkit; +pub use fontkit::Font; diff --git a/bingus/src/fnt/fontkit.rs b/bingus/src/fnt/fontkit.rs new file mode 100644 index 0000000..3da11ac --- /dev/null +++ b/bingus/src/fnt/fontkit.rs @@ -0,0 +1,42 @@ +use font_kit::error::FontLoadingError; +pub use font_kit::font::Font; +use std::sync::Arc; + +use crate::{Bendable, IntoDataBytes, TryFromDataBytes}; + +impl TryFromDataBytes for Font { + type Error = FontLoadingError; + type Format = (); + + fn try_from_data_bytes( + bytes: crate::Bytes, + _format: Self::Format, + _crop: crate::Crop, + ) -> Result + where + Self: Sized, + { + Self::from_bytes(Arc::new(bytes), 0) + } +} + +impl IntoDataBytes for Font { + fn into_data_bytes(self) -> crate::Bytes { + self.copy_font_data() + .map(|v| v.as_ref().clone()) + .unwrap_or_default() + } +} + +impl Bendable for Font { + type Unit = u8; + + fn map Self::Unit + Sync>(self, f: F) -> Self { + Self::try_from_data_bytes(self.into_data_bytes().map(f), (), Default::default()) + .expect("coudn't get font back from bytes after map") + } + + fn format() -> crate::Format { + crate::Format::Font + } +} diff --git a/bingus/src/lib.rs b/bingus/src/lib.rs index 5b7c3d6..fb89b3e 100644 --- a/bingus/src/lib.rs +++ b/bingus/src/lib.rs @@ -1,5 +1,6 @@ pub mod bin; pub mod doc; +pub mod fnt; pub mod img; pub mod snd; pub mod txt; @@ -15,6 +16,7 @@ mod dynamic { }; use super::{ + fnt::Font, img::{self, DynamicImage}, snd::{self, Audio}, txt::{ShivaDocument, Text}, @@ -37,6 +39,7 @@ mod dynamic { Doc(ShivaDocument), Archive(PdfDocument), Meta, + Font(Font), } #[derive(Debug, Error)]