Coverage Report

Created: 2026-09-14 08:06

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.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
        Ok(Self { data })
34
    }
35
}
36
37
/// The ['gvar' header](https://learn.microsoft.com/en-us/typography/opentype/spec/gvar#gvar-header)
38
#[derive(Clone)]
39
pub struct Gvar<'a> {
40
    data: FontData<'a>,
41
}
42
43
#[allow(clippy::needless_lifetimes)]
44
impl<'a> Gvar<'a> {
45
    pub const MIN_SIZE: usize = (MajorMinor::RAW_BYTE_LEN
46
        + u16::RAW_BYTE_LEN
47
        + u16::RAW_BYTE_LEN
48
        + Offset32::RAW_BYTE_LEN
49
        + u16::RAW_BYTE_LEN
50
        + GvarFlags::RAW_BYTE_LEN
51
        + u32::RAW_BYTE_LEN);
52
    basic_table_impls!(impl_the_methods);
53
54
    /// Major/minor version number of the glyph variations table — set to (1,0).
55
    pub fn version(&self) -> MajorMinor {
56
        let range = self.version_byte_range();
57
        self.data.read_at(range.start).ok().unwrap()
58
    }
59
60
    /// The number of variation axes for this font. This must be the
61
    /// same number as axisCount in the 'fvar' table.
62
    pub fn axis_count(&self) -> u16 {
63
        let range = self.axis_count_byte_range();
64
        self.data.read_at(range.start).ok().unwrap()
65
    }
66
67
    /// The number of shared tuple records. Shared tuple records can be
68
    /// referenced within glyph variation data tables for multiple
69
    /// glyphs, as opposed to other tuple records stored directly
70
    /// within a glyph variation data table.
71
    pub fn shared_tuple_count(&self) -> u16 {
72
        let range = self.shared_tuple_count_byte_range();
73
        self.data.read_at(range.start).ok().unwrap()
74
    }
75
76
    /// Offset from the start of this table to the shared tuple records.
77
    pub fn shared_tuples_offset(&self) -> Offset32 {
78
        let range = self.shared_tuples_offset_byte_range();
79
        self.data.read_at(range.start).ok().unwrap()
80
    }
81
82
    /// Attempt to resolve [`shared_tuples_offset`][Self::shared_tuples_offset].
83
    pub fn shared_tuples(&self) -> Result<SharedTuples<'a>, ReadError> {
84
        let data = self.data;
85
        let args = (self.shared_tuple_count(), self.axis_count());
86
        self.shared_tuples_offset().resolve_with_args(data, args)
87
    }
88
89
    /// The number of glyphs in this font. This must match the number
90
    /// of glyphs stored elsewhere in the font.
91
    pub fn glyph_count(&self) -> u16 {
92
        let range = self.glyph_count_byte_range();
93
        self.data.read_at(range.start).ok().unwrap()
94
    }
95
96
    /// Bit-field that gives the format of the offset array that
97
    /// follows. If bit 0 is clear, the offsets are uint16; if bit 0 is
98
    /// set, the offsets are uint32.
99
    pub fn flags(&self) -> GvarFlags {
100
        let range = self.flags_byte_range();
101
        self.data.read_at(range.start).ok().unwrap()
102
    }
103
104
    /// Offset from the start of this table to the array of
105
    /// GlyphVariationData tables.
106
    pub fn glyph_variation_data_array_offset(&self) -> u32 {
107
        let range = self.glyph_variation_data_array_offset_byte_range();
108
        self.data.read_at(range.start).ok().unwrap()
109
    }
110
111
    /// Offsets from the start of the GlyphVariationData array to each
112
    /// GlyphVariationData table.
113
    pub fn glyph_variation_data_offsets(&self) -> ComputedArray<'a, U16Or32> {
114
        let range = self.glyph_variation_data_offsets_byte_range();
115
        ComputedArray::new(self.data, range, self.flags()).unwrap_or_default()
116
    }
117
118
    pub fn version_byte_range(&self) -> Range<usize> {
119
        let start = 0;
120
        let end = start + MajorMinor::RAW_BYTE_LEN;
121
        start..end
122
    }
123
124
    pub fn axis_count_byte_range(&self) -> Range<usize> {
125
        let start = self.version_byte_range().end;
126
        let end = start + u16::RAW_BYTE_LEN;
127
        start..end
128
    }
129
130
    pub fn shared_tuple_count_byte_range(&self) -> Range<usize> {
131
        let start = self.axis_count_byte_range().end;
132
        let end = start + u16::RAW_BYTE_LEN;
133
        start..end
134
    }
