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_bitmap.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
/// [BitmapSize](https://learn.microsoft.com/en-us/typography/opentype/spec/eblc#bitmapsize-record) record.
9
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, bytemuck :: AnyBitPattern)]
10
#[repr(C)]
11
#[repr(packed)]
12
pub struct BitmapSize {
13
    /// Offset to IndexSubtableList, from beginning of EBLC/CBLC.
14
    pub index_subtable_list_offset: BigEndian<u32>,
15
    /// Total size in bytes of the IndexSubtableList including its array of IndexSubtables.
16
    pub index_subtable_list_size: BigEndian<u32>,
17
    /// Number of IndexSubtables in the IndexSubtableList.
18
    pub number_of_index_subtables: BigEndian<u32>,
19
    /// Not used; set to 0.
20
    pub color_ref: BigEndian<u32>,
21
    /// Line metrics for text rendered horizontally.
22
    pub hori: SbitLineMetrics,
23
    /// Line metrics for text rendered vertically.
24
    pub vert: SbitLineMetrics,
25
    /// Lowest glyph index for this size.
26
    pub start_glyph_index: BigEndian<GlyphId16>,
27
    /// Highest glyph index for this size.
28
    pub end_glyph_index: BigEndian<GlyphId16>,
29
    /// Horizontal pixels per em.
30
    pub ppem_x: u8,
31
    /// Vertical pixels per em.
32
    pub ppem_y: u8,
33
    /// The Microsoft rasterizer v.1.7 or greater supports the following
34
    /// bitDepth values, as described below: 1, 2, 4, and 8 (and 32 for CBLC).
35
    pub bit_depth: u8,
36
    /// Vertical or horizontal.
37
    pub flags: BigEndian<BitmapFlags>,
38
}
39
40
impl BitmapSize {
41
    /// Offset to IndexSubtableList, from beginning of EBLC/CBLC.
42
4.16k
    pub fn index_subtable_list_offset(&self) -> u32 {
43
4.16k
        self.index_subtable_list_offset.get()
44
4.16k
    }
45
46
    /// Total size in bytes of the IndexSubtableList including its array of IndexSubtables.
47
0
    pub fn index_subtable_list_size(&self) -> u32 {
48
0
        self.index_subtable_list_size.get()
49
0
    }
50
51
    /// Number of IndexSubtables in the IndexSubtableList.
52
1.04k
    pub fn number_of_index_subtables(&self) -> u32 {
53
1.04k
        self.number_of_index_subtables.get()
54
1.04k
    }
55
56
    /// Not used; set to 0.
57
0
    pub fn color_ref(&self) -> u32 {
58
0
        self.color_ref.get()
59
0
    }
60
61
    /// Line metrics for text rendered horizontally.
62
0
    pub fn hori(&self) -> &SbitLineMetrics {
63
0
        &self.hori
64
0
    }
65
66
    /// Line metrics for text rendered vertically.
67
0
    pub fn vert(&self) -> &SbitLineMetrics {
68
0
        &self.vert
69
0
    }
70
71
    /// Lowest glyph index for this size.
72
5.80k
    pub fn start_glyph_index(&self) -> GlyphId16 {
73
5.80k
        self.start_glyph_index.get()
74
5.80k
    }
75
76
    /// Highest glyph index for this size.
77
5.80k
    pub fn end_glyph_index(&self) -> GlyphId16 {
78
5.80k
        self.end_glyph_index.get()
79
5.80k
    }
80
81
    /// Horizontal pixels per em.
82
11.0k
    pub fn ppem_x(&self) -> u8 {
83
11.0k
        self.ppem_x
84
11.0k
    }
85
86
    /// Vertical pixels per em.
87
8.43k
    pub fn ppem_y(&self) -> u8 {
88
8.43k
        self.ppem_y
89
8.43k
    }
90
91
    /// The Microsoft rasterizer v.1.7 or greater supports the following
92
    /// bitDepth values, as described below: 1, 2, 4, and 8 (and 32 for CBLC).
93
0
    pub fn bit_depth(&self) -> u8 {
94
0
        self.bit_depth
95
0
    }
96
97
    /// Vertical or horizontal.
98
0
    pub fn flags(&self) -> BitmapFlags {
99
0
        self.flags.get()
100
0
    }
101
}
102
103
impl FixedSize for BitmapSize {
104
    const RAW_BYTE_LEN: usize = u32::RAW_BYTE_LEN
105
        + u32::RAW_BYTE_LEN
106
        + u32::RAW_BYTE_LEN
107
        + u32::RAW_BYTE_LEN
108
        + SbitLineMetrics::RAW_BYTE_LEN
109
        + SbitLineMetrics::RAW_BYTE_LEN
110
        + GlyphId16::RAW_BYTE_LEN
111
        + GlyphId16::RAW_BYTE_LEN
112
        + u8::RAW_BYTE_LEN
113
        + u8::RAW_BYTE_LEN
114
        + u8::RAW_BYTE_LEN
115
        + BitmapFlags::RAW_BYTE_LEN;
116
}
117
118
/// [SbitLineMetrics](https://learn.microsoft.com/en-us/typography/opentype/spec/eblc#sbitlinemetrics-record) record.
119
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, bytemuck :: AnyBitPattern)]
120
#[repr(C)]
121
#[repr(packed)]
122
pub struct SbitLineMetrics {
123
    pub ascender: BigEndian<i8>,
124
    pub descender: BigEndian<i8>,
125
    pub width_max: u8,
126
    pub caret_slope_numerator: BigEndian<i8>,
127
    pub caret_slope_denominator: u8,
128
    pub caret_offset: BigEndian<i8>,
129
    pub min_origin_sb: BigEndian<i8>,
130
    pub min_advance_sb: BigEndian<i8>,
131
    pub max_before_bl: BigEndian<i8>,
132
    pub min_after_bl: BigEndian<i8>,
133
    pub pad1: BigEndian<i8>,
134
    pub pad2: BigEndian<i8>,
135
}
136
137
impl SbitLineMetrics {
138
0
    pub fn ascender(&self) -> i8 {
139
0
        self.ascender.get()
140
0
    }
141
142
0
    pub fn descender(&self) -> i8 {
143
0
        self.descender.get()
144
0
    }
145
146
0
    pub fn width_max(&self) -> u8 {
147
0
        self.width_max
148
0
    }
149
150
0
    pub fn caret_slope_numerator(&self) -> i8 {
151
0
        self.caret_slope_numerator.get()
152
0
    }
153
154
0
    pub fn caret_slope_denominator(&self) -> u8 {
155
0
        self.caret_slope_denominator
156
0
    }
157
158
0
    pub fn caret_offset(&self) -> i8 {
159
0
        self.caret_offset.get()
160
0
    }
161
162
0
    pub fn min_origin_sb(&self) -> i8 {
163
0
        self.min_origin_sb.get()
164
0
    }
165
166
0
    pub fn min_advance_sb(&self) -> i8 {
167
0
        self.min_advance_sb.get()
168
0
    }
169
170
0
    pub fn max_before_bl(&self) -> i8 {
171
0
        self.max_before_bl.get()
172
0
    }
173
174
0
    pub fn min_after_bl(&self) -> i8 {
175
0
        self.min_after_bl.get()
176
0
    }
177
178
0
    pub fn pad1(&self) -> i8 {
179
0
        self.pad1.get()
180
0
    }
181
182
0
    pub fn pad2(&self) -> i8 {
183
0
        self.pad2.get()
184
0
    }
185
}
186
187
impl FixedSize for SbitLineMetrics {
188
    const RAW_BYTE_LEN: usize = i8::RAW_BYTE_LEN
189
        + i8::RAW_BYTE_LEN
190
        + u8::RAW_BYTE_LEN
191
        + i8::RAW_BYTE_LEN
192
        + u8::RAW_BYTE_LEN
193
        + i8::RAW_BYTE_LEN
194
        + i8::RAW_BYTE_LEN
195
        + i8::RAW_BYTE_LEN
196
        + i8::RAW_BYTE_LEN
197
        + i8::RAW_BYTE_LEN
198
        + i8::RAW_BYTE_LEN
199
        + i8::RAW_BYTE_LEN;
200
}
201
202
/// [Bitmap flags](https://learn.microsoft.com/en-us/typography/opentype/spec/eblc#bitmap-flags).
203
#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash, bytemuck :: AnyBitPattern)]
204
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
205
#[repr(transparent)]
206
pub struct BitmapFlags {
207
    bits: u8,
208
}
209
210
impl BitmapFlags {
211
    /// Horizontal
212
    pub const HORIZONTAL_METRICS: Self = Self { bits: 0x01 };
213
214
    /// Vertical
215
    pub const VERTICAL_METRICS: Self = Self { bits: 0x02 };
216
}
217
218
impl BitmapFlags {
219
    ///  Returns an empty set of flags.
220
    #[inline]
221
0
    pub const fn empty() -> Self {
222
0
        Self { bits: 0 }
223
0
    }
224
225
    /// Returns the set containing all flags.
226
    #[inline]
227
0
    pub const fn all() -> Self {
228
0
        Self {
229
0
            bits: Self::HORIZONTAL_METRICS.bits | Self::VERTICAL_METRICS.bits,
230
0
        }
231
0
    }
232
233
    /// Returns the raw value of the flags currently stored.
234
    #[inline]
235
0
    pub const fn bits(&self) -> u8 {
236
0
        self.bits
237
0
    }
238
239
    /// Convert from underlying bit representation, unless that
240
    /// representation contains bits that do not correspond to a flag.
241
    #[inline]
242
0
    pub const fn from_bits(bits: u8) -> Option<Self> {
243
0
        if (bits & !Self::all().bits()) == 0 {
244
0
            Some(Self { bits })
245
        } else {
246
0
            None
247
        }
248
0
    }
249
250
    /// Convert from underlying bit representation, dropping any bits
251
    /// that do not correspond to flags.
252
    #[inline]
253
0
    pub const fn from_bits_truncate(bits: u8) -> Self {
254
0
        Self {
255
0
            bits: bits & Self::all().bits,
256
0
        }
257
0
    }
258
259
    /// Returns `true` if no flags are currently stored.
260
    #[inline]
261
0
    pub const fn is_empty(&self) -> bool {
262
0
        self.bits() == Self::empty().bits()
263
0
    }
264
265
    /// Returns `true` if there are flags common to both `self` and `other`.
266
    #[inline]
267
0
    pub const fn intersects(&self, other: Self) -> bool {
268
0
        !(Self {
269
0
            bits: self.bits & other.bits,
270
0
        })
271
0
        .is_empty()
272
0
    }
273
274
    /// Returns `true` if all of the flags in `other` are contained within `self`.
275
    #[inline]
276
0
    pub const fn contains(&self, other: Self) -> bool {
277
0
        (self.bits & other.bits) == other.bits
278
0
    }
279
280
    /// Inserts the specified flags in-place.
281
    #[inline]
282
0
    pub fn insert(&mut self, other: Self) {
283
0
        self.bits |= other.bits;
284
0
    }
285
286
    /// Removes the specified flags in-place.
287
    #[inline]
288
0
    pub fn remove(&mut self, other: Self) {
289
0
        self.bits &= !other.bits;
290
0
    }
291
292
    /// Toggles the specified flags in-place.
293
    #[inline]
294
0
    pub fn toggle(&mut self, other: Self) {
295
0
        self.bits ^= other.bits;
296
0
    }
297
298
    /// Returns the intersection between the flags in `self` and
299
    /// `other`.
300
    ///
301
    /// Specifically, the returned set contains only the flags which are
302
    /// present in *both* `self` *and* `other`.
303
    ///
304
    /// This is equivalent to using the `&` operator (e.g.
305
    /// [`ops::BitAnd`]), as in `flags & other`.
306
    ///
307
    /// [`ops::BitAnd`]: https://doc.rust-lang.org/std/ops/trait.BitAnd.html
308
    #[inline]
309
    #[must_use]
310
0
    pub const fn intersection(self, other: Self) -> Self {
311
0
        Self {
312
0
            bits: self.bits & other.bits,
313
0
        }
314
0
    }
315
316
    /// Returns the union of between the flags in `self` and `other`.
317
    ///
318
    /// Specifically, the returned set contains all flags which are
319
    /// present in *either* `self` *or* `other`, including any which are
320
    /// present in both.
321
    ///
322
    /// This is equivalent to using the `|` operator (e.g.
323
    /// [`ops::BitOr`]), as in `flags | other`.
324
    ///
325
    /// [`ops::BitOr`]: https://doc.rust-lang.org/std/ops/trait.BitOr.html
326
    #[inline]
327
    #[must_use]
328
0
    pub const fn union(self, other: Self) -> Self {
329
0
        Self {
330
0
            bits: self.bits | other.bits,
331
0
        }
332
0
    }
333
334
    /// Returns the difference between the flags in `self` and `other`.
335
    ///
336
    /// Specifically, the returned set contains all flags present in
337
    /// `self`, except for the ones present in `other`.
338
    ///
339
    /// It is also conceptually equivalent to the "bit-clear" operation:
340
    /// `flags & !other` (and this syntax is also supported).
341
    ///
342
    /// This is equivalent to using the `-` operator (e.g.
343
    /// [`ops::Sub`]), as in `flags - other`.
344
    ///
345
    /// [`ops::Sub`]: https://doc.rust-lang.org/std/ops/trait.Sub.html
346
    #[inline]
347
    #[must_use]
348
0
    pub const fn difference(self, other: Self) -> Self {
349
0
        Self {
350
0
            bits: self.bits & !other.bits,
351
0
        }
352
0
    }
353
}
354
355
impl std::ops::BitOr for BitmapFlags {
356
    type Output = Self;
357
358
    /// Returns the union of the two sets of flags.
359
    #[inline]
360
0
    fn bitor(self, other: BitmapFlags) -> Self {
361
0
        Self {
362
0
            bits: self.bits | other.bits,
363
0
        }
364
0
    }
365
}
366
367
impl std::ops::BitOrAssign for BitmapFlags {
368
    /// Adds the set of flags.
369
    #[inline]
370
0
    fn bitor_assign(&mut self, other: Self) {
371
0
        self.bits |= other.bits;
372
0
    }
373
}
374
375
impl std::ops::BitXor for BitmapFlags {
376
    type Output = Self;
377
378
    /// Returns the left flags, but with all the right flags toggled.
379
    #[inline]
380
0
    fn bitxor(self, other: Self) -> Self {
381
0
        Self {
382
0
            bits: self.bits ^ other.bits,
383
0
        }
384
0
    }
385
}
386
387
impl std::ops::BitXorAssign for BitmapFlags {
388
    /// Toggles the set of flags.
389
    #[inline]
390
0
    fn bitxor_assign(&mut self, other: Self) {
391
0
        self.bits ^= other.bits;
392
0
    }
393
}
394
395
impl std::ops::BitAnd for BitmapFlags {
396
    type Output = Self;
397
398
    /// Returns the intersection between the two sets of flags.
399
    #[inline]
400
0
    fn bitand(self, other: Self) -> Self {
401
0
        Self {
402
0
            bits: self.bits & other.bits,
403
0
        }
404
0
    }
405
}
406
407
impl std::ops::BitAndAssign for BitmapFlags {
408
    /// Disables all flags disabled in the set.
409
    #[inline]
410
0
    fn bitand_assign(&mut self, other: Self) {
411
0
        self.bits &= other.bits;
412
0
    }
413
}
414
415
impl std::ops::Sub for BitmapFlags {
416
    type Output = Self;
417
418
    /// Returns the set difference of the two sets of flags.
419
    #[inline]
420
0
    fn sub(self, other: Self) -> Self {
421
0
        Self {
422
0
            bits: self.bits & !other.bits,
423
0
        }
424
0
    }
425
}
426
427
impl std::ops::SubAssign for BitmapFlags {
428
    /// Disables all flags enabled in the set.
429
    #[inline]
430
0
    fn sub_assign(&mut self, other: Self) {
431
0
        self.bits &= !other.bits;
432
0
    }
433
}
434
435
impl std::ops::Not for BitmapFlags {
436
    type Output = Self;
437
438
    /// Returns the complement of this set of flags.
439
    #[inline]
440
0
    fn not(self) -> Self {
441
0
        Self { bits: !self.bits } & Self::all()
442
0
    }
443
}
444
445
impl std::fmt::Debug for BitmapFlags {
446
0
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
447
0
        let members: &[(&str, Self)] = &[
448
0
            ("HORIZONTAL_METRICS", Self::HORIZONTAL_METRICS),
449
0
            ("VERTICAL_METRICS", Self::VERTICAL_METRICS),
450
0
        ];
451
0
        let mut first = true;
452
0
        for (name, value) in members {
453
0
            if self.contains(*value) {
454
0
                if !first {
455
0
                    f.write_str(" | ")?;
456
0
                }
457
0
                first = false;
458
0
                f.write_str(name)?;
459
0
            }
460
        }
461
0
        if first {
462
0
            f.write_str("(empty)")?;
463
0
        }
464
0
        Ok(())
465
0
    }
