Skip to main content

read_fonts/generated/
generated_gvar.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
8impl<'a> MinByteRange<'a> for Gvar<'a> {
9    fn min_byte_range(&self) -> Range<usize> {
10        0..self.glyph_variation_data_offsets_byte_range().end
11    }
12    fn min_table_bytes(&self) -> &'a [u8] {
13        let range = self.min_byte_range();
14        self.offset_data().as_bytes().get(range).unwrap_or_default()
15    }
16}
17
18impl TopLevelTable for Gvar<'_> {
19    /// `gvar`
20    const TAG: Tag = Tag::new(b"gvar");
21}
22
23impl ReadArgs for Gvar<'_> {
24    type Args = ();
25}
26
27impl<'a> FontRead<'a> for Gvar<'a> {
28    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
29        #[allow(clippy::absurd_extreme_comparisons)]
30        if data.len() < Self::MIN_SIZE {
31            return Err(ReadError::OutOfBounds);
32        }
33        let Some(data) = TableDataRef::new(data.as_bytes()) else {
34            return Err(ReadError::OutOfBounds);
35        };
36        Ok(Self { data })
37    }
38}
39
40/// The fields at the start of the table, which are read directly.
41#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
42#[repr(C, packed)]
43#[doc(hidden)]
44pub struct GvarFixedHeader {
45    pub version: BigEndian<MajorMinor>,
46    pub axis_count: BigEndian<u16>,
47    pub shared_tuple_count: BigEndian<u16>,
48    pub shared_tuples_offset: BigEndian<Offset32>,
49    pub glyph_count: BigEndian<u16>,
50    pub flags: BigEndian<GvarFlags>,
51    pub glyph_variation_data_array_offset: BigEndian<u32>,
52}
53
54#[allow(clippy::absurd_extreme_comparisons)]
55const _: () = assert!(std::mem::size_of::<GvarFixedHeader>() <= Gvar::MIN_SIZE);
56
57/// The ['gvar' header](https://learn.microsoft.com/en-us/typography/opentype/spec/gvar#gvar-header)
58#[derive(Clone)]
59pub struct Gvar<'a> {
60    data: TableDataRef<'a, GvarFixedHeader>,
61}
62
63#[allow(clippy::needless_lifetimes)]
64impl<'a> Gvar<'a> {
65    pub const MIN_SIZE: usize = (MajorMinor::RAW_BYTE_LEN
66        + u16::RAW_BYTE_LEN
67        + u16::RAW_BYTE_LEN
68        + Offset32::RAW_BYTE_LEN
69        + u16::RAW_BYTE_LEN
70        + GvarFlags::RAW_BYTE_LEN
71        + u32::RAW_BYTE_LEN);
72    basic_table_impls!(impl_the_methods);
73
74    /// Major/minor version number of the glyph variations table — set to (1,0).
75    pub fn version(&self) -> MajorMinor {
76        self.data.header().version.get()
77    }
78
79    /// The number of variation axes for this font. This must be the
80    /// same number as axisCount in the 'fvar' table.
81    pub fn axis_count(&self) -> u16 {
82        self.data.header().axis_count.get()
83    }
84
85    /// The number of shared tuple records. Shared tuple records can be
86    /// referenced within glyph variation data tables for multiple
87    /// glyphs, as opposed to other tuple records stored directly
88    /// within a glyph variation data table.
89    pub fn shared_tuple_count(&self) -> u16 {
90        self.data.header().shared_tuple_count.get()
91    }
92
93    /// The number of glyphs in this font. This must match the number
94    /// of glyphs stored elsewhere in the font.
95    pub fn glyph_count(&self) -> u16 {
96        self.data.header().glyph_count.get()
97    }
98
99    /// Bit-field that gives the format of the offset array that
100    /// follows. If bit 0 is clear, the offsets are uint16; if bit 0 is
101    /// set, the offsets are uint32.
102    pub fn flags(&self) -> GvarFlags {
103        self.data.header().flags.get()
104    }
105
106    /// Offset from the start of this table to the array of
107    /// GlyphVariationData tables.
108    pub fn glyph_variation_data_array_offset(&self) -> u32 {
109        self.data.header().glyph_variation_data_array_offset.get()
110    }
111
112    /// Offsets from the start of the GlyphVariationData array to each
113    /// GlyphVariationData table.
114    pub fn glyph_variation_data_offsets(&self) -> ComputedArray<'a, U16Or32> {
115        let range = self.glyph_variation_data_offsets_byte_range();
116        ComputedArray::new(self.offset_data(), range, self.flags()).unwrap_or_default()
117    }
118
119    pub fn version_byte_range(&self) -> Range<usize> {
120        let start = 0;
121        let end = start + MajorMinor::RAW_BYTE_LEN;
122        start..end
123    }
124
125    pub fn axis_count_byte_range(&self) -> Range<usize> {
126        let start = self.version_byte_range().end;
127        let end = start + u16::RAW_BYTE_LEN;
128        start..end
129    }
130
131    pub fn shared_tuple_count_byte_range(&self) -> Range<usize> {
132        let start = self.axis_count_byte_range().end;
133        let end = start + u16::RAW_BYTE_LEN;
134        start..end
135    }
136
137    pub fn shared_tuples_offset_byte_range(&self) -> Range<usize> {
138        let start = self.shared_tuple_count_byte_range().end;
139        let end = start + Offset32::RAW_BYTE_LEN;
140        start..end
141    }
142
143    pub fn glyph_count_byte_range(&self) -> Range<usize> {
144        let start = self.shared_tuples_offset_byte_range().end;
145        let end = start + u16::RAW_BYTE_LEN;
146        start..end
147    }
148
149    pub fn flags_byte_range(&self) -> Range<usize> {
150        let start = self.glyph_count_byte_range().end;
151        let end = start + GvarFlags::RAW_BYTE_LEN;
152        start..end
153    }
154
155    pub fn glyph_variation_data_array_offset_byte_range(&self) -> Range<usize> {
156        let start = self.flags_byte_range().end;
157        let end = start + u32::RAW_BYTE_LEN;
158        start..end
159    }
160
161    pub fn glyph_variation_data_offsets_byte_range(&self) -> Range<usize> {
162        let glyph_count = self.glyph_count();
163        let start = self.glyph_variation_data_array_offset_byte_range().end;
164        let end = start
165            + (transforms::add(glyph_count, 1_usize))
166                .saturating_mul(<U16Or32 as ComputeSize>::compute_size(self.flags()).unwrap_or(0));
167        start..end
168    }
169}
170
171const _: () = assert!(FontData::default_data_long_enough(Gvar::MIN_SIZE));
172
173impl Default for Gvar<'_> {
174    fn default() -> Self {
175        Self {
176            data: TableDataRef::new(FontData::default_table_data().as_bytes())
177                .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
178        }
179    }
180}
181
182impl<'a> MinByteRange<'a> for GvarExtended<'a> {
183    fn min_byte_range(&self) -> Range<usize> {
184        0..self.glyph_variation_data_offsets_byte_range().end
185    }
186    fn min_table_bytes(&self) -> &'a [u8] {
187        let range = self.min_byte_range();
188        self.offset_data().as_bytes().get(range).unwrap_or_default()
189    }
190}
191
192impl TopLevelTable for GvarExtended<'_> {
193    /// `GVAR`
194    const TAG: Tag = Tag::new(b"GVAR");
195}
196
197impl ReadArgs for GvarExtended<'_> {
198    type Args = ();
199}
200
201impl<'a> FontRead<'a> for GvarExtended<'a> {
202    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
203        #[allow(clippy::absurd_extreme_comparisons)]
204        if data.len() < Self::MIN_SIZE {
205            return Err(ReadError::OutOfBounds);
206        }
207        let Some(data) = TableDataRef::new(data.as_bytes()) else {
208            return Err(ReadError::OutOfBounds);
209        };
210        Ok(Self { data })
211    }
212}
213
214/// The fields at the start of the table, which are read directly.
215#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
216#[repr(C, packed)]
217#[doc(hidden)]
218pub struct GvarExtendedFixedHeader {
219    pub version: BigEndian<MajorMinor>,
220    pub axis_count: BigEndian<u16>,
221    pub shared_tuple_count: BigEndian<u16>,
222    pub shared_tuples_offset: BigEndian<Offset32>,
223    pub glyph_count: BigEndian<Uint24>,
224    pub flags: BigEndian<GvarFlags>,
225    pub glyph_variation_data_array_offset: BigEndian<u32>,
226}
227
228#[allow(clippy::absurd_extreme_comparisons)]
229const _: () = assert!(std::mem::size_of::<GvarExtendedFixedHeader>() <= GvarExtended::MIN_SIZE);
230
231/// GVAR glyph variations table (ISO/IEC 14496-22:2026, 7.3.9).
232#[derive(Clone)]
233pub struct GvarExtended<'a> {
234    data: TableDataRef<'a, GvarExtendedFixedHeader>,
235}
236
237#[allow(clippy::needless_lifetimes)]
238impl<'a> GvarExtended<'a> {
239    pub const MIN_SIZE: usize = (MajorMinor::RAW_BYTE_LEN
240        + u16::RAW_BYTE_LEN
241        + u16::RAW_BYTE_LEN
242        + Offset32::RAW_BYTE_LEN
243        + Uint24::RAW_BYTE_LEN
244        + GvarFlags::RAW_BYTE_LEN
245        + u32::RAW_BYTE_LEN);
246    basic_table_impls!(impl_the_methods);
247
248    pub fn version(&self) -> MajorMinor {
249        self.data.header().version.get()
250    }
251
252    pub fn axis_count(&self) -> u16 {
253        self.data.header().axis_count.get()
254    }
255
256    pub fn shared_tuple_count(&self) -> u16 {
257        self.data.header().shared_tuple_count.get()
258    }
259
260    /// Number of glyphs, equal to numGlyphs in MAXP.
261    pub fn glyph_count(&self) -> Uint24 {
262        self.data.header().glyph_count.get()
263    }
264
265    pub fn flags(&self) -> GvarFlags {
266        self.data.header().flags.get()
267    }
268
269    pub fn glyph_variation_data_array_offset(&self) -> u32 {
270        self.data.header().glyph_variation_data_array_offset.get()
271    }
272
273    pub fn glyph_variation_data_offsets(&self) -> ComputedArray<'a, U16Or32> {
274        let range = self.glyph_variation_data_offsets_byte_range();
275        ComputedArray::new(self.offset_data(), range, self.flags()).unwrap_or_default()
276    }
277
278    pub fn version_byte_range(&self) -> Range<usize> {
279        let start = 0;
280        let end = start + MajorMinor::RAW_BYTE_LEN;
281        start..end
282    }
283
284    pub fn axis_count_byte_range(&self) -> Range<usize> {
285        let start = self.version_byte_range().end;
286        let end = start + u16::RAW_BYTE_LEN;
287        start..end
288    }
289
290    pub fn shared_tuple_count_byte_range(&self) -> Range<usize> {
291        let start = self.axis_count_byte_range().end;
292        let end = start + u16::RAW_BYTE_LEN;
293        start..end
294    }
295
296    pub fn shared_tuples_offset_byte_range(&self) -> Range<usize> {
297        let start = self.shared_tuple_count_byte_range().end;
298        let end = start + Offset32::RAW_BYTE_LEN;
299        start..end
300    }
301
302    pub fn glyph_count_byte_range(&self) -> Range<usize> {
303        let start = self.shared_tuples_offset_byte_range().end;
304        let end = start + Uint24::RAW_BYTE_LEN;
305        start..end
306    }
307
308    pub fn flags_byte_range(&self) -> Range<usize> {
309        let start = self.glyph_count_byte_range().end;
310        let end = start + GvarFlags::RAW_BYTE_LEN;
311        start..end
312    }
313
314    pub fn glyph_variation_data_array_offset_byte_range(&self) -> Range<usize> {
315        let start = self.flags_byte_range().end;
316        let end = start + u32::RAW_BYTE_LEN;
317        start..end
318    }
319
320    pub fn glyph_variation_data_offsets_byte_range(&self) -> Range<usize> {
321        let glyph_count = self.glyph_count();
322        let start = self.glyph_variation_data_array_offset_byte_range().end;
323        let end = start
324            + (transforms::add(glyph_count, 1_usize))
325                .saturating_mul(<U16Or32 as ComputeSize>::compute_size(self.flags()).unwrap_or(0));
326        start..end
327    }
328}
329
330const _: () = assert!(FontData::default_data_long_enough(GvarExtended::MIN_SIZE));
331
332impl Default for GvarExtended<'_> {
333    fn default() -> Self {
334        Self {
335            data: TableDataRef::new(FontData::default_table_data().as_bytes())
336                .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
337        }
338    }
339}
340
341#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash, bytemuck :: AnyBitPattern)]
342#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
343#[repr(transparent)]
344pub struct GvarFlags {
345    bits: u16,
346}
347
348impl GvarFlags {
349    /// If set, offsets to GlyphVariationData are 32 bits
350    pub const LONG_OFFSETS: Self = Self { bits: 1 };
351}
352
353impl GvarFlags {
354    ///  Returns an empty set of flags.
355    #[inline]
356    pub const fn empty() -> Self {
357        Self { bits: 0 }
358    }
359
360    /// Returns the set containing all flags.
361    #[inline]
362    pub const fn all() -> Self {
363        Self {
364            bits: Self::LONG_OFFSETS.bits,
365        }
366    }
367
368    /// Returns the raw value of the flags currently stored.
369    #[inline]
370    pub const fn bits(&self) -> u16 {
371        self.bits
372    }
373
374    /// Convert from underlying bit representation, unless that
375    /// representation contains bits that do not correspond to a flag.
376    #[inline]
377    pub const fn from_bits(bits: u16) -> Option<Self> {
378        if (bits & !Self::all().bits()) == 0 {
379            Some(Self { bits })
380        } else {
381            None
382        }
383    }
384
385    /// Convert from underlying bit representation, dropping any bits
386    /// that do not correspond to flags.
387    #[inline]
388    pub const fn from_bits_truncate(bits: u16) -> Self {
389        Self {
390            bits: bits & Self::all().bits,
391        }
392    }
393
394    /// Returns `true` if no flags are currently stored.
395    #[inline]
396    pub const fn is_empty(&self) -> bool {
397        self.bits() == Self::empty().bits()
398    }
399
400    /// Returns `true` if there are flags common to both `self` and `other`.
401    #[inline]
402    pub const fn intersects(&self, other: Self) -> bool {
403        !(Self {
404            bits: self.bits & other.bits,
405        })
406        .is_empty()
407    }
408
409    /// Returns `true` if all of the flags in `other` are contained within `self`.
410    #[inline]
411    pub const fn contains(&self, other: Self) -> bool {
412        (self.bits & other.bits) == other.bits
413    }
414
415    /// Inserts the specified flags in-place.
416    #[inline]
417    pub fn insert(&mut self, other: Self) {
418        self.bits |= other.bits;
419    }
420
421    /// Removes the specified flags in-place.
422    #[inline]
423    pub fn remove(&mut self, other: Self) {
424        self.bits &= !other.bits;
425    }
426
427    /// Toggles the specified flags in-place.
428    #[inline]
429    pub fn toggle(&mut self, other: Self) {
430        self.bits ^= other.bits;
431    }
432
433    /// Returns the intersection between the flags in `self` and
434    /// `other`.
435    ///
436    /// Specifically, the returned set contains only the flags which are
437    /// present in *both* `self` *and* `other`.
438    ///
439    /// This is equivalent to using the `&` operator (e.g.
440    /// [`ops::BitAnd`]), as in `flags & other`.
441    ///
442    /// [`ops::BitAnd`]: https://doc.rust-lang.org/std/ops/trait.BitAnd.html
443    #[inline]
444    #[must_use]
445    pub const fn intersection(self, other: Self) -> Self {
446        Self {
447            bits: self.bits & other.bits,
448        }
449    }
450
451    /// Returns the union of between the flags in `self` and `other`.
452    ///
453    /// Specifically, the returned set contains all flags which are
454    /// present in *either* `self` *or* `other`, including any which are
455    /// present in both.
456    ///
457    /// This is equivalent to using the `|` operator (e.g.
458    /// [`ops::BitOr`]), as in `flags | other`.
459    ///
460    /// [`ops::BitOr`]: https://doc.rust-lang.org/std/ops/trait.BitOr.html
461    #[inline]
462    #[must_use]
463    pub const fn union(self, other: Self) -> Self {
464        Self {
465            bits: self.bits | other.bits,
466        }
467    }
468
469    /// Returns the difference between the flags in `self` and `other`.
470    ///
471    /// Specifically, the returned set contains all flags present in
472    /// `self`, except for the ones present in `other`.
473    ///
474    /// It is also conceptually equivalent to the "bit-clear" operation:
475    /// `flags & !other` (and this syntax is also supported).
476    ///
477    /// This is equivalent to using the `-` operator (e.g.
478    /// [`ops::Sub`]), as in `flags - other`.
479    ///
480    /// [`ops::Sub`]: https://doc.rust-lang.org/std/ops/trait.Sub.html
481    #[inline]
482    #[must_use]
483    pub const fn difference(self, other: Self) -> Self {
484        Self {
485            bits: self.bits & !other.bits,
486        }
487    }
488}
489
490impl std::ops::BitOr for GvarFlags {
491    type Output = Self;
492
493    /// Returns the union of the two sets of flags.
494    #[inline]
495    fn bitor(self, other: GvarFlags) -> Self {
496        Self {
497            bits: self.bits | other.bits,
498        }
499    }
500}
501
502impl std::ops::BitOrAssign for GvarFlags {
503    /// Adds the set of flags.
504    #[inline]
505    fn bitor_assign(&mut self, other: Self) {
506        self.bits |= other.bits;
507    }
508}
509
510impl std::ops::BitXor for GvarFlags {
511    type Output = Self;
512
513    /// Returns the left flags, but with all the right flags toggled.
514    #[inline]
515    fn bitxor(self, other: Self) -> Self {
516        Self {
517            bits: self.bits ^ other.bits,
518        }
519    }
520}
521
522impl std::ops::BitXorAssign for GvarFlags {
523    /// Toggles the set of flags.
524    #[inline]
525    fn bitxor_assign(&mut self, other: Self) {
526        self.bits ^= other.bits;
527    }
528}
529
530impl std::ops::BitAnd for GvarFlags {
531    type Output = Self;
532
533    /// Returns the intersection between the two sets of flags.
534    #[inline]
535    fn bitand(self, other: Self) -> Self {
536        Self {
537            bits: self.bits & other.bits,
538        }
539    }
540}
541
542impl std::ops::BitAndAssign for GvarFlags {
543    /// Disables all flags disabled in the set.
544    #[inline]
545    fn bitand_assign(&mut self, other: Self) {
546        self.bits &= other.bits;
547    }
548}
549
550impl std::ops::Sub for GvarFlags {
551    type Output = Self;
552
553    /// Returns the set difference of the two sets of flags.
554    #[inline]
555    fn sub(self, other: Self) -> Self {
556        Self {
557            bits: self.bits & !other.bits,
558        }
559    }
560}
561
562impl std::ops::SubAssign for GvarFlags {
563    /// Disables all flags enabled in the set.
564    #[inline]
565    fn sub_assign(&mut self, other: Self) {
566        self.bits &= !other.bits;
567    }
568}
569
570impl std::ops::Not for GvarFlags {
571    type Output = Self;
572
573    /// Returns the complement of this set of flags.
574    #[inline]
575    fn not(self) -> Self {
576        Self { bits: !self.bits } & Self::all()
577    }
578}
579
580impl std::fmt::Debug for GvarFlags {
581    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
582        let members: &[(&str, Self)] = &[("LONG_OFFSETS", Self::LONG_OFFSETS)];
583        let mut first = true;
584        for (name, value) in members {
585            if self.contains(*value) {
586                if !first {
587                    f.write_str(" | ")?;
588                }
589                first = false;
590                f.write_str(name)?;
591            }
592        }
593        if first {
594            f.write_str("(empty)")?;
595        }
596        Ok(())
597    }
598}
599
600impl std::fmt::Binary for GvarFlags {
601    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
602        std::fmt::Binary::fmt(&self.bits, f)
603    }
604}
605
606impl std::fmt::Octal for GvarFlags {
607    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
608        std::fmt::Octal::fmt(&self.bits, f)
609    }
610}
611
612impl std::fmt::LowerHex for GvarFlags {
613    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
614        std::fmt::LowerHex::fmt(&self.bits, f)
615    }
616}
617
618impl std::fmt::UpperHex for GvarFlags {
619    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
620        std::fmt::UpperHex::fmt(&self.bits, f)
621    }
622}
623
624impl font_types::Scalar for GvarFlags {
625    type Raw = <u16 as font_types::Scalar>::Raw;
626    fn to_raw(self) -> Self::Raw {
627        self.bits().to_raw()
628    }
629    fn from_raw(raw: Self::Raw) -> Self {
630        let t = <u16>::from_raw(raw);
631        Self::from_bits_truncate(t)
632    }
633}
634
635impl<'a> MinByteRange<'a> for SharedTuples<'a> {
636    fn min_byte_range(&self) -> Range<usize> {
637        0..self.tuples_byte_range().end
638    }
639    fn min_table_bytes(&self) -> &'a [u8] {
640        let range = self.min_byte_range();
641        self.offset_data().as_bytes().get(range).unwrap_or_default()
642    }
643}
644
645impl ReadArgs for SharedTuples<'_> {
646    type Args = (u16, u16);
647}
648
649impl<'a> FontRead<'a> for SharedTuples<'a> {
650    fn read_with_args(data: FontData<'a>, args: (u16, u16)) -> Result<Self, ReadError> {
651        let (shared_tuple_count, axis_count) = args;
652
653        #[allow(clippy::absurd_extreme_comparisons)]
654        if data.len() < Self::MIN_SIZE {
655            return Err(ReadError::OutOfBounds);
656        }
657        let Some(data) = TableDataRef::new(data.as_bytes()) else {
658            return Err(ReadError::OutOfBounds);
659        };
660        Ok(Self {
661            data,
662            shared_tuple_count,
663            axis_count,
664        })
665    }
666}
667
668impl<'a> SharedTuples<'a> {
669    /// A constructor that requires additional arguments.
670    ///
671    /// This type requires some external state in order to be
672    /// parsed.
673    pub fn read(
674        data: FontData<'a>,
675        shared_tuple_count: u16,
676        axis_count: u16,
677    ) -> Result<Self, ReadError> {
678        let args = (shared_tuple_count, axis_count);
679        Self::read_with_args(data, args)
680    }
681}
682
683/// The fields at the start of the table, which are read directly.
684#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
685#[repr(C, packed)]
686#[doc(hidden)]
687pub struct SharedTuplesFixedHeader {}
688
689#[allow(clippy::absurd_extreme_comparisons)]
690const _: () = assert!(std::mem::size_of::<SharedTuplesFixedHeader>() <= SharedTuples::MIN_SIZE);
691
692/// Array of tuple records shared across all glyph variation data tables.
693#[derive(Clone)]
694pub struct SharedTuples<'a> {
695    data: TableDataRef<'a, SharedTuplesFixedHeader>,
696    shared_tuple_count: u16,
697    axis_count: u16,
698}
699
700#[allow(clippy::needless_lifetimes)]
701impl<'a> SharedTuples<'a> {
702    pub const MIN_SIZE: usize = 0;
703    basic_table_impls!(impl_the_methods);
704
705    pub fn tuples(&self) -> ComputedArray<'a, Tuple<'a>> {
706        let range = self.tuples_byte_range();
707        ComputedArray::new(self.offset_data(), range, self.axis_count()).unwrap_or_default()
708    }
709
710    pub(crate) fn shared_tuple_count(&self) -> u16 {
711        self.shared_tuple_count
712    }
713
714    pub(crate) fn axis_count(&self) -> u16 {
715        self.axis_count
716    }
717
718    pub fn tuples_byte_range(&self) -> Range<usize> {
719        let shared_tuple_count = self.shared_tuple_count();
720        let start = 0;
721        let end = start
722            + (transforms::to_usize(shared_tuple_count)).saturating_mul(
723                <Tuple as ComputeSize>::compute_size(self.axis_count()).unwrap_or(0),
724            );
725        start..end
726    }
727}
728
729#[allow(clippy::absurd_extreme_comparisons)]
730const _: () = assert!(FontData::default_data_long_enough(SharedTuples::MIN_SIZE));
731
732impl Default for SharedTuples<'_> {
733    fn default() -> Self {
734        Self {
735            data: TableDataRef::new(FontData::default_table_data().as_bytes())
736                .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
737            shared_tuple_count: Default::default(),
738            axis_count: Default::default(),
739        }
740    }
741}
742
743impl<'a> MinByteRange<'a> for GlyphVariationDataHeader<'a> {
744    fn min_byte_range(&self) -> Range<usize> {
745        0..self.tuple_variation_headers_byte_range().end
746    }
747    fn min_table_bytes(&self) -> &'a [u8] {
748        let range = self.min_byte_range();
749        self.offset_data().as_bytes().get(range).unwrap_or_default()
750    }
751}
752
753impl ReadArgs for GlyphVariationDataHeader<'_> {
754    type Args = ();
755}
756
757impl<'a> FontRead<'a> for GlyphVariationDataHeader<'a> {
758    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
759        #[allow(clippy::absurd_extreme_comparisons)]
760        if data.len() < Self::MIN_SIZE {
761            return Err(ReadError::OutOfBounds);
762        }
763        let Some(data) = TableDataRef::new(data.as_bytes()) else {
764            return Err(ReadError::OutOfBounds);
765        };
766        Ok(Self { data })
767    }
768}
769
770/// The fields at the start of the table, which are read directly.
771#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
772#[repr(C, packed)]
773#[doc(hidden)]
774pub struct GlyphVariationDataHeaderFixedHeader {
775    pub tuple_variation_count: BigEndian<TupleVariationCount>,
776    pub serialized_data_offset: BigEndian<Offset16>,
777}
778
779#[allow(clippy::absurd_extreme_comparisons)]
780const _: () = assert!(
781    std::mem::size_of::<GlyphVariationDataHeaderFixedHeader>()
782        <= GlyphVariationDataHeader::MIN_SIZE
783);
784
785/// The [GlyphVariationData](https://learn.microsoft.com/en-us/typography/opentype/spec/gvar#the-glyphvariationdata-table-array) table
786#[derive(Clone)]
787pub struct GlyphVariationDataHeader<'a> {
788    data: TableDataRef<'a, GlyphVariationDataHeaderFixedHeader>,
789}
790
791#[allow(clippy::needless_lifetimes)]
792impl<'a> GlyphVariationDataHeader<'a> {
793    pub const MIN_SIZE: usize = (TupleVariationCount::RAW_BYTE_LEN + Offset16::RAW_BYTE_LEN);
794    basic_table_impls!(impl_the_methods);
795
796    /// A packed field. The high 4 bits are flags, and the low 12 bits
797    /// are the number of tuple variation tables for this glyph. The
798    /// number of tuple variation tables can be any number between 1
799    /// and 4095.
800    pub fn tuple_variation_count(&self) -> TupleVariationCount {
801        self.data.header().tuple_variation_count.get()
802    }
803
804    /// Offset from the start of the GlyphVariationData table to the
805    /// serialized data
806    pub fn serialized_data_offset(&self) -> Offset16 {
807        self.data.header().serialized_data_offset.get()
808    }
809
810    /// Attempt to resolve [`serialized_data_offset`][Self::serialized_data_offset].
811    pub fn serialized_data(&self) -> Result<FontData<'a>, ReadError> {
812        let data = self.offset_data();
813        self.serialized_data_offset().resolve(data)
814    }
815
816    /// Array of tuple variation headers.
817    pub fn tuple_variation_headers(&self) -> VarLenArray<'a, TupleVariationHeader<'a>> {
818        let range = self.tuple_variation_headers_byte_range();
819        self.offset_data()
820            .split_off(range.start)
821            .and_then(|d| VarLenArray::read(d).ok())
822            .unwrap_or_default()
823    }
824
825    pub fn tuple_variation_count_byte_range(&self) -> Range<usize> {
826        let start = 0;
827        let end = start + TupleVariationCount::RAW_BYTE_LEN;
828        start..end
829    }
830
831    pub fn serialized_data_offset_byte_range(&self) -> Range<usize> {
832        let start = self.tuple_variation_count_byte_range().end;
833        let end = start + Offset16::RAW_BYTE_LEN;
834        start..end
835    }
836
837    pub fn tuple_variation_headers_byte_range(&self) -> Range<usize> {
838        let start = self.serialized_data_offset_byte_range().end;
839        let end = start + self.offset_data().len().saturating_sub(start);
840        start..end
841    }
842}
843
844const _: () = assert!(FontData::default_data_long_enough(
845    GlyphVariationDataHeader::MIN_SIZE
846));
847
848impl Default for GlyphVariationDataHeader<'_> {
849    fn default() -> Self {
850        Self {
851            data: TableDataRef::new(FontData::default_table_data().as_bytes())
852                .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
853        }
854    }
855}
856
857impl<'a> MinByteRange<'a> for GlyphVariationDataHeaderExtended<'a> {
858    fn min_byte_range(&self) -> Range<usize> {
859        0..self.tuple_variation_headers_byte_range().end
860    }
861    fn min_table_bytes(&self) -> &'a [u8] {
862        let range = self.min_byte_range();
863        self.offset_data().as_bytes().get(range).unwrap_or_default()
864    }
865}
866
867impl ReadArgs for GlyphVariationDataHeaderExtended<'_> {
868    type Args = ();
869}
870
871impl<'a> FontRead<'a> for GlyphVariationDataHeaderExtended<'a> {
872    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
873        #[allow(clippy::absurd_extreme_comparisons)]
874        if data.len() < Self::MIN_SIZE {
875            return Err(ReadError::OutOfBounds);
876        }
877        let Some(data) = TableDataRef::new(data.as_bytes()) else {
878            return Err(ReadError::OutOfBounds);
879        };
880        Ok(Self { data })
881    }
882}
883
884/// The fields at the start of the table, which are read directly.
885#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
886#[repr(C, packed)]
887#[doc(hidden)]
888pub struct GlyphVariationDataHeaderExtendedFixedHeader {
889    pub tuple_variation_count: BigEndian<TupleVariationCount>,
890    pub serialized_data_offset: BigEndian<Offset24>,
891}
892
893#[allow(clippy::absurd_extreme_comparisons)]
894const _: () = assert!(
895    std::mem::size_of::<GlyphVariationDataHeaderExtendedFixedHeader>()
896        <= GlyphVariationDataHeaderExtended::MIN_SIZE
897);
898
899/// Per-glyph variation header in GVAR (ISO/IEC 14496-22:2026, 7.3.9.1.3).
900#[derive(Clone)]
901pub struct GlyphVariationDataHeaderExtended<'a> {
902    data: TableDataRef<'a, GlyphVariationDataHeaderExtendedFixedHeader>,
903}
904
905#[allow(clippy::needless_lifetimes)]
906impl<'a> GlyphVariationDataHeaderExtended<'a> {
907    pub const MIN_SIZE: usize = (TupleVariationCount::RAW_BYTE_LEN + Offset24::RAW_BYTE_LEN);
908    basic_table_impls!(impl_the_methods);
909
910    pub fn tuple_variation_count(&self) -> TupleVariationCount {
911        self.data.header().tuple_variation_count.get()
912    }
913
914    pub fn serialized_data_offset(&self) -> Offset24 {
915        self.data.header().serialized_data_offset.get()
916    }
917
918    /// Attempt to resolve [`serialized_data_offset`][Self::serialized_data_offset].
919    pub fn serialized_data(&self) -> Result<FontData<'a>, ReadError> {
920        let data = self.offset_data();
921        self.serialized_data_offset().resolve(data)
922    }
923
924    pub fn tuple_variation_headers(&self) -> VarLenArray<'a, TupleVariationHeader<'a>> {
925        let range = self.tuple_variation_headers_byte_range();
926        self.offset_data()
927            .split_off(range.start)
928            .and_then(|d| VarLenArray::read(d).ok())
929            .unwrap_or_default()
930    }
931
932    pub fn tuple_variation_count_byte_range(&self) -> Range<usize> {
933        let start = 0;
934        let end = start + TupleVariationCount::RAW_BYTE_LEN;
935        start..end
936    }
937
938    pub fn serialized_data_offset_byte_range(&self) -> Range<usize> {
939        let start = self.tuple_variation_count_byte_range().end;
940        let end = start + Offset24::RAW_BYTE_LEN;
941        start..end
942    }
943
944    pub fn tuple_variation_headers_byte_range(&self) -> Range<usize> {
945        let start = self.serialized_data_offset_byte_range().end;
946        let end = start + self.offset_data().len().saturating_sub(start);
947        start..end
948    }
949}
950
951const _: () = assert!(FontData::default_data_long_enough(
952    GlyphVariationDataHeaderExtended::MIN_SIZE
953));
954
955impl Default for GlyphVariationDataHeaderExtended<'_> {
956    fn default() -> Self {
957        Self {
958            data: TableDataRef::new(FontData::default_table_data().as_bytes())
959                .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
960        }
961    }
962}