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
91#[cfg(feature = "experimental_traverse")]
92impl<'a> SomeTable<'a> for Feat<'a> {
93    fn type_name(&self) -> &str {
94        "Feat"
95    }
96    fn get_field(&self, idx: usize) -> Option<Field<'a>> {
97        match idx {
98            0usize => Some(Field::new("version", self.version())),
99            1usize => Some(Field::new("feature_name_count", self.feature_name_count())),
100            2usize => Some(Field::new(
101                "names",
102                traversal::FieldType::array_of_records(
103                    stringify!(FeatureName),
104                    self.names(),
105                    self.offset_data(),
106                ),
107            )),
108            _ => None,
109        }
110    }
111}
112
113#[cfg(feature = "experimental_traverse")]
114#[allow(clippy::needless_lifetimes)]
115impl<'a> std::fmt::Debug for Feat<'a> {
116    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
117        (self as &dyn SomeTable<'a>).fmt(f)
118    }
119}
120
121/// Type, flags and names for a feature.
122#[derive(Clone, Debug, Copy, bytemuck :: AnyBitPattern)]
123#[repr(C)]
124#[repr(packed)]
125pub struct FeatureName {
126    /// Feature type.
127    pub feature: BigEndian<u16>,
128    /// The number of records in the setting name array.
129    pub n_settings: BigEndian<u16>,
130    /// Offset in bytes from the beginning of this table to this feature's
131    /// setting name array. The actual type of record this offset refers
132    /// to will depend on the exclusivity value, as described below.
133    pub setting_table_offset: BigEndian<Offset32>,
134    /// Flags associated with the feature type.
135    pub feature_flags: BigEndian<u16>,
136    /// The name table index for the feature's name.
137    pub name_index: BigEndian<NameId>,
138}
139
140impl FeatureName {
141    /// Feature type.
142    pub fn feature(&self) -> u16 {
143        self.feature.get()
144    }
145
146    /// The number of records in the setting name array.
147    pub fn n_settings(&self) -> u16 {
148        self.n_settings.get()
149    }
150
151    /// Offset in bytes from the beginning of this table to this feature's
152    /// setting name array. The actual type of record this offset refers
153    /// to will depend on the exclusivity value, as described below.
154    pub fn setting_table_offset(&self) -> Offset32 {
155        self.setting_table_offset.get()
156    }
157
158    /// Offset in bytes from the beginning of this table to this feature's
159    /// setting name array. The actual type of record this offset refers
160    /// to will depend on the exclusivity value, as described below.
161    ///
162    /// The `data` argument should be retrieved from the parent table
163    /// By calling its `offset_data` method.
164    pub fn setting_table<'a>(&self, data: FontData<'a>) -> Result<SettingNameArray<'a>, ReadError> {
165        let args = self.n_settings();
166        self.setting_table_offset().resolve_with_args(data, &args)
167    }
168
169    /// Flags associated with the feature type.
170    pub fn feature_flags(&self) -> u16 {
171        self.feature_flags.get()
172    }
173
174    /// The name table index for the feature's name.
175    pub fn name_index(&self) -> NameId {
176        self.name_index.get()
177    }
178}
179
180impl FixedSize for FeatureName {
181    const RAW_BYTE_LEN: usize = u16::RAW_BYTE_LEN
182        + u16::RAW_BYTE_LEN
183        + Offset32::RAW_BYTE_LEN
184        + u16::RAW_BYTE_LEN
185        + NameId::RAW_BYTE_LEN;
186}
187
188#[cfg(feature = "experimental_traverse")]
189impl<'a> SomeRecord<'a> for FeatureName {
190    fn traverse(self, data: FontData<'a>) -> RecordResolver<'a> {
191        RecordResolver {
192            name: "FeatureName",
193            get_field: Box::new(move |idx, _data| match idx {
194                0usize => Some(Field::new("feature", self.feature())),
195                1usize => Some(Field::new("n_settings", self.n_settings())),
196                2usize => Some(Field::new(
197                    "setting_table_offset",
198                    FieldType::offset(self.setting_table_offset(), self.setting_table(_data)),
199                )),
200                3usize => Some(Field::new("feature_flags", self.feature_flags())),
201                4usize => Some(Field::new("name_index", self.name_index())),
202                _ => None,
203            }),
204            data,
205        }
206    }
207}
208
209impl<'a> MinByteRange<'a> for SettingNameArray<'a> {
210    fn min_byte_range(&self) -> Range<usize> {
211        0..self.settings_byte_range().end
212    }
213    fn min_table_bytes(&self) -> &'a [u8] {
214        let range = self.min_byte_range();
215        self.data.as_bytes().get(range).unwrap_or_default()
216    }
217}
218
219impl ReadArgs for SettingNameArray<'_> {
220    type Args = u16;
221}
222
223impl<'a> FontReadWithArgs<'a> for SettingNameArray<'a> {
224    fn read_with_args(data: FontData<'a>, args: &u16) -> Result<Self, ReadError> {
225        let n_settings = *args;
226
227        #[allow(clippy::absurd_extreme_comparisons)]
228        if data.len() < Self::MIN_SIZE {
229            return Err(ReadError::OutOfBounds);
230        }
231        Ok(Self { data, n_settings })
232    }
233}
234
235impl<'a> SettingNameArray<'a> {
236    /// A constructor that requires additional arguments.
237    ///
238    /// This type requires some external state in order to be
239    /// parsed.
240    pub fn read(data: FontData<'a>, n_settings: u16) -> Result<Self, ReadError> {
241        let args = n_settings;
242        Self::read_with_args(data, &args)
243    }
244}
245
246#[derive(Clone)]
247pub struct SettingNameArray<'a> {
248    data: FontData<'a>,
249    n_settings: u16,
250}
251
252#[allow(clippy::needless_lifetimes)]
253impl<'a> SettingNameArray<'a> {
254    pub const MIN_SIZE: usize = 0;
255    basic_table_impls!(impl_the_methods);
256
257    /// List of setting names for a feature.
258    pub fn settings(&self) -> &'a [SettingName] {
259        let range = self.settings_byte_range();
260        self.data.read_array(range).ok().unwrap_or_default()
261    }
262
263    pub(crate) fn n_settings(&self) -> u16 {
264        self.n_settings
265    }
266
267    pub fn settings_byte_range(&self) -> Range<usize> {
268        let n_settings = self.n_settings();
269        let start = 0;
270        start..start + (n_settings as usize).saturating_mul(SettingName::RAW_BYTE_LEN)
271    }
272}
273
274#[cfg(feature = "experimental_traverse")]
275impl<'a> SomeTable<'a> for SettingNameArray<'a> {
276    fn type_name(&self) -> &str {
277        "SettingNameArray"
278    }
279    fn get_field(&self, idx: usize) -> Option<Field<'a>> {
280        match idx {
281            0usize => Some(Field::new(
282                "settings",
283                traversal::FieldType::array_of_records(
284                    stringify!(SettingName),
285                    self.settings(),
286                    self.offset_data(),
287                ),
288            )),
289            _ => None,
290        }
291    }
292}
293
294#[cfg(feature = "experimental_traverse")]
295#[allow(clippy::needless_lifetimes)]
296impl<'a> std::fmt::Debug for SettingNameArray<'a> {
297    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
298        (self as &dyn SomeTable<'a>).fmt(f)
299    }
300}
301
302/// Associates a setting with a name identifier.
303#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, bytemuck :: AnyBitPattern)]
304#[repr(C)]
305#[repr(packed)]
306pub struct SettingName {
307    /// The setting.
308    pub setting: BigEndian<u16>,
309    /// The name table index for the setting's name.
310    pub name_index: BigEndian<NameId>,
311}
312
313impl SettingName {
314    /// The setting.
315    pub fn setting(&self) -> u16 {
316        self.setting.get()
317    }
318
319    /// The name table index for the setting's name.
320    pub fn name_index(&self) -> NameId {
321        self.name_index.get()
322    }
323}
324
325impl FixedSize for SettingName {
326    const RAW_BYTE_LEN: usize = u16::RAW_BYTE_LEN + NameId::RAW_BYTE_LEN;
327}
328
329#[cfg(feature = "experimental_traverse")]
330impl<'a> SomeRecord<'a> for SettingName {
331    fn traverse(self, data: FontData<'a>) -> RecordResolver<'a> {
332        RecordResolver {
333            name: "SettingName",
334            get_field: Box::new(move |idx, _data| match idx {
335                0usize => Some(Field::new("setting", self.setting())),
336                1usize => Some(Field::new("name_index", self.name_index())),
337                _ => None,
338            }),
339            data,
340        }
341    }
342}