466
}
467
468
impl std::fmt::Binary for BitmapFlags {
469
0
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
470
0
        std::fmt::Binary::fmt(&self.bits, f)
471
0
    }
472
}
473
474
impl std::fmt::Octal for BitmapFlags {
475
0
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
476
0
        std::fmt::Octal::fmt(&self.bits, f)
477
0
    }
478
}
479
480
impl std::fmt::LowerHex for BitmapFlags {
481
0
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
482
0
        std::fmt::LowerHex::fmt(&self.bits, f)
483
0
    }
484
}
485
486
impl std::fmt::UpperHex for BitmapFlags {
487
0
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
488
0
        std::fmt::UpperHex::fmt(&self.bits, f)
489
0
    }
490
}
491
492
impl font_types::Scalar for BitmapFlags {
493
    type Raw = <u8 as font_types::Scalar>::Raw;
494
0
    fn to_raw(self) -> Self::Raw {
495
0
        self.bits().to_raw()
496
0
    }
497
0
    fn from_raw(raw: Self::Raw) -> Self {
498
0
        let t = <u8>::from_raw(raw);
499
0
        Self::from_bits_truncate(t)
500
0
    }
501
}
502
503
/// [BigGlyphMetrics](https://learn.microsoft.com/en-us/typography/opentype/spec/eblc#bigglyphmetrics) record.
504
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, bytemuck :: AnyBitPattern)]
505
#[repr(C)]
506
#[repr(packed)]
507
pub struct BigGlyphMetrics {
508
    /// Number of rows of data.
509
    pub height: u8,
510
    /// Number of columns of data.
511
    pub width: u8,
512
    /// Distance in pixels from the horizontal origin to the left edge of the bitmap.
513
    pub hori_bearing_x: BigEndian<i8>,
514
    /// Distance in pixels from the horizontal origin to the top edge of the bitmap.
515
    pub hori_bearing_y: BigEndian<i8>,
516
    /// Horizontal advance width in pixels.
517
    pub hori_advance: u8,
518
    /// Distance in pixels from the vertical origin to the left edge of the bitmap.
519
    pub vert_bearing_x: BigEndian<i8>,
520
    /// Distance in pixels from the vertical origin to the top edge of the bitmap.
521
    pub vert_bearing_y: BigEndian<i8>,
522
    /// Vertical advance width in pixels.
523
    pub vert_advance: u8,
524
}
525
526
impl BigGlyphMetrics {
527
    /// Number of rows of data.
528
0
    pub fn height(&self) -> u8 {
529
0
        self.height
530
0
    }
531
532
    /// Number of columns of data.
533
0
    pub fn width(&self) -> u8 {
534
0
        self.width
535
0
    }
536
537
    /// Distance in pixels from the horizontal origin to the left edge of the bitmap.
538
0
    pub fn hori_bearing_x(&self) -> i8 {
539
0
        self.hori_bearing_x.get()
540
0
    }
541
542
    /// Distance in pixels from the horizontal origin to the top edge of the bitmap.
543
0
    pub fn hori_bearing_y(&self) -> i8 {
544
0
        self.hori_bearing_y.get()
545
0
    }
546
547
    /// Horizontal advance width in pixels.
548
0
    pub fn hori_advance(&self) -> u8 {
549
0
        self.hori_advance
550
0
    }
551
552
    /// Distance in pixels from the vertical origin to the left edge of the bitmap.
553
0
    pub fn vert_bearing_x(&self) -> i8 {
554
0
        self.vert_bearing_x.get()
555
0
    }
556
557
    /// Distance in pixels from the vertical origin to the top edge of the bitmap.
558
0
    pub fn vert_bearing_y(&self) -> i8 {
559
0
        self.vert_bearing_y.get()
560
0
    }
561
562
    /// Vertical advance width in pixels.
563
0
    pub fn vert_advance(&self) -> u8 {
564
0
        self.vert_advance
565
0
    }
566
}
567
568
impl FixedSize for BigGlyphMetrics {
569
    const RAW_BYTE_LEN: usize = u8::RAW_BYTE_LEN
570
        + u8::RAW_BYTE_LEN
571
        + i8::RAW_BYTE_LEN
572
        + i8::RAW_BYTE_LEN
573
        + u8::RAW_BYTE_LEN
574
        + i8::RAW_BYTE_LEN
575
        + i8::RAW_BYTE_LEN
576
        + u8::RAW_BYTE_LEN;
577
}
578
579
/// [SmallGlyphMetrics](https://learn.microsoft.com/en-us/typography/opentype/spec/eblc#smallglyphmetrics) record.
580
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, bytemuck :: AnyBitPattern)]
581
#[repr(C)]
582
#[repr(packed)]
583
pub struct SmallGlyphMetrics {
584
    /// Number of rows of data.
585
    pub height: u8,
586
    /// Number of columns of data.
587
    pub width: u8,
588
    /// Distance in pixels from the horizontal origin to the left edge of the bitmap (for horizontal text); or distance in pixels from the vertical origin to the top edge of the bitmap (for vertical text).
589
    pub bearing_x: BigEndian<i8>,
590
    /// Distance in pixels from the horizontal origin to the top edge of the bitmap (for horizontal text); or distance in pixels from the vertical origin to the left edge of the bitmap (for vertical text).
591
    pub bearing_y: BigEndian<i8>,
592
    /// Horizontal or vertical advance width in pixels.
593
    pub advance: u8,
594
}
595
596
impl SmallGlyphMetrics {
597
    /// Number of rows of data.
598
0
    pub fn height(&self) -> u8 {
599
0
        self.height
600
0
    }
601
602
    /// Number of columns of data.
603
0
    pub fn width(&self) -> u8 {
604
0
        self.width
605
0
    }
606
607
    /// Distance in pixels from the horizontal origin to the left edge of the bitmap (for horizontal text); or distance in pixels from the vertical origin to the top edge of the bitmap (for vertical text).
608
0
    pub fn bearing_x(&self) -> i8 {
609
0
        self.bearing_x.get()
610
0
    }
611
612
    /// Distance in pixels from the horizontal origin to the top edge of the bitmap (for horizontal text); or distance in pixels from the vertical origin to the left edge of the bitmap (for vertical text).
613
0
    pub fn bearing_y(&self) -> i8 {
614
0
        self.bearing_y.get()
615
0
    }
616
617
    /// Horizontal or vertical advance width in pixels.
618
0
    pub fn advance(&self) -> u8 {
619
0
        self.advance
620
0
    }
621
}
622
623
impl FixedSize for SmallGlyphMetrics {
624
    const RAW_BYTE_LEN: usize = u8::RAW_BYTE_LEN
625
        + u8::RAW_BYTE_LEN
626
        + i8::RAW_BYTE_LEN
627
        + i8::RAW_BYTE_LEN
628
        + u8::RAW_BYTE_LEN;
629
}
630
631
impl<'a> MinByteRange<'a> for IndexSubtableList<'a> {
632
0
    fn min_byte_range(&self) -> Range<usize> {
633
0
        0..self.index_subtable_records_byte_range().end
634
0
    }
