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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
// 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::*;

/// <https://docs.microsoft.com/en-us/typography/opentype/spec/head>
#[derive(Debug, Clone, Copy)]
#[doc(hidden)]
pub struct HeadMarker {}

impl HeadMarker {
    fn version_byte_range(&self) -> Range<usize> {
        let start = 0;
        start..start + MajorMinor::RAW_BYTE_LEN
    }
    fn font_revision_byte_range(&self) -> Range<usize> {
        let start = self.version_byte_range().end;
        start..start + Fixed::RAW_BYTE_LEN
    }
    fn checksum_adjustment_byte_range(&self) -> Range<usize> {
        let start = self.font_revision_byte_range().end;
        start..start + u32::RAW_BYTE_LEN
    }
    fn magic_number_byte_range(&self) -> Range<usize> {
        let start = self.checksum_adjustment_byte_range().end;
        start..start + u32::RAW_BYTE_LEN
    }
    fn flags_byte_range(&self) -> Range<usize> {
        let start = self.magic_number_byte_range().end;
        start..start + u16::RAW_BYTE_LEN
    }
    fn units_per_em_byte_range(&self) -> Range<usize> {
        let start = self.flags_byte_range().end;
        start..start + u16::RAW_BYTE_LEN
    }
    fn created_byte_range(&self) -> Range<usize> {
        let start = self.units_per_em_byte_range().end;
        start..start + LongDateTime::RAW_BYTE_LEN
    }
    fn modified_byte_range(&self) -> Range<usize> {
        let start = self.created_byte_range().end;
        start..start + LongDateTime::RAW_BYTE_LEN
    }
    fn x_min_byte_range(&self) -> Range<usize> {
        let start = self.modified_byte_range().end;
        start..start + i16::RAW_BYTE_LEN
    }
    fn y_min_byte_range(&self) -> Range<usize> {
        let start = self.x_min_byte_range().end;
        start..start + i16::RAW_BYTE_LEN
    }
    fn x_max_byte_range(&self) -> Range<usize> {
        let start = self.y_min_byte_range().end;
        start..start + i16::RAW_BYTE_LEN
    }
    fn y_max_byte_range(&self) -> Range<usize> {
        let start = self.x_max_byte_range().end;
        start..start + i16::RAW_BYTE_LEN
    }
    fn mac_style_byte_range(&self) -> Range<usize> {
        let start = self.y_max_byte_range().end;
        start..start + u16::RAW_BYTE_LEN
    }
    fn lowest_rec_ppem_byte_range(&self) -> Range<usize> {
        let start = self.mac_style_byte_range().end;
        start..start + u16::RAW_BYTE_LEN
    }
    fn font_direction_hint_byte_range(&self) -> Range<usize> {
        let start = self.lowest_rec_ppem_byte_range().end;
        start..start + i16::RAW_BYTE_LEN
    }
    fn index_to_loc_format_byte_range(&self) -> Range<usize> {
        let start = self.font_direction_hint_byte_range().end;
        start..start + i16::RAW_BYTE_LEN
    }
    fn glyph_data_format_byte_range(&self) -> Range<usize> {
        let start = self.index_to_loc_format_byte_range().end;
        start..start + i16::RAW_BYTE_LEN
    }
}

impl TopLevelTable for Head<'_> {
    /// `head`
    const TAG: Tag = Tag::new(b"head");
}

impl<'a> FontRead<'a> for Head<'a> {
    fn read(data: FontData<'a>) -> Result<Self, ReadError> {
        let mut cursor = data.cursor();
        cursor.advance::<MajorMinor>();
        cursor.advance::<Fixed>();
        cursor.advance::<u32>();
        cursor.advance::<u32>();
        cursor.advance::<u16>();
        cursor.advance::<u16>();
        cursor.advance::<LongDateTime>();
        cursor.advance::<LongDateTime>();
        cursor.advance::<i16>();
        cursor.advance::<i16>();
        cursor.advance::<i16>();
        cursor.advance::<i16>();
        cursor.advance::<u16>();
        cursor.advance::<u16>();
        cursor.advance::<i16>();
        cursor.advance::<i16>();
        cursor.advance::<i16>();
        cursor.finish(HeadMarker {})
    }
}

/// <https://docs.microsoft.com/en-us/typography/opentype/spec/head>
pub type Head<'a> = TableRef<'a, HeadMarker>;

