ferri-pdf 0.1.9

HTML and CSS to PDF renderer for print, in pure Rust
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
use crate::fonts::{
    FontBook, FontDescriptor, FontFaceStyle, FontFamily, FontRange, FontSelection, FontSlant,
    FontStyle, FontWeight, FontWidth, GenericFamily,
};

const FONT: &[u8] = include_bytes!("../../../assets/fonts/DejaVuSans.ttf");
const VARIABLE_FONT: &[u8] = include_bytes!("../../../tests/fixtures/fonts/RobotoFlex.ttf");

#[test]
fn variable_faces_retain_the_original_data_for_shaping_and_embedding() {
    let mut book = FontBook::default();
    let registered = book.register(
        FontDescriptor {
            family: "Variable Fixture".into(),
            weight: FontRange::single(FontWeight::NORMAL),
            width: FontRange::single(FontWidth::NORMAL),
            style: FontFaceStyle::Normal,
        },
        VARIABLE_FONT.to_vec(),
    );
    assert!(
        registered.is_ok(),
        "a valid variable TrueType face must be usable: {registered:?}"
    );
    let id = registered.unwrap();
    assert!(
        book.face(id).unwrap().data() == Some(VARIABLE_FONT),
        "the original variable face must remain available to both shaper and PDF embedder"
    );
}

#[test]
fn relative_weight_extremes_preserve_values_beyond_the_named_weights() {
    let very_light = FontWeight::new(50.0).unwrap();
    let very_heavy = FontWeight::new(950.0).unwrap();
    assert_eq!(
        very_light.lighter(),
        very_light,
        "lighter below 100 must leave the inherited weight unchanged"
    );
    assert_eq!(
        very_heavy.bolder(),
        very_heavy,
        "bolder above 900 must leave the inherited weight unchanged"
    );
}

#[test]
fn matching_uses_css_search_order_and_last_matching_author_face() {
    let mut book = FontBook::default();
    let family = "Weighted";
    let mut ids = Vec::new();
    for weight in [300.0, 500.0, 600.0, 500.0] {
        ids.push(
            book.register(
                FontDescriptor {
                    family: family.into(),
                    weight: FontRange::single(FontWeight::new(weight).unwrap()),
                    width: FontRange::single(FontWidth::NORMAL),
                    style: FontFaceStyle::Normal,
                },
                FONT.to_vec(),
            )
            .unwrap(),
        );
    }
    let families = [FontFamily::Named(family.into())];
    for (requested, expected) in [
        (400.0, ids[3]),
        (450.0, ids[3]),
        (350.0, ids[0]),
        (550.0, ids[2]),
        (650.0, ids[2]),
    ] {
        assert_eq!(
            book.candidates(
                &families,
                FontSelection::new(FontWeight::new(requested).unwrap(), FontStyle::Normal)
            )[0],
            expected,
            "font-weight {requested} must search in the CSS-prescribed direction and prefer later equivalent faces"
        );
    }
}

fn family_order(book: &FontBook, families: &[FontFamily], style: FontStyle) -> Vec<String> {
    let mut order: Vec<String> = Vec::new();
    for &id in book
        .candidates(families, FontSelection::new(FontWeight::NORMAL, style))
        .iter()
    {
        let family = &book.face(id).unwrap().descriptor().family;
        if order.last() != Some(family) {
            order.push(family.clone());
        }
    }
    order
}

#[test]
fn fallback_tries_metric_compatible_generic_faces_before_wider_dejavu_coverage() {
    let mut book = FontBook::default();
    let author = "Author Face";
    book.register(
        FontDescriptor {
            family: author.into(),
            weight: FontRange::single(FontWeight::NORMAL),
            width: FontRange::single(FontWidth::NORMAL),
            style: FontFaceStyle::Normal,
        },
        FONT.to_vec(),
    )
    .unwrap();
    let named = FontFamily::Named(author.into());
    let sans = FontFamily::Generic(GenericFamily::SansSerif);
    let mono = FontFamily::Generic(GenericFamily::Monospace);
    for (families, style, expected) in [
        (
            vec![named.clone(), sans],
            FontStyle::Normal,
            [
                author,
                "Liberation Sans",
                "DejaVu Sans",
                "Liberation Serif",
                "DejaVu Serif",
                "Liberation Mono",
                "DejaVu Sans Mono",
            ],
        ),
        (
            vec![named.clone()],
            FontStyle::Normal,
            [
                author,
                "Liberation Serif",
                "DejaVu Serif",
                "Liberation Sans",
                "DejaVu Sans",
                "Liberation Mono",
                "DejaVu Sans Mono",
            ],
        ),
        (
            vec![mono, named],
            FontStyle::Italic,
            [
                "Liberation Mono",
                "DejaVu Sans Mono",
                author,
                "Liberation Serif",
                "DejaVu Serif",
                "Liberation Sans",
                "DejaVu Sans",
            ],
        ),
    ] {
        assert_eq!(
            family_order(&book, &families, style),
            expected,
            "{families:?} ({style:?}): a generic expands to its Arial/Times/Courier New-compatible face, then DejaVu; glyphs missing from the list use the initial serif family, then every other bundled family"
        );
    }
}

