tinymist_world/font/
slot.rsuse core::fmt;
use std::sync::Arc;
use tinymist_std::QueryRef;
use typst::text::Font;
use crate::debug_loc::DataSource;
use crate::font::FontLoader;
type FontSlotInner = QueryRef<Option<Font>, (), Box<dyn FontLoader + Send>>;
#[derive(Clone)]
pub struct FontSlot {
inner: Arc<FontSlotInner>,
pub description: Option<Arc<DataSource>>,
}
impl FontSlot {
pub fn new<F: FontLoader + Send + 'static>(f: F) -> Self {
Self::new_boxed(Box::new(f))
}
pub fn new_boxed(f: Box<dyn FontLoader + Send>) -> Self {
Self {
inner: Arc::new(FontSlotInner::with_context(f)),
description: None,
}
}
pub fn new_loaded(f: Option<Font>) -> Self {
Self {
inner: Arc::new(FontSlotInner::with_value(f)),
description: None,
}
}
pub fn with_describe(self, desc: DataSource) -> Self {
self.with_describe_arc(Arc::new(desc))
}
pub fn with_describe_arc(self, desc: Arc<DataSource>) -> Self {
Self {
inner: self.inner,
description: Some(desc),
}
}
pub fn get_or_init(&self) -> Option<Font> {
let res = self.inner.compute_with_context(|mut c| Ok(c.load()));
res.unwrap().clone()
}
pub fn get_uninitialized(&self) -> Option<Option<Font>> {
self.inner
.get_uninitialized()
.cloned()
.map(|e| e.ok().flatten())
}
}
impl fmt::Debug for FontSlot {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_tuple("FontSlot")
.field(&self.get_uninitialized())
.finish()
}
}