Skip to main content

ttf_view/tables/
mod.rs

1use crate::types::Tag;
2
3mod directory;
4pub use directory::*;
5
6pub mod cmap;
7pub mod glyf;
8pub mod head;
9pub mod hhea;
10pub mod hmtx;
11pub mod loca;
12pub mod maxp;
13pub mod name;
14pub mod os_2;
15
16impl TableDirectory {
17    // Note: Even though these tables are required, we'll still use Option here
18    pub fn cmap(&self) -> Result<cmap::Cmap<'_>, TableError> {
19        self.table()
20    }
21    pub fn glyf(&self) -> Result<glyf::Glyf<'_>, TableError> {
22        self.table()
23    }
24    pub fn head(&self) -> Result<head::Head<'_>, TableError> {
25        self.table()
26    }
27    pub fn hhea(&self) -> Result<hhea::Hhea<'_>, TableError> {
28        self.table()
29    }
30    pub fn hmtx(&self) -> Result<hmtx::Hmtx<'_>, TableError> {
31        self.table()
32    }
33    pub fn loca(&self) -> Result<loca::Loca<'_>, TableError> {
34        self.table()
35    }
36    pub fn maxp(&self) -> Result<maxp::Maxp<'_>, TableError> {
37        self.table()
38    }
39    pub fn name(&self) -> Result<name::Name<'_>, TableError> {
40        self.table()
41    }
42    pub fn os_2(&self) -> Result<os_2::Os_2<'_>, TableError> {
43        self.table()
44    }
45}
46
47pub trait Table<'a>: Sized {
48    const TAG: Tag;
49    fn new_in(dir: &'a TableDirectory) -> Result<Self, TableError>;
50}
51
52#[derive(Debug, thiserror::Error)]
53#[derive_const(Clone, PartialEq, Eq)]
54pub enum TableError {
55    #[error("table not found")]
56    NotFound,
57    #[error("unknown version")]
58    UnknownVersion,
59    #[error("unknown format")]
60    UnknownFormat,
61    #[error("dependency '{0}'")]
62    Dependency(Tag),
63    #[error("dependency: {0}")]
64    DependencyError(&'static &'static str),
65    #[error("invalid length")]
66    InvalidLen,
67    #[error("malformed: {0}")]
68    Malformed(&'static &'static str),
69}