Coverage Report

Created: 2026-10-28 07:26

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/fontations/read-fonts/generated/generated_head.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
/// The `macStyle` field for the head table.
9
#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash, bytemuck :: AnyBitPattern)]
10
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
11
#[repr(transparent)]
12
pub struct MacStyle {
13
    bits: u16,
14
}
15
16
impl MacStyle {
17
    /// Bit 0: Bold (if set to 1)
18
    pub const BOLD: Self = Self { bits: 0x0001 };
19
20
    /// Bit 1: Italic (if set to 1)
21
    pub const ITALIC: Self = Self { bits: 0x0002 };
22
23
    /// Bit 2: Underline (if set to 1)
24
    pub const UNDERLINE: Self = Self { bits: 0x0004 };
25
26
    /// Bit 3: Outline (if set to 1)
27
    pub const OUTLINE: Self = Self { bits: 0x0008 };
28
29
    /// Bit 4: Shadow (if set to 1)
30
    pub const SHADOW: Self = Self { bits: 0x0010 };
31
32
    /// Bit 5: Condensed (if set to 1)
33
    pub const CONDENSED: Self = Self { bits: 0x0020 };
34
35
    /// Bit 6: Extended (if set to 1)
36
    pub const EXTENDED: Self = Self { bits: 0x0040 };
37
}
38
39
impl MacStyle {
40
    ///  Returns an empty set of flags.
41
    #[inline]
42
    pub const fn empty() -> Self {
43
        Self { bits: 0 }
44
    }
45
46
    /// Returns the set containing all flags.
47
    #[inline]
48
    pub const fn all() -> Self {
49
        Self {
50
            bits: Self::BOLD.bits
51
                | Self::ITALIC.bits
52
                | Self::UNDERLINE.bits
53
                | Self::OUTLINE.bits
54
                | Self::SHADOW.bits
55
                | Self::CONDENSED.bits
56
                | Self::EXTENDED.bits,
57
        }
58
    }
59
60
    /// Returns the raw value of the flags currently stored.
61
    #[inline]
62
0
    pub const fn bits(&self) -> u16 {
63
0
        self.bits
64
0
    }
65
66
    /// Convert from underlying bit representation, unless that
67
    /// representation contains bits that do not correspond to a flag.
68
    #[inline]
69
    pub const fn from_bits(bits: u16) -> Option<Self> {
70
        if (bits & !Self::all().bits()) == 0 {
71
            Some(Self { bits })
72
        } else {
73
            None
74
        }
75
    }
76
77
    /// Convert from underlying bit representation, dropping any bits
78
    /// that do not correspond to flags.
79
    #[inline]
80
    pub const fn from_bits_truncate(bits: u16) -> Self {
81
        Self {
82
            bits: bits & Self::all().bits,
83
        }
84
    }
85
86
    /// Returns `true` if no flags are currently stored.
87
    #[inline]
88
    pub const fn is_empty(&self) -> bool {
89
        self.bits() == Self::empty().bits()
90
    }
91
92
    /// Returns `true` if there are flags common to both `self` and `other`.
93
    #[inline]
94
    pub const fn intersects(&self, other: Self) -> bool {
95
        !(Self {
96
            bits: self.bits & other.bits,
97
        })
98
        .is_empty()
99
    }
100
101
    /// Returns `true` if all of the flags in `other` are contained within `self`.
102
    #[inline]
103
0
    pub const fn contains(&self, other: Self) -> bool {
104
0
        (self.bits & other.bits) == other.bits
105
0
    }
106
107
    /// Inserts the specified flags in-place.
108
    #[inline]
109
    pub fn insert(&mut self, other: Self) {
110
        self.bits |= other.bits;
111
    }
112
113
    /// Removes the specified flags in-place.
114
    #[inline]
115
    pub fn remove(&mut self, other: Self) {
116
        self.bits &= !other.bits;
117
    }
118
119
    /// Toggles the specified flags in-place.
120
    #[inline]
121
    pub fn toggle(&mut self, other: Self) {
122
        self.bits ^= other.bits;
123
    }
124
125
    /// Returns the intersection between the flags in `self` and
126
    /// `other`.
127
    ///
128
    /// Specifically, the returned set contains only the flags which are
129
    /// present in *both* `self` *and* `other`.
130
    ///
131
    /// This is equivalent to using the `&` operator (e.g.
132
    /// [`ops::BitAnd`]), as in `flags & other`.
133
    ///
134
    /// [`ops::BitAnd`]: https://doc.rust-lang.org/std/ops/trait.BitAnd.html
135
    #[inline]
136
    #[must_use]
137
    pub const fn intersection(self, other: Self) -> Self {
138
        Self {
139
            bits: self.bits & other.bits,
140
        }
141
    }
142
143
    /// Returns the union of between the flags in `self` and `other`.
144
    ///
145
    /// Specifically, the returned set contains all flags which are
146
    /// present in *either* `self` *or* `other`, including any which are
147
    /// present in both.
148
    ///
149
    /// This is equivalent to using the `|` operator (e.g.
150
    /// [`ops::BitOr`]), as in `flags | other`.
151
    ///
152
    /// [`ops::BitOr`]: https://doc.rust-lang.org/std/ops/trait.BitOr.html
153
    #[inline]
154
    #[must_use]
155
    pub const fn union(self, other: Self) -> Self {
156
        Self {
157
            bits: self.bits | other.bits,
158
        }
159
    }
160
161
    /// Returns the difference between the flags in `self` and `other`.
162
    ///
163
    /// Specifically, the returned set contains all flags present in
164
    /// `self`, except for the ones present in `other`.
165
    ///
166
    /// It is also conceptually equivalent to the "bit-clear" operation:
167
    /// `flags & !other` (and this syntax is also supported).
168
    ///
169
    /// This is equivalent to using the `-` operator (e.g.
170
    /// [`ops::Sub`]), as in `flags - other`.
171
    ///
172
    /// [`ops::Sub`]: https://doc.rust-lang.org/std/ops/trait.Sub.html
173
    #[inline]
174
    #[must_use]
175
    pub const fn difference(self, other: Self) -> Self {
176
        Self {
177
            bits: self.bits & !other.bits,
178
        }
179
    }
180
}
181
182
impl std::ops::BitOr for MacStyle {
183
    type Output = Self;
184
185
    /// Returns the union of the two sets of flags.
186
    #[inline]
187
    fn bitor(self, other: MacStyle) -> Self {
188
        Self {
189
            bits: self.bits | other.bits,
190
        }
191
    }
192
}
193
194
impl std::ops::BitOrAssign for MacStyle {
195
    /// Adds the set of flags.
196
    #[inline]
197
    fn bitor_assign(&mut self, other: Self) {
198
        self.bits |= other.bits;
199
    }
200
}
201
202
impl std::ops::BitXor for MacStyle {
203
    type Output = Self;
204
205
    /// Returns the left flags, but with all the right flags toggled.
206
    #[inline]
207
    fn bitxor(self, other: Self) -> Self {
208
        Self {
209
            bits: self.bits ^ other.bits,
210
        }
211
    }
212
}
213
214
impl std::ops::BitXorAssign for MacStyle {
215
    /// Toggles the set of flags.
216
    #[inline]
217
    fn bitxor_assign(&mut self, other: Self) {
218
        self.bits ^= other.bits;
219
    }
220
}
221
222
impl std::ops::BitAnd for MacStyle {
223
    type Output = Self;
224
225
    /// Returns the intersection between the two sets of flags.
226
    #[inline]
227
    fn bitand(self, other: Self) -> Self {
228
        Self {
229
            bits: self.bits & other.bits,
230
        }
231
    }
232
}
233
234
impl std::ops::BitAndAssign for MacStyle {
235
    /// Disables all flags disabled in the set.
236
    #[inline]
237
    fn bitand_assign(&mut self, other: Self) {
238
        self.bits &= other.bits;
239
    }
240
}
241
242
impl std::ops::Sub for MacStyle {
243
    type Output = Self;
244
245
    /// Returns the set difference of the two sets of flags.
246
    #[inline]
247
    fn sub(self, other: Self) -> Self {
248
        Self {
249
            bits: self.bits & !other.bits,
250
        }
251
    }
252
}
253
254
impl std::ops::SubAssign for MacStyle {
255
    /// Disables all flags enabled in the set.
256
    #[inline]
257
    fn sub_assign(&mut self, other: Self) {
258
        self.bits &= !other.bits;
259
    }
260
}
261
262
impl std::ops::Not for MacStyle {
263
    type Output = Self;
264
265
    /// Returns the complement of this set of flags.
266
    #[inline]
267
    fn not(self) -> Self {
268
        Self { bits: !self.bits } & Self::all()
269
    }
270
}
271
272
impl std::fmt::Debug for MacStyle {
273
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
274
        let members: &[(&str, Self)] = &[
275
            ("BOLD", Self::BOLD),
276
            ("ITALIC", Self::ITALIC),
277
            ("UNDERLINE", Self::UNDERLINE),
278
            ("OUTLINE", Self::OUTLINE),
279
            ("SHADOW", Self::SHADOW),
280
            ("CONDENSED", Self::CONDENSED),
281
            ("EXTENDED", Self::EXTENDED),
282
        ];
283
        let mut first = true;
284
        for (name, value) in members {
285
            if self.contains(*value) {
286
                if !first {
287
                    f.write_str(" | ")?;
288
                }
289
                first = false;
290
                f.write_str(name)?;
291
            }
292
        }
293
        if first {
294
            f.write_str("(empty)")?;
295
        }
296
        Ok(())
297
    }