#[test]
fn overlapping_weight_ranges_follow_the_400_to_500_search_direction() {
    let mut book = FontBook::default();
    let families = [FontFamily::Named("Weight ranges".into())];
    let ids: Vec<_> = [(100.0, 430.0), (700.0, 480.0), (600.0, 900.0)]
        .into_iter()
        .map(|(start, end)| {
            book.register(
                FontDescriptor {
                    family: "Weight ranges".into(),
                    weight: FontRange::new(
                        FontWeight::new(start).unwrap(),
                        FontWeight::new(end).unwrap(),
                    ),
                    width: FontRange::single(FontWidth::NORMAL),
                    style: FontFaceStyle::Normal,
                },
                FONT.to_vec(),
            )
            .unwrap()
        })
        .collect();
    for (requested, expected) in [
        (400.0, ids[0]),
        (450.0, ids[1]),
        (550.0, ids[1]),
        (750.0, ids[2]),
        (50.0, ids[0]),
    ] {
        let actual = book.candidates(
            &families,
            FontSelection::new(FontWeight::new(requested).unwrap(), FontStyle::Normal),
        );
        assert_eq!(
            actual[0], expected,
            "weight {requested} must search the inclusive ranges in the prescribed direction, including reversed endpoints"
        );
    }
}

#[test]
fn signed_oblique_ranges_respect_the_eleven_degree_preference_boundary() {
    let mut book = FontBook::default();
    let families = [FontFamily::Named("Slant ranges".into())];
    let ids: Vec<_> = [5.0, 20.0, -5.0, -20.0]
        .into_iter()
        .map(|angle| {
            book.register(
                FontDescriptor {
                    family: "Slant ranges".into(),
                    weight: FontRange::single(FontWeight::NORMAL),
                    width: FontRange::single(FontWidth::NORMAL),
                    style: FontFaceStyle::Oblique(FontRange::single(
                        FontSlant::new(angle).unwrap(),
                    )),
                },
                FONT.to_vec(),
            )
            .unwrap()
        })
        .collect();
    for (angle, expected) in [
        (10.0, ids[0]),
        (14.0, ids[1]),
        (-10.0, ids[2]),
        (-14.0, ids[3]),
    ] {
        let selection = FontSelection {
            slant: FontSlant::new(angle).unwrap(),
            ..FontSelection::new(FontWeight::NORMAL, FontStyle::Oblique)
        };
        assert_eq!(
            book.candidates(&families, selection)[0],
            expected,
            "oblique {angle}deg must prefer the correct sign and search direction"
        );
    }
}

#[test]
fn subnormal_oblique_ranges_match_an_oblique_request() {
    const SUBNORMAL_ANGLE: f32 = 1e-40;
    let mut book = FontBook::default();
    let families = [FontFamily::Named("Subnormal slant".into())];
    let id = book
        .register(
            FontDescriptor {
                family: "Subnormal slant".into(),
                weight: FontRange::single(FontWeight::NORMAL),
                width: FontRange::single(FontWidth::NORMAL),
                style: FontFaceStyle::Oblique(FontRange::single(
                    FontSlant::new(SUBNORMAL_ANGLE).unwrap(),
                )),
            },
            FONT.to_vec(),
        )
        .unwrap();
    let selection = FontSelection {
        slant: FontSlant::new(10.0).unwrap(),
        ..FontSelection::new(FontWeight::NORMAL, FontStyle::Oblique)
    };
    assert_eq!(
        book.candidates(&families, selection).first(),
        Some(&id),
        "a face whose oblique angle is below the smallest normal float must stay a candidate"
    );
}

