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
use crate::horizontal_header::HorizontalHeader;
use crate::maximum_profile::MaximumProfile;
use crate::{GlyphID, Result, Tape, Walue};
table! {
@define
#[doc = "Horizontal metrics."]
pub HorizontalMetrics {
records (Vec<Record>), left_side_bearings (Vec<i16> ), }
}
table! {
#[doc = "A record of horizontal metrics."]
#[derive(Copy)]
pub Record { advance_width (u16), left_side_bearing (i16), }
}
impl HorizontalMetrics {
pub fn get(&self, glyph_id: GlyphID) -> (u16, i16) {
let mut index = glyph_id as usize;
let longs = self.records.len();
if index < longs {
(
self.records[index].advance_width,
self.records[index].left_side_bearing,
)
} else {
let shorts = self.left_side_bearings.len();
index -= longs;
if index < shorts {
(
self.records[longs - 1].advance_width,
self.left_side_bearings[index],
)
} else {
(
self.records[longs - 1].advance_width,
self.left_side_bearings[shorts - 1],
)
}
}
}
}
impl<'l> Walue<'l> for HorizontalMetrics {
type Parameter = (&'l HorizontalHeader, &'l MaximumProfile);
fn read<T: Tape>(tape: &mut T, (header, profile): Self::Parameter) -> Result<Self> {
let metric_count = header.horizontal_metric_count as usize;
let glyph_count = profile.glyph_count();
if metric_count == 0 || metric_count > glyph_count {
raise!("found a malformed horizontal header");
}
let bearing_count = glyph_count - metric_count;
let mut table = HorizontalMetrics {
records: Vec::with_capacity(metric_count),
left_side_bearings: Vec::with_capacity(bearing_count),
};
for _ in 0..metric_count {
table.records.push(tape.take()?);
}
for _ in 0..bearing_count {
table.left_side_bearings.push(tape.take()?);
}
Ok(table)
}
}