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.offset_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 ReadArgs for Feat<'_> {
24    type Args = ();
25}
26
27impl<'a> FontRead<'a> for Feat<'a> {
28    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
29        #[allow(clippy::absurd_extreme_comparisons)]
30        if data.len() < Self::MIN_SIZE {
31            return Err(ReadError::OutOfBounds);
32        }
33        let Some(data) = TableDataRef::new(data.as_bytes()) else {
34            return Err(ReadError::OutOfBounds);
35        };
36        Ok(Self { data })
37    }
38}
39
40/// The fields at the start of the table, which are read directly.
41#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
42#[repr(C, packed)]
43#[doc(hidden)]
44pub struct FeatFixedHeader {
45    pub version: BigEndian<MajorMinor>,
46    pub feature_name_count: BigEndian<u16>,
47    pub _reserved1: BigEndian<u16>,
48    pub _reserved2: BigEndian<u32>,
49}
50
51#[allow(clippy::absurd_extreme_comparisons)]
52const _: () = assert!(std::mem::size_of::<FeatFixedHeader>() <= Feat::MIN_SIZE);
53
54/// The [feature name](https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6feat.html) table.
55#[derive(Clone)]
56pub struct Feat<'a> {
57    data: TableDataRef<'a, FeatFixedHeader>,
58}
59
60#[allow(clippy::needless_lifetimes)]
61impl<'a> Feat<'a> {
62    pub const MIN_SIZE: usize =
63        (MajorMinor::RAW_BYTE_LEN + u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN + u32::RAW_BYTE_LEN);
64    basic_table_impls!(impl_the_methods);
65
66    /// Version number of the feature name table (0x00010000 for the current
67    /// version).
68    pub fn version(&self) -> MajorMinor {
69        self.data.header().version.get()
70    }
71
72    /// The number of entries in the feature name array.
73    pub fn feature_name_count(&self) -> u16 {
74        self.data.header().feature_name_count.get()
75    }
76
77    /// The feature name array, sorted by feature type.
78    pub fn names(&self) -> &'a [FeatureName] {
79        let range = self.names_byte_range();
80        self.offset_data()
81            .read_array(range)
82            .ok()
83            .unwrap_or_default()
84    }
85
86    pub fn version_byte_range(&self) -> Range<usize> {
87        let start = 0;
88        let end = start + MajorMinor::RAW_BYTE_LEN;
89        start..end
90    }
91
92    pub fn feature_name_count_byte_range(&self) -> Range<usize> {
93        let start = self.version_byte_range().end;
94        let end = start + u16::RAW_BYTE_LEN;
95        start..end
96    }
97
98    pub fn _reserved1_byte_range(&self) -> Range<usize> {
99        let start = self.feature_name_count_byte_range().end;
100        let end = start + u16::RAW_BYTE_LEN;
101        start..end
102    }
103
104    pub fn _reserved2_byte_range(&self) -> Range<usize> {
105        let start = self._reserved1_byte_range().end;
106        let end = start + u32::RAW_BYTE_LEN;
107        start..end
108    }
109
110    pub fn names_byte_range(&self) -> Range<usize> {
111        let feature_name_count = self.feature_name_count();
112        let start = self._reserved2_byte_range().end;
113        let end = start
114            + (transforms::to_usize(feature_name_count)).saturating_mul(FeatureName::RAW_BYTE_LEN);
115        start..end
116    }
117}
118
119const _: () = assert!(FontData::default_data_long_enough(Feat::MIN_SIZE));
120
121impl Default for Feat<'_> {
122    fn default() -> Self {
123        Self {
124            data: TableDataRef::new(FontData::default_table_data().as_bytes())
125                .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
126        }
127    }
128}
129
130/// Type, flags and names for a feature.
131#[derive(Clone, Debug, Copy, bytemuck :: AnyBitPattern)]
132#[repr(C)]
133#[repr(packed)]
134pub struct FeatureName {
135    /// Feature type.
136    pub feature: BigEndian<u16>,
137    /// The number of records in the setting name array.
138    pub n_settings: BigEndian<u16>,
139    /// Offset in bytes from the beginning of this table to this feature's
140    /// setting name array. The actual type of record this offset refers
141    /// to will depend on the exclusivity value, as described below.
142    pub setting_table_offset: BigEndian<Offset32>,
143    /// Flags associated with the feature type.
144    pub feature_flags: BigEndian<u16>,
145    /// The name table index for the feature's name.
146    pub name_index: BigEndian<NameId>,
147}
148
149impl FeatureName {
150    /// Feature type.
151    pub fn feature(&self) -> u16 {
152        self.feature.get()
153    }
154
155    /// The number of records in the setting name array.
156    pub fn n_settings(&self) -> u16 {
157        self.n_settings.get()
158    }
159
160    /// Offset in bytes from the beginning of this table to this feature's
161    /// setting name array. The actual type of record this offset refers
162    /// to will depend on the exclusivity value, as described below.
163    pub fn setting_table_offset(&self) -> Offset32 {
164        self.setting_table_offset.get()
165    }
166
167    /// Offset in bytes from the beginning of this table to this feature's
168    /// setting name array. The actual type of record this offset refers
169    /// to will depend on the exclusivity value, as described below.
170    ///
171    /// The `data` argument should be retrieved from the parent table
172    /// By calling its `offset_data` method.
173    pub fn setting_table<'a>(&self, data: FontData<'a>) -> Result<SettingNameArray<'a>, ReadError> {
174        let args = self.n_settings();
175        self.setting_table_offset().resolve_with_args(data, args)
176    }
177
178    /// Flags associated with the feature type.
179    pub fn feature_flags(&self) -> u16 {
180        self.feature_flags.get()
181    }
182
183    /// The name table index for the feature's name.
184    pub fn name_index(&self) -> NameId {
185        self.name_index.get()
186    }
187}
188
189impl FixedSize for FeatureName {
190    const RAW_BYTE_LEN: usize = u16::RAW_BYTE_LEN
191        + u16::RAW_BYTE_LEN
192        + Offset32::RAW_BYTE_LEN
193        + u16::RAW_BYTE_LEN
194        + NameId::RAW_BYTE_LEN;
195}
196
197impl<'a> MinByteRange<'a> for SettingNameArray<'a> {
198    fn min_byte_range(&self) -> Range<usize> {
199        0..self.settings_byte_range().end
200    }
201    fn min_table_bytes(&self) -> &'a [u8] {
202        let range = self.min_byte_range();
203        self.offset_data().as_bytes().get(range).unwrap_or_default()
204    }
205}
206
207impl ReadArgs for SettingNameArray<'_> {
208    type Args = u16;
209}
210
211impl<'a> FontRead<'a> for SettingNameArray<'a> {
212    fn read_with_args(data: FontData<'a>, args: u16) -> Result<Self, ReadError> {
213        let n_settings = args;
214
215        #[allow(clippy::absurd_extreme_comparisons)]
216        if data.len() < Self::MIN_SIZE {
217            return Err(ReadError::OutOfBounds);
218        }
219        let Some(data) = TableDataRef::new(data.as_bytes()) else {
220            return Err(ReadError::OutOfBounds);
221        };
222        Ok(Self { data, n_settings })
223    }
224}
225
226impl<'a> SettingNameArray<'a> {
227    /// A constructor that requires additional arguments.
228    ///
229    /// This type requires some external state in order to be
230    /// parsed.
231    pub fn read(data: FontData<'a>, n_settings: u16) -> Result<Self, ReadError> {
232        let args = n_settings;
233        Self::read_with_args(data, args)
234    }
235}
236
237/// The fields at the start of the table, which are read directly.
238#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
239#[repr(C, packed)]
240#[doc(hidden)]
241pub struct SettingNameArrayFixedHeader {}
242
243#[allow(clippy::absurd_extreme_comparisons)]
244const _: () =
245    assert!(std::mem::size_of::<SettingNameArrayFixedHeader>() <= SettingNameArray::MIN_SIZE);
246
247#[derive(Clone)]
248pub struct SettingNameArray<'a> {
249    data: TableDataRef<'a, SettingNameArrayFixedHeader>,
250    n_settings: u16,
251}
252
253#[allow(clippy::needless_lifetimes)]
254impl<'a> SettingNameArray<'a> {
255    pub const MIN_SIZE: usize = 0;
256    basic_table_impls!(impl_the_methods);
257
258    /// List of setting names for a feature.
259    pub fn settings(&self) -> &'a [SettingName] {
260        let range = self.settings_byte_range();
261        self.offset_data()
262            .read_array(range)
263            .ok()
264            .unwrap_or_default()
265    }
266
267    pub(crate) fn n_settings(&self) -> u16 {
268        self.n_settings
269    }
270
271    pub fn settings_byte_range(&self) -> Range<usize> {
272        let n_settings = self.n_settings();
273        let start = 0;
274        let end =
275            start + (transforms::to_usize(n_settings)).saturating_mul(SettingName::RAW_BYTE_LEN);
276        start..end
277    }
278}
279
280#[allow(clippy::absurd_extreme_comparisons)]
281const _: () = assert!(FontData::default_data_long_enough(
282    SettingNameArray::MIN_SIZE
283));
284
285impl Default for SettingNameArray<'_> {
286    fn default() -> Self {
287        Self {
288            data: TableDataRef::new(FontData::default_table_data().as_bytes())
289                .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
290            n_settings: Default::default(),
291        }
292    }
293}
294
295/// Associates a setting with a name identifier.
296#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, bytemuck :: AnyBitPattern)]
297#[repr(C)]
298#[repr(packed)]
299pub struct SettingName {
300    /// The setting.
301    pub setting: BigEndian<u16>,
302    /// The name table index for the setting's name.
303    pub name_index: BigEndian<NameId>,
304}
305
306impl SettingName {
307    /// The setting.
308    pub fn setting(&self) -> u16 {
309        self.setting.get()
310    }
311
312    /// The name table index for the setting's name.
313    pub fn name_index(&self) -> NameId {
314        self.name_index.get()
315    }
316}
317
318impl FixedSize for SettingName {
319    const RAW_BYTE_LEN: usize = u16::RAW_BYTE_LEN + NameId::RAW_BYTE_LEN;
320}