135
136
    pub fn shared_tuples_offset_byte_range(&self) -> Range<usize> {
137
        let start = self.shared_tuple_count_byte_range().end;
138
        let end = start + Offset32::RAW_BYTE_LEN;
139
        start..end
140
    }
141
142
    pub fn glyph_count_byte_range(&self) -> Range<usize> {
143
        let start = self.shared_tuples_offset_byte_range().end;
144
        let end = start + u16::RAW_BYTE_LEN;
145
        start..end
146
    }
147
148
    pub fn flags_byte_range(&self) -> Range<usize> {
149
        let start = self.glyph_count_byte_range().end;
150
        let end = start + GvarFlags::RAW_BYTE_LEN;
151
        start..end
152
    }
153
154
    pub fn glyph_variation_data_array_offset_byte_range(&self) -> Range<usize> {
155
        let start = self.flags_byte_range().end;
156
        let end = start + u32::RAW_BYTE_LEN;
157
        start..end
158
    }
159
160
    pub fn glyph_variation_data_offsets_byte_range(&self) -> Range<usize> {
161
        let glyph_count = self.glyph_count();
162
        let start = self.glyph_variation_data_array_offset_byte_range().end;
163
        let end = start
164
            + (transforms::add(glyph_count, 1_usize))
165
                .saturating_mul(<U16Or32 as ComputeSize>::compute_size(self.flags()).unwrap_or(0));
166
        start..end
167
    }
168
}
169
170
const _: () = assert!(FontData::default_data_long_enough(Gvar::MIN_SIZE));
171
172
impl Default for Gvar<'_> {
173
    fn default() -> Self {
174
        Self {
175
            data: FontData::default_table_data(),
176
        }
177
    }
