tinymist_world/font/
loader.rsuse tinymist_std::ReadAllOnce;
use typst::text::Font;
use crate::Bytes;
pub trait FontLoader {
fn load(&mut self) -> Option<Font>;
}
pub struct BufferFontLoader {
pub buffer: Option<Bytes>,
pub index: u32,
}
impl FontLoader for BufferFontLoader {
fn load(&mut self) -> Option<Font> {
Font::new(self.buffer.take().unwrap(), self.index)
}
}
pub struct LazyBufferFontLoader<R> {
pub read: Option<R>,
pub index: u32,
}
impl<R: ReadAllOnce + Sized> LazyBufferFontLoader<R> {
pub fn new(read: R, index: u32) -> Self {
Self {
read: Some(read),
index,
}
}
}
impl<R: ReadAllOnce + Sized> FontLoader for LazyBufferFontLoader<R> {
fn load(&mut self) -> Option<Font> {
let mut buf = vec![];
self.read.take().unwrap().read_all(&mut buf).ok()?;
Font::new(Bytes::new(buf), self.index)
}
}