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<'a> FontRead<'a> for Mvar<'a> {
24    fn read(data: FontData<'a>) -> Result<Self, ReadError> {
25        #[allow(clippy::absurd_extreme_comparisons)]
26        if data.len() < Self::MIN_SIZE {
27            return Err(ReadError::OutOfBounds);
28        }
29        Ok(Self { data })
30    }
31}
32
33/// The [MVAR (Metrics Variations)](https://docs.microsoft.com/en-us/typography/opentype/spec/mvar) table
34#[derive(Clone)]
35pub struct Mvar<'a> {
36    data: FontData<'a>,
37}
38
39#[allow(clippy::needless_lifetimes)]
40impl<'a> Mvar<'a> {
41    pub const MIN_SIZE: usize = (MajorMinor::RAW_BYTE_LEN
42        + u16::RAW_BYTE_LEN
43        + u16::RAW_BYTE_LEN
44        + u16::RAW_BYTE_LEN
45        + Offset16::RAW_BYTE_LEN);
46    basic_table_impls!(impl_the_methods);
47
48    /// Major version number of the horizontal metrics variations table — set to 1.
49    /// Minor version number of the horizontal metrics variations table — set to 0.
50    pub fn version(&self) -> MajorMinor {
51        let range = self.version_byte_range();
52        self.data.read_at(range.start).ok().unwrap()
53    }
54
55    /// The size in bytes of each value record — must be greater than zero.
56    pub fn value_record_size(&self) -> u16 {
57        let range = self.value_record_size_byte_range();
58        self.data.read_at(range.start).ok().unwrap()
59    }
60
61    /// The number of value records — may be zero.
62    pub fn value_record_count(&self) -> u16 {
63        let range = self.value_record_count_byte_range();
64        self.data.read_at(range.start).ok().unwrap()
65    }
66
67    /// 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.
68    pub fn item_variation_store_offset(&self) -> Nullable<Offset16> {
69        let range = self.item_variation_store_offset_byte_range();
70        self.data.read_at(range.start).ok().unwrap()
71    }
72
73    /// Attempt to resolve [`item_variation_store_offset`][Self::item_variation_store_offset].
74    pub fn item_variation_store(&self) -> Option<Result<ItemVariationStore<'a>, ReadError>> {
75        let data = self.data;
76        self.item_variation_store_offset().resolve(data)
77    }
78
79    /// 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.
80    pub fn value_records(&self) -> &'a [ValueRecord] {
81        let range = self.value_records_byte_range();
82        self.data.read_array(range).ok().unwrap_or_default()
83    }
84
85    pub fn version_byte_range(&self) -> Range<usize> {
86        let start = 0;
87        start..start + MajorMinor::RAW_BYTE_LEN
88    }
89
90    pub fn _reserved_byte_range(&self) -> Range<usize> {
91        let start = self.version_byte_range().end;
92        start..start + u16::RAW_BYTE_LEN
93    }
94
95    pub fn value_record_size_byte_range(&self) -> Range<usize> {
96        let start = self._reserved_byte_range().end;
97        start..start + u16::RAW_BYTE_LEN
98    }
99
100    pub fn value_record_count_byte_range(&self) -> Range<usize> {
101        let start = self.value_record_size_byte_range().end;
102        start..start + u16::RAW_BYTE_LEN
103    }
104
105    pub fn item_variation_store_offset_byte_range(&self) -> Range<usize> {
106        let start = self.value_record_count_byte_range().end;
107        start..start + Offset16::RAW_BYTE_LEN
108    }
109
110    pub fn value_records_byte_range(&self) -> Range<usize> {
111        let value_record_count = self.value_record_count();
112        let start = self.item_variation_store_offset_byte_range().end;
113        start..start + (value_record_count as usize).saturating_mul(ValueRecord::RAW_BYTE_LEN)
114    }
115}
116
117#[cfg(feature = "experimental_traverse")]
118impl<'a> SomeTable<'a> for Mvar<'a> {
119    fn type_name(&self) -> &str {
120        "Mvar"
121    }
122    fn get_field(&self, idx: usize) -> Option<Field<'a>> {
123        match idx {
124            0usize => Some(Field::new("version", self.version())),
125            1usize => Some(Field::new("value_record_size", self.value_record_size())),
126            2usize => Some(Field::new("value_record_count", self.value_record_count())),
127            3usize => Some(Field::new(
128                "item_variation_store_offset",
129                FieldType::offset(
130                    self.item_variation_store_offset(),
131                    self.item_variation_store(),
132                ),
133            )),
134            4usize => Some(Field::new(
135                "value_records",
136                traversal::FieldType::array_of_records(
137                    stringify!(ValueRecord),
138                    self.value_records(),
139                    self.offset_data(),
140                ),
141            )),
142            _ => None,
143        }
144    }
145}
146
147#[cfg(feature = "experimental_traverse")]
148#[allow(clippy::needless_lifetimes)]
149impl<'a> std::fmt::Debug for Mvar<'a> {
150    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
151        (self as &dyn SomeTable<'a>).fmt(f)
152    }
153}
154
155/// [ValueRecord](https://learn.microsoft.com/en-us/typography/opentype/spec/mvar#table-formats) metrics variation record
156#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, bytemuck :: AnyBitPattern)]
157#[repr(C)]
158#[repr(packed)]
159pub struct ValueRecord {
160    /// Four-byte tag identifying a font-wide measure.
161    pub value_tag: BigEndian<Tag>,
162    /// A delta-set outer index — used to select an item variation data subtable within the item variation store.
163    pub delta_set_outer_index: BigEndian<u16>,
164    /// A delta-set inner index — used to select a delta-set row within an item variation data subtable.
165    pub delta_set_inner_index: BigEndian<u16>,
166}
167
168impl ValueRecord {
169    /// Four-byte tag identifying a font-wide measure.
170    pub fn value_tag(&self) -> Tag {
171        self.value_tag.get()
172    }
173
174    /// A delta-set outer index — used to select an item variation data subtable within the item variation store.
175    pub fn delta_set_outer_index(&self) -> u16 {
176        self.delta_set_outer_index.get()
177    }
178
179    /// A delta-set inner index — used to select a delta-set row within an item variation data subtable.
180    pub fn delta_set_inner_index(&self) -> u16 {
181        self.delta_set_inner_index.get()
182    }
183}
184
185impl FixedSize for ValueRecord {
186    const RAW_BYTE_LEN: usize = Tag::RAW_BYTE_LEN + u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN;
187}
188
189#[cfg(feature = "experimental_traverse")]
190impl<'a> SomeRecord<'a> for ValueRecord {
191    fn traverse(self, data: FontData<'a>) -> RecordResolver<'a> {
192        RecordResolver {
193            name: "ValueRecord",
194            get_field: Box::new(move |idx, _data| match idx {
195                0usize => Some(Field::new("value_tag", self.value_tag())),
196                1usize => Some(Field::new(
197                    "delta_set_outer_index",
198                    self.delta_set_outer_index(),
199                )),
200                2usize => Some(Field::new(
201                    "delta_set_inner_index",
202                    self.delta_set_inner_index(),
203                )),
204                _ => None,
205            }),
206            data,
207        }
208    }
209}