Coverage Report

Created: 2026-12-01 06:30

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/read-fonts-0.47.0/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
0
    fn min_byte_range(&self) -> Range<usize> {
10
0
        0..self.glyph_variation_data_offsets_byte_range().end
11
0
    }
12
0
    fn min_table_bytes(&self) -> &'a [u8] {
13
0
        let range = self.min_byte_range();
14
0
        self.offset_data().as_bytes().get(range).unwrap_or_default()
15
0
    }
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
2.45k
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
29
        #[allow(clippy::absurd_extreme_comparisons)]
30
2.45k
        if data.len() < Self::MIN_SIZE {
31
7
            return Err(ReadError::OutOfBounds);
32
2.44k
        }
33
2.44k
        let Some(data) = TableDataRef::new(data.as_bytes()) else {
34
0
            return Err(ReadError::OutOfBounds);
35
        };
36
2.44k
        Ok(Self { data })
37
2.45k
    }
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
0
    pub fn version(&self) -> MajorMinor {
76
0
        self.data.header().version.get()
77
0
    }
78
79
    /// The number of variation axes for this font. This must be the
80
    /// same number as axisCount in the 'fvar' table.
81
1.04M
    pub fn axis_count(&self) -> u16 {
82
1.04M
        self.data.header().axis_count.get()
83
1.04M
    }
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
675k
    pub fn shared_tuple_count(&self) -> u16 {
90
675k
        self.data.header().shared_tuple_count.get()
91
675k
    }
92
93
    /// The number of glyphs in this font. This must match the number
94
    /// of glyphs stored elsewhere in the font.
95
726k
    pub fn glyph_count(&self) -> u16 {
96
726k
        self.data.header().glyph_count.get()
97
726k
    }
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
1.45M
    pub fn flags(&self) -> GvarFlags {
103
1.45M
        self.data.header().flags.get()
104
1.45M
    }
105
106
    /// Offset from the start of this table to the array of
107
    /// GlyphVariationData tables.
108
373k
    pub fn glyph_variation_data_array_offset(&self) -> u32 {
109
373k
        self.data.header().glyph_variation_data_array_offset.get()
110
373k
    }
111
112
    /// Offsets from the start of the GlyphVariationData array to each
113
    /// GlyphVariationData table.
114
726k
    pub fn glyph_variation_data_offsets(&self) -> ComputedArray<'a, U16Or32> {
115
726k
        let range = self.glyph_variation_data_offsets_byte_range();
116
726k
        ComputedArray::new(self.offset_data(), range, self.flags()).unwrap_or_default()
117
726k
    }
118
119
1.39M
    pub fn version_byte_range(&self) -> Range<usize> {
120
1.39M
        let start = 0usize;
121
1.39M
        let end = start.saturating_add(MajorMinor::RAW_BYTE_LEN);
122
1.39M
        start..end
123
1.39M
    }
124
125
1.39M
    pub fn axis_count_byte_range(&self) -> Range<usize> {
126
1.39M
        let start = self.version_byte_range().end;
127
1.39M
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
128
1.39M
        start..end
129
1.39M
    }
130
131
1.39M
    pub fn shared_tuple_count_byte_range(&self) -> Range<usize> {
132
1.39M
        let start = self.axis_count_byte_range().end;
133
1.39M
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
134
1.39M
        start..end
135
1.39M
    }
136
137
1.39M
    pub fn shared_tuples_offset_byte_range(&self) -> Range<usize> {
138
1.39M
        let start = self.shared_tuple_count_byte_range().end;
139
1.39M
        let end = start.saturating_add(Offset32::RAW_BYTE_LEN);
140
1.39M
        start..end
141
1.39M
    }
142
143
726k
    pub fn glyph_count_byte_range(&self) -> Range<usize> {
144
726k
        let start = self.shared_tuples_offset_byte_range().end;
145
726k
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
146
726k
        start..end
147
726k
    }