635
0
    fn min_table_bytes(&self) -> &'a [u8] {
636
0
        let range = self.min_byte_range();
637
0
        self.offset_data().as_bytes().get(range).unwrap_or_default()
638
0
    }
639
}
640
641
impl ReadArgs for IndexSubtableList<'_> {
642
    type Args = u32;
643
}
644
645
impl<'a> FontRead<'a> for IndexSubtableList<'a> {
646
1.04k
    fn read_with_args(data: FontData<'a>, args: u32) -> Result<Self, ReadError> {
647
1.04k
        let number_of_index_subtables = args;
648
649
        #[allow(clippy::absurd_extreme_comparisons)]
650
1.04k
        if data.len() < Self::MIN_SIZE {
651
0
            return Err(ReadError::OutOfBounds);
652
1.04k
        }
653
1.04k
        let Some(data) = TableDataRef::new(data.as_bytes()) else {
654
0
            return Err(ReadError::OutOfBounds);
655
        };
656
1.04k
        Ok(Self {
657
1.04k
            data,
658
1.04k
            number_of_index_subtables,
659
1.04k
        })
660
1.04k
    }
661
}
662
663
impl<'a> IndexSubtableList<'a> {
664
    /// A constructor that requires additional arguments.
665
    ///
666
    /// This type requires some external state in order to be
667
    /// parsed.
668
1.04k
    pub fn read(data: FontData<'a>, number_of_index_subtables: u32) -> Result<Self, ReadError> {
669
1.04k
        let args = number_of_index_subtables;
670
1.04k
        Self::read_with_args(data, args)
671
1.04k
    }
672
}
673
674
/// The fields at the start of the table, which are read directly.
675
#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
676
#[repr(C, packed)]
677
#[doc(hidden)]
678
pub struct IndexSubtableListFixedHeader {}
679
680
#[allow(clippy::absurd_extreme_comparisons)]
681
const _: () =
682
    assert!(std::mem::size_of::<IndexSubtableListFixedHeader>() <= IndexSubtableList::MIN_SIZE);
