Debian Codegraph @

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

raw
1 mod format0 {
2 use ttf_parser::{cmap, GlyphId};
3 use crate::{convert, Unit::*};
4
5 #[test]
6 fn maps_not_all_256_codepoints() {
7 let mut data = convert(&[
8 UInt16(0), // format
9 UInt16(262), // subtable size
10 UInt16(0), // language ID
11 ]);
12
13 // Map (only) codepoint 0x40 to 100.
14 data.extend(std::iter::repeat(0).take(256));
15 data[6 + 0x40] = 100;
16
17 let subtable = cmap::Subtable0::parse(&data).unwrap();
18
19 assert_eq!(subtable.glyph_index(0), None);
20 assert_eq!(subtable.glyph_index(0x40), Some(GlyphId(100)));
21 assert_eq!(subtable.glyph_index(100), None);
22
23 let mut vec = vec![];
24 subtable.codepoints(|c| vec.push(c));
25 assert_eq!(vec, [0x40]);
26 }
27 }
28
29 mod format2 {
30 use ttf_parser::{cmap, GlyphId};
31 use crate::{convert, Unit::*};
32
33 const U16_SIZE: usize = std::mem::size_of::<u16>();
34
35 #[test]
36 fn collect_codepoints() {
37 let mut data = convert(&[
38 UInt16(2), // format
39 UInt16(534), // subtable size
40 UInt16(0), // language ID
41 ]);
42
43 // Make only high byte 0x28 multi-byte.
44 data.extend(std::iter::repeat(0x00).take(256 * U16_SIZE));
45 data[6 + 0x28 * U16_SIZE + 1] = 0x08;
46
47 data.extend(convert(&[
48 // First sub header (for single byte mapping)
49 UInt16(254), // first code
50 UInt16(2), // entry count
51 UInt16(0), // id delta: uninteresting
52 UInt16(0), // id range offset: uninteresting
53 // Second sub header (for high byte 0x28)
54 UInt16(16), // first code: (0x28 << 8) + 0x10 = 10256
55 UInt16(3), // entry count
56 UInt16(0), // id delta: uninteresting
57 UInt16(0), // id range offset: uninteresting
58 ]));
59
60 // Now only glyph ID's would follow. Not interesting for codepoints.
61
62 let subtable = cmap::Subtable2::parse(&data).unwrap();
63
64 let mut vec = vec![];
65 subtable.codepoints(|c| vec.push(c));
66 assert_eq!(vec, [10256, 10257, 10258, 254, 255]);
67 }
68
69 #[test]
70 fn codepoint_at_range_end() {
71 let mut data = convert(&[
72 UInt16(2), // format
73 UInt16(532), // subtable size
74 UInt16(0), // language ID
75 ]);
76
77 // Only single bytes.
78 data.extend(std::iter::repeat(0x00).take(256 * U16_SIZE));
79 data.extend(convert(&[
80 // First sub header (for single byte mapping)
81 UInt16(40), // first code
82 UInt16(2), // entry count
83 UInt16(0), // id delta
84 UInt16(2), // id range offset
85 // Glyph index
86 UInt16(100), // glyph ID [0]
87 UInt16(1000), // glyph ID [1]
88 UInt16(10000), // glyph ID [2] (unused)
89 ]));
90
91 let subtable = cmap::Subtable2::parse(&data).unwrap();
92 assert_eq!(subtable.glyph_index(39), None);
93 assert_eq!(subtable.glyph_index(40), Some(GlyphId(100)));
94 assert_eq!(subtable.glyph_index(41), Some(GlyphId(1000)));
95 assert_eq!(subtable.glyph_index(42), None);
96 }
97 }
98
99 mod format4 {
100 use ttf_parser::{cmap, GlyphId};
101 use crate::{convert, Unit::*};
102
103 #[test]
104 fn single_glyph() {
105 let data = convert(&[
106 UInt16(4), // format
107 UInt16(32), // subtable size
108 UInt16(0), // language ID
109 UInt16(4), // 2 x segCount
110 UInt16(2), // search range
111 UInt16(0), // entry selector
112 UInt16(2), // range shift
113 // End character codes
114 UInt16(65), // char code [0]
115 UInt16(65535), // char code [1]
116 UInt16(0), // reserved
117 // Start character codes
118 UInt16(65), // char code [0]
119 UInt16(65535), // char code [1]
120 // Deltas
121 Int16(-64), // delta [0]
122 Int16(1), // delta [1]
123 // Offsets into Glyph index array
124 UInt16(0), // offset [0]
125 UInt16(0), // offset [1]
126 ]);
127
128 let subtable = cmap::Subtable4::parse(&data).unwrap();
129 assert_eq!(subtable.glyph_index(0x41), Some(GlyphId(1)));
130 assert_eq!(subtable.glyph_index(0x42), None);
131 }
132
133 #[test]
134 fn continuous_range() {
135 let data = convert(&[
136 UInt16(4), // format
137 UInt16(32), // subtable size
138 UInt16(0), // language ID
139 UInt16(4), // 2 x segCount
140 UInt16(2), // search range
141 UInt16(0), // entry selector
142 UInt16(2), // range shift
143 // End character codes
144 UInt16(73), // char code [0]
145 UInt16(65535), // char code [1]
146 UInt16(0), // reserved
147 // Start character codes
148 UInt16(65), // char code [0]
149 UInt16(65535), // char code [1]
150 // Deltas
151 Int16(-64), // delta [0]
152 Int16(1), // delta [1]
153 // Offsets into Glyph index array
154 UInt16(0), // offset [0]
155 UInt16(0), // offset [1]
156 ]);
157
158 let subtable = cmap::Subtable4::parse(&data).unwrap();
159 assert_eq!(subtable.glyph_index(0x40), None);
160 assert_eq!(subtable.glyph_index(0x41), Some(GlyphId(1)));
161 assert_eq!(subtable.glyph_index(0x42), Some(GlyphId(2)));
162 assert_eq!(subtable.glyph_index(0x43), Some(GlyphId(3)));
163 assert_eq!(subtable.glyph_index(0x44), Some(GlyphId(4)));
164 assert_eq!(subtable.glyph_index(0x45), Some(GlyphId(5)));
165 assert_eq!(subtable.glyph_index(0x46), Some(GlyphId(6)));
166 assert_eq!(subtable.glyph_index(0x47), Some(GlyphId(7)));
167 assert_eq!(subtable.glyph_index(0x48), Some(GlyphId(8)));
168 assert_eq!(subtable.glyph_index(0x49), Some(GlyphId(9)));
169 assert_eq!(subtable.glyph_index(0x4A), None);
170 }
171
172 #[test]
173 fn multiple_ranges() {
174 let data = convert(&[
175 UInt16(4), // format
176 UInt16(48), // subtable size
177 UInt16(0), // language ID
178 UInt16(8), // 2 x segCount
179 UInt16(4), // search range
180 UInt16(1), // entry selector
181 UInt16(4), // range shift
182 // End character codes
183 UInt16(65), // char code [0]
184 UInt16(69), // char code [1]
185 UInt16(73), // char code [2]
186 UInt16(65535), // char code [3]
187 UInt16(0), // reserved
188 // Start character codes
189 UInt16(65), // char code [0]
190 UInt16(67), // char code [1]
191 UInt16(71), // char code [2]
192 UInt16(65535), // char code [3]
193 // Deltas
194 Int16(-64), // delta [0]
195 Int16(-65), // delta [1]
196 Int16(-66), // delta [2]
197 Int16(1), // delta [3]
198 // Offsets into Glyph index array
199 UInt16(0), // offset [0]
200 UInt16(0), // offset [1]
201 UInt16(0), // offset [2]
202 UInt16(0), // offset [3]
203 ]);
204
205 let subtable = cmap::Subtable4::parse(&data).unwrap();
206 assert_eq!(subtable.glyph_index(0x40), None);
207 assert_eq!(subtable.glyph_index(0x41), Some(GlyphId(1)));
208 assert_eq!(subtable.glyph_index(0x42), None);
209 assert_eq!(subtable.glyph_index(0x43), Some(GlyphId(2)));
210 assert_eq!(subtable.glyph_index(0x44), Some(GlyphId(3)));
211 assert_eq!(subtable.glyph_index(0x45), Some(GlyphId(4)));
212 assert_eq!(subtable.glyph_index(0x46), None);
213 assert_eq!(subtable.glyph_index(0x47), Some(GlyphId(5)));
214 assert_eq!(subtable.glyph_index(0x48), Some(GlyphId(6)));
215 assert_eq!(subtable.glyph_index(0x49), Some(GlyphId(7)));
216 assert_eq!(subtable.glyph_index(0x4A), None);
217 }
218
219 #[test]
220 fn unordered_ids() {
221 let data = convert(&[
222 UInt16(4), // format
223 UInt16(42), // subtable size
224 UInt16(0), // language ID
225 UInt16(4), // 2 x segCount
226 UInt16(2), // search range
227 UInt16(0), // entry selector
228 UInt16(2), // range shift
229 // End character codes
230 UInt16(69), // char code [0]
231 UInt16(65535), // char code [1]
232 UInt16(0), // reserved
233 // Start character codes
234 UInt16(65), // char code [0]
235 UInt16(65535), // char code [1]
236 // Deltas
237 Int16(0), // delta [0]
238 Int16(1), // delta [1]
239 // Offsets into Glyph index array
240 UInt16(4), // offset [0]
241 UInt16(0), // offset [1]
242 // Glyph index array
243 UInt16(1), // glyph ID [0]
244 UInt16(10), // glyph ID [1]
245 UInt16(100), // glyph ID [2]
246 UInt16(1000), // glyph ID [3]
247 UInt16(10000), // glyph ID [4]
248 ]);
249
250 let subtable = cmap::Subtable4::parse(&data).unwrap();
251 assert_eq!(subtable.glyph_index(0x40), None);
252 assert_eq!(subtable.glyph_index(0x41), Some(GlyphId(1)));
253 assert_eq!(subtable.glyph_index(0x42), Some(GlyphId(10)));
254 assert_eq!(subtable.glyph_index(0x43), Some(GlyphId(100)));
255 assert_eq!(subtable.glyph_index(0x44), Some(GlyphId(1000)));
256 assert_eq!(subtable.glyph_index(0x45), Some(GlyphId(10000)));
257 assert_eq!(subtable.glyph_index(0x46), None);
258 }
259
260 #[test]
261 fn unordered_chars_and_ids() {
262 let data = convert(&[
263 UInt16(4), // format
264 UInt16(64), // subtable size
265 UInt16(0), // language ID
266 UInt16(12), // 2 x segCount
267 UInt16(8), // search range
268 UInt16(2), // entry selector
269 UInt16(4), // range shift
270 // End character codes
271 UInt16(80), // char code [0]
272 UInt16(256), // char code [1]
273 UInt16(336), // char code [2]
274 UInt16(512), // char code [3]
275 UInt16(592), // char code [4]
276 UInt16(65535), // char code [5]
277 UInt16(0), // reserved
278 // Start character codes
279 UInt16(80), // char code [0]
280 UInt16(256), // char code [1]
281 UInt16(336), // char code [2]
282 UInt16(512), // char code [3]
283 UInt16(592), // char code [4]
284 UInt16(65535), // char code [5]
285 // Deltas
286 Int16(-79), // delta [0]
287 Int16(-246), // delta [1]
288 Int16(-236), // delta [2]
289 Int16(488), // delta [3]
290 Int16(9408), // delta [4]
291 Int16(1), // delta [5]
292 // Offsets into Glyph index array
293 UInt16(0), // offset [0]
294 UInt16(0), // offset [1]
295 UInt16(0), // offset [2]
296 UInt16(0), // offset [3]
297 UInt16(0), // offset [4]
298 UInt16(0), // offset [5]
299 ]);
300
301 let subtable = cmap::Subtable4::parse(&data).unwrap();
302 assert_eq!(subtable.glyph_index(0x40), None);
303 assert_eq!(subtable.glyph_index(0x50), Some(GlyphId(1)));
304 assert_eq!(subtable.glyph_index(0x100), Some(GlyphId(10)));
305 assert_eq!(subtable.glyph_index(0x150), Some(GlyphId(100)));
306 assert_eq!(subtable.glyph_index(0x200), Some(GlyphId(1000)));
307 assert_eq!(subtable.glyph_index(0x250), Some(GlyphId(10000)));
308 assert_eq!(subtable.glyph_index(0x300), None);
309 }
310
311 #[test]
312 fn no_end_codes() {
313 let data = convert(&[
314 UInt16(4), // format
315 UInt16(28), // subtable size
316 UInt16(0), // language ID
317 UInt16(4), // 2 x segCount
318 UInt16(2), // search range
319 UInt16(0), // entry selector
320 UInt16(2), // range shift
321 // End character codes
322 UInt16(73), // char code [0]
323 // 0xFF, 0xFF, // char code [1] <-- removed
324 UInt16(0), // reserved
325 // Start character codes
326 UInt16(65), // char code [0]
327 // 0xFF, 0xFF, // char code [1] <-- removed
328 // Deltas
329 Int16(-64), // delta [0]
330 Int16(1), // delta [1]
331 // Offsets into Glyph index array
332 UInt16(0), // offset [0]
333 UInt16(0), // offset [1]
334 ]);
335
336 assert!(cmap::Subtable4::parse(&data).is_none());
337 }
338
339 #[test]
340 fn invalid_segment_count() {
341 let data = convert(&[
342 UInt16(4), // format
343 UInt16(32), // subtable size
344 UInt16(0), // language ID
345 UInt16(1), // 2 x segCount <-- must be more than 1
346 UInt16(2), // search range
347 UInt16(0), // entry selector
348 UInt16(2), // range shift
349 // End character codes
350 UInt16(65), // char code [0]
351 UInt16(65535), // char code [1]
352 UInt16(0), // reserved
353 // Start character codes
354 UInt16(65), // char code [0]
355 UInt16(65535), // char code [1]
356 // Deltas
357 Int16(-64), // delta [0]
358 Int16(1), // delta [1]
359 // Offsets into Glyph index array
360 UInt16(0), // offset [0]
361 UInt16(0), // offset [1]
362 ]);
363
364 assert!(cmap::Subtable4::parse(&data).is_none());
365 }
366
367 #[test]
368 fn only_end_segments() {
369 let data = convert(&[
370 UInt16(4), // format
371 UInt16(32), // subtable size
372 UInt16(0), // language ID
373 UInt16(2), // 2 x segCount
374 UInt16(2), // search range
375 UInt16(0), // entry selector
376 UInt16(2), // range shift
377 // End character codes
378 UInt16(65535), // char code [1]
379 UInt16(0), // reserved
380 // Start character codes
381 UInt16(65535), // char code [1]
382 // Deltas
383 Int16(-64), // delta [0]
384 Int16(1), // delta [1]
385 // Offsets into Glyph index array
386 UInt16(0), // offset [0]
387 UInt16(0), // offset [1]
388 ]);
389
390 let subtable = cmap::Subtable4::parse(&data).unwrap();
391 // Should not loop forever.
392 assert_eq!(subtable.glyph_index(0x41), None);
393 }
394
395 #[test]
396 fn invalid_length() {
397 let data = convert(&[
398 UInt16(4), // format
399 UInt16(16), // subtable size <-- the size should be 32, but we don't check it anyway
400 UInt16(0), // language ID
401 UInt16(4), // 2 x segCount
402 UInt16(2), // search range
403 UInt16(0), // entry selector
404 UInt16(2), // range shift
405 // End character codes
406 UInt16(65), // char code [0]
407 UInt16(65535), // char code [1]
408 UInt16(0), // reserved
409 // Start character codes
410 UInt16(65), // char code [0]
411 UInt16(65535), // char code [1]
412 // Deltas
413 Int16(-64), // delta [0]
414 Int16(1), // delta [1]
415 // Offsets into Glyph index array
416 UInt16(0), // offset [0]
417 UInt16(0), // offset [1]
418 ]);
419
420 let subtable = cmap::Subtable4::parse(&data).unwrap();
421 assert_eq!(subtable.glyph_index(0x41), Some(GlyphId(1)));
422 assert_eq!(subtable.glyph_index(0x42), None);
423 }
424
425 #[test]
426 fn codepoint_out_of_range() {
427 let data = convert(&[
428 UInt16(4), // format
429 UInt16(32), // subtable size
430 UInt16(0), // language ID
431 UInt16(4), // 2 x segCount
432 UInt16(2), // search range
433 UInt16(0), // entry selector
434 UInt16(2), // range shift
435 // End character codes
436 UInt16(65), // char code [0]
437 UInt16(65535), // char code [1]
438 UInt16(0), // reserved
439 // Start character codes
440 UInt16(65), // char code [0]
441 UInt16(65535), // char code [1]
442 // Deltas
443 Int16(-64), // delta [0]
444 Int16(1), // delta [1]
445 // Offsets into Glyph index array
446 UInt16(0), // offset [0]
447 UInt16(0), // offset [1]
448 ]);
449
450 let subtable = cmap::Subtable4::parse(&data).unwrap();
451 // Format 4 support only u16 codepoints, so we have to bail immediately otherwise.
452 assert_eq!(subtable.glyph_index(0x1FFFF), None);
453 }
454
455 #[test]
456 fn zero() {
457 let data = convert(&[
458 UInt16(4), // format
459 UInt16(42), // subtable size
460 UInt16(0), // language ID
461 UInt16(4), // 2 x segCount
462 UInt16(2), // search range
463 UInt16(0), // entry selector
464 UInt16(2), // range shift
465 // End character codes
466 UInt16(69), // char code [0]
467 UInt16(65535), // char code [1]
468 UInt16(0), // reserved
469 // Start character codes
470 UInt16(65), // char code [0]
471 UInt16(65535), // char code [1]
472 // Deltas
473 Int16(0), // delta [0]
474 Int16(1), // delta [1]
475 // Offsets into Glyph index array
476 UInt16(4), // offset [0]
477 UInt16(0), // offset [1]
478 // Glyph index array
479 UInt16(0), // glyph ID [0] <-- indicates missing glyph
480 UInt16(10), // glyph ID [1]
481 UInt16(100), // glyph ID [2]
482 UInt16(1000), // glyph ID [3]
483 UInt16(10000), // glyph ID [4]
484 ]);
485
486 let subtable = cmap::Subtable4::parse(&data).unwrap();
487 assert_eq!(subtable.glyph_index(0x41), None);
488 }
489
490 #[test]
491 fn invalid_offset() {
492 let data = convert(&[
493 UInt16(4), // format
494 UInt16(42), // subtable size
495 UInt16(0), // language ID
496 UInt16(4), // 2 x segCount
497 UInt16(2), // search range
498 UInt16(0), // entry selector
499 UInt16(2), // range shift
500 // End character codes
501 UInt16(69), // char code [0]
502 UInt16(65535), // char code [1]
503 UInt16(0), // reserved
504 // Start character codes
505 UInt16(65), // char code [0]
506 UInt16(65535), // char code [1]
507 // Deltas
508 Int16(0), // delta [0]
509 Int16(1), // delta [1]
510 // Offsets into Glyph index array
511 UInt16(4), // offset [0]
512 UInt16(65535), // offset [1]
513 // Glyph index array
514 UInt16(1), // glyph ID [0]
515 ]);
516
517 let subtable = cmap::Subtable4::parse(&data).unwrap();
518 assert_eq!(subtable.glyph_index(65535), None);
519 }
520
521 #[test]
522 fn collect_codepoints() {
523 let data = convert(&[
524 UInt16(4), // format
525 UInt16(24), // subtable size
526 UInt16(0), // language ID
527 UInt16(4), // 2 x segCount
528 UInt16(2), // search range
529 UInt16(0), // entry selector
530 UInt16(2), // range shift
531 // End character codes
532 UInt16(34), // char code [0]
533 UInt16(65535), // char code [1]
534 UInt16(0), // reserved
535 // Start character codes
536 UInt16(27), // char code [0]
537 UInt16(65533), // char code [1]
538 // Deltas
539 Int16(0), // delta [0]
540 Int16(1), // delta [1]
541 // Offsets into Glyph index array
542 UInt16(4), // offset [0]
543 UInt16(0), // offset [1]
544 // Glyph index array
545 UInt16(0), // glyph ID [0]
546 UInt16(10), // glyph ID [1]
547 ]);
548
549 let subtable = cmap::Subtable4::parse(&data).unwrap();
550
551 let mut vec = vec![];
552 subtable.codepoints(|c| vec.push(c));
553 assert_eq!(vec, [27, 28, 29, 30, 31, 32, 33, 34, 65533, 65534, 65535]);
554 }
555 }