use std::any::Any;
use std::fmt;
use fontoxide_misc::Tag;
use fontoxide_misc::xml_reader::Element;
use fontoxide_misc::xml_writer::XmlWriter;
use crate::{Error, Font, Result};
pub trait AsAny: Any {
fn as_any(&self) -> &dyn Any;
fn as_any_mut(&mut self) -> &mut dyn Any;
}
impl<T: Any> AsAny for T {
fn as_any(&self) -> &dyn Any {
self
}
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
}
pub trait Table: AsAny + Send + Sync {
fn tag(&self) -> Tag;
fn compile(&mut self, font: &mut Font) -> Result<Vec<u8>>;
fn clone_table(&self) -> Box<dyn Table>;
fn ensure_decompiled(&mut self) {}
fn remap_glyphs(&mut self, f: &mut dyn FnMut(u32) -> Result<u32>) -> Result<()>;
fn is_raw(&self) -> bool {
false
}
fn decompile_error(&self) -> Option<&str> {
None
}
}
pub trait TableXml {
fn to_xml(&self, writer: &mut XmlWriter, font: &mut Font) -> Result<()>;
#[allow(clippy::wrong_self_convention)]
fn from_xml(&mut self, element: &Element, font: &mut Font) -> Result<()>;
}
impl Clone for Box<dyn Table> {
fn clone(&self) -> Self {
self.clone_table()
}
}
impl fmt::Debug for dyn Table {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Table")
.field("tag", &self.tag())
.finish_non_exhaustive()
}
}
pub trait TableType: Table + Sized {
const TAG: Tag;
const COMPANION: Option<Tag> = None;
fn decompile(data: &[u8], font: &mut Font) -> Result<Self>;
fn decompile_as(tag: Tag, data: &[u8], font: &mut Font) -> Result<Self> {
let _ = tag;
Self::decompile(data, font)
}
fn new_as(tag: Tag) -> Self
where
Self: Default,
{
let _ = tag;
Self::default()
}
}
#[derive(Clone, PartialEq, Eq)]
pub struct DefaultTable {
pub tag: Tag,
pub data: Vec<u8>,
pub error: Option<String>,
}
impl fmt::Debug for DefaultTable {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("DefaultTable")
.field("tag", &self.tag)
.field("len", &self.data.len())
.field("error", &self.error)
.finish()
}
}
impl DefaultTable {
pub fn new(tag: Tag, data: Vec<u8>) -> Self {
DefaultTable {
tag,
data,
error: None,
}
}
}
impl Table for DefaultTable {
fn remap_glyphs(&mut self, f: &mut dyn FnMut(u32) -> Result<u32>) -> Result<()> {
let _ = f;
Ok(())
}
fn tag(&self) -> Tag {
self.tag
}
fn compile(&mut self, _font: &mut Font) -> Result<Vec<u8>> {
Ok(self.data.clone())
}
fn clone_table(&self) -> Box<dyn Table> {
Box::new(self.clone())
}
fn is_raw(&self) -> bool {
true
}
fn decompile_error(&self) -> Option<&str> {
self.error.as_deref()
}
}
impl TableXml for DefaultTable {
fn to_xml(&self, writer: &mut XmlWriter, _font: &mut Font) -> Result<()> {
if let Some(error) = &self.error {
writer.comment("An error occurred during the decompilation of this table");
writer.newline();
writer.comment(error);
writer.newline();
}
writer.begintag("hexdata", &[]);
writer.newline();
writer.dumphex(&self.data);
writer.endtag("hexdata");
writer.newline();
Ok(())
}
fn from_xml(&mut self, element: &Element, _font: &mut Font) -> Result<()> {
if element.name != "hexdata" {
return Err(Error::ttlib(format!(
"can't handle '{}' element",
element.name
)));
}
self.data = read_hex(element)?;
Ok(())
}
}
pub fn read_hex(element: &Element) -> Result<Vec<u8>> {
fontoxide_misc::text_tools::read_hex(element.text_chunks())
.ok_or_else(|| Error::value("invalid hex data"))
}
fn no_xml(tag: Tag) -> Error {
Error::ttlib(format!("no TTX support for the '{tag}' table's type"))
}
macro_rules! registry {
($($tag:literal => $ty:ty),* $(,)?) => {
pub fn decompile_table(tag: Tag, data: &[u8], font: &mut Font) -> Result<Box<dyn Table>> {
match &tag.0 {
$($tag => Ok(Box::new(<$ty as TableType>::decompile_as(tag, data, font)?)),)*
_ => Ok(Box::new(DefaultTable::new(tag, data.to_vec()))),
}
}
pub fn new_table(tag: Tag) -> Box<dyn Table> {
match &tag.0 {
$($tag => Box::new(<$ty as TableType>::new_as(tag)),)*
_ => Box::new(DefaultTable::new(tag, Vec::new())),
}
}
pub fn is_implemented(tag: Tag) -> bool {
matches!(&tag.0, $($tag)|*)
|| !crate::generated::tables::FONTTOOLS_TABLES.contains(&tag)
}
pub fn table_to_xml(table: &dyn Table, writer: &mut XmlWriter, font: &mut Font) -> Result<()> {
if let Some(raw) = table.as_any().downcast_ref::<DefaultTable>() {
return raw.to_xml(writer, font);
}
match &table.tag().0 {
$($tag => {
if let Some(t) = table.as_any().downcast_ref::<$ty>() {
return t.to_xml(writer, font);
}
})*
_ => {}
}
Err(no_xml(table.tag()))
}
pub fn table_from_xml(table: &mut dyn Table, element: &Element, font: &mut Font) -> Result<()> {
let tag = table.tag();
if let Some(raw) = table.as_any_mut().downcast_mut::<DefaultTable>() {
return raw.from_xml(element, font);
}
match &tag.0 {
$($tag => {
if let Some(t) = table.as_any_mut().downcast_mut::<$ty>() {
return t.from_xml(element, font);
}
})*
_ => {}
}
Err(no_xml(tag))
}
};
}
registry! {
b"cvt " => crate::tables::Cvt,
b"CFF " => crate::tables::Cff,
b"CFF2" => crate::tables::Cff2,
b"BASE" => crate::tables::otl::Base,
b"GDEF" => crate::tables::otl::Gdef,
b"GPOS" => crate::tables::otl::Gpos,
b"GSUB" => crate::tables::otl::Gsub,
b"HVAR" => crate::tables::otl::Hvar,
b"IFT " => crate::tables::otl::Ift,
b"IFTX" => crate::tables::otl::Iftx,
b"JSTF" => crate::tables::otl::Jstf,
b"MATH" => crate::tables::otl::Math,
b"MVAR" => crate::tables::otl::Mvar,
b"STAT" => crate::tables::otl::Stat,
b"TSIC" => crate::tables::otl::Tsic,
b"VARC" => crate::tables::otl::Varc,
b"VVAR" => crate::tables::otl::Vvar,
b"ankr" => crate::tables::otl::Ankr,
b"bsln" => crate::tables::otl::Bsln,
b"cidg" => crate::tables::otl::Cidg,
b"feat" => crate::tables::otl::Feat,
b"gcid" => crate::tables::otl::Gcid,
b"lcar" => crate::tables::otl::Lcar,
b"mort" => crate::tables::otl::Mort,
b"morx" => crate::tables::otl::Morx,
b"opbd" => crate::tables::otl::Opbd,
b"prop" => crate::tables::otl::Prop,
b"FFTM" => crate::tables::Fftm,
b"avar" => crate::tables::Avar,
b"fpgm" => crate::tables::Fpgm,
b"prep" => crate::tables::Prep,
b"CBDT" => crate::tables::Cbdt,
b"CBLC" => crate::tables::Cblc,
b"COLR" => crate::tables::Colr,
b"CPAL" => crate::tables::Cpal,
b"EBDT" => crate::tables::Ebdt,
b"EBLC" => crate::tables::Eblc,
b"bdat" => crate::tables::Bdat,
b"bloc" => crate::tables::Bloc,
b"fvar" => crate::tables::Fvar,
b"gasp" => crate::tables::Gasp,
b"glyf" => crate::tables::Glyf,
b"GLYF" => crate::tables::Glyf,
b"gvar" => crate::tables::Gvar,
b"GVAR" => crate::tables::Gvar,
b"cvar" => crate::tables::Cvar,
b"loca" => crate::tables::Loca,
b"LOCA" => crate::tables::Loca,
b"cmap" => crate::tables::Cmap,
b"DMAP" => crate::tables::Dmap,
b"DSIG" => crate::tables::Dsig,
b"SVG " => crate::tables::Svg,
b"kern" => crate::tables::Kern,
b"hdmx" => crate::tables::Hdmx,
b"head" => crate::tables::Head,
b"bhed" => crate::tables::Bhed,
b"LTSH" => crate::tables::Ltsh,
b"hhea" => crate::tables::Hhea,
b"HHEA" => crate::tables::Hhea,
b"hmtx" => crate::tables::Hmtx,
b"HMTX" => crate::tables::Hmtx,
b"ltag" => crate::tables::Ltag,
b"maxp" => crate::tables::Maxp,
b"MAXP" => crate::tables::Maxp,
b"name" => crate::tables::Name,
b"OS/2" => crate::tables::Os2,
b"post" => crate::tables::Post,
b"sbix" => crate::tables::Sbix,
b"vhea" => crate::tables::Vhea,
b"VHEA" => crate::tables::Vhea,
b"vmtx" => crate::tables::Vmtx,
b"VMTX" => crate::tables::Vmtx,
b"VORG" => crate::tables::Vorg,
b"Debg" => crate::tables::Debg,
b"bgcl" => crate::tables::Bgcl,
b"EBSC" => crate::tables::Ebsc,
b"meta" => crate::tables::Meta,
b"trak" => crate::tables::Trak,
b"VDMX" => crate::tables::Vdmx,
b"TSI0" => crate::tables::TsiIndex,
b"TSI2" => crate::tables::TsiIndex,
b"TSI1" => crate::tables::TsiPrograms,
b"TSI3" => crate::tables::TsiPrograms,
b"TSI5" => crate::tables::Tsi5,
b"TSIB" => crate::tables::AsciiTable,
b"TSID" => crate::tables::AsciiTable,
b"TSIJ" => crate::tables::AsciiTable,
b"TSIP" => crate::tables::AsciiTable,
b"TSIS" => crate::tables::AsciiTable,
b"TSIV" => crate::tables::AsciiTable,
b"TTFA" => crate::tables::AsciiTable,
b"Feat" => crate::tables::graphite::Feat,
b"Glat" => crate::tables::Glat,
b"Gloc" => crate::tables::Gloc,
b"Silf" => crate::tables::Silf,
b"Sill" => crate::tables::Sill,
}
pub fn dependencies(tag: Tag) -> &'static [Tag] {
crate::generated::tables::dependencies(tag)
}