683
684
/// [IndexSubtableList](https://learn.microsoft.com/en-us/typography/opentype/spec/eblc#indexsubtablelist) table.
685
#[derive(Clone)]
686
pub struct IndexSubtableList<'a> {
687
    data: TableDataRef<'a, IndexSubtableListFixedHeader>,
688
    number_of_index_subtables: u32,
689
}
690
691
#[allow(clippy::needless_lifetimes)]
692
impl<'a> IndexSubtableList<'a> {
693
    pub const MIN_SIZE: usize = 0;
694
    basic_table_impls!(impl_the_methods);
695
696
    /// Array of IndexSubtableRecords.
697
1.04k
    pub fn index_subtable_records(&self) -> &'a [IndexSubtableRecord] {
698
1.04k
        let range = self.index_subtable_records_byte_range();
699
1.04k
        self.offset_data()
700
1.04k
            .read_array(range)
701
1.04k
            .ok()
702
1.04k
            .unwrap_or_default()
703
1.04k
    }
704
705
1.04k
    pub(crate) fn number_of_index_subtables(&self) -> u32 {
706
1.04k
        self.number_of_index_subtables
707
1.04k
    }
708
709
1.04k
    pub fn index_subtable_records_byte_range(&self) -> Range<usize> {
710
1.04k
        let number_of_index_subtables = self.number_of_index_subtables();
711
1.04k
        let start = 0usize;
712
1.04k
        let end = start.saturating_add(
713
1.04k
            (transforms::to_usize(number_of_index_subtables))
714
1.04k
                .saturating_mul(IndexSubtableRecord::RAW_BYTE_LEN),
715
        );
716
1.04k
        start..end
717
1.04k
    }
718
}
719
720
#[allow(clippy::absurd_extreme_comparisons)]
721
const _: () = assert!(FontData::default_data_long_enough(
722
    IndexSubtableList::MIN_SIZE
723
));
724
725
impl Default for IndexSubtableList<'_> {
726
0
    fn default() -> Self {
727
        Self {
728
0
            data: TableDataRef::new(FontData::default_table_data().as_bytes())
729
0
                .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
730
0
            number_of_index_subtables: Default::default(),
731
        }
732
0
    }
733
}
734
735
#[derive(Clone, Debug, Copy, bytemuck :: AnyBitPattern)]
736
#[repr(C)]
737
#[repr(packed)]
738
pub struct IndexSubtableRecord {
739
    /// First glyph ID of this range.
740
    pub first_glyph_index: BigEndian<GlyphId16>,
741
    /// Last glyph ID of this range (inclusive).
742
    pub last_glyph_index: BigEndian<GlyphId16>,
743
    /// Offset to an IndexSubtable from the start of the IndexSubtableList.
744
    pub index_subtable_offset: BigEndian<Offset32>,
745
}
746
747
impl IndexSubtableRecord {
748
    /// First glyph ID of this range.
749
0
    pub fn first_glyph_index(&self) -> GlyphId16 {
750
0
        self.first_glyph_index.get()
751
0
    }
752
753
    /// Last glyph ID of this range (inclusive).
754
0
    pub fn last_glyph_index(&self) -> GlyphId16 {
755
0
        self.last_glyph_index.get()
756
0
    }
757
758
    /// Offset to an IndexSubtable from the start of the IndexSubtableList.
759
0
    pub fn index_subtable_offset(&self) -> Offset32 {
760
0
        self.index_subtable_offset.get()
761
0
    }
762
763
    /// Offset to an IndexSubtable from the start of the IndexSubtableList.
764
    ///
765
    /// The `data` argument should be retrieved from the parent table
766
    /// By calling its `offset_data` method.
767
0
    pub fn index_subtable<'a>(&self, data: FontData<'a>) -> Result<IndexSubtable<'a>, ReadError> {
768
0
        let args = (self.last_glyph_index(), self.first_glyph_index());
769
0
        self.index_subtable_offset().resolve_with_args(data, args)
770
0
    }
771
}
772
773
impl FixedSize for IndexSubtableRecord {
774
    const RAW_BYTE_LEN: usize =
775
        GlyphId16::RAW_BYTE_LEN + GlyphId16::RAW_BYTE_LEN + Offset32::RAW_BYTE_LEN;
776
}
777
778
impl Format<u16> for IndexSubtable1<'_> {
779
    const FORMAT: u16 = 1;
780
}
781
782
impl<'a> MinByteRange<'a> for IndexSubtable1<'a> {
783
0
    fn min_byte_range(&self) -> Range<usize> {
784
0
        0..self.sbit_offsets_byte_range().end
785
0
    }
786
0
    fn min_table_bytes(&self) -> &'a [u8] {
787
0
        let range = self.min_byte_range();
