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_cff.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 CffHeader<'a> {
9
0
    fn min_byte_range(&self) -> Range<usize> {
10
0
        0..self.trailing_data_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 ReadArgs for CffHeader<'_> {
19
    type Args = ();
20
}
21
22
impl<'a> FontRead<'a> for CffHeader<'a> {
23
33.1k
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
24
        #[allow(clippy::absurd_extreme_comparisons)]
25
33.1k
        if data.len() < Self::MIN_SIZE {
26
5
            return Err(ReadError::OutOfBounds);
27
33.1k
        }
28
33.1k
        let Some(data) = TableDataRef::new(data.as_bytes()) else {
29
0
            return Err(ReadError::OutOfBounds);
30
        };
31
33.1k
        Ok(Self { data })
32
33.1k
    }
33
}
34
35
/// The fields at the start of the table, which are read directly.
36
#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
37
#[repr(C, packed)]
38
#[doc(hidden)]
39
pub struct CffHeaderFixedHeader {
40
    pub major: u8,
41
    pub minor: u8,
42
    pub hdr_size: u8,
43
    pub off_size: u8,
44
}
45
46
#[allow(clippy::absurd_extreme_comparisons)]
47
const _: () = assert!(std::mem::size_of::<CffHeaderFixedHeader>() <= CffHeader::MIN_SIZE);
48
49
/// [Compact Font Format](https://learn.microsoft.com/en-us/typography/opentype/spec/cff) table header
50
#[derive(Clone)]
51
pub struct CffHeader<'a> {
52
    data: TableDataRef<'a, CffHeaderFixedHeader>,
53
}
54
55
#[allow(clippy::needless_lifetimes)]
56
impl<'a> CffHeader<'a> {
57
    pub const MIN_SIZE: usize =
58
        (u8::RAW_BYTE_LEN + u8::RAW_BYTE_LEN + u8::RAW_BYTE_LEN + u8::RAW_BYTE_LEN);
59
    basic_table_impls!(impl_the_methods);
60
61
    /// Format major version (starting at 1).
62
0
    pub fn major(&self) -> u8 {
63
0
        self.data.header().major
64
0
    }
65
66
    /// Format minor version (starting at 0).
67
0
    pub fn minor(&self) -> u8 {
68
0
        self.data.header().minor
69
0
    }
70
71
    /// Header size (bytes).
72
33.1k
    pub fn hdr_size(&self) -> u8 {
73
33.1k
        self.data.header().hdr_size
74
33.1k
    }
75
76
    /// Absolute offset size.
77
0
    pub fn off_size(&self) -> u8 {
78
0
        self.data.header().off_size
79
0
    }
80
81
    /// Padding bytes before the start of the Name INDEX.
82
0
    pub fn _padding(&self) -> &'a [u8] {
83
0
        let range = self._padding_byte_range();
84
0
        self.offset_data()
85
0
            .read_array(range)
86
0
            .ok()
87
0
            .unwrap_or_default()
88
0
    }
89
90
    /// Remaining table data.
91
33.1k
    pub fn trailing_data(&self) -> &'a [u8] {
92
33.1k
        let range = self.trailing_data_byte_range();
93
33.1k
        self.offset_data()
94
33.1k
            .read_array(range)
95
33.1k
            .ok()
96
33.1k
            .unwrap_or_default()
97
33.1k
    }
98
99
33.1k
    pub fn major_byte_range(&self) -> Range<usize> {
100
33.1k
        let start = 0usize;
101
33.1k
        let end = start.saturating_add(u8::RAW_BYTE_LEN);
102
33.1k
        start..end
103
33.1k
    }
104
105
33.1k
    pub fn minor_byte_range(&self) -> Range<usize> {
106
33.1k
        let start = self.major_byte_range().end;
107
33.1k
        let end = start.saturating_add(u8::RAW_BYTE_LEN);
108
33.1k
        start..end
109
33.1k
    }
110
111
33.1k
    pub fn hdr_size_byte_range(&self) -> Range<usize> {
112
33.1k
        let start = self.minor_byte_range().end;
113
33.1k
        let end = start.saturating_add(u8::RAW_BYTE_LEN);
114
33.1k
        start..end
115
33.1k
    }
116
117
33.1k
    pub fn off_size_byte_range(&self) -> Range<usize> {
118
33.1k
        let start = self.hdr_size_byte_range().end;
119
33.1k
        let end = start.saturating_add(u8::RAW_BYTE_LEN);
120
33.1k
        start..end
121
33.1k
    }
122
123
33.1k
    pub fn _padding_byte_range(&self) -> Range<usize> {
124
33.1k
        let hdr_size = self.hdr_size();
125
33.1k
        let start = self.off_size_byte_range().end;
126
33.1k
        let end = start.saturating_add(
127
33.1k
            (transforms::subtract(hdr_size, 4_usize)).saturating_mul(u8::RAW_BYTE_LEN),
128
        );
129
33.1k
        start..end
130
33.1k
    }
131
132
33.1k
    pub fn trailing_data_byte_range(&self) -> Range<usize> {
133
33.1k
        let start = self._padding_byte_range().end;
134
33.1k
        let end = start.saturating_add(
135
33.1k
            self.offset_data().len().saturating_sub(start) / u8::RAW_BYTE_LEN * u8::RAW_BYTE_LEN,
136
        );
137
33.1k
        start..end
138
33.1k
    }
