tinymist_world/font/
loader.rs

1use tinymist_std::ReadAllOnce;
2use typst::text::Font;
3
4use crate::Bytes;
5
6/// A FontLoader would help load a font from somewhere.
7pub trait FontLoader {
8    fn load(&mut self) -> Option<Font>;
9}
10
11/// Load font from a buffer.
12pub struct BufferFontLoader {
13    pub buffer: Option<Bytes>,
14    pub index: u32,
15}
16
17impl FontLoader for BufferFontLoader {
18    fn load(&mut self) -> Option<Font> {
19        Font::new(self.buffer.take().unwrap(), self.index)
20    }
21}
22
23pub struct LazyBufferFontLoader<R> {
24    pub read: Option<R>,
25    pub index: u32,
26}
27
28impl<R: ReadAllOnce + Sized> LazyBufferFontLoader<R> {
29    pub fn new(read: R, index: u32) -> Self {
30        Self {
31            read: Some(read),
32            index,
33        }
34    }
35}
36
37impl<R: ReadAllOnce + Sized> FontLoader for LazyBufferFontLoader<R> {
38    fn load(&mut self) -> Option<Font> {
39        let mut buf = vec![];
40        self.read.take().unwrap().read_all(&mut buf).ok()?;
41        Font::new(Bytes::new(buf), self.index)
42    }
43}