Skip to main content

fontoxide_ttlib/tables/
otl.rs

1//! Tables built from otData types (`BaseTTXConverter`): GDEF, GSUB, GPOS, BASE,
2//! JSTF, MATH, STAT, the variation tables and the AAT tables that use otData.
3//!
4//! Each wraps the generated top-level type, which is available through
5//! [`std::ops::Deref`].
6
7use 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			/// The table contents (`table`).
33			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	/// Baseline table.
95	Base,
96	b"BASE",
97	ot::BASE
98);
99otl_table!(
100	/// Glyph Definition table.
101	Gdef,
102	b"GDEF",
103	ot::GDEF
104);
105otl_table!(
106	/// Glyph Positioning table.
107	Gpos,
108	b"GPOS",
109	ot::GPOS
110);
111otl_table!(
112	/// Glyph Substitution table.
113	Gsub,
114	b"GSUB",
115	ot::GSUB
116);
117otl_table!(
118	/// Horizontal Metrics Variations table.
119	Hvar,
120	b"HVAR",
121	ot::HVAR
122);
123otl_table!(
124	/// Justification table.
125	Jstf,
126	b"JSTF",
127	ot::JSTF
128);
129otl_table!(
130	/// Mathematical Typesetting table.
131	Math,
132	b"MATH",
133	ot::MATH
134);
135otl_table!(
136	/// Metrics Variations table.
137	Mvar,
138	b"MVAR",
139	ot::MVAR
140);
141otl_table!(
142	/// Style Attributes table.
143	Stat,
144	b"STAT",
145	ot::STAT
146);
147otl_table!(
148	/// VTT character group data.
149	Tsic,
150	b"TSIC",
151	ot::TSIC
152);
153otl_table!(
154	/// Variable Composites table.
155	Varc,
156	b"VARC",
157	ot::VARC
158);
159otl_table!(
160	/// Vertical Metrics Variations table.
161	Vvar,
162	b"VVAR",
163	ot::VVAR
164);
165otl_table!(
166	/// AAT anchor point table.
167	Ankr,
168	b"ankr",
169	ot::Ankr
170);
171otl_table!(
172	/// AAT baseline table.
173	Bsln,
174	b"bsln",
175	ot::Bsln
176);
177otl_table!(
178	/// AAT CID to glyph mapping table.
179	Cidg,
180	b"cidg",
181	ot::Cidg
182);
183otl_table!(
184	/// AAT feature name table.
185	Feat,
186	b"feat",
187	ot::Feat
188);
189otl_table!(
190	/// AAT glyph to CID mapping table.
191	Gcid,
192	b"gcid",
193	ot::Gcid
194);
195otl_table!(
196	/// AAT ligature caret table.
197	Lcar,
198	b"lcar",
199	ot::Lcar
200);
201otl_table!(
202	/// AAT glyph metamorphosis table.
203	Mort,
204	b"mort",
205	ot::Mort
206);
207otl_table!(
208	/// AAT extended glyph metamorphosis table.
209	Morx,
210	b"morx",
211	ot::Morx
212);
213otl_table!(
214	/// AAT optical bounds table.
215	Opbd,
216	b"opbd",
217	ot::Opbd
218);
219otl_table!(
220	/// AAT glyph properties table.
221	Prop,
222	b"prop",
223	ot::Prop
224);
225
226// UPSTREAM-BUG(B8): the patch map is format-switching, but `toXML2` of the
227// top-level table does not write its `Format`, so fontTools cannot read the TTX
228// back: every element fails with AttributeError.
229fn 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	/// Incremental Font Transfer patch map.
239	Ift, b"IFT ", ot::PatchMap, from_xml: patch_map_from_xml
240);
241otl_table!(
242	/// Incremental Font Transfer patch map, for extensions.
243	Iftx, b"IFTX", ot::PatchMap, from_xml: patch_map_from_xml
244);