139
}
140
141
const _: () = assert!(FontData::default_data_long_enough(CffHeader::MIN_SIZE));
142
143
impl Default for CffHeader<'_> {
144
0
    fn default() -> Self {
145
        Self {
146
0
            data: TableDataRef::new(FontData::default_table_data().as_bytes())
147
0
                .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
148
        }
149
0
    }
150
}
151
152
impl<'a> MinByteRange<'a> for Index<'a> {
153
0
    fn min_byte_range(&self) -> Range<usize> {
154
0
        0..self.data_byte_range().end
155
0
    }
156
0
    fn min_table_bytes(&self) -> &'a [u8] {
157
0
        let range = self.min_byte_range();
158
0
        self.offset_data().as_bytes().get(range).unwrap_or_default()
159
0
    }
160
}
161
162
impl ReadArgs for Index<'_> {
163
    type Args = ();
164
}
165
166
impl<'a> FontRead<'a> for Index<'a> {
167
175k
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
168
        #[allow(clippy::absurd_extreme_comparisons)]
169
175k
        if data.len() < Self::MIN_SIZE {
170
403
            return Err(ReadError::OutOfBounds);
171
175k
        }
172
175k
        let Some(data) = TableDataRef::new(data.as_bytes()) else {
173
0
            return Err(ReadError::OutOfBounds);
174
        };
175
175k
        Ok(Self { data })
176
175k
    }
177
}
178
179
/// The fields at the start of the table, which are read directly.
180
#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
181
#[repr(C, packed)]
182
#[doc(hidden)]
183
pub struct IndexFixedHeader {
184
    pub count: BigEndian<u16>,
185
    pub off_size: u8,
186
}
187
188
#[allow(clippy::absurd_extreme_comparisons)]
189
const _: () = assert!(std::mem::size_of::<IndexFixedHeader>() <= Index::MIN_SIZE);
190
191
/// An array of variable-sized objects in a `CFF` table.
192
#[derive(Clone)]
193
pub struct Index<'a> {
194
    data: TableDataRef<'a, IndexFixedHeader>,
195
}
196
197
#[allow(clippy::needless_lifetimes)]
198
impl<'a> Index<'a> {
199
    pub const MIN_SIZE: usize = (u16::RAW_BYTE_LEN + u8::RAW_BYTE_LEN);
200
    basic_table_impls!(impl_the_methods);
201
202
    /// Number of objects stored in INDEX.
203
850k
    pub fn count(&self) -> u16 {
204
850k
        self.data.header().count.get()
205
850k
    }
206
207
    /// Object array element size.
208
685k
    pub fn off_size(&self) -> u8 {
209
685k
        self.data.header().off_size
210
685k
    }
211
212
    /// Bytes containing `count + 1` offsets each of `off_size`.
213
299k
    pub fn offsets(&self) -> &'a [u8] {
214
299k
        let range = self.offsets_byte_range();
215
299k
        self.offset_data()
216
299k
            .read_array(range)
217
299k
            .ok()
218
299k
            .unwrap_or_default()
219
299k
    }
220
221
    /// Array containing the object data.
222
120k
    pub fn data(&self) -> &'a [u8] {
223
120k
        let range = self.data_byte_range();
224
120k
        self.offset_data()
225
120k
            .read_array(range)
226
120k
            .ok()
227
120k
            .unwrap_or_default()
228
120k
    }
229
230
419k
    pub fn count_byte_range(&self) -> Range<usize> {
231
419k
        let start = 0usize;
232
419k
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
233
419k
        start..end
234
419k
    }
235
236
419k
    pub fn off_size_byte_range(&self) -> Range<usize> {
237
419k
        let start = self.count_byte_range().end;
238
419k
        let end = start.saturating_add(u8::RAW_BYTE_LEN);
239
419k
        start..end
240
419k
    }
241
242
419k
    pub fn offsets_byte_range(&self) -> Range<usize> {
243
419k
        let count = self.count();
244
419k
        let off_size = self.off_size();
245
419k
        let start = self.off_size_byte_range().end;
246
419k
        let end = start.saturating_add(
247
419k
            (transforms::add_multiply(count, 1_usize, off_size)).saturating_mul(u8::RAW_BYTE_LEN),
248
        );
249
419k
        start..end
250
419k
    }
251
252
120k
    pub fn data_byte_range(&self) -> Range<usize> {
253
120k
        let start = self.offsets_byte_range().end;
254
120k
        let end = start.saturating_add(
255
120k
            self.offset_data().len().saturating_sub(start) / u8::RAW_BYTE_LEN * u8::RAW_BYTE_LEN,
256
        );
257
120k
        start..end
258
120k
    }
259
}
260
261
const _: () = assert!(FontData::default_data_long_enough(Index::MIN_SIZE));
262
263
impl Default for Index<'_> {
264
0
    fn default() -> Self {
265
        Self {
266
0
            data: TableDataRef::new(FontData::default_table_data().as_bytes())
267
0
                .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
268
        }
269
0
    }
270
}
271
272
/// Associates a glyph identifier with a Font DICT.
273
#[derive(Clone)]
274
pub enum FdSelect<'a> {
275
    Format0(FdSelectFormat0<'a>),
276
    Format3(FdSelectFormat3<'a>),
277
    Format4(FdSelectFormat4<'a>),
278
}
279
280
impl Default for FdSelect<'_> {
281
0
    fn default() -> Self {
282
0
        Self::Format0(Default::default())
283
0
    }
284
}
285
286
impl<'a> FdSelect<'a> {
287
    ///Return the `FontData` used to resolve offsets for this table.
288
0
    pub fn offset_data(&self) -> FontData<'a> {
289
0
        match self {
290
0
            Self::Format0(item) => item.offset_data(),
291
0
            Self::Format3(item) => item.offset_data(),
292
0
            Self::Format4(item) => item.offset_data(),
293
        }
294
0
    }
295
296
    /// Format = 0.
297
0
    pub fn format(&self) -> u8 {
298
0
        match self {
299
0
            Self::Format0(item) => item.format(),
300
0
            Self::Format3(item) => item.format(),
301
0
            Self::Format4(item) => item.format(),
302
        }
303
0
    }
304
}
305
306
impl ReadArgs for FdSelect<'_> {
307
    type Args = ();
