Skip to main content

read_fonts/generated/
generated_cvar.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 Cvar<'a> {
9    fn min_byte_range(&self) -> Range<usize> {
10        0..self.tuple_variation_headers_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 TopLevelTable for Cvar<'_> {
19    /// `cvar`
20    const TAG: Tag = Tag::new(b"cvar");
21}
22
23impl ReadArgs for Cvar<'_> {
24    type Args = ();
25}
26
27impl<'a> FontRead<'a> for Cvar<'a> {
28    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
29        #[allow(clippy::absurd_extreme_comparisons)]
30        if data.len() < Self::MIN_SIZE {
31            return Err(ReadError::OutOfBounds);
32        }
33        let Some(data) = TableDataRef::new(data.as_bytes()) else {
34            return Err(ReadError::OutOfBounds);
35        };
36        Ok(Self { data })
37    }
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)]
44pub struct CvarFixedHeader {
45    pub version: BigEndian<MajorMinor>,
46    pub tuple_variation_count: BigEndian<TupleVariationCount>,
47    pub data_offset: BigEndian<Offset16>,
48}
49
50#[allow(clippy::absurd_extreme_comparisons)]
51const _: () = assert!(std::mem::size_of::<CvarFixedHeader>() <= Cvar::MIN_SIZE);
52
53/// The [cvar](https://learn.microsoft.com/en-us/typography/opentype/spec/cvar) table.
54#[derive(Clone)]
55pub struct Cvar<'a> {
56    data: TableDataRef<'a, CvarFixedHeader>,
57}
58
59#[allow(clippy::needless_lifetimes)]
60impl<'a> Cvar<'a> {
61    pub const MIN_SIZE: usize =
62        (MajorMinor::RAW_BYTE_LEN + TupleVariationCount::RAW_BYTE_LEN + Offset16::RAW_BYTE_LEN);
63    basic_table_impls!(impl_the_methods);
64
65    /// Major/minor version number of the CVT variations table — set to (1,0).
66    pub fn version(&self) -> MajorMinor {
67        self.data.header().version.get()
68    }
69
70    /// A packed field. The high 4 bits are flags, and the low 12 bits
71    /// are the number of tuple variation tables for this glyph. The
72    /// number of tuple variation tables can be any number between 1
73    /// and 4095.
74    pub fn tuple_variation_count(&self) -> TupleVariationCount {
75        self.data.header().tuple_variation_count.get()
76    }
77
78    /// Offset from the start of the 'cvar' table to the serialized data.
79    pub fn data_offset(&self) -> Offset16 {
80        self.data.header().data_offset.get()
81    }
82
83    /// Attempt to resolve [`data_offset`][Self::data_offset].
84    pub fn data(&self) -> Result<FontData<'a>, ReadError> {
85        let data = self.offset_data();
86        self.data_offset().resolve(data)
87    }
88
89    /// Array of tuple variation headers.
90    pub fn tuple_variation_headers(&self) -> VarLenArray<'a, TupleVariationHeader<'a>> {
91        let range = self.tuple_variation_headers_byte_range();
92        self.offset_data()
93            .split_off(range.start)
94            .and_then(|d| VarLenArray::read(d).ok())
95            .unwrap_or_default()
96    }
97
98    pub fn version_byte_range(&self) -> Range<usize> {
99        let start = 0;
100        let end = start + MajorMinor::RAW_BYTE_LEN;
101        start..end
102    }
103
104    pub fn tuple_variation_count_byte_range(&self) -> Range<usize> {
105        let start = self.version_byte_range().end;
106        let end = start + TupleVariationCount::RAW_BYTE_LEN;
107        start..end
108    }
109
110    pub fn data_offset_byte_range(&self) -> Range<usize> {
111        let start = self.tuple_variation_count_byte_range().end;
112        let end = start + Offset16::RAW_BYTE_LEN;
113        start..end
114    }
115
116    pub fn tuple_variation_headers_byte_range(&self) -> Range<usize> {
117        let start = self.data_offset_byte_range().end;
118        let end = start + self.offset_data().len().saturating_sub(start);
119        start..end
120    }
121}
122
123const _: () = assert!(FontData::default_data_long_enough(Cvar::MIN_SIZE));
124
125impl Default for Cvar<'_> {
126    fn default() -> Self {
127        Self {
128            data: TableDataRef::new(FontData::default_table_data().as_bytes())
129                .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
130        }
131    }
132}