Skip to main content

read_fonts/generated/
generated_mvar.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 Mvar<'a> {
9    fn min_byte_range(&self) -> Range<usize> {
10        0..self.value_records_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 Mvar<'_> {
19    /// `MVAR`
20    const TAG: Tag = Tag::new(b"MVAR");
21}
22
23impl ReadArgs for Mvar<'_> {
24    type Args = ();
25}
26
27impl<'a> FontRead<'a> for Mvar<'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 MvarFixedHeader {
45    pub version: BigEndian<MajorMinor>,
46    pub _reserved: BigEndian<u16>,
47    pub value_record_size: BigEndian<u16>,
48    pub value_record_count: BigEndian<u16>,
49    pub item_variation_store_offset: BigEndian<Nullable<Offset16>>,
50}
51
52#[allow(clippy::absurd_extreme_comparisons)]
53const _: () = assert!(std::mem::size_of::<MvarFixedHeader>() <= Mvar::MIN_SIZE);
54
55/// The [MVAR (Metrics Variations)](https://docs.microsoft.com/en-us/typography/opentype/spec/mvar) table
56#[derive(Clone)]
57pub struct Mvar<'a> {
58    data: TableDataRef<'a, MvarFixedHeader>,
59}
60
61#[allow(clippy::needless_lifetimes)]
62impl<'a> Mvar<'a> {
63    pub const MIN_SIZE: usize = (MajorMinor::RAW_BYTE_LEN
64        + u16::RAW_BYTE_LEN
65        + u16::RAW_BYTE_LEN
66        + u16::RAW_BYTE_LEN
67        + Offset16::RAW_BYTE_LEN);
68    basic_table_impls!(impl_the_methods);
69
70    /// Major version number of the horizontal metrics variations table — set to 1.
71    /// Minor version number of the horizontal metrics variations table — set to 0.
72    pub fn version(&self) -> MajorMinor {
73        self.data.header().version.get()
74    }
75
76    /// The size in bytes of each value record — must be greater than zero.
77    pub fn value_record_size(&self) -> u16 {
78        self.data.header().value_record_size.get()
79    }
80
81    /// The number of value records — may be zero.
82    pub fn value_record_count(&self) -> u16 {
83        self.data.header().value_record_count.get()
84    }
85
86    /// Offset in bytes from the start of this table to the item variation store table. If valueRecordCount is zero, set to zero; if valueRecordCount is greater than zero, must be greater than zero.
87    pub fn item_variation_store_offset(&self) -> Nullable<Offset16> {
88        self.data.header().item_variation_store_offset.get()
89    }
90
91    /// Attempt to resolve [`item_variation_store_offset`][Self::item_variation_store_offset].
92    pub fn item_variation_store(&self) -> Option<Result<ItemVariationStore<'a>, ReadError>> {
93        let data = self.offset_data();
94        self.item_variation_store_offset().resolve(data)
95    }
96
97    /// Array of value records that identify target items and the associated delta-set index for each. The valueTag records must be in binary order of their valueTag field.
98    pub fn value_records(&self) -> &'a [ValueRecord] {
99        let range = self.value_records_byte_range();
100        self.offset_data()
101            .read_array(range)
102            .ok()
103            .unwrap_or_default()
104    }
105
106    pub fn version_byte_range(&self) -> Range<usize> {
107        let start = 0;
108        let end = start + MajorMinor::RAW_BYTE_LEN;
109        start..end
110    }
111
112    pub fn _reserved_byte_range(&self) -> Range<usize> {
113        let start = self.version_byte_range().end;
114        let end = start + u16::RAW_BYTE_LEN;
115        start..end
116    }
117
118    pub fn value_record_size_byte_range(&self) -> Range<usize> {
119        let start = self._reserved_byte_range().end;
120        let end = start + u16::RAW_BYTE_LEN;
121        start..end
122    }
123
124    pub fn value_record_count_byte_range(&self) -> Range<usize> {
125        let start = self.value_record_size_byte_range().end;
126        let end = start + u16::RAW_BYTE_LEN;
127        start..end
128    }
129
130    pub fn item_variation_store_offset_byte_range(&self) -> Range<usize> {
131        let start = self.value_record_count_byte_range().end;
132        let end = start + Offset16::RAW_BYTE_LEN;
133        start..end
134    }
135
136    pub fn value_records_byte_range(&self) -> Range<usize> {
137        let value_record_count = self.value_record_count();
138        let start = self.item_variation_store_offset_byte_range().end;
139        let end = start
140            + (transforms::to_usize(value_record_count)).saturating_mul(ValueRecord::RAW_BYTE_LEN);
141        start..end
142    }
143}
144
145const _: () = assert!(FontData::default_data_long_enough(Mvar::MIN_SIZE));
146
147impl Default for Mvar<'_> {
148    fn default() -> Self {
149        Self {
150            data: TableDataRef::new(FontData::default_table_data().as_bytes())
151                .unwrap_or_else(|| unreachable!("the default data covers MIN_SIZE")),
152        }
153    }
154}
155
156/// [ValueRecord](https://learn.microsoft.com/en-us/typography/opentype/spec/mvar#table-formats) metrics variation record
157#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, bytemuck :: AnyBitPattern)]
158#[repr(C)]
159#[repr(packed)]
160pub struct ValueRecord {
161    /// Four-byte tag identifying a font-wide measure.
162    pub value_tag: BigEndian<Tag>,
163    /// A delta-set outer index — used to select an item variation data subtable within the item variation store.
164    pub delta_set_outer_index: BigEndian<u16>,
165    /// A delta-set inner index — used to select a delta-set row within an item variation data subtable.
166    pub delta_set_inner_index: BigEndian<u16>,
167}
168
169impl ValueRecord {
170    /// Four-byte tag identifying a font-wide measure.
171    pub fn value_tag(&self) -> Tag {
172        self.value_tag.get()
173    }
174
175    /// A delta-set outer index — used to select an item variation data subtable within the item variation store.
176    pub fn delta_set_outer_index(&self) -> u16 {
177        self.delta_set_outer_index.get()
178    }
179
180    /// A delta-set inner index — used to select a delta-set row within an item variation data subtable.
181    pub fn delta_set_inner_index(&self) -> u16 {
182        self.delta_set_inner_index.get()
183    }
184}
185
186impl FixedSize for ValueRecord {
187    const RAW_BYTE_LEN: usize = Tag::RAW_BYTE_LEN + u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN;
188}