1use core::convert::{TryFrom, TryInto};
7use core::ops::Range;
8
9pub trait FromData: Sized {
13 const SIZE: usize;
17
18 fn parse(data: &[u8]) -> Option<Self>;
20}
21
22pub trait FromSlice<'a>: Sized {
26 fn parse(data: &'a [u8]) -> Option<Self>;
28}
29
30impl FromData for () {
31 const SIZE: usize = 0;
32
33 #[inline]
34 fn parse(_: &[u8]) -> Option<Self> {
35 Some(())
36 }
37}
38
39impl FromData for u8 {
40 const SIZE: usize = 1;
41
42 #[inline]
43 fn parse(data: &[u8]) -> Option<Self> {
44 data.get(0).copied()
45 }
46}
47
48impl FromData for i8 {
49 const SIZE: usize = 1;
50
51 #[inline]
52 fn parse(data: &[u8]) -> Option<Self> {
53 data.get(0).copied().map(|n| n as i8)
54 }
55}
56
57impl FromData for u16 {
58 const SIZE: usize = 2;
59
60 #[inline]
61 fn parse(data: &[u8]) -> Option<Self> {
62 data.try_into().ok().map(u16::from_be_bytes)
63 }
64}
65
66impl FromData for i16 {
67 const SIZE: usize = 2;
68
69 #[inline]
70 fn parse(data: &[u8]) -> Option<Self> {
71 data.try_into().ok().map(i16::from_be_bytes)
72 }
73}
74
75impl FromData for u32 {
76 const SIZE: usize = 4;
77
78 #[inline]
79 fn parse(data: &[u8]) -> Option<Self> {
80 data.try_into().ok().map(u32::from_be_bytes)
81 }
82}
83
84impl FromData for i32 {
85 const SIZE: usize = 4;
86
87 #[inline]
88 fn parse(data: &[u8]) -> Option<Self> {
89 data.try_into().ok().map(i32::from_be_bytes)
90 }
91}
92
93impl FromData for u64 {
94 const SIZE: usize = 8;
95
96 #[inline]
97 fn parse(data: &[u8]) -> Option<Self> {
98 data.try_into().ok().map(u64::from_be_bytes)
99 }
100}
101
102#[derive(Clone, Copy, Debug)]
108pub struct U24(pub u32);
109
110impl FromData for U24 {
111 const SIZE: usize = 3;
112
113 #[inline]
114 fn parse(data: &[u8]) -> Option<Self> {
115 let data: [u8; 3] = data.try_into().ok()?;
116 Some(U24(u32::from_be_bytes([0, data[0], data[1], data[2]])))
117 }
118}
119
120#[derive(Clone, Copy, Debug)]
122pub struct F2DOT14(pub i16);
123
124impl F2DOT14 {
125 #[inline]
127 pub fn to_f32(self) -> f32 {
128 f32::from(self.0) / 16384.0
129 }
130
131 #[cfg(feature = "variable-fonts")]
132 #[inline]
133 pub fn apply_float_delta(&self, delta: f32) -> f32 {
134 self.to_f32() + (delta as f64 * (1.0 / 16384.0)) as f32
135 }
136}
137
138impl FromData for F2DOT14 {
139 const SIZE: usize = 2;
140
141 #[inline]
142 fn parse(data: &[u8]) -> Option<Self> {
143 i16::parse(data).map(F2DOT14)
144 }
145}
146
147#[derive(Clone, Copy, Debug)]
149pub struct Fixed(pub f32);
150
151impl FromData for Fixed {
152 const SIZE: usize = 4;
153
154 #[inline]
155 fn parse(data: &[u8]) -> Option<Self> {
156 i32::parse(data).map(|n| Fixed(n as f32 / 65536.0))
158 }
159}
160
161impl Fixed {
162 #[cfg(feature = "variable-fonts")]
163 #[inline]
164 pub(crate) fn apply_float_delta(&self, delta: f32) -> f32 {
165 self.0 + (delta as f64 * (1.0 / 65536.0)) as f32
166 }
167}
168
169pub trait NumFrom<T>: Sized {
175 fn num_from(_: T) -> Self;
177}
178
179impl NumFrom<u32> for usize {
180 #[inline]
181 fn num_from(v: u32) -> Self {
182 #[cfg(any(target_pointer_width = "32", target_pointer_width = "64"))]
183 {
184 v as usize
185 }
186
187 }
189}
190
191impl NumFrom<char> for usize {
192 #[inline]
193 fn num_from(v: char) -> Self {
194 #[cfg(any(target_pointer_width = "32", target_pointer_width = "64"))]
195 {
196 v as usize
197 }
198
199 }
201}
202
203pub trait TryNumFrom<T>: Sized {
205 fn try_num_from(_: T) -> Option<Self>;
207}
208
209impl TryNumFrom<f32> for u8 {
210 #[inline]
211 fn try_num_from(v: f32) -> Option<Self> {
212 i32::try_num_from(v).and_then(|v| u8::try_from(v).ok())
213 }
214}
215
216impl TryNumFrom<f32> for i16 {
217 #[inline]
218 fn try_num_from(v: f32) -> Option<Self> {
219 i32::try_num_from(v).and_then(|v| i16::try_from(v).ok())
220 }
221}
222
223impl TryNumFrom<f32> for u16 {
224 #[inline]
225 fn try_num_from(v: f32) -> Option<Self> {
226 i32::try_num_from(v).and_then(|v| u16::try_from(v).ok())
227 }
228}
229
230#[allow(clippy::manual_range_contains)]
231impl TryNumFrom<f32> for i32 {
232 #[inline]
233 fn try_num_from(v: f32) -> Option<Self> {
234 const MIN: f32 = i32::MIN as f32;
242 const MAX_P1: f32 = i32::MAX as f32;
245 if v >= MIN && v < MAX_P1 {
246 Some(v as i32)
247 } else {
248 None
249 }
250 }
251}
252
253#[derive(Clone, Copy)]
257pub struct LazyArray16<'a, T> {
258 data: &'a [u8],
259 data_type: core::marker::PhantomData<T>,
260}
261
262impl<T> Default for LazyArray16<'_, T> {
263 #[inline]
264 fn default() -> Self {
265 LazyArray16 {
266 data: &[],
267 data_type: core::marker::PhantomData,
268 }
269 }
270}
271
272impl<'a, T: FromData> LazyArray16<'a, T> {
273 #[inline]
275 pub fn new(data: &'a [u8]) -> Self {
276 LazyArray16 {
277 data,
278 data_type: core::marker::PhantomData,
279 }
280 }
281
282 #[inline]
284 pub fn get(&self, index: u16) -> Option<T> {
285 if index < self.len() {
286 let start = usize::from(index) * T::SIZE;
287 let end = start + T::SIZE;
288 self.data.get(start..end).and_then(T::parse)
289 } else {
290 None
291 }
292 }
293
294 #[inline]
296 pub fn last(&self) -> Option<T> {
297 if !self.is_empty() {
298 self.get(self.len() - 1)
299 } else {
300 None
301 }
302 }
303
304 #[inline]
306 pub fn slice(&self, range: Range<u16>) -> Option<Self> {
307 let start = usize::from(range.start) * T::SIZE;
308 let end = usize::from(range.end) * T::SIZE;
309 Some(LazyArray16 {
310 data: self.data.get(start..end)?,
311 ..LazyArray16::default()
312 })
313 }
314
315 #[inline]
317 pub fn len(&self) -> u16 {
318 (self.data.len() / T::SIZE) as u16
319 }
320
321 #[inline]
323 pub fn is_empty(&self) -> bool {
324 self.len() == 0
325 }
326
327 #[inline]
329 pub fn binary_search(&self, key: &T) -> Option<(u16, T)>
330 where
331 T: Ord,
332 {
333 self.binary_search_by(|p| p.cmp(key))
334 }
335
336 #[inline]
338 pub fn binary_search_by<F>(&self, mut f: F) -> Option<(u16, T)>
339 where
340 F: FnMut(&T) -> core::cmp::Ordering,
341 {
342 use core::cmp::Ordering;
345
346 let mut size = self.len();
347 if size == 0 {
348 return None;
349 }
350
351 let mut base = 0;
352 while size > 1 {
353 let half = size / 2;
354 let mid = base + half;
355 let cmp = f(&self.get(mid)?);
359 base = if cmp == Ordering::Greater { base } else { mid };
360 size -= half;
361 }
362
363 let value = self.get(base)?;
365 if f(&value) == Ordering::Equal {
366 Some((base, value))
367 } else {
368 None
369 }
370 }
371}
372
373impl<'a, T: FromData + core::fmt::Debug + Copy> core::fmt::Debug for LazyArray16<'a, T> {
374 fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
375 f.debug_list().entries(*self).finish()
376 }
377}
378
379impl<'a, T: FromData> IntoIterator for LazyArray16<'a, T> {
380 type Item = T;
381 type IntoIter = LazyArrayIter16<'a, T>;
382
383 #[inline]
384 fn into_iter(self) -> Self::IntoIter {
385 LazyArrayIter16 {
386 data: self,
387 index: 0,
388 }
389 }
390}
391
392#[derive(Clone, Copy)]
394#[allow(missing_debug_implementations)]
395pub struct LazyArrayIter16<'a, T> {
396 data: LazyArray16<'a, T>,
397 index: u16,
398}
399
400impl<T: FromData> Default for LazyArrayIter16<'_, T> {
401 #[inline]
402 fn default() -> Self {
403 LazyArrayIter16 {
404 data: LazyArray16::new(&[]),
405 index: 0,
406 }
407 }
408}
409
410impl<'a, T: FromData> Iterator for LazyArrayIter16<'a, T> {
411 type Item = T;
412
413 #[inline]
414 fn next(&mut self) -> Option<Self::Item> {
415 self.index += 1; self.data.get(self.index - 1)
417 }
418
419 #[inline]
420 fn count(self) -> usize {
421 usize::from(self.data.len().saturating_sub(self.index))
422 }
423}
424
425#[derive(Clone, Copy)]
429pub struct LazyArray32<'a, T> {
430 data: &'a [u8],
431 data_type: core::marker::PhantomData<T>,
432}
433
434impl<T> Default for LazyArray32<'_, T> {
435 #[inline]
436 fn default() -> Self {
437 LazyArray32 {
438 data: &[],
439 data_type: core::marker::PhantomData,
440 }
441 }
442}
443
444impl<'a, T: FromData> LazyArray32<'a, T> {
445 #[inline]
447 pub fn new(data: &'a [u8]) -> Self {
448 LazyArray32 {
449 data,
450 data_type: core::marker::PhantomData,
451 }
452 }
453
454 #[inline]
456 pub fn get(&self, index: u32) -> Option<T> {
457 if index < self.len() {
458 let start = usize::num_from(index) * T::SIZE;
459 let end = start + T::SIZE;
460 self.data.get(start..end).and_then(T::parse)
461 } else {
462 None
463 }
464 }
465
466 #[inline]
468 pub fn len(&self) -> u32 {
469 (self.data.len() / T::SIZE) as u32
470 }
471
472 pub fn is_empty(&self) -> bool {
474 self.len() == 0
475 }
476
477 #[inline]
479 pub fn binary_search(&self, key: &T) -> Option<(u32, T)>
480 where
481 T: Ord,
482 {
483 self.binary_search_by(|p| p.cmp(key))
484 }
485
486 #[inline]
488 pub fn binary_search_by<F>(&self, mut f: F) -> Option<(u32, T)>
489 where
490 F: FnMut(&T) -> core::cmp::Ordering,
491 {
492 use core::cmp::Ordering;
495
496 let mut size = self.len();
497 if size == 0 {
498 return None;
499 }
500
501 let mut base = 0;
502 while size > 1 {
503 let half = size / 2;
504 let mid = base + half;
505 let cmp = f(&self.get(mid)?);
509 base = if cmp == Ordering::Greater { base } else { mid };
510 size -= half;
511 }
512
513 let value = self.get(base)?;
515 if f(&value) == Ordering::Equal {
516 Some((base, value))
517 } else {
518 None
519 }
520 }
521}
522
523impl<'a, T: FromData + core::fmt::Debug + Copy> core::fmt::Debug for LazyArray32<'a, T> {
524 fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
525 f.debug_list().entries(*self).finish()
526 }
527}
528
529impl<'a, T: FromData> IntoIterator for LazyArray32<'a, T> {
530 type Item = T;
531 type IntoIter = LazyArrayIter32<'a, T>;
532
533 #[inline]
534 fn into_iter(self) -> Self::IntoIter {
535 LazyArrayIter32 {
536 data: self,
537 index: 0,
538 }
539 }
540}
541
542#[derive(Clone, Copy)]
544#[allow(missing_debug_implementations)]
545pub struct LazyArrayIter32<'a, T> {
546 data: LazyArray32<'a, T>,
547 index: u32,
548}
549
550impl<'a, T: FromData> Iterator for LazyArrayIter32<'a, T> {
551 type Item = T;
552
553 #[inline]
554 fn next(&mut self) -> Option<Self::Item> {
555 self.index += 1; self.data.get(self.index - 1)
557 }
558
559 #[inline]
560 fn count(self) -> usize {
561 usize::num_from(self.data.len().saturating_sub(self.index))
562 }
563}
564
565#[derive(Clone, Copy)]
571pub struct LazyOffsetArray16<'a, T: FromSlice<'a>> {
572 data: &'a [u8],
573 offsets: LazyArray16<'a, Option<Offset16>>,
575 data_type: core::marker::PhantomData<T>,
576}
577
578impl<'a, T: FromSlice<'a>> LazyOffsetArray16<'a, T> {
579 #[allow(dead_code)]
581 pub fn new(data: &'a [u8], offsets: LazyArray16<'a, Option<Offset16>>) -> Self {
582 Self {
583 data,
584 offsets,
585 data_type: core::marker::PhantomData,
586 }
587 }
588
589 #[allow(dead_code)]
591 pub fn parse(data: &'a [u8]) -> Option<Self> {
592 let mut s = Stream::new(data);
593 let count = s.read::<u16>()?;
594 let offsets = s.read_array16(count)?;
595 Some(Self {
596 data,
597 offsets,
598 data_type: core::marker::PhantomData,
599 })
600 }
601
602 #[inline]
604 pub fn get(&self, index: u16) -> Option<T> {
605 let offset = self.offsets.get(index)??.to_usize();
606 self.data.get(offset..).and_then(T::parse)
607 }
608
609 #[inline]
611 pub fn len(&self) -> u16 {
612 self.offsets.len()
613 }
614
615 #[inline]
617 #[allow(dead_code)]
618 pub fn is_empty(&self) -> bool {
619 self.len() == 0
620 }
621}
622
623impl<'a, T: FromSlice<'a> + core::fmt::Debug + Copy> core::fmt::Debug for LazyOffsetArray16<'a, T> {
624 fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
625 f.debug_list().entries(*self).finish()
626 }
627}
628
629#[derive(Clone, Copy)]
631#[allow(missing_debug_implementations)]
632pub struct LazyOffsetArrayIter16<'a, T: FromSlice<'a>> {
633 array: LazyOffsetArray16<'a, T>,
634 index: u16,
635}
636
637impl<'a, T: FromSlice<'a>> IntoIterator for LazyOffsetArray16<'a, T> {
638 type Item = T;
639 type IntoIter = LazyOffsetArrayIter16<'a, T>;
640
641 #[inline]
642 fn into_iter(self) -> Self::IntoIter {
643 LazyOffsetArrayIter16 {
644 array: self,
645 index: 0,
646 }
647 }
648}
649
650impl<'a, T: FromSlice<'a>> Iterator for LazyOffsetArrayIter16<'a, T> {
651 type Item = T;
652
653 fn next(&mut self) -> Option<Self::Item> {
654 if self.index < self.array.len() {
655 self.index += 1;
656 self.array.get(self.index - 1)
657 } else {
658 None
659 }
660 }
661
662 #[inline]
663 fn count(self) -> usize {
664 usize::from(self.array.len().saturating_sub(self.index))
665 }
666}
667
668#[derive(Clone, Default, Debug)]
670pub struct Stream<'a> {
671 data: &'a [u8],
672 offset: usize,
673}
674
675impl<'a> Stream<'a> {
676 #[inline]
678 pub fn new(data: &'a [u8]) -> Self {
679 Stream { data, offset: 0 }
680 }
681
682 #[inline]
686 pub fn new_at(data: &'a [u8], offset: usize) -> Option<Self> {
687 if offset <= data.len() {
688 Some(Stream { data, offset })
689 } else {
690 None
691 }
692 }
693
694 #[inline]
696 pub fn at_end(&self) -> bool {
697 self.offset >= self.data.len()
698 }
699
700 #[inline]
704 pub fn jump_to_end(&mut self) {
705 self.offset = self.data.len();
706 }
707
708 #[inline]
710 pub fn offset(&self) -> usize {
711 self.offset
712 }
713
714 #[inline]
718 pub fn tail(&self) -> Option<&'a [u8]> {
719 self.data.get(self.offset..)
720 }
721
722 #[inline]
726 pub fn skip<T: FromData>(&mut self) {
727 self.advance(T::SIZE);
728 }
729
730 #[inline]
734 pub fn advance(&mut self, len: usize) {
735 self.offset += len;
736 }
737
738 #[inline]
740 pub fn advance_checked(&mut self, len: usize) -> Option<()> {
741 if self.offset + len <= self.data.len() {
742 self.advance(len);
743 Some(())
744 } else {
745 None
746 }
747 }
748
749 #[inline]
754 pub fn read<T: FromData>(&mut self) -> Option<T> {
755 self.read_bytes(T::SIZE).and_then(T::parse)
756 }
757
758 #[inline]
760 pub fn read_at<T: FromData>(data: &[u8], offset: usize) -> Option<T> {
761 data.get(offset..offset + T::SIZE).and_then(T::parse)
762 }
763
764 #[inline]
766 pub fn read_bytes(&mut self, len: usize) -> Option<&'a [u8]> {
767 debug_assert!(self.offset as u64 + len as u64 <= u32::MAX as u64);
771
772 let v = self.data.get(self.offset..self.offset + len)?;
773 self.advance(len);
774 Some(v)
775 }
776
777 #[inline]
779 pub fn read_array16<T: FromData>(&mut self, count: u16) -> Option<LazyArray16<'a, T>> {
780 let len = usize::from(count) * T::SIZE;
781 self.read_bytes(len).map(LazyArray16::new)
782 }
783
784 #[inline]
786 pub fn read_array32<T: FromData>(&mut self, count: u32) -> Option<LazyArray32<'a, T>> {
787 let len = usize::num_from(count) * T::SIZE;
788 self.read_bytes(len).map(LazyArray32::new)
789 }
790
791 #[allow(dead_code)]
792 #[inline]
793 pub fn read_at_offset16(&mut self, data: &'a [u8]) -> Option<&'a [u8]> {
794 let offset = self.read::<Offset16>()?.to_usize();
795 data.get(offset..)
796 }
797}
798
799pub trait Offset {
801 fn to_usize(&self) -> usize;
803}
804
805#[derive(Clone, Copy, Debug)]
807pub struct Offset16(pub u16);
808
809impl Offset for Offset16 {
810 #[inline]
811 fn to_usize(&self) -> usize {
812 usize::from(self.0)
813 }
814}
815
816impl FromData for Offset16 {
817 const SIZE: usize = 2;
818
819 #[inline]
820 fn parse(data: &[u8]) -> Option<Self> {
821 u16::parse(data).map(Offset16)
822 }
823}
824
825impl FromData for Option<Offset16> {
826 const SIZE: usize = Offset16::SIZE;
827
828 #[inline]
829 fn parse(data: &[u8]) -> Option<Self> {
830 let offset = Offset16::parse(data)?;
831 if offset.0 != 0 {
832 Some(Some(offset))
833 } else {
834 Some(None)
835 }
836 }
837}
838
839#[derive(Clone, Copy, Debug)]
841pub struct Offset24(pub u32);
842
843impl Offset for Offset24 {
844 #[inline]
845 fn to_usize(&self) -> usize {
846 usize::num_from(self.0)
847 }
848}
849
850impl FromData for Offset24 {
851 const SIZE: usize = 3;
852
853 #[inline]
854 fn parse(data: &[u8]) -> Option<Self> {
855 U24::parse(data).map(|n| Offset24(n.0))
856 }
857}
858
859impl FromData for Option<Offset24> {
860 const SIZE: usize = Offset24::SIZE;
861
862 #[inline]
863 fn parse(data: &[u8]) -> Option<Self> {
864 let offset = Offset24::parse(data)?;
865 if offset.0 != 0 {
866 Some(Some(offset))
867 } else {
868 Some(None)
869 }
870 }
871}
872
873#[derive(Clone, Copy, Debug)]
875pub struct Offset32(pub u32);
876
877impl Offset for Offset32 {
878 #[inline]
879 fn to_usize(&self) -> usize {
880 usize::num_from(self.0)
881 }
882}
883
884impl FromData for Offset32 {
885 const SIZE: usize = 4;
886
887 #[inline]
888 fn parse(data: &[u8]) -> Option<Self> {
889 u32::parse(data).map(Offset32)
890 }
891}
892
893impl FromData for Option<Offset32> {
894 const SIZE: usize = Offset32::SIZE;
895
896 #[inline]
897 fn parse(data: &[u8]) -> Option<Self> {
898 let offset = Offset32::parse(data)?;
899 if offset.0 != 0 {
900 Some(Some(offset))
901 } else {
902 Some(None)
903 }
904 }
905}
906
907#[inline]
908pub fn i16_bound(min: i16, val: i16, max: i16) -> i16 {
909 use core::cmp;
910 cmp::max(min, cmp::min(max, val))
911}
912
913#[inline]
914pub fn f32_bound(min: f32, val: f32, max: f32) -> f32 {
915 debug_assert!(min.is_finite());
916 debug_assert!(val.is_finite());
917 debug_assert!(max.is_finite());
918
919 if val > max {
920 return max;
921 } else if val < min {
922 return min;
923 }
924
925 val
926}