Debian Codegraph @

debian/missing-sources/ttf-parser/tests/tables/cff1.rs

raw
1 // TODO: simplify/rewrite
2
3 use std::fmt::Write;
4
5 use ttf_parser::{cff, GlyphId, CFFError, Rect};
6
7 struct Builder(String);
8 impl ttf_parser::OutlineBuilder for Builder {
9 fn move_to(&mut self, x: f32, y: f32) {
10 write!(&mut self.0, "M {} {} ", x, y).unwrap();
11 }
12
13 fn line_to(&mut self, x: f32, y: f32) {
14 write!(&mut self.0, "L {} {} ", x, y).unwrap();
15 }
16
17 fn quad_to(&mut self, x1: f32, y1: f32, x: f32, y: f32) {
18 write!(&mut self.0, "Q {} {} {} {} ", x1, y1, x, y).unwrap();
19 }
20
21 fn curve_to(&mut self, x1: f32, y1: f32, x2: f32, y2: f32, x: f32, y: f32) {
22 write!(&mut self.0, "C {} {} {} {} {} {} ", x1, y1, x2, y2, x, y).unwrap();
23 }
24
25 fn close(&mut self) {
26 write!(&mut self.0, "Z ").unwrap();
27 }
28 }
29
30 #[allow(dead_code)]
31 mod operator {
32 pub const HORIZONTAL_STEM: u8 = 1;
33 pub const VERTICAL_STEM: u8 = 3;
34 pub const VERTICAL_MOVE_TO: u8 = 4;
35 pub const LINE_TO: u8 = 5;
36 pub const HORIZONTAL_LINE_TO: u8 = 6;
37 pub const VERTICAL_LINE_TO: u8 = 7;
38 pub const CURVE_TO: u8 = 8;
39 pub const CALL_LOCAL_SUBROUTINE: u8 = 10;
40 pub const RETURN: u8 = 11;
41 pub const ENDCHAR: u8 = 14;
42 pub const HORIZONTAL_STEM_HINT_MASK: u8 = 18;
43 pub const HINT_MASK: u8 = 19;
44 pub const COUNTER_MASK: u8 = 20;
45 pub const MOVE_TO: u8 = 21;
46 pub const HORIZONTAL_MOVE_TO: u8 = 22;
47 pub const VERTICAL_STEM_HINT_MASK: u8 = 23;
48 pub const CURVE_LINE: u8 = 24;
49 pub const LINE_CURVE: u8 = 25;
50 pub const VV_CURVE_TO: u8 = 26;
51 pub const HH_CURVE_TO: u8 = 27;
52 pub const SHORT_INT: u8 = 28;
53 pub const CALL_GLOBAL_SUBROUTINE: u8 = 29;
54 pub const VH_CURVE_TO: u8 = 30;
55 pub const HV_CURVE_TO: u8 = 31;
56 pub const HFLEX: u8 = 34;
57 pub const FLEX: u8 = 35;
58 pub const HFLEX1: u8 = 36;
59 pub const FLEX1: u8 = 37;
60 pub const FIXED_16_16: u8 = 255;
61 }
62
63 #[allow(dead_code)]
64 mod top_dict_operator {
65 pub const CHARSET_OFFSET: u16 = 15;
66 pub const CHAR_STRINGS_OFFSET: u16 = 17;
67 pub const PRIVATE_DICT_SIZE_AND_OFFSET: u16 = 18;
68 pub const ROS: u16 = 1230;
69 pub const FD_ARRAY: u16 = 1236;
70 pub const FD_SELECT: u16 = 1237;
71 }
72
73 mod private_dict_operator {
74 pub const LOCAL_SUBROUTINES_OFFSET: u16 = 19;
75 }
76
77 #[allow(dead_code)]
78 #[derive(Clone, Copy)]
79 enum TtfType {
80 Raw(&'static [u8]),
81 TrueTypeMagic,
82 OpenTypeMagic,
83 FontCollectionMagic,
84 Int8(i8),
85 UInt8(u8),
86 Int16(i16),
87 UInt16(u16),
88 Int32(i32),
89 UInt32(u32),
90 CFFInt(i32),
91 }
92
93 use TtfType::*;
94
95 fn convert(values: &[TtfType]) -> Vec<u8> {
96 let mut data = Vec::with_capacity(256);
97 for v in values {
98 convert_type(*v, &mut data);
99 }
100
101 data
102 }
103
104 fn convert_type(value: TtfType, data: &mut Vec<u8>) {
105 match value {
106 TtfType::Raw(bytes) => {
107 data.extend_from_slice(bytes);
108 }
109 TtfType::TrueTypeMagic => {
110 data.extend_from_slice(&[0x00, 0x01, 0x00, 0x00]);
111 }
112 TtfType::OpenTypeMagic => {
113 data.extend_from_slice(&[0x4F, 0x54, 0x54, 0x4F]);
114 }
115 TtfType::FontCollectionMagic => {
116 data.extend_from_slice(&[0x74, 0x74, 0x63, 0x66]);
117 }
118 TtfType::Int8(n) => {
119 data.extend_from_slice(&i8::to_be_bytes(n));
120 }
121 TtfType::UInt8(n) => {
122 data.extend_from_slice(&u8::to_be_bytes(n));
123 }
124 TtfType::Int16(n) => {
125 data.extend_from_slice(&i16::to_be_bytes(n));
126 }
127 TtfType::UInt16(n) => {
128 data.extend_from_slice(&u16::to_be_bytes(n));
129 }
130 TtfType::Int32(n) => {
131 data.extend_from_slice(&i32::to_be_bytes(n));
132 }
133 TtfType::UInt32(n) => {
134 data.extend_from_slice(&u32::to_be_bytes(n));
135 }
136 TtfType::CFFInt(n) => {
137 match n {
138 -107..=107 => {
139 data.push((n as i16 + 139) as u8);
140 }
141 108..=1131 => {
142 let n = n - 108;
143 data.push(((n >> 8) + 247) as u8);
144 data.push((n & 0xFF) as u8);
145 }
146 -1131..=-108 => {
147 let n = -n - 108;
148 data.push(((n >> 8) + 251) as u8);
149 data.push((n & 0xFF) as u8);
150 }
151 -32768..=32767 => {
152 data.push(28);
153 data.extend_from_slice(&i16::to_be_bytes(n as i16));
154 }
155 _ => {
156 data.push(29);
157 data.extend_from_slice(&i32::to_be_bytes(n));
158 }
159 }
160 }
161 }
162 }
163
164 #[derive(Debug)]
165 struct Writer {
166 data: Vec<u8>,
167 }
168
169 impl Writer {
170 fn new() -> Self {
171 Writer { data: Vec::with_capacity(256) }
172 }
173
174 fn offset(&self) -> usize {
175 self.data.len()
176 }
177
178 fn write(&mut self, value: TtfType) {
179 convert_type(value, &mut self.data);
180 }
181 }
182
183
184 fn gen_cff(
185 global_subrs: &[&[TtfType]],
186 local_subrs: &[&[TtfType]],
187 chars: &[TtfType],
188 ) -> Vec<u8> {
189 fn gen_global_subrs(subrs: &[&[TtfType]]) -> Vec<u8> {
190 let mut w = Writer::new();
191 for v1 in subrs {
192 for v2 in v1.iter() {
193 w.write(*v2);
194 }
195 }
196 w.data
197 }
198
199 fn gen_local_subrs(subrs: &[&[TtfType]]) -> Vec<u8> {
200 let mut w = Writer::new();
201 for v1 in subrs {
202 for v2 in v1.iter() {
203 w.write(*v2);
204 }
205 }
206 w.data
207 }
208
209 const EMPTY_INDEX_SIZE: usize = 2;
210 const INDEX_HEADER_SIZE: usize = 5;
211
212 // TODO: support multiple subrs
213 assert!(global_subrs.len() <= 1);
214 assert!(local_subrs.len() <= 1);
215
216 let global_subrs_data = gen_global_subrs(global_subrs);
217 let local_subrs_data = gen_local_subrs(local_subrs);
218 let chars_data = convert(chars);
219
220 assert!(global_subrs_data.len() < 255);
221 assert!(local_subrs_data.len() < 255);
222 assert!(chars_data.len() < 255);
223
224 let mut w = Writer::new();
225 // Header
226 w.write(UInt8(1)); // major version
227 w.write(UInt8(0)); // minor version
228 w.write(UInt8(4)); // header size
229 w.write(UInt8(0)); // absolute offset
230
231 // Name INDEX
232 w.write(UInt16(0)); // count
233
234 // Top DICT
235 // INDEX
236 w.write(UInt16(1)); // count
237 w.write(UInt8(1)); // offset size
238 w.write(UInt8(1)); // index[0]
239
240 let top_dict_idx2 = if local_subrs.is_empty() { 3 } else { 6 };
241 w.write(UInt8(top_dict_idx2)); // index[1]
242 // Item 0
243 let mut charstr_offset = w.offset() + 2;
244 charstr_offset += EMPTY_INDEX_SIZE; // String INDEX
245
246 // Global Subroutines INDEX
247 if !global_subrs_data.is_empty() {
248 charstr_offset += INDEX_HEADER_SIZE + global_subrs_data.len();
249 } else {
250 charstr_offset += EMPTY_INDEX_SIZE;
251 }
252
253 if !local_subrs_data.is_empty() {
254 charstr_offset += 3;
255 }
256
257 w.write(CFFInt(charstr_offset as i32));
258 w.write(UInt8(top_dict_operator::CHAR_STRINGS_OFFSET as u8));
259
260 if !local_subrs_data.is_empty() {
261 // Item 1
262 w.write(CFFInt(2)); // length
263 w.write(CFFInt((charstr_offset + INDEX_HEADER_SIZE + chars_data.len()) as i32)); // offset
264 w.write(UInt8(top_dict_operator::PRIVATE_DICT_SIZE_AND_OFFSET as u8));
265 }
266
267 // String INDEX
268 w.write(UInt16(0)); // count
269
270 // Global Subroutines INDEX
271 if global_subrs_data.is_empty() {
272 w.write(UInt16(0)); // count
273 } else {
274 w.write(UInt16(1)); // count
275 w.write(UInt8(1)); // offset size
276 w.write(UInt8(1)); // index[0]
277 w.write(UInt8(global_subrs_data.len() as u8 + 1)); // index[1]
278 w.data.extend_from_slice(&global_subrs_data);
279 }
280
281 // CharString INDEX
282 w.write(UInt16(1)); // count
283 w.write(UInt8(1)); // offset size
284 w.write(UInt8(1)); // index[0]
285 w.write(UInt8(chars_data.len() as u8 + 1)); // index[1]
286 w.data.extend_from_slice(&chars_data);
287
288 if !local_subrs_data.is_empty() {
289 // The local subroutines offset is relative to the beginning of the Private DICT data.
290
291 // Private DICT
292 w.write(CFFInt(2));
293 w.write(UInt8(private_dict_operator::LOCAL_SUBROUTINES_OFFSET as u8));
294
295 // Local Subroutines INDEX
296 w.write(UInt16(1)); // count
297 w.write(UInt8(1)); // offset size
298 w.write(UInt8(1)); // index[0]
299 w.write(UInt8(local_subrs_data.len() as u8 + 1)); // index[1]
300 w.data.extend_from_slice(&local_subrs_data);
301 }
302
303 w.data
304 }
305
306 #[test]
307 fn unsupported_version() {
308 let data = convert(&[
309 UInt8(10), // major version, only 1 is supported
310 UInt8(0), // minor version
311 UInt8(4), // header size
312 UInt8(0), // absolute offset
313 ]);
314
315 assert!(cff::Table::parse(&data).is_none());
316 }
317
318 #[test]
319 fn non_default_header_size() {
320 let data = convert(&[
321 // Header
322 UInt8(1), // major version
323 UInt8(0), // minor version
324 UInt8(8), // header size
325 UInt8(0), // absolute offset
326
327 // no-op, should be skipped
328 UInt8(0),
329 UInt8(0),
330 UInt8(0),
331 UInt8(0),
332
333 // Name INDEX
334 UInt16(0), // count
335
336 // Top DICT
337 // INDEX
338 UInt16(1), // count
339 UInt8(1), // offset size
340 UInt8(1), // index[0]
341 UInt8(3), // index[1]
342 // Data
343 CFFInt(21),
344 UInt8(top_dict_operator::CHAR_STRINGS_OFFSET as u8),
345
346 // String INDEX
347 UInt16(0), // count
348
349 // Global Subroutines INDEX
350 UInt16(0), // count
351
352 // CharString INDEX
353 UInt16(1), // count
354 UInt8(1), // offset size
355 UInt8(1), // index[0]
356 UInt8(4), // index[1]
357 // Data
358 CFFInt(10),
359 UInt8(operator::HORIZONTAL_MOVE_TO),
360 UInt8(operator::ENDCHAR),
361 ]);
362
363 let table = cff::Table::parse(&data).unwrap();
364 let mut builder = Builder(String::new());
365 let rect = table.outline(GlyphId(0), &mut builder).unwrap();
366
367 assert_eq!(builder.0, "M 10 0 Z ");
368 assert_eq!(rect, Rect { x_min: 10, y_min: 0, x_max: 10, y_max: 0 });
369 }
370
371 fn rect(x_min: i16, y_min: i16, x_max: i16, y_max: i16) -> Rect {
372 Rect { x_min, y_min, x_max, y_max }
373 }
374
375 macro_rules! test_cs_with_subrs {
376 ($name:ident, $glob:expr, $loc:expr, $values:expr, $path:expr, $rect_res:expr) => {
377 #[test]
378 fn $name() {
379 let data = gen_cff($glob, $loc, $values);
380 let table = cff::Table::parse(&data).unwrap();
381 let mut builder = Builder(String::new());
382 let rect = table.outline(GlyphId(0), &mut builder).unwrap();
383
384 assert_eq!(builder.0, $path);
385 assert_eq!(rect, $rect_res);
386 }
387 };
388 }
389
390 macro_rules! test_cs {
391 ($name:ident, $values:expr, $path:expr, $rect_res:expr) => {
392 test_cs_with_subrs!($name, &[], &[], $values, $path, $rect_res);
393 };
394 }
395
396 macro_rules! test_cs_err {
397 ($name:ident, $values:expr, $err:expr) => {
398 #[test]
399 fn $name() {
400 let data = gen_cff(&[], &[], $values);
401 let table = cff::Table::parse(&data).unwrap();
402 let mut builder = Builder(String::new());
403 let res = table.outline(GlyphId(0), &mut builder);
404 assert_eq!(res.unwrap_err(), $err);
405 }
406 };
407 }
408
409 test_cs!(move_to, &[
410 CFFInt(10), CFFInt(20), UInt8(operator::MOVE_TO),
411 UInt8(operator::ENDCHAR),
412 ], "M 10 20 Z ",
413 rect(10, 20, 10, 20)
414 );
415
416 test_cs!(move_to_with_width, &[
417 CFFInt(5), CFFInt(10), CFFInt(20), UInt8(operator::MOVE_TO),
418 UInt8(operator::ENDCHAR),
419 ], "M 10 20 Z ",
420 rect(10, 20, 10, 20)
421 );
422
423 test_cs!(hmove_to, &[
424 CFFInt(10), UInt8(operator::HORIZONTAL_MOVE_TO),
425 UInt8(operator::ENDCHAR),
426 ], "M 10 0 Z ",
427 rect(10, 0, 10, 0)
428 );
429
430 test_cs!(hmove_to_with_width, &[
431 CFFInt(10), CFFInt(20), UInt8(operator::HORIZONTAL_MOVE_TO),
432 UInt8(operator::ENDCHAR),
433 ], "M 20 0 Z ",
434 rect(20, 0, 20, 0)
435 );
436
437 test_cs!(vmove_to, &[
438 CFFInt(10), UInt8(operator::VERTICAL_MOVE_TO),
439 UInt8(operator::ENDCHAR),
440 ], "M 0 10 Z ",
441 rect(0, 10, 0, 10)
442 );
443
444 test_cs!(vmove_to_with_width, &[
445 CFFInt(10), CFFInt(20), UInt8(operator::VERTICAL_MOVE_TO),
446 UInt8(operator::ENDCHAR),
447 ], "M 0 20 Z ",
448 rect(0, 20, 0, 20)
449 );
450
451 // Use only the first width.
452 test_cs!(two_vmove_to_with_width, &[
453 CFFInt(10), CFFInt(20), UInt8(operator::VERTICAL_MOVE_TO),
454 CFFInt(10), CFFInt(20), UInt8(operator::VERTICAL_MOVE_TO),
455 UInt8(operator::ENDCHAR),
456 ], "M 0 20 Z M 0 40 Z ",
457 rect(0, 20, 0, 40)
458 );
459
460 test_cs!(line_to, &[
461 CFFInt(10), CFFInt(20), UInt8(operator::MOVE_TO),
462 CFFInt(30), CFFInt(40), UInt8(operator::LINE_TO),
463 UInt8(operator::ENDCHAR),
464 ], "M 10 20 L 40 60 Z ",
465 rect(10, 20, 40, 60)
466 );
467
468 test_cs!(line_to_with_multiple_pairs, &[
469 CFFInt(10), CFFInt(20), UInt8(operator::MOVE_TO),
470 CFFInt(30), CFFInt(40), CFFInt(50), CFFInt(60), UInt8(operator::LINE_TO),
471 UInt8(operator::ENDCHAR),
472 ], "M 10 20 L 40 60 L 90 120 Z ",
473 rect(10, 20, 90, 120)
474 );
475
476 test_cs!(hline_to, &[
477 CFFInt(10), CFFInt(20), UInt8(operator::MOVE_TO),
478 CFFInt(30), UInt8(operator::HORIZONTAL_LINE_TO),
479 UInt8(operator::ENDCHAR),
480 ], "M 10 20 L 40 20 Z ",
481 rect(10, 20, 40, 20)
482 );
483
484 test_cs!(hline_to_with_two_coords, &[
485 CFFInt(10), CFFInt(20), UInt8(operator::MOVE_TO),
486 CFFInt(30), CFFInt(40), UInt8(operator::HORIZONTAL_LINE_TO),
487 UInt8(operator::ENDCHAR),
488 ], "M 10 20 L 40 20 L 40 60 Z ",
489 rect(10, 20, 40, 60)
490 );
491
492 test_cs!(hline_to_with_three_coords, &[
493 CFFInt(10), CFFInt(20), UInt8(operator::MOVE_TO),
494 CFFInt(30), CFFInt(40), CFFInt(50), UInt8(operator::HORIZONTAL_LINE_TO),
495 UInt8(operator::ENDCHAR),
496 ], "M 10 20 L 40 20 L 40 60 L 90 60 Z ",
497 rect(10, 20, 90, 60)
498 );
499
500 test_cs!(vline_to, &[
501 CFFInt(10), CFFInt(20), UInt8(operator::MOVE_TO),
502 CFFInt(30), UInt8(operator::VERTICAL_LINE_TO),
503 UInt8(operator::ENDCHAR),
504 ], "M 10 20 L 10 50 Z ",
505 rect(10, 20, 10, 50)
506 );
507
508 test_cs!(vline_to_with_two_coords, &[
509 CFFInt(10), CFFInt(20), UInt8(operator::MOVE_TO),
510 CFFInt(30), CFFInt(40), UInt8(operator::VERTICAL_LINE_TO),
511 UInt8(operator::ENDCHAR),
512 ], "M 10 20 L 10 50 L 50 50 Z ",
513 rect(10, 20, 50, 50)
514 );
515
516 test_cs!(vline_to_with_three_coords, &[
517 CFFInt(10), CFFInt(20), UInt8(operator::MOVE_TO),
518 CFFInt(30), CFFInt(40), CFFInt(50), UInt8(operator::VERTICAL_LINE_TO),
519 UInt8(operator::ENDCHAR),
520 ], "M 10 20 L 10 50 L 50 50 L 50 100 Z ",
521 rect(10, 20, 50, 100)
522 );
523
524 test_cs!(curve_to, &[
525 CFFInt(10), CFFInt(20), UInt8(operator::MOVE_TO),
526 CFFInt(30), CFFInt(40), CFFInt(50), CFFInt(60), CFFInt(70), CFFInt(80),
527 UInt8(operator::CURVE_TO),
528 UInt8(operator::ENDCHAR),
529 ], "M 10 20 C 40 60 90 120 160 200 Z ",
530 rect(10, 20, 160, 200)
531 );
532
533 test_cs!(curve_to_with_two_sets_of_coords, &[
534 CFFInt(10), CFFInt(20), UInt8(operator::MOVE_TO),
535 CFFInt(30), CFFInt(40), CFFInt(50), CFFInt(60), CFFInt(70), CFFInt(80),
536 CFFInt(90), CFFInt(100), CFFInt(110), CFFInt(120), CFFInt(130), CFFInt(140),
537 UInt8(operator::CURVE_TO),
538 UInt8(operator::ENDCHAR),
539 ], "M 10 20 C 40 60 90 120 160 200 C 250 300 360 420 490 560 Z ",
540 rect(10, 20, 490, 560)
541 );
542
543 test_cs!(hh_curve_to, &[
544 CFFInt(10), CFFInt(20), UInt8(operator::MOVE_TO),
545 CFFInt(30), CFFInt(40), CFFInt(50), CFFInt(60), UInt8(operator::HH_CURVE_TO),
546 UInt8(operator::ENDCHAR),
547 ], "M 10 20 C 40 20 80 70 140 70 Z ",
548 rect(10, 20, 140, 70)
549 );
550
551 test_cs!(hh_curve_to_with_y, &[
552 CFFInt(10), CFFInt(20), UInt8(operator::MOVE_TO),
553 CFFInt(30), CFFInt(40), CFFInt(50), CFFInt(60), CFFInt(70), UInt8(operator::HH_CURVE_TO),
554 UInt8(operator::ENDCHAR),
555 ], "M 10 20 C 50 50 100 110 170 110 Z ",
556 rect(10, 20, 170, 110)
557 );
558
559 test_cs!(vv_curve_to, &[
560 CFFInt(10), CFFInt(20), UInt8(operator::MOVE_TO),
561 CFFInt(30), CFFInt(40), CFFInt(50), CFFInt(60), UInt8(operator::VV_CURVE_TO),
562 UInt8(operator::ENDCHAR),
563 ], "M 10 20 C 10 50 50 100 50 160 Z ",
564 rect(10, 20, 50, 160)
565 );
566
567 test_cs!(vv_curve_to_with_x, &[
568 CFFInt(10), CFFInt(20), UInt8(operator::MOVE_TO),
569 CFFInt(30), CFFInt(40), CFFInt(50), CFFInt(60), CFFInt(70), UInt8(operator::VV_CURVE_TO),
570 UInt8(operator::ENDCHAR),
571 ], "M 10 20 C 40 60 90 120 90 190 Z ",
572 rect(10, 20, 90, 190)
573 );
574
575 #[test]
576 fn only_endchar() {
577 let data = gen_cff(&[], &[], &[UInt8(operator::ENDCHAR)]);
578 let table = cff::Table::parse(&data).unwrap();
579 let mut builder = Builder(String::new());
580 assert!(table.outline(GlyphId(0), &mut builder).is_err());
581 }
582
583 test_cs_with_subrs!(local_subr,
584 &[],
585 &[&[
586 CFFInt(30),
587 CFFInt(40),
588 UInt8(operator::LINE_TO),
589 UInt8(operator::RETURN),
590 ]],
591 &[
592 CFFInt(10),
593 UInt8(operator::HORIZONTAL_MOVE_TO),
594 CFFInt(0 - 107), // subr index - subr bias
595 UInt8(operator::CALL_LOCAL_SUBROUTINE),
596 UInt8(operator::ENDCHAR),
597 ],
598 "M 10 0 L 40 40 Z ",
599 rect(10, 0, 40, 40)
600 );
601
602 test_cs_with_subrs!(endchar_in_subr,
603 &[],
604 &[&[
605 CFFInt(30),
606 CFFInt(40),
607 UInt8(operator::LINE_TO),
608 UInt8(operator::ENDCHAR),
609 ]],
610 &[
611 CFFInt(10),
612 UInt8(operator::HORIZONTAL_MOVE_TO),
613 CFFInt(0 - 107), // subr index - subr bias
614 UInt8(operator::CALL_LOCAL_SUBROUTINE),
615 ],
616 "M 10 0 L 40 40 Z ",
617 rect(10, 0, 40, 40)
618 );
619
620 test_cs_with_subrs!(global_subr,
621 &[&[
622 CFFInt(30),
623 CFFInt(40),
624 UInt8(operator::LINE_TO),
625 UInt8(operator::RETURN),
626 ]],
627 &[],
628 &[
629 CFFInt(10),
630 UInt8(operator::HORIZONTAL_MOVE_TO),
631 CFFInt(0 - 107), // subr index - subr bias
632 UInt8(operator::CALL_GLOBAL_SUBROUTINE),
633 UInt8(operator::ENDCHAR),
634 ],
635 "M 10 0 L 40 40 Z ",
636 rect(10, 0, 40, 40)
637 );
638
639 test_cs_err!(reserved_operator, &[
640 CFFInt(10), UInt8(2),
641 UInt8(operator::ENDCHAR),
642 ], CFFError::InvalidOperator);
643
644 test_cs_err!(line_to_without_move_to, &[
645 CFFInt(10), CFFInt(20), UInt8(operator::LINE_TO),
646 UInt8(operator::ENDCHAR),
647 ], CFFError::MissingMoveTo);
648
649 test_cs_err!(move_to_with_too_many_coords, &[
650 CFFInt(10), CFFInt(10), CFFInt(10), CFFInt(20), UInt8(operator::MOVE_TO),
651 UInt8(operator::ENDCHAR),
652 ], CFFError::InvalidArgumentsStackLength);
653
654 test_cs_err!(move_to_with_not_enough_coords, &[
655 CFFInt(10), UInt8(operator::MOVE_TO),
656 UInt8(operator::ENDCHAR),
657 ], CFFError::InvalidArgumentsStackLength);
658
659 test_cs_err!(hmove_to_with_too_many_coords, &[
660 CFFInt(10), CFFInt(10), CFFInt(10), UInt8(operator::HORIZONTAL_MOVE_TO),
661 UInt8(operator::ENDCHAR),
662 ], CFFError::InvalidArgumentsStackLength);
663
664 test_cs_err!(hmove_to_with_not_enough_coords, &[
665 UInt8(operator::HORIZONTAL_MOVE_TO),
666 UInt8(operator::ENDCHAR),
667 ], CFFError::InvalidArgumentsStackLength);
668
669 test_cs_err!(vmove_to_with_too_many_coords, &[
670 CFFInt(10), CFFInt(10), CFFInt(10), UInt8(operator::VERTICAL_MOVE_TO),
671 UInt8(operator::ENDCHAR),
672 ], CFFError::InvalidArgumentsStackLength);
673
674 test_cs_err!(vmove_to_with_not_enough_coords, &[
675 UInt8(operator::VERTICAL_MOVE_TO),
676 UInt8(operator::ENDCHAR),
677 ], CFFError::InvalidArgumentsStackLength);
678
679 test_cs_err!(line_to_with_single_coord, &[
680 CFFInt(10), CFFInt(20), UInt8(operator::MOVE_TO),
681 CFFInt(30), UInt8(operator::LINE_TO),
682 UInt8(operator::ENDCHAR),
683 ], CFFError::InvalidArgumentsStackLength);
684
685 test_cs_err!(line_to_with_odd_number_of_coord, &[
686 CFFInt(10), CFFInt(20), UInt8(operator::MOVE_TO),
687 CFFInt(30), CFFInt(40), CFFInt(50), UInt8(operator::LINE_TO),
688 UInt8(operator::ENDCHAR),
689 ], CFFError::InvalidArgumentsStackLength);
690
691 test_cs_err!(hline_to_without_coords, &[
692 CFFInt(10), CFFInt(20), UInt8(operator::MOVE_TO),
693 UInt8(operator::HORIZONTAL_LINE_TO),
694 UInt8(operator::ENDCHAR),
695 ], CFFError::InvalidArgumentsStackLength);
696
697 test_cs_err!(vline_to_without_coords, &[
698 CFFInt(10), CFFInt(20), UInt8(operator::MOVE_TO),
699 UInt8(operator::VERTICAL_LINE_TO),
700 UInt8(operator::ENDCHAR),
701 ], CFFError::InvalidArgumentsStackLength);
702
703 test_cs_err!(curve_to_with_invalid_num_of_coords_1, &[
704 CFFInt(10), CFFInt(20), UInt8(operator::MOVE_TO),
705 CFFInt(30), CFFInt(40), CFFInt(50), CFFInt(60), UInt8(operator::CURVE_TO),
706 UInt8(operator::ENDCHAR),
707 ], CFFError::InvalidArgumentsStackLength);
708
709 test_cs_err!(curve_to_with_invalid_num_of_coords_2, &[
710 CFFInt(10), CFFInt(20), UInt8(operator::MOVE_TO),
711 CFFInt(30), CFFInt(40), CFFInt(50), CFFInt(60), CFFInt(70), CFFInt(80), CFFInt(90),
712 UInt8(operator::CURVE_TO),
713 UInt8(operator::ENDCHAR),
714 ], CFFError::InvalidArgumentsStackLength);
715
716 test_cs_err!(hh_curve_to_with_not_enough_coords, &[
717 CFFInt(10), CFFInt(20), UInt8(operator::MOVE_TO),
718 CFFInt(30), CFFInt(40), CFFInt(50), UInt8(operator::HH_CURVE_TO),
719 UInt8(operator::ENDCHAR),
720 ], CFFError::InvalidArgumentsStackLength);
721
722 test_cs_err!(hh_curve_to_with_too_many_coords, &[
723 CFFInt(10), CFFInt(20), UInt8(operator::MOVE_TO),
724 CFFInt(30), CFFInt(40), CFFInt(50), CFFInt(30), CFFInt(40), CFFInt(50),
725 UInt8(operator::HH_CURVE_TO),
726 UInt8(operator::ENDCHAR),
727 ], CFFError::InvalidArgumentsStackLength);
728
729 test_cs_err!(vv_curve_to_with_not_enough_coords, &[
730 CFFInt(10), CFFInt(20), UInt8(operator::MOVE_TO),
731 CFFInt(30), CFFInt(40), CFFInt(50), UInt8(operator::VV_CURVE_TO),
732 UInt8(operator::ENDCHAR),
733 ], CFFError::InvalidArgumentsStackLength);
734
735 test_cs_err!(vv_curve_to_with_too_many_coords, &[
736 CFFInt(10), CFFInt(20), UInt8(operator::MOVE_TO),
737 CFFInt(30), CFFInt(40), CFFInt(50), CFFInt(30), CFFInt(40), CFFInt(50),
738 UInt8(operator::VV_CURVE_TO),
739 UInt8(operator::ENDCHAR),
740 ], CFFError::InvalidArgumentsStackLength);
741
742 test_cs_err!(multiple_endchar, &[
743 UInt8(operator::ENDCHAR),
744 UInt8(operator::ENDCHAR),
745 ], CFFError::DataAfterEndChar);
746
747 test_cs_err!(seac_with_not_enough_data, &[
748 CFFInt(0),
749 CFFInt(0),
750 CFFInt(0),
751 CFFInt(0),
752 UInt8(operator::ENDCHAR),
753 ], CFFError::NestingLimitReached);
754
755 test_cs_err!(operands_overflow, &[
756 CFFInt(0), CFFInt(1), CFFInt(2), CFFInt(3), CFFInt(4), CFFInt(5), CFFInt(6), CFFInt(7), CFFInt(8), CFFInt(9),
757 CFFInt(0), CFFInt(1), CFFInt(2), CFFInt(3), CFFInt(4), CFFInt(5), CFFInt(6), CFFInt(7), CFFInt(8), CFFInt(9),
758 CFFInt(0), CFFInt(1), CFFInt(2), CFFInt(3), CFFInt(4), CFFInt(5), CFFInt(6), CFFInt(7), CFFInt(8), CFFInt(9),
759 CFFInt(0), CFFInt(1), CFFInt(2), CFFInt(3), CFFInt(4), CFFInt(5), CFFInt(6), CFFInt(7), CFFInt(8), CFFInt(9),
760 CFFInt(0), CFFInt(1), CFFInt(2), CFFInt(3), CFFInt(4), CFFInt(5), CFFInt(6), CFFInt(7), CFFInt(8), CFFInt(9),
761 ], CFFError::ArgumentsStackLimitReached);
762
763 test_cs_err!(operands_overflow_with_4_byte_ints, &[
764 CFFInt(30000), CFFInt(30000), CFFInt(30000), CFFInt(30000), CFFInt(30000),
765 CFFInt(30000), CFFInt(30000), CFFInt(30000), CFFInt(30000), CFFInt(30000),
766 CFFInt(30000), CFFInt(30000), CFFInt(30000), CFFInt(30000), CFFInt(30000),
767 CFFInt(30000), CFFInt(30000), CFFInt(30000), CFFInt(30000), CFFInt(30000),
768 CFFInt(30000), CFFInt(30000), CFFInt(30000), CFFInt(30000), CFFInt(30000),
769 CFFInt(30000), CFFInt(30000), CFFInt(30000), CFFInt(30000), CFFInt(30000),
770 CFFInt(30000), CFFInt(30000), CFFInt(30000), CFFInt(30000), CFFInt(30000),
771 CFFInt(30000), CFFInt(30000), CFFInt(30000), CFFInt(30000), CFFInt(30000),
772 CFFInt(30000), CFFInt(30000), CFFInt(30000), CFFInt(30000), CFFInt(30000),
773 CFFInt(30000), CFFInt(30000), CFFInt(30000), CFFInt(30000), CFFInt(30000),
774 ], CFFError::ArgumentsStackLimitReached);
775
776 test_cs_err!(bbox_overflow, &[
777 CFFInt(32767), UInt8(operator::HORIZONTAL_MOVE_TO),
778 CFFInt(32767), UInt8(operator::HORIZONTAL_LINE_TO),
779 UInt8(operator::ENDCHAR),
780 ], CFFError::BboxOverflow);
781
782 #[test]
783 fn endchar_in_subr_with_extra_data_1() {
784 let data = gen_cff(
785 &[],
786 &[&[
787 CFFInt(30),
788 CFFInt(40),
789 UInt8(operator::LINE_TO),
790 UInt8(operator::ENDCHAR),
791 ]],
792 &[
793 CFFInt(10),
794 UInt8(operator::HORIZONTAL_MOVE_TO),
795 CFFInt(0 - 107), // subr index - subr bias
796 UInt8(operator::CALL_LOCAL_SUBROUTINE),
797 CFFInt(30),
798 CFFInt(40),
799 UInt8(operator::LINE_TO),
800 ]
801 );
802
803 let table = cff::Table::parse(&data).unwrap();
804 let mut builder = Builder(String::new());
805 let res = table.outline(GlyphId(0), &mut builder);
806 assert_eq!(res.unwrap_err(), CFFError::DataAfterEndChar);
807 }
808
809 #[test]
810 fn endchar_in_subr_with_extra_data_2() {
811 let data = gen_cff(
812 &[],
813 &[&[
814 CFFInt(30),
815 CFFInt(40),
816 UInt8(operator::LINE_TO),
817 UInt8(operator::ENDCHAR),
818 CFFInt(30),
819 CFFInt(40),
820 UInt8(operator::LINE_TO),
821 ]],
822 &[
823 CFFInt(10),
824 UInt8(operator::HORIZONTAL_MOVE_TO),
825 CFFInt(0 - 107), // subr index - subr bias
826 UInt8(operator::CALL_LOCAL_SUBROUTINE),
827 ]
828 );
829
830 let table = cff::Table::parse(&data).unwrap();
831 let mut builder = Builder(String::new());
832 let res = table.outline(GlyphId(0), &mut builder);
833 assert_eq!(res.unwrap_err(), CFFError::DataAfterEndChar);
834 }
835
836 #[test]
837 fn subr_without_return() {
838 let data = gen_cff(
839 &[],
840 &[&[
841 CFFInt(30),
842 CFFInt(40),
843 UInt8(operator::LINE_TO),
844 UInt8(operator::ENDCHAR),
845 CFFInt(30),
846 CFFInt(40),
847 UInt8(operator::LINE_TO),
848 ]],
849 &[
850 CFFInt(10),
851 UInt8(operator::HORIZONTAL_MOVE_TO),
852 CFFInt(0 - 107), // subr index - subr bias
853 UInt8(operator::CALL_LOCAL_SUBROUTINE),
854 ]
855 );
856
857 let table = cff::Table::parse(&data).unwrap();
858 let mut builder = Builder(String::new());
859 let res = table.outline(GlyphId(0), &mut builder);
860 assert_eq!(res.unwrap_err(), CFFError::DataAfterEndChar);
861 }
862
863 #[test]
864 fn recursive_local_subr() {
865 let data = gen_cff(
866 &[],
867 &[&[
868 CFFInt(0 - 107), // subr index - subr bias
869 UInt8(operator::CALL_LOCAL_SUBROUTINE),
870 ]],
871 &[
872 CFFInt(10),
873 UInt8(operator::HORIZONTAL_MOVE_TO),
874 CFFInt(0 - 107), // subr index - subr bias
875 UInt8(operator::CALL_LOCAL_SUBROUTINE),
876 ]
877 );
878
879 let table = cff::Table::parse(&data).unwrap();
880 let mut builder = Builder(String::new());
881 let res = table.outline(GlyphId(0), &mut builder);
882 assert_eq!(res.unwrap_err(), CFFError::NestingLimitReached);
883 }
884
885 #[test]
886 fn recursive_global_subr() {
887 let data = gen_cff(
888 &[&[
889 CFFInt(0 - 107), // subr index - subr bias
890 UInt8(operator::CALL_GLOBAL_SUBROUTINE),
891 ]],
892 &[],
893 &[
894 CFFInt(10),
895 UInt8(operator::HORIZONTAL_MOVE_TO),
896 CFFInt(0 - 107), // subr index - subr bias
897 UInt8(operator::CALL_GLOBAL_SUBROUTINE),
898 ]
899 );
900
901 let table = cff::Table::parse(&data).unwrap();
902 let mut builder = Builder(String::new());
903 let res = table.outline(GlyphId(0), &mut builder);
904 assert_eq!(res.unwrap_err(), CFFError::NestingLimitReached);
905 }
906
907 #[test]
908 fn recursive_mixed_subr() {
909 let data = gen_cff(
910 &[&[
911 CFFInt(0 - 107), // subr index - subr bias
912 UInt8(operator::CALL_LOCAL_SUBROUTINE),
913 ]],
914 &[&[
915 CFFInt(0 - 107), // subr index - subr bias
916 UInt8(operator::CALL_GLOBAL_SUBROUTINE),
917 ]],
918 &[
919 CFFInt(10),
920 UInt8(operator::HORIZONTAL_MOVE_TO),
921 CFFInt(0 - 107), // subr index - subr bias
922 UInt8(operator::CALL_GLOBAL_SUBROUTINE),
923 ]
924 );
925
926 let table = cff::Table::parse(&data).unwrap();
927 let mut builder = Builder(String::new());
928 let res = table.outline(GlyphId(0), &mut builder);
929 assert_eq!(res.unwrap_err(), CFFError::NestingLimitReached);
930 }
931
932 #[test]
933 fn zero_char_string_offset() {
934 let data = convert(&[
935 // Header
936 UInt8(1), // major version
937 UInt8(0), // minor version
938 UInt8(4), // header size
939 UInt8(0), // absolute offset
940
941 // Name INDEX
942 UInt16(0), // count
943
944 // Top DICT
945 // INDEX
946 UInt16(1), // count
947 UInt8(1), // offset size
948 UInt8(1), // index[0]
949 UInt8(3), // index[1]
950 // Data
951 CFFInt(0), // zero offset!
952 UInt8(top_dict_operator::CHAR_STRINGS_OFFSET as u8),
953 ]);
954
955 assert!(cff::Table::parse(&data).is_none());
956 }
957
958 #[test]
959 fn invalid_char_string_offset() {
960 let data = convert(&[
961 // Header
962 UInt8(1), // major version
963 UInt8(0), // minor version
964 UInt8(4), // header size
965 UInt8(0), // absolute offset
966
967 // Name INDEX
968 UInt16(0), // count
969
970 // Top DICT
971 // INDEX
972 UInt16(1), // count
973 UInt8(1), // offset size
974 UInt8(1), // index[0]
975 UInt8(3), // index[1]
976 // Data
977 CFFInt(2), // invalid offset!
978 UInt8(top_dict_operator::CHAR_STRINGS_OFFSET as u8),
979 ]);
980
981 assert!(cff::Table::parse(&data).is_none());
982 }
983
984 // TODO: return from main
985 // TODO: return without endchar
986 // TODO: data after return
987 // TODO: recursive subr
988 // TODO: HORIZONTAL_STEM
989 // TODO: VERTICAL_STEM
990 // TODO: HORIZONTAL_STEM_HINT_MASK
991 // TODO: HINT_MASK
992 // TODO: COUNTER_MASK
993 // TODO: VERTICAL_STEM_HINT_MASK
994 // TODO: CURVE_LINE
995 // TODO: LINE_CURVE
996 // TODO: VH_CURVE_TO
997 // TODO: HFLEX
998 // TODO: FLEX
999 // TODO: HFLEX1
1000 // TODO: FLEX1