read_fonts/generated/
font.rs1#[allow(unused_imports)]
6use crate::codegen_prelude::*;
7
8impl<'a> MinByteRange<'a> for TableDirectory<'a> {
9 fn min_byte_range(&self) -> Range<usize> {
10 0..self.table_records_byte_range().end
11 }
12 fn min_table_bytes(&self) -> &'a [u8] {
13 let range = self.min_byte_range();
14 self.offset_data().as_bytes().get(range).unwrap_or_default()
15 }
16}
17
18impl ReadArgs for TableDirectory<'_> {
19 type Args = ();
20}
21
22impl<'a> FontRead<'a> for TableDirectory<'a> {
23 fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
24 #[allow(clippy::absurd_extreme_comparisons)]
25 if data.len() < Self::MIN_SIZE {
26 return Err(ReadError::OutOfBounds);
27 }
28 let Some(data) = TableDataRef::new(data.as_bytes()) else {
29 return Err(ReadError::OutOfBounds);
30 };
31 Ok(Self { data })
32 }
33}
34
35#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
37#[repr(C, packed)]
38#[doc(hidden)]
39pub struct TableDirectoryFixedHeader {
40 pub sfnt_version: BigEndian<u32>,
41 pub num_tables: BigEndian<u16>,
42 pub search_range: BigEndian<u16>,
43 pub entry_selector: BigEndian<u16>,
44 pub range_shift: BigEndian<u16>,
45}
46
47#[allow(clippy::absurd_extreme_comparisons)]
48const _: () = assert!(std::mem::size_of::<TableDirectoryFixedHeader>() <= TableDirectory::MIN_SIZE);
49
50#[derive(Clone)]
52pub struct TableDirectory<'a> {
53 data: TableDataRef<'a, TableDirectoryFixedHeader>,
54}
55
56#[allow(clippy::needless_lifetimes)]
57impl<'a> TableDirectory<'a> {
58 pub const MIN_SIZE: usize = (u32::RAW_BYTE_LEN
59 + u16::RAW_BYTE_LEN
60 + u16::RAW_BYTE_LEN
61 + u16::RAW_BYTE_LEN
62 + u16::RAW_BYTE_LEN);
63 basic_table_impls!(impl_the_methods);
64
65 pub fn sfnt_version(&self) -> u32 {
67 self.data.header().sfnt_version.get()
68 }
69
70 pub fn num_tables(&self) -> u16 {
72 self.data.header().num_tables.get()
73 }
74
75 pub fn search_range(&self) -> u16 {
76 self.data.header().search_range.get()
77 }
78
79 pub fn entry_selector(&self) -> u16 {
80 self.data.header().entry_selector.get()
81 }
82
83 pub fn range_shift(&self) -> u16 {
84 self.data.header().range_shift.get()
85 }
86
87 pub fn table_records(&self) -> &'a [TableRecord] {
89 let range = self.table_records_byte_range();
90 self.offset_data()
91 .read_array(range)
92 .ok()
93 .unwrap_or_default()
94 }
95
96 pub fn sfnt_version_byte_range(&self) -> Range<usize> {
97 let start = 0;
98 let end = start + u32::RAW_BYTE_LEN;
99 start..end
100 }
101
102 pub fn num_tables_byte_range(&self) -> Range<usize> {
103 let start = self.sfnt_version_byte_range().end;
104 let end = start + u16::RAW_BYTE_LEN;
105 start..end
106 }
107
108 pub fn search_range_byte_range(&self) -> Range<usize> {
109 let start = self.num_tables_byte_range().end;
110 let end = start + u16::RAW_BYTE_LEN;
111 start..end
112 }
113
114 pub fn entry_selector_byte_range(&self) -> Range<usize> {
115 let start = self.search_range_byte_range().end;
116 let end = start + u16::RAW_BYTE_LEN;
117 start..end
118 }
119
120 pub fn range_shift_byte_range(&self) -> Range<usize> {
121 let start = self.entry_selector_byte_range().end;
122 let end = start + u16::RAW_BYTE_LEN;
123 start..end
124 }
125
126 pub fn table_records_byte_range(&self) -> Range<usize> {
127 let num_tables = self.num_tables();
128 let start = self.range_shift_byte_range().end;
129 let end =
130 start + (transforms::to_usize(num_tables)).saturating_mul(TableRecord::RAW_BYTE_LEN);
131 start..end
132 }
133}
134
135const _: () = assert!(FontData::default_data_long_enough(TableDirectory::MIN_SIZE));
136
137impl Default for TableDirectory<'_> {
138 fn default() -> Self {
139 Self {
140 data: TableDataRef::new(FontData::default_table_data().as_bytes())
141 .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
142 }
143 }
144}
145
146#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, bytemuck :: AnyBitPattern)]
148#[repr(C)]
149#[repr(packed)]
150pub struct TableRecord {
151 pub tag: BigEndian<Tag>,
153 pub checksum: BigEndian<u32>,
155 pub offset: BigEndian<u32>,
157 pub length: BigEndian<u32>,
159}
160
161impl TableRecord {
162 pub fn tag(&self) -> Tag {
164 self.tag.get()
165 }
166
167 pub fn checksum(&self) -> u32 {
169 self.checksum.get()
170 }
171
172 pub fn offset(&self) -> u32 {
174 self.offset.get()
175 }
176
177 pub fn length(&self) -> u32 {
179 self.length.get()
180 }
181}
182
183impl FixedSize for TableRecord {
184 const RAW_BYTE_LEN: usize =
185 Tag::RAW_BYTE_LEN + u32::RAW_BYTE_LEN + u32::RAW_BYTE_LEN + u32::RAW_BYTE_LEN;
186}
187
188impl<'a> MinByteRange<'a> for TTCHeader<'a> {
189 fn min_byte_range(&self) -> Range<usize> {
190 0..self.table_directory_offsets_byte_range().end
191 }
192 fn min_table_bytes(&self) -> &'a [u8] {
193 let range = self.min_byte_range();
194 self.offset_data().as_bytes().get(range).unwrap_or_default()
195 }
196}
197
198impl ReadArgs for TTCHeader<'_> {
199 type Args = ();
200}
201
202impl<'a> FontRead<'a> for TTCHeader<'a> {
203 fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
204 #[allow(clippy::absurd_extreme_comparisons)]
205 if data.len() < Self::MIN_SIZE {
206 return Err(ReadError::OutOfBounds);
207 }
208 let Some(data) = TableDataRef::new(data.as_bytes()) else {
209 return Err(ReadError::OutOfBounds);
210 };
211 Ok(Self { data })
212 }
213}
214
215#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
217#[repr(C, packed)]
218#[doc(hidden)]
219pub struct TTCHeaderFixedHeader {
220 pub ttc_tag: BigEndian<Tag>,
221 pub version: BigEndian<MajorMinor>,
222 pub num_fonts: BigEndian<u32>,
223}
224
225#[allow(clippy::absurd_extreme_comparisons)]
226const _: () = assert!(std::mem::size_of::<TTCHeaderFixedHeader>() <= TTCHeader::MIN_SIZE);
227
228#[derive(Clone)]
230pub struct TTCHeader<'a> {
231 data: TableDataRef<'a, TTCHeaderFixedHeader>,
232}
233
234#[allow(clippy::needless_lifetimes)]
235impl<'a> TTCHeader<'a> {
236 pub const MIN_SIZE: usize = (Tag::RAW_BYTE_LEN + MajorMinor::RAW_BYTE_LEN + u32::RAW_BYTE_LEN);
237 basic_table_impls!(impl_the_methods);
238
239 pub fn ttc_tag(&self) -> Tag {
241 self.data.header().ttc_tag.get()
242 }
243
244 pub fn version(&self) -> MajorMinor {
246 self.data.header().version.get()
247 }
248
249 pub fn num_fonts(&self) -> u32 {
251 self.data.header().num_fonts.get()
252 }
253
254 pub fn table_directory_offsets(&self) -> &'a [BigEndian<u32>] {
256 let range = self.table_directory_offsets_byte_range();
257 self.offset_data()
258 .read_array(range)
259 .ok()
260 .unwrap_or_default()
261 }
262
263 pub fn dsig_tag(&self) -> Option<u32> {
265 let range = self.dsig_tag_byte_range();
266 (!range.is_empty())
267 .then(|| self.offset_data().read_at(range.start).ok())
268 .flatten()
269 }
270
271 pub fn dsig_length(&self) -> Option<u32> {
273 let range = self.dsig_length_byte_range();
274 (!range.is_empty())
275 .then(|| self.offset_data().read_at(range.start).ok())
276 .flatten()
277 }
278
279 pub fn dsig_offset(&self) -> Option<u32> {
281 let range = self.dsig_offset_byte_range();
282 (!range.is_empty())
283 .then(|| self.offset_data().read_at(range.start).ok())
284 .flatten()
285 }
286
287 pub fn ttc_tag_byte_range(&self) -> Range<usize> {
288 let start = 0;
289 let end = start + Tag::RAW_BYTE_LEN;
290 start..end
291 }
292
293 pub fn version_byte_range(&self) -> Range<usize> {
294 let start = self.ttc_tag_byte_range().end;
295 let end = start + MajorMinor::RAW_BYTE_LEN;
296 start..end
297 }
298
299 pub fn num_fonts_byte_range(&self) -> Range<usize> {
300 let start = self.version_byte_range().end;
301 let end = start + u32::RAW_BYTE_LEN;
302 start..end
303 }
304
305 pub fn table_directory_offsets_byte_range(&self) -> Range<usize> {
306 let num_fonts = self.num_fonts();
307 let start = self.num_fonts_byte_range().end;
308 let end = start + (transforms::to_usize(num_fonts)).saturating_mul(u32::RAW_BYTE_LEN);
309 start..end
310 }
311
312 pub fn dsig_tag_byte_range(&self) -> Range<usize> {
313 let start = self.table_directory_offsets_byte_range().end;
314 let end = if self.version().compatible((2u16, 0u16)) {
315 start + u32::RAW_BYTE_LEN
316 } else {
317 start
318 };
319 start..end
320 }
321
322 pub fn dsig_length_byte_range(&self) -> Range<usize> {
323 let start = self.dsig_tag_byte_range().end;
324 let end = if self.version().compatible((2u16, 0u16)) {
325 start + u32::RAW_BYTE_LEN
326 } else {
327 start
328 };
329 start..end
330 }
331
332 pub fn dsig_offset_byte_range(&self) -> Range<usize> {
333 let start = self.dsig_length_byte_range().end;
334 let end = if self.version().compatible((2u16, 0u16)) {
335 start + u32::RAW_BYTE_LEN
336 } else {
337 start
338 };
339 start..end
340 }
341}
342
343const _: () = assert!(FontData::default_data_long_enough(TTCHeader::MIN_SIZE));
344
345impl Default for TTCHeader<'_> {
346 fn default() -> Self {
347 Self {
348 data: TableDataRef::new(FontData::default_table_data().as_bytes())
349 .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
350 }
351 }
352}