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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
// 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::*;

/// [`maxp`](https://docs.microsoft.com/en-us/typography/opentype/spec/maxp)
#[derive(Debug, Clone, Copy)]
#[doc(hidden)]
pub struct MaxpMarker {
    max_points_byte_start: Option<usize>,
    max_contours_byte_start: Option<usize>,
    max_composite_points_byte_start: Option<usize>,
    max_composite_contours_byte_start: Option<usize>,
    max_zones_byte_start: Option<usize>,
    max_twilight_points_byte_start: Option<usize>,
    max_storage_byte_start: Option<usize>,
    max_function_defs_byte_start: Option<usize>,
    max_instruction_defs_byte_start: Option<usize>,
    max_stack_elements_byte_start: Option<usize>,
    max_size_of_instructions_byte_start: Option<usize>,
    max_component_elements_byte_start: Option<usize>,
    max_component_depth_byte_start: Option<usize>,
}

impl MaxpMarker {
    fn version_byte_range(&self) -> Range<usize> {
        let start = 0;
        start..start + Version16Dot16::RAW_BYTE_LEN
    }
    fn num_glyphs_byte_range(&self) -> Range<usize> {
        let start = self.version_byte_range().end;
        start..start + u16::RAW_BYTE_LEN
    }
    fn max_points_byte_range(&self) -> Option<Range<usize>> {
        let start = self.max_points_byte_start?;
        Some(start..start + u16::RAW_BYTE_LEN)
    }
    fn max_contours_byte_range(&self) -> Option<Range<usize>> {
        let start = self.max_contours_byte_start?;
        Some(start..start + u16::RAW_BYTE_LEN)
    }
    fn max_composite_points_byte_range(&self) -> Option<Range<usize>> {
        let start = self.max_composite_points_byte_start?;
        Some(start..start + u16::RAW_BYTE_LEN)
    }
    fn max_composite_contours_byte_range(&self) -> Option<Range<usize>> {
        let start = self.max_composite_contours_byte_start?;
        Some(start..start + u16::RAW_BYTE_LEN)
    }
    fn max_zones_byte_range(&self) -> Option<Range<usize>> {
        let start = self.max_zones_byte_start?;
        Some(start..start + u16::RAW_BYTE_LEN)
    }
    fn max_twilight_points_byte_range(&self) -> Option<Range<usize>> {
        let start = self.max_twilight_points_byte_start?;
        Some(start..start + u16::RAW_BYTE_LEN)
    }
    fn max_storage_byte_range(&self) -> Option<Range<usize>> {
        let start = self.max_storage_byte_start?;
        Some(start..start + u16::RAW_BYTE_LEN)
    }
    fn max_function_defs_byte_range(&self) -> Option<Range<usize>> {
        let start = self.max_function_defs_byte_start?;
        Some(start..start + u16::RAW_BYTE_LEN)
    }
    fn max_instruction_defs_byte_range(&self) -> Option<Range<usize>> {
        let start = self.max_instruction_defs_byte_start?;
        Some(start..start + u16::RAW_BYTE_LEN)
    }
    fn max_stack_elements_byte_range(&self) -> Option<Range<usize>> {
        let start = self.max_stack_elements_byte_start?;
        Some(start..start + u16::RAW_BYTE_LEN)
    }
    fn max_size_of_instructions_byte_range(&self) -> Option<Range<usize>> {
        let start = self.max_size_of_instructions_byte_start?;
        Some(start..start + u16::RAW_BYTE_LEN)
    }
    fn max_component_elements_byte_range(&self) -> Option<Range<usize>> {
        let start = self.max_component_elements_byte_start?;
        Some(start..start + u16::RAW_BYTE_LEN)
    }
    fn max_component_depth_byte_range(&self) -> Option<Range<usize>> {
        let start = self.max_component_depth_byte_start?;
        Some(start..start + u16::RAW_BYTE_LEN)
    }
}

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

