/src/fontations/read-fonts/generated/generated_name.rs
Line | Count | Source |
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)] |
6 | | use crate::codegen_prelude::*; |
7 | | |
8 | | impl<'a> MinByteRange<'a> for Name<'a> { |
9 | 0 | fn min_byte_range(&self) -> Range<usize> { |
10 | 0 | 0..self.name_record_byte_range().end |
11 | 0 | } |
12 | 0 | fn min_table_bytes(&self) -> &'a [u8] { |
13 | 0 | let range = self.min_byte_range(); |
14 | 0 | self.offset_data().as_bytes().get(range).unwrap_or_default() |
15 | 0 | } |
16 | | } |
17 | | |
18 | | impl TopLevelTable for Name<'_> { |
19 | | /// `name` |
20 | | const TAG: Tag = Tag::new(b"name"); |
21 | | } |
22 | | |
23 | | impl ReadArgs for Name<'_> { |
24 | | type Args = (); |
25 | | } |
26 | | |
27 | | impl<'a> FontRead<'a> for Name<'a> { |
28 | 518 | fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> { |
29 | | #[allow(clippy::absurd_extreme_comparisons)] |
30 | 518 | if data.len() < Self::MIN_SIZE { |
31 | 3 | return Err(ReadError::OutOfBounds); |
32 | 515 | } |
33 | 515 | let Some(data) = TableDataRef::new(data.as_bytes()) else { |
34 | 0 | return Err(ReadError::OutOfBounds); |
35 | | }; |
36 | 515 | Ok(Self { data }) |
37 | 518 | } |
38 | | } |
39 | | |
40 | | /// The fields at the start of the table, which are read directly. |
41 | | #[derive(Clone, Copy, bytemuck :: AnyBitPattern)] |
42 | | #[repr(C, packed)] |
43 | | #[doc(hidden)] |
44 | | pub struct NameFixedHeader { |
45 | | pub version: BigEndian<u16>, |
46 | | pub count: BigEndian<u16>, |
47 | | pub storage_offset: BigEndian<u16>, |
48 | | } |
49 | | |
50 | | #[allow(clippy::absurd_extreme_comparisons)] |
51 | | const _: () = assert!(std::mem::size_of::<NameFixedHeader>() <= Name::MIN_SIZE); |
52 | | |
53 | | /// [Naming table version 1](https://docs.microsoft.com/en-us/typography/opentype/spec/name#naming-table-version-1) |
54 | | #[derive(Clone)] |
55 | | pub struct Name<'a> { |
56 | | data: TableDataRef<'a, NameFixedHeader>, |
57 | | } |
58 | | |
59 | | #[allow(clippy::needless_lifetimes)] |
60 | | impl<'a> Name<'a> { |
61 | | pub const MIN_SIZE: usize = (u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN); |
62 | | basic_table_impls!(impl_the_methods); |
63 | | |
64 | | /// Table version number (0 or 1) |
65 | 1.54k | pub fn version(&self) -> u16 { |
66 | 1.54k | self.data.header().version.get() |
67 | 1.54k | } |
68 | | |
69 | | /// Number of name records. |
70 | 1.54k | pub fn count(&self) -> u16 { |
71 | 1.54k | self.data.header().count.get() |
72 | 1.54k | } |
73 | | |
74 | | /// Offset to start of string storage (from start of table). |
75 | 83.7k | pub fn storage_offset(&self) -> u16 { |
76 | 83.7k | self.data.header().storage_offset.get() |
77 | 83.7k | } |
78 | | |
79 | | /// The name records where count is the number of records. |
80 | 515 | pub fn name_record(&self) -> &'a [NameRecord] { |
81 | 515 | let range = self.name_record_byte_range(); |
82 | 515 | self.offset_data() |
83 | 515 | .read_array(range) |
84 | 515 | .ok() |
85 | 515 | .unwrap_or_default() |
86 | 515 | } |
87 | | |
88 | | /// Number of language-tag records. |
89 | 515 | pub fn lang_tag_count(&self) -> Option<u16> { |
90 | 515 | let range = self.lang_tag_count_byte_range(); |
91 | 515 | (!range.is_empty()) |
92 | 515 | .then(|| self.offset_data().read_at(range.start).ok()) |
93 | 515 | .flatten() |
94 | 515 | } |
95 | | |
96 | | /// The language-tag records where langTagCount is the number of records. |
97 | 515 | pub fn lang_tag_record(&self) -> Option<&'a [LangTagRecord]> { |
98 | 515 | let range = self.lang_tag_record_byte_range(); |
99 | 515 | (!range.is_empty()) |
100 | 515 | .then(|| self.offset_data().read_array(range).ok()) |
101 | 515 | .flatten() |
102 | 515 | } |
103 | | |
104 | 1.54k | pub fn version_byte_range(&self) -> Range<usize> { |
105 | 1.54k | let start = 0usize; |
106 | 1.54k | let end = start.saturating_add(u16::RAW_BYTE_LEN); |
107 | 1.54k | start..end |
108 | 1.54k | } |
109 | | |
110 | 1.54k | pub fn count_byte_range(&self) -> Range<usize> { |
111 | 1.54k | let start = self.version_byte_range().end; |
112 | 1.54k | let end = start.saturating_add(u16::RAW_BYTE_LEN); |
113 | 1.54k | start..end |
114 | 1.54k | } |
115 | | |
116 | 1.54k | pub fn storage_offset_byte_range(&self) -> Range<usize> { |
117 | 1.54k | let start = self.count_byte_range().end; |
118 | 1.54k | let end = start.saturating_add(u16::RAW_BYTE_LEN); |
119 | 1.54k | start..end |
120 | 1.54k | } |
121 | | |
122 | 1.54k | pub fn name_record_byte_range(&self) -> Range<usize> { |
123 | 1.54k | let count = self.count(); |
124 | 1.54k | let start = self.storage_offset_byte_range().end; |
125 | 1.54k | let end = start |
126 | 1.54k | .saturating_add((transforms::to_usize(count)).saturating_mul(NameRecord::RAW_BYTE_LEN)); |
127 | 1.54k | start..end |
128 | 1.54k | } |
129 | | |
130 | 1.03k | pub fn lang_tag_count_byte_range(&self) -> Range<usize> { |
131 | 1.03k | let start = self.name_record_byte_range().end; |
132 | 1.03k | let end = if self.version().compatible(1u16) { |
133 | 842 | start.saturating_add(u16::RAW_BYTE_LEN) |
134 | | } else { |
135 | 188 | start |
136 | | }; |
137 | 1.03k | start..end |
138 | 1.03k | } |
139 | | |
140 | 515 | pub fn lang_tag_record_byte_range(&self) -> Range<usize> { |
141 | 515 | let lang_tag_count = self.lang_tag_count().unwrap_or_default(); |
142 | 515 | let start = self.lang_tag_count_byte_range().end; |
143 | 515 | let end = if self.version().compatible(1u16) { |
144 | 421 | start.saturating_add( |
145 | 421 | (transforms::to_usize(lang_tag_count)).saturating_mul(LangTagRecord::RAW_BYTE_LEN), |
146 | | ) |
147 | | } else { |
148 | 94 | start |
149 | | }; |
150 | 515 | start..end |
151 | 515 | } |
152 | | } |
153 | | |
154 | | const _: () = assert!(FontData::default_data_long_enough(Name::MIN_SIZE)); |
155 | | |
156 | | impl Default for Name<'_> { |
157 | 0 | fn default() -> Self { |
158 | | Self { |
159 | 0 | data: TableDataRef::new(FontData::default_table_data().as_bytes()) |
160 | 0 | .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")), |
161 | | } |
162 | 0 | } |
163 | | } |
164 | | |
165 | | /// Part of [Name] |
166 | | #[derive(Clone, Debug, Copy, bytemuck :: AnyBitPattern)] |
167 | | #[repr(C)] |
168 | | #[repr(packed)] |
169 | | pub struct LangTagRecord { |
170 | | /// Language-tag string length (in bytes) |
171 | | pub length: BigEndian<u16>, |
172 | | /// Language-tag string offset from start of storage area (in |
173 | | /// bytes). |
174 | | pub lang_tag_offset: BigEndian<Offset16>, |
175 | | } |
176 | | |
177 | | impl LangTagRecord { |
178 | | /// Language-tag string length (in bytes) |
179 | 0 | pub fn length(&self) -> u16 { |
180 | 0 | self.length.get() |
181 | 0 | } |
182 | | |
183 | | /// Language-tag string offset from start of storage area (in |
184 | | /// bytes). |
185 | 0 | pub fn lang_tag_offset(&self) -> Offset16 { |
186 | 0 | self.lang_tag_offset.get() |
187 | 0 | } |
188 | | } |
189 | | |
190 | | impl FixedSize for LangTagRecord { |
191 | | const RAW_BYTE_LEN: usize = u16::RAW_BYTE_LEN + Offset16::RAW_BYTE_LEN; |
192 | | } |
193 | | |
194 | | ///[Name Records](https://docs.microsoft.com/en-us/typography/opentype/spec/name#name-records) |
195 | | #[derive(Clone, Debug, Copy, bytemuck :: AnyBitPattern)] |
196 | | #[repr(C)] |
197 | | #[repr(packed)] |
198 | | pub struct NameRecord { |
199 | | /// Platform ID. |
200 | | pub platform_id: BigEndian<u16>, |
201 | | /// Platform-specific encoding ID. |
202 | | pub encoding_id: BigEndian<u16>, |
203 | | /// Language ID. |
204 | | pub language_id: BigEndian<u16>, |
205 | | /// Name ID. |
206 | | pub name_id: BigEndian<NameId>, |
207 | | /// String length (in bytes). |
208 | | pub length: BigEndian<u16>, |
209 | | /// String offset from start of storage area (in bytes). |
210 | | pub string_offset: BigEndian<Offset16>, |
211 | | } |
212 | | |
213 | | impl NameRecord { |
214 | | /// Platform ID. |
215 | 70.7k | pub fn platform_id(&self) -> u16 { |
216 | 70.7k | self.platform_id.get() |
217 | 70.7k | } |
218 | | |
219 | | /// Platform-specific encoding ID. |
220 | 70.7k | pub fn encoding_id(&self) -> u16 { |
221 | 70.7k | self.encoding_id.get() |
222 | 70.7k | } |
223 | | |
224 | | /// Language ID. |
225 | 0 | pub fn language_id(&self) -> u16 { |
226 | 0 | self.language_id.get() |
227 | 0 | } |
228 | | |
229 | | /// Name ID. |
230 | 0 | pub fn name_id(&self) -> NameId { |
231 | 0 | self.name_id.get() |
232 | 0 | } |
233 | | |
234 | | /// String length (in bytes). |
235 | 83.7k | pub fn length(&self) -> u16 { |
236 | 83.7k | self.length.get() |
237 | 83.7k | } |
238 | | |
239 | | /// String offset from start of storage area (in bytes). |
240 | 83.7k | pub fn string_offset(&self) -> Offset16 { |
241 | 83.7k | self.string_offset.get() |
242 | 83.7k | } |
243 | | } |
244 | | |
245 | | impl FixedSize for NameRecord { |
246 | | const RAW_BYTE_LEN: usize = u16::RAW_BYTE_LEN |
247 | | + u16::RAW_BYTE_LEN |
248 | | + u16::RAW_BYTE_LEN |
249 | | + NameId::RAW_BYTE_LEN |
250 | | + u16::RAW_BYTE_LEN |
251 | | + Offset16::RAW_BYTE_LEN; |
252 | | } |