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_cmap.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 Cmap<'a> {
9
0
    fn min_byte_range(&self) -> Range<usize> {
10
0
        0..self.encoding_records_byte_range().end
11
0
    }
12
0
    fn min_table_bytes(&self) -> &'a [u8] {
13
0
        let range = self.min_byte_range();
14
0
        self.offset_data().as_bytes().get(range).unwrap_or_default()
15
0
    }
16
}
17
18
impl TopLevelTable for Cmap<'_> {
19
    /// `cmap`
20
    const TAG: Tag = Tag::new(b"cmap");
21
}
22
23
impl ReadArgs for Cmap<'_> {
24
    type Args = ();
25
}
26
27
impl<'a> FontRead<'a> for Cmap<'a> {
28
4.42k
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
29
        #[allow(clippy::absurd_extreme_comparisons)]
30
4.42k
        if data.len() < Self::MIN_SIZE {
31
3
            return Err(ReadError::OutOfBounds);
32
4.42k
        }
33
4.42k
        let Some(data) = TableDataRef::new(data.as_bytes()) else {
34
0
            return Err(ReadError::OutOfBounds);
35
        };
36
4.42k
        Ok(Self { data })
37
4.42k
    }
38
}
39
40
/// The fields at the start of the table, which are read directly.
41
#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
42
#[repr(C, packed)]
43
#[doc(hidden)]
44
pub struct CmapFixedHeader {
45
    pub version: BigEndian<u16>,
46
    pub num_tables: BigEndian<u16>,
47
}
48
49
#[allow(clippy::absurd_extreme_comparisons)]
50
const _: () = assert!(std::mem::size_of::<CmapFixedHeader>() <= Cmap::MIN_SIZE);
51
52
/// [cmap](https://docs.microsoft.com/en-us/typography/opentype/spec/cmap#overview)
53
#[derive(Clone)]
54
pub struct Cmap<'a> {
55
    data: TableDataRef<'a, CmapFixedHeader>,
56
}
57
58
#[allow(clippy::needless_lifetimes)]
59
impl<'a> Cmap<'a> {
60
    pub const MIN_SIZE: usize = (u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN);
61
    basic_table_impls!(impl_the_methods);
62
63
    /// Table version number (0).
64
0
    pub fn version(&self) -> u16 {
65
0
        self.data.header().version.get()
66
0
    }
67
68
    /// Number of encoding tables that follow.
69
8.84k
    pub fn num_tables(&self) -> u16 {
70
8.84k
        self.data.header().num_tables.get()
71
8.84k
    }
72
73
8.84k
    pub fn encoding_records(&self) -> &'a [EncodingRecord] {
74
8.84k
        let range = self.encoding_records_byte_range();
75
8.84k
        self.offset_data()
76
8.84k
            .read_array(range)
77
8.84k
            .ok()
78
8.84k
            .unwrap_or_default()
79
8.84k
    }
80
81
8.84k
    pub fn version_byte_range(&self) -> Range<usize> {
82
8.84k
        let start = 0usize;
83
8.84k
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
84
8.84k
        start..end
85
8.84k
    }
86
87
8.84k
    pub fn num_tables_byte_range(&self) -> Range<usize> {
88
8.84k
        let start = self.version_byte_range().end;
89
8.84k
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
90
8.84k
        start..end
91
8.84k
    }
92
93
8.84k
    pub fn encoding_records_byte_range(&self) -> Range<usize> {
94
8.84k
        let num_tables = self.num_tables();
95
8.84k
        let start = self.num_tables_byte_range().end;
96
8.84k
        let end = start.saturating_add(
97
8.84k
            (transforms::to_usize(num_tables)).saturating_mul(EncodingRecord::RAW_BYTE_LEN),
98
        );
99
8.84k
        start..end
100
8.84k
    }
101
}
102
103
const _: () = assert!(FontData::default_data_long_enough(Cmap::MIN_SIZE));
104
105
impl Default for Cmap<'_> {
106
0
    fn default() -> Self {
107
        Self {
108
0
            data: TableDataRef::new(FontData::default_table_data().as_bytes())
109
0
                .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
110
        }
111
0
    }
112
}
113
114
impl<'a> MinByteRange<'a> for Dmap<'a> {
115
0
    fn min_byte_range(&self) -> Range<usize> {
116
0
        0..self.encoding_records_byte_range().end
117
0
    }
118
0
    fn min_table_bytes(&self) -> &'a [u8] {
119
0
        let range = self.min_byte_range();
120
0
        self.offset_data().as_bytes().get(range).unwrap_or_default()
121
0
    }
122
}
123
124
impl TopLevelTable for Dmap<'_> {
125
    /// `DMAP`
126
    const TAG: Tag = Tag::new(b"DMAP");
127
}
128
129
impl ReadArgs for Dmap<'_> {
130
    type Args = ();
131
}
132
133
impl<'a> FontRead<'a> for Dmap<'a> {
134
330
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
135
        #[allow(clippy::absurd_extreme_comparisons)]
136
330
        if data.len() < Self::MIN_SIZE {
137
2
            return Err(ReadError::OutOfBounds);
138
328
        }
139
328
        let Some(data) = TableDataRef::new(data.as_bytes()) else {
140
0
            return Err(ReadError::OutOfBounds);
141
        };
142
328
        Ok(Self { data })
143
330
    }
144
}
145
146
/// The fields at the start of the table, which are read directly.
147
#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
148
#[repr(C, packed)]
149
#[doc(hidden)]
150
pub struct DmapFixedHeader {
151
    pub version: BigEndian<u16>,
152
    pub num_tables: BigEndian<u16>,
153
}
154
155
#[allow(clippy::absurd_extreme_comparisons)]
156
const _: () = assert!(std::mem::size_of::<DmapFixedHeader>() <= Dmap::MIN_SIZE);
157
158
/// Delta map table (ISO/IEC 14496-22:2026, 5.6.15).
159
#[derive(Clone)]
160
pub struct Dmap<'a> {
161
    data: TableDataRef<'a, DmapFixedHeader>,
162
}
163
164
#[allow(clippy::needless_lifetimes)]
165
impl<'a> Dmap<'a> {
166
    pub const MIN_SIZE: usize = (u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN);
167
    basic_table_impls!(impl_the_methods);
168
169
    /// Table version number (0).
170
0
    pub fn version(&self) -> u16 {
171
0
        self.data.header().version.get()
172
0
    }
173
174
    /// Number of encoding tables that follow.
175
0
    pub fn num_tables(&self) -> u16 {
176
0
        self.data.header().num_tables.get()
177
0
    }
178
179
0
    pub fn encoding_records(&self) -> &'a [EncodingRecord] {
180
0
        let range = self.encoding_records_byte_range();
181
0
        self.offset_data()
182
0
            .read_array(range)
183
0
            .ok()
184
0
            .unwrap_or_default()
185
0
    }
186
187
0
    pub fn version_byte_range(&self) -> Range<usize> {
188
0
        let start = 0usize;
189
0
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
190
0
        start..end
191
0
    }
192
193
0
    pub fn num_tables_byte_range(&self) -> Range<usize> {
194
0
        let start = self.version_byte_range().end;
195
0
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
196
0
        start..end
197
0
    }
198
199
0
    pub fn encoding_records_byte_range(&self) -> Range<usize> {
200
0
        let num_tables = self.num_tables();
201
0
        let start = self.num_tables_byte_range().end;
202
0
        let end = start.saturating_add(
203
0
            (transforms::to_usize(num_tables)).saturating_mul(EncodingRecord::RAW_BYTE_LEN),
204
        );
205
0
        start..end
206
0
    }
207
}
208
209
const _: () = assert!(FontData::default_data_long_enough(Dmap::MIN_SIZE));
210
211
impl Default for Dmap<'_> {
212
0
    fn default() -> Self {
213
        Self {
214
0
            data: TableDataRef::new(FontData::default_table_data().as_bytes())
215
0
                .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
216
        }
217
0
    }
218
}
219
220
/// [Encoding Record](https://docs.microsoft.com/en-us/typography/opentype/spec/cmap#encoding-records-and-encodings)
221
#[derive(Clone, Debug, Copy, bytemuck :: AnyBitPattern)]
222
#[repr(C)]
223
#[repr(packed)]
224
pub struct EncodingRecord {
225
    /// Platform ID.
226
    pub platform_id: BigEndian<PlatformId>,
227
    /// Platform-specific encoding ID.
228
    pub encoding_id: BigEndian<u16>,
229
    /// Byte offset from beginning of the [`Cmap`] or [`Dmap`] table to the
230
    /// subtable for this encoding.
231
    pub subtable_offset: BigEndian<Offset32>,
232
}
233
234
impl EncodingRecord {
235
    /// Platform ID.
236
809k
    pub fn platform_id(&self) -> PlatformId {
237
809k
        self.platform_id.get()
238
809k
    }
239
240
    /// Platform-specific encoding ID.
241
124k
    pub fn encoding_id(&self) -> u16 {
242
124k
        self.encoding_id.get()
243
124k
    }
244
245
    /// Byte offset from beginning of the [`Cmap`] or [`Dmap`] table to the
246
    /// subtable for this encoding.
247
7.50k
    pub fn subtable_offset(&self) -> Offset32 {
248
7.50k
        self.subtable_offset.get()
249
7.50k
    }
250
251
    /// Byte offset from beginning of the [`Cmap`] or [`Dmap`] table to the
252
    /// subtable for this encoding.
253
    ///
254
    /// The `data` argument should be retrieved from the parent table
255
    /// By calling its `offset_data` method.
256
7.50k
    pub fn subtable<'a>(&self, data: FontData<'a>) -> Result<CmapSubtable<'a>, ReadError> {
257
7.50k
        self.subtable_offset().resolve(data)
258
7.50k
    }
