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
use crate::GlyphId;
use crate::parser::*;
#[derive(Clone, Copy)]
struct RangeRecord {
start_glyph_id: GlyphId,
end_glyph_id: GlyphId,
value: u16,
}
impl RangeRecord {
fn range(&self) -> core::ops::RangeInclusive<GlyphId> {
self.start_glyph_id..=self.end_glyph_id
}
}
impl FromData for RangeRecord {
const SIZE: usize = 6;
#[inline]
fn parse(data: &[u8]) -> Option<Self> {
let mut s = Stream::new(data);
Some(RangeRecord {
start_glyph_id: s.read()?,
end_glyph_id: s.read()?,
value: s.read()?,
})
}
}
#[derive(Clone, Copy, Debug)]
pub(crate) struct CoverageTable<'a> {
data: &'a [u8],
}
impl<'a> CoverageTable<'a> {
pub fn new(data: &'a [u8]) -> Self {
CoverageTable { data }
}
pub fn contains(&self, glyph_id: GlyphId) -> bool {
let mut s = Stream::new(self.data);
let format: u16 = try_opt_or!(s.read(), false);
match format {
1 => {
let count = try_opt_or!(s.read::<u16>(), false);
s.read_array16::<GlyphId>(count).unwrap().binary_search(&glyph_id).is_some()
}
2 => {
let count = try_opt_or!(s.read::<u16>(), false);
let records = try_opt_or!(s.read_array16::<RangeRecord>(count), false);
records.into_iter().any(|r| r.range().contains(&glyph_id))
}
_ => false,
}
}
}
#[repr(C)]
#[derive(Clone, Copy, PartialEq, Debug)]
pub struct Class(pub u16);
impl FromData for Class {
const SIZE: usize = 2;
#[inline]
fn parse(data: &[u8]) -> Option<Self> {
u16::parse(data).map(Class)
}
}
#[derive(Clone, Copy)]
pub(crate) struct ClassDefinitionTable<'a> {
data: &'a [u8],
}
impl<'a> ClassDefinitionTable<'a> {
pub fn new(data: &'a [u8]) -> Self {
ClassDefinitionTable { data }
}
pub fn get(&self, glyph_id: GlyphId) -> Class {
self.get_impl(glyph_id).unwrap_or(Class(0))
}
fn get_impl(&self, glyph_id: GlyphId) -> Option<Class> {
let mut s = Stream::new(self.data);
let format: u16 = s.read()?;
match format {
1 => {
let start_glyph_id: GlyphId = s.read()?;
if glyph_id < start_glyph_id {
return None;
}
let count: u16 = s.read()?;
let classes = s.read_array16::<Class>(count)?;
classes.get(glyph_id.0 - start_glyph_id.0)
}
2 => {
let count: u16 = s.read()?;
let records = s.read_array16::<RangeRecord>(count)?;
records.into_iter().find(|r| r.range().contains(&glyph_id))
.map(|record| Class(record.value))
}
_ => None,
}
}
}