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