Skip to main content

read_fonts/generated/
generated_maxp.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
8impl<'a> MinByteRange<'a> for Maxp<'a> {
9    fn min_byte_range(&self) -> Range<usize> {
10        0..self.num_glyphs_byte_range().end
11    }
12    fn min_table_bytes(&self) -> &'a [u8] {
13        let range = self.min_byte_range();
14        self.data.as_bytes().get(range).unwrap_or_default()
15    }
16}
17
18impl TopLevelTable for Maxp<'_> {
19    /// `maxp`
20    const TAG: Tag = Tag::new(b"maxp");
21}
22
23impl<'a> FontRead<'a> for Maxp<'a> {
24    fn read(data: FontData<'a>) -> Result<Self, ReadError> {
25        #[allow(clippy::absurd_extreme_comparisons)]
26        if data.len() < Self::MIN_SIZE {
27            return Err(ReadError::OutOfBounds);
28        }
29        Ok(Self { data })
30    }
31}
32
33/// [`maxp`](https://docs.microsoft.com/en-us/typography/opentype/spec/maxp)
34#[derive(Clone)]
35pub struct Maxp<'a> {
36    data: FontData<'a>,
37}
38
39#[allow(clippy::needless_lifetimes)]
40impl<'a> Maxp<'a> {
41    pub const MIN_SIZE: usize = (Version16Dot16::RAW_BYTE_LEN + u16::RAW_BYTE_LEN);
42    basic_table_impls!(impl_the_methods);
43
44    /// The version: 0x00005000 for version 0.5, 0x00010000 for version 1.0.
45    pub fn version(&self) -> Version16Dot16 {
46        let range = self.version_byte_range();
47        self.data.read_at(range.start).ok().unwrap()
48    }
49
50    /// The number of glyphs in the font.
51    pub fn num_glyphs(&self) -> u16 {
52        let range = self.num_glyphs_byte_range();
53        self.data.read_at(range.start).ok().unwrap()
54    }
55
56    /// Maximum points in a non-composite glyph.
57    pub fn max_points(&self) -> Option<u16> {
58        let range = self.max_points_byte_range();
59        (!range.is_empty())
60            .then(|| self.data.read_at(range.start).ok())
61            .flatten()
62    }
63
64    /// Maximum contours in a non-composite glyph.
65    pub fn max_contours(&self) -> Option<u16> {
66        let range = self.max_contours_byte_range();
67        (!range.is_empty())
68            .then(|| self.data.read_at(range.start).ok())
69            .flatten()
70    }
71
72    /// Maximum points in a composite glyph.
73    pub fn max_composite_points(&self) -> Option<u16> {
74        let range = self.max_composite_points_byte_range();
75        (!range.is_empty())
76            .then(|| self.data.read_at(range.start).ok())
77            .flatten()
78    }
79
80    /// Maximum contours in a composite glyph.
81    pub fn max_composite_contours(&self) -> Option<u16> {
82        let range = self.max_composite_contours_byte_range();
83        (!range.is_empty())
84            .then(|| self.data.read_at(range.start).ok())
85            .flatten()
86    }
87
88    /// 1 if instructions do not use the twilight zone (Z0), or 2 if
89    /// instructions do use Z0; should be set to 2 in most cases.
90    pub fn max_zones(&self) -> Option<u16> {
91        let range = self.max_zones_byte_range();
92        (!range.is_empty())
93            .then(|| self.data.read_at(range.start).ok())
94            .flatten()
95    }
96
97    /// Maximum points used in Z0.
98    pub fn max_twilight_points(&self) -> Option<u16> {
99        let range = self.max_twilight_points_byte_range();
100        (!range.is_empty())
101            .then(|| self.data.read_at(range.start).ok())
102            .flatten()
103    }
104
105    /// Number of Storage Area locations.
106    pub fn max_storage(&self) -> Option<u16> {
107        let range = self.max_storage_byte_range();
108        (!range.is_empty())
109            .then(|| self.data.read_at(range.start).ok())
110            .flatten()
111    }
112
113    /// Number of FDEFs, equal to the highest function number + 1.
114    pub fn max_function_defs(&self) -> Option<u16> {
115        let range = self.max_function_defs_byte_range();
116        (!range.is_empty())
117            .then(|| self.data.read_at(range.start).ok())
118            .flatten()
119    }
120
121    /// Number of IDEFs.
122    pub fn max_instruction_defs(&self) -> Option<u16> {
123        let range = self.max_instruction_defs_byte_range();
124        (!range.is_empty())
125            .then(|| self.data.read_at(range.start).ok())
126            .flatten()
127    }
128
129    /// Maximum stack depth across Font Program ('fpgm' table), CVT
130    /// Program ('prep' table) and all glyph instructions (in the
131    /// 'glyf' table).
132    pub fn max_stack_elements(&self) -> Option<u16> {
133        let range = self.max_stack_elements_byte_range();
134        (!range.is_empty())
135            .then(|| self.data.read_at(range.start).ok())
136            .flatten()
137    }
138
139    /// Maximum byte count for glyph instructions.
140    pub fn max_size_of_instructions(&self) -> Option<u16> {
141        let range = self.max_size_of_instructions_byte_range();
142        (!range.is_empty())
143            .then(|| self.data.read_at(range.start).ok())
144            .flatten()
145    }
146
147    /// Maximum number of components referenced at “top level” for
148    /// any composite glyph.
149    pub fn max_component_elements(&self) -> Option<u16> {
150        let range = self.max_component_elements_byte_range();
151        (!range.is_empty())
152            .then(|| self.data.read_at(range.start).ok())
153            .flatten()
154    }
155
156    /// Maximum levels of recursion; 1 for simple components.
157    pub fn max_component_depth(&self) -> Option<u16> {
158        let range = self.max_component_depth_byte_range();
159        (!range.is_empty())
160            .then(|| self.data.read_at(range.start).ok())
161            .flatten()
162    }
163
164    pub fn version_byte_range(&self) -> Range<usize> {
165        let start = 0;
166        let end = start + Version16Dot16::RAW_BYTE_LEN;
167        start..end
168    }
169
170    pub fn num_glyphs_byte_range(&self) -> Range<usize> {
171        let start = self.version_byte_range().end;
172        let end = start + u16::RAW_BYTE_LEN;
173        start..end
174    }
175
176    pub fn max_points_byte_range(&self) -> Range<usize> {
177        let start = self.num_glyphs_byte_range().end;
178        let end = if self.version().compatible((1u16, 0u16)) {
179            start + u16::RAW_BYTE_LEN
180        } else {
181            start
182        };
183        start..end
184    }
185
186    pub fn max_contours_byte_range(&self) -> Range<usize> {
187        let start = self.max_points_byte_range().end;
188        let end = if self.version().compatible((1u16, 0u16)) {
189            start + u16::RAW_BYTE_LEN
190        } else {
191            start
192        };
193        start..end
194    }
195
196    pub fn max_composite_points_byte_range(&self) -> Range<usize> {
197        let start = self.max_contours_byte_range().end;
198        let end = if self.version().compatible((1u16, 0u16)) {
199            start + u16::RAW_BYTE_LEN
200        } else {
201            start
202        };
203        start..end
204    }
205
206    pub fn max_composite_contours_byte_range(&self) -> Range<usize> {
207        let start = self.max_composite_points_byte_range().end;
208        let end = if self.version().compatible((1u16, 0u16)) {
209            start + u16::RAW_BYTE_LEN
210        } else {
211            start
212        };
213        start..end
214    }
215
216    pub fn max_zones_byte_range(&self) -> Range<usize> {
217        let start = self.max_composite_contours_byte_range().end;
218        let end = if self.version().compatible((1u16, 0u16)) {
219            start + u16::RAW_BYTE_LEN
220        } else {
221            start
222        };
223        start..end
224    }
225
226    pub fn max_twilight_points_byte_range(&self) -> Range<usize> {
227        let start = self.max_zones_byte_range().end;
228        let end = if self.version().compatible((1u16, 0u16)) {
229            start + u16::RAW_BYTE_LEN
230        } else {
231            start
232        };
233        start..end
234    }
235
236    pub fn max_storage_byte_range(&self) -> Range<usize> {
237        let start = self.max_twilight_points_byte_range().end;
238        let end = if self.version().compatible((1u16, 0u16)) {
239            start + u16::RAW_BYTE_LEN
240        } else {
241            start
242        };
243        start..end
244    }
245
246    pub fn max_function_defs_byte_range(&self) -> Range<usize> {
247        let start = self.max_storage_byte_range().end;
248        let end = if self.version().compatible((1u16, 0u16)) {
249            start + u16::RAW_BYTE_LEN
250        } else {
251            start
252        };
253        start..end
254    }
255
256    pub fn max_instruction_defs_byte_range(&self) -> Range<usize> {
257        let start = self.max_function_defs_byte_range().end;
258        let end = if self.version().compatible((1u16, 0u16)) {
259            start + u16::RAW_BYTE_LEN
260        } else {
261            start
262        };
263        start..end
264    }
265
266    pub fn max_stack_elements_byte_range(&self) -> Range<usize> {
267        let start = self.max_instruction_defs_byte_range().end;
268        let end = if self.version().compatible((1u16, 0u16)) {
269            start + u16::RAW_BYTE_LEN
270        } else {
271            start
272        };
273        start..end
274    }
275
276    pub fn max_size_of_instructions_byte_range(&self) -> Range<usize> {
277        let start = self.max_stack_elements_byte_range().end;
278        let end = if self.version().compatible((1u16, 0u16)) {
279            start + u16::RAW_BYTE_LEN
280        } else {
281            start
282        };
283        start..end
284    }
285
286    pub fn max_component_elements_byte_range(&self) -> Range<usize> {
287        let start = self.max_size_of_instructions_byte_range().end;
288        let end = if self.version().compatible((1u16, 0u16)) {
289            start + u16::RAW_BYTE_LEN
290        } else {
291            start
292        };
293        start..end
294    }
295
296    pub fn max_component_depth_byte_range(&self) -> Range<usize> {
297        let start = self.max_component_elements_byte_range().end;
298        let end = if self.version().compatible((1u16, 0u16)) {
299            start + u16::RAW_BYTE_LEN
300        } else {
301            start
302        };
303        start..end
304    }
305}
306
307const _: () = assert!(FontData::default_data_long_enough(Maxp::MIN_SIZE));
308
309impl Default for Maxp<'_> {
310    fn default() -> Self {
311        Self {
312            data: FontData::default_table_data(),
313        }
314    }
315}
316
317#[cfg(feature = "experimental_traverse")]
318impl<'a> SomeTable<'a> for Maxp<'a> {
319    fn type_name(&self) -> &str {
320        "Maxp"
321    }
322    fn get_field(&self, idx: usize) -> Option<Field<'a>> {
323        match idx {
324            0usize => Some(Field::new("version", self.version())),
325            1usize => Some(Field::new("num_glyphs", self.num_glyphs())),
326            2usize if self.version().compatible((1u16, 0u16)) => {
327                Some(Field::new("max_points", self.max_points().unwrap()))
328            }
329            3usize if self.version().compatible((1u16, 0u16)) => {
330                Some(Field::new("max_contours", self.max_contours().unwrap()))
331            }
332            4usize if self.version().compatible((1u16, 0u16)) => Some(Field::new(
333                "max_composite_points",
334                self.max_composite_points().unwrap(),
335            )),
336            5usize if self.version().compatible((1u16, 0u16)) => Some(Field::new(
337                "max_composite_contours",
338                self.max_composite_contours().unwrap(),
339            )),
340            6usize if self.version().compatible((1u16, 0u16)) => {
341                Some(Field::new("max_zones", self.max_zones().unwrap()))
342            }
343            7usize if self.version().compatible((1u16, 0u16)) => Some(Field::new(
344                "max_twilight_points",
345                self.max_twilight_points().unwrap(),
346            )),
347            8usize if self.version().compatible((1u16, 0u16)) => {
348                Some(Field::new("max_storage", self.max_storage().unwrap()))
349            }
350            9usize if self.version().compatible((1u16, 0u16)) => Some(Field::new(
351                "max_function_defs",
352                self.max_function_defs().unwrap(),
353            )),
354            10usize if self.version().compatible((1u16, 0u16)) => Some(Field::new(
355                "max_instruction_defs",
356                self.max_instruction_defs().unwrap(),
357            )),
358            11usize if self.version().compatible((1u16, 0u16)) => Some(Field::new(
359                "max_stack_elements",
360                self.max_stack_elements().unwrap(),
361            )),
362            12usize if self.version().compatible((1u16, 0u16)) => Some(Field::new(
363                "max_size_of_instructions",
364                self.max_size_of_instructions().unwrap(),
365            )),
366            13usize if self.version().compatible((1u16, 0u16)) => Some(Field::new(
367                "max_component_elements",
368                self.max_component_elements().unwrap(),
369            )),
370            14usize if self.version().compatible((1u16, 0u16)) => Some(Field::new(
371                "max_component_depth",
372                self.max_component_depth().unwrap(),
373            )),
374            _ => None,
375        }
376    }
377}
378
379#[cfg(feature = "experimental_traverse")]
380#[allow(clippy::needless_lifetimes)]
381impl<'a> std::fmt::Debug for Maxp<'a> {
382    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
383        (self as &dyn SomeTable<'a>).fmt(f)
384    }
385}