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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
// THIS FILE IS AUTOGENERATED.
// Any changes to this file will be overwritten.
// For more information about how codegen works, see font-codegen/README.md

#[allow(unused_imports)]
use crate::codegen_prelude::*;

/// The [fvar (Font Variations)](https://docs.microsoft.com/en-us/typography/opentype/spec/fvar) table
#[derive(Debug, Clone, Copy)]
#[doc(hidden)]
pub struct FvarMarker {}

impl FvarMarker {
    fn version_byte_range(&self) -> Range<usize> {
        let start = 0;
        start..start + MajorMinor::RAW_BYTE_LEN
    }
    fn axis_instance_arrays_offset_byte_range(&self) -> Range<usize> {
        let start = self.version_byte_range().end;
        start..start + Offset16::RAW_BYTE_LEN
    }
    fn _reserved_byte_range(&self) -> Range<usize> {
        let start = self.axis_instance_arrays_offset_byte_range().end;
        start..start + u16::RAW_BYTE_LEN
    }
    fn axis_count_byte_range(&self) -> Range<usize> {
        let start = self._reserved_byte_range().end;
        start..start + u16::RAW_BYTE_LEN
    }
    fn axis_size_byte_range(&self) -> Range<usize> {
        let start = self.axis_count_byte_range().end;
        start..start + u16::RAW_BYTE_LEN
    }
    fn instance_count_byte_range(&self) -> Range<usize> {
        let start = self.axis_size_byte_range().end;
        start..start + u16::RAW_BYTE_LEN
    }
    fn instance_size_byte_range(&self) -> Range<usize> {
        let start = self.instance_count_byte_range().end;
        start..start + u16::RAW_BYTE_LEN
    }
}

impl TopLevelTable for Fvar<'_> {
    /// `fvar`
    const TAG: Tag = Tag::new(b"fvar");
}

impl<'a> FontRead<'a> for Fvar<'a> {
    fn read(data: FontData<'a>) -> Result<Self, ReadError> {
        let mut cursor = data.cursor();
        cursor.advance::<MajorMinor>();
        cursor.advance::<Offset16>();
        cursor.advance::<u16>();
        cursor.advance::<u16>();
        cursor.advance::<u16>();
        cursor.advance::<u16>();
        cursor.advance::<u16>();
        cursor.finish(FvarMarker {})
    }
}

/// The [fvar (Font Variations)](https://docs.microsoft.com/en-us/typography/opentype/spec/fvar) table
pub type Fvar<'a> = TableRef<'a, FvarMarker>;

impl<'a> Fvar<'a> {
    /// Major version number of the font variations table — set to 1.
    /// Minor version number of the font variations table — set to 0.
    pub fn version(&self) -> MajorMinor {
        let range = self.shape.version_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// Offset in bytes from the beginning of the table to the start of the VariationAxisRecord array. The
    /// InstanceRecord array directly follows.
    pub fn axis_instance_arrays_offset(&self) -> Offset16 {
        let range = self.shape.axis_instance_arrays_offset_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// Attempt to resolve [`axis_instance_arrays_offset`][Self::axis_instance_arrays_offset].
    pub fn axis_instance_arrays(&self) -> Result<AxisInstanceArrays<'a>, ReadError> {
        let data = self.data;
        let args = (
            self.axis_count(),
            self.instance_count(),
            self.instance_size(),
        );
        self.axis_instance_arrays_offset()
            .resolve_with_args(data, &args)
    }

    /// The number of variation axes in the font (the number of records in the axes array).
    pub fn axis_count(&self) -> u16 {
        let range = self.shape.axis_count_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// The size in bytes of each VariationAxisRecord — set to 20 (0x0014) for this version.
    pub fn axis_size(&self) -> u16 {
        let range = self.shape.axis_size_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// The number of named instances defined in the font (the number of records in the instances array).
    pub fn instance_count(&self) -> u16 {
        let range = self.shape.instance_count_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// The size in bytes of each InstanceRecord — set to either axisCount * sizeof(Fixed) + 4, or to axisCount * sizeof(Fixed) + 6.
    pub fn instance_size(&self) -> u16 {
        let range = self.shape.instance_size_byte_range();
        self.data.read_at(range.start).unwrap()
    }
}

#[cfg(feature = "traversal")]
impl<'a> SomeTable<'a> for Fvar<'a> {
    fn type_name(&self) -> &str {
        "Fvar"
    }
    fn get_field(&self, idx: usize) -> Option<Field<'a>> {
        match idx {
            0usize => Some(Field::new("version", self.version())),
            1usize => Some(Field::new(
                "axis_instance_arrays_offset",
                FieldType::offset(
                    self.axis_instance_arrays_offset(),
                    self.axis_instance_arrays(),
                ),
            )),
            2usize => Some(Field::new("axis_count", self.axis_count())),
            3usize => Some(Field::new("axis_size", self.axis_size())),
            4usize => Some(Field::new("instance_count", self.instance_count())),
            5usize => Some(Field::new("instance_size", self.instance_size())),
            _ => None,
        }
    }
}

#[cfg(feature = "traversal")]
impl<'a> std::fmt::Debug for Fvar<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        (self as &dyn SomeTable<'a>).fmt(f)
    }
}

