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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
use crate::parse::*;
use crate::table::loca::*;
use crate::FontResult;
use alloc::vec::*;
#[allow(non_snake_case)]
pub mod SimpleFlags {
pub const NONE: u8 = 0x00;
pub const ON_CURVE: u8 = 0x01;
pub const X_SHORT: u8 = 0x02;
pub const Y_SHORT: u8 = 0x04;
pub const REPEAT: u8 = 0x08;
pub const X_DUAL: u8 = 0x10;
pub const Y_DUAL: u8 = 0x20;
pub const OVERLAP_SIMPLE: u8 = 0x40;
pub const X_SHORT_AND_DUAL: u8 = X_SHORT | X_DUAL;
pub const Y_SHORT_AND_DUAL: u8 = Y_SHORT | Y_DUAL;
}
#[allow(non_snake_case)]
pub mod CompoundFlags {
pub const NONE: u16 = 0x0000;
pub const ARGS_ARE_WORDS: u16 = 0x0001;
pub const ARGS_ARE_XY_VALUES: u16 = 0x0002;
pub const ROUND_XY_TO_GRID: u16 = 0x0004;
pub const WE_HAVE_A_SCALE: u16 = 0x0008;
pub const MORE_COMPONENTS: u16 = 0x0020;
pub const WE_HAVE_AN_X_AND_Y_SCALE: u16 = 0x0040;
pub const WE_HAVE_A_TWO_BY_TWO: u16 = 0x0080;
pub const WE_HAVE_INSTRUCTIONS: u16 = 0x0100;
pub const USE_MY_METRICS: u16 = 0x0200;
pub const OVERLAP_COMPOUND: u16 = 0x0400;
pub const SCALED_COMPONENT_OFFSET: u16 = 0x0800;
pub const UNSCALED_COMPONENT_OFFSET: u16 = 0x1000;
pub const ARGS_ARE_WORDS_AND_XY_VALUES: u16 = ARGS_ARE_WORDS | ARGS_ARE_XY_VALUES;
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct RawPoint {
pub x: f32,
pub y: f32,
pub flags: u8,
pub end_point: bool,
pub start_point: bool,
}
impl RawPoint {
#[inline]
pub fn on_curve(&self) -> bool {
flag_u8(self.flags, SimpleFlags::ON_CURVE)
}
pub fn transform(&mut self, a: f32, b: f32, c: f32, d: f32, cx: f32, cy: f32) {
let px = self.x;
let py = self.y;
self.x = a * px + c * py + cx;
self.y = b * px + d * py + cy;
}
}
#[derive(Clone)]
pub struct Glyph {
pub num_contours: i16,
pub xmin: i16,
pub xmax: i16,
pub ymin: i16,
pub ymax: i16,
pub metrics: usize,
pub points: Vec<RawPoint>,
}
impl Default for Glyph {
fn default() -> Glyph {
Glyph {
num_contours: 0,
xmin: 0,
xmax: 0,
ymin: 0,
ymax: 0,
metrics: 0,
points: Vec::with_capacity(0),
}
}
}
pub struct TableGlyf {
pub glyphs: Vec<Glyph>,
}
fn parse_glyph(glyf: &[u8], locations: &[GlyphLocation], index: usize) -> FontResult<Glyph> {
let loc = &locations[index];
let mut glyph = Glyph::default();
glyph.metrics = index;
if loc.length == 0 {
return Ok(glyph);
}
let mut offset = loc.offset;
glyph.num_contours = read_i16(&glyf[offset..]);
glyph.xmin = read_i16(&glyf[offset + 2..]);
glyph.ymin = read_i16(&glyf[offset + 4..]);
glyph.xmax = read_i16(&glyf[offset + 6..]);
glyph.ymax = read_i16(&glyf[offset + 8..]);
offset += 10;
if glyph.xmin == 32767 && glyph.xmax == -32767 && glyph.ymin == 32767 && glyph.ymax == -32767 {
glyph.xmin = 0;
glyph.ymin = 0;
glyph.xmax = 0;
glyph.ymax = 0;
}
if glyph.xmin > glyph.xmax || glyph.ymin > glyph.ymax {
return Err("Font.glyf: Bad glyph bounding box values (xmin > xmax || ymin > ymax)");
}
if glyph.num_contours == 0 {
} else if glyph.num_contours > 0 {
let mut end_points_of_contours = Vec::with_capacity(glyph.num_contours as usize);
for i in 0..glyph.num_contours as usize {
let end_points_of_contour = read_u16(&glyf[offset + i * 2..]);
end_points_of_contours.push(end_points_of_contour);
}
let num_points = end_points_of_contours[glyph.num_contours as usize - 1] as usize + 1;
offset += glyph.num_contours as usize * 2;
let instruction_length = read_u16(&glyf[offset..]);
offset += instruction_length as usize + 2;
glyph.points = Vec::with_capacity(num_points);
while glyph.points.len() < num_points {
let flags = read_u8(&glyf[offset..]);
let point = RawPoint {
x: 0.0,
y: 0.0,
flags,
start_point: false,
end_point: false,
};
glyph.points.push(point);
if flag_u8(flags, SimpleFlags::REPEAT) {
let count = read_u8(&glyf[offset + 1..]);
for _ in 0..count {
glyph.points.push(point);
}
offset += 2;
} else {
offset += 1;
}
}
let mut last_x = 0i16;
for point in &mut glyph.points {
match point.flags & (SimpleFlags::X_SHORT | SimpleFlags::X_DUAL) {
SimpleFlags::X_SHORT_AND_DUAL => {
last_x += read_u8(&glyf[offset..]) as i16;
offset += 1;
}
SimpleFlags::X_SHORT => {
last_x -= read_u8(&glyf[offset..]) as i16;
offset += 1;
}
SimpleFlags::X_DUAL => {
}
_ => {
last_x += read_i16(&glyf[offset..]);
offset += 2;
}
}
point.x = last_x as f32;
}
let mut last_y = 0i16;
for point in &mut glyph.points {
match point.flags & SimpleFlags::Y_SHORT_AND_DUAL {
SimpleFlags::Y_SHORT_AND_DUAL => {
last_y += read_u8(&glyf[offset..]) as i16;
offset += 1;
}
SimpleFlags::Y_SHORT => {
last_y -= read_u8(&glyf[offset..]) as i16;
offset += 1;
}
SimpleFlags::Y_DUAL => {
}
_ => {
last_y += read_i16(&glyf[offset..]);
offset += 2;
}
}
point.y = last_y as f32;
}
for end_point in end_points_of_contours {
let end_point = end_point as usize;
glyph.points[end_point].end_point = true;
glyph.points[(end_point + 1) % num_points].start_point = true;
}
} else {
let mut flags = CompoundFlags::MORE_COMPONENTS;
while flag_u16(flags, CompoundFlags::MORE_COMPONENTS) {
flags = read_u16(&glyf[offset..]);
let compound_glyph_index = read_u16(&glyf[offset + 2..]);
if flag_u16(flags, CompoundFlags::USE_MY_METRICS) {
glyph.metrics = compound_glyph_index as usize;
}
offset += 4;
let cx;
let cy;
match flags & CompoundFlags::ARGS_ARE_WORDS_AND_XY_VALUES {
CompoundFlags::ARGS_ARE_WORDS_AND_XY_VALUES => {
cx = read_i16(&glyf[offset..]) as i32;
cy = read_i16(&glyf[offset + 2..]) as i32;
offset += 4;
}
CompoundFlags::ARGS_ARE_WORDS => {
return Err("Font.glyf: Component matched point numbers are unsupported");
}
CompoundFlags::ARGS_ARE_XY_VALUES => {
cx = read_i8(&glyf[offset..]) as i32;
cy = read_i8(&glyf[offset + 1..]) as i32;
offset += 2;
}
_ => {
return Err("Font.glyf: Component matched point numbers are unsupported");
}
}
let mut a = 1.0;
let mut b = 0.0;
let mut c = 0.0;
let mut d = 1.0;
if flag_u16(flags, CompoundFlags::WE_HAVE_A_SCALE) {
a = read_f2dot14(&glyf[offset..]);
d = a;
offset += 2;
} else if flag_u16(flags, CompoundFlags::WE_HAVE_AN_X_AND_Y_SCALE) {
a = read_f2dot14(&glyf[offset..]);
d = read_f2dot14(&glyf[offset + 2..]);
offset += 4;
} else if flag_u16(flags, CompoundFlags::WE_HAVE_A_TWO_BY_TWO) {
a = read_f2dot14(&glyf[offset..]);
b = read_f2dot14(&glyf[offset + 2..]);
c = read_f2dot14(&glyf[offset + 4..]);
d = read_f2dot14(&glyf[offset + 6..]);
offset += 8;
} else {
}
if flag_u16(flags, CompoundFlags::SCALED_COMPONENT_OFFSET)
&& !flag_u16(flags, CompoundFlags::UNSCALED_COMPONENT_OFFSET)
{
return Err("Font.glyf: Scaled component offset is unsupported");
}
let mut compound_glyph_points =
parse_glyph(glyf, locations, compound_glyph_index as usize)?.points;
for point in &mut compound_glyph_points {
point.transform(a, b, c, d, cx as f32, cy as f32);
}
glyph.points.append(&mut compound_glyph_points);
}
}
for point in &glyph.points {
let x = point.x as i16;
if x < glyph.xmin {
glyph.xmin = x;
} else if x > glyph.xmax {
glyph.xmax = x;
}
let y = point.y as i16;
if y < glyph.ymin {
glyph.ymin = y;
} else if y > glyph.ymax {
glyph.ymax = y;
}
}
Ok(glyph)
}
impl TableGlyf {
pub fn new(glyf: &[u8], locations: &[GlyphLocation]) -> FontResult<TableGlyf> {
let mut glyphs = Vec::with_capacity(locations.len());
for i in 0..locations.len() {
let glyph = parse_glyph(glyf, locations, i)?;
glyphs.push(glyph);
}
Ok(TableGlyf {
glyphs,
})
}
}