pdf2-render 1.1.0

Rasteriser for the pdf2 crate: paths, clips, transforms and (soon) images and text.
Documentation
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
//! Fonts for text rendering: a provider trait, a built-in base-14 fallback and
//! TrueType outlines through `ttf-parser`.

use pdf2::Encoding;

use crate::geom::Point;

/// One glyph's outline in em units: y grows upwards, the baseline is y = 0 and
/// one em is 1.0.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Glyph {
    /// Closed contours of the outline.
    pub contours: Vec<Vec<Point>>,
    /// Advance width in em units.
    pub advance: f64,
}

/// A resolved font ready to place glyphs.
#[derive(Debug, Clone)]
pub enum Font {
    /// The built-in fallback: a 5×7 bitmap alphabet, used when the document
    /// carries no usable font program (all of the base-14 cases).
    Builtin {
        /// Code → character table.
        encoding: Encoding,
    },
    /// An embedded TrueType program.
    TrueType {
        /// Code → character table.
        encoding: Encoding,
        /// The font program, still compressed in the PDF sense is not our
        /// business: this is the decoded `/FontFile2`.
        data: Vec<u8>,
    },
}

impl Font {
    /// The built-in fallback with an encoding.
    pub fn builtin(encoding: Encoding) -> Self {
        Self::Builtin { encoding }
    }

    /// An embedded TrueType program, rejected when it cannot be parsed.
    pub fn true_type(encoding: Encoding, data: Vec<u8>) -> Option<Self> {
        let face = ttf_parser::Face::parse(&data, 0).ok()?;
        if face.units_per_em() == 0 {
            return None;
        }
        Some(Self::TrueType { encoding, data })
    }

    /// The outline and advance for a byte code, or `None` when there is no
    /// glyph for it.
    pub fn glyph(&self, code: u8) -> Option<Glyph> {
        match self {
            Self::Builtin { encoding } => builtin_glyph(encoding.character(code)?),
            Self::TrueType { encoding, data } => {
                let character = encoding.character(code)?;
                let face = ttf_parser::Face::parse(data, 0).ok()?;
                let glyph_id = face.glyph_index(character)?;
                glyph_from_face(data, glyph_id)
            }
        }
    }

    /// The advance of a code in em units, falling back to the built-in metric.
    pub fn advance(&self, code: u8) -> f64 {
        self.glyph(code)
            .map_or(BUILTIN_ADVANCE, |glyph| glyph.advance)
    }

    /// The outline for a glyph id, which is how composite (Type 0) fonts select
    /// glyphs: a CID maps to an id through `/CIDToGIDMap`, not through a cmap.
    ///
    /// The built-in bitmap alphabet has no glyph ids, so it has no answer here.
    pub fn glyph_by_id(&self, id: u16) -> Option<Glyph> {
        match self {
            Self::Builtin { .. } => None,
            Self::TrueType { data, .. } => glyph_from_face(data, ttf_parser::GlyphId(id)),
        }
    }
}

/// An outline for a glyph id, from a parsed TrueType program.
fn glyph_from_face(data: &[u8], glyph_id: ttf_parser::GlyphId) -> Option<Glyph> {
    let face = ttf_parser::Face::parse(data, 0).ok()?;
    let units = f64::from(face.units_per_em());
    let mut builder = OutlineBuilder::new(units);
    face.outline_glyph(glyph_id, &mut builder)?;
    let advance = f64::from(face.glyph_hor_advance(glyph_id).unwrap_or(0)) / units;
    Some(Glyph {
        contours: builder.contours,
        advance,
    })
}

const BUILTIN_ADVANCE: f64 = 6.0 / 9.0;
const BUILTIN_CELL: f64 = 1.0 / 9.0;
const BUILTIN_WIDTH: u8 = 5;
const BUILTIN_HEIGHT: u8 = 7;

