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/// Part of [Name]
147#[derive(Clone, Debug, Copy, bytemuck :: AnyBitPattern)]
148#[repr(C)]
149#[repr(packed)]
150pub struct LangTagRecord {
151    /// Language-tag string length (in bytes)
152    pub length: BigEndian<u16>,
153    /// Language-tag string offset from start of storage area (in
154    /// bytes).
155    pub lang_tag_offset: BigEndian<Offset16>,
156}
157
158impl LangTagRecord {
159    /// Language-tag string length (in bytes)
160    pub fn length(&self) -> u16 {
161        self.length.get()
162    }
163
164    /// Language-tag string offset from start of storage area (in
165    /// bytes).
166    pub fn lang_tag_offset(&self) -> Offset16 {
167        self.lang_tag_offset.get()
168    }
169}
170
171impl FixedSize for LangTagRecord {
172    const RAW_BYTE_LEN: usize = u16::RAW_BYTE_LEN + Offset16::RAW_BYTE_LEN;
173}
174
175///[Name Records](https://docs.microsoft.com/en-us/typography/opentype/spec/name#name-records)
176#[derive(Clone, Debug, Copy, bytemuck :: AnyBitPattern)]
177#[repr(C)]
178#[repr(packed)]
179pub struct NameRecord {
180    /// Platform ID.
181    pub platform_id: BigEndian<u16>,
182    /// Platform-specific encoding ID.
183    pub encoding_id: BigEndian<u16>,
184    /// Language ID.
185    pub language_id: BigEndian<u16>,
186    /// Name ID.
187    pub name_id: BigEndian<NameId>,
188    /// String length (in bytes).
189    pub length: BigEndian<u16>,
190    /// String offset from start of storage area (in bytes).
191    pub string_offset: BigEndian<Offset16>,
192}
193
194impl NameRecord {
195    /// Platform ID.
196    pub fn platform_id(&self) -> u16 {
197        self.platform_id.get()
198    }
199
200    /// Platform-specific encoding ID.
201    pub fn encoding_id(&self) -> u16 {
202        self.encoding_id.get()
203    }
204
205    /// Language ID.
206    pub fn language_id(&self) -> u16 {
207        self.language_id.get()
208    }
209
210    /// Name ID.
211    pub fn name_id(&self) -> NameId {
212        self.name_id.get()
213    }
214
215    /// String length (in bytes).
216    pub fn length(&self) -> u16 {
217        self.length.get()
218    }
219
220    /// String offset from start of storage area (in bytes).
221    pub fn string_offset(&self) -> Offset16 {
222        self.string_offset.get()
223    }
224}
225
226impl FixedSize for NameRecord {
227    const RAW_BYTE_LEN: usize = u16::RAW_BYTE_LEN
228        + u16::RAW_BYTE_LEN
229        + u16::RAW_BYTE_LEN
230        + NameId::RAW_BYTE_LEN
231        + u16::RAW_BYTE_LEN
232        + Offset16::RAW_BYTE_LEN;
233}