use std::any::Any;
use std::path::PathBuf;
use std::sync::Arc;
use crate::error::FontLoadingError;
use crate::font::Font;
use crate::loader::Loader;
#[derive(Debug, Clone)]
pub enum Handle {
Path {
path: PathBuf,
font_index: u32,
},
Memory {
bytes: Arc<Vec<u8>>,
font_index: u32,
},
Native {
inner: Arc<dyn Any + Sync + Send>,
},
}
impl Handle {
#[inline]
pub fn from_path(path: PathBuf, font_index: u32) -> Handle {
Handle::Path { path, font_index }
}
#[inline]
pub fn from_memory(bytes: Arc<Vec<u8>>, font_index: u32) -> Handle {
Handle::Memory { bytes, font_index }
}
pub fn from_native<T: Loader>(inner: &T) -> Self
where
T::NativeFont: Sync + Send,
{
Self::Native {
inner: Arc::new(inner.native_font()),
}
}
pub fn native_as<T: 'static>(&self) -> Option<&T> {
if let Self::Native { inner } = self {
inner.downcast_ref()
} else {
None
}
}
#[inline]
pub fn load(&self) -> Result<Font, FontLoadingError> {
Font::from_handle(self)
}
}