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)]
6 use crate::codegen_prelude::*;
7 
8 /// The [avar (Axis Variations)](https://docs.microsoft.com/en-us/typography/opentype/spec/avar) table
9 #[derive(Debug, Clone, Copy)]
10 #[doc(hidden)]
11 pub struct AvarMarker {
12     axis_segment_maps_byte_len: usize,
13 }
14 
15 impl AvarMarker {
version_byte_range(&self) -> Range<usize>16     fn version_byte_range(&self) -> Range<usize> {
17         let start = 0;
18         start..start + MajorMinor::RAW_BYTE_LEN
19     }
_reserved_byte_range(&self) -> Range<usize>20     fn _reserved_byte_range(&self) -> Range<usize> {
21         let start = self.version_byte_range().end;
22         start..start + u16::RAW_BYTE_LEN
23     }
axis_count_byte_range(&self) -> Range<usize>24     fn axis_count_byte_range(&self) -> Range<usize> {
25         let start = self._reserved_byte_range().end;
26         start..start + u16::RAW_BYTE_LEN
27     }
axis_segment_maps_byte_range(&self) -> Range<usize>28     fn axis_segment_maps_byte_range(&self) -> Range<usize> {
29         let start = self.axis_count_byte_range().end;
30         start..start + self.axis_segment_maps_byte_len
31     }
32 }
33 
34 impl TopLevelTable for Avar<'_> {
35     /// `avar`
36     const TAG: Tag = Tag::new(b"avar");
37 }
38 
39 impl<'a> FontRead<'a> for Avar<'a> {
read(data: FontData<'a>) -> Result<Self, ReadError>40     fn read(data: FontData<'a>) -> Result<Self, ReadError> {
41         let mut cursor = data.cursor();
42         cursor.advance::<MajorMinor>();
43         cursor.advance::<u16>();
44         cursor.advance::<u16>();
45         let axis_segment_maps_byte_len = cursor.remaining_bytes();
46         cursor.advance_by(axis_segment_maps_byte_len);
47         cursor.finish(AvarMarker {
48             axis_segment_maps_byte_len,
49         })
50     }
51 }
52 
53 /// The [avar (Axis Variations)](https://docs.microsoft.com/en-us/typography/opentype/spec/avar) table
54 pub type Avar<'a> = TableRef<'a, AvarMarker>;
55 
56 impl<'a> Avar<'a> {
57     /// Major version number of the axis variations table — set to 1.
58     /// Minor version number of the axis variations table — set to 0.
version(&self) -> MajorMinor59     pub fn version(&self) -> MajorMinor {
60         let range = self.shape.version_byte_range();
61         self.data.read_at(range.start).unwrap()
62     }
63 
64     /// The number of variation axes for this font. This must be the same number as axisCount in the 'fvar' table.
axis_count(&self) -> u1665     pub fn axis_count(&self) -> u16 {
66         let range = self.shape.axis_count_byte_range();
67         self.data.read_at(range.start).unwrap()
68     }
69 
70     /// The segment maps array — one segment map for each axis, in the order of axes specified in the 'fvar' table.
axis_segment_maps(&self) -> VarLenArray<'a, SegmentMaps<'a>>71     pub fn axis_segment_maps(&self) -> VarLenArray<'a, SegmentMaps<'a>> {
72         let range = self.shape.axis_segment_maps_byte_range();
73         VarLenArray::read(self.data.split_off(range.start).unwrap()).unwrap()
74     }
75 }
76 
77 #[cfg(feature = "traversal")]
78 impl<'a> SomeTable<'a> for Avar<'a> {
type_name(&self) -> &str79     fn type_name(&self) -> &str {
80         "Avar"
81     }
get_field(&self, idx: usize) -> Option<Field<'a>>82     fn get_field(&self, idx: usize) -> Option<Field<'a>> {
83         match idx {
84             0usize => Some(Field::new("version", self.version())),
85             1usize => Some(Field::new("axis_count", self.axis_count())),
86             2usize => Some(Field::new(
87                 "axis_segment_maps",
88                 traversal::FieldType::var_array(
89                     "SegmentMaps",
90                     self.axis_segment_maps(),
91                     self.offset_data(),
92                 ),
93             )),
94             _ => None,
95         }
96     }
97 }
98 
99 #[cfg(feature = "traversal")]
100 impl<'a> std::fmt::Debug for Avar<'a> {
fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result101     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
102         (self as &dyn SomeTable<'a>).fmt(f)
103     }
104 }
105 
106 /// [SegmentMaps](https://learn.microsoft.com/en-us/typography/opentype/spec/avar#table-formats) record
107 #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
108 pub struct SegmentMaps<'a> {
109     /// The number of correspondence pairs for this axis.
110     pub position_map_count: BigEndian<u16>,
111     /// The array of axis value map records for this axis.
112     pub axis_value_maps: &'a [AxisValueMap],
113 }
114 
115 impl<'a> SegmentMaps<'a> {
116     /// The number of correspondence pairs for this axis.
position_map_count(&self) -> u16117     pub fn position_map_count(&self) -> u16 {
118         self.position_map_count.get()
119     }
120 
121     /// The array of axis value map records for this axis.
axis_value_maps(&self) -> &'a [AxisValueMap]122     pub fn axis_value_maps(&self) -> &'a [AxisValueMap] {
123         self.axis_value_maps
124     }
125 }
126 
127 #[cfg(feature = "traversal")]
128 impl<'a> SomeRecord<'a> for SegmentMaps<'a> {
traverse(self, data: FontData<'a>) -> RecordResolver<'a>129     fn traverse(self, data: FontData<'a>) -> RecordResolver<'a> {
130         RecordResolver {
131             name: "SegmentMaps",
132             get_field: Box::new(move |idx, _data| match idx {
133                 0usize => Some(Field::new("position_map_count", self.position_map_count())),
134                 1usize => Some(Field::new(
135                     "axis_value_maps",
136                     traversal::FieldType::array_of_records(
137                         stringify!(AxisValueMap),
138                         self.axis_value_maps(),
139                         _data,
140                     ),
141                 )),
142                 _ => None,
143             }),
144             data,
145         }
146     }
147 }
148 
149 /// [AxisValueMap](https://learn.microsoft.com/en-us/typography/opentype/spec/avar#table-formats) record
150 #[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
151 #[repr(C)]
152 #[repr(packed)]
153 pub struct AxisValueMap {
154     /// A normalized coordinate value obtained using default normalization.
155     pub from_coordinate: BigEndian<F2Dot14>,
156     /// The modified, normalized coordinate value.
157     pub to_coordinate: BigEndian<F2Dot14>,
158 }
159 
160 impl AxisValueMap {
161     /// A normalized coordinate value obtained using default normalization.
from_coordinate(&self) -> F2Dot14162     pub fn from_coordinate(&self) -> F2Dot14 {
163         self.from_coordinate.get()
164     }
165 
166     /// The modified, normalized coordinate value.
to_coordinate(&self) -> F2Dot14167     pub fn to_coordinate(&self) -> F2Dot14 {
168         self.to_coordinate.get()
169     }
170 }
171 
172 impl FixedSize for AxisValueMap {
173     const RAW_BYTE_LEN: usize = F2Dot14::RAW_BYTE_LEN + F2Dot14::RAW_BYTE_LEN;
174 }
175 
176 impl sealed::Sealed for AxisValueMap {}
177 
178 /// SAFETY: see the [`FromBytes`] trait documentation.
179 unsafe impl FromBytes for AxisValueMap {
this_trait_should_only_be_implemented_in_generated_code()180     fn this_trait_should_only_be_implemented_in_generated_code() {}
181 }
182 
183 #[cfg(feature = "traversal")]
184 impl<'a> SomeRecord<'a> for AxisValueMap {
traverse(self, data: FontData<'a>) -> RecordResolver<'a>185     fn traverse(self, data: FontData<'a>) -> RecordResolver<'a> {
186         RecordResolver {
187             name: "AxisValueMap",
188             get_field: Box::new(move |idx, _data| match idx {
189                 0usize => Some(Field::new("from_coordinate", self.from_coordinate())),
190                 1usize => Some(Field::new("to_coordinate", self.to_coordinate())),
191                 _ => None,
192             }),
193             data,
194         }
195     }
196 }
197