Skip to main content

ttf_rs/
tables.rs

1// Table module for TTF table parsing
2
3pub mod head;
4pub mod maxp;
5pub mod cmap;
6pub mod name;
7pub mod hhea;
8pub mod hmtx;
9pub mod glyf;
10pub mod loca;
11pub mod post;
12pub mod os2;
13pub mod kern;
14pub mod gpos;
15pub mod gsub;
16pub mod base;
17pub mod jstf;
18pub mod fpgm;
19pub mod prep;
20pub mod cvt;
21pub mod fvar;
22pub mod gvar;
23pub mod avar;
24pub mod stat;
25pub mod hvar;
26pub mod colr;
27pub mod cpal;
28pub mod svg;
29pub mod cbdt;
30pub mod sbix;
31
32use crate::error::Result;
33use crate::stream::{FontReader, FontWriter};
34
35/// Table record in the SFNT header
36#[derive(Debug, Clone)]
37pub struct TableRecord {
38    pub table_tag: [u8; 4],
39    pub checksum: u32,
40    pub offset: u32,
41    pub length: u32,
42}
43
44impl TableRecord {
45    pub fn tag_to_string(&self) -> String {
46        String::from_utf8_lossy(&self.table_tag).to_string()
47    }
48
49    pub fn from_reader(reader: &mut FontReader) -> Result<Self> {
50        let table_tag = reader.read_tag()?;
51        let checksum = reader.read_u32()?;
52        let offset = reader.read_u32()?;
53        let length = reader.read_u32()?;
54
55        Ok(TableRecord {
56            table_tag,
57            checksum,
58            offset,
59            length,
60        })
61    }
62
63    pub fn write(&self, writer: &mut FontWriter) {
64        writer.write_tag(&self.table_tag);
65        writer.write_u32(self.checksum);
66        writer.write_u32(self.offset);
67        writer.write_u32(self.length);
68    }
69
70    pub fn new(tag: [u8; 4], offset: u32, length: u32) -> Self {
71        TableRecord {
72            table_tag: tag,
73            checksum: 0,
74            offset,
75            length,
76        }
77    }
78}
79
80/// Trait that all TTF tables must implement for reading
81pub trait TtfTable: Sized {
82    fn from_reader(reader: &mut FontReader, length: u32) -> Result<Self>;
83
84    fn table_tag() -> Option<&'static [u8; 4]> {
85        None
86    }
87}
88
89/// Trait for tables that can be written
90pub trait TtfTableWrite: Sized {
91    fn write(&self, writer: &mut FontWriter) -> Result<()>;
92
93    fn table_tag() -> &'static [u8; 4];
94}
95