148
149
726k
    pub fn flags_byte_range(&self) -> Range<usize> {
150
726k
        let start = self.glyph_count_byte_range().end;
151
726k
        let end = start.saturating_add(GvarFlags::RAW_BYTE_LEN);
152
726k
        start..end
153
726k
    }
154
155
726k
    pub fn glyph_variation_data_array_offset_byte_range(&self) -> Range<usize> {
156
726k
        let start = self.flags_byte_range().end;
157
726k
        let end = start.saturating_add(u32::RAW_BYTE_LEN);
158
726k
        start..end
159
726k
    }
160
161
726k
    pub fn glyph_variation_data_offsets_byte_range(&self) -> Range<usize> {
162
726k
        let glyph_count = self.glyph_count();
163
726k
        let start = self.glyph_variation_data_array_offset_byte_range().end;
164
726k
        let end = start.saturating_add(
165
726k
            (transforms::add(glyph_count, 1_usize))
166
726k
                .saturating_mul(<U16Or32 as ComputeSize>::compute_size(self.flags()).unwrap_or(0)),
167
        );
168
726k
        start..end
169
726k
    }
170
}
171
172
const _: () = assert!(FontData::default_data_long_enough(Gvar::MIN_SIZE));
173
174
impl Default for Gvar<'_> {
175
0
    fn default() -> Self {
176
        Self {
177
0
            data: TableDataRef::new(FontData::default_table_data().as_bytes())
178
0
                .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
179
        }
180
0
    }
181
}
182
183
impl<'a> MinByteRange<'a> for GvarExtended<'a> {
184
0
    fn min_byte_range(&self) -> Range<usize> {
185
0
        0..self.glyph_variation_data_offsets_byte_range().end
186
0
    }
187
0
    fn min_table_bytes(&self) -> &'a [u8] {
188
0
        let range = self.min_byte_range();
189
0
        self.offset_data().as_bytes().get(range).unwrap_or_default()
190
0
    }
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
126
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
204
        #[allow(clippy::absurd_extreme_comparisons)]
205
126
        if data.len() < Self::MIN_SIZE {
206
3
            return Err(ReadError::OutOfBounds);
207
123
        }
208
123
        let Some(data) = TableDataRef::new(data.as_bytes()) else {
209
0
            return Err(ReadError::OutOfBounds);
210
        };
211
123
        Ok(Self { data })
212
126
    }
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
0
    pub fn version(&self) -> MajorMinor {
250
0
        self.data.header().version.get()
251
0
    }
252
253
12.9k
    pub fn axis_count(&self) -> u16 {
254
12.9k
        self.data.header().axis_count.get()
255
12.9k
    }
256
257
6.91k
    pub fn shared_tuple_count(&self) -> u16 {
258
6.91k
        self.data.header().shared_tuple_count.get()
259
6.91k
    }
260
261
    /// Number of glyphs, equal to numGlyphs in MAXP.
262
6.91k
    pub fn glyph_count(&self) -> Uint24 {
263
6.91k
        self.data.header().glyph_count.get()
264
6.91k
    }
265
266
13.8k
    pub fn flags(&self) -> GvarFlags {
267
13.8k
        self.data.header().flags.get()
268
13.8k
    }
269
270
6.12k
    pub fn glyph_variation_data_array_offset(&self) -> u32 {
271
6.12k
        self.data.header().glyph_variation_data_array_offset.get()
272
6.12k
    }
273
274
6.91k
    pub fn glyph_variation_data_offsets(&self) -> ComputedArray<'a, U16Or32> {
275
6.91k
        let range = self.glyph_variation_data_offsets_byte_range();
276
6.91k
        ComputedArray::new(self.offset_data(), range, self.flags()).unwrap_or_default()
277
6.91k
    }
278
279
13.6k
    pub fn version_byte_range(&self) -> Range<usize> {
280
13.6k
        let start = 0usize;
281
13.6k
        let end = start.saturating_add(MajorMinor::RAW_BYTE_LEN);
282
13.6k
        start..end
283
13.6k
    }
