Skip to main content

write_fonts/generated/
generated_avar.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
8/// The [avar (Axis Variations)](https://docs.microsoft.com/en-us/typography/opentype/spec/avar) table
9#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
10#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
11pub struct Avar {
12    /// The segment maps array — one segment map for each axis, in the order of axes specified in the 'fvar' table.
13    pub axis_segment_maps: Vec<SegmentMaps>,
14    /// Offset to DeltaSetIndexMap table (may be NULL).
15    pub axis_index_map: NullableOffsetMarker<DeltaSetIndexMap, WIDTH_32>,
16    /// Offset to ItemVariationStore (may be NULL).
17    pub var_store: NullableOffsetMarker<ItemVariationStore, WIDTH_32>,
18}
19
20impl Avar {
21    /// Construct a new `Avar`
22    pub fn new(axis_segment_maps: Vec<SegmentMaps>) -> Self {
23        Self {
24            axis_segment_maps,
25            ..Default::default()
26        }
27    }
28}
29
30impl FontWrite for Avar {
31    #[allow(clippy::unnecessary_cast)]
32    fn write_into(&self, writer: &mut TableWriter) {
33        let version = self.compute_version() as MajorMinor;
34        version.write_into(writer);
35        (0 as u16).write_into(writer);
36        (u16::try_from(array_len(&self.axis_segment_maps)).unwrap()).write_into(writer);
37        self.axis_segment_maps.write_into(writer);
38        version
39            .compatible((2u16, 0u16))
40            .then(|| self.axis_index_map.write_into(writer));
41        version
42            .compatible((2u16, 0u16))
43            .then(|| self.var_store.write_into(writer));
44    }
45    fn table_type(&self) -> TableType {
46        TableType::TopLevel(Avar::TAG)
47    }
48}
49
50impl Validate for Avar {
51    fn validate_impl(&self, ctx: &mut ValidationCtx) {
52        ctx.in_table("Avar", |ctx| {
53            ctx.in_field("axis_segment_maps", |ctx| {
54                if self.axis_segment_maps.len() > to_usize(u16::MAX) {
55                    ctx.report("array exceeds max length");
56                }
57                self.axis_segment_maps.validate_impl(ctx);
58            });
59            ctx.in_field("axis_index_map", |ctx| {
60                self.axis_index_map.validate_impl(ctx);
61            });
62            ctx.in_field("var_store", |ctx| {
63                self.var_store.validate_impl(ctx);
64            });
65        })
66    }
67}
68
69impl TopLevelTable for Avar {
70    const TAG: Tag = Tag::new(b"avar");
71}
72
73impl<'a> FromObjRef<read_fonts::tables::avar::Avar<'a>> for Avar {
74    fn from_obj_ref(obj: &read_fonts::tables::avar::Avar<'a>, _: FontData) -> Self {
75        let offset_data = obj.offset_data();
76        Avar {
77            axis_segment_maps: obj
78                .axis_segment_maps()
79                .iter()
80                .filter_map(|x| x.map(|x| FromObjRef::from_obj_ref(&x, offset_data)).ok())
81                .collect(),
82            axis_index_map: obj.axis_index_map().to_owned_table(),
83            var_store: obj.var_store().to_owned_table(),
84        }
85    }
86}
87
88#[allow(clippy::needless_lifetimes)]
89impl<'a> FromTableRef<read_fonts::tables::avar::Avar<'a>> for Avar {}
90
91impl ReadArgs for Avar {
92    type Args = ();
93}
94
95impl<'a> FontRead<'a> for Avar {
96    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
97        <read_fonts::tables::avar::Avar as FontRead>::read(data).map(|x| x.to_owned_table())
98    }
99}
100
101/// [SegmentMaps](https://learn.microsoft.com/en-us/typography/opentype/spec/avar#table-formats) record
102#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
103#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
104pub struct SegmentMaps {
105    /// The array of axis value map records for this axis.
106    pub axis_value_maps: Vec<AxisValueMap>,
107}
108
109impl SegmentMaps {
110    /// Construct a new `SegmentMaps`
111    pub fn new(axis_value_maps: Vec<AxisValueMap>) -> Self {
112        Self { axis_value_maps }
113    }
114}
115
116impl FontWrite for SegmentMaps {
117    #[allow(clippy::unnecessary_cast)]
118    fn write_into(&self, writer: &mut TableWriter) {
119        (u16::try_from(array_len(&self.axis_value_maps)).unwrap()).write_into(writer);
120        self.axis_value_maps.write_into(writer);
121    }
122    fn table_type(&self) -> TableType {
123        TableType::Named("SegmentMaps")
124    }
125}
126
127impl Validate for SegmentMaps {
128    fn validate_impl(&self, ctx: &mut ValidationCtx) {
129        ctx.in_table("SegmentMaps", |ctx| {
130            ctx.in_field("axis_value_maps", |ctx| {
131                if self.axis_value_maps.len() > to_usize(u16::MAX) {
132                    ctx.report("array exceeds max length");
133                }
134                self.axis_value_maps.validate_impl(ctx);
135            });
136        })
137    }
138}
139
140impl FromObjRef<read_fonts::tables::avar::SegmentMaps<'_>> for SegmentMaps {
141    fn from_obj_ref(obj: &read_fonts::tables::avar::SegmentMaps, offset_data: FontData) -> Self {
142        SegmentMaps {
143            axis_value_maps: obj.axis_value_maps().to_owned_obj(offset_data),
144        }
145    }
146}
147
148/// [AxisValueMap](https://learn.microsoft.com/en-us/typography/opentype/spec/avar#table-formats) record
149#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
150#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
151pub struct AxisValueMap {
152    /// A normalized coordinate value obtained using default normalization.
153    pub from_coordinate: F2Dot14,
154    /// The modified, normalized coordinate value.
155    pub to_coordinate: F2Dot14,
156}
157
158impl AxisValueMap {
159    /// Construct a new `AxisValueMap`
160    pub fn new(from_coordinate: F2Dot14, to_coordinate: F2Dot14) -> Self {
161        Self {
162            from_coordinate,
163            to_coordinate,
164        }
165    }
166}
167
168impl FontWrite for AxisValueMap {
169    fn write_into(&self, writer: &mut TableWriter) {
170        self.from_coordinate.write_into(writer);
171        self.to_coordinate.write_into(writer);
172    }
173    fn table_type(&self) -> TableType {
174        TableType::Named("AxisValueMap")
175    }
176}
177
178impl Validate for AxisValueMap {
179    fn validate_impl(&self, _ctx: &mut ValidationCtx) {}
180}
181
182impl FromObjRef<read_fonts::tables::avar::AxisValueMap> for AxisValueMap {
183    fn from_obj_ref(obj: &read_fonts::tables::avar::AxisValueMap, _: FontData) -> Self {
184        AxisValueMap {
185            from_coordinate: obj.from_coordinate(),
186            to_coordinate: obj.to_coordinate(),
187        }
188    }
189}