impl<'a> FontRead<'a> for Maxp<'a> {
    fn read(data: FontData<'a>) -> Result<Self, ReadError> {
        let mut cursor = data.cursor();
        let version: Version16Dot16 = cursor.read()?;
        cursor.advance::<u16>();
        let max_points_byte_start = version
            .compatible((1u16, 0u16))
            .then(|| cursor.position())
            .transpose()?;
        version
            .compatible((1u16, 0u16))
            .then(|| cursor.advance::<u16>());
        let max_contours_byte_start = version
            .compatible((1u16, 0u16))
            .then(|| cursor.position())
            .transpose()?;
        version
            .compatible((1u16, 0u16))
            .then(|| cursor.advance::<u16>());
        let max_composite_points_byte_start = version
            .compatible((1u16, 0u16))
            .then(|| cursor.position())
            .transpose()?;
        version
            .compatible((1u16, 0u16))
            .then(|| cursor.advance::<u16>());
        let max_composite_contours_byte_start = version
            .compatible((1u16, 0u16))
            .then(|| cursor.position())
            .transpose()?;
        version
            .compatible((1u16, 0u16))
            .then(|| cursor.advance::<u16>());
        let max_zones_byte_start = version
            .compatible((1u16, 0u16))
            .then(|| cursor.position())
            .transpose()?;
        version
            .compatible((1u16, 0u16))
            .then(|| cursor.advance::<u16>());
        let max_twilight_points_byte_start = version
            .compatible((1u16, 0u16))
            .then(|| cursor.position())
            .transpose()?;
        version
            .compatible((1u16, 0u16))
            .then(|| cursor.advance::<u16>());
        let max_storage_byte_start = version
            .compatible((1u16, 0u16))
            .then(|| cursor.position())
            .transpose()?;
        version
            .compatible((1u16, 0u16))
            .then(|| cursor.advance::<u16>());
        let max_function_defs_byte_start = version
            .compatible((1u16, 0u16))
            .then(|| cursor.position())
            .transpose()?;
        version
            .compatible((1u16, 0u16))
            .then(|| cursor.advance::<u16>());
        let max_instruction_defs_byte_start = version
            .compatible((1u16, 0u16))
            .then(|| cursor.position())
            .transpose()?;
        version
            .compatible((1u16, 0u16))
            .then(|| cursor.advance::<u16>());
        let max_stack_elements_byte_start = version
            .compatible((1u16, 0u16))
            .then(|| cursor.position())
            .transpose()?;
        version
            .compatible((1u16, 0u16))
            .then(|| cursor.advance::<u16>());
        let max_size_of_instructions_byte_start = version
            .compatible((1u16, 0u16))
            .then(|| cursor.position())
            .transpose()?;
        version
            .compatible((1u16, 0u16))
            .then(|| cursor.advance::<u16>());
        let max_component_elements_byte_start = version
            .compatible((1u16, 0u16))
            .then(|| cursor.position())
            .transpose()?;
        version
            .compatible((1u16, 0u16))
            .then(|| cursor.advance::<u16>());
        let max_component_depth_byte_start = version
            .compatible((1u16, 0u16))
            .then(|| cursor.position())
            .transpose()?;
        version
            .compatible((1u16, 0u16))
            .then(|| cursor.advance::<u16>());
        cursor.finish(MaxpMarker {
            max_points_byte_start,
            max_contours_byte_start,
            max_composite_points_byte_start,
            max_composite_contours_byte_start,
            max_zones_byte_start,
            max_twilight_points_byte_start,
            max_storage_byte_start,
            max_function_defs_byte_start,
            max_instruction_defs_byte_start,
            max_stack_elements_byte_start,
            max_size_of_instructions_byte_start,
            max_component_elements_byte_start,
            max_component_depth_byte_start,
        })
    }
}

/// [`maxp`](https://docs.microsoft.com/en-us/typography/opentype/spec/maxp)
pub type Maxp<'a> = TableRef<'a, MaxpMarker>;

