1use fontoxide_misc::Tag;
8use fontoxide_misc::xml_reader::Element;
9use fontoxide_misc::xml_writer::XmlWriter;
10
11use crate::generated::ot;
12use crate::ot::OtReader;
13use crate::ot::table::{OtTable, XmlCtx};
14use crate::table::{Table, TableType, TableXml};
15use crate::{Error, Font, Result};
16
17macro_rules! otl_table {
18 ($(#[$meta:meta])* $name:ident, $tag:literal, $ty:ty) => {
19 otl_table!(@impl $(#[$meta])* $name, $tag, $ty, |table: &mut $ty, element: &Element, font: &mut Font| {
20 let mut ctx = XmlCtx::default();
21 table.from_xml(element, font, &mut ctx)?;
22 table.populate_defaults(&mut ctx)
23 });
24 };
25 ($(#[$meta:meta])* $name:ident, $tag:literal, $ty:ty, from_xml: $from_xml:expr) => {
26 otl_table!(@impl $(#[$meta])* $name, $tag, $ty, $from_xml);
27 };
28 (@impl $(#[$meta:meta])* $name:ident, $tag:literal, $ty:ty, $from_xml:expr) => {
29 $(#[$meta])*
30 #[derive(Debug, Clone, PartialEq, Default)]
31 pub struct $name {
32 pub table: $ty,
34 }
35
36 impl std::ops::Deref for $name {
37 type Target = $ty;
38 fn deref(&self) -> &$ty {
39 &self.table
40 }
41 }
42
43 impl std::ops::DerefMut for $name {
44 fn deref_mut(&mut self) -> &mut $ty {
45 &mut self.table
46 }
47 }
48
49 impl TableType for $name {
50 const TAG: Tag = Tag::new($tag);
51
52 fn decompile(data: &[u8], font: &mut Font) -> Result<Self> {
53 let mut reader = OtReader::new(data, Self::TAG);
54 Ok($name {
55 table: <$ty>::decompile(&mut reader, font)?,
56 })
57 }
58 }
59
60 impl Table for $name {
61 fn remap_glyphs(&mut self, f: &mut dyn FnMut(u32) -> Result<u32>) -> Result<()> {
62 self.table.remap_glyphs(f)
63 }
64
65 fn tag(&self) -> Tag {
66 Self::TAG
67 }
68
69 fn compile(&mut self, font: &mut Font) -> Result<Vec<u8>> {
70 crate::ot::overflow::compile_table(&mut self.table, Self::TAG, font)
71 }
72
73 fn clone_table(&self) -> Box<dyn Table> {
74 Box::new(self.clone())
75 }
76 }
77
78 impl TableXml for $name {
79 fn to_xml(&self, writer: &mut XmlWriter, font: &mut Font) -> Result<()> {
80 crate::ot::conv::with_stat_axis_names(&self.table, font, |font| {
81 self.table.to_xml2(writer, font)
82 })
83 }
84
85 fn from_xml(&mut self, element: &Element, font: &mut Font) -> Result<()> {
86 let from_xml: fn(&mut $ty, &Element, &mut Font) -> Result<()> = $from_xml;
87 from_xml(&mut self.table, element, font)
88 }
89 }
90 };
91}
92
93otl_table!(
94 Base,
96 b"BASE",
97 ot::BASE
98);
99otl_table!(
100 Gdef,
102 b"GDEF",
103 ot::GDEF
104);
105otl_table!(
106 Gpos,
108 b"GPOS",
109 ot::GPOS
110);
111otl_table!(
112 Gsub,
114 b"GSUB",
115 ot::GSUB
116);
117otl_table!(
118 Hvar,
120 b"HVAR",
121 ot::HVAR
122);
123otl_table!(
124 Jstf,
126 b"JSTF",
127 ot::JSTF
128);
129otl_table!(
130 Math,
132 b"MATH",
133 ot::MATH
134);
135otl_table!(
136 Mvar,
138 b"MVAR",
139 ot::MVAR
140);
141otl_table!(
142 Stat,
144 b"STAT",
145 ot::STAT
146);
147otl_table!(
148 Tsic,
150 b"TSIC",
151 ot::TSIC
152);
153otl_table!(
154 Varc,
156 b"VARC",
157 ot::VARC
158);
159otl_table!(
160 Vvar,
162 b"VVAR",
163 ot::VVAR
164);
165otl_table!(
166 Ankr,
168 b"ankr",
169 ot::Ankr
170);
171otl_table!(
172 Bsln,
174 b"bsln",
175 ot::Bsln
176);
177otl_table!(
178 Cidg,
180 b"cidg",
181 ot::Cidg
182);
183otl_table!(
184 Feat,
186 b"feat",
187 ot::Feat
188);
189otl_table!(
190 Gcid,
192 b"gcid",
193 ot::Gcid
194);
195otl_table!(
196 Lcar,
198 b"lcar",
199 ot::Lcar
200);
201otl_table!(
202 Mort,
204 b"mort",
205 ot::Mort
206);
207otl_table!(
208 Morx,
210 b"morx",
211 ot::Morx
212);
213otl_table!(
214 Opbd,
216 b"opbd",
217 ot::Opbd
218);
219otl_table!(
220 Prop,
222 b"prop",
223 ot::Prop
224);
225
226fn patch_map_from_xml(
230 _table: &mut ot::PatchMap,
231 _element: &Element,
232 _font: &mut Font,
233) -> Result<()> {
234 Err(Error::ttlib("AttributeError: Format"))
235}
236
237otl_table!(
238 Ift, b"IFT ", ot::PatchMap, from_xml: patch_map_from_xml
240);
241otl_table!(
242 Iftx, b"IFTX", ot::PatchMap, from_xml: patch_map_from_xml
244);