Skip to main content

read_fonts/generated/
generated_name.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 Name<'a> {
9    fn min_byte_range(&self) -> Range<usize> {
10        0..self.name_record_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 Name<'_> {
19    /// `name`
20    const TAG: Tag = Tag::new(b"name");
21}
22
23impl ReadArgs for Name<'_> {
24    type Args = ();
25}
26
27impl<'a> FontRead<'a> for Name<'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/// [Naming table version 1](https://docs.microsoft.com/en-us/typography/opentype/spec/name#naming-table-version-1)
38#[derive(Clone)]
39pub struct Name<'a> {
40    data: FontData<'a>,
41}
42
43#[allow(clippy::needless_lifetimes)]
44impl<'a> Name<'a> {
45    pub const MIN_SIZE: usize = (u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN);
46    basic_table_impls!(impl_the_methods);
47
48    /// Table version number (0 or 1)
49    pub fn version(&self) -> u16 {
50        let range = self.version_byte_range();
51        self.data.read_at(range.start).ok().unwrap()
52    }
53
54    /// Number of name records.
55    pub fn count(&self) -> u16 {
56        let range = self.count_byte_range();
57        self.data.read_at(range.start).ok().unwrap()
58    }
59
60    /// Offset to start of string storage (from start of table).
61    pub fn storage_offset(&self) -> u16 {
62        let range = self.storage_offset_byte_range();
63        self.data.read_at(range.start).ok().unwrap()
64    }
65
66    /// The name records where count is the number of records.
67    pub fn name_record(&self) -> &'a [NameRecord] {
68        let range = self.name_record_byte_range();
69        self.data.read_array(range).ok().unwrap_or_default()
70    }
71
72    /// Number of language-tag records.
73    pub fn lang_tag_count(&self) -> Option<u16> {
74        let range = self.lang_tag_count_byte_range();
75        (!range.is_empty())
76            .then(|| self.data.read_at(range.start).ok())
77            .flatten()
78    }
79
80    /// The language-tag records where langTagCount is the number of records.
81    pub fn lang_tag_record(&self) -> Option<&'a [LangTagRecord]> {
82        let range = self.lang_tag_record_byte_range();
83        (!range.is_empty())
84            .then(|| self.data.read_array(range).ok())
85            .flatten()
86    }
87
88    pub fn version_byte_range(&self) -> Range<usize> {
89        let start = 0;
90        let end = start + u16::RAW_BYTE_LEN;
91        start..end
92    }
93
94    pub fn count_byte_range(&self) -> Range<usize> {
95        let start = self.version_byte_range().end;
96        let end = start + u16::RAW_BYTE_LEN;
97        start..end
98    }
99
100    pub fn storage_offset_byte_range(&self) -> Range<usize> {
101        let start = self.count_byte_range().end;
102        let end = start + u16::RAW_BYTE_LEN;
103        start..end
104    }
105
106    pub fn name_record_byte_range(&self) -> Range<usize> {
107        let count = self.count();
108        let start = self.storage_offset_byte_range().end;
109        let end = start + (transforms::to_usize(count)).saturating_mul(NameRecord::RAW_BYTE_LEN);
110        start..end
111    }
112
113    pub fn lang_tag_count_byte_range(&self) -> Range<usize> {
114        let start = self.name_record_byte_range().end;
115        let end = if self.version().compatible(1u16) {
116            start + u16::RAW_BYTE_LEN
117        } else {
118            start
119        };
120        start..end
121    }
122
123    pub fn lang_tag_record_byte_range(&self) -> Range<usize> {
124        let lang_tag_count = self.lang_tag_count().unwrap_or_default();
125        let start = self.lang_tag_count_byte_range().end;
126        let end = if self.version().compatible(1u16) {
127            start
128                + (transforms::to_usize(lang_tag_count)).saturating_mul(LangTagRecord::RAW_BYTE_LEN)
129        } else {
130            start
131        };
132        start..end
133    }
134}
135
136const _: () = assert!(FontData::default_data_long_enough(Name::MIN_SIZE));
137
138impl Default for Name<'_> {
139    fn default() -> Self {
140        Self {
141            data: FontData::default_table_data(),
142        }
143    }
144}
145
146#[cfg(feature = "experimental_traverse")]
147impl<'a> SomeTable<'a> for Name<'a> {
148    fn type_name(&self) -> &str {
149        "Name"
150    }
151    fn get_field(&self, idx: usize) -> Option<Field<'a>> {
152        match idx {
153            0usize => Some(Field::new("version", self.version())),
154            1usize => Some(Field::new("count", self.count())),
155            2usize => Some(Field::new("storage_offset", self.storage_offset())),
156            3usize => Some(Field::new(
157                "name_record",
158                traversal::FieldType::array_of_records(
159                    stringify!(NameRecord),
160                    self.name_record(),
161                    self.string_data(),
162                ),
163            )),
164            4usize if self.version().compatible(1u16) => {
165                Some(Field::new("lang_tag_count", self.lang_tag_count().unwrap()))
166            }
167            5usize if self.version().compatible(1u16) => Some(Field::new(
168                "lang_tag_record",
169                traversal::FieldType::array_of_records(
170                    stringify!(LangTagRecord),
171                    self.lang_tag_record().unwrap(),
172                    self.string_data(),
173                ),
174            )),
175            _ => None,
176        }
177    }
178}
179
180#[cfg(feature = "experimental_traverse")]
181#[allow(clippy::needless_lifetimes)]
182impl<'a> std::fmt::Debug for Name<'a> {
183    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
184        (self as &dyn SomeTable<'a>).fmt(f)
185    }
186}
187
188/// Part of [Name]
189#[derive(Clone, Debug, Copy, bytemuck :: AnyBitPattern)]
190#[repr(C)]
191#[repr(packed)]
192pub struct LangTagRecord {
193    /// Language-tag string length (in bytes)
194    pub length: BigEndian<u16>,
195    /// Language-tag string offset from start of storage area (in
196    /// bytes).
197    pub lang_tag_offset: BigEndian<Offset16>,
198}
199
200impl LangTagRecord {
201    /// Language-tag string length (in bytes)
202    pub fn length(&self) -> u16 {
203        self.length.get()
204    }
205
206    /// Language-tag string offset from start of storage area (in
207    /// bytes).
208    pub fn lang_tag_offset(&self) -> Offset16 {
209        self.lang_tag_offset.get()
210    }
211}
212
213impl FixedSize for LangTagRecord {
214    const RAW_BYTE_LEN: usize = u16::RAW_BYTE_LEN + Offset16::RAW_BYTE_LEN;
215}
216
217#[cfg(feature = "experimental_traverse")]
218impl<'a> SomeRecord<'a> for LangTagRecord {
219    fn traverse(self, data: FontData<'a>) -> RecordResolver<'a> {
220        RecordResolver {
221            name: "LangTagRecord",
222            get_field: Box::new(move |idx, _data| match idx {
223                0usize => Some(Field::new("length", self.length())),
224                1usize => Some(Field::new("lang_tag_offset", self.traverse_lang_tag(_data))),
225                _ => None,
226            }),
227            data,
228        }
229    }
230}
231
232///[Name Records](https://docs.microsoft.com/en-us/typography/opentype/spec/name#name-records)
233#[derive(Clone, Debug, Copy, bytemuck :: AnyBitPattern)]
234#[repr(C)]
235#[repr(packed)]
236pub struct NameRecord {
237    /// Platform ID.
238    pub platform_id: BigEndian<u16>,
239    /// Platform-specific encoding ID.
240    pub encoding_id: BigEndian<u16>,
241    /// Language ID.
242    pub language_id: BigEndian<u16>,
243    /// Name ID.
244    pub name_id: BigEndian<NameId>,
245    /// String length (in bytes).
246    pub length: BigEndian<u16>,
247    /// String offset from start of storage area (in bytes).
248    pub string_offset: BigEndian<Offset16>,
249}
250
251impl NameRecord {
252    /// Platform ID.
253    pub fn platform_id(&self) -> u16 {
254        self.platform_id.get()
255    }
256
257    /// Platform-specific encoding ID.
258    pub fn encoding_id(&self) -> u16 {
259        self.encoding_id.get()
260    }
261
262    /// Language ID.
263    pub fn language_id(&self) -> u16 {
264        self.language_id.get()
265    }
266
267    /// Name ID.
268    pub fn name_id(&self) -> NameId {
269        self.name_id.get()
270    }
271
272    /// String length (in bytes).
273    pub fn length(&self) -> u16 {
274        self.length.get()
275    }
276
277    /// String offset from start of storage area (in bytes).
278    pub fn string_offset(&self) -> Offset16 {
279        self.string_offset.get()
280    }
281}
282
283impl FixedSize for NameRecord {
284    const RAW_BYTE_LEN: usize = u16::RAW_BYTE_LEN
285        + u16::RAW_BYTE_LEN
286        + u16::RAW_BYTE_LEN
287        + NameId::RAW_BYTE_LEN
288        + u16::RAW_BYTE_LEN
289        + Offset16::RAW_BYTE_LEN;
290}
291
292#[cfg(feature = "experimental_traverse")]
293impl<'a> SomeRecord<'a> for NameRecord {
294    fn traverse(self, data: FontData<'a>) -> RecordResolver<'a> {
295        RecordResolver {
296            name: "NameRecord",
297            get_field: Box::new(move |idx, _data| match idx {
298                0usize => Some(Field::new("platform_id", self.platform_id())),
299                1usize => Some(Field::new("encoding_id", self.encoding_id())),
300                2usize => Some(Field::new("language_id", self.language_id())),
301                3usize => Some(Field::new("name_id", self.name_id())),
302                4usize => Some(Field::new("length", self.length())),
303                5usize => Some(Field::new("string_offset", self.traverse_string(_data))),
304                _ => None,
305            }),
306            data,
307        }
308    }
309}