Skip to main content

read_fonts/generated/
generated_bitmap.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
8/// [BitmapSize](https://learn.microsoft.com/en-us/typography/opentype/spec/eblc#bitmapsize-record) record.
9#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, bytemuck :: AnyBitPattern)]
10#[repr(C)]
11#[repr(packed)]
12pub struct BitmapSize {
13    /// Offset to IndexSubtableList, from beginning of EBLC/CBLC.
14    pub index_subtable_list_offset: BigEndian<u32>,
15    /// Total size in bytes of the IndexSubtableList including its array of IndexSubtables.
16    pub index_subtable_list_size: BigEndian<u32>,
17    /// Number of IndexSubtables in the IndexSubtableList.
18    pub number_of_index_subtables: BigEndian<u32>,
19    /// Not used; set to 0.
20    pub color_ref: BigEndian<u32>,
21    /// Line metrics for text rendered horizontally.
22    pub hori: SbitLineMetrics,
23    /// Line metrics for text rendered vertically.
24    pub vert: SbitLineMetrics,
25    /// Lowest glyph index for this size.
26    pub start_glyph_index: BigEndian<GlyphId16>,
27    /// Highest glyph index for this size.
28    pub end_glyph_index: BigEndian<GlyphId16>,
29    /// Horizontal pixels per em.
30    pub ppem_x: u8,
31    /// Vertical pixels per em.
32    pub ppem_y: u8,
33    /// The Microsoft rasterizer v.1.7 or greater supports the following
34    /// bitDepth values, as described below: 1, 2, 4, and 8 (and 32 for CBLC).
35    pub bit_depth: u8,
36    /// Vertical or horizontal.
37    pub flags: BigEndian<BitmapFlags>,
38}
39
40impl BitmapSize {
41    /// Offset to IndexSubtableList, from beginning of EBLC/CBLC.
42    pub fn index_subtable_list_offset(&self) -> u32 {
43        self.index_subtable_list_offset.get()
44    }
45
46    /// Total size in bytes of the IndexSubtableList including its array of IndexSubtables.
47    pub fn index_subtable_list_size(&self) -> u32 {
48        self.index_subtable_list_size.get()
49    }
50
51    /// Number of IndexSubtables in the IndexSubtableList.
52    pub fn number_of_index_subtables(&self) -> u32 {
53        self.number_of_index_subtables.get()
54    }
55
56    /// Not used; set to 0.
57    pub fn color_ref(&self) -> u32 {
58        self.color_ref.get()
59    }
60
61    /// Line metrics for text rendered horizontally.
62    pub fn hori(&self) -> &SbitLineMetrics {
63        &self.hori
64    }
65
66    /// Line metrics for text rendered vertically.
67    pub fn vert(&self) -> &SbitLineMetrics {
68        &self.vert
69    }
70
71    /// Lowest glyph index for this size.
72    pub fn start_glyph_index(&self) -> GlyphId16 {
73        self.start_glyph_index.get()
74    }
75
76    /// Highest glyph index for this size.
77    pub fn end_glyph_index(&self) -> GlyphId16 {
78        self.end_glyph_index.get()
79    }
80
81    /// Horizontal pixels per em.
82    pub fn ppem_x(&self) -> u8 {
83        self.ppem_x
84    }
85
86    /// Vertical pixels per em.
87    pub fn ppem_y(&self) -> u8 {
88        self.ppem_y
89    }
90
91    /// The Microsoft rasterizer v.1.7 or greater supports the following
92    /// bitDepth values, as described below: 1, 2, 4, and 8 (and 32 for CBLC).
93    pub fn bit_depth(&self) -> u8 {
94        self.bit_depth
95    }
96
97    /// Vertical or horizontal.
98    pub fn flags(&self) -> BitmapFlags {
99        self.flags.get()
100    }
101}
102
103impl FixedSize for BitmapSize {
104    const RAW_BYTE_LEN: usize = u32::RAW_BYTE_LEN
105        + u32::RAW_BYTE_LEN
106        + u32::RAW_BYTE_LEN
107        + u32::RAW_BYTE_LEN
108        + SbitLineMetrics::RAW_BYTE_LEN
109        + SbitLineMetrics::RAW_BYTE_LEN
110        + GlyphId16::RAW_BYTE_LEN
111        + GlyphId16::RAW_BYTE_LEN
112        + u8::RAW_BYTE_LEN
113        + u8::RAW_BYTE_LEN
114        + u8::RAW_BYTE_LEN
115        + BitmapFlags::RAW_BYTE_LEN;
116}
117
118/// [SbitLineMetrics](https://learn.microsoft.com/en-us/typography/opentype/spec/eblc#sbitlinemetrics-record) record.
119#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, bytemuck :: AnyBitPattern)]
120#[repr(C)]
121#[repr(packed)]
122pub struct SbitLineMetrics {
123    pub ascender: BigEndian<i8>,
124    pub descender: BigEndian<i8>,
125    pub width_max: u8,
126    pub caret_slope_numerator: BigEndian<i8>,
127    pub caret_slope_denominator: u8,
128    pub caret_offset: BigEndian<i8>,
129    pub min_origin_sb: BigEndian<i8>,
130    pub min_advance_sb: BigEndian<i8>,
131    pub max_before_bl: BigEndian<i8>,
132    pub min_after_bl: BigEndian<i8>,
133    pub pad1: BigEndian<i8>,
134    pub pad2: BigEndian<i8>,
135}
136
137impl SbitLineMetrics {
138    pub fn ascender(&self) -> i8 {
139        self.ascender.get()
140    }
141
142    pub fn descender(&self) -> i8 {
143        self.descender.get()
144    }
145
146    pub fn width_max(&self) -> u8 {
147        self.width_max
148    }
149
150    pub fn caret_slope_numerator(&self) -> i8 {
151        self.caret_slope_numerator.get()
152    }
153
154    pub fn caret_slope_denominator(&self) -> u8 {
155        self.caret_slope_denominator
156    }
157
158    pub fn caret_offset(&self) -> i8 {
159        self.caret_offset.get()
160    }
161
162    pub fn min_origin_sb(&self) -> i8 {
163        self.min_origin_sb.get()
164    }
165
166    pub fn min_advance_sb(&self) -> i8 {
167        self.min_advance_sb.get()
168    }
169
170    pub fn max_before_bl(&self) -> i8 {
171        self.max_before_bl.get()
172    }
173
174    pub fn min_after_bl(&self) -> i8 {
175        self.min_after_bl.get()
176    }
177
178    pub fn pad1(&self) -> i8 {
179        self.pad1.get()
180    }
181
182    pub fn pad2(&self) -> i8 {
183        self.pad2.get()
184    }
185}
186
187impl FixedSize for SbitLineMetrics {
188    const RAW_BYTE_LEN: usize = i8::RAW_BYTE_LEN
189        + i8::RAW_BYTE_LEN
190        + u8::RAW_BYTE_LEN
191        + i8::RAW_BYTE_LEN
192        + u8::RAW_BYTE_LEN
193        + i8::RAW_BYTE_LEN
194        + i8::RAW_BYTE_LEN
195        + i8::RAW_BYTE_LEN
196        + i8::RAW_BYTE_LEN
197        + i8::RAW_BYTE_LEN
198        + i8::RAW_BYTE_LEN
199        + i8::RAW_BYTE_LEN;
200}
201
202/// [Bitmap flags](https://learn.microsoft.com/en-us/typography/opentype/spec/eblc#bitmap-flags).
203#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash, bytemuck :: AnyBitPattern)]
204#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
205#[repr(transparent)]
206pub struct BitmapFlags {
207    bits: u8,
208}
209
210impl BitmapFlags {
211    /// Horizontal
212    pub const HORIZONTAL_METRICS: Self = Self { bits: 0x01 };
213
214    /// Vertical
215    pub const VERTICAL_METRICS: Self = Self { bits: 0x02 };
216}
217
218impl BitmapFlags {
219    ///  Returns an empty set of flags.
220    #[inline]
221    pub const fn empty() -> Self {
222        Self { bits: 0 }
223    }
224
225    /// Returns the set containing all flags.
226    #[inline]
227    pub const fn all() -> Self {
228        Self {
229            bits: Self::HORIZONTAL_METRICS.bits | Self::VERTICAL_METRICS.bits,
230        }
231    }
232
233    /// Returns the raw value of the flags currently stored.
234    #[inline]
235    pub const fn bits(&self) -> u8 {
236        self.bits
237    }
238
239    /// Convert from underlying bit representation, unless that
240    /// representation contains bits that do not correspond to a flag.
241    #[inline]
242    pub const fn from_bits(bits: u8) -> Option<Self> {
243        if (bits & !Self::all().bits()) == 0 {
244            Some(Self { bits })
245        } else {
246            None
247        }
248    }
249
250    /// Convert from underlying bit representation, dropping any bits
251    /// that do not correspond to flags.
252    #[inline]
253    pub const fn from_bits_truncate(bits: u8) -> Self {
254        Self {
255            bits: bits & Self::all().bits,
256        }
257    }
258
259    /// Returns `true` if no flags are currently stored.
260    #[inline]
261    pub const fn is_empty(&self) -> bool {
262        self.bits() == Self::empty().bits()
263    }
264
265    /// Returns `true` if there are flags common to both `self` and `other`.
266    #[inline]
267    pub const fn intersects(&self, other: Self) -> bool {
268        !(Self {
269            bits: self.bits & other.bits,
270        })
271        .is_empty()
272    }
273
274    /// Returns `true` if all of the flags in `other` are contained within `self`.
275    #[inline]
276    pub const fn contains(&self, other: Self) -> bool {
277        (self.bits & other.bits) == other.bits
278    }
279
280    /// Inserts the specified flags in-place.
281    #[inline]
282    pub fn insert(&mut self, other: Self) {
283        self.bits |= other.bits;
284    }
285
286    /// Removes the specified flags in-place.
287    #[inline]
288    pub fn remove(&mut self, other: Self) {
289        self.bits &= !other.bits;
290    }
291
292    /// Toggles the specified flags in-place.
293    #[inline]
294    pub fn toggle(&mut self, other: Self) {
295        self.bits ^= other.bits;
296    }
297
298    /// Returns the intersection between the flags in `self` and
299    /// `other`.
300    ///
301    /// Specifically, the returned set contains only the flags which are
302    /// present in *both* `self` *and* `other`.
303    ///
304    /// This is equivalent to using the `&` operator (e.g.
305    /// [`ops::BitAnd`]), as in `flags & other`.
306    ///
307    /// [`ops::BitAnd`]: https://doc.rust-lang.org/std/ops/trait.BitAnd.html
308    #[inline]
309    #[must_use]
310    pub const fn intersection(self, other: Self) -> Self {
311        Self {
312            bits: self.bits & other.bits,
313        }
314    }
315
316    /// Returns the union of between the flags in `self` and `other`.
317    ///
318    /// Specifically, the returned set contains all flags which are
319    /// present in *either* `self` *or* `other`, including any which are
320    /// present in both.
321    ///
322    /// This is equivalent to using the `|` operator (e.g.
323    /// [`ops::BitOr`]), as in `flags | other`.
324    ///
325    /// [`ops::BitOr`]: https://doc.rust-lang.org/std/ops/trait.BitOr.html
326    #[inline]
327    #[must_use]
328    pub const fn union(self, other: Self) -> Self {
329        Self {
330            bits: self.bits | other.bits,
331        }
332    }
333
334    /// Returns the difference between the flags in `self` and `other`.
335    ///
336    /// Specifically, the returned set contains all flags present in
337    /// `self`, except for the ones present in `other`.
338    ///
339    /// It is also conceptually equivalent to the "bit-clear" operation:
340    /// `flags & !other` (and this syntax is also supported).
341    ///
342    /// This is equivalent to using the `-` operator (e.g.
343    /// [`ops::Sub`]), as in `flags - other`.
344    ///
345    /// [`ops::Sub`]: https://doc.rust-lang.org/std/ops/trait.Sub.html
346    #[inline]
347    #[must_use]
348    pub const fn difference(self, other: Self) -> Self {
349        Self {
350            bits: self.bits & !other.bits,
351        }
352    }
353}
354
355impl std::ops::BitOr for BitmapFlags {
356    type Output = Self;
357
358    /// Returns the union of the two sets of flags.
359    #[inline]
360    fn bitor(self, other: BitmapFlags) -> Self {
361        Self {
362            bits: self.bits | other.bits,
363        }
364    }
365}
366
367impl std::ops::BitOrAssign for BitmapFlags {
368    /// Adds the set of flags.
369    #[inline]
370    fn bitor_assign(&mut self, other: Self) {
371        self.bits |= other.bits;
372    }
373}
374
375impl std::ops::BitXor for BitmapFlags {
376    type Output = Self;
377
378    /// Returns the left flags, but with all the right flags toggled.
379    #[inline]
380    fn bitxor(self, other: Self) -> Self {
381        Self {
382            bits: self.bits ^ other.bits,
383        }
384    }
385}
386
387impl std::ops::BitXorAssign for BitmapFlags {
388    /// Toggles the set of flags.
389    #[inline]
390    fn bitxor_assign(&mut self, other: Self) {
391        self.bits ^= other.bits;
392    }
393}
394
395impl std::ops::BitAnd for BitmapFlags {
396    type Output = Self;
397
398    /// Returns the intersection between the two sets of flags.
399    #[inline]
400    fn bitand(self, other: Self) -> Self {
401        Self {
402            bits: self.bits & other.bits,
403        }
404    }
405}
406
407impl std::ops::BitAndAssign for BitmapFlags {
408    /// Disables all flags disabled in the set.
409    #[inline]
410    fn bitand_assign(&mut self, other: Self) {
411        self.bits &= other.bits;
412    }
413}
414
415impl std::ops::Sub for BitmapFlags {
416    type Output = Self;
417
418    /// Returns the set difference of the two sets of flags.
419    #[inline]
420    fn sub(self, other: Self) -> Self {
421        Self {
422            bits: self.bits & !other.bits,
423        }
424    }
425}
426
427impl std::ops::SubAssign for BitmapFlags {
428    /// Disables all flags enabled in the set.
429    #[inline]
430    fn sub_assign(&mut self, other: Self) {
431        self.bits &= !other.bits;
432    }
433}
434
435impl std::ops::Not for BitmapFlags {
436    type Output = Self;
437
438    /// Returns the complement of this set of flags.
439    #[inline]
440    fn not(self) -> Self {
441        Self { bits: !self.bits } & Self::all()
442    }
443}
444
445impl std::fmt::Debug for BitmapFlags {
446    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
447        let members: &[(&str, Self)] = &[
448            ("HORIZONTAL_METRICS", Self::HORIZONTAL_METRICS),
449            ("VERTICAL_METRICS", Self::VERTICAL_METRICS),
450        ];
451        let mut first = true;
452        for (name, value) in members {
453            if self.contains(*value) {
454                if !first {
455                    f.write_str(" | ")?;
456                }
457                first = false;
458                f.write_str(name)?;
459            }
460        }
461        if first {
462            f.write_str("(empty)")?;
463        }
464        Ok(())
465    }
466}
467
468impl std::fmt::Binary for BitmapFlags {
469    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
470        std::fmt::Binary::fmt(&self.bits, f)
471    }
472}
473
474impl std::fmt::Octal for BitmapFlags {
475    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
476        std::fmt::Octal::fmt(&self.bits, f)
477    }
478}
479
480impl std::fmt::LowerHex for BitmapFlags {
481    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
482        std::fmt::LowerHex::fmt(&self.bits, f)
483    }
484}
485
486impl std::fmt::UpperHex for BitmapFlags {
487    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
488        std::fmt::UpperHex::fmt(&self.bits, f)
489    }
490}
491
492impl font_types::Scalar for BitmapFlags {
493    type Raw = <u8 as font_types::Scalar>::Raw;
494    fn to_raw(self) -> Self::Raw {
495        self.bits().to_raw()
496    }
497    fn from_raw(raw: Self::Raw) -> Self {
498        let t = <u8>::from_raw(raw);
499        Self::from_bits_truncate(t)
500    }
501}
502
503/// [BigGlyphMetrics](https://learn.microsoft.com/en-us/typography/opentype/spec/eblc#bigglyphmetrics) record.
504#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, bytemuck :: AnyBitPattern)]
505#[repr(C)]
506#[repr(packed)]
507pub struct BigGlyphMetrics {
508    /// Number of rows of data.
509    pub height: u8,
510    /// Number of columns of data.
511    pub width: u8,
512    /// Distance in pixels from the horizontal origin to the left edge of the bitmap.
513    pub hori_bearing_x: BigEndian<i8>,
514    /// Distance in pixels from the horizontal origin to the top edge of the bitmap.
515    pub hori_bearing_y: BigEndian<i8>,
516    /// Horizontal advance width in pixels.
517    pub hori_advance: u8,
518    /// Distance in pixels from the vertical origin to the left edge of the bitmap.
519    pub vert_bearing_x: BigEndian<i8>,
520    /// Distance in pixels from the vertical origin to the top edge of the bitmap.
521    pub vert_bearing_y: BigEndian<i8>,
522    /// Vertical advance width in pixels.
523    pub vert_advance: u8,
524}
525
526impl BigGlyphMetrics {
527    /// Number of rows of data.
528    pub fn height(&self) -> u8 {
529        self.height
530    }
531
532    /// Number of columns of data.
533    pub fn width(&self) -> u8 {
534        self.width
535    }
536
537    /// Distance in pixels from the horizontal origin to the left edge of the bitmap.
538    pub fn hori_bearing_x(&self) -> i8 {
539        self.hori_bearing_x.get()
540    }
541
542    /// Distance in pixels from the horizontal origin to the top edge of the bitmap.
543    pub fn hori_bearing_y(&self) -> i8 {
544        self.hori_bearing_y.get()
545    }
546
547    /// Horizontal advance width in pixels.
548    pub fn hori_advance(&self) -> u8 {
549        self.hori_advance
550    }
551
552    /// Distance in pixels from the vertical origin to the left edge of the bitmap.
553    pub fn vert_bearing_x(&self) -> i8 {
554        self.vert_bearing_x.get()
555    }
556
557    /// Distance in pixels from the vertical origin to the top edge of the bitmap.
558    pub fn vert_bearing_y(&self) -> i8 {
559        self.vert_bearing_y.get()
560    }
561
562    /// Vertical advance width in pixels.
563    pub fn vert_advance(&self) -> u8 {
564        self.vert_advance
565    }
566}
567
568impl FixedSize for BigGlyphMetrics {
569    const RAW_BYTE_LEN: usize = u8::RAW_BYTE_LEN
570        + u8::RAW_BYTE_LEN
571        + i8::RAW_BYTE_LEN
572        + i8::RAW_BYTE_LEN
573        + u8::RAW_BYTE_LEN
574        + i8::RAW_BYTE_LEN
575        + i8::RAW_BYTE_LEN
576        + u8::RAW_BYTE_LEN;
577}
578
579/// [SmallGlyphMetrics](https://learn.microsoft.com/en-us/typography/opentype/spec/eblc#smallglyphmetrics) record.
580#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, bytemuck :: AnyBitPattern)]
581#[repr(C)]
582#[repr(packed)]
583pub struct SmallGlyphMetrics {
584    /// Number of rows of data.
585    pub height: u8,
586    /// Number of columns of data.
587    pub width: u8,
588    /// Distance in pixels from the horizontal origin to the left edge of the bitmap (for horizontal text); or distance in pixels from the vertical origin to the top edge of the bitmap (for vertical text).
589    pub bearing_x: BigEndian<i8>,
590    /// Distance in pixels from the horizontal origin to the top edge of the bitmap (for horizontal text); or distance in pixels from the vertical origin to the left edge of the bitmap (for vertical text).
591    pub bearing_y: BigEndian<i8>,
592    /// Horizontal or vertical advance width in pixels.
593    pub advance: u8,
594}
595
596impl SmallGlyphMetrics {
597    /// Number of rows of data.
598    pub fn height(&self) -> u8 {
599        self.height
600    }
601
602    /// Number of columns of data.
603    pub fn width(&self) -> u8 {
604        self.width
605    }
606
607    /// Distance in pixels from the horizontal origin to the left edge of the bitmap (for horizontal text); or distance in pixels from the vertical origin to the top edge of the bitmap (for vertical text).
608    pub fn bearing_x(&self) -> i8 {
609        self.bearing_x.get()
610    }
611
612    /// Distance in pixels from the horizontal origin to the top edge of the bitmap (for horizontal text); or distance in pixels from the vertical origin to the left edge of the bitmap (for vertical text).
613    pub fn bearing_y(&self) -> i8 {
614        self.bearing_y.get()
615    }
616
617    /// Horizontal or vertical advance width in pixels.
618    pub fn advance(&self) -> u8 {
619        self.advance
620    }
621}
622
623impl FixedSize for SmallGlyphMetrics {
624    const RAW_BYTE_LEN: usize = u8::RAW_BYTE_LEN
625        + u8::RAW_BYTE_LEN
626        + i8::RAW_BYTE_LEN
627        + i8::RAW_BYTE_LEN
628        + u8::RAW_BYTE_LEN;
629}
630
631impl<'a> MinByteRange<'a> for IndexSubtableList<'a> {
632    fn min_byte_range(&self) -> Range<usize> {
633        0..self.index_subtable_records_byte_range().end
634    }
635    fn min_table_bytes(&self) -> &'a [u8] {
636        let range = self.min_byte_range();
637        self.offset_data().as_bytes().get(range).unwrap_or_default()
638    }
639}
640
641impl ReadArgs for IndexSubtableList<'_> {
642    type Args = u32;
643}
644
645impl<'a> FontRead<'a> for IndexSubtableList<'a> {
646    fn read_with_args(data: FontData<'a>, args: u32) -> Result<Self, ReadError> {
647        let number_of_index_subtables = args;
648
649        #[allow(clippy::absurd_extreme_comparisons)]
650        if data.len() < Self::MIN_SIZE {
651            return Err(ReadError::OutOfBounds);
652        }
653        let Some(data) = TableDataRef::new(data.as_bytes()) else {
654            return Err(ReadError::OutOfBounds);
655        };
656        Ok(Self {
657            data,
658            number_of_index_subtables,
659        })
660    }
661}
662
663impl<'a> IndexSubtableList<'a> {
664    /// A constructor that requires additional arguments.
665    ///
666    /// This type requires some external state in order to be
667    /// parsed.
668    pub fn read(data: FontData<'a>, number_of_index_subtables: u32) -> Result<Self, ReadError> {
669        let args = number_of_index_subtables;
670        Self::read_with_args(data, args)
671    }
672}
673
674/// The fields at the start of the table, which are read directly.
675#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
676#[repr(C, packed)]
677#[doc(hidden)]
678pub struct IndexSubtableListFixedHeader {}
679
680#[allow(clippy::absurd_extreme_comparisons)]
681const _: () =
682    assert!(std::mem::size_of::<IndexSubtableListFixedHeader>() <= IndexSubtableList::MIN_SIZE);
683
684/// [IndexSubtableList](https://learn.microsoft.com/en-us/typography/opentype/spec/eblc#indexsubtablelist) table.
685#[derive(Clone)]
686pub struct IndexSubtableList<'a> {
687    data: TableDataRef<'a, IndexSubtableListFixedHeader>,
688    number_of_index_subtables: u32,
689}
690
691#[allow(clippy::needless_lifetimes)]
692impl<'a> IndexSubtableList<'a> {
693    pub const MIN_SIZE: usize = 0;
694    basic_table_impls!(impl_the_methods);
695
696    /// Array of IndexSubtableRecords.
697    pub fn index_subtable_records(&self) -> &'a [IndexSubtableRecord] {
698        let range = self.index_subtable_records_byte_range();
699        self.offset_data()
700            .read_array(range)
701            .ok()
702            .unwrap_or_default()
703    }
704
705    pub(crate) fn number_of_index_subtables(&self) -> u32 {
706        self.number_of_index_subtables
707    }
708
709    pub fn index_subtable_records_byte_range(&self) -> Range<usize> {
710        let number_of_index_subtables = self.number_of_index_subtables();
711        let start = 0;
712        let end = start
713            + (transforms::to_usize(number_of_index_subtables))
714                .saturating_mul(IndexSubtableRecord::RAW_BYTE_LEN);
715        start..end
716    }
717}
718
719#[allow(clippy::absurd_extreme_comparisons)]
720const _: () = assert!(FontData::default_data_long_enough(
721    IndexSubtableList::MIN_SIZE
722));
723
724impl Default for IndexSubtableList<'_> {
725    fn default() -> Self {
726        Self {
727            data: TableDataRef::new(FontData::default_table_data().as_bytes())
728                .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
729            number_of_index_subtables: Default::default(),
730        }
731    }
732}
733
734#[derive(Clone, Debug, Copy, bytemuck :: AnyBitPattern)]
735#[repr(C)]
736#[repr(packed)]
737pub struct IndexSubtableRecord {
738    /// First glyph ID of this range.
739    pub first_glyph_index: BigEndian<GlyphId16>,
740    /// Last glyph ID of this range (inclusive).
741    pub last_glyph_index: BigEndian<GlyphId16>,
742    /// Offset to an IndexSubtable from the start of the IndexSubtableList.
743    pub index_subtable_offset: BigEndian<Offset32>,
744}
745
746impl IndexSubtableRecord {
747    /// First glyph ID of this range.
748    pub fn first_glyph_index(&self) -> GlyphId16 {
749        self.first_glyph_index.get()
750    }
751
752    /// Last glyph ID of this range (inclusive).
753    pub fn last_glyph_index(&self) -> GlyphId16 {
754        self.last_glyph_index.get()
755    }
756
757    /// Offset to an IndexSubtable from the start of the IndexSubtableList.
758    pub fn index_subtable_offset(&self) -> Offset32 {
759        self.index_subtable_offset.get()
760    }
761
762    /// Offset to an IndexSubtable from the start of the IndexSubtableList.
763    ///
764    /// The `data` argument should be retrieved from the parent table
765    /// By calling its `offset_data` method.
766    pub fn index_subtable<'a>(&self, data: FontData<'a>) -> Result<IndexSubtable<'a>, ReadError> {
767        let args = (self.last_glyph_index(), self.first_glyph_index());
768        self.index_subtable_offset().resolve_with_args(data, args)
769    }
770}
771
772impl FixedSize for IndexSubtableRecord {
773    const RAW_BYTE_LEN: usize =
774        GlyphId16::RAW_BYTE_LEN + GlyphId16::RAW_BYTE_LEN + Offset32::RAW_BYTE_LEN;
775}
776
777impl Format<u16> for IndexSubtable1<'_> {
778    const FORMAT: u16 = 1;
779}
780
781impl<'a> MinByteRange<'a> for IndexSubtable1<'a> {
782    fn min_byte_range(&self) -> Range<usize> {
783        0..self.sbit_offsets_byte_range().end
784    }
785    fn min_table_bytes(&self) -> &'a [u8] {
786        let range = self.min_byte_range();
787        self.offset_data().as_bytes().get(range).unwrap_or_default()
788    }
789}
790
791impl ReadArgs for IndexSubtable1<'_> {
792    type Args = (GlyphId16, GlyphId16);
793}
794
795impl<'a> FontRead<'a> for IndexSubtable1<'a> {
796    fn read_with_args(data: FontData<'a>, args: (GlyphId16, GlyphId16)) -> Result<Self, ReadError> {
797        let (last_glyph_index, first_glyph_index) = args;
798
799        #[allow(clippy::absurd_extreme_comparisons)]
800        if data.len() < Self::MIN_SIZE {
801            return Err(ReadError::OutOfBounds);
802        }
803        let Some(data) = TableDataRef::new(data.as_bytes()) else {
804            return Err(ReadError::OutOfBounds);
805        };
806        Ok(Self {
807            data,
808            last_glyph_index,
809            first_glyph_index,
810        })
811    }
812}
813
814impl<'a> IndexSubtable1<'a> {
815    /// A constructor that requires additional arguments.
816    ///
817    /// This type requires some external state in order to be
818    /// parsed.
819    pub fn read(
820        data: FontData<'a>,
821        last_glyph_index: GlyphId16,
822        first_glyph_index: GlyphId16,
823    ) -> Result<Self, ReadError> {
824        let args = (last_glyph_index, first_glyph_index);
825        Self::read_with_args(data, args)
826    }
827}
828
829/// The fields at the start of the table, which are read directly.
830#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
831#[repr(C, packed)]
832#[doc(hidden)]
833pub struct IndexSubtable1FixedHeader {
834    pub index_format: BigEndian<u16>,
835    pub image_format: BigEndian<u16>,
836    pub image_data_offset: BigEndian<u32>,
837}
838
839#[allow(clippy::absurd_extreme_comparisons)]
840const _: () = assert!(std::mem::size_of::<IndexSubtable1FixedHeader>() <= IndexSubtable1::MIN_SIZE);
841
842/// [IndexSubTable1](https://learn.microsoft.com/en-us/typography/opentype/spec/eblc#indexsubtable1-variable-metrics-glyphs-with-4-byte-offsets): variable-metrics glyphs with 4-byte offsets.
843#[derive(Clone)]
844pub struct IndexSubtable1<'a> {
845    data: TableDataRef<'a, IndexSubtable1FixedHeader>,
846    last_glyph_index: GlyphId16,
847    first_glyph_index: GlyphId16,
848}
849
850#[allow(clippy::needless_lifetimes)]
851impl<'a> IndexSubtable1<'a> {
852    pub const MIN_SIZE: usize = (u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN + u32::RAW_BYTE_LEN);
853    basic_table_impls!(impl_the_methods);
854
855    /// Format of this IndexSubTable.
856    pub fn index_format(&self) -> u16 {
857        self.data.header().index_format.get()
858    }
859
860    /// Format of EBDT image data.
861    pub fn image_format(&self) -> u16 {
862        self.data.header().image_format.get()
863    }
864
865    /// Offset to image data in EBDT table.
866    pub fn image_data_offset(&self) -> u32 {
867        self.data.header().image_data_offset.get()
868    }
869
870    pub fn sbit_offsets(&self) -> &'a [BigEndian<u32>] {
871        let range = self.sbit_offsets_byte_range();
872        self.offset_data()
873            .read_array(range)
874            .ok()
875            .unwrap_or_default()
876    }
877
878    pub(crate) fn last_glyph_index(&self) -> GlyphId16 {
879        self.last_glyph_index
880    }
881
882    pub(crate) fn first_glyph_index(&self) -> GlyphId16 {
883        self.first_glyph_index
884    }
885
886    pub fn index_format_byte_range(&self) -> Range<usize> {
887        let start = 0;
888        let end = start + u16::RAW_BYTE_LEN;
889        start..end
890    }
891
892    pub fn image_format_byte_range(&self) -> Range<usize> {
893        let start = self.index_format_byte_range().end;
894        let end = start + u16::RAW_BYTE_LEN;
895        start..end
896    }
897
898    pub fn image_data_offset_byte_range(&self) -> Range<usize> {
899        let start = self.image_format_byte_range().end;
900        let end = start + u32::RAW_BYTE_LEN;
901        start..end
902    }
903
904    pub fn sbit_offsets_byte_range(&self) -> Range<usize> {
905        let last_glyph_index = self.last_glyph_index();
906        let first_glyph_index = self.first_glyph_index();
907        let start = self.image_data_offset_byte_range().end;
908        let end = start
909            + (transforms::subtract_add_two(last_glyph_index, first_glyph_index))
910                .saturating_mul(u32::RAW_BYTE_LEN);
911        start..end
912    }
913}
914
915const _: () = assert!(FontData::default_data_long_enough(IndexSubtable1::MIN_SIZE));
916
917impl Default for IndexSubtable1<'_> {
918    fn default() -> Self {
919        Self {
920            data: TableDataRef::new(FontData::default_format_1_u16_table_data().as_bytes())
921                .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
922            last_glyph_index: Default::default(),
923            first_glyph_index: Default::default(),
924        }
925    }
926}
927
928impl Format<u16> for IndexSubtable2<'_> {
929    const FORMAT: u16 = 2;
930}
931
932impl<'a> MinByteRange<'a> for IndexSubtable2<'a> {
933    fn min_byte_range(&self) -> Range<usize> {
934        0..self.big_metrics_byte_range().end
935    }
936    fn min_table_bytes(&self) -> &'a [u8] {
937        let range = self.min_byte_range();
938        self.offset_data().as_bytes().get(range).unwrap_or_default()
939    }
940}
941
942impl ReadArgs for IndexSubtable2<'_> {
943    type Args = ();
944}
945
946impl<'a> FontRead<'a> for IndexSubtable2<'a> {
947    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
948        #[allow(clippy::absurd_extreme_comparisons)]
949        if data.len() < Self::MIN_SIZE {
950            return Err(ReadError::OutOfBounds);
951        }
952        let Some(data) = TableDataRef::new(data.as_bytes()) else {
953            return Err(ReadError::OutOfBounds);
954        };
955        Ok(Self { data })
956    }
957}
958
959/// The fields at the start of the table, which are read directly.
960#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
961#[repr(C, packed)]
962#[doc(hidden)]
963pub struct IndexSubtable2FixedHeader {
964    pub index_format: BigEndian<u16>,
965    pub image_format: BigEndian<u16>,
966    pub image_data_offset: BigEndian<u32>,
967    pub image_size: BigEndian<u32>,
968    pub big_metrics: BigGlyphMetrics,
969}
970
971#[allow(clippy::absurd_extreme_comparisons)]
972const _: () = assert!(std::mem::size_of::<IndexSubtable2FixedHeader>() <= IndexSubtable2::MIN_SIZE);
973
974/// [IndexSubTable2](https://learn.microsoft.com/en-us/typography/opentype/spec/eblc#indexsubtable2-all-glyphs-have-identical-metrics): all glyphs have identical metrics.
975#[derive(Clone)]
976pub struct IndexSubtable2<'a> {
977    data: TableDataRef<'a, IndexSubtable2FixedHeader>,
978}
979
980#[allow(clippy::needless_lifetimes)]
981impl<'a> IndexSubtable2<'a> {
982    pub const MIN_SIZE: usize = (u16::RAW_BYTE_LEN
983        + u16::RAW_BYTE_LEN
984        + u32::RAW_BYTE_LEN
985        + u32::RAW_BYTE_LEN
986        + BigGlyphMetrics::RAW_BYTE_LEN);
987    basic_table_impls!(impl_the_methods);
988
989    /// Format of this IndexSubTable.
990    pub fn index_format(&self) -> u16 {
991        self.data.header().index_format.get()
992    }
993
994    /// Format of EBDT image data.
995    pub fn image_format(&self) -> u16 {
996        self.data.header().image_format.get()
997    }
998
999    /// Offset to image data in EBDT table.
1000    pub fn image_data_offset(&self) -> u32 {
1001        self.data.header().image_data_offset.get()
1002    }
1003
1004    /// All the glyphs are of the same size.
1005    pub fn image_size(&self) -> u32 {
1006        self.data.header().image_size.get()
1007    }
1008
1009    /// All glyphs have the same metrics; glyph data may be compressed, byte-aligned, or bit-aligned.
1010    pub fn big_metrics(&self) -> &'a BigGlyphMetrics {
1011        &self.data.header().big_metrics
1012    }
1013
1014    pub fn index_format_byte_range(&self) -> Range<usize> {
1015        let start = 0;
1016        let end = start + u16::RAW_BYTE_LEN;
1017        start..end
1018    }
1019
1020    pub fn image_format_byte_range(&self) -> Range<usize> {
1021        let start = self.index_format_byte_range().end;
1022        let end = start + u16::RAW_BYTE_LEN;
1023        start..end
1024    }
1025
1026    pub fn image_data_offset_byte_range(&self) -> Range<usize> {
1027        let start = self.image_format_byte_range().end;
1028        let end = start + u32::RAW_BYTE_LEN;
1029        start..end
1030    }
1031
1032    pub fn image_size_byte_range(&self) -> Range<usize> {
1033        let start = self.image_data_offset_byte_range().end;
1034        let end = start + u32::RAW_BYTE_LEN;
1035        start..end
1036    }
1037
1038    pub fn big_metrics_byte_range(&self) -> Range<usize> {
1039        let start = self.image_size_byte_range().end;
1040        let end = start + BigGlyphMetrics::RAW_BYTE_LEN;
1041        start..end
1042    }
1043}
1044
1045impl Format<u16> for IndexSubtable3<'_> {
1046    const FORMAT: u16 = 3;
1047}
1048
1049impl<'a> MinByteRange<'a> for IndexSubtable3<'a> {
1050    fn min_byte_range(&self) -> Range<usize> {
1051        0..self.sbit_offsets_byte_range().end
1052    }
1053    fn min_table_bytes(&self) -> &'a [u8] {
1054        let range = self.min_byte_range();
1055        self.offset_data().as_bytes().get(range).unwrap_or_default()
1056    }
1057}
1058
1059impl ReadArgs for IndexSubtable3<'_> {
1060    type Args = (GlyphId16, GlyphId16);
1061}
1062
1063impl<'a> FontRead<'a> for IndexSubtable3<'a> {
1064    fn read_with_args(data: FontData<'a>, args: (GlyphId16, GlyphId16)) -> Result<Self, ReadError> {
1065        let (last_glyph_index, first_glyph_index) = args;
1066
1067        #[allow(clippy::absurd_extreme_comparisons)]
1068        if data.len() < Self::MIN_SIZE {
1069            return Err(ReadError::OutOfBounds);
1070        }
1071        let Some(data) = TableDataRef::new(data.as_bytes()) else {
1072            return Err(ReadError::OutOfBounds);
1073        };
1074        Ok(Self {
1075            data,
1076            last_glyph_index,
1077            first_glyph_index,
1078        })
1079    }
1080}
1081
1082impl<'a> IndexSubtable3<'a> {
1083    /// A constructor that requires additional arguments.
1084    ///
1085    /// This type requires some external state in order to be
1086    /// parsed.
1087    pub fn read(
1088        data: FontData<'a>,
1089        last_glyph_index: GlyphId16,
1090        first_glyph_index: GlyphId16,
1091    ) -> Result<Self, ReadError> {
1092        let args = (last_glyph_index, first_glyph_index);
1093        Self::read_with_args(data, args)
1094    }
1095}
1096
1097/// The fields at the start of the table, which are read directly.
1098#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
1099#[repr(C, packed)]
1100#[doc(hidden)]
1101pub struct IndexSubtable3FixedHeader {
1102    pub index_format: BigEndian<u16>,
1103    pub image_format: BigEndian<u16>,
1104    pub image_data_offset: BigEndian<u32>,
1105}
1106
1107#[allow(clippy::absurd_extreme_comparisons)]
1108const _: () = assert!(std::mem::size_of::<IndexSubtable3FixedHeader>() <= IndexSubtable3::MIN_SIZE);
1109
1110/// [IndexSubTable3](https://learn.microsoft.com/en-us/typography/opentype/spec/eblc#indexsubtable3-variable-metrics-glyphs-with-2-byte-offsets): variable-metrics glyphs with 2-byte offsets.
1111#[derive(Clone)]
1112pub struct IndexSubtable3<'a> {
1113    data: TableDataRef<'a, IndexSubtable3FixedHeader>,
1114    last_glyph_index: GlyphId16,
1115    first_glyph_index: GlyphId16,
1116}
1117
1118#[allow(clippy::needless_lifetimes)]
1119impl<'a> IndexSubtable3<'a> {
1120    pub const MIN_SIZE: usize = (u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN + u32::RAW_BYTE_LEN);
1121    basic_table_impls!(impl_the_methods);
1122
1123    /// Format of this IndexSubTable.
1124    pub fn index_format(&self) -> u16 {
1125        self.data.header().index_format.get()
1126    }
1127
1128    /// Format of EBDT image data.
1129    pub fn image_format(&self) -> u16 {
1130        self.data.header().image_format.get()
1131    }
1132
1133    /// Offset to image data in EBDT table.
1134    pub fn image_data_offset(&self) -> u32 {
1135        self.data.header().image_data_offset.get()
1136    }
1137
1138    pub fn sbit_offsets(&self) -> &'a [BigEndian<u16>] {
1139        let range = self.sbit_offsets_byte_range();
1140        self.offset_data()
1141            .read_array(range)
1142            .ok()
1143            .unwrap_or_default()
1144    }
1145
1146    pub(crate) fn last_glyph_index(&self) -> GlyphId16 {
1147        self.last_glyph_index
1148    }
1149
1150    pub(crate) fn first_glyph_index(&self) -> GlyphId16 {
1151        self.first_glyph_index
1152    }
1153
1154    pub fn index_format_byte_range(&self) -> Range<usize> {
1155        let start = 0;
1156        let end = start + u16::RAW_BYTE_LEN;
1157        start..end
1158    }
1159
1160    pub fn image_format_byte_range(&self) -> Range<usize> {
1161        let start = self.index_format_byte_range().end;
1162        let end = start + u16::RAW_BYTE_LEN;
1163        start..end
1164    }
1165
1166    pub fn image_data_offset_byte_range(&self) -> Range<usize> {
1167        let start = self.image_format_byte_range().end;
1168        let end = start + u32::RAW_BYTE_LEN;
1169        start..end
1170    }
1171
1172    pub fn sbit_offsets_byte_range(&self) -> Range<usize> {
1173        let last_glyph_index = self.last_glyph_index();
1174        let first_glyph_index = self.first_glyph_index();
1175        let start = self.image_data_offset_byte_range().end;
1176        let end = start
1177            + (transforms::subtract_add_two(last_glyph_index, first_glyph_index))
1178                .saturating_mul(u16::RAW_BYTE_LEN);
1179        start..end
1180    }
1181}
1182
1183impl Format<u16> for IndexSubtable4<'_> {
1184    const FORMAT: u16 = 4;
1185}
1186
1187impl<'a> MinByteRange<'a> for IndexSubtable4<'a> {
1188    fn min_byte_range(&self) -> Range<usize> {
1189        0..self.glyph_array_byte_range().end
1190    }
1191    fn min_table_bytes(&self) -> &'a [u8] {
1192        let range = self.min_byte_range();
1193        self.offset_data().as_bytes().get(range).unwrap_or_default()
1194    }
1195}
1196
1197impl ReadArgs for IndexSubtable4<'_> {
1198    type Args = ();
1199}
1200
1201impl<'a> FontRead<'a> for IndexSubtable4<'a> {
1202    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
1203        #[allow(clippy::absurd_extreme_comparisons)]
1204        if data.len() < Self::MIN_SIZE {
1205            return Err(ReadError::OutOfBounds);
1206        }
1207        let Some(data) = TableDataRef::new(data.as_bytes()) else {
1208            return Err(ReadError::OutOfBounds);
1209        };
1210        Ok(Self { data })
1211    }
1212}
1213
1214/// The fields at the start of the table, which are read directly.
1215#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
1216#[repr(C, packed)]
1217#[doc(hidden)]
1218pub struct IndexSubtable4FixedHeader {
1219    pub index_format: BigEndian<u16>,
1220    pub image_format: BigEndian<u16>,
1221    pub image_data_offset: BigEndian<u32>,
1222    pub num_glyphs: BigEndian<u32>,
1223}
1224
1225#[allow(clippy::absurd_extreme_comparisons)]
1226const _: () = assert!(std::mem::size_of::<IndexSubtable4FixedHeader>() <= IndexSubtable4::MIN_SIZE);
1227
1228/// [IndexSubTable4](https://learn.microsoft.com/en-us/typography/opentype/spec/eblc#indexsubtable3-variable-metrics-glyphs-with-2-byte-offsets): variable-metrics glyphs with sparse glyph codes.
1229#[derive(Clone)]
1230pub struct IndexSubtable4<'a> {
1231    data: TableDataRef<'a, IndexSubtable4FixedHeader>,
1232}
1233
1234#[allow(clippy::needless_lifetimes)]
1235impl<'a> IndexSubtable4<'a> {
1236    pub const MIN_SIZE: usize =
1237        (u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN + u32::RAW_BYTE_LEN + u32::RAW_BYTE_LEN);
1238    basic_table_impls!(impl_the_methods);
1239
1240    /// Format of this IndexSubTable.
1241    pub fn index_format(&self) -> u16 {
1242        self.data.header().index_format.get()
1243    }
1244
1245    /// Format of EBDT image data.
1246    pub fn image_format(&self) -> u16 {
1247        self.data.header().image_format.get()
1248    }
1249
1250    /// Offset to image data in EBDT table.
1251    pub fn image_data_offset(&self) -> u32 {
1252        self.data.header().image_data_offset.get()
1253    }
1254
1255    /// Array length.
1256    pub fn num_glyphs(&self) -> u32 {
1257        self.data.header().num_glyphs.get()
1258    }
1259
1260    /// One per glyph.
1261    pub fn glyph_array(&self) -> &'a [GlyphIdOffsetPair] {
1262        let range = self.glyph_array_byte_range();
1263        self.offset_data()
1264            .read_array(range)
1265            .ok()
1266            .unwrap_or_default()
1267    }
1268
1269    pub fn index_format_byte_range(&self) -> Range<usize> {
1270        let start = 0;
1271        let end = start + u16::RAW_BYTE_LEN;
1272        start..end
1273    }
1274
1275    pub fn image_format_byte_range(&self) -> Range<usize> {
1276        let start = self.index_format_byte_range().end;
1277        let end = start + u16::RAW_BYTE_LEN;
1278        start..end
1279    }
1280
1281    pub fn image_data_offset_byte_range(&self) -> Range<usize> {
1282        let start = self.image_format_byte_range().end;
1283        let end = start + u32::RAW_BYTE_LEN;
1284        start..end
1285    }
1286
1287    pub fn num_glyphs_byte_range(&self) -> Range<usize> {
1288        let start = self.image_data_offset_byte_range().end;
1289        let end = start + u32::RAW_BYTE_LEN;
1290        start..end
1291    }
1292
1293    pub fn glyph_array_byte_range(&self) -> Range<usize> {
1294        let num_glyphs = self.num_glyphs();
1295        let start = self.num_glyphs_byte_range().end;
1296        let end = start
1297            + (transforms::add(num_glyphs, 1_usize))
1298                .saturating_mul(GlyphIdOffsetPair::RAW_BYTE_LEN);
1299        start..end
1300    }
1301}
1302
1303/// [GlyphIdOffsetPair](https://learn.microsoft.com/en-us/typography/opentype/spec/eblc#glyphidoffsetpair-record) record.
1304#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, bytemuck :: AnyBitPattern)]
1305#[repr(C)]
1306#[repr(packed)]
1307pub struct GlyphIdOffsetPair {
1308    /// Glyph ID of glyph present.
1309    pub glyph_id: BigEndian<GlyphId16>,
1310    /// Location in EBDT.
1311    pub sbit_offset: BigEndian<u16>,
1312}
1313
1314impl GlyphIdOffsetPair {
1315    /// Glyph ID of glyph present.
1316    pub fn glyph_id(&self) -> GlyphId16 {
1317        self.glyph_id.get()
1318    }
1319
1320    /// Location in EBDT.
1321    pub fn sbit_offset(&self) -> u16 {
1322        self.sbit_offset.get()
1323    }
1324}
1325
1326impl FixedSize for GlyphIdOffsetPair {
1327    const RAW_BYTE_LEN: usize = GlyphId16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN;
1328}
1329
1330impl Format<u16> for IndexSubtable5<'_> {
1331    const FORMAT: u16 = 5;
1332}
1333
1334impl<'a> MinByteRange<'a> for IndexSubtable5<'a> {
1335    fn min_byte_range(&self) -> Range<usize> {
1336        0..self.glyph_array_byte_range().end
1337    }
1338    fn min_table_bytes(&self) -> &'a [u8] {
1339        let range = self.min_byte_range();
1340        self.offset_data().as_bytes().get(range).unwrap_or_default()
1341    }
1342}
1343
1344impl ReadArgs for IndexSubtable5<'_> {
1345    type Args = ();
1346}
1347
1348impl<'a> FontRead<'a> for IndexSubtable5<'a> {
1349    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
1350        #[allow(clippy::absurd_extreme_comparisons)]
1351        if data.len() < Self::MIN_SIZE {
1352            return Err(ReadError::OutOfBounds);
1353        }
1354        let Some(data) = TableDataRef::new(data.as_bytes()) else {
1355            return Err(ReadError::OutOfBounds);
1356        };
1357        Ok(Self { data })
1358    }
1359}
1360
1361/// The fields at the start of the table, which are read directly.
1362#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
1363#[repr(C, packed)]
1364#[doc(hidden)]
1365pub struct IndexSubtable5FixedHeader {
1366    pub index_format: BigEndian<u16>,
1367    pub image_format: BigEndian<u16>,
1368    pub image_data_offset: BigEndian<u32>,
1369    pub image_size: BigEndian<u32>,
1370    pub big_metrics: BigGlyphMetrics,
1371    pub num_glyphs: BigEndian<u32>,
1372}
1373
1374#[allow(clippy::absurd_extreme_comparisons)]
1375const _: () = assert!(std::mem::size_of::<IndexSubtable5FixedHeader>() <= IndexSubtable5::MIN_SIZE);
1376
1377/// [IndexSubTable5](https://learn.microsoft.com/en-us/typography/opentype/spec/eblc#indexsubtable5-constant-metrics-glyphs-with-sparse-glyph-codes): constant-metrics glyphs with sparse glyph codes
1378#[derive(Clone)]
1379pub struct IndexSubtable5<'a> {
1380    data: TableDataRef<'a, IndexSubtable5FixedHeader>,
1381}
1382
1383#[allow(clippy::needless_lifetimes)]
1384impl<'a> IndexSubtable5<'a> {
1385    pub const MIN_SIZE: usize = (u16::RAW_BYTE_LEN
1386        + u16::RAW_BYTE_LEN
1387        + u32::RAW_BYTE_LEN
1388        + u32::RAW_BYTE_LEN
1389        + BigGlyphMetrics::RAW_BYTE_LEN
1390        + u32::RAW_BYTE_LEN);
1391    basic_table_impls!(impl_the_methods);
1392
1393    /// Format of this IndexSubTable.
1394    pub fn index_format(&self) -> u16 {
1395        self.data.header().index_format.get()
1396    }
1397
1398    /// Format of EBDT image data.
1399    pub fn image_format(&self) -> u16 {
1400        self.data.header().image_format.get()
1401    }
1402
1403    /// Offset to image data in EBDT table.
1404    pub fn image_data_offset(&self) -> u32 {
1405        self.data.header().image_data_offset.get()
1406    }
1407
1408    /// All glyphs have the same data size.
1409    pub fn image_size(&self) -> u32 {
1410        self.data.header().image_size.get()
1411    }
1412
1413    /// All glyphs have the same metrics.
1414    pub fn big_metrics(&self) -> &'a BigGlyphMetrics {
1415        &self.data.header().big_metrics
1416    }
1417
1418    /// Array length.
1419    pub fn num_glyphs(&self) -> u32 {
1420        self.data.header().num_glyphs.get()
1421    }
1422
1423    /// One per glyph, sorted by glyhph ID.
1424    pub fn glyph_array(&self) -> &'a [BigEndian<GlyphId16>] {
1425        let range = self.glyph_array_byte_range();
1426        self.offset_data()
1427            .read_array(range)
1428            .ok()
1429            .unwrap_or_default()
1430    }
1431
1432    pub fn index_format_byte_range(&self) -> Range<usize> {
1433        let start = 0;
1434        let end = start + u16::RAW_BYTE_LEN;
1435        start..end
1436    }
1437
1438    pub fn image_format_byte_range(&self) -> Range<usize> {
1439        let start = self.index_format_byte_range().end;
1440        let end = start + u16::RAW_BYTE_LEN;
1441        start..end
1442    }
1443
1444    pub fn image_data_offset_byte_range(&self) -> Range<usize> {
1445        let start = self.image_format_byte_range().end;
1446        let end = start + u32::RAW_BYTE_LEN;
1447        start..end
1448    }
1449
1450    pub fn image_size_byte_range(&self) -> Range<usize> {
1451        let start = self.image_data_offset_byte_range().end;
1452        let end = start + u32::RAW_BYTE_LEN;
1453        start..end
1454    }
1455
1456    pub fn big_metrics_byte_range(&self) -> Range<usize> {
1457        let start = self.image_size_byte_range().end;
1458        let end = start + BigGlyphMetrics::RAW_BYTE_LEN;
1459        start..end
1460    }
1461
1462    pub fn num_glyphs_byte_range(&self) -> Range<usize> {
1463        let start = self.big_metrics_byte_range().end;
1464        let end = start + u32::RAW_BYTE_LEN;
1465        start..end
1466    }
1467
1468    pub fn glyph_array_byte_range(&self) -> Range<usize> {
1469        let num_glyphs = self.num_glyphs();
1470        let start = self.num_glyphs_byte_range().end;
1471        let end =
1472            start + (transforms::to_usize(num_glyphs)).saturating_mul(GlyphId16::RAW_BYTE_LEN);
1473        start..end
1474    }
1475}
1476
1477/// [EbdtComponent](https://learn.microsoft.com/en-us/typography/opentype/spec/ebdt#ebdtcomponent-record) record.
1478#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, bytemuck :: AnyBitPattern)]
1479#[repr(C)]
1480#[repr(packed)]
1481pub struct BdtComponent {
1482    /// Component glyph ID.
1483    pub glyph_id: BigEndian<GlyphId16>,
1484    /// Position of component left.
1485    pub x_offset: BigEndian<i8>,
1486    /// Position of component top.
1487    pub y_offset: BigEndian<i8>,
1488}
1489
1490impl BdtComponent {
1491    /// Component glyph ID.
1492    pub fn glyph_id(&self) -> GlyphId16 {
1493        self.glyph_id.get()
1494    }
1495
1496    /// Position of component left.
1497    pub fn x_offset(&self) -> i8 {
1498        self.x_offset.get()
1499    }
1500
1501    /// Position of component top.
1502    pub fn y_offset(&self) -> i8 {
1503        self.y_offset.get()
1504    }
1505}
1506
1507impl FixedSize for BdtComponent {
1508    const RAW_BYTE_LEN: usize = GlyphId16::RAW_BYTE_LEN + i8::RAW_BYTE_LEN + i8::RAW_BYTE_LEN;
1509}