write_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
8/// [`maxp`](https://docs.microsoft.com/en-us/typography/opentype/spec/maxp)
9#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
10#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
11pub struct Maxp {
12    /// The number of glyphs in the font.
13    pub num_glyphs: u16,
14    /// Maximum points in a non-composite glyph.
15    pub max_points: Option<u16>,
16    /// Maximum contours in a non-composite glyph.
17    pub max_contours: Option<u16>,
18    /// Maximum points in a composite glyph.
19    pub max_composite_points: Option<u16>,
20    /// Maximum contours in a composite glyph.
21    pub max_composite_contours: Option<u16>,
22    /// 1 if instructions do not use the twilight zone (Z0), or 2 if
23    /// instructions do use Z0; should be set to 2 in most cases.
24    pub max_zones: Option<u16>,
25    /// Maximum points used in Z0.
26    pub max_twilight_points: Option<u16>,
27    /// Number of Storage Area locations.
28    pub max_storage: Option<u16>,
29    /// Number of FDEFs, equal to the highest function number + 1.
30    pub max_function_defs: Option<u16>,
31    /// Number of IDEFs.
32    pub max_instruction_defs: Option<u16>,
33    /// Maximum stack depth across Font Program ('fpgm' table), CVT
34    /// Program ('prep' table) and all glyph instructions (in the
35    /// 'glyf' table).
36    pub max_stack_elements: Option<u16>,
37    /// Maximum byte count for glyph instructions.
38    pub max_size_of_instructions: Option<u16>,
39    /// Maximum number of components referenced at “top level” for
40    /// any composite glyph.
41    pub max_component_elements: Option<u16>,
42    /// Maximum levels of recursion; 1 for simple components.
43    pub max_component_depth: Option<u16>,
44}
45
46impl Maxp {
47    /// Construct a new `Maxp`
48    pub fn new(num_glyphs: u16) -> Self {
49        Self {
50            num_glyphs,
51            ..Default::default()
52        }
53    }
54}
55
56impl FontWrite for Maxp {
57    #[allow(clippy::unnecessary_cast)]
58    fn write_into(&self, writer: &mut TableWriter) {
59        let version = self.compute_version() as Version16Dot16;
60        version.write_into(writer);
61        self.num_glyphs.write_into(writer);
62        version.compatible((1u16, 0u16)).then(|| {
63            self.max_points
64                .as_ref()
65                .expect("missing conditional field should have failed validation")
66                .write_into(writer)
67        });
68        version.compatible((1u16, 0u16)).then(|| {
69            self.max_contours
70                .as_ref()
71                .expect("missing conditional field should have failed validation")
72                .write_into(writer)
73        });
74        version.compatible((1u16, 0u16)).then(|| {
75            self.max_composite_points
76                .as_ref()
77                .expect("missing conditional field should have failed validation")
78                .write_into(writer)
79        });
80        version.compatible((1u16, 0u16)).then(|| {
81            self.max_composite_contours
82                .as_ref()
83                .expect("missing conditional field should have failed validation")
84                .write_into(writer)
85        });
86        version.compatible((1u16, 0u16)).then(|| {
87            self.max_zones
88                .as_ref()
89                .expect("missing conditional field should have failed validation")
90                .write_into(writer)
91        });
92        version.compatible((1u16, 0u16)).then(|| {
93            self.max_twilight_points
94                .as_ref()
95                .expect("missing conditional field should have failed validation")
96                .write_into(writer)
97        });
98        version.compatible((1u16, 0u16)).then(|| {
99            self.max_storage
100                .as_ref()
101                .expect("missing conditional field should have failed validation")
102                .write_into(writer)
103        });
104        version.compatible((1u16, 0u16)).then(|| {
105            self.max_function_defs
106                .as_ref()
107                .expect("missing conditional field should have failed validation")
108                .write_into(writer)
109        });
110        version.compatible((1u16, 0u16)).then(|| {
111            self.max_instruction_defs
112                .as_ref()
113                .expect("missing conditional field should have failed validation")
114                .write_into(writer)
115        });
116        version.compatible((1u16, 0u16)).then(|| {
117            self.max_stack_elements
118                .as_ref()
119                .expect("missing conditional field should have failed validation")
120                .write_into(writer)
121        });
122        version.compatible((1u16, 0u16)).then(|| {
123            self.max_size_of_instructions
124                .as_ref()
125                .expect("missing conditional field should have failed validation")
126                .write_into(writer)
127        });
128        version.compatible((1u16, 0u16)).then(|| {
129            self.max_component_elements
130                .as_ref()
131                .expect("missing conditional field should have failed validation")
132                .write_into(writer)
133        });
134        version.compatible((1u16, 0u16)).then(|| {
135            self.max_component_depth
136                .as_ref()
137                .expect("missing conditional field should have failed validation")
138                .write_into(writer)
139        });
140    }
141    fn table_type(&self) -> TableType {
142        TableType::TopLevel(Maxp::TAG)
143    }
144}
145
146impl Validate for Maxp {
147    fn validate_impl(&self, ctx: &mut ValidationCtx) {
148        ctx.in_table("Maxp", |ctx| {
149            let version: Version16Dot16 = self.compute_version();
150            ctx.in_field("max_points", |ctx| {
151                if version.compatible((1u16, 0u16)) && self.max_points.is_none() {
152                    ctx.report(format!("field must be present for version {version}"));
153                }
154            });
155            ctx.in_field("max_contours", |ctx| {
156                if version.compatible((1u16, 0u16)) && self.max_contours.is_none() {
157                    ctx.report(format!("field must be present for version {version}"));
158                }
159            });
160            ctx.in_field("max_composite_points", |ctx| {
161                if version.compatible((1u16, 0u16)) && self.max_composite_points.is_none() {
162                    ctx.report(format!("field must be present for version {version}"));
163                }
164            });
165            ctx.in_field("max_composite_contours", |ctx| {
166                if version.compatible((1u16, 0u16)) && self.max_composite_contours.is_none() {
167                    ctx.report(format!("field must be present for version {version}"));
168                }
169            });
170            ctx.in_field("max_zones", |ctx| {
171                if version.compatible((1u16, 0u16)) && self.max_zones.is_none() {
172                    ctx.report(format!("field must be present for version {version}"));
173                }
174            });
175            ctx.in_field("max_twilight_points", |ctx| {
176                if version.compatible((1u16, 0u16)) && self.max_twilight_points.is_none() {
177                    ctx.report(format!("field must be present for version {version}"));
178                }
179            });
180            ctx.in_field("max_storage", |ctx| {
181                if version.compatible((1u16, 0u16)) && self.max_storage.is_none() {
182                    ctx.report(format!("field must be present for version {version}"));
183                }
184            });
185            ctx.in_field("max_function_defs", |ctx| {
186                if version.compatible((1u16, 0u16)) && self.max_function_defs.is_none() {
187                    ctx.report(format!("field must be present for version {version}"));
188                }
189            });
190            ctx.in_field("max_instruction_defs", |ctx| {
191                if version.compatible((1u16, 0u16)) && self.max_instruction_defs.is_none() {
192                    ctx.report(format!("field must be present for version {version}"));
193                }
194            });
195            ctx.in_field("max_stack_elements", |ctx| {
196                if version.compatible((1u16, 0u16)) && self.max_stack_elements.is_none() {
197                    ctx.report(format!("field must be present for version {version}"));
198                }
199            });
200            ctx.in_field("max_size_of_instructions", |ctx| {
201                if version.compatible((1u16, 0u16)) && self.max_size_of_instructions.is_none() {
202                    ctx.report(format!("field must be present for version {version}"));
203                }
204            });
205            ctx.in_field("max_component_elements", |ctx| {
206                if version.compatible((1u16, 0u16)) && self.max_component_elements.is_none() {
207                    ctx.report(format!("field must be present for version {version}"));
208                }
209            });
210            ctx.in_field("max_component_depth", |ctx| {
211                if version.compatible((1u16, 0u16)) && self.max_component_depth.is_none() {
212                    ctx.report(format!("field must be present for version {version}"));
213                }
214            });
215        })
216    }
217}
218
219impl TopLevelTable for Maxp {
220    const TAG: Tag = Tag::new(b"maxp");
221}
222
223impl<'a> FromObjRef<read_fonts::tables::maxp::Maxp<'a>> for Maxp {
224    fn from_obj_ref(obj: &read_fonts::tables::maxp::Maxp<'a>, _: FontData) -> Self {
225        Maxp {
226            num_glyphs: obj.num_glyphs(),
227            max_points: obj.max_points(),
228            max_contours: obj.max_contours(),
229            max_composite_points: obj.max_composite_points(),
230            max_composite_contours: obj.max_composite_contours(),
231            max_zones: obj.max_zones(),
232            max_twilight_points: obj.max_twilight_points(),
233            max_storage: obj.max_storage(),
234            max_function_defs: obj.max_function_defs(),
235            max_instruction_defs: obj.max_instruction_defs(),
236            max_stack_elements: obj.max_stack_elements(),
237            max_size_of_instructions: obj.max_size_of_instructions(),
238            max_component_elements: obj.max_component_elements(),
239            max_component_depth: obj.max_component_depth(),
240        }
241    }
242}
243
244#[allow(clippy::needless_lifetimes)]
245impl<'a> FromTableRef<read_fonts::tables::maxp::Maxp<'a>> for Maxp {}
246
247impl<'a> FontRead<'a> for Maxp {
248    fn read(data: FontData<'a>) -> Result<Self, ReadError> {
249        <read_fonts::tables::maxp::Maxp as FontRead>::read(data).map(|x| x.to_owned_table())
250    }
251}