1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#![allow(dead_code)]
pub mod fixed;
pub mod aat;
pub mod at;
pub mod cmap;
pub mod glyf;
pub mod head;
pub mod var;
pub mod vorg;
pub mod xmtx;
mod parse;
pub use parse::*;
use head::*;
pub type RawTag = u32;
pub const fn raw_tag(bytes: &[u8; 4]) -> RawTag {
(bytes[0] as u32) << 24 | (bytes[1] as u32) << 16 | (bytes[2] as u32) << 8 | bytes[3] as u32
}
pub mod raw_data {
use super::{raw_tag, Bytes, RawTag};
const OTTO: RawTag = raw_tag(b"OTTO");
const TTCF: RawTag = raw_tag(b"ttcf");
const FONT: RawTag = 0x10000;
const TRUE: RawTag = raw_tag(b"true");
pub fn is_collection(data: &[u8]) -> bool {
Bytes::new(data).read_u32(0) == Some(TTCF)
}
pub fn is_font(data: &[u8], offset: u32) -> bool {
let tag = Bytes::new(data).read_u32(offset as usize).unwrap_or(0);
tag == FONT || tag == OTTO || tag == TRUE
}
pub fn count(data: &[u8]) -> u32 {
if is_collection(data) {
Bytes::new(data).read_u32(8).unwrap_or(0)
} else if is_font(data, 0) {
1
} else {
0
}
}
pub fn offset(data: &[u8], index: u32) -> Option<u32> {
if index >= count(data) {
return None;
}
if is_font(data, 0) {
Some(0)
} else {
Bytes::new(data).read_u32(12 + index as usize * 4)
}
}
}
pub trait RawFont<'a>: Sized {
fn data(&self) -> &'a [u8];
fn offset(&self) -> u32;
fn dump_tables(&self) -> Option<()> {
let base = self.offset() as usize;
let b = Bytes::new(self.data());
let len = b.read_u16(base.checked_add(4)?)? as usize;
let record_base = base.checked_add(12)?;
let reclen = 16usize;
for i in 0..len {
let recbase = reclen.checked_mul(i)?.checked_add(record_base)?;
let mut s = b.stream_at(recbase)?;
let table_tag = s.read_u32()?;
let tb = table_tag.to_be_bytes();
println!("{}", core::str::from_utf8(&tb).unwrap_or("??"));
}
Some(())
}
fn table_range(&self, tag: RawTag) -> Option<(u32, u32)> {
let base = self.offset() as usize;
let b = Bytes::new(self.data());
let len = b.read_u16(base.checked_add(4)?)? as usize;
let record_base = base.checked_add(12)?;
let reclen = 16usize;
let mut l = 0;
let mut h = len;
while l < h {
use core::cmp::Ordering::*;
let i = (l + h) / 2;
let recbase = reclen.checked_mul(i)?.checked_add(record_base)?;
let mut s = b.stream_at(recbase)?;
let table_tag = s.read_u32()?;
match tag.cmp(&table_tag) {
Less => h = i,
Greater => l = i + 1,
Equal => {
s.skip(4)?;
let start = s.read_u32()?;
let len = s.read_u32()?;
let end = start.checked_add(len)?;
return Some((start, end));
}
}
}
None
}
fn table_offset(&self, tag: RawTag) -> u32 {
self.table_range(tag).map(|r| r.0).unwrap_or(0)
}
fn table_data(&self, tag: RawTag) -> Option<&'a [u8]> {
let r = self.table_range(tag)?;
self.data().get(r.0 as usize..r.1 as usize)
}
fn head(&self) -> Option<Head<'a>> {
Head::from_font(self)
}
fn os2(&self) -> Option<Os2<'a>> {
Os2::from_font(self)
}
fn post(&self) -> Option<Post<'a>> {
Post::from_font(self)
}
fn maxp(&self) -> Option<Maxp<'a>> {
Maxp::from_font(self)
}
fn hhea(&self) -> Option<Hhea<'a>> {
Hhea::from_font(self)
}
fn vhea(&self) -> Option<Vhea<'a>> {
Vhea::from_font(self)
}
}
impl<'a, T> RawFont<'a> for &T
where
T: RawFont<'a>,
{
fn data(&self) -> &'a [u8] {
(*self).data()
}
fn offset(&self) -> u32 {
(*self).offset()
}
}