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
// 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 [avar (Axis Variations)](https://docs.microsoft.com/en-us/typography/opentype/spec/avar) table
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Avar {
    /// The segment maps array — one segment map for each axis, in the order of axes specified in the 'fvar' table.
    pub axis_segment_maps: Vec<SegmentMaps>,
}

impl Avar {
    /// Construct a new `Avar`
    pub fn new(axis_segment_maps: Vec<SegmentMaps>) -> Self {
        Self { axis_segment_maps }
    }
}

impl FontWrite for Avar {
    #[allow(clippy::unnecessary_cast)]
    fn write_into(&self, writer: &mut TableWriter) {
        (MajorMinor::VERSION_1_0 as MajorMinor).write_into(writer);
        (0 as u16).write_into(writer);
        (array_len(&self.axis_segment_maps).unwrap() as u16).write_into(writer);
        self.axis_segment_maps.write_into(writer);
    }
    fn table_type(&self) -> TableType {
        TableType::TopLevel(Avar::TAG)
    }
}

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

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

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

impl<'a> FromTableRef<read_fonts::tables::avar::Avar<'a>> for Avar {}

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

/// [SegmentMaps](https://learn.microsoft.com/en-us/typography/opentype/spec/avar#table-formats) record
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct SegmentMaps {
    /// The array of axis value map records for this axis.
    pub axis_value_maps: Vec<AxisValueMap>,
}

impl SegmentMaps {
    /// Construct a new `SegmentMaps`
    pub fn new(axis_value_maps: Vec<AxisValueMap>) -> Self {
        Self {
            axis_value_maps: axis_value_maps.into_iter().map(Into::into).collect(),
        }
    }
}

impl FontWrite for SegmentMaps {
    #[allow(clippy::unnecessary_cast)]
    fn write_into(&self, writer: &mut TableWriter) {
        (array_len(&self.axis_value_maps).unwrap() as u16).write_into(writer);
        self.axis_value_maps.write_into(writer);
    }
    fn table_type(&self) -> TableType {
        TableType::Named("SegmentMaps")
    }
}

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

impl FromObjRef<read_fonts::tables::avar::SegmentMaps<'_>> for SegmentMaps {
    fn from_obj_ref(obj: &read_fonts::tables::avar::SegmentMaps, offset_data: FontData) -> Self {
        SegmentMaps {
            axis_value_maps: obj.axis_value_maps().to_owned_obj(offset_data),
        }
    }
}

/// [AxisValueMap](https://learn.microsoft.com/en-us/typography/opentype/spec/avar#table-formats) record
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct AxisValueMap {
    /// A normalized coordinate value obtained using default normalization.
    pub from_coordinate: F2Dot14,
    /// The modified, normalized coordinate value.
    pub to_coordinate: F2Dot14,
}

impl AxisValueMap {
    /// Construct a new `AxisValueMap`
    pub fn new(from_coordinate: F2Dot14, to_coordinate: F2Dot14) -> Self {
        Self {
            from_coordinate,
            to_coordinate,
        }
    }
}

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

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

impl FromObjRef<read_fonts::tables::avar::AxisValueMap> for AxisValueMap {
    fn from_obj_ref(obj: &read_fonts::tables::avar::AxisValueMap, _: FontData) -> Self {
        AxisValueMap {
            from_coordinate: obj.from_coordinate(),
            to_coordinate: obj.to_coordinate(),
        }
    }
}