/// A 5×7 bitmap alphabet. Lowercase letters use the uppercase shapes; anything
/// without a pattern is drawn as a notdef box.
fn builtin_glyph(character: char) -> Option<Glyph> {
    let code = u8::try_from(u32::from(character)).ok()?;
    if character == ' ' {
        return Some(Glyph {
            contours: Vec::new(),
            advance: BUILTIN_ADVANCE,
        });
    }
    if let Some(rows) = glyph_rows(code) {
        return Some(Glyph {
            contours: rows_to_contours(&rows),
            advance: BUILTIN_ADVANCE,
        });
    }
    if character == '\u{2022}' {
        return Some(Glyph {
            contours: rows_to_contours(&[0, 0, 0b00100, 0b01110, 0b00100, 0, 0]),
            advance: BUILTIN_ADVANCE,
        });
    }
    // Latin-1 text is the common case for base-14 fonts, so an accented letter
    // is drawn as its unaccented base plus the mark rather than as .notdef.
    if let Some((base, accent)) = decompose(character) {
        let rows = glyph_rows(u8::try_from(u32::from(base)).unwrap_or(b'?'))
            .or_else(|| upper(base).and_then(|upper| glyph_rows(upper as u8)))
            .unwrap_or_else(notdef_rows);
        let mut contours = rows_to_contours(&rows);
        contours.extend(
            accent
                .cells()
                .iter()
                .map(|&(column, layer)| mark_cell(column, layer)),
        );
        return Some(Glyph {
            contours,
            advance: BUILTIN_ADVANCE,
        });
    }
    if character == '\u{00F8}' || character == '\u{00D8}' {
        // `ø` is `o` with a slash through it, which no mark above the box can
        // show, so the slash goes across the glyph body instead.
        let mut contours = rows_to_contours(&glyph_rows(b'O').unwrap_or_else(notdef_rows));
        contours.extend(
            [(0, 5), (1, 4), (2, 3), (3, 2), (4, 1)]
                .iter()
                .map(|&(column, layer)| mark_cell(column, layer)),
        );
        return Some(Glyph {
            contours,
            advance: BUILTIN_ADVANCE,
        });
    }
    let rows = upper(character)
        .and_then(|upper| glyph_rows(upper as u8))
        .unwrap_or_else(notdef_rows);
    Some(Glyph {
        contours: rows_to_contours(&rows),
        advance: BUILTIN_ADVANCE,
    })
}

/// A mark that can sit on a 5×7 base letter: above the glyph box (layers 0 and
/// up) or below the baseline (negative layers, used by the cedilla).
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
enum Accent {
    Acute,
    Grave,
    Circumflex,
    Diaeresis,
    Tilde,
    Ring,
    Cedilla,
}

impl Accent {
    /// Cell coordinates: `column` across the five-column box, `layer` up from
    /// the top of the glyph (layer 0 starts at 7/9 em) or, when negative, down
    /// from the baseline.
    fn cells(self) -> &'static [(u8, i8)] {
        match self {
            Self::Acute => &[(2, 0), (3, 0), (3, 1)],
            Self::Grave => &[(2, 1), (3, 1), (3, 0)],
            Self::Circumflex => &[(1, 0), (2, 1), (3, 0)],
            Self::Diaeresis => &[(1, 1), (3, 1)],
            Self::Tilde => &[(1, 1), (2, 0), (3, 1)],
            Self::Ring => &[(1, 0), (2, 1), (3, 0), (2, 0)],
            Self::Cedilla => &[(2, -1), (3, -1), (3, -2)],
        }
    }
}

/// One cell of a mark, as a rectangle in em units.
fn mark_cell(column: u8, layer: i8) -> Vec<Point> {
    let x0 = f64::from(column) * BUILTIN_CELL;
    let x1 = x0 + BUILTIN_CELL;
    let (bottom, top) = if layer < 0 {
        (
            f64::from(layer) * BUILTIN_CELL,
            f64::from(layer + 1) * BUILTIN_CELL,
        )
    } else {
        (
            f64::from(BUILTIN_HEIGHT + layer as u8) * BUILTIN_CELL,
            f64::from(BUILTIN_HEIGHT + layer as u8 + 1) * BUILTIN_CELL,
        )
    };
    vec![
        Point::new(x0, bottom),
        Point::new(x1, bottom),
        Point::new(x1, top),
        Point::new(x0, top),
    ]
}