308
}
309
310
impl<'a> FontRead<'a> for FdSelect<'a> {
311
    #[inline]
312
373
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
313
373
        let format: u8 = data.read_at(0usize)?;
314
362
        match format {
315
105
            FdSelectFormat0::FORMAT => Ok(Self::Format0(FontRead::read(data)?)),
316
71
            FdSelectFormat3::FORMAT => Ok(Self::Format3(FontRead::read(data)?)),
317
93
            FdSelectFormat4::FORMAT => Ok(Self::Format4(FontRead::read(data)?)),
318
93
            other => Err(ReadError::InvalidFormat(other.into())),
319
        }
320
373
    }
321
}
322
323
impl<'a> MinByteRange<'a> for FdSelect<'a> {
324
0
    fn min_byte_range(&self) -> Range<usize> {
325
0
        match self {
326
0
            Self::Format0(item) => item.min_byte_range(),
327
0
            Self::Format3(item) => item.min_byte_range(),
328
0
            Self::Format4(item) => item.min_byte_range(),
329
        }
330
0
    }
331
0
    fn min_table_bytes(&self) -> &'a [u8] {
332
0
        match self {
333
0
            Self::Format0(item) => item.min_table_bytes(),
334
0
            Self::Format3(item) => item.min_table_bytes(),
335
0
            Self::Format4(item) => item.min_table_bytes(),
336
        }
337
0
    }
338
}
339
340
impl Format<u8> for FdSelectFormat0<'_> {
341
    const FORMAT: u8 = 0;
342
}
343
344
impl<'a> MinByteRange<'a> for FdSelectFormat0<'a> {
345
0
    fn min_byte_range(&self) -> Range<usize> {
346
0
        0..self.fds_byte_range().end
347
0
    }
348
0
    fn min_table_bytes(&self) -> &'a [u8] {
349
0
        let range = self.min_byte_range();
350
0
        self.offset_data().as_bytes().get(range).unwrap_or_default()
351
0
    }
352
}
353
354
impl ReadArgs for FdSelectFormat0<'_> {
355
    type Args = ();
356
}
357
358
impl<'a> FontRead<'a> for FdSelectFormat0<'a> {
359
105
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
360
        #[allow(clippy::absurd_extreme_comparisons)]
361
105
        if data.len() < Self::MIN_SIZE {
362
0
            return Err(ReadError::OutOfBounds);
363
105
        }
364
105
        let Some(data) = TableDataRef::new(data.as_bytes()) else {
365
0
            return Err(ReadError::OutOfBounds);
366
        };
367
105
        Ok(Self { data })
368
105
    }
369
}
370
371
/// The fields at the start of the table, which are read directly.
372
#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
373
#[repr(C, packed)]
374
#[doc(hidden)]
375
pub struct FdSelectFormat0FixedHeader {
376
    pub format: u8,
377
}
378
379
#[allow(clippy::absurd_extreme_comparisons)]
380
const _: () =
381
    assert!(std::mem::size_of::<FdSelectFormat0FixedHeader>() <= FdSelectFormat0::MIN_SIZE);
382
383
/// FdSelect format 0.
384
#[derive(Clone)]
385
pub struct FdSelectFormat0<'a> {
386
    data: TableDataRef<'a, FdSelectFormat0FixedHeader>,
387
}
388
389
#[allow(clippy::needless_lifetimes)]
390
impl<'a> FdSelectFormat0<'a> {
391
    pub const MIN_SIZE: usize = u8::RAW_BYTE_LEN;
392
    basic_table_impls!(impl_the_methods);
393
394
    /// Format = 0.
395
0
    pub fn format(&self) -> u8 {
396
0
        self.data.header().format
397
0
    }
398
399
    /// FD selector array (one entry for each glyph).
400
525
    pub fn fds(&self) -> &'a [u8] {
401
525
        let range = self.fds_byte_range();
402
525
        self.offset_data()
403
525
            .read_array(range)
404
525
            .ok()
405
525
            .unwrap_or_default()
406
525
    }
407
408
525
    pub fn format_byte_range(&self) -> Range<usize> {
409
525
        let start = 0usize;
410
525
        let end = start.saturating_add(u8::RAW_BYTE_LEN);
411
525
        start..end
412
525
    }
413
414
525
    pub fn fds_byte_range(&self) -> Range<usize> {
415
525
        let start = self.format_byte_range().end;
416
525
        let end = start.saturating_add(
417
525
            self.offset_data().len().saturating_sub(start) / u8::RAW_BYTE_LEN * u8::RAW_BYTE_LEN,
418
        );
419
525
        start..end
420
525
    }
421
}
422
423
const _: () = assert!(FontData::default_data_long_enough(
424
    FdSelectFormat0::MIN_SIZE
425
));
426
427
impl Default for FdSelectFormat0<'_> {
428
0
    fn default() -> Self {
429
        Self {
430
0
            data: TableDataRef::new(FontData::default_table_data().as_bytes())
431
0
                .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
432
        }
433
0
    }