298
}
299
300
impl std::fmt::Binary for MacStyle {
301
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
302
        std::fmt::Binary::fmt(&self.bits, f)
303
    }
304
}
305
306
impl std::fmt::Octal for MacStyle {
307
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
308
        std::fmt::Octal::fmt(&self.bits, f)
309
    }
310
}
311
312
impl std::fmt::LowerHex for MacStyle {
313
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
314
        std::fmt::LowerHex::fmt(&self.bits, f)
315
    }
316
}
317
318
impl std::fmt::UpperHex for MacStyle {
319
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
320
        std::fmt::UpperHex::fmt(&self.bits, f)
321
    }
322
}
323
324
impl font_types::Scalar for MacStyle {
325
    type Raw = <u16 as font_types::Scalar>::Raw;
326
    fn to_raw(self) -> Self::Raw {
327
        self.bits().to_raw()
328
    }
329
    fn from_raw(raw: Self::Raw) -> Self {
330
        let t = <u16>::from_raw(raw);
331
        Self::from_bits_truncate(t)
332
    }
333
}
334
335
/// The `flags` field for the head table.
336
#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash, bytemuck :: AnyBitPattern)]
337
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
338
#[repr(transparent)]
339
pub struct Flags {
340
    bits: u16,
341
}
342
343
impl Flags {
344
    /// Bit 0: Baseline for font at y=0.
345
    pub const BASELINE_AT_Y_0: Self = Self { bits: 0x0001 };
346
347
    /// Bit 1: Left sidebearing point at x=0 (relevant only for TrueType rasterizers).
348
    pub const LSB_AT_X_0: Self = Self { bits: 0x0002 };
349
350
    /// Bit 2: Instructions may depend on point size.
351
    pub const INSTRUCTIONS_MAY_DEPEND_ON_POINT_SIZE: Self = Self { bits: 0x0004 };
352
353
    /// Bit 3: Force ppem to integer values for all internal scaler math; may use fractional ppem sizes if this bit is clear. It is strongly recommended that this be set in hinted fonts.
354
    pub const FORCE_INTEGER_PPEM: Self = Self { bits: 0x0008 };
355
356
    /// Bit 4: Instructions may alter advance width (the advance widths might not scale linearly).
357
    pub const INSTRUCTIONS_MAY_ALTER_ADVANCE_WIDTH: Self = Self { bits: 0x0010 };
358
359
    /// Bit 11: Font data is “lossless” as a result of having been subjected to optimizing transformation and/or compression (such as compression mechanisms defined by ISO/IEC 14496-18, MicroType® Express, WOFF 2.0, or similar) where the original font functionality and features are retained but the binary compatibility between input and output font files is not guaranteed. As a result of the applied transform, the DSIG table may also be invalidated.
360
    pub const LOSSLESS_TRANSFORMED_FONT_DATA: Self = Self { bits: 0x0800 };
361
362
    /// Bit 12: Font converted (produce compatible metrics).
363
    pub const CONVERTED_FONT: Self = Self { bits: 0x1000 };
364
365
    /// Bit 13: Font optimized for ClearType. Note, fonts that rely on embedded bitmaps (EBDT) for rendering should not be considered optimized for ClearType, and therefore should keep this bit cleared.
366
    pub const OPTIMIZED_FOR_CLEARTYPE: Self = Self { bits: 0x2000 };
367
368
    /// Bit 14: Last Resort font. If set, indicates that the glyphs encoded in the 'cmap' subtables are simply generic symbolic representations of code point ranges and do not truly represent support for those code points. If unset, indicates that the glyphs encoded in the 'cmap' subtables represent proper support for those code points.
369
    pub const LAST_RESORT_FONT: Self = Self { bits: 0x4000 };
370
}
371
372
impl Flags {
373
    ///  Returns an empty set of flags.
374
    #[inline]
375
    pub const fn empty() -> Self {
376
        Self { bits: 0 }
377
    }
378
379
    /// Returns the set containing all flags.
380
    #[inline]
381
    pub const fn all() -> Self {
382
        Self {
383
            bits: Self::BASELINE_AT_Y_0.bits
384
                | Self::LSB_AT_X_0.bits
385
                | Self::INSTRUCTIONS_MAY_DEPEND_ON_POINT_SIZE.bits
386
                | Self::FORCE_INTEGER_PPEM.bits
387
                | Self::INSTRUCTIONS_MAY_ALTER_ADVANCE_WIDTH.bits
388
                | Self::LOSSLESS_TRANSFORMED_FONT_DATA.bits
389
                | Self::CONVERTED_FONT.bits
390
                | Self::OPTIMIZED_FOR_CLEARTYPE.bits
391
                | Self::LAST_RESORT_FONT.bits,
392
        }
393
    }
394
395
    /// Returns the raw value of the flags currently stored.
396
    #[inline]
397
0
    pub const fn bits(&self) -> u16 {
398
0
        self.bits
399
0
    }
400
401
    /// Convert from underlying bit representation, unless that
402
    /// representation contains bits that do not correspond to a flag.
403
    #[inline]
404
    pub const fn from_bits(bits: u16) -> Option<Self> {
405
        if (bits & !Self::all().bits()) == 0 {
406
            Some(Self { bits })
407
        } else {
408
            None
409
        }
410
    }
411
412
    /// Convert from underlying bit representation, dropping any bits
413
    /// that do not correspond to flags.
414
    #[inline]
415
    pub const fn from_bits_truncate(bits: u16) -> Self {
416
        Self {
417
            bits: bits & Self::all().bits,
418
        }
419
    }
420
421
    /// Returns `true` if no flags are currently stored.
422
    #[inline]
423
    pub const fn is_empty(&self) -> bool {
424
        self.bits() == Self::empty().bits()
425
    }
426
427
    /// Returns `true` if there are flags common to both `self` and `other`.
428
    #[inline]
429
    pub const fn intersects(&self, other: Self) -> bool {
430
        !(Self {
431
            bits: self.bits & other.bits,
432
        })
433
        .is_empty()
434
    }
435
436
    /// Returns `true` if all of the flags in `other` are contained within `self`.
437
    #[inline]
438
0
    pub const fn contains(&self, other: Self) -> bool {
439
0
        (self.bits & other.bits) == other.bits
440
0
    }
441
442
    /// Inserts the specified flags in-place.
443
    #[inline]
444
    pub fn insert(&mut self, other: Self) {
445
        self.bits |= other.bits;
446
    }
447
448
    /// Removes the specified flags in-place.
449
    #[inline]
450
    pub fn remove(&mut self, other: Self) {
451
        self.bits &= !other.bits;
452
    }
453
454
    /// Toggles the specified flags in-place.
455
    #[inline]
456
    pub fn toggle(&mut self, other: Self) {
457
        self.bits ^= other.bits;
458
    }
459
460
    /// Returns the intersection between the flags in `self` and
461
    /// `other`.
462
    ///
463
    /// Specifically, the returned set contains only the flags which are
464
    /// present in *both* `self` *and* `other`.
465
    ///
466
    /// This is equivalent to using the `&` operator (e.g.
467
    /// [`ops::BitAnd`]), as in `flags & other`.
468
    ///
469
    /// [`ops::BitAnd`]: https://doc.rust-lang.org/std/ops/trait.BitAnd.html
470
    #[inline]
471
    #[must_use]
472
    pub const fn intersection(self, other: Self) -> Self {
473
        Self {
474
            bits: self.bits & other.bits,
475
        }
476
    }
477
478
    /// Returns the union of between the flags in `self` and `other`.
479
    ///
480
    /// Specifically, the returned set contains all flags which are
481
    /// present in *either* `self` *or* `other`, including any which are
482
    /// present in both.
483
    ///
484
    /// This is equivalent to using the `|` operator (e.g.
485
    /// [`ops::BitOr`]), as in `flags | other`.
486
    ///
487
    /// [`ops::BitOr`]: https://doc.rust-lang.org/std/ops/trait.BitOr.html
488
    #[inline]
489
    #[must_use]
490
    pub const fn union(self, other: Self) -> Self {
491
        Self {
492
            bits: self.bits | other.bits,
493
        }
494
    }
495
496
    /// Returns the difference between the flags in `self` and `other`.
497
    ///
498
    /// Specifically, the returned set contains all flags present in
499
    /// `self`, except for the ones present in `other`.
500
    ///
501
    /// It is also conceptually equivalent to the "bit-clear" operation:
502
    /// `flags & !other` (and this syntax is also supported).
503
    ///
504
    /// This is equivalent to using the `-` operator (e.g.
505
    /// [`ops::Sub`]), as in `flags - other`.
506
    ///
507
    /// [`ops::Sub`]: https://doc.rust-lang.org/std/ops/trait.Sub.html
508
    #[inline]
509
    #[must_use]
510
    pub const fn difference(self, other: Self) -> Self {
511
        Self {
512
            bits: self.bits & !other.bits,
513
        }
514
    }
515
}
516
517
impl std::ops::BitOr for Flags {
518
    type Output = Self;
519
520
    /// Returns the union of the two sets of flags.
521
    #[inline]
522
    fn bitor(self, other: Flags) -> Self {
523
        Self {
524
            bits: self.bits | other.bits,
525
        }
526
    }
527
}
528
529
impl std::ops::BitOrAssign for Flags {
530
    /// Adds the set of flags.
531
    #[inline]
532
    fn bitor_assign(&mut self, other: Self) {
533
        self.bits |= other.bits;
534
    }
535
}
536
537
impl std::ops::BitXor for Flags {
538
    type Output = Self;
539
540
    /// Returns the left flags, but with all the right flags toggled.
541
    #[inline]
542
    fn bitxor(self, other: Self) -> Self {
543
        Self {
544
            bits: self.bits ^ other.bits,
545
        }
546
    }
547
}
548
549
impl std::ops::BitXorAssign for Flags {
550
    /// Toggles the set of flags.
551
    #[inline]
552
    fn bitxor_assign(&mut self, other: Self) {
553
        self.bits ^= other.bits;
554
    }
555
}
556
557
impl std::ops::BitAnd for Flags {
558
    type Output = Self;
559
560
    /// Returns the intersection between the two sets of flags.
561
    #[inline]
562
    fn bitand(self, other: Self) -> Self {
563
        Self {
564
            bits: self.bits & other.bits,
565
        }
566
    }
567
}
568
569
impl std::ops::BitAndAssign for Flags {
570
    /// Disables all flags disabled in the set.
571
    #[inline]
572
    fn bitand_assign(&mut self, other: Self) {
573
        self.bits &= other.bits;
574
    }
575
}
576
577
impl std::ops::Sub for Flags {
578
    type Output = Self;
579
580
    /// Returns the set difference of the two sets of flags.
581
    #[inline]
582
    fn sub(self, other: Self) -> Self {
583
        Self {
584
            bits: self.bits & !other.bits,
585
        }
586
    }
587
}
588
589
impl std::ops::SubAssign for Flags {
590
    /// Disables all flags enabled in the set.
591
    #[inline]
592
    fn sub_assign(&mut self, other: Self) {
593
        self.bits &= !other.bits;
594
    }
595
}
596
597
impl std::ops::Not for Flags {
598
    type Output = Self;
599
600
    /// Returns the complement of this set of flags.
601
    #[inline]
602
    fn not(self) -> Self {
603
        Self { bits: !self.bits } & Self::all()
604
    }
605
}
606
607
impl std::fmt::Debug for Flags {
608
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
609
        let members: &[(&str, Self)] = &[
610
            ("BASELINE_AT_Y_0", Self::BASELINE_AT_Y_0),
611
            ("LSB_AT_X_0", Self::LSB_AT_X_0),
612
            (
613
                "INSTRUCTIONS_MAY_DEPEND_ON_POINT_SIZE",
614
                Self::INSTRUCTIONS_MAY_DEPEND_ON_POINT_SIZE,
615
            ),
616
            ("FORCE_INTEGER_PPEM", Self::FORCE_INTEGER_PPEM),
617
            (
618
                "INSTRUCTIONS_MAY_ALTER_ADVANCE_WIDTH",
619
                Self::INSTRUCTIONS_MAY_ALTER_ADVANCE_WIDTH,
620
            ),
621
            (
622
                "LOSSLESS_TRANSFORMED_FONT_DATA",
623
                Self::LOSSLESS_TRANSFORMED_FONT_DATA,
624
            ),
625
            ("CONVERTED_FONT", Self::CONVERTED_FONT),
626
            ("OPTIMIZED_FOR_CLEARTYPE", Self::OPTIMIZED_FOR_CLEARTYPE),
627
            ("LAST_RESORT_FONT", Self::LAST_RESORT_FONT),
628
        ];
629
        let mut first = true;
630
        for (name, value) in members {
631
            if self.contains(*value) {
632
                if !first {
633
                    f.write_str(" | ")?;
634
                }
635
                first = false;
636
                f.write_str(name)?;
637
            }
638
        }
639
        if first {
640
            f.write_str("(empty)")?;
641
        }
642
        Ok(())
643
    }
