Skip to main content

read_fonts/generated/
generated_cff.rs

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