434
}
435
436
impl Format<u8> for FdSelectFormat3<'_> {
437
    const FORMAT: u8 = 3;
438
}
439
440
impl<'a> MinByteRange<'a> for FdSelectFormat3<'a> {
441
0
    fn min_byte_range(&self) -> Range<usize> {
442
0
        0..self.sentinel_byte_range().end
443
0
    }
444
0
    fn min_table_bytes(&self) -> &'a [u8] {
445
0
        let range = self.min_byte_range();
446
0
        self.offset_data().as_bytes().get(range).unwrap_or_default()
447
0
    }
448
}
449
450
impl ReadArgs for FdSelectFormat3<'_> {
451
    type Args = ();
452
}
453
454
impl<'a> FontRead<'a> for FdSelectFormat3<'a> {
455
71
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
456
        #[allow(clippy::absurd_extreme_comparisons)]
457
71
        if data.len() < Self::MIN_SIZE {
458
11
            return Err(ReadError::OutOfBounds);
459
60
        }
460
60
        let Some(data) = TableDataRef::new(data.as_bytes()) else {
461
0
            return Err(ReadError::OutOfBounds);
462
        };
463
60
        Ok(Self { data })
464
71
    }
465
}
466
467
/// The fields at the start of the table, which are read directly.
468
#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
469
#[repr(C, packed)]
470
#[doc(hidden)]
471
pub struct FdSelectFormat3FixedHeader {
472
    pub format: u8,
473
    pub n_ranges: BigEndian<u16>,
474
}
475
476
#[allow(clippy::absurd_extreme_comparisons)]
477
const _: () =
478
    assert!(std::mem::size_of::<FdSelectFormat3FixedHeader>() <= FdSelectFormat3::MIN_SIZE);
479
480
/// FdSelect format 3.
481
#[derive(Clone)]
482
pub struct FdSelectFormat3<'a> {
483
    data: TableDataRef<'a, FdSelectFormat3FixedHeader>,
484
}
485
486
#[allow(clippy::needless_lifetimes)]
487
impl<'a> FdSelectFormat3<'a> {
488
    pub const MIN_SIZE: usize = (u8::RAW_BYTE_LEN + u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN);
489
    basic_table_impls!(impl_the_methods);
490
491
    /// Format = 3.
492
0
    pub fn format(&self) -> u8 {
493
0
        self.data.header().format
494
0
    }
495
496
    /// Number of ranges.
497
254
    pub fn n_ranges(&self) -> u16 {
498
254
        self.data.header().n_ranges.get()
499
254
    }
500
501
    /// Range3 array.
502
138
    pub fn ranges(&self) -> &'a [FdSelectRange3] {
503
138
        let range = self.ranges_byte_range();
504
138
        self.offset_data()
505
138
            .read_array(range)
506
138
            .ok()
507
138
            .unwrap_or_default()
508
138
    }
509
510
    /// Sentinel GID. Set equal to the number of glyphs in the font.
511
116
    pub fn sentinel(&self) -> u16 {
512
116
        let range = self.sentinel_byte_range();
513
116
        self.offset_data()
514
116
            .read_at(range.start)
515
116
            .ok()
516
116
            .unwrap_or_default()
517
116
    }
518
519
254
    pub fn format_byte_range(&self) -> Range<usize> {
520
254
        let start = 0usize;
521
254
        let end = start.saturating_add(u8::RAW_BYTE_LEN);
522
254
        start..end
523
254
    }
524
525
254
    pub fn n_ranges_byte_range(&self) -> Range<usize> {
526
254
        let start = self.format_byte_range().end;
527
254
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
528
254
        start..end
529
254
    }
530
531
254
    pub fn ranges_byte_range(&self) -> Range<usize> {
532
254
        let n_ranges = self.n_ranges();
533
254
        let start = self.n_ranges_byte_range().end;
534
254
        let end = start.saturating_add(
535
254
            (transforms::to_usize(n_ranges)).saturating_mul(FdSelectRange3::RAW_BYTE_LEN),
536
        );
537
254
        start..end
538
254
    }
539
540
116
    pub fn sentinel_byte_range(&self) -> Range<usize> {
541
116
        let start = self.ranges_byte_range().end;
542
116
        let end = start.saturating_add(u16::RAW_BYTE_LEN);
543
116
        start..end
544
116
    }
545
}
546
547
/// Range struct for FdSelect format 3.
548
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, bytemuck :: AnyBitPattern)]
549
#[repr(C)]
550
#[repr(packed)]
551
pub struct FdSelectRange3 {
552
    /// First glyph index in range.
553
    pub first: BigEndian<u16>,
554
    /// FD index for all glyphs in range.
555
    pub fd: u8,
556
}
557
558
impl FdSelectRange3 {
559
    /// First glyph index in range.
560
781
    pub fn first(&self) -> u16 {
561
781
        self.first.get()
562
781
    }
563
564
    /// FD index for all glyphs in range.
565
22
    pub fn fd(&self) -> u8 {
566
22
        self.fd
567
22
    }
568
}
569
570
impl FixedSize for FdSelectRange3 {
571
    const RAW_BYTE_LEN: usize = u16::RAW_BYTE_LEN + u8::RAW_BYTE_LEN;
572
}
573
574
impl Format<u8> for FdSelectFormat4<'_> {
575
    const FORMAT: u8 = 4;
576
}
577
578
impl<'a> MinByteRange<'a> for FdSelectFormat4<'a> {
579
0
    fn min_byte_range(&self) -> Range<usize> {
580
0
        0..self.sentinel_byte_range().end
581
0
    }