284
285
13.6k
    pub fn axis_count_byte_range(&self) -> Range<usize> {
286
13.6k
        let start = self.version_byte_range().end;
287
13.6k
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
288
13.6k
        start..end
289
13.6k
    }
290
291
13.6k
    pub fn shared_tuple_count_byte_range(&self) -> Range<usize> {
292
13.6k
        let start = self.axis_count_byte_range().end;
293
13.6k
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
294
13.6k
        start..end
295
13.6k
    }
296
297
13.6k
    pub fn shared_tuples_offset_byte_range(&self) -> Range<usize> {
298
13.6k
        let start = self.shared_tuple_count_byte_range().end;
299
13.6k
        let end = start.saturating_add(Offset32::RAW_BYTE_LEN);
300
13.6k
        start..end
301
13.6k
    }
302
303
6.91k
    pub fn glyph_count_byte_range(&self) -> Range<usize> {
304
6.91k
        let start = self.shared_tuples_offset_byte_range().end;
305
6.91k
        let end = start.saturating_add(Uint24::RAW_BYTE_LEN);
306
6.91k
        start..end
307
6.91k
    }
308
309
6.91k
    pub fn flags_byte_range(&self) -> Range<usize> {
310
6.91k
        let start = self.glyph_count_byte_range().end;
311
6.91k
        let end = start.saturating_add(GvarFlags::RAW_BYTE_LEN);
312
6.91k
        start..end
313
6.91k
    }
314
315
6.91k
    pub fn glyph_variation_data_array_offset_byte_range(&self) -> Range<usize> {
316
6.91k
        let start = self.flags_byte_range().end;
317
6.91k
        let end = start.saturating_add(u32::RAW_BYTE_LEN);
318
6.91k
        start..end
319
6.91k
    }
320
321
6.91k
    pub fn glyph_variation_data_offsets_byte_range(&self) -> Range<usize> {
322
6.91k
        let glyph_count = self.glyph_count();
323
6.91k
        let start = self.glyph_variation_data_array_offset_byte_range().end;
324
6.91k
        let end = start.saturating_add(
325
6.91k
            (transforms::add(glyph_count, 1_usize))
326
6.91k
                .saturating_mul(<U16Or32 as ComputeSize>::compute_size(self.flags()).unwrap_or(0)),
327
        );
328
6.91k
        start..end
329
6.91k
    }
330
}
331
332
const _: () = assert!(FontData::default_data_long_enough(GvarExtended::MIN_SIZE));
333
334
impl Default for GvarExtended<'_> {
335
0
    fn default() -> Self {
336
        Self {
337
0
            data: TableDataRef::new(FontData::default_table_data().as_bytes())
338
0
                .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
339
        }
340
0
    }
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
0
    pub const fn empty() -> Self {
359
0
        Self { bits: 0 }
360
0
    }
361
362
    /// Returns the set containing all flags.
363
    #[inline]
364
1.46M
    pub const fn all() -> Self {
365
1.46M
        Self {
366
1.46M
            bits: Self::LONG_OFFSETS.bits,
367
1.46M
        }
368
1.46M
    }
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
0
    pub const fn from_bits(bits: u16) -> Option<Self> {
380
0
        if (bits & !Self::all().bits()) == 0 {
381
0
            Some(Self { bits })
382
        } else {
383
0
            None
384
        }
385
0
    }
386
387
    /// Convert from underlying bit representation, dropping any bits
388
    /// that do not correspond to flags.
389
    #[inline]
390
1.46M
    pub const fn from_bits_truncate(bits: u16) -> Self {
391
1.46M
        Self {
392
1.46M
            bits: bits & Self::all().bits,
393
1.46M
        }
394
1.46M
    }
395
396
    /// Returns `true` if no flags are currently stored.
397
    #[inline]