/// The base letter and mark for a precomposed Latin-1 / Latin Extended-A
/// character the 5×7 alphabet does not carry itself.
fn decompose(character: char) -> Option<(char, Accent)> {
    let (base, accent) = match character {
        'á' | 'Á' => ('a', Accent::Acute),
        'é' | 'É' => ('e', Accent::Acute),
        'í' | 'Í' => ('i', Accent::Acute),
        'ó' | 'Ó' => ('o', Accent::Acute),
        'ú' | 'Ú' => ('u', Accent::Acute),
        'ý' | 'Ý' => ('y', Accent::Acute),
        'à' | 'À' => ('a', Accent::Grave),
        'è' | 'È' => ('e', Accent::Grave),
        'ì' | 'Ì' => ('i', Accent::Grave),
        'ò' | 'Ò' => ('o', Accent::Grave),
        'ù' | 'Ù' => ('u', Accent::Grave),
        'â' | 'Â' => ('a', Accent::Circumflex),
        'ê' | 'Ê' => ('e', Accent::Circumflex),
        'î' | 'Î' => ('i', Accent::Circumflex),
        'ô' | 'Ô' => ('o', Accent::Circumflex),
        'û' | 'Û' => ('u', Accent::Circumflex),
        'ä' | 'Ä' => ('a', Accent::Diaeresis),
        'ë' | 'Ë' => ('e', Accent::Diaeresis),
        'ï' | 'Ï' => ('i', Accent::Diaeresis),
        'ö' | 'Ö' => ('o', Accent::Diaeresis),
        'ü' | 'Ü' => ('u', Accent::Diaeresis),
        'ÿ' | 'Ÿ' => ('y', Accent::Diaeresis),
        'ã' | 'Ã' => ('a', Accent::Tilde),
        'õ' | 'Õ' => ('o', Accent::Tilde),
        'ñ' | 'Ñ' => ('n', Accent::Tilde),
        'å' | 'Å' => ('a', Accent::Ring),
        'ç' | 'Ç' => ('c', Accent::Cedilla),
        _ => return None,
    };
    Some((base, accent))
}

fn upper(character: char) -> Option<char> {
    character
        .to_uppercase()
        .next()
        .filter(|upper| *upper != character)
}

fn notdef_rows() -> [u8; BUILTIN_HEIGHT as usize] {
    [
        0b11111, 0b10001, 0b10001, 0b10001, 0b10001, 0b10001, 0b11111,
    ]
}

fn rows_to_contours(rows: &[u8; BUILTIN_HEIGHT as usize]) -> Vec<Vec<Point>> {
    let mut contours = Vec::new();
    for (row, bits) in rows.iter().enumerate() {
        for column in 0..BUILTIN_WIDTH {
            let mask = 1_u8 << (BUILTIN_WIDTH - 1 - column);
            if bits & mask == 0 {
                continue;
            }
            let x0 = f64::from(column) * BUILTIN_CELL;
            let x1 = x0 + BUILTIN_CELL;
            let top = f64::from(BUILTIN_HEIGHT - 1 - row as u8) * BUILTIN_CELL;
            let bottom = top + BUILTIN_CELL;
            contours.push(vec![
                Point::new(x0, bottom),
                Point::new(x1, bottom),
                Point::new(x1, top),
                Point::new(x0, top),
            ]);
        }
    }
    contours
}

