Skip to main content

read_fonts/generated/
generated_fvar.rs

1// THIS FILE IS AUTOGENERATED.
2// Any changes to this file will be overwritten.
3// For more information about how codegen works, see font-codegen/README.md
4
5#[allow(unused_imports)]
6use crate::codegen_prelude::*;
7
8impl<'a> MinByteRange<'a> for Fvar<'a> {
9    fn min_byte_range(&self) -> Range<usize> {
10        0..self.instance_size_byte_range().end
11    }
12    fn min_table_bytes(&self) -> &'a [u8] {
13        let range = self.min_byte_range();
14        self.offset_data().as_bytes().get(range).unwrap_or_default()
15    }
16}
17
18impl TopLevelTable for Fvar<'_> {
19    /// `fvar`
20    const TAG: Tag = Tag::new(b"fvar");
21}
22
23impl ReadArgs for Fvar<'_> {
24    type Args = ();
25}
26
27impl<'a> FontRead<'a> for Fvar<'a> {
28    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
29        #[allow(clippy::absurd_extreme_comparisons)]
30        if data.len() < Self::MIN_SIZE {
31            return Err(ReadError::OutOfBounds);
32        }
33        let Some(data) = TableDataRef::new(data.as_bytes()) else {
34            return Err(ReadError::OutOfBounds);
35        };
36        Ok(Self { data })
37    }
38}
39
40/// The fields at the start of the table, which are read directly.
41#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
42#[repr(C, packed)]
43#[doc(hidden)]
44pub struct FvarFixedHeader {
45    pub version: BigEndian<MajorMinor>,
46    pub axis_instance_arrays_offset: BigEndian<Offset16>,
47    pub _reserved: BigEndian<u16>,
48    pub axis_count: BigEndian<u16>,
49    pub axis_size: BigEndian<u16>,
50    pub instance_count: BigEndian<u16>,
51    pub instance_size: BigEndian<u16>,
52}
53
54#[allow(clippy::absurd_extreme_comparisons)]
55const _: () = assert!(std::mem::size_of::<FvarFixedHeader>() <= Fvar::MIN_SIZE);
56
57/// The [fvar (Font Variations)](https://docs.microsoft.com/en-us/typography/opentype/spec/fvar) table
58#[derive(Clone)]
59pub struct Fvar<'a> {
60    data: TableDataRef<'a, FvarFixedHeader>,
61}
62
63#[allow(clippy::needless_lifetimes)]
64impl<'a> Fvar<'a> {
65    pub const MIN_SIZE: usize = (MajorMinor::RAW_BYTE_LEN
66        + Offset16::RAW_BYTE_LEN
67        + u16::RAW_BYTE_LEN
68        + u16::RAW_BYTE_LEN
69        + u16::RAW_BYTE_LEN
70        + u16::RAW_BYTE_LEN
71        + u16::RAW_BYTE_LEN);
72    basic_table_impls!(impl_the_methods);
73
74    /// Major version number of the font variations table — set to 1.
75    /// Minor version number of the font variations table — set to 0.
76    pub fn version(&self) -> MajorMinor {
77        self.data.header().version.get()
78    }
79
80    /// Offset in bytes from the beginning of the table to the start of the VariationAxisRecord array. The
81    /// InstanceRecord array directly follows.
82    pub fn axis_instance_arrays_offset(&self) -> Offset16 {
83        self.data.header().axis_instance_arrays_offset.get()
84    }
85
86    /// Attempt to resolve [`axis_instance_arrays_offset`][Self::axis_instance_arrays_offset].
87    pub fn axis_instance_arrays(&self) -> Result<AxisInstanceArrays<'a>, ReadError> {
88        let data = self.offset_data();
89        let args = (
90            self.axis_count(),
91            self.instance_count(),
92            self.instance_size(),
93        );
94        self.axis_instance_arrays_offset()
95            .resolve_with_args(data, args)
96    }
97
98    /// The number of variation axes in the font (the number of records in the axes array).
99    pub fn axis_count(&self) -> u16 {
100        self.data.header().axis_count.get()
101    }
102
103    /// The size in bytes of each VariationAxisRecord — set to 20 (0x0014) for this version.
104    pub fn axis_size(&self) -> u16 {
105        self.data.header().axis_size.get()
106    }
107
108    /// The number of named instances defined in the font (the number of records in the instances array).
109    pub fn instance_count(&self) -> u16 {
110        self.data.header().instance_count.get()
111    }
112
113    /// The size in bytes of each InstanceRecord — set to either axisCount * sizeof(Fixed) + 4, or to axisCount * sizeof(Fixed) + 6.
114    pub fn instance_size(&self) -> u16 {
115        self.data.header().instance_size.get()
116    }
117
118    pub fn version_byte_range(&self) -> Range<usize> {
119        let start = 0;
120        let end = start + MajorMinor::RAW_BYTE_LEN;
121        start..end
122    }
123
124    pub fn axis_instance_arrays_offset_byte_range(&self) -> Range<usize> {
125        let start = self.version_byte_range().end;
126        let end = start + Offset16::RAW_BYTE_LEN;
127        start..end
128    }
129
130    pub fn _reserved_byte_range(&self) -> Range<usize> {
131        let start = self.axis_instance_arrays_offset_byte_range().end;
132        let end = start + u16::RAW_BYTE_LEN;
133        start..end
134    }
135
136    pub fn axis_count_byte_range(&self) -> Range<usize> {
137        let start = self._reserved_byte_range().end;
138        let end = start + u16::RAW_BYTE_LEN;
139        start..end
140    }
141
142    pub fn axis_size_byte_range(&self) -> Range<usize> {
143        let start = self.axis_count_byte_range().end;
144        let end = start + u16::RAW_BYTE_LEN;
145        start..end
146    }
147
148    pub fn instance_count_byte_range(&self) -> Range<usize> {
149        let start = self.axis_size_byte_range().end;
150        let end = start + u16::RAW_BYTE_LEN;
151        start..end
152    }
153
154    pub fn instance_size_byte_range(&self) -> Range<usize> {
155        let start = self.instance_count_byte_range().end;
156        let end = start + u16::RAW_BYTE_LEN;
157        start..end
158    }
159}
160
161const _: () = assert!(FontData::default_data_long_enough(Fvar::MIN_SIZE));
162
163impl Default for Fvar<'_> {
164    fn default() -> Self {
165        Self {
166            data: TableDataRef::new(FontData::default_table_data().as_bytes())
167                .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
168        }
169    }
170}
171
172impl<'a> MinByteRange<'a> for AxisInstanceArrays<'a> {
173    fn min_byte_range(&self) -> Range<usize> {
174        0..self.instances_byte_range().end
175    }
176    fn min_table_bytes(&self) -> &'a [u8] {
177        let range = self.min_byte_range();
178        self.offset_data().as_bytes().get(range).unwrap_or_default()
179    }
180}
181
182impl ReadArgs for AxisInstanceArrays<'_> {
183    type Args = (u16, u16, u16);
184}
185
186impl<'a> FontRead<'a> for AxisInstanceArrays<'a> {
187    fn read_with_args(data: FontData<'a>, args: (u16, u16, u16)) -> Result<Self, ReadError> {
188        let (axis_count, instance_count, instance_size) = args;
189
190        #[allow(clippy::absurd_extreme_comparisons)]
191        if data.len() < Self::MIN_SIZE {
192            return Err(ReadError::OutOfBounds);
193        }
194        let Some(data) = TableDataRef::new(data.as_bytes()) else {
195            return Err(ReadError::OutOfBounds);
196        };
197        Ok(Self {
198            data,
199            axis_count,
200            instance_count,
201            instance_size,
202        })
203    }
204}
205
206impl<'a> AxisInstanceArrays<'a> {
207    /// A constructor that requires additional arguments.
208    ///
209    /// This type requires some external state in order to be
210    /// parsed.
211    pub fn read(
212        data: FontData<'a>,
213        axis_count: u16,
214        instance_count: u16,
215        instance_size: u16,
216    ) -> Result<Self, ReadError> {
217        let args = (axis_count, instance_count, instance_size);
218        Self::read_with_args(data, args)
219    }
220}
221
222/// The fields at the start of the table, which are read directly.
223#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
224#[repr(C, packed)]
225#[doc(hidden)]
226pub struct AxisInstanceArraysFixedHeader {}
227
228#[allow(clippy::absurd_extreme_comparisons)]
229const _: () =
230    assert!(std::mem::size_of::<AxisInstanceArraysFixedHeader>() <= AxisInstanceArrays::MIN_SIZE);
231
232/// Shim table to handle combined axis and instance arrays.
233#[derive(Clone)]
234pub struct AxisInstanceArrays<'a> {
235    data: TableDataRef<'a, AxisInstanceArraysFixedHeader>,
236    axis_count: u16,
237    instance_count: u16,
238    instance_size: u16,
239}
240
241#[allow(clippy::needless_lifetimes)]
242impl<'a> AxisInstanceArrays<'a> {
243    pub const MIN_SIZE: usize = 0;
244    basic_table_impls!(impl_the_methods);
245
246    /// Variation axis record array.
247    pub fn axes(&self) -> &'a [VariationAxisRecord] {
248        let range = self.axes_byte_range();
249        self.offset_data()
250            .read_array(range)
251            .ok()
252            .unwrap_or_default()
253    }
254
255    /// Instance record array.
256    pub fn instances(&self) -> ComputedArray<'a, InstanceRecord<'a>> {
257        let range = self.instances_byte_range();
258        ComputedArray::new(
259            self.offset_data(),
260            range,
261            (self.axis_count(), self.instance_size()),
262        )
263        .unwrap_or_default()
264    }
265
266    pub(crate) fn axis_count(&self) -> u16 {
267        self.axis_count
268    }
269
270    pub(crate) fn instance_count(&self) -> u16 {
271        self.instance_count
272    }
273
274    pub(crate) fn instance_size(&self) -> u16 {
275        self.instance_size
276    }
277
278    pub fn axes_byte_range(&self) -> Range<usize> {
279        let axis_count = self.axis_count();
280        let start = 0;
281        let end = start
282            + (transforms::to_usize(axis_count)).saturating_mul(VariationAxisRecord::RAW_BYTE_LEN);
283        start..end
284    }
285
286    pub fn instances_byte_range(&self) -> Range<usize> {
287        let instance_count = self.instance_count();
288        let start = self.axes_byte_range().end;
289        let end = start
290            + (transforms::to_usize(instance_count)).saturating_mul(
291                <InstanceRecord as ComputeSize>::compute_size((
292                    self.axis_count(),
293                    self.instance_size(),
294                ))
295                .unwrap_or(0),
296            );
297        start..end
298    }
299}
300
301#[allow(clippy::absurd_extreme_comparisons)]
302const _: () = assert!(FontData::default_data_long_enough(
303    AxisInstanceArrays::MIN_SIZE
304));
305
306impl Default for AxisInstanceArrays<'_> {
307    fn default() -> Self {
308        Self {
309            data: TableDataRef::new(FontData::default_table_data().as_bytes())
310                .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
311            axis_count: Default::default(),
312            instance_count: Default::default(),
313            instance_size: Default::default(),
314        }
315    }
316}
317
318/// The [VariationAxisRecord](https://learn.microsoft.com/en-us/typography/opentype/spec/fvar#variationaxisrecord)
319#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, bytemuck :: AnyBitPattern)]
320#[repr(C)]
321#[repr(packed)]
322pub struct VariationAxisRecord {
323    /// Tag identifying the design variation for the axis.
324    pub axis_tag: BigEndian<Tag>,
325    /// The minimum coordinate value for the axis.
326    pub min_value: BigEndian<Fixed>,
327    /// The default coordinate value for the axis.
328    pub default_value: BigEndian<Fixed>,
329    /// The maximum coordinate value for the axis.
330    pub max_value: BigEndian<Fixed>,
331    /// Axis qualifiers — see details below.
332    pub flags: BigEndian<u16>,
333    /// The name ID for entries in the 'name' table that provide a display name for this axis.
334    pub axis_name_id: BigEndian<NameId>,
335}
336
337impl VariationAxisRecord {
338    /// Tag identifying the design variation for the axis.
339    pub fn axis_tag(&self) -> Tag {
340        self.axis_tag.get()
341    }
342
343    /// The minimum coordinate value for the axis.
344    pub fn min_value(&self) -> Fixed {
345        self.min_value.get()
346    }
347
348    /// The default coordinate value for the axis.
349    pub fn default_value(&self) -> Fixed {
350        self.default_value.get()
351    }
352
353    /// The maximum coordinate value for the axis.
354    pub fn max_value(&self) -> Fixed {
355        self.max_value.get()
356    }
357
358    /// Axis qualifiers — see details below.
359    pub fn flags(&self) -> u16 {
360        self.flags.get()
361    }
362
363    /// The name ID for entries in the 'name' table that provide a display name for this axis.
364    pub fn axis_name_id(&self) -> NameId {
365        self.axis_name_id.get()
366    }
367}
368
369impl FixedSize for VariationAxisRecord {
370    const RAW_BYTE_LEN: usize = Tag::RAW_BYTE_LEN
371        + Fixed::RAW_BYTE_LEN
372        + Fixed::RAW_BYTE_LEN
373        + Fixed::RAW_BYTE_LEN
374        + u16::RAW_BYTE_LEN
375        + NameId::RAW_BYTE_LEN;
376}