tinymist_world/font/
pure.rs

1use tinymist_std::debug_loc::{DataSource, MemoryDataSource};
2use typst::foundations::Bytes;
3use typst::text::{FontBook, FontInfo};
4
5use crate::font::{BufferFontLoader, FontResolverImpl, FontSlot};
6
7/// memory font builder.
8#[derive(Debug)]
9pub struct MemoryFontBuilder {
10    pub book: FontBook,
11    pub fonts: Vec<FontSlot>,
12}
13
14impl Default for MemoryFontBuilder {
15    fn default() -> Self {
16        Self::new()
17    }
18}
19
20impl From<MemoryFontBuilder> for FontResolverImpl {
21    fn from(searcher: MemoryFontBuilder) -> Self {
22        FontResolverImpl::new(
23            Vec::new(),
24            searcher.book,
25            Default::default(),
26            searcher.fonts,
27            Default::default(),
28        )
29    }
30}
31
32impl MemoryFontBuilder {
33    /// Create a new, empty system searcher.
34    pub fn new() -> Self {
35        Self {
36            book: FontBook::new(),
37            fonts: vec![],
38        }
39    }
40
41    /// Add an in-memory font.
42    pub fn add_memory_font(&mut self, data: Bytes) {
43        for (index, info) in FontInfo::iter(&data).enumerate() {
44            self.book.push(info.clone());
45            self.fonts.push(
46                FontSlot::new_boxed(BufferFontLoader {
47                    buffer: Some(data.clone()),
48                    index: index as u32,
49                })
50                .describe(DataSource::Memory(MemoryDataSource {
51                    name: "<memory>".to_owned(),
52                })),
53            );
54        }
55    }
56}