fn glyph_rows(code: u8) -> Option<[u8; 7]> {
    let rows = match code {
        b'A' => [
            0b01110, 0b10001, 0b10001, 0b11111, 0b10001, 0b10001, 0b10001,
        ],
        b'B' => [
            0b11110, 0b10001, 0b10001, 0b11110, 0b10001, 0b10001, 0b11110,
        ],
        b'C' => [
            0b01110, 0b10001, 0b10000, 0b10000, 0b10000, 0b10001, 0b01110,
        ],
        b'D' => [
            0b11110, 0b10001, 0b10001, 0b10001, 0b10001, 0b10001, 0b11110,
        ],
        b'E' => [
            0b11111, 0b10000, 0b10000, 0b11110, 0b10000, 0b10000, 0b11111,
        ],
        b'F' => [
            0b11111, 0b10000, 0b10000, 0b11110, 0b10000, 0b10000, 0b10000,
        ],
        b'G' => [
            0b01110, 0b10001, 0b10000, 0b10111, 0b10001, 0b10001, 0b01110,
        ],
        b'H' => [
            0b10001, 0b10001, 0b10001, 0b11111, 0b10001, 0b10001, 0b10001,
        ],
        b'I' => [
            0b01110, 0b00100, 0b00100, 0b00100, 0b00100, 0b00100, 0b01110,
        ],
        b'J' => [
            0b00111, 0b00010, 0b00010, 0b00010, 0b00010, 0b10010, 0b01100,
        ],
        b'K' => [
            0b10001, 0b10010, 0b10100, 0b11000, 0b10100, 0b10010, 0b10001,
        ],
        b'L' => [
            0b10000, 0b10000, 0b10000, 0b10000, 0b10000, 0b10000, 0b11111,
        ],
        b'M' => [
            0b10001, 0b11011, 0b10101, 0b10101, 0b10001, 0b10001, 0b10001,
        ],
        b'N' => [
            0b10001, 0b11001, 0b10101, 0b10011, 0b10001, 0b10001, 0b10001,
        ],
        b'O' => [
            0b01110, 0b10001, 0b10001, 0b10001, 0b10001, 0b10001, 0b01110,
        ],
        b'P' => [
            0b11110, 0b10001, 0b10001, 0b11110, 0b10000, 0b10000, 0b10000,
        ],
        b'Q' => [
            0b01110, 0b10001, 0b10001, 0b10001, 0b10101, 0b10010, 0b01101,
        ],
        b'R' => [
            0b11110, 0b10001, 0b10001, 0b11110, 0b10100, 0b10010, 0b10001,
        ],
        b'S' => [
            0b01111, 0b10000, 0b10000, 0b01110, 0b00001, 0b00001, 0b11110,
        ],
        b'T' => [
            0b11111, 0b00100, 0b00100, 0b00100, 0b00100, 0b00100, 0b00100,
        ],
        b'U' => [
            0b10001, 0b10001, 0b10001, 0b10001, 0b10001, 0b10001, 0b01110,
        ],
        b'V' => [
            0b10001, 0b10001, 0b10001, 0b10001, 0b10001, 0b01010, 0b00100,
        ],
        b'W' => [
            0b10001, 0b10001, 0b10001, 0b10101, 0b10101, 0b11011, 0b10001,
        ],
        b'X' => [
            0b10001, 0b10001, 0b01010, 0b00100, 0b01010, 0b10001, 0b10001,
        ],
        b'Y' => [
            0b10001, 0b10001, 0b01010, 0b00100, 0b00100, 0b00100, 0b00100,
        ],
        b'Z' => [
            0b11111, 0b00001, 0b00010, 0b00100, 0b01000, 0b10000, 0b11111,
        ],
        b'0' => [
            0b01110, 0b10001, 0b10011, 0b10101, 0b11001, 0b10001, 0b01110,
        ],
        b'1' => [
            0b00100, 0b01100, 0b00100, 0b00100, 0b00100, 0b00100, 0b01110,
        ],
        b'2' => [
            0b01110, 0b10001, 0b00001, 0b00010, 0b00100, 0b01000, 0b11111,
        ],
        b'3' => [
            0b11111, 0b00010, 0b00100, 0b00010, 0b00001, 0b10001, 0b01110,
        ],
        b'4' => [
            0b00010, 0b00110, 0b01010, 0b10010, 0b11111, 0b00010, 0b00010,
        ],
        b'5' => [
            0b11111, 0b10000, 0b11110, 0b00001, 0b00001, 0b10001, 0b01110,
        ],
        b'6' => [
            0b00110, 0b01000, 0b10000, 0b11110, 0b10001, 0b10001, 0b01110,
        ],
        b'7' => [
            0b11111, 0b00001, 0b00010, 0b00100, 0b01000, 0b01000, 0b01000,
        ],
        b'8' => [
            0b01110, 0b10001, 0b10001, 0b01110, 0b10001, 0b10001, 0b01110,
        ],
        b'9' => [
            0b01110, 0b10001, 0b10001, 0b01111, 0b00001, 0b00010, 0b01100,
        ],
        b'.' => [0, 0, 0, 0, 0, 0b01100, 0b01100],
        b',' => [0, 0, 0, 0, 0b01100, 0b01100, 0b01000],
        b'!' => [
            0b00100, 0b00100, 0b00100, 0b00100, 0b00100, 0b00000, 0b00100,
        ],
        b'?' => [
            0b01110, 0b10001, 0b00001, 0b00010, 0b00100, 0b00000, 0b00100,
        ],
        b'-' => [0, 0, 0, 0b11111, 0, 0, 0],
        b':' => [0, 0b01100, 0b01100, 0, 0b01100, 0b01100, 0],
        b';' => [0, 0b01100, 0b01100, 0, 0b01100, 0b01100, 0b01000],
        b'\'' => [0b00100, 0b00100, 0b01000, 0, 0, 0, 0],
        b'"' => [0b01010, 0b01010, 0, 0, 0, 0, 0],
        b'(' => [
            0b00010, 0b00100, 0b01000, 0b01000, 0b01000, 0b00100, 0b00010,
        ],
        b')' => [
            0b01000, 0b00100, 0b00010, 0b00010, 0b00010, 0b00100, 0b01000,
        ],
        b'/' => [
            0b00001, 0b00010, 0b00010, 0b00100, 0b01000, 0b01000, 0b10000,
        ],
        b'+' => [0, 0b00100, 0b00100, 0b11111, 0b00100, 0b00100, 0],
        b'=' => [0, 0, 0b11111, 0, 0b11111, 0, 0],
        b'*' => [0, 0b10101, 0b01110, 0b11111, 0b01110, 0b10101, 0],
        b'#' => [
            0b01010, 0b01010, 0b11111, 0b01010, 0b11111, 0b01010, 0b01010,
        ],
        b'%' => [
            0b11001, 0b11010, 0b00010, 0b00100, 0b01000, 0b01011, 0b10011,
        ],
        b'&' => [
            0b01100, 0b10010, 0b10100, 0b01000, 0b10101, 0b10010, 0b01101,
        ],
        _ => return None,
    };
    Some(rows)
}

