Skip to main content

read_fonts/generated/
generated_avar.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 Avar<'a> {
9    fn min_byte_range(&self) -> Range<usize> {
10        0..self.axis_segment_maps_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 Avar<'_> {
19    /// `avar`
20    const TAG: Tag = Tag::new(b"avar");
21}
22
23impl ReadArgs for Avar<'_> {
24    type Args = ();
25}
26
27impl<'a> FontRead<'a> for Avar<'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 AvarFixedHeader {
45    pub version: BigEndian<MajorMinor>,
46    pub _reserved: BigEndian<u16>,
47    pub axis_count: BigEndian<u16>,
48}
49
50#[allow(clippy::absurd_extreme_comparisons)]
51const _: () = assert!(std::mem::size_of::<AvarFixedHeader>() <= Avar::MIN_SIZE);
52
53/// The [avar (Axis Variations)](https://docs.microsoft.com/en-us/typography/opentype/spec/avar) table
54#[derive(Clone)]
55pub struct Avar<'a> {
56    data: TableDataRef<'a, AvarFixedHeader>,
57}
58
59#[allow(clippy::needless_lifetimes)]
60impl<'a> Avar<'a> {
61    pub const MIN_SIZE: usize = (MajorMinor::RAW_BYTE_LEN + u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN);
62    basic_table_impls!(impl_the_methods);
63
64    /// Major version number of the axis variations table — set to 1 or 2.
65    /// Minor version number of the axis variations table — set to 0.
66    pub fn version(&self) -> MajorMinor {
67        self.data.header().version.get()
68    }
69
70    /// The number of variation axes for this font. This must be the same number as axisCount in the 'fvar' table.
71    pub fn axis_count(&self) -> u16 {
72        self.data.header().axis_count.get()
73    }
74
75    /// The segment maps array — one segment map for each axis, in the order of axes specified in the 'fvar' table.
76    pub fn axis_segment_maps(&self) -> VarLenArray<'a, SegmentMaps<'a>> {
77        let range = self.axis_segment_maps_byte_range();
78        self.offset_data()
79            .split_off(range.start)
80            .and_then(|d| VarLenArray::read(d).ok())
81            .unwrap_or_default()
82    }
83
84    /// Offset to DeltaSetIndexMap table (may be NULL).
85    pub fn axis_index_map_offset(&self) -> Option<Nullable<Offset32>> {
86        let range = self.axis_index_map_offset_byte_range();
87        (!range.is_empty())
88            .then(|| self.offset_data().read_at(range.start).ok())
89            .flatten()
90    }
91
92    /// Attempt to resolve [`axis_index_map_offset`][Self::axis_index_map_offset].
93    pub fn axis_index_map(&self) -> Option<Result<DeltaSetIndexMap<'a>, ReadError>> {
94        let data = self.offset_data();
95        self.axis_index_map_offset().map(|x| x.resolve(data))?
96    }
97
98    /// Offset to ItemVariationStore (may be NULL).
99    pub fn var_store_offset(&self) -> Option<Nullable<Offset32>> {
100        let range = self.var_store_offset_byte_range();
101        (!range.is_empty())
102            .then(|| self.offset_data().read_at(range.start).ok())
103            .flatten()
104    }
105
106    /// Attempt to resolve [`var_store_offset`][Self::var_store_offset].
107    pub fn var_store(&self) -> Option<Result<ItemVariationStore<'a>, ReadError>> {
108        let data = self.offset_data();
109        self.var_store_offset().map(|x| x.resolve(data))?
110    }
111
112    pub fn version_byte_range(&self) -> Range<usize> {
113        let start = 0;
114        let end = start + MajorMinor::RAW_BYTE_LEN;
115        start..end
116    }
117
118    pub fn _reserved_byte_range(&self) -> Range<usize> {
119        let start = self.version_byte_range().end;
120        let end = start + u16::RAW_BYTE_LEN;
121        start..end
122    }
123
124    pub fn axis_count_byte_range(&self) -> Range<usize> {
125        let start = self._reserved_byte_range().end;
126        let end = start + u16::RAW_BYTE_LEN;
127        start..end
128    }
129
130    pub fn axis_segment_maps_byte_range(&self) -> Range<usize> {
131        let axis_count = self.axis_count();
132        let start = self.axis_count_byte_range().end;
133        let end = start + {
134            let data = self.offset_data().split_off(start).unwrap_or_default();
135            <SegmentMaps as VarSize>::total_len_for_count(data, transforms::to_usize(axis_count))
136                .unwrap_or(0)
137        };
138        start..end
139    }
140
141    pub fn axis_index_map_offset_byte_range(&self) -> Range<usize> {
142        let start = self.axis_segment_maps_byte_range().end;
143        let end = if self.version().compatible((2u16, 0u16)) {
144            start + Offset32::RAW_BYTE_LEN
145        } else {
146            start
147        };
148        start..end
149    }
150
151    pub fn var_store_offset_byte_range(&self) -> Range<usize> {
152        let start = self.axis_index_map_offset_byte_range().end;
153        let end = if self.version().compatible((2u16, 0u16)) {
154            start + Offset32::RAW_BYTE_LEN
155        } else {
156            start
157        };
158        start..end
159    }
160}
161
162const _: () = assert!(FontData::default_data_long_enough(Avar::MIN_SIZE));
163
164impl Default for Avar<'_> {
165    fn default() -> Self {
166        Self {
167            data: TableDataRef::new(FontData::default_table_data().as_bytes())
168                .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
169        }
170    }
171}
172
173/// [SegmentMaps](https://learn.microsoft.com/en-us/typography/opentype/spec/avar#table-formats) record
174#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
175pub struct SegmentMaps<'a> {
176    /// The number of correspondence pairs for this axis.
177    pub position_map_count: BigEndian<u16>,
178    /// The array of axis value map records for this axis.
179    pub axis_value_maps: &'a [AxisValueMap],
180}
181
182impl<'a> SegmentMaps<'a> {
183    /// The number of correspondence pairs for this axis.
184    pub fn position_map_count(&self) -> u16 {
185        self.position_map_count.get()
186    }
187
188    /// The array of axis value map records for this axis.
189    pub fn axis_value_maps(&self) -> &'a [AxisValueMap] {
190        self.axis_value_maps
191    }
192}
193
194/// [AxisValueMap](https://learn.microsoft.com/en-us/typography/opentype/spec/avar#table-formats) record
195#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, bytemuck :: AnyBitPattern)]
196#[repr(C)]
197#[repr(packed)]
198pub struct AxisValueMap {
199    /// A normalized coordinate value obtained using default normalization.
200    pub from_coordinate: BigEndian<F2Dot14>,
201    /// The modified, normalized coordinate value.
202    pub to_coordinate: BigEndian<F2Dot14>,
203}
204
205impl AxisValueMap {
206    /// A normalized coordinate value obtained using default normalization.
207    pub fn from_coordinate(&self) -> F2Dot14 {
208        self.from_coordinate.get()
209    }
210
211    /// The modified, normalized coordinate value.
212    pub fn to_coordinate(&self) -> F2Dot14 {
213        self.to_coordinate.get()
214    }
215}
216
217impl FixedSize for AxisValueMap {
218    const RAW_BYTE_LEN: usize = F2Dot14::RAW_BYTE_LEN + F2Dot14::RAW_BYTE_LEN;
219}