read_fonts/generated/
generated_avar.rs1#[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 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#[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 pub fn version(&self) -> MajorMinor {
47 let range = self.version_byte_range();
48 self.data.read_at(range.start).ok().unwrap()
49 }
50
51 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 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 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 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 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 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
135const _: () = assert!(FontData::default_data_long_enough(Avar::MIN_SIZE));
136
137impl Default for Avar<'_> {
138 fn default() -> Self {
139 Self {
140 data: FontData::default_table_data(),
141 }
142 }
143}
144
145#[cfg(feature = "experimental_traverse")]
146impl<'a> SomeTable<'a> for Avar<'a> {
147 fn type_name(&self) -> &str {
148 "Avar"
149 }
150 fn get_field(&self, idx: usize) -> Option<Field<'a>> {
151 match idx {
152 0usize => Some(Field::new("version", self.version())),
153 1usize => Some(Field::new("axis_count", self.axis_count())),
154 2usize => Some(Field::new(
155 "axis_segment_maps",
156 traversal::FieldType::var_array(
157 "SegmentMaps",
158 self.axis_segment_maps(),
159 self.offset_data(),
160 ),
161 )),
162 3usize if self.version().compatible((2u16, 0u16)) => Some(Field::new(
163 "axis_index_map_offset",
164 FieldType::offset(self.axis_index_map_offset().unwrap(), self.axis_index_map()),
165 )),
166 4usize if self.version().compatible((2u16, 0u16)) => Some(Field::new(
167 "var_store_offset",
168 FieldType::offset(self.var_store_offset().unwrap(), self.var_store()),
169 )),
170 _ => None,
171 }
172 }
173}
174
175#[cfg(feature = "experimental_traverse")]
176#[allow(clippy::needless_lifetimes)]
177impl<'a> std::fmt::Debug for Avar<'a> {
178 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
179 (self as &dyn SomeTable<'a>).fmt(f)
180 }
181}
182
183#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
185pub struct SegmentMaps<'a> {
186 pub position_map_count: BigEndian<u16>,
188 pub axis_value_maps: &'a [AxisValueMap],
190}
191
192impl<'a> SegmentMaps<'a> {
193 pub fn position_map_count(&self) -> u16 {
195 self.position_map_count.get()
196 }
197
198 pub fn axis_value_maps(&self) -> &'a [AxisValueMap] {
200 self.axis_value_maps
201 }
202}
203
204#[cfg(feature = "experimental_traverse")]
205impl<'a> SomeRecord<'a> for SegmentMaps<'a> {
206 fn traverse(self, data: FontData<'a>) -> RecordResolver<'a> {
207 RecordResolver {
208 name: "SegmentMaps",
209 get_field: Box::new(move |idx, _data| match idx {
210 0usize => Some(Field::new("position_map_count", self.position_map_count())),
211 1usize => Some(Field::new(
212 "axis_value_maps",
213 traversal::FieldType::array_of_records(
214 stringify!(AxisValueMap),
215 self.axis_value_maps(),
216 _data,
217 ),
218 )),
219 _ => None,
220 }),
221 data,
222 }
223 }
224}
225
226#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, bytemuck :: AnyBitPattern)]
228#[repr(C)]
229#[repr(packed)]
230pub struct AxisValueMap {
231 pub from_coordinate: BigEndian<F2Dot14>,
233 pub to_coordinate: BigEndian<F2Dot14>,
235}
236
237impl AxisValueMap {
238 pub fn from_coordinate(&self) -> F2Dot14 {
240 self.from_coordinate.get()
241 }
242
243 pub fn to_coordinate(&self) -> F2Dot14 {
245 self.to_coordinate.get()
246 }
247}
248
249impl FixedSize for AxisValueMap {
250 const RAW_BYTE_LEN: usize = F2Dot14::RAW_BYTE_LEN + F2Dot14::RAW_BYTE_LEN;
251}
252
253#[cfg(feature = "experimental_traverse")]
254impl<'a> SomeRecord<'a> for AxisValueMap {
255 fn traverse(self, data: FontData<'a>) -> RecordResolver<'a> {
256 RecordResolver {
257 name: "AxisValueMap",
258 get_field: Box::new(move |idx, _data| match idx {
259 0usize => Some(Field::new("from_coordinate", self.from_coordinate())),
260 1usize => Some(Field::new("to_coordinate", self.to_coordinate())),
261 _ => None,
262 }),
263 data,
264 }
265 }
266}