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        let end = start + MajorMinor::RAW_BYTE_LEN;
88        start..end
89    }
90
91    pub fn _reserved_byte_range(&self) -> Range<usize> {
92        let start = self.version_byte_range().end;
93        let end = start + u16::RAW_BYTE_LEN;
94        start..end
95    }
96
97    pub fn value_record_size_byte_range(&self) -> Range<usize> {
98        let start = self._reserved_byte_range().end;
99        let end = start + u16::RAW_BYTE_LEN;
100        start..end
101    }
102
103    pub fn value_record_count_byte_range(&self) -> Range<usize> {
104        let start = self.value_record_size_byte_range().end;
105        let end = start + u16::RAW_BYTE_LEN;
106        start..end
107    }
108
109    pub fn item_variation_store_offset_byte_range(&self) -> Range<usize> {
110        let start = self.value_record_count_byte_range().end;
111        let end = start + Offset16::RAW_BYTE_LEN;
112        start..end
113    }
114
115    pub fn value_records_byte_range(&self) -> Range<usize> {
116        let value_record_count = self.value_record_count();
117        let start = self.item_variation_store_offset_byte_range().end;
118        let end = start
119            + (transforms::to_usize(value_record_count)).saturating_mul(ValueRecord::RAW_BYTE_LEN);
120        start..end
121    }
122}
123
124const _: () = assert!(FontData::default_data_long_enough(Mvar::MIN_SIZE));
125
126impl Default for Mvar<'_> {
127    fn default() -> Self {
128        Self {
129            data: FontData::default_table_data(),
130        }
131    }
132}
133
134#[cfg(feature = "experimental_traverse")]
135impl<'a> SomeTable<'a> for Mvar<'a> {
136    fn type_name(&self) -> &str {
137        "Mvar"
138    }
139    fn get_field(&self, idx: usize) -> Option<Field<'a>> {
140        match idx {
141            0usize => Some(Field::new("version", self.version())),
142            1usize => Some(Field::new("value_record_size", self.value_record_size())),
143            2usize => Some(Field::new("value_record_count", self.value_record_count())),
144            3usize => Some(Field::new(
145                "item_variation_store_offset",
146                FieldType::offset(
147                    self.item_variation_store_offset(),
148                    self.item_variation_store(),
149                ),
150            )),
151            4usize => Some(Field::new(
152                "value_records",
153                traversal::FieldType::array_of_records(
154                    stringify!(ValueRecord),
155                    self.value_records(),
156                    self.offset_data(),
157                ),
158            )),
159            _ => None,
160        }
161    }
162}
163
164#[cfg(feature = "experimental_traverse")]
165#[allow(clippy::needless_lifetimes)]
166impl<'a> std::fmt::Debug for Mvar<'a> {
167    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
168        (self as &dyn SomeTable<'a>).fmt(f)
169    }
170}
171
172/// [ValueRecord](https://learn.microsoft.com/en-us/typography/opentype/spec/mvar#table-formats) metrics variation record
173#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, bytemuck :: AnyBitPattern)]
174#[repr(C)]
175#[repr(packed)]
176pub struct ValueRecord {
177    /// Four-byte tag identifying a font-wide measure.
178    pub value_tag: BigEndian<Tag>,
179    /// A delta-set outer index — used to select an item variation data subtable within the item variation store.
180    pub delta_set_outer_index: BigEndian<u16>,
181    /// A delta-set inner index — used to select a delta-set row within an item variation data subtable.
182    pub delta_set_inner_index: BigEndian<u16>,
183}
184
185impl ValueRecord {
186    /// Four-byte tag identifying a font-wide measure.
187    pub fn value_tag(&self) -> Tag {
188        self.value_tag.get()
189    }
190
191    /// A delta-set outer index — used to select an item variation data subtable within the item variation store.
192    pub fn delta_set_outer_index(&self) -> u16 {
193        self.delta_set_outer_index.get()
194    }
195
196    /// A delta-set inner index — used to select a delta-set row within an item variation data subtable.
197    pub fn delta_set_inner_index(&self) -> u16 {
198        self.delta_set_inner_index.get()
199    }
200}
201
202impl FixedSize for ValueRecord {
203    const RAW_BYTE_LEN: usize = Tag::RAW_BYTE_LEN + u16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN;
204}
205
206#[cfg(feature = "experimental_traverse")]
207impl<'a> SomeRecord<'a> for ValueRecord {
208    fn traverse(self, data: FontData<'a>) -> RecordResolver<'a> {
209        RecordResolver {
210            name: "ValueRecord",
211            get_field: Box::new(move |idx, _data| match idx {
212                0usize => Some(Field::new("value_tag", self.value_tag())),
213                1usize => Some(Field::new(
214                    "delta_set_outer_index",
215                    self.delta_set_outer_index(),
216                )),
217                2usize => Some(Field::new(
218                    "delta_set_inner_index",
219                    self.delta_set_inner_index(),
220                )),
221                _ => None,
222            }),
223            data,
224        }
225    }
226}