788
0
        self.offset_data().as_bytes().get(range).unwrap_or_default()
789
0
    }
790
}
791
792
impl ReadArgs for IndexSubtable1<'_> {
793
    type Args = (GlyphId16, GlyphId16);
794
}
795
796
impl<'a> FontRead<'a> for IndexSubtable1<'a> {
797
0
    fn read_with_args(data: FontData<'a>, args: (GlyphId16, GlyphId16)) -> Result<Self, ReadError> {
798
0
        let (last_glyph_index, first_glyph_index) = args;
799
800
        #[allow(clippy::absurd_extreme_comparisons)]
801
0
        if data.len() < Self::MIN_SIZE {
802
0
            return Err(ReadError::OutOfBounds);
803
0
        }
804
0
        let Some(data) = TableDataRef::new(data.as_bytes()) else {
805
0
            return Err(ReadError::OutOfBounds);
806
        };
807
0
        Ok(Self {
808
0
            data,
809
0
            last_glyph_index,
810
0
            first_glyph_index,
811
0
        })
812
0
    }
813
}
814
815
impl<'a> IndexSubtable1<'a> {
816
    /// A constructor that requires additional arguments.
817
    ///
818
    /// This type requires some external state in order to be
819
    /// parsed.
820
0
    pub fn read(
821
0
        data: FontData<'a>,
822
0
        last_glyph_index: GlyphId16,
823
0
        first_glyph_index: GlyphId16,
824
0
    ) -> Result<Self, ReadError> {
825
0
        let args = (last_glyph_index, first_glyph_index);
826
0
        Self::read_with_args(data, args)
827
0
    }
828
}
829
830
/// The fields at the start of the table, which are read directly.
831
#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
832
#[repr(C, packed)]
833
#[doc(hidden)]
834
pub struct IndexSubtable1FixedHeader {
835
    pub index_format: BigEndian<u16>,
836
    pub image_format: BigEndian<u16>,
837
    pub image_data_offset: BigEndian<u32>,
838
}
839
840
#[allow(clippy::absurd_extreme_comparisons)]
841
const _: () = assert!(std::mem::size_of::<IndexSubtable1FixedHeader>() <= IndexSubtable1::MIN_SIZE);
842
843
/// [IndexSubTable1](https://learn.microsoft.com/en-us/typography/opentype/spec/eblc#indexsubtable1-variable-metrics-glyphs-with-4-byte-offsets): variable-metrics glyphs with 4-byte offsets.
844
#[derive(Clone)]
845
pub struct IndexSubtable1<'a> {
846
    data: TableDataRef<'a, IndexSubtable1FixedHeader>,
847
    last_glyph_index: GlyphId16,
848
    first_glyph_index: GlyphId16,
849
}
850
851
#[allow(clippy::needless_lifetimes)]
852
impl<'a> IndexSubtable1<'a> {
853
    pub const MIN_SIZE: usize = (u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN + u32::RAW_BYTE_LEN);
854
    basic_table_impls!(impl_the_methods);
855
856
    /// Format of this IndexSubTable.
857
0
    pub fn index_format(&self) -> u16 {
858
0
        self.data.header().index_format.get()
859
0
    }
860
861
    /// Format of EBDT image data.
862
0
    pub fn image_format(&self) -> u16 {
863
0
        self.data.header().image_format.get()
864
0
    }
865
866
    /// Offset to image data in EBDT table.
867
0
    pub fn image_data_offset(&self) -> u32 {
868
0
        self.data.header().image_data_offset.get()
869
0
    }
870
871
0
    pub fn sbit_offsets(&self) -> &'a [BigEndian<u32>] {
872
0
        let range = self.sbit_offsets_byte_range();
873
0
        self.offset_data()
874
0
            .read_array(range)
875
0
            .ok()
876
0
            .unwrap_or_default()
877
0
    }
878
879
0
    pub(crate) fn last_glyph_index(&self) -> GlyphId16 {
880
0
        self.last_glyph_index
881
0
    }
882
883
0
    pub(crate) fn first_glyph_index(&self) -> GlyphId16 {
884
0
        self.first_glyph_index
885
0
    }
886
887
0
    pub fn index_format_byte_range(&self) -> Range<usize> {
888
0
        let start = 0usize;
889
0
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
890
0
        start..end
891
0
    }
892
893
0
    pub fn image_format_byte_range(&self) -> Range<usize> {
894
0
        let start = self.index_format_byte_range().end;
895
0
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
896
0
        start..end
897
0
    }
898
899
0
    pub fn image_data_offset_byte_range(&self) -> Range<usize> {
900
0
        let start = self.image_format_byte_range().end;
901
0
        let end = start.saturating_add(u32::RAW_BYTE_LEN);
902
0
        start..end
903
0
    }
904
905
0
    pub fn sbit_offsets_byte_range(&self) -> Range<usize> {
906
0
        let last_glyph_index = self.last_glyph_index();
907
0
        let first_glyph_index = self.first_glyph_index();
908
0
        let start = self.image_data_offset_byte_range().end;
909
0
        let end = start.saturating_add(
910
0
            (transforms::subtract_add_two(last_glyph_index, first_glyph_index))
911
0
                .saturating_mul(u32::RAW_BYTE_LEN),
912
        );
913
0
        start..end
914
0
    }
915
}
916
917
const _: () = assert!(FontData::default_data_long_enough(IndexSubtable1::MIN_SIZE));
918
919
impl Default for IndexSubtable1<'_> {
920
0
    fn default() -> Self {
921
        Self {
922
0
            data: TableDataRef::new(FontData::default_format_1_u16_table_data().as_bytes())
923
0
                .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
924
0
            last_glyph_index: Default::default(),
925
0
            first_glyph_index: Default::default(),
926
        }
927
0
    }
928
}
929
930
impl Format<u16> for IndexSubtable2<'_> {
931
    const FORMAT: u16 = 2;
932
}
933
934
impl<'a> MinByteRange<'a> for IndexSubtable2<'a> {
935
0
    fn min_byte_range(&self) -> Range<usize> {
936
0
        0..self.big_metrics_byte_range().end
937
0
    }
938
0
    fn min_table_bytes(&self) -> &'a [u8] {
939
0
        let range = self.min_byte_range();
940
0
        self.offset_data().as_bytes().get(range).unwrap_or_default()
941
0
    }
942
}
943
944
impl ReadArgs for IndexSubtable2<'_> {
945
    type Args = ();
946
}
947
948
impl<'a> FontRead<'a> for IndexSubtable2<'a> {
949
0
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
950
        #[allow(clippy::absurd_extreme_comparisons)]
951
0
        if data.len() < Self::MIN_SIZE {
952
0
            return Err(ReadError::OutOfBounds);
953
0
        }
954
0
        let Some(data) = TableDataRef::new(data.as_bytes()) else {
955
0
            return Err(ReadError::OutOfBounds);
956
        };
957
0
        Ok(Self { data })
958
0
    }
959
}
960
961
/// The fields at the start of the table, which are read directly.
962
#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
963
#[repr(C, packed)]
964
#[doc(hidden)]
965
pub struct IndexSubtable2FixedHeader {
966
    pub index_format: BigEndian<u16>,
967
    pub image_format: BigEndian<u16>,
968
    pub image_data_offset: BigEndian<u32>,
969
    pub image_size: BigEndian<u32>,
970
    pub big_metrics: BigGlyphMetrics,
971
}
972
973
#[allow(clippy::absurd_extreme_comparisons)]
974
const _: () = assert!(std::mem::size_of::<IndexSubtable2FixedHeader>() <= IndexSubtable2::MIN_SIZE);
975
976
/// [IndexSubTable2](https://learn.microsoft.com/en-us/typography/opentype/spec/eblc#indexsubtable2-all-glyphs-have-identical-metrics): all glyphs have identical metrics.
977
#[derive(Clone)]
978
pub struct IndexSubtable2<'a> {
979
    data: TableDataRef<'a, IndexSubtable2FixedHeader>,