644
}
645
646
impl std::fmt::Binary for Flags {
647
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
648
        std::fmt::Binary::fmt(&self.bits, f)
649
    }
650
}
651
652
impl std::fmt::Octal for Flags {
653
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
654
        std::fmt::Octal::fmt(&self.bits, f)
655
    }
656
}
657
658
impl std::fmt::LowerHex for Flags {
659
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
660
        std::fmt::LowerHex::fmt(&self.bits, f)
661
    }
662
}
663
664
impl std::fmt::UpperHex for Flags {
665
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
666
        std::fmt::UpperHex::fmt(&self.bits, f)
667
    }
668
}
669
670
impl font_types::Scalar for Flags {
671
    type Raw = <u16 as font_types::Scalar>::Raw;
672
    fn to_raw(self) -> Self::Raw {
673
        self.bits().to_raw()
674
    }
675
    fn from_raw(raw: Self::Raw) -> Self {
676
        let t = <u16>::from_raw(raw);
677
        Self::from_bits_truncate(t)
678
    }
679
}
680
681
impl<'a> MinByteRange<'a> for Head<'a> {
682
    fn min_byte_range(&self) -> Range<usize> {
683
        0..self.glyph_data_format_byte_range().end
684
    }
685
    fn min_table_bytes(&self) -> &'a [u8] {
