Skip to main content

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 ReadArgs for Fvar {
69    type Args = ();
70}
71
72impl<'a> FontRead<'a> for Fvar {
73    fn read_with_args(data: FontData<'a>, _: ()) -> Result<Self, ReadError> {
74        <read_fonts::tables::fvar::Fvar as FontRead>::read(data).map(|x| x.to_owned_table())
75    }
76}
77
78/// Shim table to handle combined axis and instance arrays.
79#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
80#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
81pub struct AxisInstanceArrays {
82    /// Variation axis record array.
83    pub axes: Vec<VariationAxisRecord>,
84    /// Instance record array.
85    pub instances: Vec<InstanceRecord>,
86}
87
88impl AxisInstanceArrays {
89    /// Construct a new `AxisInstanceArrays`
90    pub fn new(axes: Vec<VariationAxisRecord>, instances: Vec<InstanceRecord>) -> Self {
91        Self { axes, instances }
92    }
93}
94
95impl FontWrite for AxisInstanceArrays {
96    fn write_into(&self, writer: &mut TableWriter) {
97        self.axes.write_into(writer);
98        self.instances.write_into(writer);
99    }
100    fn table_type(&self) -> TableType {
101        TableType::Named("AxisInstanceArrays")
102    }
103}
104
105impl Validate for AxisInstanceArrays {
106    fn validate_impl(&self, ctx: &mut ValidationCtx) {
107        ctx.in_table("AxisInstanceArrays", |ctx| {
108            ctx.in_field("axes", |ctx| {
109                if self.axes.len() > to_usize(u16::MAX) {
110                    ctx.report("array exceeds max length");
111                }
112                self.axes.validate_impl(ctx);
113            });
114            ctx.in_field("instances", |ctx| {
115                if self.instances.len() > to_usize(u16::MAX) {
116                    ctx.report("array exceeds max length");
117                }
118                self.instances.validate_impl(ctx);
119            });
120        })
121    }
122}
123
124impl<'a> FromObjRef<read_fonts::tables::fvar::AxisInstanceArrays<'a>> for AxisInstanceArrays {
125    fn from_obj_ref(obj: &read_fonts::tables::fvar::AxisInstanceArrays<'a>, _: FontData) -> Self {
126        let offset_data = obj.offset_data();
127        AxisInstanceArrays {
128            axes: obj.axes().to_owned_obj(offset_data),
129            instances: obj
130                .instances()
131                .iter()
132                .filter_map(|x| x.map(|x| FromObjRef::from_obj_ref(&x, offset_data)).ok())
133                .collect(),
134        }
135    }
136}
137
138#[allow(clippy::needless_lifetimes)]
139impl<'a> FromTableRef<read_fonts::tables::fvar::AxisInstanceArrays<'a>> for AxisInstanceArrays {}
140
141/// The [VariationAxisRecord](https://learn.microsoft.com/en-us/typography/opentype/spec/fvar#variationaxisrecord)
142#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
143#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
144pub struct VariationAxisRecord {
145    /// Tag identifying the design variation for the axis.
146    pub axis_tag: Tag,
147    /// The minimum coordinate value for the axis.
148    pub min_value: Fixed,
149    /// The default coordinate value for the axis.
150    pub default_value: Fixed,
151    /// The maximum coordinate value for the axis.
152    pub max_value: Fixed,
153    /// Axis qualifiers — see details below.
154    pub flags: u16,
155    /// The name ID for entries in the 'name' table that provide a display name for this axis.
156    pub axis_name_id: NameId,
157}
158
159impl VariationAxisRecord {
160    /// Construct a new `VariationAxisRecord`
161    pub fn new(
162        axis_tag: Tag,
163        min_value: Fixed,
164        default_value: Fixed,
165        max_value: Fixed,
166        flags: u16,
167        axis_name_id: NameId,
168    ) -> Self {
169        Self {
170            axis_tag,
171            min_value,
172            default_value,
173            max_value,
174            flags,
175            axis_name_id,
176        }
177    }
178}
179
180impl FontWrite for VariationAxisRecord {
181    fn write_into(&self, writer: &mut TableWriter) {
182        self.axis_tag.write_into(writer);
183        self.min_value.write_into(writer);
184        self.default_value.write_into(writer);
185        self.max_value.write_into(writer);
186        self.flags.write_into(writer);
187        self.axis_name_id.write_into(writer);
188    }
189    fn table_type(&self) -> TableType {
190        TableType::Named("VariationAxisRecord")
191    }
192}
193
194impl Validate for VariationAxisRecord {
195    fn validate_impl(&self, _ctx: &mut ValidationCtx) {}
196}
197
198impl FromObjRef<read_fonts::tables::fvar::VariationAxisRecord> for VariationAxisRecord {
199    fn from_obj_ref(obj: &read_fonts::tables::fvar::VariationAxisRecord, _: FontData) -> Self {
200        VariationAxisRecord {
201            axis_tag: obj.axis_tag(),
202            min_value: obj.min_value(),
203            default_value: obj.default_value(),
204            max_value: obj.max_value(),
205            flags: obj.flags(),
206            axis_name_id: obj.axis_name_id(),
207        }
208    }
209}