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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
use crate::GlyphId;
use crate::parser::{Stream, FromData, NumFrom, Offset16, Offset};
#[derive(Clone, Copy, Debug)]
struct OTCoverage(u8);
impl OTCoverage {
#[inline]
fn is_horizontal(self) -> bool {
self.0 & (1 << 0) != 0
}
#[inline]
fn has_cross_stream(self) -> bool {
self.0 & (1 << 2) != 0
}
}
impl FromData for OTCoverage {
const SIZE: usize = 1;
#[inline]
fn parse(data: &[u8]) -> Option<Self> {
data.get(0).copied().map(OTCoverage)
}
}
#[derive(Clone, Copy, Debug)]
struct AATCoverage(u8);
impl AATCoverage {
#[inline]
fn is_horizontal(self) -> bool {
self.0 & (1 << 7) == 0
}
#[inline]
fn has_cross_stream(self) -> bool {
self.0 & (1 << 6) != 0
}
#[inline]
fn is_variable(self) -> bool {
self.0 & (1 << 5) != 0
}
}
impl FromData for AATCoverage {
const SIZE: usize = 1;
#[inline]
fn parse(data: &[u8]) -> Option<Self> {
data.get(0).copied().map(AATCoverage)
}
}
#[derive(Clone, Copy)]
struct KerningRecord {
pair: u32,
value: i16,
}
impl FromData for KerningRecord {
const SIZE: usize = 6;
#[inline]
fn parse(data: &[u8]) -> Option<Self> {
let mut s = Stream::new(data);
Some(KerningRecord {
pair: s.read::<u32>()?,
value: s.read::<i16>()?,
})
}
}
#[derive(Clone, Copy, Default)]
pub struct Subtable<'a> {
is_horizontal: bool,
is_variable: bool,
has_cross_stream: bool,
format: u8,
header_size: u8,
data: &'a [u8],
}
impl<'a> Subtable<'a> {
#[inline]
pub fn is_horizontal(&self) -> bool {
self.is_horizontal
}
#[inline]
pub fn is_variable(&self) -> bool {
self.is_variable
}
#[inline]
pub fn has_cross_stream(&self) -> bool {
self.has_cross_stream
}
#[inline]
pub fn has_state_machine(&self) -> bool {
self.format == 1
}
#[inline]
pub fn glyphs_kerning(&self, left: GlyphId, right: GlyphId) -> Option<i16> {
match self.format {
0 => parse_format0(self.data, left, right),
2 => parse_format2(left, right, self.header_size, self.data),
3 => parse_format3(self.data, left, right),
_ => None,
}
}
}
impl core::fmt::Debug for Subtable<'_> {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
f.debug_struct("Subtable")
.field("is_horizontal", &self.is_horizontal())
.field("has_state_machine", &self.has_state_machine())
.field("has_cross_stream", &self.has_cross_stream())
.field("format", &self.format)
.finish()
}
}
#[allow(missing_debug_implementations)]
#[derive(Clone, Copy, Default)]
pub struct Subtables<'a> {
is_aat: bool,
table_index: u32,
number_of_tables: u32,
stream: Stream<'a>,
}
impl<'a> Iterator for Subtables<'a> {
type Item = Subtable<'a>;
fn next(&mut self) -> Option<Self::Item> {
if self.table_index == self.number_of_tables {
return None;
}
if self.stream.at_end() {
return None;
}
if self.is_aat {
const HEADER_SIZE: u8 = 8;
let table_len: u32 = self.stream.read()?;
let coverage: AATCoverage = self.stream.read()?;
let format: u8 = self.stream.read()?;
self.stream.skip::<u16>();
if format > 3 {
return None;
}
let data_len = usize::num_from(table_len).checked_sub(usize::from(HEADER_SIZE))?;
Some(Subtable {
is_horizontal: coverage.is_horizontal(),
is_variable: coverage.is_variable(),
has_cross_stream: coverage.has_cross_stream(),
format,
header_size: HEADER_SIZE,
data: self.stream.read_bytes(data_len)?,
})
} else {
const HEADER_SIZE: u8 = 6;
self.stream.skip::<u16>();
let table_len: u16 = self.stream.read()?;
let format: u8 = self.stream.read()?;
let coverage: OTCoverage = self.stream.read()?;
if format != 0 && format != 2 {
return None;
}
let data_len = if self.number_of_tables == 1 {
self.stream.tail()?.len()
} else {
usize::from(table_len).checked_sub(usize::from(HEADER_SIZE))?
};
Some(Subtable {
is_horizontal: coverage.is_horizontal(),
is_variable: false,
has_cross_stream: coverage.has_cross_stream(),
format,
header_size: HEADER_SIZE,
data: self.stream.read_bytes(data_len)?,
})
}
}
}
pub(crate) fn parse(data: &[u8]) -> Option<Subtables> {
let mut s = Stream::new(data);
let version: u16 = s.read()?;
if version == 0 {
let number_of_tables: u16 = s.read()?;
Some(Subtables {
is_aat: false,
table_index: 0,
number_of_tables: u32::from(number_of_tables),
stream: s,
})
} else {
s.skip::<u16>();
let number_of_tables: u32 = s.read()?;
Some(Subtables {
is_aat: true,
table_index: 0,
number_of_tables: u32::from(number_of_tables),
stream: s,
})
}
}
fn parse_format0(data: &[u8], left: GlyphId, right: GlyphId) -> Option<i16> {
let mut s = Stream::new(data);
let number_of_pairs: u16 = s.read()?;
s.advance(6);
let pairs = s.read_array16::<KerningRecord>(number_of_pairs)?;
let needle = u32::from(left.0) << 16 | u32::from(right.0);
pairs.binary_search_by(|v| v.pair.cmp(&needle)).map(|(_, v)| v.value)
}
fn parse_format2(left: GlyphId, right: GlyphId, header_len: u8, data: &[u8]) -> Option<i16> {
let mut s = Stream::new(data);
s.skip::<u16>();
let header_len = usize::from(header_len);
let left_hand_table_offset = s.read::<Offset16>()?.to_usize().checked_sub(header_len)?;
let right_hand_table_offset = s.read::<Offset16>()?.to_usize().checked_sub(header_len)?;
let array_offset = s.read::<Offset16>()?.to_usize().checked_sub(header_len)?;
let left_class = get_format2_class(left.0, left_hand_table_offset, data).unwrap_or(0);
let right_class = get_format2_class(right.0, right_hand_table_offset, data).unwrap_or(0);
if usize::from(left_class) < array_offset {
return None;
}
let index = usize::from(left_class) + usize::from(right_class);
let value_offset = index.checked_sub(header_len)?;
Stream::read_at::<i16>(data, value_offset)
}
fn get_format2_class(glyph_id: u16, offset: usize, data: &[u8]) -> Option<u16> {
let mut s = Stream::new_at(data, offset)?;
let first_glyph: u16 = s.read()?;
let index = glyph_id.checked_sub(first_glyph)?;
let number_of_classes: u16 = s.read()?;
let classes = s.read_array16::<u16>(number_of_classes)?;
classes.get(index)
}
fn parse_format3(data: &[u8], left: GlyphId, right: GlyphId) -> Option<i16> {
let mut s = Stream::new(data);
let glyph_count: u16 = s.read()?;
let kerning_values_count: u8 = s.read()?;
let left_hand_classes_count: u8 = s.read()?;
let right_hand_classes_count: u8 = s.read()?;
s.skip::<u8>();
let indices_count = u16::from(left_hand_classes_count) * u16::from(right_hand_classes_count);
let kerning_values = s.read_array16::<i16>(u16::from(kerning_values_count))?;
let left_hand_classes = s.read_array16::<u8>(glyph_count)?;
let right_hand_classes = s.read_array16::<u8>(glyph_count)?;
let indices = s.read_array16::<u8>(indices_count)?;
let left_class = left_hand_classes.get(left.0)?;
let right_class = right_hand_classes.get(right.0)?;
if left_class > left_hand_classes_count || right_class > right_hand_classes_count {
return None;
}
let index = u16::from(left_class) * u16::from(right_hand_classes_count) + u16::from(right_class);
let index = indices.get(index)?;
kerning_values.get(u16::from(index))
}