178
}
179
180
#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash, bytemuck :: AnyBitPattern)]
181
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
182
#[repr(transparent)]
183
pub struct GvarFlags {
184
    bits: u16,
185
}
186
187
impl GvarFlags {
188
    /// If set, offsets to GlyphVariationData are 32 bits
189
    pub const LONG_OFFSETS: Self = Self { bits: 1 };
190
}
191
192
impl GvarFlags {
193
    ///  Returns an empty set of flags.
194
    #[inline]
195
    pub const fn empty() -> Self {
196
        Self { bits: 0 }
197
    }
198
199
    /// Returns the set containing all flags.
200
    #[inline]
201
    pub const fn all() -> Self {
202
        Self {
203
            bits: Self::LONG_OFFSETS.bits,
204
        }
205
    }
206
207
    /// Returns the raw value of the flags currently stored.
208
    #[inline]
209
0
    pub const fn bits(&self) -> u16 {
210
0
        self.bits
211
0
    }
212
213
    /// Convert from underlying bit representation, unless that
214
    /// representation contains bits that do not correspond to a flag.
215
    #[inline]
216
    pub const fn from_bits(bits: u16) -> Option<Self> {
217
        if (bits & !Self::all().bits()) == 0 {
218
            Some(Self { bits })
219
        } else {
220
            None
221
        }
222
    }
223
224
    /// Convert from underlying bit representation, dropping any bits
225
    /// that do not correspond to flags.
226
    #[inline]
227
    pub const fn from_bits_truncate(bits: u16) -> Self {
228
        Self {
229
            bits: bits & Self::all().bits,
230
        }
231
    }
232
233
    /// Returns `true` if no flags are currently stored.
234
    #[inline]
235
    pub const fn is_empty(&self) -> bool {
236
        self.bits() == Self::empty().bits()
237
    }
238
239
    /// Returns `true` if there are flags common to both `self` and `other`.
240
    #[inline]
241
    pub const fn intersects(&self, other: Self) -> bool {
242
        !(Self {
243
            bits: self.bits & other.bits,
244
        })
245
        .is_empty()
246
    }
247
248
    /// Returns `true` if all of the flags in `other` are contained within `self`.
249
    #[inline]
250
0
    pub const fn contains(&self, other: Self) -> bool {
251
0
        (self.bits & other.bits) == other.bits
252
0
    }
253
254
    /// Inserts the specified flags in-place.
255
    #[inline]
256
    pub fn insert(&mut self, other: Self) {
257
        self.bits |= other.bits;
258
    }
259
260
    /// Removes the specified flags in-place.
261
    #[inline]
262
    pub fn remove(&mut self, other: Self) {
263
        self.bits &= !other.bits;
264
    }
265
266
    /// Toggles the specified flags in-place.
267
    #[inline]
268
    pub fn toggle(&mut self, other: Self) {
269
        self.bits ^= other.bits;
270
    }
271
272
    /// Returns the intersection between the flags in `self` and
273
    /// `other`.
274
    ///
275
    /// Specifically, the returned set contains only the flags which are
276
    /// present in *both* `self` *and* `other`.
277
    ///
278
    /// This is equivalent to using the `&` operator (e.g.
279
    /// [`ops::BitAnd`]), as in `flags & other`.
280
    ///
281
    /// [`ops::BitAnd`]: https://doc.rust-lang.org/std/ops/trait.BitAnd.html
282
    #[inline]
283
    #[must_use]
284
    pub const fn intersection(self, other: Self) -> Self {
285
        Self {
286
            bits: self.bits & other.bits,
287
        }
288
    }
289
290
    /// Returns the union of between the flags in `self` and `other`.
291
    ///
292
    /// Specifically, the returned set contains all flags which are
293
    /// present in *either* `self` *or* `other`, including any which are
294
    /// present in both.
295
    ///
296
    /// This is equivalent to using the `|` operator (e.g.
297
    /// [`ops::BitOr`]), as in `flags | other`.
298
    ///
299
    /// [`ops::BitOr`]: https://doc.rust-lang.org/std/ops/trait.BitOr.html
300
    #[inline]
301
    #[must_use]
302
    pub const fn union(self, other: Self) -> Self {
303
        Self {
304
            bits: self.bits | other.bits,
305
        }
306
    }
307
308
    /// Returns the difference between the flags in `self` and `other`.
309
    ///
310
    /// Specifically, the returned set contains all flags present in
311
    /// `self`, except for the ones present in `other`.
312
    ///
313
    /// It is also conceptually equivalent to the "bit-clear" operation:
314
    /// `flags & !other` (and this syntax is also supported).
315
    ///
316
    /// This is equivalent to using the `-` operator (e.g.
317
    /// [`ops::Sub`]), as in `flags - other`.
318
    ///
319
    /// [`ops::Sub`]: https://doc.rust-lang.org/std/ops/trait.Sub.html
320
    #[inline]
321
    #[must_use]
322
    pub const fn difference(self, other: Self) -> Self {
323
        Self {
324
            bits: self.bits & !other.bits,
325
        }
326
    }
327
}
328
329
impl std::ops::BitOr for GvarFlags {
330
    type Output = Self;
331
332
    /// Returns the union of the two sets of flags.
333
    #[inline]
334
    fn bitor(self, other: GvarFlags) -> Self {
335
        Self {
336
            bits: self.bits | other.bits,
337
        }
338
    }
339
}
340
341
impl std::ops::BitOrAssign for GvarFlags {
342
    /// Adds the set of flags.
343
    #[inline]
344
    fn bitor_assign(&mut self, other: Self) {
345
        self.bits |= other.bits;
346
    }
347
}
348
349
impl std::ops::BitXor for GvarFlags {
350
    type Output = Self;
351
352
    /// Returns the left flags, but with all the right flags toggled.
353
    #[inline]
354
    fn bitxor(self, other: Self) -> Self {
355
        Self {
356
            bits: self.bits ^ other.bits,
357
        }
358
    }
359
}
360
361
impl std::ops::BitXorAssign for GvarFlags {
362
    /// Toggles the set of flags.
363
    #[inline]
364
    fn bitxor_assign(&mut self, other: Self) {
365
        self.bits ^= other.bits;
366
    }
367
}
368
369
impl std::ops::BitAnd for GvarFlags {
370
    type Output = Self;
371
372
    /// Returns the intersection between the two sets of flags.
373
    #[inline]
374
    fn bitand(self, other: Self) -> Self {
375
        Self {
376
            bits: self.bits & other.bits,
377
        }
378
    }
379
}
380
381
impl std::ops::BitAndAssign for GvarFlags {
382
    /// Disables all flags disabled in the set.
383
    #[inline]
384
    fn bitand_assign(&mut self, other: Self) {
385
        self.bits &= other.bits;
386
    }
387
}
388
389
impl std::ops::Sub for GvarFlags {
390
    type Output = Self;
391
392
    /// Returns the set difference of the two sets of flags.
393
    #[inline]
394
    fn sub(self, other: Self) -> Self {
395
        Self {
396
            bits: self.bits & !other.bits,
397
        }
398
    }
399
}
400
401
impl std::ops::SubAssign for GvarFlags {
402
    /// Disables all flags enabled in the set.
403
    #[inline]
404
    fn sub_assign(&mut self, other: Self) {
405
        self.bits &= !other.bits;
406
    }
407
}
408
409
impl std::ops::Not for GvarFlags {
410
    type Output = Self;
411
412
    /// Returns the complement of this set of flags.
413
    #[inline]
414
    fn not(self) -> Self {
415
        Self { bits: !self.bits } & Self::all()
416
    }
417
}
418
419
impl std::fmt::Debug for GvarFlags {
420
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
421
        let members: &[(&str, Self)] = &[("LONG_OFFSETS", Self::LONG_OFFSETS)];
422
        let mut first = true;
423
        for (name, value) in members {
424
            if self.contains(*value) {
425
                if !first {
426
                    f.write_str(" | ")?;
427
                }
428
                first = false;
429
                f.write_str(name)?;
430
            }
431
        }
432
        if first {
433
            f.write_str("(empty)")?;
434
        }
435
        Ok(())
436
    }
