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