use std::io::Result;
use crate::metrics::Metrics;
use crate::{Axes, Characters, Features, Glyph, Names, Palettes, Tables};
pub struct Font {
case: Box<dyn Case>,
}
pub trait Case {
fn axes(&mut self) -> Result<Axes>;
fn characters(&mut self) -> Result<Characters>;
fn features(&mut self) -> Result<Features>;
fn metrics(&mut self) -> Result<Metrics>;
fn names(&mut self) -> Result<Names>;
fn palettes(&mut self) -> Result<Palettes>;
fn tables(&mut self) -> Result<Tables>;
fn draw(&mut self, character: char) -> Result<Option<Glyph>>;
}
impl Font {
#[inline]
pub fn axes(&mut self) -> Result<Axes> {
self.case.axes()
}
#[inline]
pub fn characters(&mut self) -> Result<Characters> {
self.case.characters()
}
#[inline]
pub fn features(&mut self) -> Result<Features> {
self.case.features()
}
#[inline]
pub fn metrics(&mut self) -> Result<Metrics> {
self.case.metrics()
}
#[inline]
pub fn names(&mut self) -> Result<Names> {
self.case.names()
}
#[inline]
pub fn palettes(&mut self) -> Result<Palettes> {
self.case.palettes()
}
#[inline]
pub fn tables(&mut self) -> Result<Tables> {
self.case.tables()
}
#[inline]
pub fn draw(&mut self, character: char) -> Result<Option<Glyph>> {
self.case.draw(character)
}
}
pub fn read<T: typeface::tape::Read + 'static>(mut tape: T) -> Result<Vec<Font>> {
use opentype::truetype::Tag;
let tag = tape.peek::<Tag>()?;
if opentype::accept(&tag) {
Ok(crate::formats::opentype::read(tape)?
.into_iter()
.map(|font| Font {
case: Box::new(font),
})
.collect())
} else if webtype::accept(&tag) {
Ok(crate::formats::webtype::read(tape)?
.into_iter()
.map(|font| Font {
case: Box::new(font),
})
.collect())
} else {
error!("found an unknown file format")
}
}