write_fonts/tables/variations/common_builder.rs
1//! Common code for variation store builders.
2use std::collections::HashMap;
3
4/// The special index indicating no variation.
5pub const NO_VARIATION_INDEX: u32 = 0xFFFFFFFF;
6
7pub(crate) type TemporaryDeltaSetId = u32;
8
9/// A map from the temporary delta set identifiers to the final values.
10///
11/// This is generated when the [ItemVariationStore](crate::tables::variations::ItemVariationStore) is built; afterwards
12/// any tables or records that contain VariationIndex tables need to be remapped.
13#[derive(Clone, Debug, Default)]
14pub struct VarStoreRemapping<T> {
15 pub(crate) map: HashMap<TemporaryDeltaSetId, T>,
16}
17
18/// Remapping temporary delta set identifiers to the final values.
19///
20/// This is called after the [ItemVariationStore](crate::tables::variations::ItemVariationStore) has been built, at which
21/// point any table containing a delta set index needs to be updated to point
22/// to the final value.
23///
24/// This trait should be implemented by any table that contains delta set indices,
25/// as well as for any of table containing such a table, which should recursively
26/// call it on the relevant subtables.
27pub trait RemapVarStore<T> {
28 /// Remap any `TemporaryDeltaSetId`s to their final index values
29 fn remap_variation_indices(&mut self, key_map: &VarStoreRemapping<T>);
30}
31
32impl<T: Clone> VarStoreRemapping<T> {
33 pub(crate) fn set(&mut self, from: TemporaryDeltaSetId, to: T) {
34 self.map.insert(from, to);
35 }
36
37 pub fn get(&self, from: TemporaryDeltaSetId) -> Option<T> {
38 self.map.get(&from).cloned()
39 }
40}