398
0
    pub const fn is_empty(&self) -> bool {
399
0
        self.bits() == Self::empty().bits()
400
0
    }
401
402
    /// Returns `true` if there are flags common to both `self` and `other`.
403
    #[inline]
404
0
    pub const fn intersects(&self, other: Self) -> bool {
405
0
        !(Self {
406
0
            bits: self.bits & other.bits,
407
0
        })
408
0
        .is_empty()
409
0
    }
410
411
    /// Returns `true` if all of the flags in `other` are contained within `self`.
412
    #[inline]
413
2.85M
    pub const fn contains(&self, other: Self) -> bool {
414
2.85M
        (self.bits & other.bits) == other.bits
415
2.85M
    }
416
417
    /// Inserts the specified flags in-place.
418
    #[inline]
419
0
    pub fn insert(&mut self, other: Self) {
420
0
        self.bits |= other.bits;
421
0
    }
422
423
    /// Removes the specified flags in-place.
424
    #[inline]
425
0
    pub fn remove(&mut self, other: Self) {
426
0
        self.bits &= !other.bits;
427
0
    }
428
429
    /// Toggles the specified flags in-place.
430
    #[inline]
431
0
    pub fn toggle(&mut self, other: Self) {
432
0
        self.bits ^= other.bits;
433
0
    }
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
0
    pub const fn intersection(self, other: Self) -> Self {
448
0
        Self {
449
0
            bits: self.bits & other.bits,
450
0
        }
451
0
    }
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
0
    pub const fn union(self, other: Self) -> Self {
466
0
        Self {
467
0
            bits: self.bits | other.bits,
468
0
        }
469
0
    }
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
0
    pub const fn difference(self, other: Self) -> Self {
486
0
        Self {
487
0
            bits: self.bits & !other.bits,
488
0
        }
489
0
    }
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
0
    fn bitor(self, other: GvarFlags) -> Self {
498
0
        Self {
499
0
            bits: self.bits | other.bits,
500
0
        }
501
0
    }
502
}
503
504
impl std::ops::BitOrAssign for GvarFlags {
505
    /// Adds the set of flags.
506
    #[inline]
507
0
    fn bitor_assign(&mut self, other: Self) {
508
0
        self.bits |= other.bits;
509
0
    }
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
0
    fn bitxor(self, other: Self) -> Self {
518
0
        Self {
519
0
            bits: self.bits ^ other.bits,
520
0
        }
521
0
    }
522
}
523
524
impl std::ops::BitXorAssign for GvarFlags {
525
    /// Toggles the set of flags.
526
    #[inline]
527
0
    fn bitxor_assign(&mut self, other: Self) {
528
0
        self.bits ^= other.bits;
529
0
    }
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
0
    fn bitand(self, other: Self) -> Self {
538
0
        Self {
539
0
            bits: self.bits & other.bits,
540
0
        }
541
0
    }
542
}
543
544
impl std::ops::BitAndAssign for GvarFlags {
545
    /// Disables all flags disabled in the set.
546
    #[inline]
547
0
    fn bitand_assign(&mut self, other: Self) {
548
0
        self.bits &= other.bits;
549
0
    }
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
0
    fn sub(self, other: Self) -> Self {
558
0
        Self {
559
0
            bits: self.bits & !other.bits,
560
0
        }
561
0
    }
562
}
563
564
impl std::ops::SubAssign for GvarFlags {
565
    /// Disables all flags enabled in the set.
566
    #[inline]
567
0
    fn sub_assign(&mut self, other: Self) {
568
0
        self.bits &= !other.bits;
569
0
    }
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
0
    fn not(self) -> Self {
578
0
        Self { bits: !self.bits } & Self::all()
579
0
    }
580
}
581
582
impl std::fmt::Debug for GvarFlags {
583
0
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
584
0
        let members: &[(&str, Self)] = &[("LONG_OFFSETS", Self::LONG_OFFSETS)];
585
0
        let mut first = true;
586
0
        for (name, value) in members {
587
0
            if self.contains(*value) {
588
0
                if !first {
589
0
                    f.write_str(" | ")?;
590
0
                }
591
0
                first = false;
592
0
                f.write_str(name)?;
593
0
            }
594
        }
595
0
        if first {
596
0
            f.write_str("(empty)")?;
597
0
        }
598
0
        Ok(())
599
0
    }
