tinymist_world/font/
cache.rs

1//! The cache of the font info.
2
3use serde::{Deserialize, Serialize};
4use sha2::{Digest, Sha256};
5use typst::text::FontInfo;
6
7/// The condition of the cache.
8#[derive(Serialize, Deserialize)]
9#[serde(tag = "t", content = "v")]
10pub enum CacheCondition {
11    /// The sha256 hash of the data.
12    Sha256(String),
13}
14
15/// The cache of the font info.
16#[derive(Serialize, Deserialize)]
17pub struct FontInfoCache {
18    /// The font info.
19    pub info: Vec<FontInfo>,
20    /// The conditions of the cache.
21    pub conditions: Vec<CacheCondition>,
22}
23
24impl FontInfoCache {
25    /// Creates a new font info cache from the data.
26    pub fn from_data(buffer: &[u8]) -> Self {
27        let hash = hex::encode(Sha256::digest(buffer));
28
29        FontInfoCache {
30            info: FontInfo::iter(buffer).collect(),
31            conditions: vec![CacheCondition::Sha256(hash)],
32        }
33    }
34}