582
0
    fn min_table_bytes(&self) -> &'a [u8] {
583
0
        let range = self.min_byte_range();
584
0
        self.offset_data().as_bytes().get(range).unwrap_or_default()
585
0
    }
586
}
587
588
impl ReadArgs for FdSelectFormat4<'_> {
589
    type Args = ();
590
}
591
592
impl<'a> FontRead<'a> for FdSelectFormat4<'a> {
593
93
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
594
        #[allow(clippy::absurd_extreme_comparisons)]
595
93
        if data.len() < Self::MIN_SIZE {
596
10
            return Err(ReadError::OutOfBounds);
597
83
        }
598
83
        let Some(data) = TableDataRef::new(data.as_bytes()) else {
599
0
            return Err(ReadError::OutOfBounds);
600
        };
601
83
        Ok(Self { data })
602
93
    }
603
}
604
605
/// The fields at the start of the table, which are read directly.
606
#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
607
#[repr(C, packed)]
608
#[doc(hidden)]
609
pub struct FdSelectFormat4FixedHeader {
610
    pub format: u8,
611
    pub n_ranges: BigEndian<u32>,
612
}
613
614
#[allow(clippy::absurd_extreme_comparisons)]
615
const _: () =
616
    assert!(std::mem::size_of::<FdSelectFormat4FixedHeader>() <= FdSelectFormat4::MIN_SIZE);
617
618
/// FdSelect format 4.
619
#[derive(Clone)]
620
pub struct FdSelectFormat4<'a> {
621
    data: TableDataRef<'a, FdSelectFormat4FixedHeader>,
622
}
623
624
#[allow(clippy::needless_lifetimes)]
625
impl<'a> FdSelectFormat4<'a> {
626
    pub const MIN_SIZE: usize = (u8::RAW_BYTE_LEN + u32::RAW_BYTE_LEN + u32::RAW_BYTE_LEN);
627
    basic_table_impls!(impl_the_methods);
628
629
    /// Format = 4.
630
0
    pub fn format(&self) -> u8 {
631
0
        self.data.header().format
632
0
    }
633
634
    /// Number of ranges.
635
283
    pub fn n_ranges(&self) -> u32 {
636
283
        self.data.header().n_ranges.get()
637
283
    }
638
639
    /// Range4 array.
640
161
    pub fn ranges(&self) -> &'a [FdSelectRange4] {
641
161
        let range = self.ranges_byte_range();
642
161
        self.offset_data()
643
161
            .read_array(range)
644
161
            .ok()
645
161
            .unwrap_or_default()
646
161
    }
647
648
    /// Sentinel GID. Set equal to the number of glyphs in the font.
649
122
    pub fn sentinel(&self) -> u32 {
650
122
        let range = self.sentinel_byte_range();
651
122
        self.offset_data()
652
122
            .read_at(range.start)
653
122
            .ok()
654
122
            .unwrap_or_default()
655
122
    }
656
657
283
    pub fn format_byte_range(&self) -> Range<usize> {
658
283
        let start = 0usize;
659
283
        let end = start.saturating_add(u8::RAW_BYTE_LEN);
660
283
        start..end
661
283
    }
662
663
283
    pub fn n_ranges_byte_range(&self) -> Range<usize> {
664
283
        let start = self.format_byte_range().end;
665
283
        let end = start.saturating_add(u32::RAW_BYTE_LEN);
666
283
        start..end
667
283
    }
668
669
283
    pub fn ranges_byte_range(&self) -> Range<usize> {
670
283
        let n_ranges = self.n_ranges();
671
283
        let start = self.n_ranges_byte_range().end;
672
283
        let end = start.saturating_add(
673
283
            (transforms::to_usize(n_ranges)).saturating_mul(FdSelectRange4::RAW_BYTE_LEN),
674
        );
675
283
        start..end
676
283
    }
677
678
122
    pub fn sentinel_byte_range(&self) -> Range<usize> {
679
122
        let start = self.ranges_byte_range().end;
680
122
        let end = start.saturating_add(u32::RAW_BYTE_LEN);
681
122
        start..end
682
122
    }
683
}
684
685
/// Range struct for FdSelect format 4.
686
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, bytemuck :: AnyBitPattern)]
687
#[repr(C)]
688
#[repr(packed)]
689
pub struct FdSelectRange4 {
690
    /// First glyph index in range.
691
    pub first: BigEndian<u32>,
692
    /// FD index for all glyphs in range.
693
    pub fd: BigEndian<u16>,
694
}
695
696
impl FdSelectRange4 {
697
    /// First glyph index in range.
698
667
    pub fn first(&self) -> u32 {
699
667
        self.first.get()
700
667
    }
701
702
    /// FD index for all glyphs in range.
703
51
    pub fn fd(&self) -> u16 {
704
51
        self.fd.get()
705
51
    }
706
}
707
708
impl FixedSize for FdSelectRange4 {
709
    const RAW_BYTE_LEN: usize = u32::RAW_BYTE_LEN + u16::RAW_BYTE_LEN;
710
}
711
712
/// Charset with custom glyph id to string id mappings.
713
#[derive(Clone)]
714
pub enum CustomCharset<'a> {
715
    Format0(CharsetFormat0<'a>),
716
    Format1(CharsetFormat1<'a>),
717
    Format2(CharsetFormat2<'a>),
718
}
719
720
impl Default for CustomCharset<'_> {
721
0
    fn default() -> Self {
722
0
        Self::Format0(Default::default())
723
0
    }
724
}
725
726
impl<'a> CustomCharset<'a> {
727
    ///Return the `FontData` used to resolve offsets for this table.
728
0
    pub fn offset_data(&self) -> FontData<'a> {
729
0
        match self {
730
0
            Self::Format0(item) => item.offset_data(),
731
0
            Self::Format1(item) => item.offset_data(),
732
0
            Self::Format2(item) => item.offset_data(),
733
        }
734
0
    }
735
736
    /// Format; =0
737
0
    pub fn format(&self) -> u8 {
738
0
        match self {
739
0
            Self::Format0(item) => item.format(),
740
0
            Self::Format1(item) => item.format(),
741
0
            Self::Format2(item) => item.format(),
742
        }
743
0
    }
744
}
745
746
impl ReadArgs for CustomCharset<'_> {
747
    type Args = ();
