fontoxide_ttlib/tables/
maxp.rs1use fontoxide_misc::Tag;
4use fontoxide_misc::xml_reader::Element;
5use fontoxide_misc::xml_writer::XmlWriter;
6
7use crate::binary::WriteBytes;
8use crate::sstruct::{I32, U16, eval};
9use crate::table::{Table, TableType, TableXml};
10use crate::{Error, Font, Result};
11
12crate::sstruct! {
13 pub struct MaxpFormat05 = "maxpFormat_0_5" {
16 pub table_version: i32 = "tableVersion" as I32,
18 pub num_glyphs: u16 = "numGlyphs" as U16,
20 }
21}
22
23crate::sstruct! {
24 pub struct MaxpV1 = "maxpFormat_1_0_add" {
26 pub max_points: u16 = "maxPoints" as U16,
28 pub max_contours: u16 = "maxContours" as U16,
30 pub max_composite_points: u16 = "maxCompositePoints" as U16,
32 pub max_composite_contours: u16 = "maxCompositeContours" as U16,
34 pub max_zones: u16 = "maxZones" as U16,
36 pub max_twilight_points: u16 = "maxTwilightPoints" as U16,
38 pub max_storage: u16 = "maxStorage" as U16,
40 pub max_function_defs: u16 = "maxFunctionDefs" as U16,
42 pub max_instruction_defs: u16 = "maxInstructionDefs" as U16,
44 pub max_stack_elements: u16 = "maxStackElements" as U16,
46 pub max_size_of_instructions: u16 = "maxSizeOfInstructions" as U16,
48 pub max_component_elements: u16 = "maxComponentElements" as U16,
50 pub max_component_depth: u16 = "maxComponentDepth" as U16,
52 }
53}
54
55#[derive(Debug, Clone, PartialEq, Default)]
59pub struct Maxp {
60 pub table_version: u32,
62 pub num_glyphs: u32,
64 pub v1: MaxpV1,
67 pub extended: bool,
69}
70
71const VERSION_0_5: u32 = 0x0000_5000;
72
73impl TableType for Maxp {
74 const TAG: Tag = Tag::new(b"maxp");
75 const COMPANION: Option<Tag> = Some(Tag::new(b"MAXP"));
76
77 fn decompile(data: &[u8], font: &mut Font) -> Result<Self> {
78 Self::decompile_as(Self::TAG, data, font)
79 }
80
81 fn new_as(tag: Tag) -> Self {
82 Maxp {
83 extended: tag == Tag::new(b"MAXP"),
84 ..Default::default()
85 }
86 }
87
88 fn decompile_as(tag: Tag, data: &[u8], _font: &mut Font) -> Result<Self> {
89 let extended = tag == Tag::new(b"MAXP");
90 let header_size = if extended { 7 } else { 6 };
91 let int = |bytes: &[u8]| bytes.iter().fold(0u64, |acc, &b| acc << 8 | b as u64);
93 let table_version = int(&data[..data.len().min(4)]) as u32;
94 let num_glyphs = int(data.get(4..data.len().min(header_size)).unwrap_or_default()) as u32;
95 let mut rest = data.get(header_size..).unwrap_or_default();
96 let mut v1 = MaxpV1::default();
97 if table_version != VERSION_0_5 {
98 let (parsed, r) = MaxpV1::unpack2(rest)?;
99 v1 = parsed;
100 rest = r;
101 }
102 if !rest.is_empty() {
103 return Err(Error::Assertion("len(data) == 0".into()));
104 }
105 Ok(Maxp {
106 table_version,
107 num_glyphs,
108 v1,
109 extended,
110 })
111 }
112}
113
114impl Table for Maxp {
115 fn remap_glyphs(&mut self, f: &mut dyn FnMut(u32) -> Result<u32>) -> Result<()> {
116 let _ = f;
117 Ok(())
118 }
119
120 fn tag(&self) -> Tag {
121 Font::locked_tag::<Self>(self.extended)
122 }
123
124 fn compile(&mut self, font: &mut Font) -> Result<Vec<u8>> {
125 font.insert(self.clone_table());
130 let glyf = Font::locked_tag::<crate::tables::Glyf>(self.extended);
131 if font.contains(glyf) && font.is_loaded(glyf) && font.recalc_bboxes {
132 self.recalc(font)?;
133 font.insert(self.clone_table());
134 }
135 self.num_glyphs = crate::binary::fit(font.glyph_order()?.len(), "numGlyphs")?;
136 if self.table_version != VERSION_0_5 {
137 self.table_version = 0x0001_0000;
138 }
139 let mut data = Vec::new();
140 data.put_u32(self.table_version);
141 if self.extended {
142 if self.num_glyphs > 0xFF_FFFF {
143 return Err(Error::value("OverflowError: int too big to convert"));
144 }
145 data.extend_from_slice(&self.num_glyphs.to_be_bytes()[1..]);
146 } else {
147 data.put_u16(crate::binary::fit(self.num_glyphs, "numGlyphs")?);
148 }
149 if self.table_version == 0x0001_0000 {
150 self.v1.pack(&mut data)?;
151 }
152 Ok(data)
153 }
154
155 fn clone_table(&self) -> Box<dyn Table> {
156 Box::new(self.clone())
157 }
158}
159
160impl TableXml for Maxp {
161 fn to_xml(&self, writer: &mut XmlWriter, _font: &mut Font) -> Result<()> {
162 let v1 = self.table_version != VERSION_0_5;
163 if v1 {
164 writer.comment("Most of this table will be recalculated by the compiler");
165 writer.newline();
166 }
167 let version = format!("{:#x}", self.table_version);
168 writer.simpletag("tableVersion", &[("value", &version)]);
169 writer.newline();
170 writer.simpletag("numGlyphs", &[("value", &self.num_glyphs)]);
171 writer.newline();
172 if v1 {
173 for (name, value) in self.v1.values() {
174 writer.simpletag(name, &[("value", &value)]);
175 writer.newline();
176 }
177 }
178 Ok(())
179 }
180
181 fn from_xml(&mut self, element: &Element, _font: &mut Font) -> Result<()> {
182 let name = element.name.as_str();
183 let value = element
184 .attr("value")
185 .ok_or_else(|| Error::ttx(format!("missing 'value' on <{name}>")))?;
186 let lit = eval(value)?;
187 match name {
188 "tableVersion" => {
189 let v = lit
190 .as_int()
191 .ok_or_else(|| Error::value("invalid tableVersion"))?;
192 self.table_version = crate::binary::fit(v, name)?;
193 }
194 "numGlyphs" => {
195 let v = lit
196 .as_int()
197 .ok_or_else(|| Error::value("invalid numGlyphs"))?;
198 self.num_glyphs = crate::binary::fit(v, name)?;
199 }
200 _ => {
201 if !self.v1.set_literal(name, &lit)? {
202 return Err(Error::ttx(format!("unknown 'maxp' field: {name}")));
203 }
204 }
205 }
206 Ok(())
207 }
208}
209
210impl Maxp {
211 pub fn recalc(&mut self, font: &mut Font) -> Result<()> {
216 crate::tables::metrics::recalc_maxp(self, font)
217 }
218}
219
220#[cfg(test)]
221mod tests {
222 use super::*;
223
224 #[test]
225 fn formats_match_upstream() {
226 crate::sstruct::tests::check(MaxpFormat05::FORMAT_NAME, MaxpFormat05::FIELDS);
227 crate::sstruct::tests::check(MaxpV1::FORMAT_NAME, MaxpV1::FIELDS);
228 }
229}