259
}
260
261
impl FixedSize for EncodingRecord {
262
    const RAW_BYTE_LEN: usize =
263
        PlatformId::RAW_BYTE_LEN + u16::RAW_BYTE_LEN + Offset32::RAW_BYTE_LEN;
264
}
265
266
/// <https://docs.microsoft.com/en-us/typography/opentype/spec/cmap#platform-ids>
267
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, PartialOrd, Ord)]
268
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
269
#[repr(u16)]
270
#[allow(clippy::manual_non_exhaustive)]
271
pub enum PlatformId {
272
    #[default]
273
    Unicode = 0,
274
    Macintosh = 1,
275
    ISO = 2,
276
    Windows = 3,
277
    Custom = 4,
278
    #[doc(hidden)]
279
    /// If font data is malformed we will map unknown values to this variant
280
    Unknown,
281
}
282
283
impl PlatformId {
284
    /// Create from a raw scalar.
285
    ///
286
    /// This will never fail; unknown values will be mapped to the `Unknown` variant
287
809k
    pub fn new(raw: u16) -> Self {
288
809k
        match raw {
289
193k
            0 => Self::Unicode,
290
14.3k
            1 => Self::Macintosh,
291
9.21k
            2 => Self::ISO,
292
5.94k
            3 => Self::Windows,
293
5.63k
            4 => Self::Custom,
294
581k
            _ => Self::Unknown,
295
        }
296
809k
    }
297
}
298
299
impl font_types::Scalar for PlatformId {
300
    type Raw = <u16 as font_types::Scalar>::Raw;
301
0
    fn to_raw(self) -> Self::Raw {
302
0
        (self as u16).to_raw()
303
0
    }
304
809k
    fn from_raw(raw: Self::Raw) -> Self {
305
809k
        let t = <u16>::from_raw(raw);
306
809k
        Self::new(t)
307
809k
    }
308
}
309
310
/// The different cmap subtable formats.
311
#[derive(Clone)]
312
pub enum CmapSubtable<'a> {
313
    Format0(Cmap0<'a>),
314
    Format2(Cmap2<'a>),
315
    Format4(Cmap4<'a>),
316
    Format6(Cmap6<'a>),
317
    Format8(Cmap8<'a>),
318
    Format10(Cmap10<'a>),
319
    Format12(Cmap12<'a>),
320
    Format13(Cmap13<'a>),
321
    Format14(Cmap14<'a>),
322
    Format15(Cmap15<'a>),
323
}
324
325
impl Default for CmapSubtable<'_> {
326
0
    fn default() -> Self {
327
0
        Self::Format0(Default::default())
328
0
    }
329
}
330
331
impl<'a> CmapSubtable<'a> {
332
    ///Return the `FontData` used to resolve offsets for this table.
333
0
    pub fn offset_data(&self) -> FontData<'a> {
334
0
        match self {
335
0
            Self::Format0(item) => item.offset_data(),
336
0
            Self::Format2(item) => item.offset_data(),
337
0
            Self::Format4(item) => item.offset_data(),
338
0
            Self::Format6(item) => item.offset_data(),
339
0
            Self::Format8(item) => item.offset_data(),
340
0
            Self::Format10(item) => item.offset_data(),
341
0
            Self::Format12(item) => item.offset_data(),
342
0
            Self::Format13(item) => item.offset_data(),
343
0
            Self::Format14(item) => item.offset_data(),
344
0
            Self::Format15(item) => item.offset_data(),
345
        }
346
0
    }
347
348
    /// Format number is set to 0.
349
0
    pub fn format(&self) -> u16 {
350
0
        match self {
351
0
            Self::Format0(item) => item.format(),
352
0
            Self::Format2(item) => item.format(),
353
0
            Self::Format4(item) => item.format(),
354
0
            Self::Format6(item) => item.format(),
355
0
            Self::Format8(item) => item.format(),
356
0
            Self::Format10(item) => item.format(),
357
0
            Self::Format12(item) => item.format(),
358
0
            Self::Format13(item) => item.format(),
359
0
            Self::Format14(item) => item.format(),
360
0
            Self::Format15(item) => item.format(),
361
        }
362
0
    }
363
}
364
365
impl ReadArgs for CmapSubtable<'_> {
366
    type Args = ();
367
}
368
369
impl<'a> FontRead<'a> for CmapSubtable<'a> {
370
    #[inline]
371
3.88k
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
372
3.88k
        let format: u16 = data.read_at(0usize)?;
373
3.86k
        match format {
374
251
            Cmap0::FORMAT => Ok(Self::Format0(FontRead::read(data)?)),
375
71
            Cmap2::FORMAT => Ok(Self::Format2(FontRead::read(data)?)),
376
535
            Cmap4::FORMAT => Ok(Self::Format4(FontRead::read(data)?)),
377
114
            Cmap6::FORMAT => Ok(Self::Format6(FontRead::read(data)?)),
378
0
            Cmap8::FORMAT => Ok(Self::Format8(FontRead::read(data)?)),
379
158
            Cmap10::FORMAT => Ok(Self::Format10(FontRead::read(data)?)),
380
1.14k
            Cmap12::FORMAT => Ok(Self::Format12(FontRead::read(data)?)),
381
914
            Cmap13::FORMAT => Ok(Self::Format13(FontRead::read(data)?)),
382
151
            Cmap14::FORMAT => Ok(Self::Format14(FontRead::read(data)?)),
383
53
            Cmap15::FORMAT => Ok(Self::Format15(FontRead::read(data)?)),
384
465
            other => Err(ReadError::InvalidFormat(other.into())),
385
        }
386
3.88k
    }
387
}
388
389
impl<'a> MinByteRange<'a> for CmapSubtable<'a> {
390
0
    fn min_byte_range(&self) -> Range<usize> {
391
0
        match self {
392
0
            Self::Format0(item) => item.min_byte_range(),
393
0
            Self::Format2(item) => item.min_byte_range(),
394
0
            Self::Format4(item) => item.min_byte_range(),
395
0
            Self::Format6(item) => item.min_byte_range(),
396
0
            Self::Format8(item) => item.min_byte_range(),
397
0
            Self::Format10(item) => item.min_byte_range(),
398
0
            Self::Format12(item) => item.min_byte_range(),
399
0
            Self::Format13(item) => item.min_byte_range(),
400
0
            Self::Format14(item) => item.min_byte_range(),
401
0
            Self::Format15(item) => item.min_byte_range(),
402
        }
403
0
    }
404
0
    fn min_table_bytes(&self) -> &'a [u8] {
405
0
        match self {
406
0
            Self::Format0(item) => item.min_table_bytes(),
407
0
            Self::Format2(item) => item.min_table_bytes(),
408
0
            Self::Format4(item) => item.min_table_bytes(),
409
0
            Self::Format6(item) => item.min_table_bytes(),
410
0
            Self::Format8(item) => item.min_table_bytes(),
411
0
            Self::Format10(item) => item.min_table_bytes(),
412
0
            Self::Format12(item) => item.min_table_bytes(),
413
0
            Self::Format13(item) => item.min_table_bytes(),
414
0
            Self::Format14(item) => item.min_table_bytes(),
415
0
            Self::Format15(item) => item.min_table_bytes(),
416
        }
417
0
    }
418
}
419
420
impl Format<u16> for Cmap0<'_> {
421
    const FORMAT: u16 = 0;
422
}
423
424
impl<'a> MinByteRange<'a> for Cmap0<'a> {
425
0
    fn min_byte_range(&self) -> Range<usize> {
426
0
        0..self.glyph_id_array_byte_range().end
427
0
    }
428
0
    fn min_table_bytes(&self) -> &'a [u8] {
429
0
        let range = self.min_byte_range();
430
0
        self.offset_data().as_bytes().get(range).unwrap_or_default()
431
0
    }
432
}
433
434
impl ReadArgs for Cmap0<'_> {
435
    type Args = ();
436
}
437
438
impl<'a> FontRead<'a> for Cmap0<'a> {
439
251
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
440
        #[allow(clippy::absurd_extreme_comparisons)]
441
251
        if data.len() < Self::MIN_SIZE {
442
99
            return Err(ReadError::OutOfBounds);
443
152
        }
444
152
        let Some(data) = TableDataRef::new(data.as_bytes()) else {
445
0
            return Err(ReadError::OutOfBounds);
446
        };
447
152
        Ok(Self { data })
448
251
    }
449
}
450
451
/// The fields at the start of the table, which are read directly.
452
#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
453
#[repr(C, packed)]
454
#[doc(hidden)]
455
pub struct Cmap0FixedHeader {
456
    pub format: BigEndian<u16>,
457
    pub length: BigEndian<u16>,
458
    pub language: BigEndian<u16>,
459
}
460
461
#[allow(clippy::absurd_extreme_comparisons)]
462
const _: () = assert!(std::mem::size_of::<Cmap0FixedHeader>() <= Cmap0::MIN_SIZE);
463
464
/// [cmap Format 0](https://docs.microsoft.com/en-us/typography/opentype/spec/cmap#format-0-byte-encoding-table): Byte encoding table
465
#[derive(Clone)]
466
pub struct Cmap0<'a> {
467
    data: TableDataRef<'a, Cmap0FixedHeader>,
468
}
469
470
#[allow(clippy::needless_lifetimes)]
471
impl<'a> Cmap0<'a> {
472
    pub const MIN_SIZE: usize =
473
        (u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN + u8::RAW_BYTE_LEN * 256_usize);
474
    basic_table_impls!(impl_the_methods);
475
476
    /// Format number is set to 0.
477
0
    pub fn format(&self) -> u16 {
478
0
        self.data.header().format.get()
479
0
    }
480
481
    /// This is the length in bytes of the subtable.
482
0
    pub fn length(&self) -> u16 {
483
0
        self.data.header().length.get()
484
0
    }
485
486
    /// For requirements on use of the language field, see “Use of
487
    /// the language field in 'cmap' subtables” in this document.
488
0
    pub fn language(&self) -> u16 {
489
0
        self.data.header().language.get()
490
0
    }
491
492
    /// An array that maps character codes to glyph index values.
493
492k
    pub fn glyph_id_array(&self) -> &'a [u8] {
494
492k
        let range = self.glyph_id_array_byte_range();
495
492k
        self.offset_data()
496
492k
            .read_array(range)
497
492k
            .ok()
498
492k
            .unwrap_or_default()
499
492k
    }
500
501
492k
    pub fn format_byte_range(&self) -> Range<usize> {
502
492k
        let start = 0usize;
503
492k
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
504
492k
        start..end
505
492k
    }
506
507
492k
    pub fn length_byte_range(&self) -> Range<usize> {
508
492k
        let start = self.format_byte_range().end;
509
492k
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
510
492k
        start..end
511
492k
    }
512
513
492k
    pub fn language_byte_range(&self) -> Range<usize> {
514
492k
        let start = self.length_byte_range().end;
515
492k
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
516
492k
        start..end
517
492k
    }
518
519
492k
    pub fn glyph_id_array_byte_range(&self) -> Range<usize> {
520
492k
        let start = self.language_byte_range().end;
521
492k
        let end = start.saturating_add((256_usize).saturating_mul(u8::RAW_BYTE_LEN));
522
492k
        start..end
523
492k
    }
524
}
525
526
const _: () = assert!(FontData::default_data_long_enough(Cmap0::MIN_SIZE));
527
528
impl Default for Cmap0<'_> {
529
0
    fn default() -> Self {
530
        Self {
531
0
            data: TableDataRef::new(FontData::default_table_data().as_bytes())
532
0
                .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
533
        }
534
0
    }
535
}
536
537
impl Format<u16> for Cmap2<'_> {
538
    const FORMAT: u16 = 2;
539
}
540
541
impl<'a> MinByteRange<'a> for Cmap2<'a> {
542
0
    fn min_byte_range(&self) -> Range<usize> {
543
0
        0..self.sub_header_keys_byte_range().end
544
0
    }
545
0
    fn min_table_bytes(&self) -> &'a [u8] {
546
0
        let range = self.min_byte_range();
547
0
        self.offset_data().as_bytes().get(range).unwrap_or_default()
548
0
    }
549
}
550
551
impl ReadArgs for Cmap2<'_> {
552
    type Args = ();
553
}
554
555
impl<'a> FontRead<'a> for Cmap2<'a> {
556
71
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
557
        #[allow(clippy::absurd_extreme_comparisons)]
558
71
        if data.len() < Self::MIN_SIZE {
559
62
            return Err(ReadError::OutOfBounds);
560
9
        }
561
9
        let Some(data) = TableDataRef::new(data.as_bytes()) else {
562
0
            return Err(ReadError::OutOfBounds);
563
        };
564
9
        Ok(Self { data })
565
71
    }
566
}
567
568
/// The fields at the start of the table, which are read directly.
569
#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
570
#[repr(C, packed)]
571
#[doc(hidden)]
572
pub struct Cmap2FixedHeader {
573
    pub format: BigEndian<u16>,
574
    pub length: BigEndian<u16>,
575
    pub language: BigEndian<u16>,
576
}
577
578
#[allow(clippy::absurd_extreme_comparisons)]
579
const _: () = assert!(std::mem::size_of::<Cmap2FixedHeader>() <= Cmap2::MIN_SIZE);
580
581
/// [cmap Format 2](https://docs.microsoft.com/en-us/typography/opentype/spec/cmap#format-2-high-byte-mapping-through-table): High-byte mapping through table
582
#[derive(Clone)]
583
pub struct Cmap2<'a> {
584
    data: TableDataRef<'a, Cmap2FixedHeader>,
585
}
586
587
#[allow(clippy::needless_lifetimes)]
588
impl<'a> Cmap2<'a> {
589
    pub const MIN_SIZE: usize =
590
        (u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN * 256_usize);
591
    basic_table_impls!(impl_the_methods);
592
593
    /// Format number is set to 2.
594
0
    pub fn format(&self) -> u16 {
595
0
        self.data.header().format.get()
596
0
    }
597
598
    /// This is the length in bytes of the subtable.
599
0
    pub fn length(&self) -> u16 {
600
0
        self.data.header().length.get()
601
0
    }
602
603
    /// For requirements on use of the language field, see “Use of
604
    /// the language field in 'cmap' subtables” in this document.
605
0
    pub fn language(&self) -> u16 {
606
0
        self.data.header().language.get()
607
0
    }
608
609
    /// Array that maps high bytes to subHeaders: value is subHeader
610
    /// index × 8.
611
0
    pub fn sub_header_keys(&self) -> &'a [BigEndian<u16>] {
612
0
        let range = self.sub_header_keys_byte_range();
613
0
        self.offset_data()
614
0
            .read_array(range)
615
0
            .ok()
616
0
            .unwrap_or_default()
617
0
    }