/// Collects `ttf-parser` outlines into em-space contours.
struct OutlineBuilder {
    units: f64,
    contours: Vec<Vec<Point>>,
    current: Vec<Point>,
}

impl OutlineBuilder {
    fn new(units: f64) -> Self {
        Self {
            units,
            contours: Vec::new(),
            current: Vec::new(),
        }
    }

    fn point(&self, x: f32, y: f32) -> Point {
        Point::new(f64::from(x) / self.units, f64::from(y) / self.units)
    }

    fn finish_contour(&mut self) {
        if self.current.len() > 1 {
            self.contours.push(std::mem::take(&mut self.current));
        }
    }
}

impl ttf_parser::OutlineBuilder for OutlineBuilder {
    fn move_to(&mut self, x: f32, y: f32) {
        self.finish_contour();
        self.current.push(self.point(x, y));
    }

    fn line_to(&mut self, x: f32, y: f32) {
        self.current.push(self.point(x, y));
    }

    fn quad_to(&mut self, x1: f32, y1: f32, x: f32, y: f32) {
        let Some(&start) = self.current.last() else {
            return;
        };
        let control = self.point(x1, y1);
        let end = self.point(x, y);
        for step in 1..=8 {
            let t = f64::from(step) / 8.0;
            let u = 1.0 - t;
            self.current.push(Point::new(
                u * u * start.x + 2.0 * u * t * control.x + t * t * end.x,
                u * u * start.y + 2.0 * u * t * control.y + t * t * end.y,
            ));
        }
    }

    fn curve_to(&mut self, x1: f32, y1: f32, x2: f32, y2: f32, x: f32, y: f32) {
        let Some(&start) = self.current.last() else {
            return;
        };
        let c1 = self.point(x1, y1);
        let c2 = self.point(x2, y2);
        let end = self.point(x, y);
        for step in 1..=12 {
            let t = f64::from(step) / 12.0;
            let u = 1.0 - t;
            self.current.push(Point::new(
                u * u * u * start.x
                    + 3.0 * u * u * t * c1.x
                    + 3.0 * u * t * t * c2.x
                    + t * t * t * end.x,
                u * u * u * start.y
                    + 3.0 * u * u * t * c1.y
                    + 3.0 * u * t * t * c2.y
                    + t * t * t * end.y,
            ));
        }
    }

