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