618
619
0
    pub fn format_byte_range(&self) -> Range<usize> {
620
0
        let start = 0usize;
621
0
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
622
0
        start..end
623
0
    }
624
625
0
    pub fn length_byte_range(&self) -> Range<usize> {
626
0
        let start = self.format_byte_range().end;
627
0
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
628
0
        start..end
629
0
    }
630
631
0
    pub fn language_byte_range(&self) -> Range<usize> {
632
0
        let start = self.length_byte_range().end;
633
0
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
634
0
        start..end
635
0
    }
636
637
0
    pub fn sub_header_keys_byte_range(&self) -> Range<usize> {
638
0
        let start = self.language_byte_range().end;
639
0
        let end = start.saturating_add((256_usize).saturating_mul(u16::RAW_BYTE_LEN));
640
0
        start..end
641
0
    }
642
}
643
644
/// Part of [Cmap2]
645
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, bytemuck :: AnyBitPattern)]
646
#[repr(C)]
647
#[repr(packed)]
648
pub struct SubHeader {
649
    /// First valid low byte for this SubHeader.
650
    pub first_code: BigEndian<u16>,
651
    /// Number of valid low bytes for this SubHeader.
652
    pub entry_count: BigEndian<u16>,
653
    /// See text below.
654
    pub id_delta: BigEndian<i16>,
655
    /// See text below.
656
    pub id_range_offset: BigEndian<u16>,
657
}
658
659
impl SubHeader {
660
    /// First valid low byte for this SubHeader.
661
0
    pub fn first_code(&self) -> u16 {
662
0
        self.first_code.get()
663
0
    }
664
665
    /// Number of valid low bytes for this SubHeader.
666
0
    pub fn entry_count(&self) -> u16 {
667
0
        self.entry_count.get()
668
0
    }
669
670
    /// See text below.
671
0
    pub fn id_delta(&self) -> i16 {
672
0
        self.id_delta.get()
673
0
    }
674
675
    /// See text below.
676
0
    pub fn id_range_offset(&self) -> u16 {
677
0
        self.id_range_offset.get()
678
0
    }
679
}
680
681
impl FixedSize for SubHeader {
682
    const RAW_BYTE_LEN: usize =
683
        u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN + i16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN;
684
}
685
686
impl Format<u16> for Cmap4<'_> {
687
    const FORMAT: u16 = 4;
688
}
689
690
impl<'a> MinByteRange<'a> for Cmap4<'a> {
691
0
    fn min_byte_range(&self) -> Range<usize> {
692
0
        0..self.glyph_id_array_byte_range().end
693
0
    }
694
0
    fn min_table_bytes(&self) -> &'a [u8] {
695
0
        let range = self.min_byte_range();
696
0
        self.offset_data().as_bytes().get(range).unwrap_or_default()
697
0
    }
698
}
699
700
impl ReadArgs for Cmap4<'_> {
701
    type Args = ();
702
}
703
704
impl<'a> FontRead<'a> for Cmap4<'a> {
705
535
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
706
        #[allow(clippy::absurd_extreme_comparisons)]
707
535
        if data.len() < Self::MIN_SIZE {
708
11
            return Err(ReadError::OutOfBounds);
709
524
        }
710
524
        let Some(data) = TableDataRef::new(data.as_bytes()) else {
711
0
            return Err(ReadError::OutOfBounds);
712
        };
713
524
        Ok(Self { data })
714
535
    }
715
}
716
717
/// The fields at the start of the table, which are read directly.
718
#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
719
#[repr(C, packed)]
720
#[doc(hidden)]
721
pub struct Cmap4FixedHeader {
722
    pub format: BigEndian<u16>,
723
    pub length: BigEndian<u16>,
724
    pub language: BigEndian<u16>,
725
    pub seg_count_x2: BigEndian<u16>,
726
    pub search_range: BigEndian<u16>,
727
    pub entry_selector: BigEndian<u16>,
728
    pub range_shift: BigEndian<u16>,
729
}
730
731
#[allow(clippy::absurd_extreme_comparisons)]
732
const _: () = assert!(std::mem::size_of::<Cmap4FixedHeader>() <= Cmap4::MIN_SIZE);
733
734
/// [cmap Format 4](https://docs.microsoft.com/en-us/typography/opentype/spec/cmap#format-4-segment-mapping-to-delta-values): Segment mapping to delta values
735
#[derive(Clone)]
736
pub struct Cmap4<'a> {
737
    data: TableDataRef<'a, Cmap4FixedHeader>,
738
}
739
740
#[allow(clippy::needless_lifetimes)]
741
impl<'a> Cmap4<'a> {
742
    pub const MIN_SIZE: usize = (u16::RAW_BYTE_LEN
743
        + u16::RAW_BYTE_LEN
744
        + u16::RAW_BYTE_LEN
745
        + u16::RAW_BYTE_LEN
746
        + u16::RAW_BYTE_LEN
747
        + u16::RAW_BYTE_LEN
748
        + u16::RAW_BYTE_LEN
749
        + u16::RAW_BYTE_LEN);
750
    basic_table_impls!(impl_the_methods);
751
752
    /// Format number is set to 4.
753
0
    pub fn format(&self) -> u16 {
754
0
        self.data.header().format.get()
755
0
    }
756
757
    /// This is the length in bytes of the subtable.
758
0
    pub fn length(&self) -> u16 {
759
0
        self.data.header().length.get()
760
0
    }
761
762
    /// For requirements on use of the language field, see “Use of
763
    /// the language field in 'cmap' subtables” in this document.
764
0
    pub fn language(&self) -> u16 {
765
0
        self.data.header().language.get()
766
0
    }
767
768
    /// 2 × segCount.
769
1.22M
    pub fn seg_count_x2(&self) -> u16 {
770
1.22M
        self.data.header().seg_count_x2.get()
771
1.22M
    }
772
773
    /// Maximum power of 2 less than or equal to segCount, times 2
774
    /// ((2**floor(log2(segCount))) * 2, where “**” is an
775
    /// exponentiation operator)
776
0
    pub fn search_range(&self) -> u16 {
777
0
        self.data.header().search_range.get()
778
0
    }
779
780
    /// Log2 of the maximum power of 2 less than or equal to numTables
781
    /// (log2(searchRange/2), which is equal to floor(log2(segCount)))
782
0
    pub fn entry_selector(&self) -> u16 {
783
0
        self.data.header().entry_selector.get()
784
0
    }
785
786
    /// segCount times 2, minus searchRange ((segCount * 2) -
787
    /// searchRange)
788
0
    pub fn range_shift(&self) -> u16 {
789
0
        self.data.header().range_shift.get()
790
0
    }
791
792
    /// End characterCode for each segment, last=0xFFFF.
793
223k
    pub fn end_code(&self) -> &'a [BigEndian<u16>] {
794
223k
        let range = self.end_code_byte_range();
795
223k
        self.offset_data()
796
223k
            .read_array(range)
797
223k
            .ok()
798
223k
            .unwrap_or_default()
799
223k
    }
800
801
    /// Start character code for each segment.
802
223k
    pub fn start_code(&self) -> &'a [BigEndian<u16>] {
803
223k
        let range = self.start_code_byte_range();
804
223k
        self.offset_data()
805
223k
            .read_array(range)
806
223k
            .ok()
807
223k
            .unwrap_or_default()
808
223k
    }
809
810
    /// Delta for all character codes in segment.
811
41.1k
    pub fn id_delta(&self) -> &'a [BigEndian<i16>] {
812
41.1k
        let range = self.id_delta_byte_range();
813
41.1k
        self.offset_data()
814
41.1k
            .read_array(range)
815
41.1k
            .ok()
816
41.1k
            .unwrap_or_default()
817
41.1k
    }
818
819
    /// Offsets into glyphIdArray or 0
820
41.1k
    pub fn id_range_offsets(&self) -> &'a [BigEndian<u16>] {
821
41.1k
        let range = self.id_range_offsets_byte_range();
822
41.1k
        self.offset_data()
823
41.1k
            .read_array(range)
824
41.1k
            .ok()
825
41.1k
            .unwrap_or_default()
826
41.1k
    }
827
828
    /// Glyph index array (arbitrary length)
829
10.9k
    pub fn glyph_id_array(&self) -> &'a [BigEndian<u16>] {
830
10.9k
        let range = self.glyph_id_array_byte_range();
831
10.9k
        self.offset_data()
832
10.9k
            .read_array(range)
833
10.9k
            .ok()
834
10.9k
            .unwrap_or_default()
835
10.9k
    }
836
837
539k
    pub fn format_byte_range(&self) -> Range<usize> {
838
539k
        let start = 0usize;
839
539k
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
840
539k
        start..end
841
539k
    }
842
843
539k
    pub fn length_byte_range(&self) -> Range<usize> {
844
539k
        let start = self.format_byte_range().end;
845
539k
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
846
539k
        start..end
847
539k
    }
848
849
539k
    pub fn language_byte_range(&self) -> Range<usize> {
850
539k
        let start = self.length_byte_range().end;
851
539k
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
852
539k
        start..end
853
539k
    }
854
855
539k
    pub fn seg_count_x2_byte_range(&self) -> Range<usize> {
856
539k
        let start = self.language_byte_range().end;
857
539k
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
858
539k
        start..end
859
539k
    }
860
861
539k
    pub fn search_range_byte_range(&self) -> Range<usize> {
862
539k
        let start = self.seg_count_x2_byte_range().end;
863
539k
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
864
539k
        start..end
865
539k
    }
866
867
539k
    pub fn entry_selector_byte_range(&self) -> Range<usize> {
868
539k
        let start = self.search_range_byte_range().end;
869
539k
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
870
539k
        start..end
871
539k
    }
872
873
539k
    pub fn range_shift_byte_range(&self) -> Range<usize> {
874
539k
        let start = self.entry_selector_byte_range().end;
875
539k
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
876
539k
        start..end
877
539k
    }
878
879
539k
    pub fn end_code_byte_range(&self) -> Range<usize> {
880
539k
        let seg_count_x2 = self.seg_count_x2();
881
539k
        let start = self.range_shift_byte_range().end;
882
539k
        let end = start
883
539k
            .saturating_add((transforms::half(seg_count_x2)).saturating_mul(u16::RAW_BYTE_LEN));
884
539k
        start..end
885
539k
    }
886
887
316k
    pub fn reserved_pad_byte_range(&self) -> Range<usize> {
888
316k
        let start = self.end_code_byte_range().end;
889
316k
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
890
316k
        start..end
891
316k
    }
892
893
316k
    pub fn start_code_byte_range(&self) -> Range<usize> {
894
316k
        let seg_count_x2 = self.seg_count_x2();
895
316k
        let start = self.reserved_pad_byte_range().end;
896
316k
        let end = start
897
316k
            .saturating_add((transforms::half(seg_count_x2)).saturating_mul(u16::RAW_BYTE_LEN));
898
316k
        start..end
899
316k
    }
900
901
93.1k
    pub fn id_delta_byte_range(&self) -> Range<usize> {
902
93.1k
        let seg_count_x2 = self.seg_count_x2();
903
93.1k
        let start = self.start_code_byte_range().end;
904
93.1k
        let end = start
905
93.1k
            .saturating_add((transforms::half(seg_count_x2)).saturating_mul(i16::RAW_BYTE_LEN));
906
93.1k
        start..end
907
93.1k
    }
908
909
52.0k
    pub fn id_range_offsets_byte_range(&self) -> Range<usize> {
910
52.0k
        let seg_count_x2 = self.seg_count_x2();
911
52.0k
        let start = self.id_delta_byte_range().end;
912
52.0k
        let end = start
913
52.0k
            .saturating_add((transforms::half(seg_count_x2)).saturating_mul(u16::RAW_BYTE_LEN));
914
52.0k
        start..end
915
52.0k
    }
916
917
10.9k
    pub fn glyph_id_array_byte_range(&self) -> Range<usize> {
918
10.9k
        let start = self.id_range_offsets_byte_range().end;
919
10.9k
        let end = start.saturating_add(
920
10.9k
            self.offset_data().len().saturating_sub(start) / u16::RAW_BYTE_LEN * u16::RAW_BYTE_LEN,
921
        );
922
10.9k
        start..end
923
10.9k
    }
924
}
925
926
impl Format<u16> for Cmap6<'_> {
927
    const FORMAT: u16 = 6;
