swash/shape/
cache.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
use super::at::FeatureStore;
use super::engine::EngineMetadata;
use super::internal::var::Fvar;
use crate::{charmap::CharmapProxy, metrics::MetricsProxy, FontRef};

pub type Epoch = u64;

pub struct FontEntry {
    pub metrics: MetricsProxy,
    pub charmap: CharmapProxy,
    pub coord_count: u16,
    pub metadata: EngineMetadata,
}

impl FontEntry {
    pub fn new(font: &FontRef) -> Self {
        Self {
            metrics: MetricsProxy::from_font(font),
            charmap: CharmapProxy::from_font(font),
            coord_count: Fvar::from_font(font)
                .map(|fvar| fvar.axis_count())
                .unwrap_or(0),
            metadata: EngineMetadata::from_font(font),
        }
    }
}

pub struct FeatureEntry {
    pub epoch: Epoch,
    pub id: [u64; 2],
    pub coords: Vec<i16>,
    pub tags: [u32; 4],
    pub store: FeatureStore,
}

pub struct FeatureCache {
    entries: Vec<FeatureEntry>,
    epoch: Epoch,
    max_entries: usize,
}

pub enum FeatureCacheEntry<'a> {
    New(&'a mut FeatureStore),
    Present(&'a mut FeatureStore),
}

impl FeatureCache {
    pub fn new(max_entries: usize) -> Self {
        Self {
            entries: Default::default(),
            epoch: 0,
            max_entries,
        }
    }

    pub fn entry<'a>(
        &'a mut self,
        id: [u64; 2],
        coords: &[i16],
        has_feature_vars: bool,
        tags: &[u32; 4],
    ) -> FeatureCacheEntry<'a> {
        match self.find_entry(id, coords, has_feature_vars, tags) {
            (true, index) => {
                let entry = &mut self.entries[index];
                entry.epoch = self.epoch;
                FeatureCacheEntry::Present(&mut entry.store)
            }
            (false, index) => {
                self.epoch += 1;
                let entry = &mut self.entries[index];
                entry.epoch = self.epoch;
                FeatureCacheEntry::New(&mut entry.store)
            }
        }
    }

    fn find_entry(
        &mut self,
        id: [u64; 2],
        coords: &[i16],
        has_feature_vars: bool,
        tags: &[u32; 4],
    ) -> (bool, usize) {
        let epoch = self.epoch;
        let mut lowest_serial = epoch;
        let mut lowest_index = 0;
        for (i, entry) in self.entries.iter().enumerate() {
            if entry.id == id && &entry.tags == tags {
                if has_feature_vars && coords != &entry.coords[..] {
                    continue;
                }
                return (true, i);
            }
            if entry.epoch < lowest_serial {
                lowest_serial = entry.epoch;
                lowest_index = i;
            }
        }
        if self.entries.len() < self.max_entries {
            lowest_index = self.entries.len();
            self.entries.push(FeatureEntry {
                epoch,
                id,
                coords: Vec::from(coords),
                store: FeatureStore::default(),
                tags: *tags,
            });
        } else {
            let entry = &mut self.entries[lowest_index];
            entry.epoch = epoch;
            entry.id = id;
            entry.coords.clear();
            entry.coords.extend_from_slice(coords);
            entry.store.clear();
            entry.tags = *tags;
        }
        (false, lowest_index)
    }
}