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        start..start + Version16Dot16::RAW_BYTE_LEN
167    }
168
169    pub fn num_glyphs_byte_range(&self) -> Range<usize> {
170        let start = self.version_byte_range().end;
171        start..start + u16::RAW_BYTE_LEN
172    }
173
174    pub fn max_points_byte_range(&self) -> Range<usize> {
175        let start = self.num_glyphs_byte_range().end;
176        start
177            ..(self.version().compatible((1u16, 0u16)))
178                .then(|| start + u16::RAW_BYTE_LEN)
179                .unwrap_or(start)
180    }
181
182    pub fn max_contours_byte_range(&self) -> Range<usize> {
183        let start = self.max_points_byte_range().end;
184        start
185            ..(self.version().compatible((1u16, 0u16)))
186                .then(|| start + u16::RAW_BYTE_LEN)
187                .unwrap_or(start)
188    }
189
190    pub fn max_composite_points_byte_range(&self) -> Range<usize> {
191        let start = self.max_contours_byte_range().end;
192        start
193            ..(self.version().compatible((1u16, 0u16)))
194                .then(|| start + u16::RAW_BYTE_LEN)
195                .unwrap_or(start)
196    }
197
198    pub fn max_composite_contours_byte_range(&self) -> Range<usize> {
199        let start = self.max_composite_points_byte_range().end;
200        start
201            ..(self.version().compatible((1u16, 0u16)))
202                .then(|| start + u16::RAW_BYTE_LEN)
203                .unwrap_or(start)
204    }
205
206    pub fn max_zones_byte_range(&self) -> Range<usize> {
207        let start = self.max_composite_contours_byte_range().end;
208        start
209            ..(self.version().compatible((1u16, 0u16)))
210                .then(|| start + u16::RAW_BYTE_LEN)
211                .unwrap_or(start)
212    }
213
214    pub fn max_twilight_points_byte_range(&self) -> Range<usize> {
215        let start = self.max_zones_byte_range().end;
216        start
217            ..(self.version().compatible((1u16, 0u16)))
218                .then(|| start + u16::RAW_BYTE_LEN)
219                .unwrap_or(start)
220    }
221
222    pub fn max_storage_byte_range(&self) -> Range<usize> {
223        let start = self.max_twilight_points_byte_range().end;
224        start
225            ..(self.version().compatible((1u16, 0u16)))
226                .then(|| start + u16::RAW_BYTE_LEN)
227                .unwrap_or(start)
228    }
229
230    pub fn max_function_defs_byte_range(&self) -> Range<usize> {
231        let start = self.max_storage_byte_range().end;
232        start
233            ..(self.version().compatible((1u16, 0u16)))
234                .then(|| start + u16::RAW_BYTE_LEN)
235                .unwrap_or(start)
236    }
237
238    pub fn max_instruction_defs_byte_range(&self) -> Range<usize> {
239        let start = self.max_function_defs_byte_range().end;
240        start
241            ..(self.version().compatible((1u16, 0u16)))
242                .then(|| start + u16::RAW_BYTE_LEN)
243                .unwrap_or(start)
244    }
245
246    pub fn max_stack_elements_byte_range(&self) -> Range<usize> {
247        let start = self.max_instruction_defs_byte_range().end;
248        start
249            ..(self.version().compatible((1u16, 0u16)))
250                .then(|| start + u16::RAW_BYTE_LEN)
251                .unwrap_or(start)
252    }
253
254    pub fn max_size_of_instructions_byte_range(&self) -> Range<usize> {
255        let start = self.max_stack_elements_byte_range().end;
256        start
257            ..(self.version().compatible((1u16, 0u16)))
258                .then(|| start + u16::RAW_BYTE_LEN)
259                .unwrap_or(start)
260    }
261
262    pub fn max_component_elements_byte_range(&self) -> Range<usize> {
263        let start = self.max_size_of_instructions_byte_range().end;
264        start
265            ..(self.version().compatible((1u16, 0u16)))
266                .then(|| start + u16::RAW_BYTE_LEN)
267                .unwrap_or(start)
268    }
269
270    pub fn max_component_depth_byte_range(&self) -> Range<usize> {
271        let start = self.max_component_elements_byte_range().end;
272        start
273            ..(self.version().compatible((1u16, 0u16)))
274                .then(|| start + u16::RAW_BYTE_LEN)
275                .unwrap_or(start)
276    }
277}
278
279#[cfg(feature = "experimental_traverse")]
280impl<'a> SomeTable<'a> for Maxp<'a> {
281    fn type_name(&self) -> &str {
282        "Maxp"
283    }
284    fn get_field(&self, idx: usize) -> Option<Field<'a>> {
285        match idx {
286            0usize => Some(Field::new("version", self.version())),
287            1usize => Some(Field::new("num_glyphs", self.num_glyphs())),
288            2usize if self.version().compatible((1u16, 0u16)) => {
289                Some(Field::new("max_points", self.max_points().unwrap()))
290            }
291            3usize if self.version().compatible((1u16, 0u16)) => {
292                Some(Field::new("max_contours", self.max_contours().unwrap()))
293            }
294            4usize if self.version().compatible((1u16, 0u16)) => Some(Field::new(
295                "max_composite_points",
296                self.max_composite_points().unwrap(),
297            )),
298            5usize if self.version().compatible((1u16, 0u16)) => Some(Field::new(
299                "max_composite_contours",
300                self.max_composite_contours().unwrap(),
301            )),
302            6usize if self.version().compatible((1u16, 0u16)) => {
303                Some(Field::new("max_zones", self.max_zones().unwrap()))
304            }
305            7usize if self.version().compatible((1u16, 0u16)) => Some(Field::new(
306                "max_twilight_points",
307                self.max_twilight_points().unwrap(),
308            )),
309            8usize if self.version().compatible((1u16, 0u16)) => {
310                Some(Field::new("max_storage", self.max_storage().unwrap()))
311            }
312            9usize if self.version().compatible((1u16, 0u16)) => Some(Field::new(
313                "max_function_defs",
314                self.max_function_defs().unwrap(),
315            )),
316            10usize if self.version().compatible((1u16, 0u16)) => Some(Field::new(
317                "max_instruction_defs",
318                self.max_instruction_defs().unwrap(),
319            )),
320            11usize if self.version().compatible((1u16, 0u16)) => Some(Field::new(
321                "max_stack_elements",
322                self.max_stack_elements().unwrap(),
323            )),
324            12usize if self.version().compatible((1u16, 0u16)) => Some(Field::new(
325                "max_size_of_instructions",
326                self.max_size_of_instructions().unwrap(),
327            )),
328            13usize if self.version().compatible((1u16, 0u16)) => Some(Field::new(
329                "max_component_elements",
330                self.max_component_elements().unwrap(),
331            )),
332            14usize if self.version().compatible((1u16, 0u16)) => Some(Field::new(
333                "max_component_depth",
334                self.max_component_depth().unwrap(),
335            )),
336            _ => None,
337        }
338    }
339}
340
341#[cfg(feature = "experimental_traverse")]
342#[allow(clippy::needless_lifetimes)]
343impl<'a> std::fmt::Debug for Maxp<'a> {
344    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
345        (self as &dyn SomeTable<'a>).fmt(f)
346    }
347}