928
}
929
930
impl<'a> MinByteRange<'a> for Cmap6<'a> {
931
0
    fn min_byte_range(&self) -> Range<usize> {
932
0
        0..self.glyph_id_array_byte_range().end
933
0
    }
934
0
    fn min_table_bytes(&self) -> &'a [u8] {
935
0
        let range = self.min_byte_range();
936
0
        self.offset_data().as_bytes().get(range).unwrap_or_default()
937
0
    }
938
}
939
940
impl ReadArgs for Cmap6<'_> {
941
    type Args = ();
942
}
943
944
impl<'a> FontRead<'a> for Cmap6<'a> {
945
114
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
946
        #[allow(clippy::absurd_extreme_comparisons)]
947
114
        if data.len() < Self::MIN_SIZE {
948
10
            return Err(ReadError::OutOfBounds);
949
104
        }
950
104
        let Some(data) = TableDataRef::new(data.as_bytes()) else {
951
0
            return Err(ReadError::OutOfBounds);
952
        };
953
104
        Ok(Self { data })
954
114
    }
955
}
956
957
/// The fields at the start of the table, which are read directly.
958
#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
959
#[repr(C, packed)]
960
#[doc(hidden)]
961
pub struct Cmap6FixedHeader {
962
    pub format: BigEndian<u16>,
963
    pub length: BigEndian<u16>,
964
    pub language: BigEndian<u16>,
965
    pub first_code: BigEndian<u16>,
966
    pub entry_count: BigEndian<u16>,
967
}
968
969
#[allow(clippy::absurd_extreme_comparisons)]
970
const _: () = assert!(std::mem::size_of::<Cmap6FixedHeader>() <= Cmap6::MIN_SIZE);
971
972
/// [cmap Format 6](https://docs.microsoft.com/en-us/typography/opentype/spec/cmap#format-6-trimmed-table-mapping): Trimmed table mapping
973
#[derive(Clone)]
974
pub struct Cmap6<'a> {
975
    data: TableDataRef<'a, Cmap6FixedHeader>,
976
}
977
978
#[allow(clippy::needless_lifetimes)]
979
impl<'a> Cmap6<'a> {
980
    pub const MIN_SIZE: usize = (u16::RAW_BYTE_LEN
981
        + u16::RAW_BYTE_LEN
982
        + u16::RAW_BYTE_LEN
983
        + u16::RAW_BYTE_LEN
984
        + u16::RAW_BYTE_LEN);
985
    basic_table_impls!(impl_the_methods);
986
987
    /// Format number is set to 6.
988
0
    pub fn format(&self) -> u16 {
989
0
        self.data.header().format.get()
990
0
    }
991
992
    /// This is the length in bytes of the subtable.
993
0
    pub fn length(&self) -> u16 {
994
0
        self.data.header().length.get()
995
0
    }
996
997
    /// For requirements on use of the language field, see “Use of
998
    /// the language field in 'cmap' subtables” in this document.
999
0
    pub fn language(&self) -> u16 {
1000
0
        self.data.header().language.get()
1001
0
    }
1002
1003
    /// First character code of subrange.
1004
122k
    pub fn first_code(&self) -> u16 {
1005
122k
        self.data.header().first_code.get()
1006
122k
    }
1007
1008
    /// Number of character codes in subrange.
1009
5.90k
    pub fn entry_count(&self) -> u16 {
1010
5.90k
        self.data.header().entry_count.get()
1011
5.90k
    }
1012
1013
    /// Array of glyph index values for character codes in the range.
1014
5.90k
    pub fn glyph_id_array(&self) -> &'a [BigEndian<u16>] {
1015
5.90k
        let range = self.glyph_id_array_byte_range();
1016
5.90k
        self.offset_data()
1017
5.90k
            .read_array(range)
1018
5.90k
            .ok()
1019
5.90k
            .unwrap_or_default()
1020
5.90k
    }
1021
1022
5.90k
    pub fn format_byte_range(&self) -> Range<usize> {
1023
5.90k
        let start = 0usize;
1024
5.90k
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
1025
5.90k
        start..end
1026
5.90k
    }
1027
1028
5.90k
    pub fn length_byte_range(&self) -> Range<usize> {
1029
5.90k
        let start = self.format_byte_range().end;
1030
5.90k
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
1031
5.90k
        start..end
1032
5.90k
    }
1033
1034
5.90k
    pub fn language_byte_range(&self) -> Range<usize> {
1035
5.90k
        let start = self.length_byte_range().end;
1036
5.90k
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
1037
5.90k
        start..end
1038
5.90k
    }
1039
1040
5.90k
    pub fn first_code_byte_range(&self) -> Range<usize> {
1041
5.90k
        let start = self.language_byte_range().end;
1042
5.90k
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
1043
5.90k
        start..end
1044
5.90k
    }
1045
1046
5.90k
    pub fn entry_count_byte_range(&self) -> Range<usize> {
1047
5.90k
        let start = self.first_code_byte_range().end;
1048
5.90k
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
1049
5.90k
        start..end
1050
5.90k
    }
1051
1052
5.90k
    pub fn glyph_id_array_byte_range(&self) -> Range<usize> {
1053
5.90k
        let entry_count = self.entry_count();
1054
5.90k
        let start = self.entry_count_byte_range().end;
1055
5.90k
        let end = start
1056
5.90k
            .saturating_add((transforms::to_usize(entry_count)).saturating_mul(u16::RAW_BYTE_LEN));
1057
5.90k
        start..end
1058
5.90k
    }
1059
}
1060
1061
impl Format<u16> for Cmap8<'_> {
1062
    const FORMAT: u16 = 8;
1063
}
1064
1065
impl<'a> MinByteRange<'a> for Cmap8<'a> {
1066
0
    fn min_byte_range(&self) -> Range<usize> {
1067
0
        0..self.groups_byte_range().end
1068
0
    }
1069
0
    fn min_table_bytes(&self) -> &'a [u8] {
1070
0
        let range = self.min_byte_range();
1071
0
        self.offset_data().as_bytes().get(range).unwrap_or_default()
1072
0
    }
1073
}
1074
1075
impl ReadArgs for Cmap8<'_> {
1076
    type Args = ();
1077
}
1078
1079
impl<'a> FontRead<'a> for Cmap8<'a> {
1080
0
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
1081
        #[allow(clippy::absurd_extreme_comparisons)]
1082
0
        if data.len() < Self::MIN_SIZE {
1083
0
            return Err(ReadError::OutOfBounds);
1084
0
        }
1085
0
        let Some(data) = TableDataRef::new(data.as_bytes()) else {
1086
0
            return Err(ReadError::OutOfBounds);
1087
        };
1088
0
        Ok(Self { data })
1089
0
    }
1090
}
1091
1092
/// The fields at the start of the table, which are read directly.
1093
#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
1094
#[repr(C, packed)]
1095
#[doc(hidden)]
1096
pub struct Cmap8FixedHeader {
1097
    pub format: BigEndian<u16>,
1098
    pub reserved: BigEndian<u16>,
1099
    pub length: BigEndian<u32>,
1100
    pub language: BigEndian<u32>,
1101
}
1102
1103
#[allow(clippy::absurd_extreme_comparisons)]
1104
const _: () = assert!(std::mem::size_of::<Cmap8FixedHeader>() <= Cmap8::MIN_SIZE);
1105
1106
/// [cmap Format 8](https://docs.microsoft.com/en-us/typography/opentype/spec/cmap#format-8-mixed-16-bit-and-32-bit-coverage): mixed 16-bit and 32-bit coverage
1107
#[derive(Clone)]
1108
pub struct Cmap8<'a> {
1109
    data: TableDataRef<'a, Cmap8FixedHeader>,
1110
}
1111
1112
#[allow(clippy::needless_lifetimes)]
1113
impl<'a> Cmap8<'a> {
1114
    pub const MIN_SIZE: usize = (u16::RAW_BYTE_LEN
1115
        + u16::RAW_BYTE_LEN
1116
        + u32::RAW_BYTE_LEN
1117
        + u32::RAW_BYTE_LEN
1118
        + u8::RAW_BYTE_LEN * 8192_usize
1119
        + u32::RAW_BYTE_LEN);
1120
    basic_table_impls!(impl_the_methods);
1121
1122
    /// Subtable format; set to 8.
1123
0
    pub fn format(&self) -> u16 {
1124
0
        self.data.header().format.get()
1125
0
    }
1126
1127
    /// Byte length of this subtable (including the header)
1128
0
    pub fn length(&self) -> u32 {
1129
0
        self.data.header().length.get()
1130
0
    }
1131
1132
    /// For requirements on use of the language field, see “Use of
1133
    /// the language field in 'cmap' subtables” in this document.
1134
0
    pub fn language(&self) -> u32 {
1135
0
        self.data.header().language.get()
1136
0
    }
1137
1138
    /// Tightly packed array of bits (8K bytes total) indicating
1139
    /// whether the particular 16-bit (index) value is the start of a
1140
    /// 32-bit character code
1141
0
    pub fn is32(&self) -> &'a [u8] {
1142
0
        let range = self.is32_byte_range();
1143
0
        self.offset_data()
1144
0
            .read_array(range)
1145
0
            .ok()
1146
0
            .unwrap_or_default()
1147
0
    }
1148
1149
    /// Number of groupings which follow
1150
0
    pub fn num_groups(&self) -> u32 {
1151
0
        let range = self.num_groups_byte_range();
1152
0
        self.offset_data()
1153
0
            .read_at(range.start)
1154
0
            .ok()
1155
0
            .unwrap_or_default()
1156
0
    }
1157
1158
    /// Array of SequentialMapGroup records.
1159
0
    pub fn groups(&self) -> &'a [SequentialMapGroup] {
1160
0
        let range = self.groups_byte_range();
1161
0
        self.offset_data()
1162
0
            .read_array(range)
1163
0
            .ok()
1164
0
            .unwrap_or_default()
1165
0
    }
1166
1167
0
    pub fn format_byte_range(&self) -> Range<usize> {
1168
0
        let start = 0usize;
1169
0
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
1170
0
        start..end
1171
0
    }
1172
1173
0
    pub fn reserved_byte_range(&self) -> Range<usize> {
1174
0
        let start = self.format_byte_range().end;
1175
0
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
1176
0
        start..end
1177
0
    }
1178
1179
0
    pub fn length_byte_range(&self) -> Range<usize> {
1180
0
        let start = self.reserved_byte_range().end;
1181
0
        let end = start.saturating_add(u32::RAW_BYTE_LEN);
1182
0
        start..end
1183
0
    }
1184
1185
0
    pub fn language_byte_range(&self) -> Range<usize> {
1186
0
        let start = self.length_byte_range().end;
1187
0
        let end = start.saturating_add(u32::RAW_BYTE_LEN);
1188
0
        start..end
1189
0
    }
1190
1191
0
    pub fn is32_byte_range(&self) -> Range<usize> {
1192
0
        let start = self.language_byte_range().end;
1193
0
        let end = start.saturating_add((8192_usize).saturating_mul(u8::RAW_BYTE_LEN));
1194
0
        start..end
1195
0
    }
1196
1197
0
    pub fn num_groups_byte_range(&self) -> Range<usize> {
1198
0
        let start = self.is32_byte_range().end;
1199
0
        let end = start.saturating_add(u32::RAW_BYTE_LEN);
1200
0
        start..end
1201
0
    }
1202
1203
0
    pub fn groups_byte_range(&self) -> Range<usize> {
1204
0
        let num_groups = self.num_groups();
1205
0
        let start = self.num_groups_byte_range().end;
1206
0
        let end = start.saturating_add(
1207
0
            (transforms::to_usize(num_groups)).saturating_mul(SequentialMapGroup::RAW_BYTE_LEN),
1208
        );
1209
0
        start..end
1210
0
    }