748
}
749
750
impl<'a> FontRead<'a> for CustomCharset<'a> {
751
    #[inline]
752
1.20k
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
753
1.20k
        let format: u8 = data.read_at(0usize)?;
754
1.16k
        match format {
755
340
            CharsetFormat0::FORMAT => Ok(Self::Format0(FontRead::read(data)?)),
756
169
            CharsetFormat1::FORMAT => Ok(Self::Format1(FontRead::read(data)?)),
757
241
            CharsetFormat2::FORMAT => Ok(Self::Format2(FontRead::read(data)?)),
758
417
            other => Err(ReadError::InvalidFormat(other.into())),
759
        }
760
1.20k
    }
761
}
762
763
impl<'a> MinByteRange<'a> for CustomCharset<'a> {
764
0
    fn min_byte_range(&self) -> Range<usize> {
765
0
        match self {
766
0
            Self::Format0(item) => item.min_byte_range(),
767
0
            Self::Format1(item) => item.min_byte_range(),
768
0
            Self::Format2(item) => item.min_byte_range(),
769
        }
770
0
    }
771
0
    fn min_table_bytes(&self) -> &'a [u8] {
772
0
        match self {
773
0
            Self::Format0(item) => item.min_table_bytes(),
774
0
            Self::Format1(item) => item.min_table_bytes(),
775
0
            Self::Format2(item) => item.min_table_bytes(),
776
        }
777
0
    }
778
}
779
780
impl Format<u8> for CharsetFormat0<'_> {
781
    const FORMAT: u8 = 0;
782
}
783
784
impl<'a> MinByteRange<'a> for CharsetFormat0<'a> {
785
0
    fn min_byte_range(&self) -> Range<usize> {
786
0
        0..self.glyph_byte_range().end
787
0
    }
788
0
    fn min_table_bytes(&self) -> &'a [u8] {
789
0
        let range = self.min_byte_range();
790
0
        self.offset_data().as_bytes().get(range).unwrap_or_default()
791
0
    }
792
}
793
794
impl ReadArgs for CharsetFormat0<'_> {
795
    type Args = ();
796
}
797
798
impl<'a> FontRead<'a> for CharsetFormat0<'a> {
799
340
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
800
        #[allow(clippy::absurd_extreme_comparisons)]
801
340
        if data.len() < Self::MIN_SIZE {
802
0
            return Err(ReadError::OutOfBounds);
803
340
        }
804
340
        let Some(data) = TableDataRef::new(data.as_bytes()) else {
805
0
            return Err(ReadError::OutOfBounds);
806
        };
807
340
        Ok(Self { data })
808
340
    }
809
}
810
811
/// The fields at the start of the table, which are read directly.
812
#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
813
#[repr(C, packed)]
814
#[doc(hidden)]
815
pub struct CharsetFormat0FixedHeader {
816
    pub format: u8,
817
}
818
819
#[allow(clippy::absurd_extreme_comparisons)]
820
const _: () = assert!(std::mem::size_of::<CharsetFormat0FixedHeader>() <= CharsetFormat0::MIN_SIZE);
821
822
/// Charset format 0.
823
#[derive(Clone)]
824
pub struct CharsetFormat0<'a> {
825
    data: TableDataRef<'a, CharsetFormat0FixedHeader>,
826
}
827
828
#[allow(clippy::needless_lifetimes)]
829
impl<'a> CharsetFormat0<'a> {
830
    pub const MIN_SIZE: usize = u8::RAW_BYTE_LEN;
831
    basic_table_impls!(impl_the_methods);
832
833
    /// Format; =0
834
0
    pub fn format(&self) -> u8 {
835
0
        self.data.header().format
836
0
    }
837
838
    /// Glyph name array.
839
268
    pub fn glyph(&self) -> &'a [BigEndian<u16>] {
840
268
        let range = self.glyph_byte_range();
841
268
        self.offset_data()
842
268
            .read_array(range)
843
268
            .ok()
844
268
            .unwrap_or_default()
845
268
    }
846
847
268
    pub fn format_byte_range(&self) -> Range<usize> {
848
268
        let start = 0usize;
849
268
        let end = start.saturating_add(u8::RAW_BYTE_LEN);
850
268
        start..end
851
268
    }
852
853
268
    pub fn glyph_byte_range(&self) -> Range<usize> {
854
268
        let start = self.format_byte_range().end;
855
268
        let end = start.saturating_add(
856
268
            self.offset_data().len().saturating_sub(start) / u16::RAW_BYTE_LEN * u16::RAW_BYTE_LEN,
857
        );
858
268
        start..end
859
268
    }
