use alloc::borrow::Cow;
use alloc::boxed::Box;
use crate::binary::read::{ReadBinary, ReadCtxt};
use crate::error::{ParseError, ReadWriteError};
use crate::tables::{FontTableProvider, OpenTypeFont, CFF_MAGIC, TTCF_MAGIC, TTF_MAGIC};
use crate::woff::{self, WoffFont};
use crate::woff2::{self, Woff2Font};
pub enum FontData<'a> {
OpenType(OpenTypeFont<'a>),
Woff(WoffFont<'a>),
Woff2(Woff2Font<'a>),
}
pub struct DynamicFontTableProvider<'a> {
provider: Box<dyn FontTableProvider + 'a>,
}
impl<'a> ReadBinary<'a> for FontData<'a> {
type HostType = Self;
fn read(ctxt: &mut ReadCtxt<'a>) -> Result<Self, ParseError> {
let mut peek = ctxt.clone();
let magic = peek.read_u32be()?;
match magic {
TTF_MAGIC | CFF_MAGIC => Ok(FontData::OpenType(OpenTypeFont::read(ctxt)?)),
TTCF_MAGIC => Ok(FontData::OpenType(OpenTypeFont::read(ctxt)?)),
woff::MAGIC => Ok(FontData::Woff(WoffFont::read(ctxt)?)),
woff2::MAGIC => Ok(FontData::Woff2(Woff2Font::read(ctxt)?)),
_ => Err(ParseError::BadVersion),
}
}
}
impl<'a> FontTableProvider for DynamicFontTableProvider<'a> {
fn table_data<'b>(&'b self, tag: u32) -> Result<Option<Cow<'b, [u8]>>, ParseError> {
self.provider.table_data(tag)
}
fn has_table(&self, tag: u32) -> bool {
self.provider.has_table(tag)
}
}
impl<'a> FontData<'a> {
pub fn table_provider(
&'a self,
index: usize,
) -> Result<DynamicFontTableProvider<'a>, ReadWriteError> {
match self {
FontData::OpenType(font) => {
let provider = font.table_provider(index)?;
Ok(DynamicFontTableProvider {
provider: Box::new(provider),
})
}
FontData::Woff(font) => {
Ok(DynamicFontTableProvider {
provider: Box::new(font.clone()),
})
}
FontData::Woff2(font) => {
let provider = font.table_provider(index)?;
Ok(DynamicFontTableProvider {
provider: Box::new(provider),
})
}
}
}
}