Skip to main content

write_fonts/tables/
gdef.rs

1//! the [GDEF] table
2//!
3//! [GDEF]: https://docs.microsoft.com/en-us/typography/opentype/spec/gdef
4
5use types::MajorMinor;
6
7use crate::tables::layout::VariationIndex;
8
9use super::{
10    layout::{ClassDef, CoverageTable, DeviceOrVariationIndex},
11    variations::{
12        common_builder::RemapVarStore, ivs_builder::VariationIndexRemapping, ItemVariationStore,
13    },
14};
15
16include!("../../generated/generated_gdef.rs");
17
18impl Gdef {
19    fn compute_version(&self) -> MajorMinor {
20        if self.item_var_store.is_some() {
21            MajorMinor::VERSION_1_3
22        } else if self.mark_glyph_sets_def.is_some() {
23            MajorMinor::VERSION_1_2
24        } else {
25            MajorMinor::VERSION_1_0
26        }
27    }
28}
29
30impl RemapVarStore<VariationIndex> for Gdef {
31    fn remap_variation_indices(&mut self, key_map: &VariationIndexRemapping) {
32        if let Some(ligs) = self.lig_caret_list.as_mut() {
33            ligs.remap_variation_indices(key_map);
34        }
35    }
36}
37
38impl RemapVarStore<VariationIndex> for LigCaretList {
39    fn remap_variation_indices(&mut self, key_map: &VariationIndexRemapping) {
40        self.lig_glyphs.iter_mut().for_each(|lig| {
41            lig.caret_values
42                .iter_mut()
43                .for_each(|caret| caret.remap_variation_indices(key_map))
44        })
45    }
46}
47
48impl RemapVarStore<VariationIndex> for CaretValue {
49    fn remap_variation_indices(&mut self, key_map: &VariationIndexRemapping) {
50        if let CaretValue::Format3(table) = self {
51            table.remap_variation_indices(key_map)
52        }
53    }
54}
55
56impl RemapVarStore<VariationIndex> for CaretValueFormat3 {
57    fn remap_variation_indices(&mut self, key_map: &VariationIndexRemapping) {
58        self.device.remap_variation_indices(key_map)
59    }
60}
61
62#[cfg(test)]
63mod tests {
64    use super::*;
65
66    #[test]
67    fn var_store_without_glyph_sets() {
68        // this should compile, and version should be 1.3
69        let gdef = Gdef {
70            item_var_store: ItemVariationStore::default().into(),
71            ..Default::default()
72        };
73
74        assert_eq!(gdef.compute_version(), MajorMinor::VERSION_1_3);
75        let _dumped = crate::write::dump_table(&gdef).unwrap();
76        let data = FontData::new(&_dumped);
77        let loaded = read_fonts::tables::gdef::Gdef::read(data).unwrap();
78
79        assert_eq!(loaded.version(), MajorMinor::VERSION_1_3);
80        assert!(!loaded.item_var_store_offset().unwrap().is_null());
81    }
82}