tinymist_world/font/
slot.rs1use core::fmt;
2use std::sync::Arc;
3
4use tinymist_std::QueryRef;
5use typst::text::Font;
6
7use crate::debug_loc::DataSource;
8use crate::font::FontLoader;
9
10type FontSlotInner = QueryRef<Option<Font>, (), Box<dyn FontLoader + Send>>;
11
12#[derive(Clone)]
17pub struct FontSlot {
18 inner: Arc<FontSlotInner>,
19 pub description: Option<Arc<DataSource>>,
20}
21
22impl FontSlot {
23 pub fn new<F: FontLoader + Send + 'static>(f: F) -> Self {
25 Self::new_boxed(Box::new(f))
26 }
27
28 pub fn new_boxed(f: Box<dyn FontLoader + Send>) -> Self {
30 Self {
31 inner: Arc::new(FontSlotInner::with_context(f)),
32 description: None,
33 }
34 }
35
36 pub fn new_loaded(f: Option<Font>) -> Self {
38 Self {
39 inner: Arc::new(FontSlotInner::with_value(f)),
40 description: None,
41 }
42 }
43
44 pub fn with_describe(self, desc: DataSource) -> Self {
46 self.with_describe_arc(Arc::new(desc))
47 }
48
49 pub fn with_describe_arc(self, desc: Arc<DataSource>) -> Self {
51 Self {
52 inner: self.inner,
53 description: Some(desc),
54 }
55 }
56
57 pub fn get_or_init(&self) -> Option<Font> {
59 let res = self.inner.compute_with_context(|mut c| Ok(c.load()));
60 res.unwrap().clone()
61 }
62
63 pub fn get_uninitialized(&self) -> Option<Option<Font>> {
68 self.inner
69 .get_uninitialized()
70 .cloned()
71 .map(|e| e.ok().flatten())
72 }
73}
74
75impl fmt::Debug for FontSlot {
76 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
77 f.debug_tuple("FontSlot")
78 .field(&self.get_uninitialized())
79 .finish()
80 }
81}