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.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<'a> FontRead<'a> for Avar<'a> {
24    fn read(data: FontData<'a>) -> Result<Self, ReadError> {
25        #[allow(clippy::absurd_extreme_comparisons)]
26        if data.len() < Self::MIN_SIZE {
27            return Err(ReadError::OutOfBounds);
28        }
29        Ok(Self { data })
30    }
31}
32
33/// The [avar (Axis Variations)](https://docs.microsoft.com/en-us/typography/opentype/spec/avar) table
34#[derive(Clone)]
35pub struct Avar<'a> {
36    data: FontData<'a>,
37}
38
39#[allow(clippy::needless_lifetimes)]
40impl<'a> Avar<'a> {
41    pub const MIN_SIZE: usize = (MajorMinor::RAW_BYTE_LEN + u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN);
42    basic_table_impls!(impl_the_methods);
43
44    /// Major version number of the axis variations table — set to 1 or 2.
45    /// Minor version number of the axis variations table — set to 0.
46    pub fn version(&self) -> MajorMinor {
47        let range = self.version_byte_range();
48        self.data.read_at(range.start).ok().unwrap()
49    }
50
51    /// The number of variation axes for this font. This must be the same number as axisCount in the 'fvar' table.
52    pub fn axis_count(&self) -> u16 {
53        let range = self.axis_count_byte_range();
54        self.data.read_at(range.start).ok().unwrap()
55    }
56
57    /// The segment maps array — one segment map for each axis, in the order of axes specified in the 'fvar' table.
58    pub fn axis_segment_maps(&self) -> VarLenArray<'a, SegmentMaps<'a>> {
59        let range = self.axis_segment_maps_byte_range();
60        self.data
61            .split_off(range.start)
62            .and_then(|d| VarLenArray::read(d).ok())
63            .unwrap_or_default()
64    }
65
66    /// Offset to DeltaSetIndexMap table (may be NULL).
67    pub fn axis_index_map_offset(&self) -> Option<Nullable<Offset32>> {
68        let range = self.axis_index_map_offset_byte_range();
69        (!range.is_empty())
70            .then(|| self.data.read_at(range.start).ok())
71            .flatten()
72    }
73
74    /// Attempt to resolve [`axis_index_map_offset`][Self::axis_index_map_offset].
75    pub fn axis_index_map(&self) -> Option<Result<DeltaSetIndexMap<'a>, ReadError>> {
76        let data = self.data;
77        self.axis_index_map_offset().map(|x| x.resolve(data))?
78    }
79
80    /// Offset to ItemVariationStore (may be NULL).
81    pub fn var_store_offset(&self) -> Option<Nullable<Offset32>> {
82        let range = self.var_store_offset_byte_range();
83        (!range.is_empty())
84            .then(|| self.data.read_at(range.start).ok())
85            .flatten()
86    }
87
88    /// Attempt to resolve [`var_store_offset`][Self::var_store_offset].
89    pub fn var_store(&self) -> Option<Result<ItemVariationStore<'a>, ReadError>> {
90        let data = self.data;
91        self.var_store_offset().map(|x| x.resolve(data))?
92    }
93
94    pub fn version_byte_range(&self) -> Range<usize> {
95        let start = 0;
96        start..start + MajorMinor::RAW_BYTE_LEN
97    }
98
99    pub fn _reserved_byte_range(&self) -> Range<usize> {
100        let start = self.version_byte_range().end;
101        start..start + u16::RAW_BYTE_LEN
102    }
103
104    pub fn axis_count_byte_range(&self) -> Range<usize> {
105        let start = self._reserved_byte_range().end;
106        start..start + u16::RAW_BYTE_LEN
107    }
108
109    pub fn axis_segment_maps_byte_range(&self) -> Range<usize> {
110        let axis_count = self.axis_count();
111        let start = self.axis_count_byte_range().end;
112        start..start + {
113            let data = self.data.split_off(start).unwrap_or_default();
114            <SegmentMaps as VarSize>::total_len_for_count(data, axis_count as usize).unwrap_or(0)
115        }
116    }
117
118    pub fn axis_index_map_offset_byte_range(&self) -> Range<usize> {
119        let start = self.axis_segment_maps_byte_range().end;
120        start
121            ..(self.version().compatible((2u16, 0u16)))
122                .then(|| start + Offset32::RAW_BYTE_LEN)
123                .unwrap_or(start)
124    }
125
126    pub fn var_store_offset_byte_range(&self) -> Range<usize> {
127        let start = self.axis_index_map_offset_byte_range().end;
128        start
129            ..(self.version().compatible((2u16, 0u16)))
130                .then(|| start + Offset32::RAW_BYTE_LEN)
131                .unwrap_or(start)
132    }
133}
134
135#[cfg(feature = "experimental_traverse")]
136impl<'a> SomeTable<'a> for Avar<'a> {
137    fn type_name(&self) -> &str {
138        "Avar"
139    }
140    fn get_field(&self, idx: usize) -> Option<Field<'a>> {
141        match idx {
142            0usize => Some(Field::new("version", self.version())),
143            1usize => Some(Field::new("axis_count", self.axis_count())),
144            2usize => Some(Field::new(
145                "axis_segment_maps",
146                traversal::FieldType::var_array(
147                    "SegmentMaps",
148                    self.axis_segment_maps(),
149                    self.offset_data(),
150                ),
151            )),
152            3usize if self.version().compatible((2u16, 0u16)) => Some(Field::new(
153                "axis_index_map_offset",
154                FieldType::offset(self.axis_index_map_offset().unwrap(), self.axis_index_map()),
155            )),
156            4usize if self.version().compatible((2u16, 0u16)) => Some(Field::new(
157                "var_store_offset",
158                FieldType::offset(self.var_store_offset().unwrap(), self.var_store()),
159            )),
160            _ => None,
161        }
162    }
163}
164
165#[cfg(feature = "experimental_traverse")]
166#[allow(clippy::needless_lifetimes)]
167impl<'a> std::fmt::Debug for Avar<'a> {
168    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
169        (self as &dyn SomeTable<'a>).fmt(f)
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#[cfg(feature = "experimental_traverse")]
195impl<'a> SomeRecord<'a> for SegmentMaps<'a> {
196    fn traverse(self, data: FontData<'a>) -> RecordResolver<'a> {
197        RecordResolver {
198            name: "SegmentMaps",
199            get_field: Box::new(move |idx, _data| match idx {
200                0usize => Some(Field::new("position_map_count", self.position_map_count())),
201                1usize => Some(Field::new(
202                    "axis_value_maps",
203                    traversal::FieldType::array_of_records(
204                        stringify!(AxisValueMap),
205                        self.axis_value_maps(),
206                        _data,
207                    ),
208                )),
209                _ => None,
210            }),
211            data,
212        }
213    }
214}
215
216/// [AxisValueMap](https://learn.microsoft.com/en-us/typography/opentype/spec/avar#table-formats) record
217#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, bytemuck :: AnyBitPattern)]
218#[repr(C)]
219#[repr(packed)]
220pub struct AxisValueMap {
221    /// A normalized coordinate value obtained using default normalization.
222    pub from_coordinate: BigEndian<F2Dot14>,
223    /// The modified, normalized coordinate value.
224    pub to_coordinate: BigEndian<F2Dot14>,
225}
226
227impl AxisValueMap {
228    /// A normalized coordinate value obtained using default normalization.
229    pub fn from_coordinate(&self) -> F2Dot14 {
230        self.from_coordinate.get()
231    }
232
233    /// The modified, normalized coordinate value.
234    pub fn to_coordinate(&self) -> F2Dot14 {
235        self.to_coordinate.get()
236    }
237}
238
239impl FixedSize for AxisValueMap {
240    const RAW_BYTE_LEN: usize = F2Dot14::RAW_BYTE_LEN + F2Dot14::RAW_BYTE_LEN;
241}
242
243#[cfg(feature = "experimental_traverse")]
244impl<'a> SomeRecord<'a> for AxisValueMap {
245    fn traverse(self, data: FontData<'a>) -> RecordResolver<'a> {
246        RecordResolver {
247            name: "AxisValueMap",
248            get_field: Box::new(move |idx, _data| match idx {
249                0usize => Some(Field::new("from_coordinate", self.from_coordinate())),
250                1usize => Some(Field::new("to_coordinate", self.to_coordinate())),
251                _ => None,
252            }),
253            data,
254        }
255    }
256}