bingus 0.10.0

databending made easy
Documentation
use cfg_if::cfg_if;
use font_kit::error::FontLoadingError;
pub use font_kit::font::Font;
use std::{borrow::Cow, 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<Self, Self::Error> {
        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<F: Fn(Cow<Self::Unit>) -> Self::Unit + Sync>(self, f: F) -> Self {
        Self::try_from_data_bytes(
            {
                let bytes = self.into_data_bytes();
                cfg_if! {
                    if #[cfg(feature = "binary")] {
                        bytes.map(f)
                    } else {
                        bytes.into_iter().map(|e| f(Cow::Owned(e))).collect()
                    }
                }
            },
            (),
            Default::default(),
        )
        .expect("coudn't get font back from bytes after map")
    }
}