980
}
981
982
#[allow(clippy::needless_lifetimes)]
983
impl<'a> IndexSubtable2<'a> {
984
    pub const MIN_SIZE: usize = (u16::RAW_BYTE_LEN
985
        + u16::RAW_BYTE_LEN
986
        + u32::RAW_BYTE_LEN
987
        + u32::RAW_BYTE_LEN
988
        + BigGlyphMetrics::RAW_BYTE_LEN);
989
    basic_table_impls!(impl_the_methods);
990
991
    /// Format of this IndexSubTable.
992
0
    pub fn index_format(&self) -> u16 {
993
0
        self.data.header().index_format.get()
994
0
    }
995
996
    /// Format of EBDT image data.
997
0
    pub fn image_format(&self) -> u16 {
998
0
        self.data.header().image_format.get()
999
0
    }
1000
1001
    /// Offset to image data in EBDT table.
1002
0
    pub fn image_data_offset(&self) -> u32 {
1003
0
        self.data.header().image_data_offset.get()
1004
0
    }
1005
1006
    /// All the glyphs are of the same size.
1007
0
    pub fn image_size(&self) -> u32 {
1008
0
        self.data.header().image_size.get()
1009
0
    }
1010
1011
    /// All glyphs have the same metrics; glyph data may be compressed, byte-aligned, or bit-aligned.
1012
0
    pub fn big_metrics(&self) -> &'a BigGlyphMetrics {
1013
0
        &self.data.header().big_metrics
1014
0
    }
1015
1016
0
    pub fn index_format_byte_range(&self) -> Range<usize> {
1017
0
        let start = 0usize;
1018
0
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
1019
0
        start..end
1020
0
    }
1021
1022
0
    pub fn image_format_byte_range(&self) -> Range<usize> {
1023
0
        let start = self.index_format_byte_range().end;
1024
0
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
1025
0
        start..end
1026
0
    }
1027
1028
0
    pub fn image_data_offset_byte_range(&self) -> Range<usize> {
1029
0
        let start = self.image_format_byte_range().end;
1030
0
        let end = start.saturating_add(u32::RAW_BYTE_LEN);
1031
0
        start..end
1032
0
    }
1033
1034
0
    pub fn image_size_byte_range(&self) -> Range<usize> {
1035
0
        let start = self.image_data_offset_byte_range().end;
1036
0
        let end = start.saturating_add(u32::RAW_BYTE_LEN);
1037
0
        start..end
1038
0
    }
1039
1040
0
    pub fn big_metrics_byte_range(&self) -> Range<usize> {
1041
0
        let start = self.image_size_byte_range().end;
1042
0
        let end = start.saturating_add(BigGlyphMetrics::RAW_BYTE_LEN);
1043
0
        start..end
1044
0
    }
1045
}
1046
1047
impl Format<u16> for IndexSubtable3<'_> {
1048
    const FORMAT: u16 = 3;
1049
}
1050
1051
impl<'a> MinByteRange<'a> for IndexSubtable3<'a> {
1052
0
    fn min_byte_range(&self) -> Range<usize> {
1053
0
        0..self.sbit_offsets_byte_range().end
1054
0
    }
1055
0
    fn min_table_bytes(&self) -> &'a [u8] {
1056
0
        let range = self.min_byte_range();
1057
0
        self.offset_data().as_bytes().get(range).unwrap_or_default()
1058
0
    }
1059
}
1060
1061
impl ReadArgs for IndexSubtable3<'_> {
1062
    type Args = (GlyphId16, GlyphId16);
1063
}
1064
1065
impl<'a> FontRead<'a> for IndexSubtable3<'a> {
1066
0
    fn read_with_args(data: FontData<'a>, args: (GlyphId16, GlyphId16)) -> Result<Self, ReadError> {
1067
0
        let (last_glyph_index, first_glyph_index) = args;
1068
1069
        #[allow(clippy::absurd_extreme_comparisons)]
1070
0
        if data.len() < Self::MIN_SIZE {
1071
0
            return Err(ReadError::OutOfBounds);
1072
0
        }
1073
0
        let Some(data) = TableDataRef::new(data.as_bytes()) else {
1074
0
            return Err(ReadError::OutOfBounds);
1075
        };
1076
0
        Ok(Self {
1077
0
            data,
1078
0
            last_glyph_index,
1079
0
            first_glyph_index,
1080
0
        })
1081
0
    }
1082
}
1083
1084
impl<'a> IndexSubtable3<'a> {
1085
    /// A constructor that requires additional arguments.
1086
    ///
1087
    /// This type requires some external state in order to be
1088
    /// parsed.
1089
0
    pub fn read(
1090
0
        data: FontData<'a>,
1091
0
        last_glyph_index: GlyphId16,
1092
0
        first_glyph_index: GlyphId16,
1093
0
    ) -> Result<Self, ReadError> {
1094
0
        let args = (last_glyph_index, first_glyph_index);
1095
0
        Self::read_with_args(data, args)
1096
0
    }
1097
}
1098
1099
/// The fields at the start of the table, which are read directly.
1100
#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
1101
#[repr(C, packed)]
1102
#[doc(hidden)]
1103
pub struct IndexSubtable3FixedHeader {
1104
    pub index_format: BigEndian<u16>,
1105
    pub image_format: BigEndian<u16>,
1106
    pub image_data_offset: BigEndian<u32>,
1107
}
1108
1109
#[allow(clippy::absurd_extreme_comparisons)]
1110
const _: () = assert!(std::mem::size_of::<IndexSubtable3FixedHeader>() <= IndexSubtable3::MIN_SIZE);
1111
1112
/// [IndexSubTable3](https://learn.microsoft.com/en-us/typography/opentype/spec/eblc#indexsubtable3-variable-metrics-glyphs-with-2-byte-offsets): variable-metrics glyphs with 2-byte offsets.
1113
#[derive(Clone)]
1114
pub struct IndexSubtable3<'a> {
1115
    data: TableDataRef<'a, IndexSubtable3FixedHeader>,
1116
    last_glyph_index: GlyphId16,
1117
    first_glyph_index: GlyphId16,
1118
}
1119
1120
#[allow(clippy::needless_lifetimes)]
1121
impl<'a> IndexSubtable3<'a> {
1122
    pub const MIN_SIZE: usize = (u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN + u32::RAW_BYTE_LEN);
1123
    basic_table_impls!(impl_the_methods);
1124
1125
    /// Format of this IndexSubTable.
1126
0
    pub fn index_format(&self) -> u16 {
1127
0
        self.data.header().index_format.get()
1128
0
    }
1129
1130
    /// Format of EBDT image data.
1131
0
    pub fn image_format(&self) -> u16 {
1132
0
        self.data.header().image_format.get()
1133
0
    }
1134
1135
    /// Offset to image data in EBDT table.
1136
0
    pub fn image_data_offset(&self) -> u32 {
1137
0
        self.data.header().image_data_offset.get()
1138
0
    }
1139
1140
0
    pub fn sbit_offsets(&self) -> &'a [BigEndian<u16>] {
1141
0
        let range = self.sbit_offsets_byte_range();
1142
0
        self.offset_data()
1143
0
            .read_array(range)
1144
0
            .ok()
1145
0
            .unwrap_or_default()
1146
0
    }
1147
1148
0
    pub(crate) fn last_glyph_index(&self) -> GlyphId16 {
1149
0
        self.last_glyph_index
1150
0
    }
1151
1152
0
    pub(crate) fn first_glyph_index(&self) -> GlyphId16 {
1153
0
        self.first_glyph_index
1154
0
    }
