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
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
use core::fmt;
use ttf_parser::{cmap, kern, Face, GlyphId};
use crate::{AsFaceRef, OwnedFace};
#[derive(Clone)]
pub struct PreParsedSubtables<'face, F> {
pub(crate) face: F,
pub(crate) subtables: FaceSubtables<'face>,
}
impl<'face> From<Face<'face>> for PreParsedSubtables<'face, Face<'face>> {
fn from(face: Face<'face>) -> Self {
let subtables = FaceSubtables::from(&face);
Self { face, subtables }
}
}
impl From<OwnedFace> for PreParsedSubtables<'static, OwnedFace> {
fn from(face: OwnedFace) -> Self {
face.pre_parse_subtables()
}
}
impl<F> fmt::Debug for PreParsedSubtables<'_, F> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "PreParsedSubtables")
}
}
#[derive(Clone)]
pub(crate) struct FaceSubtables<'face> {
cmap: Vec<cmap::Subtable<'face>>,
h_kern: Vec<kern::Subtable<'face>>,
}
impl<'face> From<&Face<'face>> for FaceSubtables<'face> {
fn from(face: &Face<'face>) -> Self {
let cmap = face
.tables()
.cmap
.iter()
.flat_map(|cmap| cmap.subtables)
.filter(|st| st.is_unicode())
.collect();
let h_kern = face
.tables()
.kern
.iter()
.flat_map(|c| c.subtables)
.filter(|st| st.horizontal && !st.variable)
.collect();
Self { cmap, h_kern }
}
}
impl<F> PreParsedSubtables<'_, F> {
#[inline]
pub fn glyph_index(&self, c: char) -> Option<GlyphId> {
self.subtables
.cmap
.iter()
.find_map(|t| t.glyph_index(c.into()))
}
#[inline]
pub fn glyphs_hor_kerning(&self, first: GlyphId, second: GlyphId) -> Option<i16> {
self.subtables
.h_kern
.iter()
.find_map(|st| st.glyphs_kerning(first, second))
}
}
impl<F> AsFaceRef for PreParsedSubtables<'_, F>
where
F: AsFaceRef,
{
#[inline]
fn as_face_ref(&self) -> &ttf_parser::Face<'_> {
self.face.as_face_ref()
}
}