686
        let range = self.min_byte_range();
687
        self.offset_data().as_bytes().get(range).unwrap_or_default()
688
    }
689
}
690
691
impl TopLevelTable for Head<'_> {
692
    /// `head`
693
    const TAG: Tag = Tag::new(b"head");
694
}
695
696
impl ReadArgs for Head<'_> {
697
    type Args = ();
698
}
699
700
impl<'a> FontRead<'a> for Head<'a> {
701
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
702
        #[allow(clippy::absurd_extreme_comparisons)]
703
        if data.len() < Self::MIN_SIZE {
704
            return Err(ReadError::OutOfBounds);
705
        }
706
        let Some(data) = TableDataRef::new(data.as_bytes()) else {
707
            return Err(ReadError::OutOfBounds);
708
        };
709
        Ok(Self { data })
710
    }
711
}
712
713
/// The fields at the start of the table, which are read directly.
714
#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
715
#[repr(C, packed)]
716
#[doc(hidden)]
717
pub struct HeadFixedHeader {
718
    pub version: BigEndian<MajorMinor>,
719
    pub font_revision: BigEndian<Fixed>,
720
    pub checksum_adjustment: BigEndian<u32>,
721
    pub magic_number: BigEndian<u32>,
722
    pub flags: BigEndian<Flags>,
723
    pub units_per_em: BigEndian<u16>,
724
    pub created: BigEndian<LongDateTime>,
725
    pub modified: BigEndian<LongDateTime>,
726
    pub x_min: BigEndian<i16>,
727
    pub y_min: BigEndian<i16>,
728
    pub x_max: BigEndian<i16>,
729
    pub y_max: BigEndian<i16>,
730
    pub mac_style: BigEndian<MacStyle>,
731
    pub lowest_rec_ppem: BigEndian<u16>,
732
    pub font_direction_hint: BigEndian<i16>,
733
    pub index_to_loc_format: BigEndian<i16>,
734
    pub glyph_data_format: BigEndian<i16>,
735
}
736
737
#[allow(clippy::absurd_extreme_comparisons)]
738
const _: () = assert!(std::mem::size_of::<HeadFixedHeader>() <= Head::MIN_SIZE);
739
740
/// The [head](https://docs.microsoft.com/en-us/typography/opentype/spec/head)
741
/// (font header) table.
742
#[derive(Clone)]
743
pub struct Head<'a> {
744
    data: TableDataRef<'a, HeadFixedHeader>,
