1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// THIS FILE IS AUTOGENERATED.
// Any changes to this file will be overwritten.
// For more information about how codegen works, see font-codegen/README.md

#[allow(unused_imports)]
use crate::codegen_prelude::*;

/// The [fvar (Font Variations)](https://docs.microsoft.com/en-us/typography/opentype/spec/fvar) table
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Fvar {
    /// Offset in bytes from the beginning of the table to the start of the VariationAxisRecord array. The
    /// InstanceRecord array directly follows.
    pub axis_instance_arrays: OffsetMarker<AxisInstanceArrays>,
}

impl Fvar {
    /// Construct a new `Fvar`
    pub fn new(axis_instance_arrays: AxisInstanceArrays) -> Self {
        Self {
            axis_instance_arrays: axis_instance_arrays.into(),
        }
    }
}

impl FontWrite for Fvar {
    #[allow(clippy::unnecessary_cast)]
    fn write_into(&self, writer: &mut TableWriter) {
        (MajorMinor::VERSION_1_0 as MajorMinor).write_into(writer);
        self.axis_instance_arrays.write_into(writer);
        (2 as u16).write_into(writer);
        (self.axis_count() as u16).write_into(writer);
        (20 as u16).write_into(writer);
        (self.instance_count() as u16).write_into(writer);
        (self.instance_size() as u16).write_into(writer);
    }
    fn table_type(&self) -> TableType {
        TableType::TopLevel(Fvar::TAG)
    }
}

impl Validate for Fvar {
    fn validate_impl(&self, ctx: &mut ValidationCtx) {
        ctx.in_table("Fvar", |ctx| {
            ctx.in_field("axis_instance_arrays", |ctx| {
                self.axis_instance_arrays.validate_impl(ctx);
            });
            self.check_instances(ctx);
        })
    }
}

impl TopLevelTable for Fvar {
    const TAG: Tag = Tag::new(b"fvar");
}

impl<'a> FromObjRef<read_fonts::tables::fvar::Fvar<'a>> for Fvar {
    fn from_obj_ref(obj: &read_fonts::tables::fvar::Fvar<'a>, _: FontData) -> Self {
        Fvar {
            axis_instance_arrays: obj.axis_instance_arrays().to_owned_table(),
        }
    }
}

impl<'a> FromTableRef<read_fonts::tables::fvar::Fvar<'a>> for Fvar {}

impl<'a> FontRead<'a> for Fvar {
    fn read(data: FontData<'a>) -> Result<Self, ReadError> {
        <read_fonts::tables::fvar::Fvar as FontRead>::read(data).map(|x| x.to_owned_table())
    }
}

/// Shim table to handle combined axis and instance arrays.
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct AxisInstanceArrays {
    /// Variation axis record array.
    pub axes: Vec<VariationAxisRecord>,
    /// Instance record array.
    pub instances: Vec<InstanceRecord>,
}

impl AxisInstanceArrays {
    /// Construct a new `AxisInstanceArrays`
    pub fn new(axes: Vec<VariationAxisRecord>, instances: Vec<InstanceRecord>) -> Self {
        Self {
            axes: axes.into_iter().map(Into::into).collect(),
            instances,
        }
    }
}

impl FontWrite for AxisInstanceArrays {
    fn write_into(&self, writer: &mut TableWriter) {
        self.axes.write_into(writer);
        self.instances.write_into(writer);
    }
    fn table_type(&self) -> TableType {
        TableType::Named("AxisInstanceArrays")
    }
}

impl Validate for AxisInstanceArrays {
    fn validate_impl(&self, ctx: &mut ValidationCtx) {
        ctx.in_table("AxisInstanceArrays", |ctx| {
            ctx.in_field("axes", |ctx| {
                if self.axes.len() > (u16::MAX as usize) {
                    ctx.report("array exceeds max length");
                }
                self.axes.validate_impl(ctx);
            });
            ctx.in_field("instances", |ctx| {
                if self.instances.len() > (u16::MAX as usize) {
                    ctx.report("array exceeds max length");
                }
                self.instances.validate_impl(ctx);
            });
        })
    }
}

impl<'a> FromObjRef<read_fonts::tables::fvar::AxisInstanceArrays<'a>> for AxisInstanceArrays {
    fn from_obj_ref(obj: &read_fonts::tables::fvar::AxisInstanceArrays<'a>, _: FontData) -> Self {
        let offset_data = obj.offset_data();
        AxisInstanceArrays {
            axes: obj.axes().to_owned_obj(offset_data),
            instances: obj
                .instances()
                .iter()
                .filter_map(|x| x.map(|x| FromObjRef::from_obj_ref(&x, offset_data)).ok())
                .collect(),
        }
    }
}

impl<'a> FromTableRef<read_fonts::tables::fvar::AxisInstanceArrays<'a>> for AxisInstanceArrays {}

/// The [VariationAxisRecord](https://learn.microsoft.com/en-us/typography/opentype/spec/fvar#variationaxisrecord)
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct VariationAxisRecord {
    /// Tag identifying the design variation for the axis.
    pub axis_tag: Tag,
    /// The minimum coordinate value for the axis.
    pub min_value: Fixed,
    /// The default coordinate value for the axis.
    pub default_value: Fixed,
    /// The maximum coordinate value for the axis.
    pub max_value: Fixed,
    /// Axis qualifiers — see details below.
    pub flags: u16,
    /// The name ID for entries in the 'name' table that provide a display name for this axis.
    pub axis_name_id: NameId,
}

impl VariationAxisRecord {
    /// Construct a new `VariationAxisRecord`
    pub fn new(
        axis_tag: Tag,
        min_value: Fixed,
        default_value: Fixed,
        max_value: Fixed,
        flags: u16,
        axis_name_id: NameId,
    ) -> Self {
        Self {
            axis_tag,
            min_value,
            default_value,
            max_value,
            flags,
            axis_name_id,
        }
    }
}

impl FontWrite for VariationAxisRecord {
    fn write_into(&self, writer: &mut TableWriter) {
        self.axis_tag.write_into(writer);
        self.min_value.write_into(writer);
        self.default_value.write_into(writer);
        self.max_value.write_into(writer);
        self.flags.write_into(writer);
        self.axis_name_id.write_into(writer);
    }
    fn table_type(&self) -> TableType {
        TableType::Named("VariationAxisRecord")
    }
}

impl Validate for VariationAxisRecord {
    fn validate_impl(&self, _ctx: &mut ValidationCtx) {}
}

impl FromObjRef<read_fonts::tables::fvar::VariationAxisRecord> for VariationAxisRecord {
    fn from_obj_ref(obj: &read_fonts::tables::fvar::VariationAxisRecord, _: FontData) -> Self {
        VariationAxisRecord {
            axis_tag: obj.axis_tag(),
            min_value: obj.min_value(),
            default_value: obj.default_value(),
            max_value: obj.max_value(),
            flags: obj.flags(),
            axis_name_id: obj.axis_name_id(),
        }
    }
}