Skip to main content

fret_runtime/
font_catalog_cache.rs

1use std::sync::Arc;
2
3use crate::FontCatalog;
4
5/// Cached font family name list for efficient UI rendering.
6///
7/// This is intended to be stored as a global and refreshed by runners when the renderer's font
8/// backend changes (e.g. dynamic font injection on web or user-installed fonts on desktop).
9#[derive(Debug, Clone, PartialEq, Eq)]
10pub struct FontCatalogCache {
11    pub revision: u64,
12    families: Arc<[Arc<str>]>,
13}
14
15impl Default for FontCatalogCache {
16    fn default() -> Self {
17        Self {
18            revision: 0,
19            families: Arc::<[Arc<str>]>::from([]),
20        }
21    }
22}
23
24impl FontCatalogCache {
25    pub fn families(&self) -> &[Arc<str>] {
26        &self.families
27    }
28
29    pub fn families_arc(&self) -> Arc<[Arc<str>]> {
30        self.families.clone()
31    }
32
33    pub fn from_catalog(catalog: &FontCatalog) -> Self {
34        Self::from_families(catalog.revision, &catalog.families)
35    }
36
37    pub fn from_families(revision: u64, families: &[String]) -> Self {
38        let families: Vec<Arc<str>> = families.iter().map(|s| Arc::from(s.as_str())).collect();
39        Self {
40            revision,
41            families: Arc::from(families),
42        }
43    }
44}