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