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
use crate::GlyphId;
use crate::parser::*;
#[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::<crate::raw::gdef::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 {
fn parse(data: &[u8]) -> Self {
Class(SafeStream::new(data).read())
}
}
#[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::<crate::raw::gdef::ClassRangeRecord>(count)?;
records.into_iter().find(|r| r.range().contains(&glyph_id))
.map(|record| Class(record.class()))
}
_ => None,
}
}
}