1211
}
1212
1213
/// Used in [Cmap8] and [Cmap12]
1214
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, bytemuck :: AnyBitPattern)]
1215
#[repr(C)]
1216
#[repr(packed)]
1217
pub struct SequentialMapGroup {
1218
    /// First character code in this group; note that if this group is
1219
    /// for one or more 16-bit character codes (which is determined
1220
    /// from the is32 array), this 32-bit value will have the high
1221
    /// 16-bits set to zero
1222
    pub start_char_code: BigEndian<u32>,
1223
    /// Last character code in this group; same condition as listed
1224
    /// above for the startCharCode
1225
    pub end_char_code: BigEndian<u32>,
1226
    /// Glyph index corresponding to the starting character code
1227
    pub start_glyph_id: BigEndian<u32>,
1228
}
1229
1230
impl SequentialMapGroup {
1231
    /// First character code in this group; note that if this group is
1232
    /// for one or more 16-bit character codes (which is determined
1233
    /// from the is32 array), this 32-bit value will have the high
1234
    /// 16-bits set to zero
1235
213k
    pub fn start_char_code(&self) -> u32 {
1236
213k
        self.start_char_code.get()
1237
213k
    }
1238
1239
    /// Last character code in this group; same condition as listed
1240
    /// above for the startCharCode
1241
148k
    pub fn end_char_code(&self) -> u32 {
1242
148k
        self.end_char_code.get()
1243
148k
    }
1244
1245
    /// Glyph index corresponding to the starting character code
1246
64.6k
    pub fn start_glyph_id(&self) -> u32 {
1247
64.6k
        self.start_glyph_id.get()
1248
64.6k
    }
1249
}
1250
1251
impl FixedSize for SequentialMapGroup {
1252
    const RAW_BYTE_LEN: usize = u32::RAW_BYTE_LEN + u32::RAW_BYTE_LEN + u32::RAW_BYTE_LEN;
1253
}
1254
1255
impl Format<u16> for Cmap10<'_> {
1256
    const FORMAT: u16 = 10;
1257
}
1258
1259
impl<'a> MinByteRange<'a> for Cmap10<'a> {
1260
0
    fn min_byte_range(&self) -> Range<usize> {
1261
0
        0..self.glyph_id_array_byte_range().end
1262
0
    }
1263
0
    fn min_table_bytes(&self) -> &'a [u8] {
1264
0
        let range = self.min_byte_range();
1265
0
        self.offset_data().as_bytes().get(range).unwrap_or_default()
1266
0
    }
1267
}
1268
1269
impl ReadArgs for Cmap10<'_> {
1270
    type Args = ();
1271
}
1272
1273
impl<'a> FontRead<'a> for Cmap10<'a> {
1274
158
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
1275
        #[allow(clippy::absurd_extreme_comparisons)]
1276
158
        if data.len() < Self::MIN_SIZE {
1277
15
            return Err(ReadError::OutOfBounds);
1278
143
        }
1279
143
        let Some(data) = TableDataRef::new(data.as_bytes()) else {
1280
0
            return Err(ReadError::OutOfBounds);
1281
        };
1282
143
        Ok(Self { data })
1283
158
    }
1284
}
1285
1286
/// The fields at the start of the table, which are read directly.
1287
#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
1288
#[repr(C, packed)]
1289
#[doc(hidden)]
1290
pub struct Cmap10FixedHeader {
1291
    pub format: BigEndian<u16>,
1292
    pub reserved: BigEndian<u16>,
1293
    pub length: BigEndian<u32>,
1294
    pub language: BigEndian<u32>,
1295
    pub start_char_code: BigEndian<u32>,
1296
    pub num_chars: BigEndian<u32>,
1297
}
1298
1299
#[allow(clippy::absurd_extreme_comparisons)]
1300
const _: () = assert!(std::mem::size_of::<Cmap10FixedHeader>() <= Cmap10::MIN_SIZE);
1301
1302
/// [cmap Format 10](https://docs.microsoft.com/en-us/typography/opentype/spec/cmap#format-10-trimmed-array): Tr
1303
#[derive(Clone)]
1304
pub struct Cmap10<'a> {
1305
    data: TableDataRef<'a, Cmap10FixedHeader>,
1306
}
1307
1308
#[allow(clippy::needless_lifetimes)]
1309
impl<'a> Cmap10<'a> {
1310
    pub const MIN_SIZE: usize = (u16::RAW_BYTE_LEN
1311
        + u16::RAW_BYTE_LEN
1312
        + u32::RAW_BYTE_LEN
1313
        + u32::RAW_BYTE_LEN
1314
        + u32::RAW_BYTE_LEN
1315
        + u32::RAW_BYTE_LEN);
1316
    basic_table_impls!(impl_the_methods);
1317
1318
    /// Subtable format; set to 10.
1319
0
    pub fn format(&self) -> u16 {
1320
0
        self.data.header().format.get()
1321
0
    }
1322
1323
    /// Byte length of this subtable (including the header)
1324
0
    pub fn length(&self) -> u32 {
1325
0
        self.data.header().length.get()
1326
0
    }
1327
1328
    /// For requirements on use of the language field, see “Use of
1329
    /// the language field in 'cmap' subtables” in this document.
1330
0
    pub fn language(&self) -> u32 {
1331
0
        self.data.header().language.get()
1332
0
    }
1333
1334
    /// First character code covered
1335
216k
    pub fn start_char_code(&self) -> u32 {
1336
216k
        self.data.header().start_char_code.get()
1337
216k
    }
1338
1339
    /// Number of character codes covered
1340
3.51k
    pub fn num_chars(&self) -> u32 {
1341
3.51k
        self.data.header().num_chars.get()
1342
3.51k
    }
1343
1344
    /// Array of glyph indices for the character codes covered
1345
3.51k
    pub fn glyph_id_array(&self) -> &'a [BigEndian<u16>] {
1346
3.51k
        let range = self.glyph_id_array_byte_range();
1347
3.51k
        self.offset_data()
1348
3.51k
            .read_array(range)
1349
3.51k
            .ok()
1350
3.51k
            .unwrap_or_default()
1351
3.51k
    }
1352
1353
3.51k
    pub fn format_byte_range(&self) -> Range<usize> {
1354
3.51k
        let start = 0usize;
1355
3.51k
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
1356
3.51k
        start..end
1357
3.51k
    }
1358
1359
3.51k
    pub fn reserved_byte_range(&self) -> Range<usize> {
1360
3.51k
        let start = self.format_byte_range().end;
1361
3.51k
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
1362
3.51k
        start..end
1363
3.51k
    }
1364
1365
3.51k
    pub fn length_byte_range(&self) -> Range<usize> {
1366
3.51k
        let start = self.reserved_byte_range().end;
1367
3.51k
        let end = start.saturating_add(u32::RAW_BYTE_LEN);
1368
3.51k
        start..end
1369
3.51k
    }
1370
1371
3.51k
    pub fn language_byte_range(&self) -> Range<usize> {
1372
3.51k
        let start = self.length_byte_range().end;
1373
3.51k
        let end = start.saturating_add(u32::RAW_BYTE_LEN);
1374
3.51k
        start..end
1375
3.51k
    }
1376
1377
3.51k
    pub fn start_char_code_byte_range(&self) -> Range<usize> {
1378
3.51k
        let start = self.language_byte_range().end;
1379
3.51k
        let end = start.saturating_add(u32::RAW_BYTE_LEN);
1380
3.51k
        start..end
1381
3.51k
    }
1382
1383
3.51k
    pub fn num_chars_byte_range(&self) -> Range<usize> {
1384
3.51k
        let start = self.start_char_code_byte_range().end;
1385
3.51k
        let end = start.saturating_add(u32::RAW_BYTE_LEN);
1386
3.51k
        start..end
1387
3.51k
    }
1388
1389
3.51k
    pub fn glyph_id_array_byte_range(&self) -> Range<usize> {
1390
3.51k
        let num_chars = self.num_chars();
1391
3.51k
        let start = self.num_chars_byte_range().end;
1392
3.51k
        let end = start
1393
3.51k
            .saturating_add((transforms::to_usize(num_chars)).saturating_mul(u16::RAW_BYTE_LEN));
1394
3.51k
        start..end
1395
3.51k
    }
1396
}
1397
1398
impl Format<u16> for Cmap12<'_> {
1399
    const FORMAT: u16 = 12;
1400
}
1401
1402
impl<'a> MinByteRange<'a> for Cmap12<'a> {
1403
0
    fn min_byte_range(&self) -> Range<usize> {
1404
0
        0..self.groups_byte_range().end
1405
0
    }
1406
0
    fn min_table_bytes(&self) -> &'a [u8] {
1407
0
        let range = self.min_byte_range();
1408
0
        self.offset_data().as_bytes().get(range).unwrap_or_default()
1409
0
    }
1410
}
1411
1412
impl ReadArgs for Cmap12<'_> {
1413
    type Args = ();
1414
}
1415
1416
impl<'a> FontRead<'a> for Cmap12<'a> {
1417
1.14k
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
1418
        #[allow(clippy::absurd_extreme_comparisons)]
1419
1.14k
        if data.len() < Self::MIN_SIZE {
1420
10
            return Err(ReadError::OutOfBounds);
1421
1.13k
        }
1422
1.13k
        let Some(data) = TableDataRef::new(data.as_bytes()) else {
1423
0
            return Err(ReadError::OutOfBounds);
1424
        };
1425
1.13k
        Ok(Self { data })
1426
1.14k
    }
1427
}
1428
1429
/// The fields at the start of the table, which are read directly.
1430
#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
1431
#[repr(C, packed)]
1432
#[doc(hidden)]
1433
pub struct Cmap12FixedHeader {
1434
    pub format: BigEndian<u16>,
1435
    pub reserved: BigEndian<u16>,
1436
    pub length: BigEndian<u32>,
1437
    pub language: BigEndian<u32>,
1438
    pub num_groups: BigEndian<u32>,
1439
}
1440
1441
#[allow(clippy::absurd_extreme_comparisons)]
1442
const _: () = assert!(std::mem::size_of::<Cmap12FixedHeader>() <= Cmap12::MIN_SIZE);
1443
1444
/// [cmap Format 12](https://docs.microsoft.com/en-us/typography/opentype/spec/cmap#format-12-segmented-coverage): Segmented coverage
1445
#[derive(Clone)]
1446
pub struct Cmap12<'a> {
1447
    data: TableDataRef<'a, Cmap12FixedHeader>,
1448
}
1449
1450
#[allow(clippy::needless_lifetimes)]
1451
impl<'a> Cmap12<'a> {
1452
    pub const MIN_SIZE: usize = (u16::RAW_BYTE_LEN
1453
        + u16::RAW_BYTE_LEN
1454
        + u32::RAW_BYTE_LEN
1455
        + u32::RAW_BYTE_LEN
1456
        + u32::RAW_BYTE_LEN);
1457
    basic_table_impls!(impl_the_methods);
1458
1459
    /// Subtable format; set to 12.
1460
0
    pub fn format(&self) -> u16 {
1461
0
        self.data.header().format.get()
1462
0
    }
1463
1464
    /// Byte length of this subtable (including the header)
1465
0
    pub fn length(&self) -> u32 {
1466
0
        self.data.header().length.get()
1467
0
    }
1468
1469
    /// For requirements on use of the language field, see “Use of
1470
    /// the language field in 'cmap' subtables” in this document.
1471
0
    pub fn language(&self) -> u32 {
1472
0
        self.data.header().language.get()
1473
0
    }
1474
1475
    /// Number of groupings which follow
1476
87.7k
    pub fn num_groups(&self) -> u32 {
1477
87.7k
        self.data.header().num_groups.get()
1478
87.7k
    }
1479
1480
    /// Array of SequentialMapGroup records.
1481
87.7k
    pub fn groups(&self) -> &'a [SequentialMapGroup] {
1482
87.7k
        let range = self.groups_byte_range();
1483
87.7k
        self.offset_data()
1484
87.7k
            .read_array(range)
1485
87.7k
            .ok()
1486
87.7k
            .unwrap_or_default()
1487
87.7k
    }
1488
1489
87.7k
    pub fn format_byte_range(&self) -> Range<usize> {
1490
87.7k
        let start = 0usize;
1491
87.7k
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
1492
87.7k
        start..end
1493
87.7k
    }
1494
1495
87.7k
    pub fn reserved_byte_range(&self) -> Range<usize> {
1496
87.7k
        let start = self.format_byte_range().end;
1497
87.7k
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
1498
87.7k
        start..end
1499
87.7k
    }
1500
1501
87.7k
    pub fn length_byte_range(&self) -> Range<usize> {
1502
87.7k
        let start = self.reserved_byte_range().end;
1503
87.7k
        let end = start.saturating_add(u32::RAW_BYTE_LEN);
1504
87.7k
        start..end
1505
87.7k
    }
