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#[cfg(feature = "experimental_traverse")]
139impl<'a> SomeTable<'a> for Mvar<'a> {
140    fn type_name(&self) -> &str {
141        "Mvar"
142    }
143    fn get_field(&self, idx: usize) -> Option<Field<'a>> {
144        match idx {
145            0usize => Some(Field::new("version", self.version())),
146            1usize => Some(Field::new("value_record_size", self.value_record_size())),
147            2usize => Some(Field::new("value_record_count", self.value_record_count())),
148            3usize => Some(Field::new(
149                "item_variation_store_offset",
150                FieldType::offset(
151                    self.item_variation_store_offset(),
152                    self.item_variation_store(),
153                ),
154            )),
155            4usize => Some(Field::new(
156                "value_records",
157                traversal::FieldType::array_of_records(
158                    stringify!(ValueRecord),
159                    self.value_records(),
160                    self.offset_data(),
161                ),
162            )),
163            _ => None,
164        }
165    }
166}
167
168#[cfg(feature = "experimental_traverse")]
169#[allow(clippy::needless_lifetimes)]
170impl<'a> std::fmt::Debug for Mvar<'a> {
171    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
172        (self as &dyn SomeTable<'a>).fmt(f)
173    }
174}
175
176/// [ValueRecord](https://learn.microsoft.com/en-us/typography/opentype/spec/mvar#table-formats) metrics variation record
177#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, bytemuck :: AnyBitPattern)]
178#[repr(C)]
179#[repr(packed)]
180pub struct ValueRecord {
181    /// Four-byte tag identifying a font-wide measure.
182    pub value_tag: BigEndian<Tag>,
183    /// A delta-set outer index — used to select an item variation data subtable within the item variation store.
184    pub delta_set_outer_index: BigEndian<u16>,
185    /// A delta-set inner index — used to select a delta-set row within an item variation data subtable.
186    pub delta_set_inner_index: BigEndian<u16>,
187}
188
189impl ValueRecord {
190    /// Four-byte tag identifying a font-wide measure.
191    pub fn value_tag(&self) -> Tag {
192        self.value_tag.get()
193    }
194
195    /// A delta-set outer index — used to select an item variation data subtable within the item variation store.
196    pub fn delta_set_outer_index(&self) -> u16 {
197        self.delta_set_outer_index.get()
198    }
199
200    /// A delta-set inner index — used to select a delta-set row within an item variation data subtable.
201    pub fn delta_set_inner_index(&self) -> u16 {
202        self.delta_set_inner_index.get()
203    }
204}
205
206impl FixedSize for ValueRecord {
207    const RAW_BYTE_LEN: usize = Tag::RAW_BYTE_LEN + u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN;
208}
209
210#[cfg(feature = "experimental_traverse")]
211impl<'a> SomeRecord<'a> for ValueRecord {
212    fn traverse(self, data: FontData<'a>) -> RecordResolver<'a> {
213        RecordResolver {
214            name: "ValueRecord",
215            get_field: Box::new(move |idx, _data| match idx {
216                0usize => Some(Field::new("value_tag", self.value_tag())),
217                1usize => Some(Field::new(
218                    "delta_set_outer_index",
219                    self.delta_set_outer_index(),
220                )),
221                2usize => Some(Field::new(
222                    "delta_set_inner_index",
223                    self.delta_set_inner_index(),
224                )),
225                _ => None,
226            }),
227            data,
228        }
229    }
230}