745
}
746
747
#[allow(clippy::needless_lifetimes)]
748
impl<'a> Head<'a> {
749
    pub const MIN_SIZE: usize = (MajorMinor::RAW_BYTE_LEN
750
        + Fixed::RAW_BYTE_LEN
751
        + u32::RAW_BYTE_LEN
752
        + u32::RAW_BYTE_LEN
753
        + Flags::RAW_BYTE_LEN
754
        + u16::RAW_BYTE_LEN
755
        + LongDateTime::RAW_BYTE_LEN
756
        + LongDateTime::RAW_BYTE_LEN
757
        + i16::RAW_BYTE_LEN
758
        + i16::RAW_BYTE_LEN
759
        + i16::RAW_BYTE_LEN
760
        + i16::RAW_BYTE_LEN
761
        + MacStyle::RAW_BYTE_LEN
762
        + u16::RAW_BYTE_LEN
763
        + i16::RAW_BYTE_LEN
764
        + i16::RAW_BYTE_LEN
765
        + i16::RAW_BYTE_LEN);
766
    basic_table_impls!(impl_the_methods);
767
768
    /// Version number of the font header table, set to (1, 0)
769
    pub fn version(&self) -> MajorMinor {
770
        self.data.header().version.get()
771
    }
772
773
    /// Set by font manufacturer.
774
    pub fn font_revision(&self) -> Fixed {
775
        self.data.header().font_revision.get()
776
    }
777
778
    /// To compute: set it to 0, sum the entire font as uint32, then
779
    /// store 0xB1B0AFBA - sum. If the font is used as a component in a
780
    /// font collection file, the value of this field will be
781
    /// invalidated by changes to the file structure and font table
782
    /// directory, and must be ignored.
783
    pub fn checksum_adjustment(&self) -> u32 {
784
        self.data.header().checksum_adjustment.get()
785
    }
786
787
    /// Set to 0x5F0F3CF5.
788
    pub fn magic_number(&self) -> u32 {
789
        self.data.header().magic_number.get()
790
    }
791
792
    /// See the flags enum.
793
    pub fn flags(&self) -> Flags {
794
        self.data.header().flags.get()
795
    }
796
797
    /// Set to a value from 16 to 16384. Any value in this range is
798
    /// valid. In fonts that have TrueType outlines, a power of 2 is
799
    /// recommended as this allows performance optimizations in some
800
    /// rasterizers.
801
    pub fn units_per_em(&self) -> u16 {
802
        self.data.header().units_per_em.get()
803
    }
804
805
    /// Number of seconds since 12:00 midnight that started January 1st
806
    /// 1904 in GMT/UTC time zone.
807
    pub fn created(&self) -> LongDateTime {
808
        self.data.header().created.get()
809
    }
810
811
    /// Number of seconds since 12:00 midnight that started January 1st
812
    /// 1904 in GMT/UTC time zone.
813
    pub fn modified(&self) -> LongDateTime {
814
        self.data.header().modified.get()
815
    }
816
817
    /// Minimum x coordinate across all glyph bounding boxes.
818
    pub fn x_min(&self) -> i16 {
819
        self.data.header().x_min.get()
820
    }
821
822
    /// Minimum y coordinate across all glyph bounding boxes.
823
    pub fn y_min(&self) -> i16 {
824
        self.data.header().y_min.get()
825
    }
826
827
    /// Maximum x coordinate across all glyph bounding boxes.
828
    pub fn x_max(&self) -> i16 {
829
        self.data.header().x_max.get()
830
    }
831
832
    /// Maximum y coordinate across all glyph bounding boxes.
833
    pub fn y_max(&self) -> i16 {
834
        self.data.header().y_max.get()
835
    }
836
837
    /// Bits identifying the font's style; see [MacStyle]
838
    pub fn mac_style(&self) -> MacStyle {
839
        self.data.header().mac_style.get()
840
    }
841
842
    /// Smallest readable size in pixels.
843
    pub fn lowest_rec_ppem(&self) -> u16 {
844
        self.data.header().lowest_rec_ppem.get()
845
    }
846
847
    /// Deprecated (Set to 2).
848
    pub fn font_direction_hint(&self) -> i16 {
849
        self.data.header().font_direction_hint.get()
850
    }
851
852
    /// 0 for short offsets (Offset16), 1 for long (Offset32).
853
    pub fn index_to_loc_format(&self) -> i16 {
854
        self.data.header().index_to_loc_format.get()
855
    }
856
857
    /// 0 for current format.
858
    pub fn glyph_data_format(&self) -> i16 {
859
        self.data.header().glyph_data_format.get()
860
    }
861
862
    pub fn version_byte_range(&self) -> Range<usize> {
863
        let start = 0usize;
864
        let end = start.saturating_add(MajorMinor::RAW_BYTE_LEN);
865
        start..end
866
    }
867
868
    pub fn font_revision_byte_range(&self) -> Range<usize> {
869
        let start = self.version_byte_range().end;
870
        let end = start.saturating_add(Fixed::RAW_BYTE_LEN);
871
        start..end
872
    }
873
874
    pub fn checksum_adjustment_byte_range(&self) -> Range<usize> {
875
        let start = self.font_revision_byte_range().end;
876
        let end = start.saturating_add(u32::RAW_BYTE_LEN);
877
        start..end
878
    }
879
880
    pub fn magic_number_byte_range(&self) -> Range<usize> {
881
        let start = self.checksum_adjustment_byte_range().end;
882
        let end = start.saturating_add(u32::RAW_BYTE_LEN);
883
        start..end
884
    }
885
886
    pub fn flags_byte_range(&self) -> Range<usize> {
887
        let start = self.magic_number_byte_range().end;
888
        let end = start.saturating_add(Flags::RAW_BYTE_LEN);
889
        start..end
890
    }
891
892
    pub fn units_per_em_byte_range(&self) -> Range<usize> {
893
        let start = self.flags_byte_range().end;
894
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
895
        start..end
896
    }
897
898
    pub fn created_byte_range(&self) -> Range<usize> {
899
        let start = self.units_per_em_byte_range().end;
900
        let end = start.saturating_add(LongDateTime::RAW_BYTE_LEN);
901
        start..end
902
    }
903
904
    pub fn modified_byte_range(&self) -> Range<usize> {
905
        let start = self.created_byte_range().end;
906
        let end = start.saturating_add(LongDateTime::RAW_BYTE_LEN);
907
        start..end
908
    }
909
910
    pub fn x_min_byte_range(&self) -> Range<usize> {
911
        let start = self.modified_byte_range().end;
912
        let end = start.saturating_add(i16::RAW_BYTE_LEN);
913
        start..end
914
    }
915
916
    pub fn y_min_byte_range(&self) -> Range<usize> {
917
        let start = self.x_min_byte_range().end;
918
        let end = start.saturating_add(i16::RAW_BYTE_LEN);
919
        start..end
920
    }
921
922
    pub fn x_max_byte_range(&self) -> Range<usize> {
923
        let start = self.y_min_byte_range().end;
924
        let end = start.saturating_add(i16::RAW_BYTE_LEN);
925
        start..end
926
    }
927
928
    pub fn y_max_byte_range(&self) -> Range<usize> {
929
        let start = self.x_max_byte_range().end;
930
        let end = start.saturating_add(i16::RAW_BYTE_LEN);
931
        start..end
932
    }
933
934
    pub fn mac_style_byte_range(&self) -> Range<usize> {
935
        let start = self.y_max_byte_range().end;
936
        let end = start.saturating_add(MacStyle::RAW_BYTE_LEN);
937
        start..end
938
    }
939
940
    pub fn lowest_rec_ppem_byte_range(&self) -> Range<usize> {
941
        let start = self.mac_style_byte_range().end;
942
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
943
        start..end
944
    }
945
946
    pub fn font_direction_hint_byte_range(&self) -> Range<usize> {
947
        let start = self.lowest_rec_ppem_byte_range().end;
948
        let end = start.saturating_add(i16::RAW_BYTE_LEN);
949
        start..end
950
    }
951
952
    pub fn index_to_loc_format_byte_range(&self) -> Range<usize> {
953
        let start = self.font_direction_hint_byte_range().end;
954
        let end = start.saturating_add(i16::RAW_BYTE_LEN);
955
        start..end
956
    }
957
958
    pub fn glyph_data_format_byte_range(&self) -> Range<usize> {
959
        let start = self.index_to_loc_format_byte_range().end;
960
        let end = start.saturating_add(i16::RAW_BYTE_LEN);
961
        start..end
962
    }
963
}
964
965
const _: () = assert!(FontData::default_data_long_enough(Head::MIN_SIZE));
966
967
impl Default for Head<'_> {
968
    fn default() -> Self {
969
        Self {
970
            data: TableDataRef::new(FontData::default_table_data().as_bytes())
971
                .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
972
        }
973
    }
974
}