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
use crate::GlyphId;
use crate::parser::{Stream, FromData, LazyArray16};
#[derive(Clone, Copy, Debug)]
pub struct VerticalOriginMetrics {
pub glyph_id: GlyphId,
pub y: i16,
}
impl FromData for VerticalOriginMetrics {
const SIZE: usize = 4;
#[inline]
fn parse(data: &[u8]) -> Option<Self> {
let mut s = Stream::new(data);
Some(VerticalOriginMetrics {
glyph_id: s.read::<GlyphId>()?,
y: s.read::<i16>()?,
})
}
}
#[derive(Clone, Copy, Debug)]
pub struct Table<'a> {
pub default_y: i16,
pub metrics: LazyArray16<'a, VerticalOriginMetrics>,
}
impl<'a> Table<'a> {
pub fn parse(data: &'a [u8]) -> Option<Self> {
let mut s = Stream::new(data);
let version = s.read::<u32>()?;
if version != 0x00010000 {
return None;
}
let default_y = s.read::<i16>()?;
let count = s.read::<u16>()?;
let metrics = s.read_array16::<VerticalOriginMetrics>(count)?;
Some(Table {
default_y,
metrics,
})
}
pub fn glyph_y_origin(&self, glyph_id: GlyphId) -> i16 {
self.metrics.binary_search_by(|m| m.glyph_id.cmp(&glyph_id))
.map(|(_, m)| m.y)
.unwrap_or(self.default_y)
}
}