1155
1156
0
    pub fn index_format_byte_range(&self) -> Range<usize> {
1157
0
        let start = 0usize;
1158
0
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
1159
0
        start..end
1160
0
    }
1161
1162
0
    pub fn image_format_byte_range(&self) -> Range<usize> {
1163
0
        let start = self.index_format_byte_range().end;
1164
0
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
1165
0
        start..end
1166
0
    }
1167
1168
0
    pub fn image_data_offset_byte_range(&self) -> Range<usize> {
1169
0
        let start = self.image_format_byte_range().end;
1170
0
        let end = start.saturating_add(u32::RAW_BYTE_LEN);
1171
0
        start..end
1172
0
    }
1173
1174
0
    pub fn sbit_offsets_byte_range(&self) -> Range<usize> {
1175
0
        let last_glyph_index = self.last_glyph_index();
1176
0
        let first_glyph_index = self.first_glyph_index();
1177
0
        let start = self.image_data_offset_byte_range().end;
1178
0
        let end = start.saturating_add(
1179
0
            (transforms::subtract_add_two(last_glyph_index, first_glyph_index))
1180
0
                .saturating_mul(u16::RAW_BYTE_LEN),
1181
        );
1182
0
        start..end
1183
0
    }
1184
}
1185
1186
impl Format<u16> for IndexSubtable4<'_> {
1187
    const FORMAT: u16 = 4;
1188
}
1189
1190
impl<'a> MinByteRange<'a> for IndexSubtable4<'a> {
1191
0
    fn min_byte_range(&self) -> Range<usize> {
1192
0
        0..self.glyph_array_byte_range().end
1193
0
    }
1194
0
    fn min_table_bytes(&self) -> &'a [u8] {
1195
0
        let range = self.min_byte_range();
1196
0
        self.offset_data().as_bytes().get(range).unwrap_or_default()
1197
0
    }
1198
}
1199
1200
impl ReadArgs for IndexSubtable4<'_> {
1201
    type Args = ();
1202
}
1203
1204
impl<'a> FontRead<'a> for IndexSubtable4<'a> {
1205
0
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
1206
        #[allow(clippy::absurd_extreme_comparisons)]
1207
0
        if data.len() < Self::MIN_SIZE {
1208
0
            return Err(ReadError::OutOfBounds);
1209
0
        }
1210
0
        let Some(data) = TableDataRef::new(data.as_bytes()) else {
1211
0
            return Err(ReadError::OutOfBounds);
1212
        };
1213
0
        Ok(Self { data })
1214
0
    }
1215
}
1216
1217
/// The fields at the start of the table, which are read directly.
1218
#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
1219
#[repr(C, packed)]
1220
#[doc(hidden)]
1221
pub struct IndexSubtable4FixedHeader {
1222
    pub index_format: BigEndian<u16>,
1223
    pub image_format: BigEndian<u16>,
1224
    pub image_data_offset: BigEndian<u32>,
1225
    pub num_glyphs: BigEndian<u32>,
1226
}
1227
1228
#[allow(clippy::absurd_extreme_comparisons)]
1229
const _: () = assert!(std::mem::size_of::<IndexSubtable4FixedHeader>() <= IndexSubtable4::MIN_SIZE);
1230
1231
/// [IndexSubTable4](https://learn.microsoft.com/en-us/typography/opentype/spec/eblc#indexsubtable3-variable-metrics-glyphs-with-2-byte-offsets): variable-metrics glyphs with sparse glyph codes.
1232
#[derive(Clone)]
1233
pub struct IndexSubtable4<'a> {
1234
    data: TableDataRef<'a, IndexSubtable4FixedHeader>,
1235
}
1236
1237
#[allow(clippy::needless_lifetimes)]
1238
impl<'a> IndexSubtable4<'a> {
1239
    pub const MIN_SIZE: usize =
1240
        (u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN + u32::RAW_BYTE_LEN + u32::RAW_BYTE_LEN);
1241
    basic_table_impls!(impl_the_methods);
1242
1243
    /// Format of this IndexSubTable.
1244
0
    pub fn index_format(&self) -> u16 {
1245
0
        self.data.header().index_format.get()
1246
0
    }
1247
1248
    /// Format of EBDT image data.
1249
0
    pub fn image_format(&self) -> u16 {
1250
0
        self.data.header().image_format.get()
1251
0
    }
1252
1253
    /// Offset to image data in EBDT table.
1254
0
    pub fn image_data_offset(&self) -> u32 {
1255
0
        self.data.header().image_data_offset.get()
1256
0
    }
1257
1258
    /// Array length.
1259
0
    pub fn num_glyphs(&self) -> u32 {
1260
0
        self.data.header().num_glyphs.get()
1261
0
    }
1262
1263
    /// One per glyph.
1264
0
    pub fn glyph_array(&self) -> &'a [GlyphIdOffsetPair] {
1265
0
        let range = self.glyph_array_byte_range();
1266
0
        self.offset_data()
1267
0
            .read_array(range)
1268
0
            .ok()
1269
0
            .unwrap_or_default()
1270
0
    }
1271
1272
0
    pub fn index_format_byte_range(&self) -> Range<usize> {
1273
0
        let start = 0usize;
1274
0
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
1275
0
        start..end
1276
0
    }
1277
1278
0
    pub fn image_format_byte_range(&self) -> Range<usize> {
1279
0
        let start = self.index_format_byte_range().end;
1280
0
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
1281
0
        start..end
1282
0
    }
1283
1284
0
    pub fn image_data_offset_byte_range(&self) -> Range<usize> {
1285
0
        let start = self.image_format_byte_range().end;
1286
0
        let end = start.saturating_add(u32::RAW_BYTE_LEN);
1287
0
        start..end
1288
0
    }
1289
1290
0
    pub fn num_glyphs_byte_range(&self) -> Range<usize> {
1291
0
        let start = self.image_data_offset_byte_range().end;
1292
0
        let end = start.saturating_add(u32::RAW_BYTE_LEN);
1293
0
        start..end
1294
0
    }
1295
1296
0
    pub fn glyph_array_byte_range(&self) -> Range<usize> {
1297
0
        let num_glyphs = self.num_glyphs();
1298
0
        let start = self.num_glyphs_byte_range().end;
1299
0
        let end = start.saturating_add(
1300
0
            (transforms::add(num_glyphs, 1_usize)).saturating_mul(GlyphIdOffsetPair::RAW_BYTE_LEN),
1301
        );
1302
0
        start..end
1303
0
    }
1304
}
1305
1306
/// [GlyphIdOffsetPair](https://learn.microsoft.com/en-us/typography/opentype/spec/eblc#glyphidoffsetpair-record) record.
1307
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, bytemuck :: AnyBitPattern)]
1308
#[repr(C)]
1309
#[repr(packed)]
1310
pub struct GlyphIdOffsetPair {
1311
    /// Glyph ID of glyph present.
1312
    pub glyph_id: BigEndian<GlyphId16>,
1313
    /// Location in EBDT.
1314
    pub sbit_offset: BigEndian<u16>,
1315
}
1316
1317
impl GlyphIdOffsetPair {
1318
    /// Glyph ID of glyph present.
1319
0
    pub fn glyph_id(&self) -> GlyphId16 {
1320
0
        self.glyph_id.get()
1321
0
    }
1322
1323
    /// Location in EBDT.
1324
0
    pub fn sbit_offset(&self) -> u16 {
1325
0
        self.sbit_offset.get()
1326
0
    }
1327
}
1328
1329
impl FixedSize for GlyphIdOffsetPair {
1330
    const RAW_BYTE_LEN: usize = GlyphId16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN;
1331
}
1332
1333
impl Format<u16> for IndexSubtable5<'_> {
1334
    const FORMAT: u16 = 5;
