Skip to main content

read_fonts/generated/
generated_feat.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 Feat<'a> {
9    fn min_byte_range(&self) -> Range<usize> {
10        0..self.names_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 Feat<'_> {
19    /// `feat`
20    const TAG: Tag = Tag::new(b"feat");
21}
22
23impl<'a> FontRead<'a> for Feat<'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 [feature name](https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6feat.html) table.
34#[derive(Clone)]
35pub struct Feat<'a> {
36    data: FontData<'a>,
37}
38
39#[allow(clippy::needless_lifetimes)]
40impl<'a> Feat<'a> {
41    pub const MIN_SIZE: usize =
42        (MajorMinor::RAW_BYTE_LEN + u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN + u32::RAW_BYTE_LEN);
43    basic_table_impls!(impl_the_methods);
44
45    /// Version number of the feature name table (0x00010000 for the current
46    /// version).
47    pub fn version(&self) -> MajorMinor {
48        let range = self.version_byte_range();
49        self.data.read_at(range.start).ok().unwrap()
50    }
51
52    /// The number of entries in the feature name array.
53    pub fn feature_name_count(&self) -> u16 {
54        let range = self.feature_name_count_byte_range();
55        self.data.read_at(range.start).ok().unwrap()
56    }
57
58    /// The feature name array, sorted by feature type.
59    pub fn names(&self) -> &'a [FeatureName] {
60        let range = self.names_byte_range();
61        self.data.read_array(range).ok().unwrap_or_default()
62    }
63
64    pub fn version_byte_range(&self) -> Range<usize> {
65        let start = 0;
66        start..start + MajorMinor::RAW_BYTE_LEN
67    }
68
69    pub fn feature_name_count_byte_range(&self) -> Range<usize> {
70        let start = self.version_byte_range().end;
71        start..start + u16::RAW_BYTE_LEN
72    }
73
74    pub fn _reserved1_byte_range(&self) -> Range<usize> {
75        let start = self.feature_name_count_byte_range().end;
76        start..start + u16::RAW_BYTE_LEN
77    }
78
79    pub fn _reserved2_byte_range(&self) -> Range<usize> {
80        let start = self._reserved1_byte_range().end;
81        start..start + u32::RAW_BYTE_LEN
82    }
83
84    pub fn names_byte_range(&self) -> Range<usize> {
85        let feature_name_count = self.feature_name_count();
86        let start = self._reserved2_byte_range().end;
87        start..start + (feature_name_count as usize).saturating_mul(FeatureName::RAW_BYTE_LEN)
88    }
89}
90
91const _: () = assert!(FontData::default_data_long_enough(Feat::MIN_SIZE));
92
93impl Default for Feat<'_> {
94    fn default() -> Self {
95        Self {
96            data: FontData::default_table_data(),
97        }
98    }
99}
100
101#[cfg(feature = "experimental_traverse")]
102impl<'a> SomeTable<'a> for Feat<'a> {
103    fn type_name(&self) -> &str {
104        "Feat"
105    }
106    fn get_field(&self, idx: usize) -> Option<Field<'a>> {
107        match idx {
108            0usize => Some(Field::new("version", self.version())),
109            1usize => Some(Field::new("feature_name_count", self.feature_name_count())),
110            2usize => Some(Field::new(
111                "names",
112                traversal::FieldType::array_of_records(
113                    stringify!(FeatureName),
114                    self.names(),
115                    self.offset_data(),
116                ),
117            )),
118            _ => None,
119        }
120    }
121}
122
123#[cfg(feature = "experimental_traverse")]
124#[allow(clippy::needless_lifetimes)]
125impl<'a> std::fmt::Debug for Feat<'a> {
126    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
127        (self as &dyn SomeTable<'a>).fmt(f)
128    }
129}
130
131/// Type, flags and names for a feature.
132#[derive(Clone, Debug, Copy, bytemuck :: AnyBitPattern)]
133#[repr(C)]
134#[repr(packed)]
135pub struct FeatureName {
136    /// Feature type.
137    pub feature: BigEndian<u16>,
138    /// The number of records in the setting name array.
139    pub n_settings: BigEndian<u16>,
140    /// Offset in bytes from the beginning of this table to this feature's
141    /// setting name array. The actual type of record this offset refers
142    /// to will depend on the exclusivity value, as described below.
143    pub setting_table_offset: BigEndian<Offset32>,
144    /// Flags associated with the feature type.
145    pub feature_flags: BigEndian<u16>,
146    /// The name table index for the feature's name.
147    pub name_index: BigEndian<NameId>,
148}
149
150impl FeatureName {
151    /// Feature type.
152    pub fn feature(&self) -> u16 {
153        self.feature.get()
154    }
155
156    /// The number of records in the setting name array.
157    pub fn n_settings(&self) -> u16 {
158        self.n_settings.get()
159    }
160
161    /// Offset in bytes from the beginning of this table to this feature's
162    /// setting name array. The actual type of record this offset refers
163    /// to will depend on the exclusivity value, as described below.
164    pub fn setting_table_offset(&self) -> Offset32 {
165        self.setting_table_offset.get()
166    }
167
168    /// Offset in bytes from the beginning of this table to this feature's
169    /// setting name array. The actual type of record this offset refers
170    /// to will depend on the exclusivity value, as described below.
171    ///
172    /// The `data` argument should be retrieved from the parent table
173    /// By calling its `offset_data` method.
174    pub fn setting_table<'a>(&self, data: FontData<'a>) -> Result<SettingNameArray<'a>, ReadError> {
175        let args = self.n_settings();
176        self.setting_table_offset().resolve_with_args(data, &args)
177    }
178
179    /// Flags associated with the feature type.
180    pub fn feature_flags(&self) -> u16 {
181        self.feature_flags.get()
182    }
183
184    /// The name table index for the feature's name.
185    pub fn name_index(&self) -> NameId {
186        self.name_index.get()
187    }
188}
189
190impl FixedSize for FeatureName {
191    const RAW_BYTE_LEN: usize = u16::RAW_BYTE_LEN
192        + u16::RAW_BYTE_LEN
193        + Offset32::RAW_BYTE_LEN
194        + u16::RAW_BYTE_LEN
195        + NameId::RAW_BYTE_LEN;
196}
197
198#[cfg(feature = "experimental_traverse")]
199impl<'a> SomeRecord<'a> for FeatureName {
200    fn traverse(self, data: FontData<'a>) -> RecordResolver<'a> {
201        RecordResolver {
202            name: "FeatureName",
203            get_field: Box::new(move |idx, _data| match idx {
204                0usize => Some(Field::new("feature", self.feature())),
205                1usize => Some(Field::new("n_settings", self.n_settings())),
206                2usize => Some(Field::new(
207                    "setting_table_offset",
208                    FieldType::offset(self.setting_table_offset(), self.setting_table(_data)),
209                )),
210                3usize => Some(Field::new("feature_flags", self.feature_flags())),
211                4usize => Some(Field::new("name_index", self.name_index())),
212                _ => None,
213            }),
214            data,
215        }
216    }
217}
218
219impl<'a> MinByteRange<'a> for SettingNameArray<'a> {
220    fn min_byte_range(&self) -> Range<usize> {
221        0..self.settings_byte_range().end
222    }
223    fn min_table_bytes(&self) -> &'a [u8] {
224        let range = self.min_byte_range();
225        self.data.as_bytes().get(range).unwrap_or_default()
226    }
227}
228
229impl ReadArgs for SettingNameArray<'_> {
230    type Args = u16;
231}
232
233impl<'a> FontReadWithArgs<'a> for SettingNameArray<'a> {
234    fn read_with_args(data: FontData<'a>, args: &u16) -> Result<Self, ReadError> {
235        let n_settings = *args;
236
237        #[allow(clippy::absurd_extreme_comparisons)]
238        if data.len() < Self::MIN_SIZE {
239            return Err(ReadError::OutOfBounds);
240        }
241        Ok(Self { data, n_settings })
242    }
243}
244
245impl<'a> SettingNameArray<'a> {
246    /// A constructor that requires additional arguments.
247    ///
248    /// This type requires some external state in order to be
249    /// parsed.
250    pub fn read(data: FontData<'a>, n_settings: u16) -> Result<Self, ReadError> {
251        let args = n_settings;
252        Self::read_with_args(data, &args)
253    }
254}
255
256#[derive(Clone)]
257pub struct SettingNameArray<'a> {
258    data: FontData<'a>,
259    n_settings: u16,
260}
261
262#[allow(clippy::needless_lifetimes)]
263impl<'a> SettingNameArray<'a> {
264    pub const MIN_SIZE: usize = 0;
265    basic_table_impls!(impl_the_methods);
266
267    /// List of setting names for a feature.
268    pub fn settings(&self) -> &'a [SettingName] {
269        let range = self.settings_byte_range();
270        self.data.read_array(range).ok().unwrap_or_default()
271    }
272
273    pub(crate) fn n_settings(&self) -> u16 {
274        self.n_settings
275    }
276
277    pub fn settings_byte_range(&self) -> Range<usize> {
278        let n_settings = self.n_settings();
279        let start = 0;
280        start..start + (n_settings as usize).saturating_mul(SettingName::RAW_BYTE_LEN)
281    }
282}
283
284#[allow(clippy::absurd_extreme_comparisons)]
285const _: () = assert!(FontData::default_data_long_enough(
286    SettingNameArray::MIN_SIZE
287));
288
289impl Default for SettingNameArray<'_> {
290    fn default() -> Self {
291        Self {
292            data: FontData::default_table_data(),
293            n_settings: Default::default(),
294        }
295    }
296}
297
298#[cfg(feature = "experimental_traverse")]
299impl<'a> SomeTable<'a> for SettingNameArray<'a> {
300    fn type_name(&self) -> &str {
301        "SettingNameArray"
302    }
303    fn get_field(&self, idx: usize) -> Option<Field<'a>> {
304        match idx {
305            0usize => Some(Field::new(
306                "settings",
307                traversal::FieldType::array_of_records(
308                    stringify!(SettingName),
309                    self.settings(),
310                    self.offset_data(),
311                ),
312            )),
313            _ => None,
314        }
315    }
316}
317
318#[cfg(feature = "experimental_traverse")]
319#[allow(clippy::needless_lifetimes)]
320impl<'a> std::fmt::Debug for SettingNameArray<'a> {
321    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
322        (self as &dyn SomeTable<'a>).fmt(f)
323    }
324}
325
326/// Associates a setting with a name identifier.
327#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, bytemuck :: AnyBitPattern)]
328#[repr(C)]
329#[repr(packed)]
330pub struct SettingName {
331    /// The setting.
332    pub setting: BigEndian<u16>,
333    /// The name table index for the setting's name.
334    pub name_index: BigEndian<NameId>,
335}
336
337impl SettingName {
338    /// The setting.
339    pub fn setting(&self) -> u16 {
340        self.setting.get()
341    }
342
343    /// The name table index for the setting's name.
344    pub fn name_index(&self) -> NameId {
345        self.name_index.get()
346    }
347}
348
349impl FixedSize for SettingName {
350    const RAW_BYTE_LEN: usize = u16::RAW_BYTE_LEN + NameId::RAW_BYTE_LEN;
351}
352
353#[cfg(feature = "experimental_traverse")]
354impl<'a> SomeRecord<'a> for SettingName {
355    fn traverse(self, data: FontData<'a>) -> RecordResolver<'a> {
356        RecordResolver {
357            name: "SettingName",
358            get_field: Box::new(move |idx, _data| match idx {
359                0usize => Some(Field::new("setting", self.setting())),
360                1usize => Some(Field::new("name_index", self.name_index())),
361                _ => None,
362            }),
363            data,
364        }
365    }
366}