1506
1507
87.7k
    pub fn language_byte_range(&self) -> Range<usize> {
1508
87.7k
        let start = self.length_byte_range().end;
1509
87.7k
        let end = start.saturating_add(u32::RAW_BYTE_LEN);
1510
87.7k
        start..end
1511
87.7k
    }
1512
1513
87.7k
    pub fn num_groups_byte_range(&self) -> Range<usize> {
1514
87.7k
        let start = self.language_byte_range().end;
1515
87.7k
        let end = start.saturating_add(u32::RAW_BYTE_LEN);
1516
87.7k
        start..end
1517
87.7k
    }
1518
1519
87.7k
    pub fn groups_byte_range(&self) -> Range<usize> {
1520
87.7k
        let num_groups = self.num_groups();
1521
87.7k
        let start = self.num_groups_byte_range().end;
1522
87.7k
        let end = start.saturating_add(
1523
87.7k
            (transforms::to_usize(num_groups)).saturating_mul(SequentialMapGroup::RAW_BYTE_LEN),
1524
        );
1525
87.7k
        start..end
1526
87.7k
    }
1527
}
1528
1529
impl Format<u16> for Cmap13<'_> {
1530
    const FORMAT: u16 = 13;
1531
}
1532
1533
impl<'a> MinByteRange<'a> for Cmap13<'a> {
1534
0
    fn min_byte_range(&self) -> Range<usize> {
1535
0
        0..self.groups_byte_range().end
1536
0
    }
1537
0
    fn min_table_bytes(&self) -> &'a [u8] {
1538
0
        let range = self.min_byte_range();
1539
0
        self.offset_data().as_bytes().get(range).unwrap_or_default()
1540
0
    }
1541
}
1542
1543
impl ReadArgs for Cmap13<'_> {
1544
    type Args = ();
1545
}
1546
1547
impl<'a> FontRead<'a> for Cmap13<'a> {
1548
914
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
1549
        #[allow(clippy::absurd_extreme_comparisons)]
1550
914
        if data.len() < Self::MIN_SIZE {
1551
11
            return Err(ReadError::OutOfBounds);
1552
903
        }
1553
903
        let Some(data) = TableDataRef::new(data.as_bytes()) else {
1554
0
            return Err(ReadError::OutOfBounds);
1555
        };
1556
903
        Ok(Self { data })
1557
914
    }
1558
}
1559
1560
/// The fields at the start of the table, which are read directly.
1561
#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
1562
#[repr(C, packed)]
1563
#[doc(hidden)]
1564
pub struct Cmap13FixedHeader {
1565
    pub format: BigEndian<u16>,
1566
    pub reserved: BigEndian<u16>,
1567
    pub length: BigEndian<u32>,
1568
    pub language: BigEndian<u32>,
1569
    pub num_groups: BigEndian<u32>,
1570
}
1571
1572
#[allow(clippy::absurd_extreme_comparisons)]
1573
const _: () = assert!(std::mem::size_of::<Cmap13FixedHeader>() <= Cmap13::MIN_SIZE);
1574
1575
/// [cmap Format 13](https://docs.microsoft.com/en-us/typography/opentype/spec/cmap#format-13-many-to-one-range-mappings): Many-to-one range mappings
1576
#[derive(Clone)]
1577
pub struct Cmap13<'a> {
1578
    data: TableDataRef<'a, Cmap13FixedHeader>,
1579
}
1580
1581
#[allow(clippy::needless_lifetimes)]
1582
impl<'a> Cmap13<'a> {
1583
    pub const MIN_SIZE: usize = (u16::RAW_BYTE_LEN
1584
        + u16::RAW_BYTE_LEN
1585
        + u32::RAW_BYTE_LEN
1586
        + u32::RAW_BYTE_LEN
1587
        + u32::RAW_BYTE_LEN);
1588
    basic_table_impls!(impl_the_methods);
1589
1590
    /// Subtable format; set to 13.
1591
0
    pub fn format(&self) -> u16 {
1592
0
        self.data.header().format.get()
1593
0
    }
1594
1595
    /// Byte length of this subtable (including the header)
1596
0
    pub fn length(&self) -> u32 {
1597
0
        self.data.header().length.get()
1598
0
    }
1599
1600
    /// For requirements on use of the language field, see “Use of
1601
    /// the language field in 'cmap' subtables” in this document.
1602
0
    pub fn language(&self) -> u32 {
1603
0
        self.data.header().language.get()
1604
0
    }
1605
1606
    /// Number of groupings which follow
1607
180k
    pub fn num_groups(&self) -> u32 {
1608
180k
        self.data.header().num_groups.get()
1609
180k
    }
1610
1611
    /// Array of ConstantMapGroup records.
1612
180k
    pub fn groups(&self) -> &'a [ConstantMapGroup] {
1613
180k
        let range = self.groups_byte_range();
1614
180k
        self.offset_data()
1615
180k
            .read_array(range)
1616
180k
            .ok()
1617
180k
            .unwrap_or_default()
1618
180k
    }
1619
1620
180k
    pub fn format_byte_range(&self) -> Range<usize> {
1621
180k
        let start = 0usize;
1622
180k
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
1623
180k
        start..end
1624
180k
    }
1625
1626
180k
    pub fn reserved_byte_range(&self) -> Range<usize> {
1627
180k
        let start = self.format_byte_range().end;
1628
180k
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
1629
180k
        start..end
1630
180k
    }
1631
1632
180k
    pub fn length_byte_range(&self) -> Range<usize> {
1633
180k
        let start = self.reserved_byte_range().end;
1634
180k
        let end = start.saturating_add(u32::RAW_BYTE_LEN);
1635
180k
        start..end
1636
180k
    }
1637
1638
180k
    pub fn language_byte_range(&self) -> Range<usize> {
1639
180k
        let start = self.length_byte_range().end;
1640
180k
        let end = start.saturating_add(u32::RAW_BYTE_LEN);
1641
180k
        start..end
1642
180k
    }
1643
1644
180k
    pub fn num_groups_byte_range(&self) -> Range<usize> {
1645
180k
        let start = self.language_byte_range().end;
1646
180k
        let end = start.saturating_add(u32::RAW_BYTE_LEN);
1647
180k
        start..end
1648
180k
    }
1649
1650
180k
    pub fn groups_byte_range(&self) -> Range<usize> {
1651
180k
        let num_groups = self.num_groups();
1652
180k
        let start = self.num_groups_byte_range().end;
1653
180k
        let end = start.saturating_add(
1654
180k
            (transforms::to_usize(num_groups)).saturating_mul(ConstantMapGroup::RAW_BYTE_LEN),
1655
        );
1656
180k
        start..end
1657
180k
    }
1658
}
1659
1660
/// Part of [Cmap13]
1661
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, bytemuck :: AnyBitPattern)]
1662
#[repr(C)]
1663
#[repr(packed)]
1664
pub struct ConstantMapGroup {
1665
    /// First character code in this group
1666
    pub start_char_code: BigEndian<u32>,
1667
    /// Last character code in this group
1668
    pub end_char_code: BigEndian<u32>,
1669
    /// Glyph index to be used for all the characters in the group’s
1670
    /// range.
1671
    pub glyph_id: BigEndian<u32>,
1672
}
1673
1674
impl ConstantMapGroup {
1675
    /// First character code in this group
1676
390k
    pub fn start_char_code(&self) -> u32 {
1677
390k
        self.start_char_code.get()
1678
390k
    }
1679
1680
    /// Last character code in this group
1681
258k
    pub fn end_char_code(&self) -> u32 {
1682
258k
        self.end_char_code.get()
1683
258k
    }
1684
1685
    /// Glyph index to be used for all the characters in the group’s
1686
    /// range.
1687
131k
    pub fn glyph_id(&self) -> u32 {
1688
131k
        self.glyph_id.get()
1689
131k
    }
1690
}
1691
1692
impl FixedSize for ConstantMapGroup {
1693
    const RAW_BYTE_LEN: usize = u32::RAW_BYTE_LEN + u32::RAW_BYTE_LEN + u32::RAW_BYTE_LEN;
1694
}
1695
1696
impl Format<u16> for Cmap14<'_> {
1697
    const FORMAT: u16 = 14;
1698
}
1699
1700
impl<'a> MinByteRange<'a> for Cmap14<'a> {
1701
0
    fn min_byte_range(&self) -> Range<usize> {
1702
0
        0..self.var_selector_byte_range().end
1703
0
    }
1704
0
    fn min_table_bytes(&self) -> &'a [u8] {
1705
0
        let range = self.min_byte_range();
1706
0
        self.offset_data().as_bytes().get(range).unwrap_or_default()
1707
0
    }
1708
}
1709
1710
impl ReadArgs for Cmap14<'_> {
1711
    type Args = ();
1712
}
1713
1714
impl<'a> FontRead<'a> for Cmap14<'a> {
1715
151
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
1716
        #[allow(clippy::absurd_extreme_comparisons)]
1717
151
        if data.len() < Self::MIN_SIZE {
1718
10
            return Err(ReadError::OutOfBounds);
1719
141
        }
1720
141
        let Some(data) = TableDataRef::new(data.as_bytes()) else {
1721
0
            return Err(ReadError::OutOfBounds);
1722
        };
1723
141
        Ok(Self { data })
1724
151
    }
1725
}
1726
1727
/// The fields at the start of the table, which are read directly.
1728
#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
1729
#[repr(C, packed)]
1730
#[doc(hidden)]
1731
pub struct Cmap14FixedHeader {
1732
    pub format: BigEndian<u16>,
1733
    pub length: BigEndian<u32>,
1734
    pub num_var_selector_records: BigEndian<u32>,
1735
}
1736
1737
#[allow(clippy::absurd_extreme_comparisons)]
1738
const _: () = assert!(std::mem::size_of::<Cmap14FixedHeader>() <= Cmap14::MIN_SIZE);
1739
1740
/// [cmap Format 14](https://docs.microsoft.com/en-us/typography/opentype/spec/cmap#format-14-unicode-variation-sequences): Unicode Variation Sequences
1741
#[derive(Clone)]
1742
pub struct Cmap14<'a> {
1743
    data: TableDataRef<'a, Cmap14FixedHeader>,
1744
}
1745
1746
#[allow(clippy::needless_lifetimes)]
1747
impl<'a> Cmap14<'a> {
1748
    pub const MIN_SIZE: usize = (u16::RAW_BYTE_LEN + u32::RAW_BYTE_LEN + u32::RAW_BYTE_LEN);
1749
    basic_table_impls!(impl_the_methods);
1750
1751
    /// Subtable format. Set to 14.
1752
0
    pub fn format(&self) -> u16 {
1753
0
        self.data.header().format.get()
1754
0
    }
1755
1756
    /// Byte length of this subtable (including this header)
1757
0
    pub fn length(&self) -> u32 {
1758
0
        self.data.header().length.get()
1759
0
    }
1760
1761
    /// Number of variation Selector Records
1762
129
    pub fn num_var_selector_records(&self) -> u32 {
1763
129
        self.data.header().num_var_selector_records.get()
1764
129
    }
1765
1766
    /// Array of VariationSelector records.
1767
129
    pub fn var_selector(&self) -> &'a [VariationSelector] {
1768
129
        let range = self.var_selector_byte_range();
1769
129
        self.offset_data()
1770
129
            .read_array(range)
1771
129
            .ok()
1772
129
            .unwrap_or_default()
1773
129
    }
1774
1775
129
    pub fn format_byte_range(&self) -> Range<usize> {
1776
129
        let start = 0usize;
1777
129
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
1778
129
        start..end
1779
129
    }
1780
1781
129
    pub fn length_byte_range(&self) -> Range<usize> {
1782
129
        let start = self.format_byte_range().end;
1783
129
        let end = start.saturating_add(u32::RAW_BYTE_LEN);
1784
129
        start..end
1785
129
    }
1786
1787
129
    pub fn num_var_selector_records_byte_range(&self) -> Range<usize> {
1788
129
        let start = self.length_byte_range().end;
1789
129
        let end = start.saturating_add(u32::RAW_BYTE_LEN);
1790
129
        start..end
1791
129
    }