437
}
438
439
impl std::fmt::Binary for GvarFlags {
440
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
441
        std::fmt::Binary::fmt(&self.bits, f)
442
    }
443
}
444
445
impl std::fmt::Octal for GvarFlags {
446
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
447
        std::fmt::Octal::fmt(&self.bits, f)
448
    }
449
}
450
451
impl std::fmt::LowerHex for GvarFlags {
452
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
453
        std::fmt::LowerHex::fmt(&self.bits, f)
454
    }
455
}
456
457
impl std::fmt::UpperHex for GvarFlags {
458
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
459
        std::fmt::UpperHex::fmt(&self.bits, f)
460
    }
461
}
462
463
impl font_types::Scalar for GvarFlags {
464
    type Raw = <u16 as font_types::Scalar>::Raw;
465
    fn to_raw(self) -> Self::Raw {
466
        self.bits().to_raw()
467
    }
468
    fn from_raw(raw: Self::Raw) -> Self {
469
        let t = <u16>::from_raw(raw);
470
        Self::from_bits_truncate(t)
471
    }
472
}
473
474
impl<'a> MinByteRange<'a> for SharedTuples<'a> {
475
    fn min_byte_range(&self) -> Range<usize> {
476
        0..self.tuples_byte_range().end
477
    }
478
    fn min_table_bytes(&self) -> &'a [u8] {
479
        let range = self.min_byte_range();
480
        self.data.as_bytes().get(range).unwrap_or_default()
481
    }
482
}
483
484
impl ReadArgs for SharedTuples<'_> {
485
    type Args = (u16, u16);
486
}
487
488
impl<'a> FontRead<'a> for SharedTuples<'a> {
489
    fn read_with_args(data: FontData<'a>, args: (u16, u16)) -> Result<Self, ReadError> {
490
        let (shared_tuple_count, axis_count) = args;
491
492
        #[allow(clippy::absurd_extreme_comparisons)]
493
        if data.len() < Self::MIN_SIZE {
494
            return Err(ReadError::OutOfBounds);
495
        }
496
        Ok(Self {
497
            data,
498
            shared_tuple_count,
499
            axis_count,
500
        })
501
    }
502
}
503
504
impl<'a> SharedTuples<'a> {
505
    /// A constructor that requires additional arguments.
506
    ///
507
    /// This type requires some external state in order to be
508
    /// parsed.
509
    pub fn read(
510
        data: FontData<'a>,
511
        shared_tuple_count: u16,
512
        axis_count: u16,
513
    ) -> Result<Self, ReadError> {
514
        let args = (shared_tuple_count, axis_count);
515
        Self::read_with_args(data, args)
516
    }
517
}
518
519
/// Array of tuple records shared across all glyph variation data tables.
520
#[derive(Clone)]
521
pub struct SharedTuples<'a> {
522
    data: FontData<'a>,
523
    shared_tuple_count: u16,
524
    axis_count: u16,
525
}
526
527
#[allow(clippy::needless_lifetimes)]
528
impl<'a> SharedTuples<'a> {
529
    pub const MIN_SIZE: usize = 0;
530
    basic_table_impls!(impl_the_methods);
531
532
    pub fn tuples(&self) -> ComputedArray<'a, Tuple<'a>> {
533
        let range = self.tuples_byte_range();
534
        ComputedArray::new(self.data, range, self.axis_count()).unwrap_or_default()
535
    }
536
537
    pub(crate) fn shared_tuple_count(&self) -> u16 {
538
        self.shared_tuple_count
539
    }
540
541
    pub(crate) fn axis_count(&self) -> u16 {
542
        self.axis_count
543
    }
544
545
    pub fn tuples_byte_range(&self) -> Range<usize> {
546
        let shared_tuple_count = self.shared_tuple_count();
547
        let start = 0;
548
        let end = start
549
            + (transforms::to_usize(shared_tuple_count)).saturating_mul(
550
                <Tuple as ComputeSize>::compute_size(self.axis_count()).unwrap_or(0),
551
            );
552
        start..end
553
    }
554
}
555
556
#[allow(clippy::absurd_extreme_comparisons)]
557
const _: () = assert!(FontData::default_data_long_enough(SharedTuples::MIN_SIZE));
558
559
impl Default for SharedTuples<'_> {
560
    fn default() -> Self {
561
        Self {
562
            data: FontData::default_table_data(),
563
            shared_tuple_count: Default::default(),
564
            axis_count: Default::default(),
565
        }
566
    }