impl<'a> Head<'a> {
    /// Version number of the font header table, set to (1, 0)
    pub fn version(&self) -> MajorMinor {
        let range = self.shape.version_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// Set by font manufacturer.
    pub fn font_revision(&self) -> Fixed {
        let range = self.shape.font_revision_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// To compute: set it to 0, sum the entire font as uint32, then
    /// store 0xB1B0AFBA - sum. If the font is used as a component in a
    /// font collection file, the value of this field will be
    /// invalidated by changes to the file structure and font table
    /// directory, and must be ignored.
    pub fn checksum_adjustment(&self) -> u32 {
        let range = self.shape.checksum_adjustment_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// Set to 0x5F0F3CF5.
    pub fn magic_number(&self) -> u32 {
        let range = self.shape.magic_number_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// See the flags enum
    pub fn flags(&self) -> u16 {
        let range = self.shape.flags_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// Set to a value from 16 to 16384. Any value in this range is
    /// valid. In fonts that have TrueType outlines, a power of 2 is
    /// recommended as this allows performance optimizations in some
    /// rasterizers.
    pub fn units_per_em(&self) -> u16 {
        let range = self.shape.units_per_em_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// Number of seconds since 12:00 midnight that started January 1st
    /// 1904 in GMT/UTC time zone.
    pub fn created(&self) -> LongDateTime {
        let range = self.shape.created_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// Number of seconds since 12:00 midnight that started January 1st
    /// 1904 in GMT/UTC time zone.
    pub fn modified(&self) -> LongDateTime {
        let range = self.shape.modified_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// Minimum x coordinate across all glyph bounding boxes.
    pub fn x_min(&self) -> i16 {
        let range = self.shape.x_min_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// Minimum y coordinate across all glyph bounding boxes.
    pub fn y_min(&self) -> i16 {
        let range = self.shape.y_min_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// Maximum x coordinate across all glyph bounding boxes.
    pub fn x_max(&self) -> i16 {
        let range = self.shape.x_max_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// Maximum y coordinate across all glyph bounding boxes.
    pub fn y_max(&self) -> i16 {
        let range = self.shape.y_max_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// see somewhere else
    pub fn mac_style(&self) -> u16 {
        let range = self.shape.mac_style_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// Smallest readable size in pixels.
    pub fn lowest_rec_ppem(&self) -> u16 {
        let range = self.shape.lowest_rec_ppem_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// Deprecated (Set to 2).
    pub fn font_direction_hint(&self) -> i16 {
        let range = self.shape.font_direction_hint_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// 0 for short offsets (Offset16), 1 for long (Offset32).
    pub fn index_to_loc_format(&self) -> i16 {
        let range = self.shape.index_to_loc_format_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// 0 for current format.
    pub fn glyph_data_format(&self) -> i16 {
        let range = self.shape.glyph_data_format_byte_range();
        self.data.read_at(range.start).unwrap()
    }
}

#[cfg(feature = "traversal")]
impl<'a> SomeTable<'a> for Head<'a> {
    fn type_name(&self) -> &str {
        "Head"
    }
    fn get_field(&self, idx: usize) -> Option<Field<'a>> {
        match idx {
            0usize => Some(Field::new("version", self.version())),
            1usize => Some(Field::new("font_revision", self.font_revision())),
            2usize => Some(Field::new(
                "checksum_adjustment",
                self.checksum_adjustment(),
            )),
            3usize => Some(Field::new("magic_number", self.magic_number())),
            4usize => Some(Field::new("flags", self.flags())),
            5usize => Some(Field::new("units_per_em", self.units_per_em())),
            6usize => Some(Field::new("created", self.created())),
            7usize => Some(Field::new("modified", self.modified())),
            8usize => Some(Field::new("x_min", self.x_min())),
            9usize => Some(Field::new("y_min", self.y_min())),
            10usize => Some(Field::new("x_max", self.x_max())),
            11usize => Some(Field::new("y_max", self.y_max())),
            12usize => Some(Field::new("mac_style", self.mac_style())),
            13usize => Some(Field::new("lowest_rec_ppem", self.lowest_rec_ppem())),
            14usize => Some(Field::new(
                "font_direction_hint",
                self.font_direction_hint(),
            )),
            15usize => Some(Field::new(
                "index_to_loc_format",
                self.index_to_loc_format(),
            )),
            16usize => Some(Field::new("glyph_data_format", self.glyph_data_format())),
            _ => None,
        }
    }
}

#[cfg(feature = "traversal")]
impl<'a> std::fmt::Debug for Head<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        (self as &dyn SomeTable<'a>).fmt(f)
    }
}