1792
1793
129
    pub fn var_selector_byte_range(&self) -> Range<usize> {
1794
129
        let num_var_selector_records = self.num_var_selector_records();
1795
129
        let start = self.num_var_selector_records_byte_range().end;
1796
129
        let end = start.saturating_add(
1797
129
            (transforms::to_usize(num_var_selector_records))
1798
129
                .saturating_mul(VariationSelector::RAW_BYTE_LEN),
1799
        );
1800
129
        start..end
1801
129
    }
1802
}
1803
1804
impl Format<u16> for Cmap15<'_> {
1805
    const FORMAT: u16 = 15;
1806
}
1807
1808
impl<'a> MinByteRange<'a> for Cmap15<'a> {
1809
0
    fn min_byte_range(&self) -> Range<usize> {
1810
0
        0..self.var_selector_byte_range().end
1811
0
    }
1812
0
    fn min_table_bytes(&self) -> &'a [u8] {
1813
0
        let range = self.min_byte_range();
1814
0
        self.offset_data().as_bytes().get(range).unwrap_or_default()
1815
0
    }
1816
}
1817
1818
impl ReadArgs for Cmap15<'_> {
1819
    type Args = ();
1820
}
1821
1822
impl<'a> FontRead<'a> for Cmap15<'a> {
1823
53
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
1824
        #[allow(clippy::absurd_extreme_comparisons)]
1825
53
        if data.len() < Self::MIN_SIZE {
1826
19
            return Err(ReadError::OutOfBounds);
1827
34
        }
1828
34
        let Some(data) = TableDataRef::new(data.as_bytes()) else {
1829
0
            return Err(ReadError::OutOfBounds);
1830
        };
1831
34
        Ok(Self { data })
1832
53
    }
1833
}
1834
1835
/// The fields at the start of the table, which are read directly.
1836
#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
1837
#[repr(C, packed)]
1838
#[doc(hidden)]
1839
pub struct Cmap15FixedHeader {
1840
    pub format: BigEndian<u16>,
1841
    pub length: BigEndian<u32>,
1842
    pub num_var_selector_records: BigEndian<u32>,
1843
}
1844
1845
#[allow(clippy::absurd_extreme_comparisons)]
1846
const _: () = assert!(std::mem::size_of::<Cmap15FixedHeader>() <= Cmap15::MIN_SIZE);
1847
1848
/// Extended glyph repertoire UVS mapping (ISO/IEC 14496-22:2026, 5.1.2.5.11).
1849
#[derive(Clone)]
1850
pub struct Cmap15<'a> {
1851
    data: TableDataRef<'a, Cmap15FixedHeader>,
1852
}
1853
1854
#[allow(clippy::needless_lifetimes)]
1855
impl<'a> Cmap15<'a> {
1856
    pub const MIN_SIZE: usize = (u16::RAW_BYTE_LEN + u32::RAW_BYTE_LEN + u32::RAW_BYTE_LEN);
1857
    basic_table_impls!(impl_the_methods);
1858
1859
    /// Subtable format. Set to 15.
1860
0
    pub fn format(&self) -> u16 {
1861
0
        self.data.header().format.get()
1862
0
    }
1863
1864
    /// Byte length of this subtable (including this header).
1865
0
    pub fn length(&self) -> u32 {
1866
0
        self.data.header().length.get()
1867
0
    }
1868
1869
    /// Number of variation selector records.
1870
34
    pub fn num_var_selector_records(&self) -> u32 {
1871
34
        self.data.header().num_var_selector_records.get()
1872
34
    }
1873
1874
0
    pub fn var_selector(&self) -> &'a [VariationSelector15] {
1875
0
        let range = self.var_selector_byte_range();
1876
0
        self.offset_data()
1877
0
            .read_array(range)
1878
0
            .ok()
1879
0
            .unwrap_or_default()
1880
0
    }
1881
1882
34
    pub fn format_byte_range(&self) -> Range<usize> {
1883
34
        let start = 0usize;
1884
34
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
1885
34
        start..end
1886
34
    }
1887
1888
34
    pub fn length_byte_range(&self) -> Range<usize> {
1889
34
        let start = self.format_byte_range().end;
1890
34
        let end = start.saturating_add(u32::RAW_BYTE_LEN);
1891
34
        start..end
1892
34
    }
1893
1894
34
    pub fn num_var_selector_records_byte_range(&self) -> Range<usize> {
1895
34
        let start = self.length_byte_range().end;
1896
34
        let end = start.saturating_add(u32::RAW_BYTE_LEN);
1897
34
        start..end
1898
34
    }
1899
1900
34
    pub fn var_selector_byte_range(&self) -> Range<usize> {
1901
34
        let num_var_selector_records = self.num_var_selector_records();
1902
34
        let start = self.num_var_selector_records_byte_range().end;
1903
34
        let end = start.saturating_add(
1904
34
            (transforms::to_usize(num_var_selector_records))
1905
34
                .saturating_mul(VariationSelector15::RAW_BYTE_LEN),
1906
        );
1907
34
        start..end
1908
34
    }
1909
}
1910
1911
/// Part of [Cmap15].
1912
#[derive(Clone, Debug, Copy, bytemuck :: AnyBitPattern)]
1913
#[repr(C)]
1914
#[repr(packed)]
1915
pub struct VariationSelector15 {
1916
    pub var_selector: BigEndian<Uint24>,
1917
    pub default_uvs_offset: BigEndian<Nullable<Offset32>>,
1918
    pub non_default_uvs_offset: BigEndian<Nullable<Offset32>>,
1919
}
1920
1921
impl VariationSelector15 {
1922
0
    pub fn var_selector(&self) -> Uint24 {
1923
0
        self.var_selector.get()
1924
0
    }
1925
1926
0
    pub fn default_uvs_offset(&self) -> Nullable<Offset32> {
1927
0
        self.default_uvs_offset.get()
1928
0
    }
1929
1930
    ///
1931
    /// The `data` argument should be retrieved from the parent table
1932
    /// By calling its `offset_data` method.
1933
0
    pub fn default_uvs<'a>(&self, data: FontData<'a>) -> Option<Result<DefaultUvs<'a>, ReadError>> {
1934
0
        self.default_uvs_offset().resolve(data)
1935
0
    }
1936
1937
0
    pub fn non_default_uvs_offset(&self) -> Nullable<Offset32> {
1938
0
        self.non_default_uvs_offset.get()
1939
0
    }
1940
1941
    ///
1942
    /// The `data` argument should be retrieved from the parent table
1943
    /// By calling its `offset_data` method.
1944
0
    pub fn non_default_uvs<'a>(
1945
0
        &self,
1946
0
        data: FontData<'a>,
1947
0
    ) -> Option<Result<NonDefaultUvs24<'a>, ReadError>> {
1948
0
        self.non_default_uvs_offset().resolve(data)
1949
0
    }
1950
}
1951
1952
impl FixedSize for VariationSelector15 {
1953
    const RAW_BYTE_LEN: usize =
1954
        Uint24::RAW_BYTE_LEN + Offset32::RAW_BYTE_LEN + Offset32::RAW_BYTE_LEN;
1955
}
1956
1957
/// Part of [Cmap14]
1958
#[derive(Clone, Debug, Copy, bytemuck :: AnyBitPattern)]
1959
#[repr(C)]
1960
#[repr(packed)]
1961
pub struct VariationSelector {
1962
    /// Variation selector
1963
    pub var_selector: BigEndian<Uint24>,
1964
    /// Offset from the start of the [`Cmap14`] subtable to Default UVS
1965
    /// Table. May be NULL.
1966
    pub default_uvs_offset: BigEndian<Nullable<Offset32>>,
1967
    /// Offset from the start of the [`Cmap14`] subtable to Non-Default
1968
    /// UVS Table. May be NULL.
1969
    pub non_default_uvs_offset: BigEndian<Nullable<Offset32>>,
1970
}
1971
1972
impl VariationSelector {
1973
    /// Variation selector
1974
1.26k
    pub fn var_selector(&self) -> Uint24 {
1975
1.26k
        self.var_selector.get()
1976
1.26k
    }
1977
1978
    /// Offset from the start of the [`Cmap14`] subtable to Default UVS
1979
    /// Table. May be NULL.
1980
225
    pub fn default_uvs_offset(&self) -> Nullable<Offset32> {
1981
225
        self.default_uvs_offset.get()
1982
225
    }
1983
1984
    /// Offset from the start of the [`Cmap14`] subtable to Default UVS
1985
    /// Table. May be NULL.
1986
    ///
1987
    /// The `data` argument should be retrieved from the parent table
1988
    /// By calling its `offset_data` method.
1989
225
    pub fn default_uvs<'a>(&self, data: FontData<'a>) -> Option<Result<DefaultUvs<'a>, ReadError>> {
1990
225
        self.default_uvs_offset().resolve(data)
1991
225
    }
1992
1993
    /// Offset from the start of the [`Cmap14`] subtable to Non-Default
1994
    /// UVS Table. May be NULL.
1995
221
    pub fn non_default_uvs_offset(&self) -> Nullable<Offset32> {
1996
221
        self.non_default_uvs_offset.get()
1997
221
    }
1998
1999
    /// Offset from the start of the [`Cmap14`] subtable to Non-Default
2000
    /// UVS Table. May be NULL.
2001
    ///
2002
    /// The `data` argument should be retrieved from the parent table
2003
    /// By calling its `offset_data` method.
2004
0
    pub fn non_default_uvs<'a>(
2005
0
        &self,
2006
0
        data: FontData<'a>,
2007
0
    ) -> Option<Result<NonDefaultUvs<'a>, ReadError>> {
2008
0
        self.non_default_uvs_offset().resolve(data)
2009
0
    }
2010
}
2011
2012
impl FixedSize for VariationSelector {
2013
    const RAW_BYTE_LEN: usize =
2014
        Uint24::RAW_BYTE_LEN + Offset32::RAW_BYTE_LEN + Offset32::RAW_BYTE_LEN;
2015
}
2016
2017
impl<'a> MinByteRange<'a> for DefaultUvs<'a> {
2018
0
    fn min_byte_range(&self) -> Range<usize> {
2019
0
        0..self.ranges_byte_range().end
2020
0
    }
2021
0
    fn min_table_bytes(&self) -> &'a [u8] {
2022
0
        let range = self.min_byte_range();
2023
0
        self.offset_data().as_bytes().get(range).unwrap_or_default()
2024
0
    }
2025
}
2026
2027
impl ReadArgs for DefaultUvs<'_> {
2028
    type Args = ();
2029
}
2030
2031
impl<'a> FontRead<'a> for DefaultUvs<'a> {
2032
65
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
2033
        #[allow(clippy::absurd_extreme_comparisons)]
2034
65
        if data.len() < Self::MIN_SIZE {
2035
10
            return Err(ReadError::OutOfBounds);
2036
55
        }
2037
55
        let Some(data) = TableDataRef::new(data.as_bytes()) else {
2038
0
            return Err(ReadError::OutOfBounds);
2039
        };
2040
55
        Ok(Self { data })
2041
65
    }
2042
}
2043
2044
/// The fields at the start of the table, which are read directly.
2045
#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
2046
#[repr(C, packed)]
2047
#[doc(hidden)]
2048
pub struct DefaultUvsFixedHeader {
2049
    pub num_unicode_value_ranges: BigEndian<u32>,
2050
}
2051
2052
#[allow(clippy::absurd_extreme_comparisons)]
2053
const _: () = assert!(std::mem::size_of::<DefaultUvsFixedHeader>() <= DefaultUvs::MIN_SIZE);
2054
2055
/// [Default UVS table](https://docs.microsoft.com/en-us/typography/opentype/spec/cmap#default-uvs-table)
2056
#[derive(Clone)]
2057
pub struct DefaultUvs<'a> {
2058
    data: TableDataRef<'a, DefaultUvsFixedHeader>,
2059
}
2060
2061
#[allow(clippy::needless_lifetimes)]
2062
impl<'a> DefaultUvs<'a> {
2063
    pub const MIN_SIZE: usize = u32::RAW_BYTE_LEN;
2064
    basic_table_impls!(impl_the_methods);
2065
2066
    /// Number of Unicode character ranges.
2067
55
    pub fn num_unicode_value_ranges(&self) -> u32 {
2068
55
        self.data.header().num_unicode_value_ranges.get()
2069
55
    }
2070
2071
    /// Array of UnicodeRange records.
2072
55
    pub fn ranges(&self) -> &'a [UnicodeRange] {
2073
55
        let range = self.ranges_byte_range();
2074
55
        self.offset_data()
2075
55
            .read_array(range)
2076
55
            .ok()
2077
55
            .unwrap_or_default()
2078
55
    }
