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 + (count as usize).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 + (lang_tag_count as usize).saturating_mul(LangTagRecord::RAW_BYTE_LEN)
120                })
121                .unwrap_or(start)
122    }
123}
124
125#[cfg(feature = "experimental_traverse")]
126impl<'a> SomeTable<'a> for Name<'a> {
127    fn type_name(&self) -> &str {
128        "Name"
129    }
130    fn get_field(&self, idx: usize) -> Option<Field<'a>> {
131        match idx {
132            0usize => Some(Field::new("version", self.version())),
133            1usize => Some(Field::new("count", self.count())),
134            2usize => Some(Field::new("storage_offset", self.storage_offset())),
135            3usize => Some(Field::new(
136                "name_record",
137                traversal::FieldType::array_of_records(
138                    stringify!(NameRecord),
139                    self.name_record(),
140                    self.string_data(),
141                ),
142            )),
143            4usize if self.version().compatible(1u16) => {
144                Some(Field::new("lang_tag_count", self.lang_tag_count().unwrap()))
145            }
146            5usize if self.version().compatible(1u16) => Some(Field::new(
147                "lang_tag_record",
148                traversal::FieldType::array_of_records(
149                    stringify!(LangTagRecord),
150                    self.lang_tag_record().unwrap(),
151                    self.string_data(),
152                ),
153            )),
154            _ => None,
155        }
156    }
157}
158
159#[cfg(feature = "experimental_traverse")]
160#[allow(clippy::needless_lifetimes)]
161impl<'a> std::fmt::Debug for Name<'a> {
162    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
163        (self as &dyn SomeTable<'a>).fmt(f)
164    }
165}
166
167/// Part of [Name]
168#[derive(Clone, Debug, Copy, bytemuck :: AnyBitPattern)]
169#[repr(C)]
170#[repr(packed)]
171pub struct LangTagRecord {
172    /// Language-tag string length (in bytes)
173    pub length: BigEndian<u16>,
174    /// Language-tag string offset from start of storage area (in
175    /// bytes).
176    pub lang_tag_offset: BigEndian<Offset16>,
177}
178
179impl LangTagRecord {
180    /// Language-tag string length (in bytes)
181    pub fn length(&self) -> u16 {
182        self.length.get()
183    }
184
185    /// Language-tag string offset from start of storage area (in
186    /// bytes).
187    pub fn lang_tag_offset(&self) -> Offset16 {
188        self.lang_tag_offset.get()
189    }
190}
191
192impl FixedSize for LangTagRecord {
193    const RAW_BYTE_LEN: usize = u16::RAW_BYTE_LEN + Offset16::RAW_BYTE_LEN;
194}
195
196#[cfg(feature = "experimental_traverse")]
197impl<'a> SomeRecord<'a> for LangTagRecord {
198    fn traverse(self, data: FontData<'a>) -> RecordResolver<'a> {
199        RecordResolver {
200            name: "LangTagRecord",
201            get_field: Box::new(move |idx, _data| match idx {
202                0usize => Some(Field::new("length", self.length())),
203                1usize => Some(Field::new("lang_tag_offset", self.traverse_lang_tag(_data))),
204                _ => None,
205            }),
206            data,
207        }
208    }
209}
210
211///[Name Records](https://docs.microsoft.com/en-us/typography/opentype/spec/name#name-records)
212#[derive(Clone, Debug, Copy, bytemuck :: AnyBitPattern)]
213#[repr(C)]
214#[repr(packed)]
215pub struct NameRecord {
216    /// Platform ID.
217    pub platform_id: BigEndian<u16>,
218    /// Platform-specific encoding ID.
219    pub encoding_id: BigEndian<u16>,
220    /// Language ID.
221    pub language_id: BigEndian<u16>,
222    /// Name ID.
223    pub name_id: BigEndian<NameId>,
224    /// String length (in bytes).
225    pub length: BigEndian<u16>,
226    /// String offset from start of storage area (in bytes).
227    pub string_offset: BigEndian<Offset16>,
228}
229
230impl NameRecord {
231    /// Platform ID.
232    pub fn platform_id(&self) -> u16 {
233        self.platform_id.get()
234    }
235
236    /// Platform-specific encoding ID.
237    pub fn encoding_id(&self) -> u16 {
238        self.encoding_id.get()
239    }
240
241    /// Language ID.
242    pub fn language_id(&self) -> u16 {
243        self.language_id.get()
244    }
245
246    /// Name ID.
247    pub fn name_id(&self) -> NameId {
248        self.name_id.get()
249    }
250
251    /// String length (in bytes).
252    pub fn length(&self) -> u16 {
253        self.length.get()
254    }
255
256    /// String offset from start of storage area (in bytes).
257    pub fn string_offset(&self) -> Offset16 {
258        self.string_offset.get()
259    }
260}
261
262impl FixedSize for NameRecord {
263    const RAW_BYTE_LEN: usize = u16::RAW_BYTE_LEN
264        + u16::RAW_BYTE_LEN
265        + u16::RAW_BYTE_LEN
266        + NameId::RAW_BYTE_LEN
267        + u16::RAW_BYTE_LEN
268        + Offset16::RAW_BYTE_LEN;
269}
270
271#[cfg(feature = "experimental_traverse")]
272impl<'a> SomeRecord<'a> for NameRecord {
273    fn traverse(self, data: FontData<'a>) -> RecordResolver<'a> {
274        RecordResolver {
275            name: "NameRecord",
276            get_field: Box::new(move |idx, _data| match idx {
277                0usize => Some(Field::new("platform_id", self.platform_id())),
278                1usize => Some(Field::new("encoding_id", self.encoding_id())),
279                2usize => Some(Field::new("language_id", self.language_id())),
280                3usize => Some(Field::new("name_id", self.name_id())),
281                4usize => Some(Field::new("length", self.length())),
282                5usize => Some(Field::new("string_offset", self.traverse_string(_data))),
283                _ => None,
284            }),
285            data,
286        }
287    }
288}