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>>,
21}
22
23impl FontSlot {
24 pub fn new<F: FontLoader + Send + 'static>(f: F) -> Self {
26 Self::new_boxed(Box::new(f))
27 }
28
29 pub fn new_boxed(f: Box<dyn FontLoader + Send>) -> Self {
31 Self {
32 inner: Arc::new(FontSlotInner::with_context(f)),
33 description: None,
34 }
35 }
36
37 pub fn new_loaded(f: Option<Font>) -> Self {
39 Self {
40 inner: Arc::new(FontSlotInner::with_value(f)),
41 description: None,
42 }
43 }
44
45 pub fn with_describe(self, desc: DataSource) -> Self {
47 self.with_describe_arc(Arc::new(desc))
48 }
49
50 pub fn with_describe_arc(self, desc: Arc<DataSource>) -> Self {
52 Self {
53 inner: self.inner,
54 description: Some(desc),
55 }
56 }
57
58 pub fn get_or_init(&self) -> Option<Font> {
60 let res = self.inner.compute_with_context(|mut c| Ok(c.load()));
61 res.unwrap().clone()
62 }
63
64 pub fn get_uninitialized(&self) -> Option<Option<Font>> {
69 self.inner
70 .get_uninitialized()
71 .cloned()
72 .map(|e| e.ok().flatten())
73 }
74}
75
76impl fmt::Debug for FontSlot {
77 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
78 f.debug_tuple("FontSlot")
79 .field(&self.get_uninitialized())
80 .finish()
81 }
82}