write_fonts/generated/
generated_fvar.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 [fvar (Font Variations)](https://docs.microsoft.com/en-us/typography/opentype/spec/fvar) table
9#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
10#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
11pub struct Fvar {
12    /// Offset in bytes from the beginning of the table to the start of the VariationAxisRecord array. The
13    /// InstanceRecord array directly follows.
14    pub axis_instance_arrays: OffsetMarker<AxisInstanceArrays>,
15}
16
17impl Fvar {
18    /// Construct a new `Fvar`
19    pub fn new(axis_instance_arrays: AxisInstanceArrays) -> Self {
20        Self {
21            axis_instance_arrays: axis_instance_arrays.into(),
22        }
23    }
24}
25
26impl FontWrite for Fvar {
27    #[allow(clippy::unnecessary_cast)]
28    fn write_into(&self, writer: &mut TableWriter) {
29        (MajorMinor::VERSION_1_0 as MajorMinor).write_into(writer);
30        self.axis_instance_arrays.write_into(writer);
31        (2 as u16).write_into(writer);
32        (self.axis_count() as u16).write_into(writer);
33        (20 as u16).write_into(writer);
34        (self.instance_count() as u16).write_into(writer);
35        (self.instance_size() as u16).write_into(writer);
36    }
37    fn table_type(&self) -> TableType {
38        TableType::TopLevel(Fvar::TAG)
39    }
40}
41
42impl Validate for Fvar {
43    fn validate_impl(&self, ctx: &mut ValidationCtx) {
44        ctx.in_table("Fvar", |ctx| {
45            ctx.in_field("axis_instance_arrays", |ctx| {
46                self.axis_instance_arrays.validate_impl(ctx);
47            });
48            self.check_instances(ctx);
49        })
50    }
51}
52
53impl TopLevelTable for Fvar {
54    const TAG: Tag = Tag::new(b"fvar");
55}
56
57impl<'a> FromObjRef<read_fonts::tables::fvar::Fvar<'a>> for Fvar {
58    fn from_obj_ref(obj: &read_fonts::tables::fvar::Fvar<'a>, _: FontData) -> Self {
59        Fvar {
60            axis_instance_arrays: obj.axis_instance_arrays().to_owned_table(),
61        }
62    }
63}
64
65#[allow(clippy::needless_lifetimes)]
66impl<'a> FromTableRef<read_fonts::tables::fvar::Fvar<'a>> for Fvar {}
67
68impl<'a> FontRead<'a> for Fvar {
69    fn read(data: FontData<'a>) -> Result<Self, ReadError> {
70        <read_fonts::tables::fvar::Fvar as FontRead>::read(data).map(|x| x.to_owned_table())
71    }
72}
73
74/// Shim table to handle combined axis and instance arrays.
75#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
76#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
77pub struct AxisInstanceArrays {
78    /// Variation axis record array.
79    pub axes: Vec<VariationAxisRecord>,
80    /// Instance record array.
81    pub instances: Vec<InstanceRecord>,
82}
83
84impl AxisInstanceArrays {
85    /// Construct a new `AxisInstanceArrays`
86    pub fn new(axes: Vec<VariationAxisRecord>, instances: Vec<InstanceRecord>) -> Self {
87        Self { axes, instances }
88    }
89}
90
91impl FontWrite for AxisInstanceArrays {
92    fn write_into(&self, writer: &mut TableWriter) {
93        self.axes.write_into(writer);
94        self.instances.write_into(writer);
95    }
96    fn table_type(&self) -> TableType {
97        TableType::Named("AxisInstanceArrays")
98    }
99}
100
101impl Validate for AxisInstanceArrays {
102    fn validate_impl(&self, ctx: &mut ValidationCtx) {
103        ctx.in_table("AxisInstanceArrays", |ctx| {
104            ctx.in_field("axes", |ctx| {
105                if self.axes.len() > (u16::MAX as usize) {
106                    ctx.report("array exceeds max length");
107                }
108                self.axes.validate_impl(ctx);
109            });
110            ctx.in_field("instances", |ctx| {
111                if self.instances.len() > (u16::MAX as usize) {
112                    ctx.report("array exceeds max length");
113                }
114                self.instances.validate_impl(ctx);
115            });
116        })
117    }
118}
119
120impl<'a> FromObjRef<read_fonts::tables::fvar::AxisInstanceArrays<'a>> for AxisInstanceArrays {
121    fn from_obj_ref(obj: &read_fonts::tables::fvar::AxisInstanceArrays<'a>, _: FontData) -> Self {
122        let offset_data = obj.offset_data();
123        AxisInstanceArrays {
124            axes: obj.axes().to_owned_obj(offset_data),
125            instances: obj
126                .instances()
127                .iter()
128                .filter_map(|x| x.map(|x| FromObjRef::from_obj_ref(&x, offset_data)).ok())
129                .collect(),
130        }
131    }
132}
133
134#[allow(clippy::needless_lifetimes)]
135impl<'a> FromTableRef<read_fonts::tables::fvar::AxisInstanceArrays<'a>> for AxisInstanceArrays {}
136
137/// The [VariationAxisRecord](https://learn.microsoft.com/en-us/typography/opentype/spec/fvar#variationaxisrecord)
138#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
139#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
140pub struct VariationAxisRecord {
141    /// Tag identifying the design variation for the axis.
142    pub axis_tag: Tag,
143    /// The minimum coordinate value for the axis.
144    pub min_value: Fixed,
145    /// The default coordinate value for the axis.
146    pub default_value: Fixed,
147    /// The maximum coordinate value for the axis.
148    pub max_value: Fixed,
149    /// Axis qualifiers — see details below.
150    pub flags: u16,
151    /// The name ID for entries in the 'name' table that provide a display name for this axis.
152    pub axis_name_id: NameId,
153}
154
155impl VariationAxisRecord {
156    /// Construct a new `VariationAxisRecord`
157    pub fn new(
158        axis_tag: Tag,
159        min_value: Fixed,
160        default_value: Fixed,
161        max_value: Fixed,
162        flags: u16,
163        axis_name_id: NameId,
164    ) -> Self {
165        Self {
166            axis_tag,
167            min_value,
168            default_value,
169            max_value,
170            flags,
171            axis_name_id,
172        }
173    }
174}
175
176impl FontWrite for VariationAxisRecord {
177    fn write_into(&self, writer: &mut TableWriter) {
178        self.axis_tag.write_into(writer);
179        self.min_value.write_into(writer);
180        self.default_value.write_into(writer);
181        self.max_value.write_into(writer);
182        self.flags.write_into(writer);
183        self.axis_name_id.write_into(writer);
184    }
185    fn table_type(&self) -> TableType {
186        TableType::Named("VariationAxisRecord")
187    }
188}
189
190impl Validate for VariationAxisRecord {
191    fn validate_impl(&self, _ctx: &mut ValidationCtx) {}
192}
193
194impl FromObjRef<read_fonts::tables::fvar::VariationAxisRecord> for VariationAxisRecord {
195    fn from_obj_ref(obj: &read_fonts::tables::fvar::VariationAxisRecord, _: FontData) -> Self {
196        VariationAxisRecord {
197            axis_tag: obj.axis_tag(),
198            min_value: obj.min_value(),
199            default_value: obj.default_value(),
200            max_value: obj.max_value(),
201            flags: obj.flags(),
202            axis_name_id: obj.axis_name_id(),
203        }
204    }
205}