600
}
601
602
impl std::fmt::Binary for GvarFlags {
603
0
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
604
0
        std::fmt::Binary::fmt(&self.bits, f)
605
0
    }
606
}
607
608
impl std::fmt::Octal for GvarFlags {
609
0
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
610
0
        std::fmt::Octal::fmt(&self.bits, f)
611
0
    }
612
}
613
614
impl std::fmt::LowerHex for GvarFlags {
615
0
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
616
0
        std::fmt::LowerHex::fmt(&self.bits, f)
617
0
    }
618
}
619
620
impl std::fmt::UpperHex for GvarFlags {
621
0
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
622
0
        std::fmt::UpperHex::fmt(&self.bits, f)
623
0
    }
624
}
625
626
impl font_types::Scalar for GvarFlags {
627
    type Raw = <u16 as font_types::Scalar>::Raw;
628
0
    fn to_raw(self) -> Self::Raw {
629
0
        self.bits().to_raw()
630
0
    }
631
1.46M
    fn from_raw(raw: Self::Raw) -> Self {
632
1.46M
        let t = <u16>::from_raw(raw);
633
1.46M
        Self::from_bits_truncate(t)
634
1.46M
    }
635
}
636
637
impl<'a> MinByteRange<'a> for SharedTuples<'a> {
638
0
    fn min_byte_range(&self) -> Range<usize> {
639
0
        0..self.tuples_byte_range().end
640
0
    }
641
0
    fn min_table_bytes(&self) -> &'a [u8] {
642
0
        let range = self.min_byte_range();
643
0
        self.offset_data().as_bytes().get(range).unwrap_or_default()
644
0
    }
645
}
646
647
impl ReadArgs for SharedTuples<'_> {
648
    type Args = (u16, u16);
649
}
650
651
impl<'a> FontRead<'a> for SharedTuples<'a> {
652
381k
    fn read_with_args(data: FontData<'a>, args: (u16, u16)) -> Result<Self, ReadError> {
653
381k
        let (shared_tuple_count, axis_count) = args;
654
655
        #[allow(clippy::absurd_extreme_comparisons)]
656
381k
        if data.len() < Self::MIN_SIZE {
657
0
            return Err(ReadError::OutOfBounds);
658
381k
        }
659
381k
        let Some(data) = TableDataRef::new(data.as_bytes()) else {
660
0
            return Err(ReadError::OutOfBounds);
661
        };
662
381k
        Ok(Self {
663
381k
            data,
664
381k
            shared_tuple_count,
665
381k
            axis_count,
666
381k
        })
667
381k
    }
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
13.9k
    pub fn read(
676
13.9k
        data: FontData<'a>,
677
13.9k
        shared_tuple_count: u16,
678
13.9k
        axis_count: u16,
679
13.9k
    ) -> Result<Self, ReadError> {
680
13.9k
        let args = (shared_tuple_count, axis_count);
681
13.9k
        Self::read_with_args(data, args)
682
13.9k
    }
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
327k
    pub fn tuples(&self) -> ComputedArray<'a, Tuple<'a>> {
708
327k
        let range = self.tuples_byte_range();
709
327k
        ComputedArray::new(self.offset_data(), range, self.axis_count()).unwrap_or_default()
710
327k
    }
711
712
327k
    pub(crate) fn shared_tuple_count(&self) -> u16 {
713
327k
        self.shared_tuple_count
714
327k
    }
715
716
655k
    pub(crate) fn axis_count(&self) -> u16 {
717
655k
        self.axis_count
718
655k
    }