860
}
861
862
const _: () = assert!(FontData::default_data_long_enough(CharsetFormat0::MIN_SIZE));
863
864
impl Default for CharsetFormat0<'_> {
865
0
    fn default() -> Self {
866
        Self {
867
0
            data: TableDataRef::new(FontData::default_table_data().as_bytes())
868
0
                .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
869
        }
870
0
    }
871
}
872
873
impl Format<u8> for CharsetFormat1<'_> {
874
    const FORMAT: u8 = 1;
875
}
876
877
impl<'a> MinByteRange<'a> for CharsetFormat1<'a> {
878
0
    fn min_byte_range(&self) -> Range<usize> {
879
0
        0..self.ranges_byte_range().end
880
0
    }
881
0
    fn min_table_bytes(&self) -> &'a [u8] {
882
0
        let range = self.min_byte_range();
883
0
        self.offset_data().as_bytes().get(range).unwrap_or_default()
884
0
    }
885
}
886
887
impl ReadArgs for CharsetFormat1<'_> {
888
    type Args = ();
889
}
890
891
impl<'a> FontRead<'a> for CharsetFormat1<'a> {
892
169
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
893
        #[allow(clippy::absurd_extreme_comparisons)]
894
169
        if data.len() < Self::MIN_SIZE {
895
0
            return Err(ReadError::OutOfBounds);
896
169
        }
897
169
        let Some(data) = TableDataRef::new(data.as_bytes()) else {
898
0
            return Err(ReadError::OutOfBounds);
899
        };
900
169
        Ok(Self { data })
901
169
    }
902
}
903
904
/// The fields at the start of the table, which are read directly.
905
#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
906
#[repr(C, packed)]
907
#[doc(hidden)]
908
pub struct CharsetFormat1FixedHeader {
909
    pub format: u8,
910
}
911
912
#[allow(clippy::absurd_extreme_comparisons)]
913
const _: () = assert!(std::mem::size_of::<CharsetFormat1FixedHeader>() <= CharsetFormat1::MIN_SIZE);
914
915
/// Charset format 1.
916
#[derive(Clone)]
917
pub struct CharsetFormat1<'a> {
918
    data: TableDataRef<'a, CharsetFormat1FixedHeader>,
919
}
920
921
#[allow(clippy::needless_lifetimes)]
922
impl<'a> CharsetFormat1<'a> {
923
    pub const MIN_SIZE: usize = u8::RAW_BYTE_LEN;
924
    basic_table_impls!(impl_the_methods);
925
926
    /// Format; =1
927
0
    pub fn format(&self) -> u8 {
928
0
        self.data.header().format
929
0
    }
930
931
    /// Range1 array.
932
269
    pub fn ranges(&self) -> &'a [CharsetRange1] {
933
269
        let range = self.ranges_byte_range();
934
269
        self.offset_data()
935
269
            .read_array(range)
936
269
            .ok()
937
269
            .unwrap_or_default()
938
269
    }
939
940
269
    pub fn format_byte_range(&self) -> Range<usize> {
941
269
        let start = 0usize;
942
269
        let end = start.saturating_add(u8::RAW_BYTE_LEN);
943
269
        start..end
944
269
    }
945
946
269
    pub fn ranges_byte_range(&self) -> Range<usize> {
947
269
        let start = self.format_byte_range().end;
948
269
        let end = start.saturating_add(
949
269
            self.offset_data().len().saturating_sub(start) / CharsetRange1::RAW_BYTE_LEN
950
269
                * CharsetRange1::RAW_BYTE_LEN,
951
        );
952
269
        start..end
953
269
    }
954
}
955
956
const _: () = assert!(FontData::default_data_long_enough(CharsetFormat1::MIN_SIZE));
957
958
impl Default for CharsetFormat1<'_> {
959
0
    fn default() -> Self {
960
        Self {
961
0
            data: TableDataRef::new(FontData::default_format_1_u8_table_data().as_bytes())
962
0
                .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
963
        }
964
0
    }
965
}
966
967
/// Range struct for Charset format 1.
968
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, bytemuck :: AnyBitPattern)]
969
#[repr(C)]
970
#[repr(packed)]
971
pub struct CharsetRange1 {
972
    /// First glyph in range.
973
    pub first: BigEndian<u16>,
974
    /// Glyphs left in range (excluding first).
975
    pub n_left: u8,
976
}
977
978
impl CharsetRange1 {
979
    /// First glyph in range.
980
0
    pub fn first(&self) -> u16 {
981
0
        self.first.get()
982
0
    }
983
984
    /// Glyphs left in range (excluding first).
985
0
    pub fn n_left(&self) -> u8 {
986
0
        self.n_left
987
0
    }
988
}
989
990
impl FixedSize for CharsetRange1 {
991
    const RAW_BYTE_LEN: usize = u16::RAW_BYTE_LEN + u8::RAW_BYTE_LEN;
992
}
993
994
impl Format<u8> for CharsetFormat2<'_> {
995
    const FORMAT: u8 = 2;
996
}
997
998
impl<'a> MinByteRange<'a> for CharsetFormat2<'a> {
999
0
    fn min_byte_range(&self) -> Range<usize> {
1000
0
        0..self.ranges_byte_range().end
1001
0
    }
1002
0
    fn min_table_bytes(&self) -> &'a [u8] {
1003
0
        let range = self.min_byte_range();
1004
0
        self.offset_data().as_bytes().get(range).unwrap_or_default()
1005
0
    }
1006
}
1007
1008
impl ReadArgs for CharsetFormat2<'_> {
1009
    type Args = ();