/// Shim table to handle combined axis and instance arrays.
#[derive(Debug, Clone, Copy)]
#[doc(hidden)]
pub struct AxisInstanceArraysMarker {
    axis_count: u16,
    instance_size: u16,
    axes_byte_len: usize,
    instances_byte_len: usize,
}

impl AxisInstanceArraysMarker {
    fn axes_byte_range(&self) -> Range<usize> {
        let start = 0;
        start..start + self.axes_byte_len
    }
    fn instances_byte_range(&self) -> Range<usize> {
        let start = self.axes_byte_range().end;
        start..start + self.instances_byte_len
    }
}

impl ReadArgs for AxisInstanceArrays<'_> {
    type Args = (u16, u16, u16);
}

impl<'a> FontReadWithArgs<'a> for AxisInstanceArrays<'a> {
    fn read_with_args(data: FontData<'a>, args: &(u16, u16, u16)) -> Result<Self, ReadError> {
        let (axis_count, instance_count, instance_size) = *args;
        let mut cursor = data.cursor();
        let axes_byte_len = axis_count as usize * VariationAxisRecord::RAW_BYTE_LEN;
        cursor.advance_by(axes_byte_len);
        let instances_byte_len = instance_count as usize
            * <InstanceRecord as ComputeSize>::compute_size(&(axis_count, instance_size));
        cursor.advance_by(instances_byte_len);
        cursor.finish(AxisInstanceArraysMarker {
            axis_count,
            instance_size,
            axes_byte_len,
            instances_byte_len,
        })
    }
}

impl<'a> AxisInstanceArrays<'a> {
    /// A constructor that requires additional arguments.
    ///
    /// This type requires some external state in order to be
    /// parsed.
    pub fn read(
        data: FontData<'a>,
        axis_count: u16,
        instance_count: u16,
        instance_size: u16,
    ) -> Result<Self, ReadError> {
        let args = (axis_count, instance_count, instance_size);
        Self::read_with_args(data, &args)
    }
}

/// Shim table to handle combined axis and instance arrays.
pub type AxisInstanceArrays<'a> = TableRef<'a, AxisInstanceArraysMarker>;

impl<'a> AxisInstanceArrays<'a> {
    /// Variation axis record array.
    pub fn axes(&self) -> &'a [VariationAxisRecord] {
        let range = self.shape.axes_byte_range();
        self.data.read_array(range).unwrap()
    }

    /// Instance record array.
    pub fn instances(&self) -> ComputedArray<'a, InstanceRecord<'a>> {
        let range = self.shape.instances_byte_range();
        self.data
            .read_with_args(range, &(self.axis_count(), self.instance_size()))
            .unwrap()
    }

    pub(crate) fn axis_count(&self) -> u16 {
        self.shape.axis_count
    }

    pub(crate) fn instance_size(&self) -> u16 {
        self.shape.instance_size
    }
}