567
}
568
569
impl<'a> MinByteRange<'a> for GlyphVariationDataHeader<'a> {
570
    fn min_byte_range(&self) -> Range<usize> {
571
        0..self.tuple_variation_headers_byte_range().end
572
    }
573
    fn min_table_bytes(&self) -> &'a [u8] {
574
        let range = self.min_byte_range();
575
        self.data.as_bytes().get(range).unwrap_or_default()
576
    }
577
}
578
579
impl ReadArgs for GlyphVariationDataHeader<'_> {
580
    type Args = ();
581
}
582
583
impl<'a> FontRead<'a> for GlyphVariationDataHeader<'a> {
584
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
585
        #[allow(clippy::absurd_extreme_comparisons)]
586
        if data.len() < Self::MIN_SIZE {
587
            return Err(ReadError::OutOfBounds);
588
        }
589
        Ok(Self { data })
590
    }
591
}
592
593
/// The [GlyphVariationData](https://learn.microsoft.com/en-us/typography/opentype/spec/gvar#the-glyphvariationdata-table-array) table
594
#[derive(Clone)]
595
pub struct GlyphVariationDataHeader<'a> {
596
    data: FontData<'a>,
597
}
598
599
#[allow(clippy::needless_lifetimes)]
600
impl<'a> GlyphVariationDataHeader<'a> {
601
    pub const MIN_SIZE: usize = (TupleVariationCount::RAW_BYTE_LEN + Offset16::RAW_BYTE_LEN);
602
    basic_table_impls!(impl_the_methods);
603
604
    /// A packed field. The high 4 bits are flags, and the low 12 bits
605
    /// are the number of tuple variation tables for this glyph. The
606
    /// number of tuple variation tables can be any number between 1
607
    /// and 4095.
608
    pub fn tuple_variation_count(&self) -> TupleVariationCount {
609
        let range = self.tuple_variation_count_byte_range();
610
        self.data.read_at(range.start).ok().unwrap()
611
    }
612
613
    /// Offset from the start of the GlyphVariationData table to the
614
    /// serialized data
615
    pub fn serialized_data_offset(&self) -> Offset16 {
616
        let range = self.serialized_data_offset_byte_range();
617
        self.data.read_at(range.start).ok().unwrap()
618
    }
619
620
    /// Attempt to resolve [`serialized_data_offset`][Self::serialized_data_offset].
621
    pub fn serialized_data(&self) -> Result<FontData<'a>, ReadError> {
622
        let data = self.data;
623
        self.serialized_data_offset().resolve(data)
624
    }
625
626
    /// Array of tuple variation headers.
627
    pub fn tuple_variation_headers(&self) -> VarLenArray<'a, TupleVariationHeader<'a>> {
628
        let range = self.tuple_variation_headers_byte_range();
629
        self.data
630
            .split_off(range.start)
631
            .and_then(|d| VarLenArray::read(d).ok())
632
            .unwrap_or_default()
633
    }
634
635
    pub fn tuple_variation_count_byte_range(&self) -> Range<usize> {
636
        let start = 0;
637
        let end = start + TupleVariationCount::RAW_BYTE_LEN;
638
        start..end
639
    }
640
641
    pub fn serialized_data_offset_byte_range(&self) -> Range<usize> {
642
        let start = self.tuple_variation_count_byte_range().end;
643
        let end = start + Offset16::RAW_BYTE_LEN;
644
        start..end
645
    }
646
647
    pub fn tuple_variation_headers_byte_range(&self) -> Range<usize> {
648
        let start = self.serialized_data_offset_byte_range().end;
649
        let end = start + self.data.len().saturating_sub(start);
650
        start..end
651
    }
652
}
653
654
const _: () = assert!(FontData::default_data_long_enough(
655
    GlyphVariationDataHeader::MIN_SIZE
656
));
657
658
impl Default for GlyphVariationDataHeader<'_> {
659
    fn default() -> Self {
660
        Self {
661
            data: FontData::default_table_data(),
662
        }
663
    }
664
}