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.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        Ok(Self { data })
34    }
35}
36
37/// The [MVAR (Metrics Variations)](https://docs.microsoft.com/en-us/typography/opentype/spec/mvar) table
38#[derive(Clone)]
39pub struct Mvar<'a> {
40    data: FontData<'a>,
41}
42
43#[allow(clippy::needless_lifetimes)]
44impl<'a> Mvar<'a> {
45    pub const MIN_SIZE: usize = (MajorMinor::RAW_BYTE_LEN
46        + u16::RAW_BYTE_LEN
47        + u16::RAW_BYTE_LEN
48        + u16::RAW_BYTE_LEN
49        + Offset16::RAW_BYTE_LEN);
50    basic_table_impls!(impl_the_methods);
51
52    /// Major version number of the horizontal metrics variations table — set to 1.
53    /// Minor version number of the horizontal metrics variations table — set to 0.
54    pub fn version(&self) -> MajorMinor {
55        let range = self.version_byte_range();
56        self.data.read_at(range.start).ok().unwrap()
57    }
58
59    /// The size in bytes of each value record — must be greater than zero.
60    pub fn value_record_size(&self) -> u16 {
61        let range = self.value_record_size_byte_range();
62        self.data.read_at(range.start).ok().unwrap()
63    }
64
65    /// The number of value records — may be zero.
66    pub fn value_record_count(&self) -> u16 {
67        let range = self.value_record_count_byte_range();
68        self.data.read_at(range.start).ok().unwrap()
69    }
70
71    /// 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.
72    pub fn item_variation_store_offset(&self) -> Nullable<Offset16> {
73        let range = self.item_variation_store_offset_byte_range();
74        self.data.read_at(range.start).ok().unwrap()
75    }
76
77    /// Attempt to resolve [`item_variation_store_offset`][Self::item_variation_store_offset].
78    pub fn item_variation_store(&self) -> Option<Result<ItemVariationStore<'a>, ReadError>> {
79        let data = self.data;
80        self.item_variation_store_offset().resolve(data)
81    }
82
83    /// 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.
84    pub fn value_records(&self) -> &'a [ValueRecord] {
85        let range = self.value_records_byte_range();
86        self.data.read_array(range).ok().unwrap_or_default()
87    }
88
89    pub fn version_byte_range(&self) -> Range<usize> {
90        let start = 0;
91        let end = start + MajorMinor::RAW_BYTE_LEN;
92        start..end
93    }
94
95    pub fn _reserved_byte_range(&self) -> Range<usize> {
96        let start = self.version_byte_range().end;
97        let end = start + u16::RAW_BYTE_LEN;
98        start..end
99    }
100
101    pub fn value_record_size_byte_range(&self) -> Range<usize> {
102        let start = self._reserved_byte_range().end;
103        let end = start + u16::RAW_BYTE_LEN;
104        start..end
105    }
106
107    pub fn value_record_count_byte_range(&self) -> Range<usize> {
108        let start = self.value_record_size_byte_range().end;
109        let end = start + u16::RAW_BYTE_LEN;
110        start..end
111    }
112
113    pub fn item_variation_store_offset_byte_range(&self) -> Range<usize> {
114        let start = self.value_record_count_byte_range().end;
115        let end = start + Offset16::RAW_BYTE_LEN;
116        start..end
117    }
118
119    pub fn value_records_byte_range(&self) -> Range<usize> {
120        let value_record_count = self.value_record_count();
121        let start = self.item_variation_store_offset_byte_range().end;
122        let end = start
123            + (transforms::to_usize(value_record_count)).saturating_mul(ValueRecord::RAW_BYTE_LEN);
124        start..end
125    }
126}
127
128const _: () = assert!(FontData::default_data_long_enough(Mvar::MIN_SIZE));
129
130impl Default for Mvar<'_> {
131    fn default() -> Self {
132        Self {
133            data: FontData::default_table_data(),
134        }
135    }
136}
137
138/// [ValueRecord](https://learn.microsoft.com/en-us/typography/opentype/spec/mvar#table-formats) metrics variation record
139#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, bytemuck :: AnyBitPattern)]
140#[repr(C)]
141#[repr(packed)]
142pub struct ValueRecord {
143    /// Four-byte tag identifying a font-wide measure.
144    pub value_tag: BigEndian<Tag>,
145    /// A delta-set outer index — used to select an item variation data subtable within the item variation store.
146    pub delta_set_outer_index: BigEndian<u16>,
147    /// A delta-set inner index — used to select a delta-set row within an item variation data subtable.
148    pub delta_set_inner_index: BigEndian<u16>,
149}
150
151impl ValueRecord {
152    /// Four-byte tag identifying a font-wide measure.
153    pub fn value_tag(&self) -> Tag {
154        self.value_tag.get()
155    }
156
157    /// A delta-set outer index — used to select an item variation data subtable within the item variation store.
158    pub fn delta_set_outer_index(&self) -> u16 {
159        self.delta_set_outer_index.get()
160    }
161
162    /// A delta-set inner index — used to select a delta-set row within an item variation data subtable.
163    pub fn delta_set_inner_index(&self) -> u16 {
164        self.delta_set_inner_index.get()
165    }
166}
167
168impl FixedSize for ValueRecord {
169    const RAW_BYTE_LEN: usize = Tag::RAW_BYTE_LEN + u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN;
170}