    fn close(&mut self) {
        self.finish_contour();
    }
}

#[cfg(test)]
mod tests {
    use super::Font;
    use pdf2::Encoding;

    #[test]
    fn the_builtin_font_draws_letters_as_ink() {
        let font = Font::builtin(Encoding::standard());
        let glyph = font.glyph(b'H').expect("H");
        assert!(!glyph.contours.is_empty());
        assert!((glyph.advance - 0.6667).abs() < 1e-3);
        // Every contour is a rectangle inside the glyph box.
        for contour in &glyph.contours {
            assert_eq!(contour.len(), 4);
            for point in contour {
                assert!(point.x >= 0.0 && point.x <= 6.0 / 9.0 + 1e-9);
                assert!(point.y >= 0.0 && point.y <= 7.0 / 9.0 + 1e-9);
            }
        }
    }

    #[test]
    fn the_builtin_font_is_blank_for_space() {
        let font = Font::builtin(Encoding::standard());
        let glyph = font.glyph(b' ').expect("space");
        assert!(glyph.contours.is_empty());
        assert!(glyph.advance > 0.0);
    }

    #[test]
    fn glyph_ids_are_selected_directly_for_composite_fonts() {
        let font = Font::builtin(Encoding::standard());
        assert!(
            font.glyph_by_id(1).is_none(),
            "the bitmap alphabet has no glyph ids"
        );
    }

    #[test]
    fn a_broken_font_program_is_rejected() {
        assert!(Font::true_type(Encoding::standard(), vec![0, 1, 2, 3]).is_none());
        assert!(Font::true_type(Encoding::standard(), Vec::new()).is_none());
    }

    #[test]
    fn accented_latin_composes_instead_of_drawing_notdef() {
        let font = Font::builtin(Encoding::win_ansi());
        // code, the ASCII base the accent is composed onto, and the number of
        // mark cells the accent contributes.
        for (code, base, marks) in [
            (0xE9_u8, b'E', 3), // é, acute
            (0xF1, b'N', 3),    // ñ, tilde
            (0xFC, b'U', 2),    // ü, diaeresis
            (0xE7, b'C', 3),    // ç, cedilla
            (0xE5, b'A', 4),    // å, ring
            (0xE2, b'A', 3),    // â, circumflex
        ] {
            let glyph = font.glyph(code).expect("a composed glyph");
            let plain = font.glyph(base).expect("the base letter");
            assert_eq!(
                glyph.contours.len(),
                plain.contours.len() + marks,
                "{} on {}",
                code as char,
                base as char
            );
            assert_ne!(glyph.contours, plain.contours);
        }
        // Anything the table does not name still falls back to .notdef, and
        // that is a hollow box, not a panic.
        let other = font.glyph(0xA4).expect("undefined code");
        assert_eq!(other.contours.len(), 20, "the notdef box");
    }

    #[test]
    fn marks_sit_outside_the_glyph_box_and_cedillas_below_the_baseline() {
        let font = Font::builtin(Encoding::win_ansi());
        let acute = font.glyph(0xE9).expect("é");
        let top = acute
            .contours
            .iter()
            .flatten()
            .fold(f64::MIN, |max, point| max.max(point.y));
        assert!((7.0 / 9.0..=1.0).contains(&top), "accent tops out at {top}");
        let cedilla = font.glyph(0xE7).expect("ç");
        let bottom = cedilla
            .contours
            .iter()
            .flatten()
            .fold(f64::MAX, |min, point| min.min(point.y));
        assert!(bottom < 0.0, "the cedilla hangs to {bottom}");
    }

    #[test]
    fn unknown_characters_become_the_notdef_box() {
        let font = Font::builtin(Encoding::standard());
        // 0xA4 is undefined in StandardEncoding, so there is no glyph at all.
        assert!(font.glyph(0xA4).is_none());
        // A mapped but unpatterned character gets the notdef box, not a panic.
        let glyph = font.glyph(b'@').expect("@");
        assert_eq!(
            glyph.contours.len(),
            20,
            "the notdef box is a hollow 5×7 frame"
        );
    }
}