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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
use crate::error::Error;
use crate::opentype::{read_i16, read_u16};
use std::collections::HashMap;
#[derive(Debug, Clone, Copy, Default, PartialEq)]
pub struct Constant {
pub script_percent: f64,
pub script_script_percent: f64,
pub delimited_min_height: f64,
pub display_operator_min_height: f64,
pub axis_height: f64,
pub accent_base_height: f64,
pub subscript_shift_down: f64,
pub subscript_top_max: f64,
pub subscript_baseline_drop_min: f64,
pub superscript_shift_up: f64,
pub superscript_bottom_min: f64,
pub superscript_baseline_drop_max: f64,
pub sub_superscript_gap_min: f64,
pub superscript_bottom_max_with_subscript: f64,
pub space_after_script: f64,
pub upper_limit_gap_min: f64,
pub upper_limit_baseline_rise_min: f64,
pub lower_limit_gap_min: f64,
pub lower_limit_baseline_drop_min: f64,
pub stack_top_shift_up: f64,
pub stack_top_display_shift_up: f64,
pub stack_bottom_shift_down: f64,
pub stack_bottom_display_shift_down: f64,
pub stack_gap_min: f64,
pub stack_display_gap_min: f64,
pub fraction_numerator_shift_up: f64,
pub fraction_numerator_display_shift_up: f64,
pub fraction_denominator_shift_down: f64,
pub fraction_denominator_display_shift_down: f64,
pub fraction_numerator_gap_min: f64,
pub fraction_numerator_display_gap_min: f64,
pub fraction_rule_thickness: f64,
pub fraction_denominator_gap_min: f64,
pub fraction_denominator_display_gap_min: f64,
pub overbar_vertical_gap: f64,
pub overbar_rule_thickness: f64,
pub overbar_extra_ascender: f64,
pub underbar_vertical_gap: f64,
pub underbar_rule_thickness: f64,
pub underbar_extra_descender: f64,
pub radical_vertical_gap: f64,
pub radical_display_vertical_gap: f64,
pub radical_rule_thickness: f64,
pub radical_extra_ascender: f64,
pub radical_kern_before_degree: f64,
pub radical_kern_after_degree: f64,
pub radical_degree_bottom_raise_percent: f64,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Variant {
pub glyph: u16,
pub measure: f64,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Part {
pub glyph: u16,
pub start_connector: f64,
pub end_connector: f64,
pub full_advance: f64,
pub extender: bool,
}
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Construction {
pub variant: Vec<Variant>,
pub part: Vec<Part>,
}
#[derive(Debug, Clone, Default)]
pub struct Table {
pub constant: Constant,
pub min_overlap: f64,
italic: HashMap<u16, f64>,
top_accent: HashMap<u16, f64>,
vertical: HashMap<u16, Construction>,
horizontal: HashMap<u16, Construction>,
}
pub fn with_constant(constant: Constant) -> Table {
Table {constant, ..Table::default()}
}
pub fn italic_correction(table: &Table, glyph: u16) -> f64 {
table.italic.get(&glyph).copied().unwrap_or(0.0)
}
pub fn top_accent(table: &Table, glyph: u16) -> Option<f64> {
table.top_accent.get(&glyph).copied()
}
pub fn vertical(table: &Table, glyph: u16) -> Option<&Construction> {
table.vertical.get(&glyph)
}
pub fn horizontal(table: &Table, glyph: u16) -> Option<&Construction> {
table.horizontal.get(&glyph)
}
pub fn parse(data: &[u8], at: usize, units: f64) -> Result<Table, Error> {
let constant_at = read_u16(data, at + 4)? as usize;
let glyph_info_at = read_u16(data, at + 6)? as usize;
let variant_at = read_u16(data, at + 8)? as usize;
let mut table = Table {
constant: read_constant(data, at + constant_at, units)?,
..Table::default()
};
if glyph_info_at != 0 {
read_glyph_info(data, at + glyph_info_at, units, &mut table)?;
}
if variant_at != 0 {
read_variants(data, at + variant_at, units, &mut table)?;
}
Ok(table)
}
fn value(data: &[u8], base: usize, index: usize, units: f64) -> Result<f64, Error> {
Ok(read_i16(data, base + index * 4)? as f64 / units)
}
fn read_constant(data: &[u8], at: usize, units: f64) -> Result<Constant, Error> {
let record = at + 8;
let take = |index: usize| value(data, record, index, units);
Ok(Constant {
script_percent: read_i16(data, at)? as f64 / 100.0,
script_script_percent: read_i16(data, at + 2)? as f64 / 100.0,
delimited_min_height: read_u16(data, at + 4)? as f64 / units,
display_operator_min_height: read_u16(data, at + 6)? as f64 / units,
axis_height: take(1)?,
accent_base_height: take(2)?,
subscript_shift_down: take(4)?,
subscript_top_max: take(5)?,
subscript_baseline_drop_min: take(6)?,
superscript_shift_up: take(7)?,
superscript_bottom_min: take(9)?,
superscript_baseline_drop_max: take(10)?,
sub_superscript_gap_min: take(11)?,
superscript_bottom_max_with_subscript: take(12)?,
space_after_script: take(13)?,
upper_limit_gap_min: take(14)?,
upper_limit_baseline_rise_min: take(15)?,
lower_limit_gap_min: take(16)?,
lower_limit_baseline_drop_min: take(17)?,
stack_top_shift_up: take(18)?,
stack_top_display_shift_up: take(19)?,
stack_bottom_shift_down: take(20)?,
stack_bottom_display_shift_down: take(21)?,
stack_gap_min: take(22)?,
stack_display_gap_min: take(23)?,
fraction_numerator_shift_up: take(28)?,
fraction_numerator_display_shift_up: take(29)?,
fraction_denominator_shift_down: take(30)?,
fraction_denominator_display_shift_down: take(31)?,
fraction_numerator_gap_min: take(32)?,
fraction_numerator_display_gap_min: take(33)?,
fraction_rule_thickness: take(34)?,
fraction_denominator_gap_min: take(35)?,
fraction_denominator_display_gap_min: take(36)?,
overbar_vertical_gap: take(39)?,
overbar_rule_thickness: take(40)?,
overbar_extra_ascender: take(41)?,
underbar_vertical_gap: take(42)?,
underbar_rule_thickness: take(43)?,
underbar_extra_descender: take(44)?,
radical_vertical_gap: take(45)?,
radical_display_vertical_gap: take(46)?,
radical_rule_thickness: take(47)?,
radical_extra_ascender: take(48)?,
radical_kern_before_degree: take(49)?,
radical_kern_after_degree: take(50)?,
radical_degree_bottom_raise_percent: read_i16(data, record + 51 * 4)? as f64 / 100.0,
})
}
fn coverage(data: &[u8], at: usize) -> Result<Vec<u16>, Error> {
let mut found = Vec::new();
match read_u16(data, at)? {
1 => {
let count = read_u16(data, at + 2)? as usize;
for index in 0..count {
found.push(read_u16(data, at + 4 + index * 2)?);
}
}
2 => {
let count = read_u16(data, at + 2)? as usize;
for index in 0..count {
let record = at + 4 + index * 6;
let start = read_u16(data, record)?;
let end = read_u16(data, record + 2)?;
if start > end {
continue;
}
for glyph in start..=end {
found.push(glyph);
}
}
}
format => return Err(Error::new(format!("cannot read Coverage format {format}"), at)),
}
Ok(found)
}
fn read_glyph_info(data: &[u8], at: usize, units: f64, table: &mut Table) -> Result<(), Error> {
let italic_at = read_u16(data, at)? as usize;
let accent_at = read_u16(data, at + 2)? as usize;
if italic_at != 0 {
table.italic = read_value_map(data, at + italic_at, units)?;
}
if accent_at != 0 {
table.top_accent = read_value_map(data, at + accent_at, units)?;
}
Ok(())
}
fn read_value_map(data: &[u8], at: usize, units: f64) -> Result<HashMap<u16, f64>, Error> {
let coverage_at = read_u16(data, at)? as usize;
let count = read_u16(data, at + 2)? as usize;
if coverage_at == 0 {
return Ok(HashMap::new());
}
let glyphs = coverage(data, at + coverage_at)?;
let mut found = HashMap::with_capacity(count);
for (index, glyph) in glyphs.into_iter().enumerate() {
if index >= count {
break;
}
found.insert(glyph, value(data, at + 4, index, units)?);
}
Ok(found)
}
fn read_variants(data: &[u8], at: usize, units: f64, table: &mut Table) -> Result<(), Error> {
table.min_overlap = read_u16(data, at)? as f64 / units;
let vertical_coverage = read_u16(data, at + 2)? as usize;
let horizontal_coverage = read_u16(data, at + 4)? as usize;
let vertical_count = read_u16(data, at + 6)? as usize;
let horizontal_count = read_u16(data, at + 8)? as usize;
let vertical_at = at + 10;
let horizontal_at = vertical_at + vertical_count * 2;
if vertical_coverage != 0 {
table.vertical = read_constructions(
data,
at,
at + vertical_coverage,
vertical_at,
vertical_count,
units,
)?;
}
if horizontal_coverage != 0 {
table.horizontal = read_constructions(
data,
at,
at + horizontal_coverage,
horizontal_at,
horizontal_count,
units,
)?;
}
Ok(())
}
fn read_constructions(
data: &[u8],
base: usize,
coverage_at: usize,
list_at: usize,
count: usize,
units: f64,
) -> Result<HashMap<u16, Construction>, Error> {
let glyphs = coverage(data, coverage_at)?;
let mut found = HashMap::with_capacity(count);
for (index, glyph) in glyphs.into_iter().enumerate() {
if index >= count {
break;
}
let offset = read_u16(data, list_at + index * 2)? as usize;
if offset == 0 {
continue;
}
found.insert(glyph, read_construction(data, base + offset, units)?);
}
Ok(found)
}
fn read_construction(data: &[u8], at: usize, units: f64) -> Result<Construction, Error> {
let assembly_at = read_u16(data, at)? as usize;
let count = read_u16(data, at + 2)? as usize;
let mut variant = Vec::with_capacity(count);
for index in 0..count {
let record = at + 4 + index * 4;
variant.push(Variant {
glyph: read_u16(data, record)?,
measure: read_u16(data, record + 2)? as f64 / units,
});
}
let part = if assembly_at == 0 {
Vec::new()
} else {
read_assembly(data, at + assembly_at, units)?
};
Ok(Construction {variant, part})
}
fn read_assembly(data: &[u8], at: usize, units: f64) -> Result<Vec<Part>, Error> {
let count = read_u16(data, at + 4)? as usize;
let mut found = Vec::with_capacity(count);
for index in 0..count {
let record = at + 6 + index * 10;
found.push(Part {
glyph: read_u16(data, record)?,
start_connector: read_u16(data, record + 2)? as f64 / units,
end_connector: read_u16(data, record + 4)? as f64 / units,
full_advance: read_u16(data, record + 6)? as f64 / units,
extender: read_u16(data, record + 8)? & 1 != 0,
});
}
Ok(found)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::opentype::{character_map, require};
fn font() -> (Vec<u8>, Table, HashMap<char, u16>) {
let data = std::fs::read("font/latinmodern-math.otf").expect("cannot read the test font");
let (math_at, _) = require(&data, "MATH").unwrap();
let table = parse(&data, math_at, 1000.0).unwrap();
let (cmap_at, _) = require(&data, "cmap").unwrap();
let map = character_map(&data, cmap_at).unwrap();
(data, table, map)
}
#[test]
fn reads_the_constants() {
let (_, table, _) = font();
let constant = table.constant;
assert_eq!(constant.axis_height, 0.25);
assert_eq!(constant.script_percent, 0.7);
assert_eq!(constant.script_script_percent, 0.5);
assert!(constant.fraction_rule_thickness > 0.0);
assert!(constant.radical_rule_thickness > 0.0);
assert!(constant.accent_base_height > 0.0);
assert!(constant.display_operator_min_height > 0.0);
}
#[test]
fn a_paren_can_grow() {
let (_, table, map) = font();
let construction = vertical(&table, map[&'(']).expect("the parenthesis has no variants");
assert!(construction.variant.len() > 3, "variant count: {}", construction.variant.len());
for pair in construction.variant.windows(2) {
assert!(pair[1].measure >= pair[0].measure, "the variants are not in order of height");
}
assert!(!construction.part.is_empty(), "the parenthesis has no assembly");
assert!(construction.part.iter().any(|part| part.extender));
}
#[test]
fn the_radical_can_grow() {
let (_, table, map) = font();
let construction = vertical(&table, map[&'\u{221a}']).expect("the radical has no variants");
assert!(!construction.variant.is_empty());
}
#[test]
fn slanted_letters_carry_an_italic_correction() {
let (_, table, map) = font();
let slanted = map[&'\u{1d453}'];
assert!(italic_correction(&table, slanted) > 0.0, "f has no italic correction");
}
#[test]
fn accents_have_an_attachment_point() {
let (_, table, map) = font();
assert!(top_accent(&table, map[&'x']).is_some());
}
#[test]
fn the_overlap_is_recorded() {
let (_, table, _) = font();
assert!(table.min_overlap > 0.0);
}
}