1#[allow(unused_imports)]
6use crate::codegen_prelude::*;
7
8#[derive(Clone)]
11pub enum Lookup<'a> {
12 Format0(Lookup0<'a>),
13 Format2(Lookup2<'a>),
14 Format4(Lookup4<'a>),
15 Format6(Lookup6<'a>),
16 Format8(Lookup8<'a>),
17 Format10(Lookup10<'a>),
18}
19
20impl Default for Lookup<'_> {
21 fn default() -> Self {
22 Self::Format0(Default::default())
23 }
24}
25
26impl<'a> Lookup<'a> {
27 pub fn offset_data(&self) -> FontData<'a> {
29 match self {
30 Self::Format0(item) => item.offset_data(),
31 Self::Format2(item) => item.offset_data(),
32 Self::Format4(item) => item.offset_data(),
33 Self::Format6(item) => item.offset_data(),
34 Self::Format8(item) => item.offset_data(),
35 Self::Format10(item) => item.offset_data(),
36 }
37 }
38
39 pub fn format(&self) -> u16 {
41 match self {
42 Self::Format0(item) => item.format(),
43 Self::Format2(item) => item.format(),
44 Self::Format4(item) => item.format(),
45 Self::Format6(item) => item.format(),
46 Self::Format8(item) => item.format(),
47 Self::Format10(item) => item.format(),
48 }
49 }
50}
51
52impl ReadArgs for Lookup<'_> {
53 type Args = ();
54}
55
56impl<'a> FontRead<'a> for Lookup<'a> {
57 #[inline]
58 fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
59 let format: u16 = data.read_at(0usize)?;
60 match format {
61 Lookup0::FORMAT => Ok(Self::Format0(FontRead::read(data)?)),
62 Lookup2::FORMAT => Ok(Self::Format2(FontRead::read(data)?)),
63 Lookup4::FORMAT => Ok(Self::Format4(FontRead::read(data)?)),
64 Lookup6::FORMAT => Ok(Self::Format6(FontRead::read(data)?)),
65 Lookup8::FORMAT => Ok(Self::Format8(FontRead::read(data)?)),
66 Lookup10::FORMAT => Ok(Self::Format10(FontRead::read(data)?)),
67 other => Err(ReadError::InvalidFormat(other.into())),
68 }
69 }
70}
71
72impl<'a> MinByteRange<'a> for Lookup<'a> {
73 fn min_byte_range(&self) -> Range<usize> {
74 match self {
75 Self::Format0(item) => item.min_byte_range(),
76 Self::Format2(item) => item.min_byte_range(),
77 Self::Format4(item) => item.min_byte_range(),
78 Self::Format6(item) => item.min_byte_range(),
79 Self::Format8(item) => item.min_byte_range(),
80 Self::Format10(item) => item.min_byte_range(),
81 }
82 }
83 fn min_table_bytes(&self) -> &'a [u8] {
84 match self {
85 Self::Format0(item) => item.min_table_bytes(),
86 Self::Format2(item) => item.min_table_bytes(),
87 Self::Format4(item) => item.min_table_bytes(),
88 Self::Format6(item) => item.min_table_bytes(),
89 Self::Format8(item) => item.min_table_bytes(),
90 Self::Format10(item) => item.min_table_bytes(),
91 }
92 }
93}
94
95impl Format<u16> for Lookup0<'_> {
96 const FORMAT: u16 = 0;
97}
98
99impl<'a> MinByteRange<'a> for Lookup0<'a> {
100 fn min_byte_range(&self) -> Range<usize> {
101 0..self.values_data_byte_range().end
102 }
103 fn min_table_bytes(&self) -> &'a [u8] {
104 let range = self.min_byte_range();
105 self.offset_data().as_bytes().get(range).unwrap_or_default()
106 }
107}
108
109impl ReadArgs for Lookup0<'_> {
110 type Args = ();
111}
112
113impl<'a> FontRead<'a> for Lookup0<'a> {
114 fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
115 #[allow(clippy::absurd_extreme_comparisons)]
116 if data.len() < Self::MIN_SIZE {
117 return Err(ReadError::OutOfBounds);
118 }
119 let Some(data) = TableDataRef::new(data.as_bytes()) else {
120 return Err(ReadError::OutOfBounds);
121 };
122 Ok(Self { data })
123 }
124}
125
126#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
128#[repr(C, packed)]
129#[doc(hidden)]
130pub struct Lookup0FixedHeader {
131 pub format: BigEndian<u16>,
132}
133
134#[allow(clippy::absurd_extreme_comparisons)]
135const _: () = assert!(std::mem::size_of::<Lookup0FixedHeader>() <= Lookup0::MIN_SIZE);
136
137#[derive(Clone)]
140pub struct Lookup0<'a> {
141 data: TableDataRef<'a, Lookup0FixedHeader>,
142}
143
144#[allow(clippy::needless_lifetimes)]
145impl<'a> Lookup0<'a> {
146 pub const MIN_SIZE: usize = u16::RAW_BYTE_LEN;
147 basic_table_impls!(impl_the_methods);
148
149 pub fn format(&self) -> u16 {
151 self.data.header().format.get()
152 }
153
154 pub fn values_data(&self) -> &'a [u8] {
156 let range = self.values_data_byte_range();
157 self.offset_data()
158 .read_array(range)
159 .ok()
160 .unwrap_or_default()
161 }
162
163 pub fn format_byte_range(&self) -> Range<usize> {
164 let start = 0;
165 let end = start + u16::RAW_BYTE_LEN;
166 start..end
167 }
168
169 pub fn values_data_byte_range(&self) -> Range<usize> {
170 let start = self.format_byte_range().end;
171 let end = start
172 + self.offset_data().len().saturating_sub(start) / u8::RAW_BYTE_LEN * u8::RAW_BYTE_LEN;
173 start..end
174 }
175}
176
177const _: () = assert!(FontData::default_data_long_enough(Lookup0::MIN_SIZE));
178
179impl Default for Lookup0<'_> {
180 fn default() -> Self {
181 Self {
182 data: TableDataRef::new(FontData::default_table_data().as_bytes())
183 .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
184 }
185 }
186}
187
188impl Format<u16> for Lookup2<'_> {
189 const FORMAT: u16 = 2;
190}
191
192impl<'a> MinByteRange<'a> for Lookup2<'a> {
193 fn min_byte_range(&self) -> Range<usize> {
194 0..self.segments_data_byte_range().end
195 }
196 fn min_table_bytes(&self) -> &'a [u8] {
197 let range = self.min_byte_range();
198 self.offset_data().as_bytes().get(range).unwrap_or_default()
199 }
200}
201
202impl ReadArgs for Lookup2<'_> {
203 type Args = ();
204}
205
206impl<'a> FontRead<'a> for Lookup2<'a> {
207 fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
208 #[allow(clippy::absurd_extreme_comparisons)]
209 if data.len() < Self::MIN_SIZE {
210 return Err(ReadError::OutOfBounds);
211 }
212 let Some(data) = TableDataRef::new(data.as_bytes()) else {
213 return Err(ReadError::OutOfBounds);
214 };
215 Ok(Self { data })
216 }
217}
218
219#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
221#[repr(C, packed)]
222#[doc(hidden)]
223pub struct Lookup2FixedHeader {
224 pub format: BigEndian<u16>,
225 pub unit_size: BigEndian<u16>,
226 pub n_units: BigEndian<u16>,
227 pub search_range: BigEndian<u16>,
228 pub entry_selector: BigEndian<u16>,
229 pub range_shift: BigEndian<u16>,
230}
231
232#[allow(clippy::absurd_extreme_comparisons)]
233const _: () = assert!(std::mem::size_of::<Lookup2FixedHeader>() <= Lookup2::MIN_SIZE);
234
235#[derive(Clone)]
239pub struct Lookup2<'a> {
240 data: TableDataRef<'a, Lookup2FixedHeader>,
241}
242
243#[allow(clippy::needless_lifetimes)]
244impl<'a> Lookup2<'a> {
245 pub const MIN_SIZE: usize = (u16::RAW_BYTE_LEN
246 + u16::RAW_BYTE_LEN
247 + u16::RAW_BYTE_LEN
248 + u16::RAW_BYTE_LEN
249 + u16::RAW_BYTE_LEN
250 + u16::RAW_BYTE_LEN);
251 basic_table_impls!(impl_the_methods);
252
253 pub fn format(&self) -> u16 {
255 self.data.header().format.get()
256 }
257
258 pub fn unit_size(&self) -> u16 {
260 self.data.header().unit_size.get()
261 }
262
263 pub fn n_units(&self) -> u16 {
265 self.data.header().n_units.get()
266 }
267
268 pub fn search_range(&self) -> u16 {
270 self.data.header().search_range.get()
271 }
272
273 pub fn entry_selector(&self) -> u16 {
275 self.data.header().entry_selector.get()
276 }
277
278 pub fn range_shift(&self) -> u16 {
280 self.data.header().range_shift.get()
281 }
282
283 pub fn segments_data(&self) -> &'a [u8] {
285 let range = self.segments_data_byte_range();
286 self.offset_data()
287 .read_array(range)
288 .ok()
289 .unwrap_or_default()
290 }
291
292 pub fn format_byte_range(&self) -> Range<usize> {
293 let start = 0;
294 let end = start + u16::RAW_BYTE_LEN;
295 start..end
296 }
297
298 pub fn unit_size_byte_range(&self) -> Range<usize> {
299 let start = self.format_byte_range().end;
300 let end = start + u16::RAW_BYTE_LEN;
301 start..end
302 }
303
304 pub fn n_units_byte_range(&self) -> Range<usize> {
305 let start = self.unit_size_byte_range().end;
306 let end = start + u16::RAW_BYTE_LEN;
307 start..end
308 }
309
310 pub fn search_range_byte_range(&self) -> Range<usize> {
311 let start = self.n_units_byte_range().end;
312 let end = start + u16::RAW_BYTE_LEN;
313 start..end
314 }
315
316 pub fn entry_selector_byte_range(&self) -> Range<usize> {
317 let start = self.search_range_byte_range().end;
318 let end = start + u16::RAW_BYTE_LEN;
319 start..end
320 }
321
322 pub fn range_shift_byte_range(&self) -> Range<usize> {
323 let start = self.entry_selector_byte_range().end;
324 let end = start + u16::RAW_BYTE_LEN;
325 start..end
326 }
327
328 pub fn segments_data_byte_range(&self) -> Range<usize> {
329 let unit_size = self.unit_size();
330 let n_units = self.n_units();
331 let start = self.range_shift_byte_range().end;
332 let end = start
333 + (transforms::add_multiply(unit_size, 0_usize, n_units))
334 .saturating_mul(u8::RAW_BYTE_LEN);
335 start..end
336 }
337}
338
339impl Format<u16> for Lookup4<'_> {
340 const FORMAT: u16 = 4;
341}
342
343impl<'a> MinByteRange<'a> for Lookup4<'a> {
344 fn min_byte_range(&self) -> Range<usize> {
345 0..self.segments_byte_range().end
346 }
347 fn min_table_bytes(&self) -> &'a [u8] {
348 let range = self.min_byte_range();
349 self.offset_data().as_bytes().get(range).unwrap_or_default()
350 }
351}
352
353impl ReadArgs for Lookup4<'_> {
354 type Args = ();
355}
356
357impl<'a> FontRead<'a> for Lookup4<'a> {
358 fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
359 #[allow(clippy::absurd_extreme_comparisons)]
360 if data.len() < Self::MIN_SIZE {
361 return Err(ReadError::OutOfBounds);
362 }
363 let Some(data) = TableDataRef::new(data.as_bytes()) else {
364 return Err(ReadError::OutOfBounds);
365 };
366 Ok(Self { data })
367 }
368}
369
370#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
372#[repr(C, packed)]
373#[doc(hidden)]
374pub struct Lookup4FixedHeader {
375 pub format: BigEndian<u16>,
376 pub unit_size: BigEndian<u16>,
377 pub n_units: BigEndian<u16>,
378 pub search_range: BigEndian<u16>,
379 pub entry_selector: BigEndian<u16>,
380 pub range_shift: BigEndian<u16>,
381}
382
383#[allow(clippy::absurd_extreme_comparisons)]
384const _: () = assert!(std::mem::size_of::<Lookup4FixedHeader>() <= Lookup4::MIN_SIZE);
385
386#[derive(Clone)]
390pub struct Lookup4<'a> {
391 data: TableDataRef<'a, Lookup4FixedHeader>,
392}
393
394#[allow(clippy::needless_lifetimes)]
395impl<'a> Lookup4<'a> {
396 pub const MIN_SIZE: usize = (u16::RAW_BYTE_LEN
397 + u16::RAW_BYTE_LEN
398 + u16::RAW_BYTE_LEN
399 + u16::RAW_BYTE_LEN
400 + u16::RAW_BYTE_LEN
401 + u16::RAW_BYTE_LEN);
402 basic_table_impls!(impl_the_methods);
403
404 pub fn format(&self) -> u16 {
406 self.data.header().format.get()
407 }
408
409 pub fn unit_size(&self) -> u16 {
411 self.data.header().unit_size.get()
412 }
413
414 pub fn n_units(&self) -> u16 {
416 self.data.header().n_units.get()
417 }
418
419 pub fn search_range(&self) -> u16 {
421 self.data.header().search_range.get()
422 }
423
424 pub fn entry_selector(&self) -> u16 {
426 self.data.header().entry_selector.get()
427 }
428
429 pub fn range_shift(&self) -> u16 {
431 self.data.header().range_shift.get()
432 }
433
434 pub fn segments(&self) -> &'a [LookupSegment4] {
436 let range = self.segments_byte_range();
437 self.offset_data()
438 .read_array(range)
439 .ok()
440 .unwrap_or_default()
441 }
442
443 pub fn format_byte_range(&self) -> Range<usize> {
444 let start = 0;
445 let end = start + u16::RAW_BYTE_LEN;
446 start..end
447 }
448
449 pub fn unit_size_byte_range(&self) -> Range<usize> {
450 let start = self.format_byte_range().end;
451 let end = start + u16::RAW_BYTE_LEN;
452 start..end
453 }
454
455 pub fn n_units_byte_range(&self) -> Range<usize> {
456 let start = self.unit_size_byte_range().end;
457 let end = start + u16::RAW_BYTE_LEN;
458 start..end
459 }
460
461 pub fn search_range_byte_range(&self) -> Range<usize> {
462 let start = self.n_units_byte_range().end;
463 let end = start + u16::RAW_BYTE_LEN;
464 start..end
465 }
466
467 pub fn entry_selector_byte_range(&self) -> Range<usize> {
468 let start = self.search_range_byte_range().end;
469 let end = start + u16::RAW_BYTE_LEN;
470 start..end
471 }
472
473 pub fn range_shift_byte_range(&self) -> Range<usize> {
474 let start = self.entry_selector_byte_range().end;
475 let end = start + u16::RAW_BYTE_LEN;
476 start..end
477 }
478
479 pub fn segments_byte_range(&self) -> Range<usize> {
480 let n_units = self.n_units();
481 let start = self.range_shift_byte_range().end;
482 let end =
483 start + (transforms::to_usize(n_units)).saturating_mul(LookupSegment4::RAW_BYTE_LEN);
484 start..end
485 }
486}
487
488#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, bytemuck :: AnyBitPattern)]
490#[repr(C)]
491#[repr(packed)]
492pub struct LookupSegment4 {
493 pub last_glyph: BigEndian<u16>,
495 pub first_glyph: BigEndian<u16>,
497 pub value_offset: BigEndian<u16>,
499}
500
501impl LookupSegment4 {
502 pub fn last_glyph(&self) -> u16 {
504 self.last_glyph.get()
505 }
506
507 pub fn first_glyph(&self) -> u16 {
509 self.first_glyph.get()
510 }
511
512 pub fn value_offset(&self) -> u16 {
514 self.value_offset.get()
515 }
516}
517
518impl FixedSize for LookupSegment4 {
519 const RAW_BYTE_LEN: usize = u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN;
520}
521
522impl Format<u16> for Lookup6<'_> {
523 const FORMAT: u16 = 6;
524}
525
526impl<'a> MinByteRange<'a> for Lookup6<'a> {
527 fn min_byte_range(&self) -> Range<usize> {
528 0..self.entries_data_byte_range().end
529 }
530 fn min_table_bytes(&self) -> &'a [u8] {
531 let range = self.min_byte_range();
532 self.offset_data().as_bytes().get(range).unwrap_or_default()
533 }
534}
535
536impl ReadArgs for Lookup6<'_> {
537 type Args = ();
538}
539
540impl<'a> FontRead<'a> for Lookup6<'a> {
541 fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
542 #[allow(clippy::absurd_extreme_comparisons)]
543 if data.len() < Self::MIN_SIZE {
544 return Err(ReadError::OutOfBounds);
545 }
546 let Some(data) = TableDataRef::new(data.as_bytes()) else {
547 return Err(ReadError::OutOfBounds);
548 };
549 Ok(Self { data })
550 }
551}
552
553#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
555#[repr(C, packed)]
556#[doc(hidden)]
557pub struct Lookup6FixedHeader {
558 pub format: BigEndian<u16>,
559 pub unit_size: BigEndian<u16>,
560 pub n_units: BigEndian<u16>,
561 pub search_range: BigEndian<u16>,
562 pub entry_selector: BigEndian<u16>,
563 pub range_shift: BigEndian<u16>,
564}
565
566#[allow(clippy::absurd_extreme_comparisons)]
567const _: () = assert!(std::mem::size_of::<Lookup6FixedHeader>() <= Lookup6::MIN_SIZE);
568
569#[derive(Clone)]
572pub struct Lookup6<'a> {
573 data: TableDataRef<'a, Lookup6FixedHeader>,
574}
575
576#[allow(clippy::needless_lifetimes)]
577impl<'a> Lookup6<'a> {
578 pub const MIN_SIZE: usize = (u16::RAW_BYTE_LEN
579 + u16::RAW_BYTE_LEN
580 + u16::RAW_BYTE_LEN
581 + u16::RAW_BYTE_LEN
582 + u16::RAW_BYTE_LEN
583 + u16::RAW_BYTE_LEN);
584 basic_table_impls!(impl_the_methods);
585
586 pub fn format(&self) -> u16 {
588 self.data.header().format.get()
589 }
590
591 pub fn unit_size(&self) -> u16 {
593 self.data.header().unit_size.get()
594 }
595
596 pub fn n_units(&self) -> u16 {
598 self.data.header().n_units.get()
599 }
600
601 pub fn search_range(&self) -> u16 {
603 self.data.header().search_range.get()
604 }
605
606 pub fn entry_selector(&self) -> u16 {
608 self.data.header().entry_selector.get()
609 }
610
611 pub fn range_shift(&self) -> u16 {
613 self.data.header().range_shift.get()
614 }
615
616 pub fn entries_data(&self) -> &'a [u8] {
618 let range = self.entries_data_byte_range();
619 self.offset_data()
620 .read_array(range)
621 .ok()
622 .unwrap_or_default()
623 }
624
625 pub fn format_byte_range(&self) -> Range<usize> {
626 let start = 0;
627 let end = start + u16::RAW_BYTE_LEN;
628 start..end
629 }
630
631 pub fn unit_size_byte_range(&self) -> Range<usize> {
632 let start = self.format_byte_range().end;
633 let end = start + u16::RAW_BYTE_LEN;
634 start..end
635 }
636
637 pub fn n_units_byte_range(&self) -> Range<usize> {
638 let start = self.unit_size_byte_range().end;
639 let end = start + u16::RAW_BYTE_LEN;
640 start..end
641 }
642
643 pub fn search_range_byte_range(&self) -> Range<usize> {
644 let start = self.n_units_byte_range().end;
645 let end = start + u16::RAW_BYTE_LEN;
646 start..end
647 }
648
649 pub fn entry_selector_byte_range(&self) -> Range<usize> {
650 let start = self.search_range_byte_range().end;
651 let end = start + u16::RAW_BYTE_LEN;
652 start..end
653 }
654
655 pub fn range_shift_byte_range(&self) -> Range<usize> {
656 let start = self.entry_selector_byte_range().end;
657 let end = start + u16::RAW_BYTE_LEN;
658 start..end
659 }
660
661 pub fn entries_data_byte_range(&self) -> Range<usize> {
662 let unit_size = self.unit_size();
663 let n_units = self.n_units();
664 let start = self.range_shift_byte_range().end;
665 let end = start
666 + (transforms::add_multiply(unit_size, 0_usize, n_units))
667 .saturating_mul(u8::RAW_BYTE_LEN);
668 start..end
669 }
670}
671
672impl Format<u16> for Lookup8<'_> {
673 const FORMAT: u16 = 8;
674}
675
676impl<'a> MinByteRange<'a> for Lookup8<'a> {
677 fn min_byte_range(&self) -> Range<usize> {
678 0..self.value_array_byte_range().end
679 }
680 fn min_table_bytes(&self) -> &'a [u8] {
681 let range = self.min_byte_range();
682 self.offset_data().as_bytes().get(range).unwrap_or_default()
683 }
684}
685
686impl ReadArgs for Lookup8<'_> {
687 type Args = ();
688}
689
690impl<'a> FontRead<'a> for Lookup8<'a> {
691 fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
692 #[allow(clippy::absurd_extreme_comparisons)]
693 if data.len() < Self::MIN_SIZE {
694 return Err(ReadError::OutOfBounds);
695 }
696 let Some(data) = TableDataRef::new(data.as_bytes()) else {
697 return Err(ReadError::OutOfBounds);
698 };
699 Ok(Self { data })
700 }
701}
702
703#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
705#[repr(C, packed)]
706#[doc(hidden)]
707pub struct Lookup8FixedHeader {
708 pub format: BigEndian<u16>,
709 pub first_glyph: BigEndian<u16>,
710 pub glyph_count: BigEndian<u16>,
711}
712
713#[allow(clippy::absurd_extreme_comparisons)]
714const _: () = assert!(std::mem::size_of::<Lookup8FixedHeader>() <= Lookup8::MIN_SIZE);
715
716#[derive(Clone)]
719pub struct Lookup8<'a> {
720 data: TableDataRef<'a, Lookup8FixedHeader>,
721}
722
723#[allow(clippy::needless_lifetimes)]
724impl<'a> Lookup8<'a> {
725 pub const MIN_SIZE: usize = (u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN);
726 basic_table_impls!(impl_the_methods);
727
728 pub fn format(&self) -> u16 {
730 self.data.header().format.get()
731 }
732
733 pub fn first_glyph(&self) -> u16 {
735 self.data.header().first_glyph.get()
736 }
737
738 pub fn glyph_count(&self) -> u16 {
741 self.data.header().glyph_count.get()
742 }
743
744 pub fn value_array(&self) -> &'a [BigEndian<u16>] {
747 let range = self.value_array_byte_range();
748 self.offset_data()
749 .read_array(range)
750 .ok()
751 .unwrap_or_default()
752 }
753
754 pub fn format_byte_range(&self) -> Range<usize> {
755 let start = 0;
756 let end = start + u16::RAW_BYTE_LEN;
757 start..end
758 }
759
760 pub fn first_glyph_byte_range(&self) -> Range<usize> {
761 let start = self.format_byte_range().end;
762 let end = start + u16::RAW_BYTE_LEN;
763 start..end
764 }
765
766 pub fn glyph_count_byte_range(&self) -> Range<usize> {
767 let start = self.first_glyph_byte_range().end;
768 let end = start + u16::RAW_BYTE_LEN;
769 start..end
770 }
771
772 pub fn value_array_byte_range(&self) -> Range<usize> {
773 let glyph_count = self.glyph_count();
774 let start = self.glyph_count_byte_range().end;
775 let end = start + (transforms::to_usize(glyph_count)).saturating_mul(u16::RAW_BYTE_LEN);
776 start..end
777 }
778}
779
780impl Format<u16> for Lookup10<'_> {
781 const FORMAT: u16 = 10;
782}
783
784impl<'a> MinByteRange<'a> for Lookup10<'a> {
785 fn min_byte_range(&self) -> Range<usize> {
786 0..self.values_data_byte_range().end
787 }
788 fn min_table_bytes(&self) -> &'a [u8] {
789 let range = self.min_byte_range();
790 self.offset_data().as_bytes().get(range).unwrap_or_default()
791 }
792}
793
794impl ReadArgs for Lookup10<'_> {
795 type Args = ();
796}
797
798impl<'a> FontRead<'a> for Lookup10<'a> {
799 fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
800 #[allow(clippy::absurd_extreme_comparisons)]
801 if data.len() < Self::MIN_SIZE {
802 return Err(ReadError::OutOfBounds);
803 }
804 let Some(data) = TableDataRef::new(data.as_bytes()) else {
805 return Err(ReadError::OutOfBounds);
806 };
807 Ok(Self { data })
808 }
809}
810
811#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
813#[repr(C, packed)]
814#[doc(hidden)]
815pub struct Lookup10FixedHeader {
816 pub format: BigEndian<u16>,
817 pub unit_size: BigEndian<u16>,
818 pub first_glyph: BigEndian<u16>,
819 pub glyph_count: BigEndian<u16>,
820}
821
822#[allow(clippy::absurd_extreme_comparisons)]
823const _: () = assert!(std::mem::size_of::<Lookup10FixedHeader>() <= Lookup10::MIN_SIZE);
824
825#[derive(Clone)]
828pub struct Lookup10<'a> {
829 data: TableDataRef<'a, Lookup10FixedHeader>,
830}
831
832#[allow(clippy::needless_lifetimes)]
833impl<'a> Lookup10<'a> {
834 pub const MIN_SIZE: usize =
835 (u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN);
836 basic_table_impls!(impl_the_methods);
837
838 pub fn format(&self) -> u16 {
840 self.data.header().format.get()
841 }
842
843 pub fn unit_size(&self) -> u16 {
846 self.data.header().unit_size.get()
847 }
848
849 pub fn first_glyph(&self) -> u16 {
851 self.data.header().first_glyph.get()
852 }
853
854 pub fn glyph_count(&self) -> u16 {
857 self.data.header().glyph_count.get()
858 }
859
860 pub fn values_data(&self) -> &'a [u8] {
863 let range = self.values_data_byte_range();
864 self.offset_data()
865 .read_array(range)
866 .ok()
867 .unwrap_or_default()
868 }
869
870 pub fn format_byte_range(&self) -> Range<usize> {
871 let start = 0;
872 let end = start + u16::RAW_BYTE_LEN;
873 start..end
874 }
875
876 pub fn unit_size_byte_range(&self) -> Range<usize> {
877 let start = self.format_byte_range().end;
878 let end = start + u16::RAW_BYTE_LEN;
879 start..end
880 }
881
882 pub fn first_glyph_byte_range(&self) -> Range<usize> {
883 let start = self.unit_size_byte_range().end;
884 let end = start + u16::RAW_BYTE_LEN;
885 start..end
886 }
887
888 pub fn glyph_count_byte_range(&self) -> Range<usize> {
889 let start = self.first_glyph_byte_range().end;
890 let end = start + u16::RAW_BYTE_LEN;
891 start..end
892 }
893
894 pub fn values_data_byte_range(&self) -> Range<usize> {
895 let glyph_count = self.glyph_count();
896 let unit_size = self.unit_size();
897 let start = self.glyph_count_byte_range().end;
898 let end = start
899 + (transforms::add_multiply(glyph_count, 0_usize, unit_size))
900 .saturating_mul(u8::RAW_BYTE_LEN);
901 start..end
902 }
903}
904
905impl<'a> MinByteRange<'a> for StateHeader<'a> {
906 fn min_byte_range(&self) -> Range<usize> {
907 0..self.entry_table_offset_byte_range().end
908 }
909 fn min_table_bytes(&self) -> &'a [u8] {
910 let range = self.min_byte_range();
911 self.offset_data().as_bytes().get(range).unwrap_or_default()
912 }
913}
914
915impl ReadArgs for StateHeader<'_> {
916 type Args = ();
917}
918
919impl<'a> FontRead<'a> for StateHeader<'a> {
920 fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
921 #[allow(clippy::absurd_extreme_comparisons)]
922 if data.len() < Self::MIN_SIZE {
923 return Err(ReadError::OutOfBounds);
924 }
925 let Some(data) = TableDataRef::new(data.as_bytes()) else {
926 return Err(ReadError::OutOfBounds);
927 };
928 Ok(Self { data })
929 }
930}
931
932#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
934#[repr(C, packed)]
935#[doc(hidden)]
936pub struct StateHeaderFixedHeader {
937 pub state_size: BigEndian<u16>,
938 pub class_table_offset: BigEndian<Offset16>,
939 pub state_array_offset: BigEndian<Offset16>,
940 pub entry_table_offset: BigEndian<Offset16>,
941}
942
943#[allow(clippy::absurd_extreme_comparisons)]
944const _: () = assert!(std::mem::size_of::<StateHeaderFixedHeader>() <= StateHeader::MIN_SIZE);
945
946#[derive(Clone)]
948pub struct StateHeader<'a> {
949 data: TableDataRef<'a, StateHeaderFixedHeader>,
950}
951
952#[allow(clippy::needless_lifetimes)]
953impl<'a> StateHeader<'a> {
954 pub const MIN_SIZE: usize = (u16::RAW_BYTE_LEN
955 + Offset16::RAW_BYTE_LEN
956 + Offset16::RAW_BYTE_LEN
957 + Offset16::RAW_BYTE_LEN);
958 basic_table_impls!(impl_the_methods);
959
960 pub fn state_size(&self) -> u16 {
963 self.data.header().state_size.get()
964 }
965
966 pub fn class_table_offset(&self) -> Offset16 {
968 self.data.header().class_table_offset.get()
969 }
970
971 pub fn class_table(&self) -> Result<ClassSubtable<'a>, ReadError> {
973 let data = self.offset_data();
974 self.class_table_offset().resolve(data)
975 }
976
977 pub fn state_array_offset(&self) -> Offset16 {
979 self.data.header().state_array_offset.get()
980 }
981
982 pub fn state_array(&self) -> Result<RawBytes<'a>, ReadError> {
984 let data = self.offset_data();
985 self.state_array_offset().resolve(data)
986 }
987
988 pub fn entry_table_offset(&self) -> Offset16 {
990 self.data.header().entry_table_offset.get()
991 }
992
993 pub fn entry_table(&self) -> Result<RawBytes<'a>, ReadError> {
995 let data = self.offset_data();
996 self.entry_table_offset().resolve(data)
997 }
998
999 pub fn state_size_byte_range(&self) -> Range<usize> {
1000 let start = 0;
1001 let end = start + u16::RAW_BYTE_LEN;
1002 start..end
1003 }
1004
1005 pub fn class_table_offset_byte_range(&self) -> Range<usize> {
1006 let start = self.state_size_byte_range().end;
1007 let end = start + Offset16::RAW_BYTE_LEN;
1008 start..end
1009 }
1010
1011 pub fn state_array_offset_byte_range(&self) -> Range<usize> {
1012 let start = self.class_table_offset_byte_range().end;
1013 let end = start + Offset16::RAW_BYTE_LEN;
1014 start..end
1015 }
1016
1017 pub fn entry_table_offset_byte_range(&self) -> Range<usize> {
1018 let start = self.state_array_offset_byte_range().end;
1019 let end = start + Offset16::RAW_BYTE_LEN;
1020 start..end
1021 }
1022}
1023
1024const _: () = assert!(FontData::default_data_long_enough(StateHeader::MIN_SIZE));
1025
1026impl Default for StateHeader<'_> {
1027 fn default() -> Self {
1028 Self {
1029 data: TableDataRef::new(FontData::default_table_data().as_bytes())
1030 .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
1031 }
1032 }
1033}
1034
1035impl<'a> MinByteRange<'a> for ClassSubtable<'a> {
1036 fn min_byte_range(&self) -> Range<usize> {
1037 0..self.class_array_byte_range().end
1038 }
1039 fn min_table_bytes(&self) -> &'a [u8] {
1040 let range = self.min_byte_range();
1041 self.offset_data().as_bytes().get(range).unwrap_or_default()
1042 }
1043}
1044
1045impl ReadArgs for ClassSubtable<'_> {
1046 type Args = ();
1047}
1048
1049impl<'a> FontRead<'a> for ClassSubtable<'a> {
1050 fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
1051 #[allow(clippy::absurd_extreme_comparisons)]
1052 if data.len() < Self::MIN_SIZE {
1053 return Err(ReadError::OutOfBounds);
1054 }
1055 let Some(data) = TableDataRef::new(data.as_bytes()) else {
1056 return Err(ReadError::OutOfBounds);
1057 };
1058 Ok(Self { data })
1059 }
1060}
1061
1062#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
1064#[repr(C, packed)]
1065#[doc(hidden)]
1066pub struct ClassSubtableFixedHeader {
1067 pub first_glyph: BigEndian<u16>,
1068 pub n_glyphs: BigEndian<u16>,
1069}
1070
1071#[allow(clippy::absurd_extreme_comparisons)]
1072const _: () = assert!(std::mem::size_of::<ClassSubtableFixedHeader>() <= ClassSubtable::MIN_SIZE);
1073
1074#[derive(Clone)]
1076pub struct ClassSubtable<'a> {
1077 data: TableDataRef<'a, ClassSubtableFixedHeader>,
1078}
1079
1080#[allow(clippy::needless_lifetimes)]
1081impl<'a> ClassSubtable<'a> {
1082 pub const MIN_SIZE: usize = (u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN);
1083 basic_table_impls!(impl_the_methods);
1084
1085 pub fn first_glyph(&self) -> u16 {
1087 self.data.header().first_glyph.get()
1088 }
1089
1090 pub fn n_glyphs(&self) -> u16 {
1092 self.data.header().n_glyphs.get()
1093 }
1094
1095 pub fn class_array(&self) -> &'a [u8] {
1098 let range = self.class_array_byte_range();
1099 self.offset_data()
1100 .read_array(range)
1101 .ok()
1102 .unwrap_or_default()
1103 }
1104
1105 pub fn first_glyph_byte_range(&self) -> Range<usize> {
1106 let start = 0;
1107 let end = start + u16::RAW_BYTE_LEN;
1108 start..end
1109 }
1110
1111 pub fn n_glyphs_byte_range(&self) -> Range<usize> {
1112 let start = self.first_glyph_byte_range().end;
1113 let end = start + u16::RAW_BYTE_LEN;
1114 start..end
1115 }
1116
1117 pub fn class_array_byte_range(&self) -> Range<usize> {
1118 let n_glyphs = self.n_glyphs();
1119 let start = self.n_glyphs_byte_range().end;
1120 let end = start + (transforms::to_usize(n_glyphs)).saturating_mul(u8::RAW_BYTE_LEN);
1121 start..end
1122 }
1123}
1124
1125const _: () = assert!(FontData::default_data_long_enough(ClassSubtable::MIN_SIZE));
1126
1127impl Default for ClassSubtable<'_> {
1128 fn default() -> Self {
1129 Self {
1130 data: TableDataRef::new(FontData::default_table_data().as_bytes())
1131 .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
1132 }
1133 }
1134}
1135
1136impl<'a> MinByteRange<'a> for RawBytes<'a> {
1137 fn min_byte_range(&self) -> Range<usize> {
1138 0..self.data_byte_range().end
1139 }
1140 fn min_table_bytes(&self) -> &'a [u8] {
1141 let range = self.min_byte_range();
1142 self.offset_data().as_bytes().get(range).unwrap_or_default()
1143 }
1144}
1145
1146impl ReadArgs for RawBytes<'_> {
1147 type Args = ();
1148}
1149
1150impl<'a> FontRead<'a> for RawBytes<'a> {
1151 fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
1152 #[allow(clippy::absurd_extreme_comparisons)]
1153 if data.len() < Self::MIN_SIZE {
1154 return Err(ReadError::OutOfBounds);
1155 }
1156 let Some(data) = TableDataRef::new(data.as_bytes()) else {
1157 return Err(ReadError::OutOfBounds);
1158 };
1159 Ok(Self { data })
1160 }
1161}
1162
1163#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
1165#[repr(C, packed)]
1166#[doc(hidden)]
1167pub struct RawBytesFixedHeader {}
1168
1169#[allow(clippy::absurd_extreme_comparisons)]
1170const _: () = assert!(std::mem::size_of::<RawBytesFixedHeader>() <= RawBytes::MIN_SIZE);
1171
1172#[derive(Clone)]
1174pub struct RawBytes<'a> {
1175 data: TableDataRef<'a, RawBytesFixedHeader>,
1176}
1177
1178#[allow(clippy::needless_lifetimes)]
1179impl<'a> RawBytes<'a> {
1180 pub const MIN_SIZE: usize = 0;
1181 basic_table_impls!(impl_the_methods);
1182
1183 pub fn data(&self) -> &'a [u8] {
1184 let range = self.data_byte_range();
1185 self.offset_data()
1186 .read_array(range)
1187 .ok()
1188 .unwrap_or_default()
1189 }
1190
1191 pub fn data_byte_range(&self) -> Range<usize> {
1192 let start = 0;
1193 let end = start
1194 + self.offset_data().len().saturating_sub(start) / u8::RAW_BYTE_LEN * u8::RAW_BYTE_LEN;
1195 start..end
1196 }
1197}
1198
1199#[allow(clippy::absurd_extreme_comparisons)]
1200const _: () = assert!(FontData::default_data_long_enough(RawBytes::MIN_SIZE));
1201
1202impl Default for RawBytes<'_> {
1203 fn default() -> Self {
1204 Self {
1205 data: TableDataRef::new(FontData::default_table_data().as_bytes())
1206 .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
1207 }
1208 }
1209}
1210
1211impl<'a> MinByteRange<'a> for StxHeader<'a> {
1212 fn min_byte_range(&self) -> Range<usize> {
1213 0..self.entry_table_offset_byte_range().end
1214 }
1215 fn min_table_bytes(&self) -> &'a [u8] {
1216 let range = self.min_byte_range();
1217 self.offset_data().as_bytes().get(range).unwrap_or_default()
1218 }
1219}
1220
1221impl ReadArgs for StxHeader<'_> {
1222 type Args = ();
1223}
1224
1225impl<'a> FontRead<'a> for StxHeader<'a> {
1226 fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
1227 #[allow(clippy::absurd_extreme_comparisons)]
1228 if data.len() < Self::MIN_SIZE {
1229 return Err(ReadError::OutOfBounds);
1230 }
1231 let Some(data) = TableDataRef::new(data.as_bytes()) else {
1232 return Err(ReadError::OutOfBounds);
1233 };
1234 Ok(Self { data })
1235 }
1236}
1237
1238#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
1240#[repr(C, packed)]
1241#[doc(hidden)]
1242pub struct StxHeaderFixedHeader {
1243 pub n_classes: BigEndian<u32>,
1244 pub class_table_offset: BigEndian<Offset32>,
1245 pub state_array_offset: BigEndian<Offset32>,
1246 pub entry_table_offset: BigEndian<Offset32>,
1247}
1248
1249#[allow(clippy::absurd_extreme_comparisons)]
1250const _: () = assert!(std::mem::size_of::<StxHeaderFixedHeader>() <= StxHeader::MIN_SIZE);
1251
1252#[derive(Clone)]
1254pub struct StxHeader<'a> {
1255 data: TableDataRef<'a, StxHeaderFixedHeader>,
1256}
1257
1258#[allow(clippy::needless_lifetimes)]
1259impl<'a> StxHeader<'a> {
1260 pub const MIN_SIZE: usize = (u32::RAW_BYTE_LEN
1261 + Offset32::RAW_BYTE_LEN
1262 + Offset32::RAW_BYTE_LEN
1263 + Offset32::RAW_BYTE_LEN);
1264 basic_table_impls!(impl_the_methods);
1265
1266 pub fn n_classes(&self) -> u32 {
1268 self.data.header().n_classes.get()
1269 }
1270
1271 pub fn class_table_offset(&self) -> Offset32 {
1273 self.data.header().class_table_offset.get()
1274 }
1275
1276 pub fn class_table(&self) -> Result<LookupU16<'a>, ReadError> {
1278 let data = self.offset_data();
1279 self.class_table_offset().resolve(data)
1280 }
1281
1282 pub fn state_array_offset(&self) -> Offset32 {
1284 self.data.header().state_array_offset.get()
1285 }
1286
1287 pub fn state_array(&self) -> Result<RawWords<'a>, ReadError> {
1289 let data = self.offset_data();
1290 self.state_array_offset().resolve(data)
1291 }
1292
1293 pub fn entry_table_offset(&self) -> Offset32 {
1295 self.data.header().entry_table_offset.get()
1296 }
1297
1298 pub fn entry_table(&self) -> Result<RawBytes<'a>, ReadError> {
1300 let data = self.offset_data();
1301 self.entry_table_offset().resolve(data)
1302 }
1303
1304 pub fn n_classes_byte_range(&self) -> Range<usize> {
1305 let start = 0;
1306 let end = start + u32::RAW_BYTE_LEN;
1307 start..end
1308 }
1309
1310 pub fn class_table_offset_byte_range(&self) -> Range<usize> {
1311 let start = self.n_classes_byte_range().end;
1312 let end = start + Offset32::RAW_BYTE_LEN;
1313 start..end
1314 }
1315
1316 pub fn state_array_offset_byte_range(&self) -> Range<usize> {
1317 let start = self.class_table_offset_byte_range().end;
1318 let end = start + Offset32::RAW_BYTE_LEN;
1319 start..end
1320 }
1321
1322 pub fn entry_table_offset_byte_range(&self) -> Range<usize> {
1323 let start = self.state_array_offset_byte_range().end;
1324 let end = start + Offset32::RAW_BYTE_LEN;
1325 start..end
1326 }
1327}
1328
1329const _: () = assert!(FontData::default_data_long_enough(StxHeader::MIN_SIZE));
1330
1331impl Default for StxHeader<'_> {
1332 fn default() -> Self {
1333 Self {
1334 data: TableDataRef::new(FontData::default_table_data().as_bytes())
1335 .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
1336 }
1337 }
1338}
1339
1340impl<'a> MinByteRange<'a> for RawWords<'a> {
1341 fn min_byte_range(&self) -> Range<usize> {
1342 0..self.data_byte_range().end
1343 }
1344 fn min_table_bytes(&self) -> &'a [u8] {
1345 let range = self.min_byte_range();
1346 self.offset_data().as_bytes().get(range).unwrap_or_default()
1347 }
1348}
1349
1350impl ReadArgs for RawWords<'_> {
1351 type Args = ();
1352}
1353
1354impl<'a> FontRead<'a> for RawWords<'a> {
1355 fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
1356 #[allow(clippy::absurd_extreme_comparisons)]
1357 if data.len() < Self::MIN_SIZE {
1358 return Err(ReadError::OutOfBounds);
1359 }
1360 let Some(data) = TableDataRef::new(data.as_bytes()) else {
1361 return Err(ReadError::OutOfBounds);
1362 };
1363 Ok(Self { data })
1364 }
1365}
1366
1367#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
1369#[repr(C, packed)]
1370#[doc(hidden)]
1371pub struct RawWordsFixedHeader {}
1372
1373#[allow(clippy::absurd_extreme_comparisons)]
1374const _: () = assert!(std::mem::size_of::<RawWordsFixedHeader>() <= RawWords::MIN_SIZE);
1375
1376#[derive(Clone)]
1378pub struct RawWords<'a> {
1379 data: TableDataRef<'a, RawWordsFixedHeader>,
1380}
1381
1382#[allow(clippy::needless_lifetimes)]
1383impl<'a> RawWords<'a> {
1384 pub const MIN_SIZE: usize = 0;
1385 basic_table_impls!(impl_the_methods);
1386
1387 pub fn data(&self) -> &'a [BigEndian<u16>] {
1388 let range = self.data_byte_range();
1389 self.offset_data()
1390 .read_array(range)
1391 .ok()
1392 .unwrap_or_default()
1393 }
1394
1395 pub fn data_byte_range(&self) -> Range<usize> {
1396 let start = 0;
1397 let end = start
1398 + self.offset_data().len().saturating_sub(start) / u16::RAW_BYTE_LEN
1399 * u16::RAW_BYTE_LEN;
1400 start..end
1401 }
1402}
1403
1404#[allow(clippy::absurd_extreme_comparisons)]
1405const _: () = assert!(FontData::default_data_long_enough(RawWords::MIN_SIZE));
1406
1407impl Default for RawWords<'_> {
1408 fn default() -> Self {
1409 Self {
1410 data: TableDataRef::new(FontData::default_table_data().as_bytes())
1411 .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
1412 }
1413 }
1414}