#[test]
fn caseless_family_aliases_do_not_switch_descriptors_after_missing_glyphs() {
    use crate::fonts::{FontError, GlyphMatch};
    const LIMITED: &[u8] = include_bytes!("../../../tests/fixtures/fonts/MetricGlyphTest.ttf");
    let mut book = FontBook::default();
    let descriptor = FontDescriptor {
        family: "StraรŸe".into(),
        weight: FontRange::single(FontWeight::NORMAL),
        width: FontRange::single(FontWidth::NORMAL),
        style: FontFaceStyle::Italic,
    };
    let selected = book.register(descriptor.clone(), LIMITED.to_vec()).unwrap();
    let alternate = book
        .register(
            FontDescriptor {
                family: "STRASSE".into(),
                style: FontFaceStyle::Normal,
                ..descriptor.clone()
            },
            FONT.to_vec(),
        )
        .unwrap();
    let fallback = book
        .register(
            FontDescriptor {
                family: "Other Family".into(),
                ..descriptor
            },
            FONT.to_vec(),
        )
        .unwrap();
    let mut probed = Vec::new();
    let found: Result<_, FontError> =
        book.find_glyph(&[selected, alternate, fallback], "H", |id| {
            probed.push(id);
            let face = ttf_parser::Face::parse(book.face(id).unwrap().data().unwrap(), 0).unwrap();
            Ok(if face.glyph_index('H').is_some() {
                GlyphMatch::Found(())
            } else {
                GlyphMatch::Missing
            })
        });
    assert_eq!(
        found.unwrap().map(|(id, ())| id),
        Some(fallback),
        "missing H must continue to the next family, not change style through a caseless alias"
    );
    assert_eq!(
        probed,
        [selected, fallback],
        "the alternate descriptor of the same folded family must never be probed"
    );
}

#[test]
fn generic_preferences_select_installed_faces_with_the_requested_style() {
    let book = FontBook::with_preferences(crate::fonts::FontPreferences {
        sans_serif: vec!["DejaVu Sans".into()],
        serif: vec!["DejaVu Serif".into()],
        monospace: vec!["DejaVu Sans Mono".into()],
        ..Default::default()
    });
    for (generic, name, data) in [
        (
            GenericFamily::SansSerif,
            "DejaVu Sans",
            include_bytes!("../../../assets/fonts/DejaVuSans-BoldOblique.ttf").as_slice(),
        ),
        (
            GenericFamily::Serif,
            "DejaVu Serif",
            include_bytes!("../../../assets/fonts/DejaVuSerif-BoldItalic.ttf").as_slice(),
        ),
        (
            GenericFamily::Monospace,
            "DejaVu Sans Mono",
            include_bytes!("../../../assets/fonts/DejaVuSansMono-BoldOblique.ttf").as_slice(),
        ),
    ] {
        let candidates = book.candidates(
            &[FontFamily::Generic(generic)],
            FontSelection::new(FontWeight::BOLD, FontStyle::Italic),
        );
        let face = book.face(candidates[0]).unwrap();
        assert_eq!(
            face.descriptor().family,
            name,
            "configured {generic:?} must select its own family"
        );
        assert_eq!(
            face.data(),
            Some(data),
            "{generic:?} must select the actual bold italic/oblique font"
        );
    }
}

#[test]
fn preferred_fallback_follows_the_css_family_list_and_deduplicates_faces() {
    let book = FontBook::with_preferences(crate::fonts::FontPreferences {
        sans_serif: vec![
            "Unavailable Family".into(),
            "DejaVu ลฟans".into(),
            "DejaVu Sans".into(),
        ],
        fallback: vec![
            "DejaVu Sans Mono".into(),
            "DejaVu Serif".into(),
            "DejaVu Sans Mono".into(),
        ],
        ..Default::default()
    });
    let named = [FontFamily::Named("Liberation Serif".into())];
    let order = family_order(&book, &named, FontStyle::Normal);
    assert_eq!(
        &order[..3],
        ["Liberation Serif", "DejaVu Sans Mono", "DejaVu Serif"],
        "the complete authored list precedes the installed fallback preference"
    );
    let families = [FontFamily::Generic(GenericFamily::SansSerif)];
    let candidates = book.candidates(&families, FontSelection::default());
    assert_eq!(
        book.face(candidates[0]).unwrap().descriptor().family,
        "DejaVu Sans"
    );
    for id in candidates.iter() {
        assert_eq!(
            candidates.iter().filter(|other| *other == id).count(),
            1,
            "aliases and repeated preferences must not repeat font probing"
        );
    }
}

