| Name | Message | Date |
|---|---|---|
| 📄 mod.rs | 1 month ago | |
| 📄 receipt_world.rs | 1 month ago |
📄
src/typst_renderer/receipt_world.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
use std::{cell::LazyCell, sync::Arc}; use typst::{ Library, LibraryExt, World, diag::{FileError, FileResult}, foundations::{Bytes, Datetime, Dict, IntoValue}, syntax::{FileId, Source}, text::{Font, FontBook}, utils::LazyHash, }; const TEMPLATE_STRING: &str = include_str!("../../assets/template.typ"); const LIBREBASKERVILLE_REGULAR_BYTES: &[u8] = include_bytes!("../../assets/LibreBaskerville-Regular.ttf"); const CASCADIACODE_REGULAR_BYTES: &[u8] = include_bytes!("../../assets/CascadiaCode-Regular.ttf"); thread_local! { static TEMPLATE_SOURCE: LazyCell<Source> = LazyCell::new(|| Source::detached(TEMPLATE_STRING)); static FONTS: LazyCell<(Arc<LazyHash<FontBook>>, Vec<Font>)> = LazyCell::new(|| { let fonts = vec![ Font::new(Bytes::new(LIBREBASKERVILLE_REGULAR_BYTES), 0).unwrap(), Font::new(Bytes::new(CASCADIACODE_REGULAR_BYTES), 0).unwrap(), ]; let font_book = Arc::new(LazyHash::new(FontBook::from_fonts(&fonts))); (font_book, fonts) }); } pub struct ReceiptWorld { library: LazyHash<Library>, font_book: Arc<LazyHash<FontBook>>, } impl ReceiptWorld { pub fn new(content: &str) -> Self { let mut input = Dict::new(); input.insert("content".into(), content.into_value()); let font_book = FONTS.with(|fonts| fonts.0.clone()); Self { library: LazyHash::new(Library::builder().with_inputs(input).build()), font_book, } } } impl World for ReceiptWorld { fn library(&self) -> &LazyHash<Library> { &self.library } fn book(&self) -> &LazyHash<FontBook> { &self.font_book } fn main(&self) -> FileId { TEMPLATE_SOURCE.with(|source| source.id()) } fn source(&self, id: FileId) -> FileResult<Source> { if id == self.main() { Ok(TEMPLATE_SOURCE.with(|source| (*source).clone())) } else { Err(FileError::NotFound( id.vpath().as_rootless_path().to_path_buf(), )) } } fn file(&self, id: FileId) -> FileResult<Bytes> { Err(FileError::NotFound( id.vpath().as_rootless_path().to_path_buf(), )) } fn font(&self, index: usize) -> Option<Font> { FONTS.with(|fonts| fonts.1.get(index).cloned()) } fn today(&self, _offset: Option<i64>) -> Option<Datetime> { None } }