tinymist_world/font/
loader.rs1use tinymist_std::ReadAllOnce;
2use typst::text::Font;
3
4use crate::Bytes;
5
6pub trait FontLoader {
8 fn load(&mut self) -> Option<Font>;
10}
11
12pub struct BufferFontLoader {
14 pub buffer: Option<Bytes>,
16 pub index: u32,
18}
19
20impl FontLoader for BufferFontLoader {
21 fn load(&mut self) -> Option<Font> {
22 Font::new(self.buffer.take().unwrap(), self.index)
23 }
24}
25
26pub struct LazyBufferFontLoader<R> {
28 pub read: Option<R>,
30 pub index: u32,
32}
33
34impl<R: ReadAllOnce + Sized> LazyBufferFontLoader<R> {
35 pub fn new(read: R, index: u32) -> Self {
37 Self {
38 read: Some(read),
39 index,
40 }
41 }
42}
43
44impl<R: ReadAllOnce + Sized> FontLoader for LazyBufferFontLoader<R> {
45 fn load(&mut self) -> Option<Font> {
46 let mut buf = vec![];
47 self.read.take().unwrap().read_all(&mut buf).ok()?;
48 Font::new(Bytes::new(buf), self.index)
49 }
50}