#[test]
fn generic_preferences_cannot_turn_an_authored_face_into_installed_fallback() {
    let mut book = FontBook::with_preferences(crate::fonts::FontPreferences {
        sans_serif: vec!["DejaVu Sans".into()],
        fallback: vec!["Declared Only".into()],
        ..Default::default()
    });
    let descriptor = |family: &str| FontDescriptor {
        family: family.into(),
        weight: FontRange::single(FontWeight::NORMAL),
        width: FontRange::single(FontWidth::NORMAL),
        style: FontFaceStyle::Normal,
    };
    let sources = || crate::fonts::FontSources {
        sources: vec![crate::fonts::FontSource::Url("denied-font.ttf".into())],
        base: Default::default(),
    };
    let shadow = book.declare(descriptor("DejaVu Sans"), sources());
    let declared = book.declare(descriptor("Declared Only"), sources());
    let generic = [FontFamily::Generic(GenericFamily::SansSerif)];
    let candidates = book.candidates(&generic, FontSelection::default());
    assert!(
        !candidates.contains(&shadow),
        "generic preferences use installed fonts, not CSS aliases"
    );
    assert!(
        !candidates.contains(&declared),
        "installed fallback cannot expose an unrequested webfont"
    );
    let named = [FontFamily::Named("DejaVu Sans".into())];
    assert_eq!(
        book.candidates(&named, FontSelection::default())[0],
        shadow,
        "explicitly named authored families keep their ordinary precedence"
    );
    assert!(
        book.named_candidates_in_namespace(
            crate::fonts::FontNamespace::ROOT,
            &generic,
            FontSelection::default()
        )
        .is_empty(),
        "private-use matching must still require CSS named families"
    );
}

#[test]
fn cloned_books_share_lazy_results_and_original_bytes_without_acquiring_cached_faces() {
    use crate::fonts::{FontError, FontSource, FontSources};
    use crate::resources::{Resource, ResourceBase, ResourceError, ResourceResolver};
    use std::cell::Cell;
    struct Assets {
        calls: Cell<usize>,
    }
    impl ResourceResolver for Assets {
        fn resolve(&self, reference: &str, base: &ResourceBase) -> Result<Resource, ResourceError> {
            self.calls.set(self.calls.get() + 1);
            if reference == "missing.ttf" {
                return Err(ResourceError::UnsupportedScheme(reference.into()));
            }
            assert_eq!(reference, "face.ttf");
            Ok(Resource {
                bytes: FONT.to_vec(),
                base: base.clone(),
            })
        }
    }
    let mut book = FontBook::default();
    let mut declare = |reference: &str| {
        book.declare(
            FontDescriptor {
                family: reference.into(),
                weight: FontRange::single(FontWeight::NORMAL),
                width: FontRange::single(FontWidth::NORMAL),
                style: FontFaceStyle::Normal,
            },
            FontSources {
                sources: vec![FontSource::Url(reference.into())],
                base: Default::default(),
            },
        )
    };
    let face = declare("face.ttf");
    let missing = declare("missing.ttf");
    let clone = book.clone();
    let assets = Assets {
        calls: Cell::new(0),
    };
    assert!(matches!(clone.resolve_cached(face), Err(FontError::UnresolvedFont(id)) if id == face));
    assert_eq!(assets.calls.get(), 0);
    assert_eq!(clone.resolve(face, &assets).unwrap(), Some(FONT));
    assert_eq!(book.resolve(face, &assets).unwrap(), Some(FONT));
    assert_eq!(
        assets.calls.get(),
        1,
        "acquisition through a clone populates the original job cache"
    );
    assert_eq!(
        clone.resolve_cached(face).unwrap().unwrap().as_ptr(),
        book.resolve_cached(face).unwrap().unwrap().as_ptr(),
        "SVG and document consumers share the original allocation"
    );
    assert!(clone.resolve(missing, &assets).unwrap().is_none());
    assert!(book.resolve_cached(missing).unwrap().is_none());
    assert!(book.resolve(missing, &assets).unwrap().is_none());
    assert_eq!(assets.calls.get(), 2, "unavailable results are shared too");
    let selected = book
        .cached_instance(face, FontSelection::default())
        .unwrap()
        .unwrap();
    assert!(selected.face.glyph_index('H').is_some());
}

#[test]
fn bundled_italic_labels_follow_the_post_table_slant_direction() {
    for face in super::super::bundled::faces() {
        let crate::fonts::FontData::Bundled(data) = face.data else {
            unreachable!("bundled faces embed their data")
        };
        let angle = ttf_parser::Face::parse(data, 0).unwrap().italic_angle();
        // OpenType measures italicAngle counter-clockwise, CSS measures slant clockwise.
        let expected = match (face.descriptor.style, -angle) {
            (FontFaceStyle::Left, slant) => slant > 0.0,
            (FontFaceStyle::Right, slant) => slant < 0.0,
            (FontFaceStyle::Italic, slant) => slant == 0.0,
            _ => true,
        };
        assert!(
            expected,
            "{} {:?}: the label must give the italicAngle {angle} direction (CSS Fonts 4 ยง2.4)",
            face.descriptor.family, face.descriptor.weight
        );
    }
}