719
720
327k
    pub fn tuples_byte_range(&self) -> Range<usize> {
721
327k
        let shared_tuple_count = self.shared_tuple_count();
722
327k
        let start = 0usize;
723
327k
        let end =
724
327k
            start.saturating_add((transforms::to_usize(shared_tuple_count)).saturating_mul(
725
327k
                <Tuple as ComputeSize>::compute_size(self.axis_count()).unwrap_or(0),
726
            ));
727
327k
        start..end
728
327k
    }
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
0
    fn default() -> Self {
736
        Self {
737
0
            data: TableDataRef::new(FontData::default_table_data().as_bytes())
738
0
                .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
739
0
            shared_tuple_count: Default::default(),
740
0
            axis_count: Default::default(),
741
        }
742
0
    }
743
}
744
745
impl<'a> MinByteRange<'a> for GlyphVariationDataHeader<'a> {
746
0
    fn min_byte_range(&self) -> Range<usize> {
747
0
        0..self.tuple_variation_headers_byte_range().end
748
0
    }
749
0
    fn min_table_bytes(&self) -> &'a [u8] {
750
0
        let range = self.min_byte_range();
751
0
        self.offset_data().as_bytes().get(range).unwrap_or_default()
752
0
    }
753
}
754
755
impl ReadArgs for GlyphVariationDataHeader<'_> {
756
    type Args = ();
757
}
758
759
impl<'a> FontRead<'a> for GlyphVariationDataHeader<'a> {
760
327k
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
761
        #[allow(clippy::absurd_extreme_comparisons)]
762
327k
        if data.len() < Self::MIN_SIZE {
763
267
            return Err(ReadError::OutOfBounds);
764
327k
        }
765
327k
        let Some(data) = TableDataRef::new(data.as_bytes()) else {
766
0
            return Err(ReadError::OutOfBounds);
767
        };
768
327k
        Ok(Self { data })
769
327k
    }
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
327k
    pub fn tuple_variation_count(&self) -> TupleVariationCount {
803
327k
        self.data.header().tuple_variation_count.get()
804
327k
    }
805
806
    /// Offset from the start of the GlyphVariationData table to the
807
    /// serialized data
808
327k
    pub fn serialized_data_offset(&self) -> Offset16 {
809
327k
        self.data.header().serialized_data_offset.get()
810
327k
    }
811
812
    /// Attempt to resolve [`serialized_data_offset`][Self::serialized_data_offset].
813
327k
    pub fn serialized_data(&self) -> Result<FontData<'a>, ReadError> {
814
327k
        let data = self.offset_data();
815
327k
        self.serialized_data_offset().resolve(data)
816
327k
    }
817
818
    /// Array of tuple variation headers.
819
0
    pub fn tuple_variation_headers(&self) -> VarLenArray<'a, TupleVariationHeader<'a>> {
820
0
        let range = self.tuple_variation_headers_byte_range();
821
0
        self.offset_data()
822
0
            .split_off(range.start)
823
0
            .and_then(|d| VarLenArray::read(d).ok())
824
0
            .unwrap_or_default()
825
0
    }
826
827
327k
    pub fn tuple_variation_count_byte_range(&self) -> Range<usize> {
828
327k
        let start = 0usize;
829
327k
        let end = start.saturating_add(TupleVariationCount::RAW_BYTE_LEN);
830
327k
        start..end
831
327k
    }
832
833
327k
    pub fn serialized_data_offset_byte_range(&self) -> Range<usize> {
834
327k
        let start = self.tuple_variation_count_byte_range().end;
835
327k
        let end = start.saturating_add(Offset16::RAW_BYTE_LEN);
836
327k
        start..end
837
327k
    }
838
839
327k
    pub fn tuple_variation_headers_byte_range(&self) -> Range<usize> {
840
327k
        let start = self.serialized_data_offset_byte_range().end;
841
327k
        let end = start.saturating_add(self.offset_data().len().saturating_sub(start));
842
327k
        start..end
843
327k
    }
