Skip to main content

read_fonts/tables/
cvar.rs

1//! The [cvar (CVT Variations)](https://learn.microsoft.com/en-us/typography/opentype/spec/cvar)
2//! table.
3
4include!("../../generated/generated_cvar.rs");
5
6use super::variations::{
7    PackedPointNumbers, TupleDelta, TupleVariationCount, TupleVariationData, TupleVariationHeader,
8};
9
10/// Variation data specialized for the CVT variation table.
11pub type CvtVariationData<'a> = TupleVariationData<'a, CvtDelta>;
12
13impl<'a> Cvar<'a> {
14    /// Returns the variation data containing the tuples and deltas for the
15    /// control value table.
16    ///
17    /// This table doesn't contain an axis count field so this must be provided
18    /// by the user and can be read from the `fvar` table.
19    pub fn variation_data(&self, axis_count: u16) -> Option<CvtVariationData<'a>> {
20        let count = self.tuple_variation_count();
21        let data = self.data().ok()?;
22        let header_data = self.raw_tuple_header_data();
23        // if there are shared point numbers, get them now
24        let (shared_point_numbers, serialized_data) = if count.shared_point_numbers() {
25            let (packed, data) = PackedPointNumbers::split_off_front(data);
26            (Some(packed), data)
27        } else {
28            (None, data)
29        };
30        Some(CvtVariationData {
31            tuple_count: count,
32            axis_count,
33            shared_tuples: None,
34            shared_point_numbers,
35            header_data,
36            serialized_data,
37            _marker: std::marker::PhantomData,
38        })
39    }
40
41    /// Computes the accumulated deltas for the given set of normalized
42    /// coordinates and stores them in `deltas`.
43    ///
44    /// The `axis_count` parameter expects the value from the `fvar`
45    /// table.
46    ///
47    /// The `deltas` slice should have a length greater than or equal
48    /// to the number of values in the `cvt` table. The values are
49    /// computed in 16.16 format.
50    pub fn deltas(&self, axis_count: u16, coords: &[F2Dot14], deltas: &mut [i32]) -> Option<()> {
51        let var_data = self.variation_data(axis_count)?;
52        for (tuple, scalar) in var_data.active_tuples_at(coords) {
53            for delta in tuple.deltas() {
54                let ix = delta.position as usize;
55                if let Some(value) = deltas.get_mut(ix) {
56                    *value += delta.apply_scalar(scalar).to_bits();
57                }
58            }
59        }
60        Some(())
61    }
62
63    fn raw_tuple_header_data(&self) -> FontData<'a> {
64        let range = self.tuple_variation_headers_byte_range();
65        self.offset_data().split_off(range.start).unwrap()
66    }
67}
68
69/// Delta for an entry in the control value table.
70#[derive(Clone, Copy, Debug, PartialEq, Eq)]
71pub struct CvtDelta {
72    /// The index in the CVT.
73    pub position: u16,
74    /// The delta to apply to the value in the CVT.
75    pub value: i32,
76}
77
78impl CvtDelta {
79    /// Applies a tuple scalar to this delta.
80    pub fn apply_scalar(self, scalar: Fixed) -> Fixed {
81        Fixed::from_i32(self.value) * scalar
82    }
83}
84
85impl TupleDelta for CvtDelta {
86    fn is_point() -> bool {
87        false
88    }
89
90    fn new(position: u16, x: i32, _y: i32) -> Self {
91        Self { position, value: x }
92    }
93}
94
95#[cfg(test)]
96mod tests {
97    use font_types::F2Dot14;
98
99    use crate::{FontRef, TableProvider};
100
101    #[test]
102    fn scaled_deltas() {
103        let font = FontRef::new(font_test_data::CVAR).unwrap();
104        // Elements are ([coords], [deltas]) where deltas are fixed point
105        // values.
106        // These were generated by fancy printf debugging in FreeType.
107        let cases = &[
108            (
109                [0.5, 0.5],
110                [
111                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
112                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
113                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 720896, 3276800, 1179648, 0, 0, 0, 0, 0,
114                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 622592, 0, 1179648, 0, 0, 0, 0, 0, 622592,
115                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
116                ],
117            ),
118            (
119                [-0.5, 0.5],
120                [
121                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
122                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
123                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1441792, -2162688, -1277952, 0, 0, 0,
124                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -917504, 0, -1277952, 0, 0, 0, 0, 0,
125                    -720896, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
126                    0, 0, 0,
127                ],
128            ),
129            (
130                [0.5, -0.5],
131                [
132                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
133                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
134                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1900544, 2621440, 2129920, 0, 0, 0, 0,
135                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 360448, 0, 1015808, 0, 0, 0, 0, 0,
136                    524288, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
137                    0, 0,
138                ],
139            ),
140            (
141                [-0.5, -0.5],
142                [
143                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
144                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
145                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1212416, -2293760, -1130496, 0, 0, 0,
146                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1097728, 0, -1277952, 0, 0, 0, 0, 0,
147                    -737280, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
148                    0, 0, 0,
149                ],
150            ),
151            (
152                [-1.0, -1.0],
153                [
154                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
155                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
156                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -2490368, -4325376, -2490368, 0, 0, 0,
157                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1835008, 0, -2555904, 0, 0, 0, 0, 0,
158                    -1441792, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
159                    0, 0, 0,
160                ],
161            ),
162            (
163                [1.0, 1.0],
164                [
165                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
166                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
167                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1441792, 6553600, 2359296, 0, 0, 0, 0,
168                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1245184, 0, 2359296, 0, 0, 0, 0, 0,
169                    1245184, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
170                    0, 0, 0,
171                ],
172            ),
173            (
174                [-1.0, 1.0],
175                [
176                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
177                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
178                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -2883584, -4325376, -2555904, 0, 0, 0,
179                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1835008, 0, -2555904, 0, 0, 0, 0, 0,
180                    -1441792, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
181                    0, 0, 0,
182                ],
183            ),
184            (
185                [1.0, -1.0],
186                [
187                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
188                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
189                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5636096, 4456448, 5636096, 0, 0, 0, 0,
190                    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 917504, 0, 1703936, 0, 0, 0, 0, 0,
191                    917504, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
192                    0, 0,
193                ],
194            ),
195        ];
196        let cvar = font.cvar().unwrap();
197        let axis_count = font.fvar().unwrap().axis_count();
198        let cvar_data = cvar.variation_data(axis_count).unwrap();
199        for (coords, expected_deltas) in cases {
200            let coords = coords.map(F2Dot14::from_f32);
201            let mut deltas = vec![0; expected_deltas.len()];
202            for tuple in cvar_data.tuples() {
203                let Some(scalar) = tuple.compute_scalar(&coords) else {
204                    continue;
205                };
206                for delta in tuple.deltas() {
207                    let scaled_delta = delta.apply_scalar(scalar);
208                    deltas[delta.position as usize] += scaled_delta.to_bits();
209                }
210            }
211            assert_eq!(&deltas, expected_deltas);
212        }
213    }
214
215    #[test]
216    fn raw_tuple_deltas() {
217        let font = FontRef::new(font_test_data::CVAR).unwrap();
218        let cvar = font.cvar().unwrap();
219        let axis_count = font.fvar().unwrap().axis_count();
220        let cvar_data = cvar.variation_data(axis_count).unwrap();
221        // An array of slices of (point number, delta) pairs, one for each
222        // tuple.
223        // These were taken directly from the ttx
224        let expected = [
225            &[(65, 8), (66, -8), (67, 8), (85, -11), (87, 0), (93, -1)],
226            &[(65, -2), (66, 8), (67, -7), (85, 11), (87, 0), (93, 1)],
227            &[(65, 56), (66, -24), (67, 42), (85, 6), (87, -10), (93, -4)],
228            &[
229                (65, -44),
230                (66, -66),
231                (67, -39),
232                (85, -28),
233                (87, -39),
234                (93, -22),
235            ],
236            &[(65, 22), (66, 100), (67, 36), (85, 19), (87, 36), (93, 19)],
237            &[(65, 8), (66, 0), (67, 8), (85, -43), (87, -49), (93, -32)],
238            &[(65, -8), (66, 0), (67, -8), (85, 11), (87, 9), (93, 1)],
239            &[(65, -80), (66, 0), (67, -90), (85, -6), (87, -47), (93, 4)],
240            &[(65, -16), (66, 0), (67, -21), (85, 28), (87, 39), (93, 22)],
241            &[
242                (65, -46),
243                (66, 0),
244                (67, -22),
245                (85, -19),
246                (87, 35),
247                (93, -19),
248            ],
249            &[(65, 2), (66, 0), (67, 7), (85, -11), (87, -9), (93, -1)],
250        ];
251        let mut count = 0;
252        for (tuple, expected) in cvar_data.tuples().zip(&expected) {
253            count += 1;
254            let deltas = tuple
255                .deltas()
256                .map(|delta| (delta.position, delta.value))
257                .collect::<Vec<_>>();
258            assert_eq!(&deltas, expected);
259        }
260        assert_eq!(count, expected.len());
261    }
262}