1010
}
1011
1012
impl<'a> FontRead<'a> for CharsetFormat2<'a> {
1013
241
    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
1014
        #[allow(clippy::absurd_extreme_comparisons)]
1015
241
        if data.len() < Self::MIN_SIZE {
1016
0
            return Err(ReadError::OutOfBounds);
1017
241
        }
1018
241
        let Some(data) = TableDataRef::new(data.as_bytes()) else {
1019
0
            return Err(ReadError::OutOfBounds);
1020
        };
1021
241
        Ok(Self { data })
1022
241
    }
1023
}
1024
1025
/// The fields at the start of the table, which are read directly.
1026
#[derive(Clone, Copy, bytemuck :: AnyBitPattern)]
1027
#[repr(C, packed)]
1028
#[doc(hidden)]
1029
pub struct CharsetFormat2FixedHeader {
1030
    pub format: u8,
1031
}
1032
1033
#[allow(clippy::absurd_extreme_comparisons)]
1034
const _: () = assert!(std::mem::size_of::<CharsetFormat2FixedHeader>() <= CharsetFormat2::MIN_SIZE);
1035
1036
/// Charset format 2.
1037
#[derive(Clone)]
1038
pub struct CharsetFormat2<'a> {
1039
    data: TableDataRef<'a, CharsetFormat2FixedHeader>,
1040
}
1041
1042
#[allow(clippy::needless_lifetimes)]
1043
impl<'a> CharsetFormat2<'a> {
1044
    pub const MIN_SIZE: usize = u8::RAW_BYTE_LEN;
1045
    basic_table_impls!(impl_the_methods);
1046
1047
    /// Format; =2
1048
0
    pub fn format(&self) -> u8 {
1049
0
        self.data.header().format
1050
0
    }
1051
1052
    /// Range2 array.
1053
429
    pub fn ranges(&self) -> &'a [CharsetRange2] {
1054
429
        let range = self.ranges_byte_range();
1055
429
        self.offset_data()
1056
429
            .read_array(range)
1057
429
            .ok()
1058
429
            .unwrap_or_default()
1059
429
    }
1060
1061
429
    pub fn format_byte_range(&self) -> Range<usize> {
1062
429
        let start = 0usize;
1063
429
        let end = start.saturating_add(u8::RAW_BYTE_LEN);
1064
429
        start..end
1065
429
    }
1066
1067
429
    pub fn ranges_byte_range(&self) -> Range<usize> {
1068
429
        let start = self.format_byte_range().end;
1069
429
        let end = start.saturating_add(
1070
429
            self.offset_data().len().saturating_sub(start) / CharsetRange2::RAW_BYTE_LEN
1071
429
                * CharsetRange2::RAW_BYTE_LEN,
1072
        );
1073
429
        start..end
1074
429
    }
1075
}
1076
1077
/// Range struct for Charset format 2.
1078
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, bytemuck :: AnyBitPattern)]
1079
#[repr(C)]
1080
#[repr(packed)]
1081
pub struct CharsetRange2 {
1082
    /// First glyph in range.
1083
    pub first: BigEndian<u16>,
1084
    /// Glyphs left in range (excluding first).
1085
    pub n_left: BigEndian<u16>,
1086
}
1087
1088
impl CharsetRange2 {
1089
    /// First glyph in range.
1090
0
    pub fn first(&self) -> u16 {
1091
0
        self.first.get()
1092
0
    }
1093
1094
    /// Glyphs left in range (excluding first).
1095
0
    pub fn n_left(&self) -> u16 {
1096
0
        self.n_left.get()
1097
0
    }
1098
}
1099
1100
impl FixedSize for CharsetRange2 {
1101
    const RAW_BYTE_LEN: usize = u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN;
1102
}
1103
1104
/// Range struct for Encoding format 1.
1105
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, bytemuck :: AnyBitPattern)]
1106
#[repr(C)]
1107
#[repr(packed)]
1108
pub struct EncodingRange1 {
1109
    /// First code in range.
1110
    pub first: u8,
1111
    /// Codes left in range (excluding first).
1112
    pub n_left: u8,
1113
}
1114
1115
impl EncodingRange1 {
1116
    /// First code in range.
1117
0
    pub fn first(&self) -> u8 {
1118
0
        self.first
1119
0
    }
1120
1121
    /// Codes left in range (excluding first).
1122
0
    pub fn n_left(&self) -> u8 {
1123
0
        self.n_left
1124
0
    }
1125
}
1126
1127
impl FixedSize for EncodingRange1 {
1128
    const RAW_BYTE_LEN: usize = u8::RAW_BYTE_LEN + u8::RAW_BYTE_LEN;
1129
}
1130
1131
/// Supplemental encoding record.
1132
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, bytemuck :: AnyBitPattern)]
1133
#[repr(C)]
1134
#[repr(packed)]
1135
pub struct EncodingSupplement {
1136
    /// Encoding.
1137
    pub code: u8,
1138
    /// Name.
1139
    pub glyph: BigEndian<u16>,
1140
}
1141
1142
impl EncodingSupplement {
1143
    /// Encoding.
1144
0
    pub fn code(&self) -> u8 {
1145
0
        self.code
1146
0
    }
1147
1148
    /// Name.
1149
0
    pub fn glyph(&self) -> u16 {
1150
0
        self.glyph.get()
1151
0
    }
1152
}
1153
1154
impl FixedSize for EncodingSupplement {
1155
    const RAW_BYTE_LEN: usize = u8::RAW_BYTE_LEN + u16::RAW_BYTE_LEN;
1156
}