impl<'a> Maxp<'a> {
    /// The version: 0x00005000 for version 0.5, 0x00010000 for version 1.0.
    pub fn version(&self) -> Version16Dot16 {
        let range = self.shape.version_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// The number of glyphs in the font.
    pub fn num_glyphs(&self) -> u16 {
        let range = self.shape.num_glyphs_byte_range();
        self.data.read_at(range.start).unwrap()
    }

    /// Maximum points in a non-composite glyph.
    pub fn max_points(&self) -> Option<u16> {
        let range = self.shape.max_points_byte_range()?;
        Some(self.data.read_at(range.start).unwrap())
    }

    /// Maximum contours in a non-composite glyph.
    pub fn max_contours(&self) -> Option<u16> {
        let range = self.shape.max_contours_byte_range()?;
        Some(self.data.read_at(range.start).unwrap())
    }

    /// Maximum points in a composite glyph.
    pub fn max_composite_points(&self) -> Option<u16> {
        let range = self.shape.max_composite_points_byte_range()?;
        Some(self.data.read_at(range.start).unwrap())
    }

    /// Maximum contours in a composite glyph.
    pub fn max_composite_contours(&self) -> Option<u16> {
        let range = self.shape.max_composite_contours_byte_range()?;
        Some(self.data.read_at(range.start).unwrap())
    }

    /// 1 if instructions do not use the twilight zone (Z0), or 2 if
    /// instructions do use Z0; should be set to 2 in most cases.
    pub fn max_zones(&self) -> Option<u16> {
        let range = self.shape.max_zones_byte_range()?;
        Some(self.data.read_at(range.start).unwrap())
    }

    /// Maximum points used in Z0.
    pub fn max_twilight_points(&self) -> Option<u16> {
        let range = self.shape.max_twilight_points_byte_range()?;
        Some(self.data.read_at(range.start).unwrap())
    }

    /// Number of Storage Area locations.
    pub fn max_storage(&self) -> Option<u16> {
        let range = self.shape.max_storage_byte_range()?;
        Some(self.data.read_at(range.start).unwrap())
    }

    /// Number of FDEFs, equal to the highest function number + 1.
    pub fn max_function_defs(&self) -> Option<u16> {
        let range = self.shape.max_function_defs_byte_range()?;
        Some(self.data.read_at(range.start).unwrap())
    }

    /// Number of IDEFs.
    pub fn max_instruction_defs(&self) -> Option<u16> {
        let range = self.shape.max_instruction_defs_byte_range()?;
        Some(self.data.read_at(range.start).unwrap())
    }

    /// Maximum stack depth across Font Program ('fpgm' table), CVT
    /// Program ('prep' table) and all glyph instructions (in the
    /// 'glyf' table).
    pub fn max_stack_elements(&self) -> Option<u16> {
        let range = self.shape.max_stack_elements_byte_range()?;
        Some(self.data.read_at(range.start).unwrap())
    }

    /// Maximum byte count for glyph instructions.
    pub fn max_size_of_instructions(&self) -> Option<u16> {
        let range = self.shape.max_size_of_instructions_byte_range()?;
        Some(self.data.read_at(range.start).unwrap())
    }

    /// Maximum number of components referenced at “top level” for
    /// any composite glyph.
    pub fn max_component_elements(&self) -> Option<u16> {
        let range = self.shape.max_component_elements_byte_range()?;
        Some(self.data.read_at(range.start).unwrap())
    }

    /// Maximum levels of recursion; 1 for simple components.
    pub fn max_component_depth(&self) -> Option<u16> {
        let range = self.shape.max_component_depth_byte_range()?;
        Some(self.data.read_at(range.start).unwrap())
    }
}

#[cfg(feature = "experimental_traverse")]
impl<'a> SomeTable<'a> for Maxp<'a> {
    fn type_name(&self) -> &str {
        "Maxp"
    }
    fn get_field(&self, idx: usize) -> Option<Field<'a>> {
        let version = self.version();
        match idx {
            0usize => Some(Field::new("version", self.version())),
            1usize => Some(Field::new("num_glyphs", self.num_glyphs())),
            2usize if version.compatible((1u16, 0u16)) => {
                Some(Field::new("max_points", self.max_points().unwrap()))
            }
            3usize if version.compatible((1u16, 0u16)) => {
                Some(Field::new("max_contours", self.max_contours().unwrap()))
            }
            4usize if version.compatible((1u16, 0u16)) => Some(Field::new(
                "max_composite_points",
                self.max_composite_points().unwrap(),
            )),
            5usize if version.compatible((1u16, 0u16)) => Some(Field::new(
                "max_composite_contours",
                self.max_composite_contours().unwrap(),
            )),
            6usize if version.compatible((1u16, 0u16)) => {
                Some(Field::new("max_zones", self.max_zones().unwrap()))
            }
            7usize if version.compatible((1u16, 0u16)) => Some(Field::new(
                "max_twilight_points",
                self.max_twilight_points().unwrap(),
            )),
            8usize if version.compatible((1u16, 0u16)) => {
                Some(Field::new("max_storage", self.max_storage().unwrap()))
            }
            9usize if version.compatible((1u16, 0u16)) => Some(Field::new(
                "max_function_defs",
                self.max_function_defs().unwrap(),
            )),
            10usize if version.compatible((1u16, 0u16)) => Some(Field::new(
                "max_instruction_defs",
                self.max_instruction_defs().unwrap(),
            )),
            11usize if version.compatible((1u16, 0u16)) => Some(Field::new(
                "max_stack_elements",
                self.max_stack_elements().unwrap(),
            )),
            12usize if version.compatible((1u16, 0u16)) => Some(Field::new(
                "max_size_of_instructions",
                self.max_size_of_instructions().unwrap(),
            )),
            13usize if version.compatible((1u16, 0u16)) => Some(Field::new(
                "max_component_elements",
                self.max_component_elements().unwrap(),
            )),
            14usize if version.compatible((1u16, 0u16)) => Some(Field::new(
                "max_component_depth",
                self.max_component_depth().unwrap(),
            )),
            _ => None,
        }
    }
}

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