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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
use ttf_parser::GlyphId;
use ttf_parser::gdef::GlyphClass;
use ttf_parser::opentype_layout::LayoutTable;
use crate::Variation;
use crate::ot::{TableIndex, PositioningTable, SubstitutionTable};
use crate::buffer::GlyphPropsFlags;
const WINDOWS_SYMBOL_ENCODING: u16 = 0;
const WINDOWS_UNICODE_BMP_ENCODING: u16 = 1;
const WINDOWS_UNICODE_FULL_ENCODING: u16 = 10;
const UNICODE_1_0_ENCODING: u16 = 0;
const UNICODE_1_1_ENCODING: u16 = 1;
const UNICODE_ISO_ENCODING: u16 = 2;
const UNICODE_2_0_BMP_ENCODING: u16 = 3;
const UNICODE_2_0_FULL_ENCODING: u16 = 4;
const UNICODE_FULL_ENCODING: u16 = 6;
#[derive(Clone)]
pub struct Face<'a> {
pub(crate) ttfp_face: ttf_parser::Face<'a>,
pub(crate) units_per_em: u16,
pixels_per_em: Option<(u16, u16)>,
pub(crate) points_per_em: Option<f32>,
prefered_cmap_encoding_subtable: Option<u16>,
pub(crate) gsub: Option<SubstitutionTable<'a>>,
pub(crate) gpos: Option<PositioningTable<'a>>,
}
impl<'a> AsRef<ttf_parser::Face<'a>> for Face<'a> {
#[inline]
fn as_ref(&self) -> &ttf_parser::Face<'a> {
&self.ttfp_face
}
}
impl<'a> AsMut<ttf_parser::Face<'a>> for Face<'a> {
#[inline]
fn as_mut(&mut self) -> &mut ttf_parser::Face<'a> {
&mut self.ttfp_face
}
}
impl<'a> core::ops::Deref for Face<'a> {
type Target = ttf_parser::Face<'a>;
#[inline]
fn deref(&self) -> &Self::Target {
&self.ttfp_face
}
}
impl<'a> core::ops::DerefMut for Face<'a> {
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.ttfp_face
}
}
impl<'a> Face<'a> {
pub fn from_slice(data: &'a [u8], face_index: u32) -> Option<Self> {
let face = ttf_parser::Face::from_slice(data, face_index).ok()?;
Self::from_face(face)
}
pub fn from_face(face: ttf_parser::Face<'a>) -> Option<Self> {
Some(Face {
units_per_em: face.units_per_em(),
pixels_per_em: None,
points_per_em: None,
prefered_cmap_encoding_subtable: find_best_cmap_subtable(&face),
gsub: face.tables().gsub.map(SubstitutionTable::new),
gpos: face.tables().gpos.map(PositioningTable::new),
ttfp_face: face,
})
}
#[inline]
pub fn units_per_em(&self) -> i32 {
self.units_per_em as i32
}
#[inline]
pub(crate) fn pixels_per_em(&self) -> Option<(u16, u16)> {
self.pixels_per_em
}
#[inline]
pub fn set_pixels_per_em(&mut self, ppem: Option<(u16, u16)>) {
self.pixels_per_em = ppem;
}
#[inline]
pub fn set_points_per_em(&mut self, ptem: Option<f32>) {
self.points_per_em = ptem;
}
pub fn set_variations(&mut self, variations: &[Variation]) {
for variation in variations {
self.set_variation(variation.tag, variation.value);
}
}
pub(crate) fn has_glyph(&self, c: u32) -> bool {
self.glyph_index(c).is_some()
}
pub(crate) fn glyph_index(&self, c: u32) -> Option<GlyphId> {
let subtable_idx = self.prefered_cmap_encoding_subtable?;
let subtable = self.tables().cmap?.subtables.get(subtable_idx)?;
match subtable.glyph_index(c) {
Some(gid) => Some(gid),
None => {
if subtable.platform_id == ttf_parser::PlatformId::Windows &&
subtable.encoding_id == WINDOWS_SYMBOL_ENCODING
{
if c <= 0x00FF {
return self.glyph_index(0xF000 + c);
}
}
None
}
}
}
pub(crate) fn glyph_h_advance(&self, glyph: GlyphId) -> i32 {
self.glyph_advance(glyph, false) as i32
}
pub(crate) fn glyph_v_advance(&self, glyph: GlyphId) -> i32 {
-(self.glyph_advance(glyph, true) as i32)
}
fn glyph_advance(&self, glyph: GlyphId, is_vertical: bool) -> u32 {
let face = &self.ttfp_face;
if face.is_variable() &&
face.has_non_default_variation_coordinates() &&
face.tables().hvar.is_none() &&
face.tables().vvar.is_none()
{
return match face.glyph_bounding_box(glyph) {
Some(bbox) => {
(if is_vertical {
bbox.y_max + bbox.y_min
} else {
bbox.x_max + bbox.x_min
}) as u32
}
None => 0,
};
}
if is_vertical && face.tables().vmtx.is_some() {
face.glyph_ver_advance(glyph).unwrap_or(0) as u32
} else if !is_vertical && face.tables().hmtx.is_some() {
face.glyph_hor_advance(glyph).unwrap_or(0) as u32
} else {
face.units_per_em() as u32
}
}
pub(crate) fn glyph_h_origin(&self, glyph: GlyphId) -> i32 {
self.glyph_h_advance(glyph) / 2
}
pub(crate) fn glyph_v_origin(&self, glyph: GlyphId) -> i32 {
match self.ttfp_face.glyph_y_origin(glyph) {
Some(y) => i32::from(y),
None => self.glyph_extents(glyph).map_or(0, |ext| ext.y_bearing)
+ self.glyph_side_bearing(glyph, true)
}
}
pub(crate) fn glyph_side_bearing(&self, glyph: GlyphId, is_vertical: bool) -> i32 {
let face = &self.ttfp_face;
if face.is_variable() &&
face.tables().hvar.is_none() &&
face.tables().vvar.is_none()
{
return match face.glyph_bounding_box(glyph) {
Some(bbox) => (if is_vertical { bbox.x_min } else { bbox.y_min }) as i32,
None => 0,
}
}
if is_vertical {
face.glyph_ver_side_bearing(glyph).unwrap_or(0) as i32
} else {
face.glyph_hor_side_bearing(glyph).unwrap_or(0) as i32
}
}
pub(crate) fn glyph_extents(&self, glyph: GlyphId) -> Option<GlyphExtents> {
let pixels_per_em = match self.pixels_per_em {
Some(ppem) => ppem.0,
None => core::u16::MAX,
};
if let Some(img) = self.ttfp_face.glyph_raster_image(glyph, pixels_per_em) {
if img.format == ttf_parser::RasterImageFormat::PNG {
let scale = self.units_per_em as f32 / img.pixels_per_em as f32;
return Some(GlyphExtents {
x_bearing: crate::round(f32::from(img.x) * scale) as i32,
y_bearing: crate::round((f32::from(img.y) + f32::from(img.height)) * scale) as i32,
width: crate::round(f32::from(img.width) * scale) as i32,
height: crate::round(-f32::from(img.height) * scale) as i32,
});
}
}
let bbox = self.ttfp_face.glyph_bounding_box(glyph)?;
Some(GlyphExtents {
x_bearing: i32::from(bbox.x_min),
y_bearing: i32::from(bbox.y_max),
width: i32::from(bbox.width()),
height: i32::from(bbox.y_min - bbox.y_max),
})
}
pub(crate) fn glyph_name(&self, glyph: GlyphId) -> Option<&str> {
self.ttfp_face.glyph_name(glyph)
}
pub(crate) fn glyph_props(&self, glyph: GlyphId) -> u16 {
let table = match self.tables().gdef {
Some(v) => v,
None => return 0,
};
match table.glyph_class(glyph) {
Some(GlyphClass::Base) => GlyphPropsFlags::BASE_GLYPH.bits(),
Some(GlyphClass::Ligature) => GlyphPropsFlags::LIGATURE.bits(),
Some(GlyphClass::Mark) => {
let class = table.glyph_mark_attachment_class(glyph);
(class << 8) | GlyphPropsFlags::MARK.bits()
}
_ => 0,
}
}
pub(crate) fn layout_table(&self, table_index: TableIndex) -> Option<&LayoutTable<'a>> {
match table_index {
TableIndex::GSUB => self.gsub.as_ref().map(|table| &table.inner),
TableIndex::GPOS => self.gpos.as_ref().map(|table| &table.inner),
}
}
pub(crate) fn layout_tables(&self) -> impl Iterator<Item = (TableIndex, &LayoutTable<'a>)> + '_ {
TableIndex::iter().filter_map(move |idx| self.layout_table(idx).map(|table| (idx, table)))
}
}
#[derive(Clone, Copy, Default)]
pub struct GlyphExtents {
pub x_bearing: i32,
pub y_bearing: i32,
pub width: i32,
pub height: i32,
}
fn find_best_cmap_subtable(face: &ttf_parser::Face) -> Option<u16> {
use ttf_parser::PlatformId;
find_cmap_subtable(face, PlatformId::Windows, WINDOWS_SYMBOL_ENCODING)
.or_else(|| find_cmap_subtable(face, PlatformId::Windows, WINDOWS_UNICODE_FULL_ENCODING))
.or_else(|| find_cmap_subtable(face, PlatformId::Unicode, UNICODE_FULL_ENCODING))
.or_else(|| find_cmap_subtable(face, PlatformId::Unicode, UNICODE_2_0_FULL_ENCODING))
.or_else(|| find_cmap_subtable(face, PlatformId::Windows, WINDOWS_UNICODE_BMP_ENCODING))
.or_else(|| find_cmap_subtable(face, PlatformId::Unicode, UNICODE_2_0_BMP_ENCODING))
.or_else(|| find_cmap_subtable(face, PlatformId::Unicode, UNICODE_ISO_ENCODING))
.or_else(|| find_cmap_subtable(face, PlatformId::Unicode, UNICODE_1_1_ENCODING))
.or_else(|| find_cmap_subtable(face, PlatformId::Unicode, UNICODE_1_0_ENCODING))
}
fn find_cmap_subtable(
face: &ttf_parser::Face,
platform_id: ttf_parser::PlatformId,
encoding_id: u16,
) -> Option<u16> {
for (i, subtable) in face.tables().cmap?.subtables.into_iter().enumerate() {
if subtable.platform_id == platform_id && subtable.encoding_id == encoding_id {
return Some(i as u16)
}
}
None
}