2079
2080
55
    pub fn num_unicode_value_ranges_byte_range(&self) -> Range<usize> {
2081
55
        let start = 0usize;
2082
55
        let end = start.saturating_add(u32::RAW_BYTE_LEN);
2083
55
        start..end
2084
55
    }
2085
2086
55
    pub fn ranges_byte_range(&self) -> Range<usize> {
2087
55
        let num_unicode_value_ranges = self.num_unicode_value_ranges();
2088
55
        let start = self.num_unicode_value_ranges_byte_range().end;
2089
55
        let end = start.saturating_add(
2090
55
            (transforms::to_usize(num_unicode_value_ranges))
2091
55
                .saturating_mul(UnicodeRange::RAW_BYTE_LEN),
2092
        );
2093
55
        start..end
2094
55
    }
2095
}
2096
2097
const _: () = assert!(FontData::default_data_long_enough(DefaultUvs::MIN_SIZE));
2098
2099
impl Default for DefaultUvs<'_> {
2100
0
    fn default() -> Self {
2101
        Self {
2102
0
            data: TableDataRef::new(FontData::default_table_data().as_bytes())
2103
0
                .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
2104
        }
2105
0
    }
2106
}
2107
2108
impl<'a> MinByteRange<'a> for NonDefaultUvs<'a> {
2109
0
    fn min_byte_range(&self) -> Range<usize> {
2110
0
        0..self.uvs_mapping_byte_range().end
2111
0
    }
2112
0
    fn min_table_bytes(&self) -> &'a [u8] {
2113
0
        let range = self.min_byte_range();
2114
0
        self.offset_data().as_bytes().get(range).unwrap_or_default()
2115
0
    }
2116
}
2117
2118
impl ReadArgs for NonDefaultUvs<'_> {
2119
    type Args = ();
2120
}
2121
2122
impl<'a> FontRead<'a> for NonDefaultUvs<'a> {
2123
0
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
2124
        #[allow(clippy::absurd_extreme_comparisons)]
2125
0
        if data.len() < Self::MIN_SIZE {
2126
0
            return Err(ReadError::OutOfBounds);
2127
0
        }
2128
0
        let Some(data) = TableDataRef::new(data.as_bytes()) else {
2129
0
            return Err(ReadError::OutOfBounds);
2130
        };
2131
0
        Ok(Self { data })
2132
0
    }
2133
}
2134
2135
/// The fields at the start of the table, which are read directly.
2136
#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
2137
#[repr(C, packed)]
2138
#[doc(hidden)]
2139
pub struct NonDefaultUvsFixedHeader {
2140
    pub num_uvs_mappings: BigEndian<u32>,
2141
}
2142
2143
#[allow(clippy::absurd_extreme_comparisons)]
2144
const _: () = assert!(std::mem::size_of::<NonDefaultUvsFixedHeader>() <= NonDefaultUvs::MIN_SIZE);
2145
2146
/// [Non-Default UVS table](https://learn.microsoft.com/en-us/typography/opentype/spec/cmap#non-default-uvs-table)
2147
#[derive(Clone)]
2148
pub struct NonDefaultUvs<'a> {
2149
    data: TableDataRef<'a, NonDefaultUvsFixedHeader>,
2150
}
2151
2152
#[allow(clippy::needless_lifetimes)]
2153
impl<'a> NonDefaultUvs<'a> {
2154
    pub const MIN_SIZE: usize = u32::RAW_BYTE_LEN;
2155
    basic_table_impls!(impl_the_methods);
2156
2157
0
    pub fn num_uvs_mappings(&self) -> u32 {
2158
0
        self.data.header().num_uvs_mappings.get()
2159
0
    }
2160
2161
0
    pub fn uvs_mapping(&self) -> &'a [UvsMapping] {
2162
0
        let range = self.uvs_mapping_byte_range();
2163
0
        self.offset_data()
2164
0
            .read_array(range)
2165
0
            .ok()
2166
0
            .unwrap_or_default()
2167
0
    }
2168
2169
0
    pub fn num_uvs_mappings_byte_range(&self) -> Range<usize> {
2170
0
        let start = 0usize;
2171
0
        let end = start.saturating_add(u32::RAW_BYTE_LEN);
2172
0
        start..end
2173
0
    }
2174
2175
0
    pub fn uvs_mapping_byte_range(&self) -> Range<usize> {
2176
0
        let num_uvs_mappings = self.num_uvs_mappings();
2177
0
        let start = self.num_uvs_mappings_byte_range().end;
2178
0
        let end = start.saturating_add(
2179
0
            (transforms::to_usize(num_uvs_mappings)).saturating_mul(UvsMapping::RAW_BYTE_LEN),
2180
        );
2181
0
        start..end
2182
0
    }
2183
}
2184
2185
const _: () = assert!(FontData::default_data_long_enough(NonDefaultUvs::MIN_SIZE));
2186
2187
impl Default for NonDefaultUvs<'_> {
2188
0
    fn default() -> Self {
2189
        Self {
2190
0
            data: TableDataRef::new(FontData::default_table_data().as_bytes())
2191
0
                .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
2192
        }
2193
0
    }
2194
}
2195
2196
impl<'a> MinByteRange<'a> for NonDefaultUvs24<'a> {
2197
0
    fn min_byte_range(&self) -> Range<usize> {
2198
0
        0..self.uvs_mapping_byte_range().end
2199
0
    }
2200
0
    fn min_table_bytes(&self) -> &'a [u8] {
2201
0
        let range = self.min_byte_range();
2202
0
        self.offset_data().as_bytes().get(range).unwrap_or_default()
2203
0
    }
2204
}
2205
2206
impl ReadArgs for NonDefaultUvs24<'_> {
2207
    type Args = ();
2208
}
2209
2210
impl<'a> FontRead<'a> for NonDefaultUvs24<'a> {
2211
0
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
2212
        #[allow(clippy::absurd_extreme_comparisons)]
2213
0
        if data.len() < Self::MIN_SIZE {
2214
0
            return Err(ReadError::OutOfBounds);
2215
0
        }
2216
0
        let Some(data) = TableDataRef::new(data.as_bytes()) else {
2217
0
            return Err(ReadError::OutOfBounds);
2218
        };
2219
0
        Ok(Self { data })
2220
0
    }
2221
}
2222
2223
/// The fields at the start of the table, which are read directly.
2224
#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
2225
#[repr(C, packed)]
2226
#[doc(hidden)]
2227
pub struct NonDefaultUvs24FixedHeader {
2228
    pub num_uvs_mappings: BigEndian<u32>,
2229
}
2230
2231
#[allow(clippy::absurd_extreme_comparisons)]
2232
const _: () =
2233
    assert!(std::mem::size_of::<NonDefaultUvs24FixedHeader>() <= NonDefaultUvs24::MIN_SIZE);
2234
2235
/// Non-default UVS mappings with 24-bit glyph IDs, used by [Cmap15].
2236
#[derive(Clone)]
2237
pub struct NonDefaultUvs24<'a> {
2238
    data: TableDataRef<'a, NonDefaultUvs24FixedHeader>,
2239
}
2240
2241
#[allow(clippy::needless_lifetimes)]
2242
impl<'a> NonDefaultUvs24<'a> {
2243
    pub const MIN_SIZE: usize = u32::RAW_BYTE_LEN;
2244
    basic_table_impls!(impl_the_methods);
2245
2246
0
    pub fn num_uvs_mappings(&self) -> u32 {
2247
0
        self.data.header().num_uvs_mappings.get()
2248
0
    }
2249
2250
0
    pub fn uvs_mapping(&self) -> &'a [UvsMapping24] {
2251
0
        let range = self.uvs_mapping_byte_range();
2252
0
        self.offset_data()
2253
0
            .read_array(range)
2254
0
            .ok()
2255
0
            .unwrap_or_default()
2256
0
    }
2257
2258
0
    pub fn num_uvs_mappings_byte_range(&self) -> Range<usize> {
2259
0
        let start = 0usize;
2260
0
        let end = start.saturating_add(u32::RAW_BYTE_LEN);
2261
0
        start..end
2262
0
    }
2263
2264
0
    pub fn uvs_mapping_byte_range(&self) -> Range<usize> {
2265
0
        let num_uvs_mappings = self.num_uvs_mappings();
2266
0
        let start = self.num_uvs_mappings_byte_range().end;
2267
0
        let end = start.saturating_add(
2268
0
            (transforms::to_usize(num_uvs_mappings)).saturating_mul(UvsMapping24::RAW_BYTE_LEN),
2269
        );
2270
0
        start..end
2271
0
    }
2272
}
2273
2274
const _: () = assert!(FontData::default_data_long_enough(
2275
    NonDefaultUvs24::MIN_SIZE
2276
));
2277
2278
impl Default for NonDefaultUvs24<'_> {
2279
0
    fn default() -> Self {
2280
        Self {
2281
0
            data: TableDataRef::new(FontData::default_table_data().as_bytes())
2282
0
                .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
2283
        }
2284
0
    }
2285
}
2286
2287
/// UVSMapping24 record (ISO/IEC 14496-22:2026, 5.1.2.5.11).
2288
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, bytemuck :: AnyBitPattern)]
2289
#[repr(C)]
2290
#[repr(packed)]
2291
pub struct UvsMapping24 {
2292
    pub unicode_value: BigEndian<Uint24>,
2293
    pub glyph_id: BigEndian<Uint24>,
2294
}
2295
2296
impl UvsMapping24 {
2297
0
    pub fn unicode_value(&self) -> Uint24 {
2298
0
        self.unicode_value.get()
2299
0
    }
2300
2301
0
    pub fn glyph_id(&self) -> Uint24 {
2302
0
        self.glyph_id.get()
2303
0
    }
2304
}
2305
2306
impl FixedSize for UvsMapping24 {
2307
    const RAW_BYTE_LEN: usize = Uint24::RAW_BYTE_LEN + Uint24::RAW_BYTE_LEN;
2308
}
2309
2310
/// Part of [Cmap14]
2311
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, bytemuck :: AnyBitPattern)]
2312
#[repr(C)]
2313
#[repr(packed)]
2314
pub struct UvsMapping {
2315
    /// Base Unicode value of the UVS
2316
    pub unicode_value: BigEndian<Uint24>,
2317
    /// Glyph ID of the UVS
2318
    pub glyph_id: BigEndian<u16>,
2319
}
2320
2321
impl UvsMapping {
2322
    /// Base Unicode value of the UVS
2323
0
    pub fn unicode_value(&self) -> Uint24 {
2324
0
        self.unicode_value.get()
2325
0
    }
2326
2327
    /// Glyph ID of the UVS
2328
0
    pub fn glyph_id(&self) -> u16 {
2329
0
        self.glyph_id.get()
2330
0
    }
2331
}
2332
2333
impl FixedSize for UvsMapping {
2334
    const RAW_BYTE_LEN: usize = Uint24::RAW_BYTE_LEN + u16::RAW_BYTE_LEN;
2335
}
2336
2337
/// Part of [Cmap14]
2338
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, bytemuck :: AnyBitPattern)]
2339
#[repr(C)]
2340
#[repr(packed)]
2341
pub struct UnicodeRange {
2342
    /// First value in this range
2343
    pub start_unicode_value: BigEndian<Uint24>,
2344
    /// Number of additional values in this range
2345
    pub additional_count: u8,
2346
}
2347
2348
impl UnicodeRange {
2349
    /// First value in this range
2350
137
    pub fn start_unicode_value(&self) -> Uint24 {
2351
137
        self.start_unicode_value.get()
2352
137
    }
2353
2354
    /// Number of additional values in this range
2355
60
    pub fn additional_count(&self) -> u8 {
2356
60
        self.additional_count
2357
60
    }
2358
}
2359
2360
impl FixedSize for UnicodeRange {
2361
    const RAW_BYTE_LEN: usize = Uint24::RAW_BYTE_LEN + u8::RAW_BYTE_LEN;
2362
}