844
}
845
846
const _: () = assert!(FontData::default_data_long_enough(
847
    GlyphVariationDataHeader::MIN_SIZE
848
));
849
850
impl Default for GlyphVariationDataHeader<'_> {
851
0
    fn default() -> Self {
852
        Self {
853
0
            data: TableDataRef::new(FontData::default_table_data().as_bytes())
854
0
                .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
855
        }
856
0
    }
857
}
858
859
impl<'a> MinByteRange<'a> for GlyphVariationDataHeaderExtended<'a> {
860
0
    fn min_byte_range(&self) -> Range<usize> {
861
0
        0..self.tuple_variation_headers_byte_range().end
862
0
    }
863
0
    fn min_table_bytes(&self) -> &'a [u8] {
864
0
        let range = self.min_byte_range();
865
0
        self.offset_data().as_bytes().get(range).unwrap_or_default()
866
0
    }
867
}
868
869
impl ReadArgs for GlyphVariationDataHeaderExtended<'_> {
870
    type Args = ();
871
}
872
873
impl<'a> FontRead<'a> for GlyphVariationDataHeaderExtended<'a> {
874
602
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
875
        #[allow(clippy::absurd_extreme_comparisons)]
876
602
        if data.len() < Self::MIN_SIZE {
877
81
            return Err(ReadError::OutOfBounds);
878
521
        }
879
521
        let Some(data) = TableDataRef::new(data.as_bytes()) else {
880
0
            return Err(ReadError::OutOfBounds);
881
        };
882
521
        Ok(Self { data })
883
602
    }
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
521
    pub fn tuple_variation_count(&self) -> TupleVariationCount {
913
521
        self.data.header().tuple_variation_count.get()
914
521
    }
915
916
521
    pub fn serialized_data_offset(&self) -> Offset24 {
917
521
        self.data.header().serialized_data_offset.get()
918
521
    }
919
920
    /// Attempt to resolve [`serialized_data_offset`][Self::serialized_data_offset].
921
521
    pub fn serialized_data(&self) -> Result<FontData<'a>, ReadError> {
922
521
        let data = self.offset_data();
923
521
        self.serialized_data_offset().resolve(data)
924
521
    }
925
926
0
    pub fn tuple_variation_headers(&self) -> VarLenArray<'a, TupleVariationHeader<'a>> {
927
0
        let range = self.tuple_variation_headers_byte_range();
928
0
        self.offset_data()
929
0
            .split_off(range.start)
930
0
            .and_then(|d| VarLenArray::read(d).ok())
931
0
            .unwrap_or_default()
932
0
    }
933
934
521
    pub fn tuple_variation_count_byte_range(&self) -> Range<usize> {
935
521
        let start = 0usize;
936
521
        let end = start.saturating_add(TupleVariationCount::RAW_BYTE_LEN);
937
521
        start..end
938
521
    }
939
940
521
    pub fn serialized_data_offset_byte_range(&self) -> Range<usize> {
941
521
        let start = self.tuple_variation_count_byte_range().end;
942
521
        let end = start.saturating_add(Offset24::RAW_BYTE_LEN);
943
521
        start..end
944
521
    }
945
946
521
    pub fn tuple_variation_headers_byte_range(&self) -> Range<usize> {
947
521
        let start = self.serialized_data_offset_byte_range().end;
948
521
        let end = start.saturating_add(self.offset_data().len().saturating_sub(start));
949
521
        start..end
950
521
    }
951
}
952
953
const _: () = assert!(FontData::default_data_long_enough(
954
    GlyphVariationDataHeaderExtended::MIN_SIZE
955
));
956
957
impl Default for GlyphVariationDataHeaderExtended<'_> {
958
0
    fn default() -> Self {
959
        Self {
960
0
            data: TableDataRef::new(FontData::default_table_data().as_bytes())
961
0
                .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
962
        }
963
0
    }
964
}