Coverage Report

Created: 2026-11-11 07:51

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