1335
}
1336
1337
impl<'a> MinByteRange<'a> for IndexSubtable5<'a> {
1338
0
    fn min_byte_range(&self) -> Range<usize> {
1339
0
        0..self.glyph_array_byte_range().end
1340
0
    }
1341
0
    fn min_table_bytes(&self) -> &'a [u8] {
1342
0
        let range = self.min_byte_range();
1343
0
        self.offset_data().as_bytes().get(range).unwrap_or_default()
1344
0
    }
1345
}
1346
1347
impl ReadArgs for IndexSubtable5<'_> {
1348
    type Args = ();
1349
}
1350
1351
impl<'a> FontRead<'a> for IndexSubtable5<'a> {
1352
0
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
1353
        #[allow(clippy::absurd_extreme_comparisons)]
1354
0
        if data.len() < Self::MIN_SIZE {
1355
0
            return Err(ReadError::OutOfBounds);
1356
0
        }
1357
0
        let Some(data) = TableDataRef::new(data.as_bytes()) else {
1358
0
            return Err(ReadError::OutOfBounds);
1359
        };
1360
0
        Ok(Self { data })
1361
0
    }
1362
}
1363
1364
/// The fields at the start of the table, which are read directly.
1365
#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
1366
#[repr(C, packed)]
1367
#[doc(hidden)]
1368
pub struct IndexSubtable5FixedHeader {
1369
    pub index_format: BigEndian<u16>,
1370
    pub image_format: BigEndian<u16>,
1371
    pub image_data_offset: BigEndian<u32>,
1372
    pub image_size: BigEndian<u32>,
1373
    pub big_metrics: BigGlyphMetrics,
1374
    pub num_glyphs: BigEndian<u32>,
1375
}
1376
1377
#[allow(clippy::absurd_extreme_comparisons)]
1378
const _: () = assert!(std::mem::size_of::<IndexSubtable5FixedHeader>() <= IndexSubtable5::MIN_SIZE);
1379
1380
/// [IndexSubTable5](https://learn.microsoft.com/en-us/typography/opentype/spec/eblc#indexsubtable5-constant-metrics-glyphs-with-sparse-glyph-codes): constant-metrics glyphs with sparse glyph codes
1381
#[derive(Clone)]
1382
pub struct IndexSubtable5<'a> {
1383
    data: TableDataRef<'a, IndexSubtable5FixedHeader>,
1384
}
1385
1386
#[allow(clippy::needless_lifetimes)]
1387
impl<'a> IndexSubtable5<'a> {
1388
    pub const MIN_SIZE: usize = (u16::RAW_BYTE_LEN
1389
        + u16::RAW_BYTE_LEN
1390
        + u32::RAW_BYTE_LEN
1391
        + u32::RAW_BYTE_LEN
1392
        + BigGlyphMetrics::RAW_BYTE_LEN
1393
        + u32::RAW_BYTE_LEN);
1394
    basic_table_impls!(impl_the_methods);
1395
1396
    /// Format of this IndexSubTable.
1397
0
    pub fn index_format(&self) -> u16 {
1398
0
        self.data.header().index_format.get()
1399
0
    }
1400
1401
    /// Format of EBDT image data.
1402
0
    pub fn image_format(&self) -> u16 {
1403
0
        self.data.header().image_format.get()
1404
0
    }
1405
1406
    /// Offset to image data in EBDT table.
1407
0
    pub fn image_data_offset(&self) -> u32 {
1408
0
        self.data.header().image_data_offset.get()
1409
0
    }
1410
1411
    /// All glyphs have the same data size.
1412
0
    pub fn image_size(&self) -> u32 {
1413
0
        self.data.header().image_size.get()
1414
0
    }
1415
1416
    /// All glyphs have the same metrics.
1417
0
    pub fn big_metrics(&self) -> &'a BigGlyphMetrics {
1418
0
        &self.data.header().big_metrics
1419
0
    }
1420
1421
    /// Array length.
1422
0
    pub fn num_glyphs(&self) -> u32 {
1423
0
        self.data.header().num_glyphs.get()
1424
0
    }
1425
1426
    /// One per glyph, sorted by glyhph ID.
1427
0
    pub fn glyph_array(&self) -> &'a [BigEndian<GlyphId16>] {
1428
0
        let range = self.glyph_array_byte_range();
1429
0
        self.offset_data()
1430
0
            .read_array(range)
1431
0
            .ok()
1432
0
            .unwrap_or_default()
1433
0
    }
1434
1435
0
    pub fn index_format_byte_range(&self) -> Range<usize> {
1436
0
        let start = 0usize;
1437
0
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
1438
0
        start..end
1439
0
    }
1440
1441
0
    pub fn image_format_byte_range(&self) -> Range<usize> {
1442
0
        let start = self.index_format_byte_range().end;
1443
0
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
1444
0
        start..end
1445
0
    }
1446
1447
0
    pub fn image_data_offset_byte_range(&self) -> Range<usize> {
1448
0
        let start = self.image_format_byte_range().end;
1449
0
        let end = start.saturating_add(u32::RAW_BYTE_LEN);
1450
0
        start..end
1451
0
    }
1452
1453
0
    pub fn image_size_byte_range(&self) -> Range<usize> {
1454
0
        let start = self.image_data_offset_byte_range().end;
1455
0
        let end = start.saturating_add(u32::RAW_BYTE_LEN);
1456
0
        start..end
1457
0
    }
1458
1459
0
    pub fn big_metrics_byte_range(&self) -> Range<usize> {
1460
0
        let start = self.image_size_byte_range().end;
1461
0
        let end = start.saturating_add(BigGlyphMetrics::RAW_BYTE_LEN);
1462
0
        start..end
1463
0
    }
1464
1465
0
    pub fn num_glyphs_byte_range(&self) -> Range<usize> {
1466
0
        let start = self.big_metrics_byte_range().end;
1467
0
        let end = start.saturating_add(u32::RAW_BYTE_LEN);
1468
0
        start..end
1469
0
    }
1470
1471
0
    pub fn glyph_array_byte_range(&self) -> Range<usize> {
1472
0
        let num_glyphs = self.num_glyphs();
1473
0
        let start = self.num_glyphs_byte_range().end;
1474
0
        let end = start.saturating_add(
1475
0
            (transforms::to_usize(num_glyphs)).saturating_mul(GlyphId16::RAW_BYTE_LEN),
1476
        );
1477
0
        start..end
1478
0
    }
1479
}
1480
1481
/// [EbdtComponent](https://learn.microsoft.com/en-us/typography/opentype/spec/ebdt#ebdtcomponent-record) record.
1482
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, bytemuck :: AnyBitPattern)]
1483
#[repr(C)]
1484
#[repr(packed)]
1485
pub struct BdtComponent {
1486
    /// Component glyph ID.
1487
    pub glyph_id: BigEndian<GlyphId16>,
1488
    /// Position of component left.
1489
    pub x_offset: BigEndian<i8>,
1490
    /// Position of component top.
1491
    pub y_offset: BigEndian<i8>,
1492
}
1493
1494
impl BdtComponent {
1495
    /// Component glyph ID.
1496
0
    pub fn glyph_id(&self) -> GlyphId16 {
1497
0
        self.glyph_id.get()
1498
0
    }
1499
1500
    /// Position of component left.
1501
0
    pub fn x_offset(&self) -> i8 {
1502
0
        self.x_offset.get()
1503
0
    }
1504
1505
    /// Position of component top.
1506
0
    pub fn y_offset(&self) -> i8 {
1507
0
        self.y_offset.get()
1508
0
    }
1509
}
1510
1511
impl FixedSize for BdtComponent {
1512
    const RAW_BYTE_LEN: usize = GlyphId16::RAW_BYTE_LEN + i8::RAW_BYTE_LEN + i8::RAW_BYTE_LEN;
1513
}