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
use crate::GlyphId;
use crate::parser::{Stream, FromData, FromSlice, LazyArray16};
mod context;
mod chained_context;
mod lookup;
mod layout_table;
#[cfg(feature = "variable-fonts")] mod feature_variations;
pub use context::*;
pub use chained_context::*;
pub use lookup::*;
pub use layout_table::*;
#[cfg(feature = "variable-fonts")] pub use feature_variations::*;
#[derive(Clone, Copy, Debug)]
pub struct RangeRecord {
pub start: GlyphId,
pub end: GlyphId,
pub value: u16,
}
impl LazyArray16<'_, RangeRecord> {
pub fn range(&self, glyph: GlyphId) -> Option<RangeRecord> {
self.binary_search_by(|record| {
if glyph < record.start {
core::cmp::Ordering::Greater
} else if glyph <= record.end {
core::cmp::Ordering::Equal
} else {
core::cmp::Ordering::Less
}
}).map(|p| p.1)
}
}
impl FromData for RangeRecord {
const SIZE: usize = 6;
#[inline]
fn parse(data: &[u8]) -> Option<Self> {
let mut s = Stream::new(data);
Some(RangeRecord {
start: s.read::<GlyphId>()?,
end: s.read::<GlyphId>()?,
value: s.read::<u16>()?,
})
}
}
#[allow(missing_docs)]
#[derive(Clone, Copy, Debug)]
pub enum Coverage<'a> {
Format1 {
glyphs: LazyArray16<'a, GlyphId>,
},
Format2 {
records: LazyArray16<'a, RangeRecord>,
},
}
impl<'a> FromSlice<'a> for Coverage<'a> {
fn parse(data: &'a [u8]) -> Option<Self> {
let mut s = Stream::new(data);
match s.read::<u16>()? {
1 => {
let count = s.read::<u16>()?;
let glyphs = s.read_array16(count)?;
Some(Self::Format1 { glyphs })
}
2 => {
let count = s.read::<u16>()?;
let records = s.read_array16(count)?;
Some(Self::Format2 { records })
}
_ => None,
}
}
}
impl<'a> Coverage<'a> {
pub fn contains(&self, glyph: GlyphId) -> bool {
self.get(glyph).is_some()
}
pub fn get(&self, glyph: GlyphId) -> Option<u16> {
match self {
Self::Format1 { glyphs } => {
glyphs.binary_search(&glyph).map(|p| p.0)
}
Self::Format2 { records } => {
let record = records.range(glyph)?;
let offset = glyph.0 - record.start.0;
record.value.checked_add(offset)
}
}
}
}
pub type Class = u16;
#[allow(missing_docs)]
#[derive(Clone, Copy, Debug)]
pub enum ClassDefinition<'a> {
Format1 {
start: GlyphId,
classes: LazyArray16<'a, Class>,
},
Format2 {
records: LazyArray16<'a, RangeRecord>,
},
}
impl<'a> ClassDefinition<'a> {
#[inline]
pub(crate) fn parse(data: &'a [u8]) -> Option<Self> {
let mut s = Stream::new(data);
match s.read::<u16>()? {
1 => {
let start = s.read::<GlyphId>()?;
let count = s.read::<u16>()?;
let classes = s.read_array16(count)?;
Some(Self::Format1 { start, classes })
},
2 => {
let count = s.read::<u16>()?;
let records = s.read_array16(count)?;
Some(Self::Format2 { records })
},
_ => None,
}
}
pub fn get(&self, glyph: GlyphId) -> Class {
match self {
Self::Format1 { start, classes } => {
glyph.0.checked_sub(start.0).and_then(|index| classes.get(index))
}
Self::Format2 { records } => {
records.range(glyph).map(|record| record.value)
}
}.unwrap_or(0)
}
}