#[cfg(feature = "traversal")]
impl<'a> SomeTable<'a> for AxisInstanceArrays<'a> {
    fn type_name(&self) -> &str {
        "AxisInstanceArrays"
    }
    fn get_field(&self, idx: usize) -> Option<Field<'a>> {
        match idx {
            0usize => Some(Field::new(
                "axes",
                traversal::FieldType::array_of_records(
                    stringify!(VariationAxisRecord),
                    self.axes(),
                    self.offset_data(),
                ),
            )),
            1usize => Some(Field::new(
                "instances",
                traversal::FieldType::computed_array(
                    "InstanceRecord",
                    self.instances(),
                    self.offset_data(),
                ),
            )),
            _ => None,
        }
    }
}

#[cfg(feature = "traversal")]
impl<'a> std::fmt::Debug for AxisInstanceArrays<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        (self as &dyn SomeTable<'a>).fmt(f)
    }
}

/// The [VariationAxisRecord](https://learn.microsoft.com/en-us/typography/opentype/spec/fvar#variationaxisrecord)
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, bytemuck :: AnyBitPattern)]
#[repr(C)]
#[repr(packed)]
pub struct VariationAxisRecord {
    /// Tag identifying the design variation for the axis.
    pub axis_tag: BigEndian<Tag>,
    /// The minimum coordinate value for the axis.
    pub min_value: BigEndian<Fixed>,
    /// The default coordinate value for the axis.
    pub default_value: BigEndian<Fixed>,
    /// The maximum coordinate value for the axis.
    pub max_value: BigEndian<Fixed>,
    /// Axis qualifiers — see details below.
    pub flags: BigEndian<u16>,
    /// The name ID for entries in the 'name' table that provide a display name for this axis.
    pub axis_name_id: BigEndian<NameId>,
}

impl VariationAxisRecord {
    /// Tag identifying the design variation for the axis.
    pub fn axis_tag(&self) -> Tag {
        self.axis_tag.get()
    }

    /// The minimum coordinate value for the axis.
    pub fn min_value(&self) -> Fixed {
        self.min_value.get()
    }

    /// The default coordinate value for the axis.
    pub fn default_value(&self) -> Fixed {
        self.default_value.get()
    }

    /// The maximum coordinate value for the axis.
    pub fn max_value(&self) -> Fixed {
        self.max_value.get()
    }

    /// Axis qualifiers — see details below.
    pub fn flags(&self) -> u16 {
        self.flags.get()
    }

    /// The name ID for entries in the 'name' table that provide a display name for this axis.
    pub fn axis_name_id(&self) -> NameId {
        self.axis_name_id.get()
    }
}

impl FixedSize for VariationAxisRecord {
    const RAW_BYTE_LEN: usize = Tag::RAW_BYTE_LEN
        + Fixed::RAW_BYTE_LEN
        + Fixed::RAW_BYTE_LEN
        + Fixed::RAW_BYTE_LEN
        + u16::RAW_BYTE_LEN
        + NameId::RAW_BYTE_LEN;
}

#[cfg(feature = "traversal")]
impl<'a> SomeRecord<'a> for VariationAxisRecord {
    fn traverse(self, data: FontData<'a>) -> RecordResolver<'a> {
        RecordResolver {
            name: "VariationAxisRecord",
            get_field: Box::new(move |idx, _data| match idx {
                0usize => Some(Field::new("axis_tag", self.axis_tag())),
                1usize => Some(Field::new("min_value", self.min_value())),
                2usize => Some(Field::new("default_value", self.default_value())),
                3usize => Some(Field::new("max_value", self.max_value())),
                4usize => Some(Field::new("flags", self.flags())),
                5usize => Some(Field::new("axis_name_id", self.axis_name_id())),
                _ => None,
            }),
            data,
        }
    }
}