ferri-pdf 0.1.12

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
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
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
//! Font data ownership and CSS face matching, independent of shaping and PDF output.
mod baselines;
mod bundled;
mod feature_values;
#[cfg(file_system)]
mod installed;
mod loading;
mod matching;
mod metrics;
mod palettes;
mod selection;
mod sfnt;
mod silhouette;
mod woff;
mod woff2;

pub(crate) use baselines::BaselineTable;
pub(crate) use feature_values::FeatureValueType;
pub(crate) use loading::OPTICAL_SIZE_AXIS;
pub(crate) use metrics::{
    FALLBACK_IDEOGRAPH_ADVANCE_EM, FALLBACK_ZERO_ADVANCE_EM, FaceMetrics, InitialFont, SizeAdjust,
    SizeMetric, WATER_IDEOGRAPH, ZERO, advance, advance_in_axis, initial_font,
};
pub(crate) use palettes::{BasePalette, FontPalette, default_palette};
pub(crate) use selection::{
    FaceSettings, FontAdjustments, FontFeature, FontInstance, MetricOverrides, UnicodeRanges,
};
pub use selection::{FontFaceStyle, FontRange, FontSelection, FontSlant, FontVariation, FontWidth};
pub(crate) use silhouette::Silhouettes;

use serde::Serialize;
use std::collections::HashMap;
use std::ops::Range;
#[cfg(file_system)]
use std::path::PathBuf;
use std::sync::{Arc, Mutex, OnceLock, PoisonError};

/// The initial `font-family`, resolved to `serif`.
pub(crate) const DEFAULT_FAMILY: GenericFamily = GenericFamily::Serif;
/// The character whose `unicode-range` coverage makes a face the first available font.
const FIRST_AVAILABLE_CHARACTER: char = ' ';

#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Serialize)]
pub struct FontId(usize);
impl FontId {
    pub fn index(self) -> usize {
        self.0
    }
}

/// A font-name namespace within one book; independent of DOM and selector identities.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Hash)]
pub(crate) struct FontNamespace(usize);
impl FontNamespace {
    pub(crate) const ROOT: Self = Self(0);
}

#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum FontStyle {
    #[default]
    Normal,
    Italic,
    /// An italic slanted clockwise (CSS Fonts 4 ยง2.4).
    Left,
    /// An italic slanted counter-clockwise.
    Right,
    Oblique,
}

#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Hash, Serialize)]
pub struct FontSynthesis {
    pub bold: bool,
    pub oblique: bool,
    pub small_caps: bool,
    /// Superscript and subscript forms for `font-variant-position`.
    pub position: bool,
}
impl FontSynthesis {
    pub const ALL: Self = Self {
        bold: true,
        oblique: true,
        small_caps: true,
        position: true,
    };
}

#[derive(Clone, Copy, Debug, PartialEq, PartialOrd, Serialize)]
pub struct FontWeight(f32);
impl FontWeight {
    pub const MIN: Self = Self(1.0);
    pub const MAX: Self = Self(1000.0);
    pub const THIN: Self = Self(100.0);
    pub const NORMAL: Self = Self(400.0);
    pub const MEDIUM: Self = Self(500.0);
    pub const SEMIBOLD: Self = Self(600.0);
    pub const BOLD: Self = Self(700.0);
    pub const BLACK: Self = Self(900.0);
    const RELATIVE_NORMAL_LIMIT: f32 = 350.0;
    const RELATIVE_BOLD_LIMIT: f32 = 550.0;
    const RELATIVE_HEAVY_LIMIT: f32 = 750.0;
    pub fn new(value: f32) -> Result<Self, FontError> {
        if !(Self::MIN.0..=Self::MAX.0).contains(&value) {
            return Err(FontError::InvalidWeight(value));
        }
        Ok(Self(value))
    }
    pub const fn value(self) -> f32 {
        self.0
    }
    #[must_use]
    pub fn bolder(self) -> Self {
        if self >= Self::BLACK {
            return self;
        }
        if self.0 < Self::RELATIVE_NORMAL_LIMIT {
            Self::NORMAL
        } else if self.0 < Self::RELATIVE_BOLD_LIMIT {
            Self::BOLD
        } else {
            Self::BLACK
        }
    }
    #[must_use]
    pub fn lighter(self) -> Self {
        if self < Self::THIN {
            return self;
        }
        if self.0 < Self::RELATIVE_BOLD_LIMIT {
            Self::THIN
        } else if self.0 < Self::RELATIVE_HEAVY_LIMIT {
            Self::NORMAL
        } else {
            Self::BOLD
        }
    }
}

#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum GenericFamily {
    Math,
    SansSerif,
    Serif,
    Monospace,
}
impl GenericFamily {
    /// Arial-, Times New Roman- and Courier New-compatible faces first, then wider `DejaVu`
    /// coverage.
    fn bundled_names(self) -> &'static [&'static str] {
        match self {
            Self::SansSerif => &[bundled::LIBERATION_SANS, bundled::DEJAVU_SANS],
            Self::Serif => &[bundled::LIBERATION_SERIF, bundled::DEJAVU_SERIF],
            Self::Math => &[bundled::DEJAVU_SERIF],
            Self::Monospace => &[bundled::LIBERATION_MONO, bundled::DEJAVU_SANS_MONO],
        }
    }
    pub fn primary_name(self) -> &'static str {
        self.bundled_names()[0]
    }
}

/// Ordered installed-family preferences, and the font folders installed families come from.
/// Empty lists retain the bundled defaults.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct FontPreferences {
    pub sans_serif: Vec<String>,
    pub serif: Vec<String>,
    pub monospace: Vec<String>,
    /// Installed families tried after the complete CSS font-family list.
    pub fallback: Vec<String>,
    /// Whether the fonts in the operating system's font folders are installed families.
    #[cfg(file_system)]
    pub system_fonts: bool,
    /// Further folders, searched with their subfolders, whose fonts are installed families.
    #[cfg(file_system)]
    pub font_dirs: Vec<PathBuf>,
}
impl FontPreferences {
    /// Whether no family preference is set.
    pub fn is_empty(&self) -> bool {
        self.sans_serif.is_empty()
            && self.serif.is_empty()
            && self.monospace.is_empty()
            && self.fallback.is_empty()
    }
    fn generic(&self, family: GenericFamily) -> &[String] {
        match family {
            GenericFamily::Math => &[],
            GenericFamily::SansSerif => &self.sans_serif,
            GenericFamily::Serif => &self.serif,
            GenericFamily::Monospace => &self.monospace,
        }
    }
}

#[derive(Clone, Debug, Eq, PartialEq, Hash, Serialize)]
pub enum FontFamily {
    Named(String),
    Generic(GenericFamily),
}

#[derive(Clone, Debug, Serialize)]
pub struct FontDescriptor {
    pub family: String,
    pub weight: FontRange<FontWeight>,
    pub width: FontRange<FontWidth>,
    pub style: FontFaceStyle,
}

/// Availability of a face when looking for a glyph or a complete shaping cluster.
pub(crate) enum GlyphMatch<T> {
    Unavailable,
    Missing,
    Found(T),
}

/// Sorted source offsets, including the end, shared by a run's shaped glyphs.
pub(crate) fn cluster_boundaries(clusters: impl Iterator<Item = usize>, end: usize) -> Vec<usize> {
    let mut boundaries: Vec<_> = clusters.collect();
    boundaries.push(end);
    boundaries.sort_unstable();
    boundaries.dedup();
    boundaries
}

#[derive(Clone)]
enum FontData {
    Bundled(&'static [u8]),
    #[cfg(test)]
    Loaded(Arc<Vec<u8>>),
    Lazy {
        sources: Arc<FontSources>,
        data: Arc<OnceLock<Option<LoadedFont>>>,
    },
    #[cfg(file_system)]
    Installed(Arc<installed::InstalledSource>),
}

/// Where a face comes from, in the order in which faces of one family name hide later ones.
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
enum FaceOrigin {
    Author,
    Bundled,
    #[cfg(file_system)]
    Installed,
}

/// The data of the first `@font-face` source that loaded.
struct LoadedFont {
    data: Vec<u8>,
    /// The `url()` or installed file it came from; `None` for a bundled `local()` face.
    reference: Option<String>,
}

pub(crate) enum FontSource {
    Url(String),
    Local(String),
}

pub(crate) struct FontSources {
    pub sources: Vec<FontSource>,
    pub base: crate::resources::ResourceBase,
}

#[derive(Clone)]
pub(crate) struct FontFace {
    descriptor: FontDescriptor,
    settings: FaceSettings,
    data: FontData,
    namespace: FontNamespace,
}
impl FontFace {
    pub(crate) fn descriptor(&self) -> &FontDescriptor {
        &self.descriptor
    }
    pub(crate) fn settings(&self) -> &FaceSettings {
        &self.settings
    }
    pub(crate) fn data(&self) -> Option<&[u8]> {
        match &self.data {
            FontData::Bundled(bytes) => Some(bytes),
            #[cfg(test)]
            FontData::Loaded(bytes) => Some(bytes),
            FontData::Lazy { data, .. } => data
                .get()
                .and_then(Option::as_ref)
                .map(|loaded| loaded.data.as_slice()),
            #[cfg(file_system)]
            FontData::Installed(source) => source.data(),
        }
    }
    /// The index of the face in its data, which is a collection only for installed faces.
    #[cfg_attr(
        not(file_system),
        expect(clippy::unused_self, reason = "only installed faces are collections")
    )]
    pub(crate) fn index(&self) -> u32 {
        #[cfg(file_system)]
        if let FontData::Installed(source) = &self.data {
            return source.index;
        }
        0
    }
    fn origin(&self) -> FaceOrigin {
        match self.data {
            FontData::Bundled(_) => FaceOrigin::Bundled,
            #[cfg(file_system)]
            FontData::Installed(_) => FaceOrigin::Installed,
            #[cfg(test)]
            FontData::Loaded(_) => FaceOrigin::Author,
            FontData::Lazy { .. } => FaceOrigin::Author,
        }
    }
}

#[derive(Clone)]
pub(crate) struct FontBook {
    faces: Vec<FontFace>,
    /// The installed fonts, which `faces` holds at `installed_faces`, after the bundled ones.
    #[cfg(file_system)]
    installed: Option<Arc<installed::InstalledFonts>>,
    /// Empty where there is no file system to install fonts in.
    installed_faces: Range<usize>,
    namespaces: Vec<Option<FontNamespace>>,
    preferences: FontPreferences,
    feature_values: Vec<feature_values::FeatureValue>,
    palettes: Vec<palettes::PaletteValues>,
    /// The `@font-face` sources loading skipped, shared by the book's copies.
    failures: Arc<Mutex<Vec<FontFailure>>>,
    /// Whether each loaded face embeds at the variation values it was instanced at.
    instances: Arc<Mutex<HashMap<InstanceKey, bool>>>,
    /// Families that a used font list names, with no family before them usable, that no face
    /// has; not yet reported.
    missing_families: Arc<Mutex<Vec<String>>>,
    /// Configured font folders that cannot be read, not yet reported.
    #[cfg(file_system)]
    unreadable_directories: Arc<Mutex<Vec<(PathBuf, std::io::Error)>>>,
    /// What each font list matched, shared by the book's copies until one adds a face or a
    /// namespace.
    matched: Arc<Mutex<HashMap<FontList, MatchedList>>>,
}

/// A font list as matching takes it: its namespace, families and selection, and whether it
/// falls back to faces it does not name.
#[derive(Debug, Eq, PartialEq, Hash)]
struct FontList {
    namespace: FontNamespace,
    families: Vec<FontFamily>,
    selection: FontSelection,
    fallback: bool,
}

/// The candidates of a font list, and the families it names that no face has, which a list
/// with fallback reports.
#[derive(Clone)]
struct MatchedList {
    candidates: Arc<[FontId]>,
    missing: Vec<String>,
}

/// A face and the bits of its variation values.
type InstanceKey = (FontId, Vec<([u8; 4], u32)>);
impl Default for FontBook {
    fn default() -> Self {
        Self::with_preferences(FontPreferences::default())
    }
}
impl FontBook {
    pub(crate) fn with_preferences(preferences: FontPreferences) -> Self {
        #[cfg_attr(
            not(file_system),
            expect(unused_mut, reason = "only installed fonts follow the bundled ones")
        )]
        let mut faces = bundled::faces();
        let start = faces.len();
        #[cfg(file_system)]
        let installed = installed::installed(&preferences);
        #[cfg(file_system)]
        if let Some(installed) = &installed {
            faces.extend(installed.book_faces());
        }
        #[cfg(file_system)]
        let unreadable_directories = preferences
            .font_dirs
            .iter()
            .filter_map(|directory| {
                std::fs::read_dir(directory)
                    .err()
                    .map(|error| (directory.clone(), error))
            })
            .collect();
        Self {
            installed_faces: start..faces.len(),
            faces,
            #[cfg(file_system)]
            installed,
            namespaces: vec![None],
            preferences,
            feature_values: Vec::new(),
            palettes: Vec::new(),
            failures: Arc::default(),
            instances: Arc::default(),
            missing_families: Arc::default(),
            #[cfg(file_system)]
            unreadable_directories: Arc::new(Mutex::new(unreadable_directories)),
            matched: Arc::default(),
        }
    }
    /// The `@font-face` sources and installed files loading has skipped since the last call.
    pub(crate) fn take_failures(&self) -> Vec<FontFailure> {
        std::mem::take(&mut self.failures.lock().unwrap_or_else(PoisonError::into_inner))
    }
    /// The missing families font lists have named since the last call.
    pub(crate) fn take_missing_families(&self) -> Vec<String> {
        std::mem::take(
            &mut self
                .missing_families
                .lock()
                .unwrap_or_else(PoisonError::into_inner),
        )
    }
    /// The configured font folders that could not be read, since the last call.
    #[cfg(file_system)]
    pub(crate) fn take_unreadable_directories(&self) -> Vec<(PathBuf, std::io::Error)> {
        std::mem::take(
            &mut self
                .unreadable_directories
                .lock()
                .unwrap_or_else(PoisonError::into_inner),
        )
    }
    fn generic_names(&self, generic: GenericFamily) -> impl Iterator<Item = &str> {
        self.preferences
            .generic(generic)
            .iter()
            .map(String::as_str)
            .chain(generic.bundled_names().iter().copied())
    }
    /// The candidates that can be the first available font: those whose `unicode-range`
    /// includes U+0020 (CSS Fonts 4 ยง5).
    pub(crate) fn first_available_candidates<'c>(
        &'c self,
        candidates: &'c [FontId],
    ) -> impl Iterator<Item = FontId> + 'c {
        candidates.iter().copied().filter(|&id| {
            self.face(id)
                .is_none_or(|face| face.settings.covers(FIRST_AVAILABLE_CHARACTER))
        })
    }
    /// A family's first available face fixes its descriptor before glyph fallback proceeds.
    /// Faces whose `unicode-range` excludes part of `text` are skipped unloaded (ยง4.5).
    pub(crate) fn find_glyph<T, E: From<FontError>>(
        &self,
        candidates: &[FontId],
        text: &str,
        mut probe: impl FnMut(FontId) -> Result<GlyphMatch<T>, E>,
    ) -> Result<Option<(FontId, T)>, E> {
        let mut matched: Vec<&FontDescriptor> = Vec::new();
        for &id in candidates {
            let face = self.face(id).ok_or(FontError::UnknownFont(id))?;
            if !face.settings.covers_text(text) {
                continue;
            }
            let descriptor = face.descriptor();
            let previous = matched
                .iter()
                .find(|previous| matching::names_match(&previous.family, &descriptor.family));
            if previous.is_some_and(|previous| {
                previous.style != descriptor.style
                    || previous.weight != descriptor.weight
                    || previous.width != descriptor.width
            }) {
                continue;
            }
            match probe(id)? {
                GlyphMatch::Unavailable => {}
                GlyphMatch::Found(value) => return Ok(Some((id, value))),
                GlyphMatch::Missing if previous.is_none() => matched.push(descriptor),
                GlyphMatch::Missing => {}
            }
        }
        Ok(None)
    }
    pub(crate) fn child_namespace(&mut self, parent: FontNamespace) -> FontNamespace {
        assert!(
            parent.0 < self.namespaces.len(),
            "parent namespace belongs to this book"
        );
        let namespace = FontNamespace(self.namespaces.len());
        self.namespaces.push(Some(parent));
        self.matched = Arc::default();
        namespace
    }
    #[cfg(test)]
    pub(crate) fn register(
        &mut self,
        descriptor: FontDescriptor,
        data: Vec<u8>,
    ) -> Result<FontId, FontError> {
        validate(&data, 0)?;
        self.matched = Arc::default();
        let id = FontId(self.faces.len());
        self.faces.push(FontFace {
            descriptor,
            settings: FaceSettings::default(),
            data: FontData::Loaded(Arc::new(data)),
            namespace: FontNamespace::ROOT,
        });
        Ok(id)
    }
    #[cfg(test)]
    pub(crate) fn declare(&mut self, descriptor: FontDescriptor, sources: FontSources) -> FontId {
        self.declare_in_namespace(
            FontNamespace::ROOT,
            descriptor,
            FaceSettings::default(),
            sources,
        )
    }
    pub(crate) fn declare_in_namespace(
        &mut self,
        namespace: FontNamespace,
        descriptor: FontDescriptor,
        settings: FaceSettings,
        sources: FontSources,
    ) -> FontId {
        self.matched = Arc::default();
        let id = FontId(self.faces.len());
        self.faces.push(FontFace {
            descriptor,
            settings,
            namespace,
            data: FontData::Lazy {
                sources: Arc::new(sources),
                data: Arc::default(),
            },
        });
        id
    }
    pub(crate) fn face(&self, id: FontId) -> Option<&FontFace> {
        self.faces.get(id.0)
    }
    pub(crate) fn faces(&self) -> &[FontFace] {
        &self.faces
    }
    pub(crate) fn faces_with_ids(&self) -> impl Iterator<Item = (FontId, &FontFace)> {
        self.faces
            .iter()
            .enumerate()
            .map(|(index, face)| (FontId(index), face))
    }
    pub(crate) fn candidates(
        &self,
        families: &[FontFamily],
        selection: FontSelection,
    ) -> Arc<[FontId]> {
        self.candidates_in_namespace(FontNamespace::ROOT, families, selection)
    }
    pub(crate) fn candidates_in_namespace(
        &self,
        namespace: FontNamespace,
        families: &[FontFamily],
        selection: FontSelection,
    ) -> Arc<[FontId]> {
        self.matching_candidates(namespace, families, selection, true)
    }

    /// Private-use characters can match only explicitly named families.
    pub(crate) fn named_candidates_in_namespace(
        &self,
        namespace: FontNamespace,
        families: &[FontFamily],
        selection: FontSelection,
    ) -> Arc<[FontId]> {
        self.matching_candidates(namespace, families, selection, false)
    }

    /// The bundled face has a visible replacement symbol, independent of author overrides.
    pub(crate) fn missing_glyph_font(&self) -> Option<FontId> {
        self.faces.iter().enumerate().find_map(|(index, face)| {
            (matches!(face.data, FontData::Bundled(_))
                && face.descriptor.family == bundled::DEJAVU_SANS
                && face.descriptor.style == FontFaceStyle::Normal
                && face.descriptor.weight.bounds(FontWeight::NORMAL)
                    == (FontWeight::NORMAL, FontWeight::NORMAL))
                .then_some(FontId(index))
        })
    }

    /// The ascending IDs of the faces in `namespace` named `name`.
    fn named_faces(&self, namespace: FontNamespace, name: &str) -> Vec<FontId> {
        let named = |(index, face): (usize, &FontFace)| {
            (face.namespace == namespace && matching::names_match(&face.descriptor.family, name))
                .then_some(FontId(index))
        };
        let Range { start, end } = self.installed_faces;
        #[cfg(file_system)]
        let installed = self
            .installed
            .as_ref()
            .filter(|_| namespace == FontNamespace::ROOT)
            .map_or(&[][..], |installed| installed.family(name));
        #[cfg(not(file_system))]
        let installed: &[usize] = &[];
        self.faces
            .iter()
            .enumerate()
            .take(start)
            .filter_map(named)
            .chain(installed.iter().map(|position| FontId(start + position)))
            .chain(self.faces.iter().enumerate().skip(end).filter_map(named))
            .collect()
    }

    fn matching_candidates(
        &self,
        namespace: FontNamespace,
        families: &[FontFamily],
        selection: FontSelection,
        fallback: bool,
    ) -> Arc<[FontId]> {
        let list = FontList {
            namespace,
            families: families.to_vec(),
            selection,
            fallback,
        };
        let cached = self
            .matched
            .lock()
            .unwrap_or_else(PoisonError::into_inner)
            .get(&list)
            .cloned();
        let MatchedList {
            candidates,
            missing,
        } = cached.unwrap_or_else(|| {
            let matched = self.match_list(&list);
            self.matched
                .lock()
                .unwrap_or_else(PoisonError::into_inner)
                .insert(list, matched.clone());
            matched
        });
        if !missing.is_empty() {
            let mut reported = self
                .missing_families
                .lock()
                .unwrap_or_else(PoisonError::into_inner);
            for name in missing {
                if !reported
                    .iter()
                    .any(|known| matching::names_match(known, &name))
                {
                    reported.push(name);
                }
            }
        }
        candidates
    }

    fn match_list(&self, list: &FontList) -> MatchedList {
        crate::work::count(crate::work::Work::FontListMatch, 1);
        let FontList {
            namespace,
            ref families,
            selection,
            fallback,
        } = *list;
        let mut result = Vec::new();
        let mut append = |name: &str, namespace, installed_only| {
            let namespace = self.family_namespace(name, namespace);
            let named = self.named_faces(namespace, name);
            let mut candidates =
                matching::candidates(&self.faces, &named, selection, installed_only);
            if selection.style != FontStyle::Normal {
                candidates.extend(matching::candidates(
                    &self.faces,
                    &named,
                    FontSelection {
                        style: FontStyle::Normal,
                        slant: FontSlant::NORMAL,
                        ..selection
                    },
                    installed_only,
                ));
            }
            let found = !candidates.is_empty();
            for id in candidates {
                if !result.contains(&id) {
                    result.push(id);
                }
            }
            found
        };
        // A named family no face has is reported only while no earlier family matched.
        let mut matched = false;
        let mut missing = Vec::new();
        for family in families {
            match family {
                FontFamily::Named(name) => {
                    let found = append(name, namespace, false);
                    if !found && !matched {
                        missing.push(name);
                    }
                    matched |= found;
                }
                FontFamily::Generic(generic) if fallback => {
                    matched = true;
                    for name in self.generic_names(*generic) {
                        append(name, FontNamespace::ROOT, true);
                    }
                }
                FontFamily::Generic(_) => {}
            }
        }
        if !fallback {
            return MatchedList {
                candidates: result.into(),
                missing: Vec::new(),
            };
        }
        for name in &self.preferences.fallback {
            append(name, FontNamespace::ROOT, true);
        }
        // Glyphs absent from the whole list use the initial family, then any bundled face.
        for generic in [
            DEFAULT_FAMILY,
            GenericFamily::SansSerif,
            GenericFamily::Serif,
            GenericFamily::Monospace,
        ] {
            for name in self.generic_names(generic) {
                append(name, FontNamespace::ROOT, true);
            }
        }
        MatchedList {
            candidates: result.into(),
            missing: missing.into_iter().cloned().collect(),
        }
    }

    fn family_namespace(&self, name: &str, mut namespace: FontNamespace) -> FontNamespace {
        while namespace != FontNamespace::ROOT {
            if self.faces.iter().any(|face| {
                face.namespace == namespace
                    && !matches!(face.data, FontData::Bundled(_))
                    && matching::names_match(&face.descriptor.family, name)
            }) {
                return namespace;
            }
            namespace = self.namespaces[namespace.0].expect("child font namespace has a parent");
        }
        FontNamespace::ROOT
    }
}

/// Rejects face `index` of `data` if the renderer cannot both shape and embed it, as user
/// agents sanitize fonts before use (CSS Fonts 4 ยง4.3): a font that fails is invalid, so
/// loading moves on to the face's next source.
fn validate(data: &[u8], index: u32) -> Result<(), FontError> {
    let face = ttf_parser::Face::parse(data, index).map_err(FontError::InvalidFont)?;
    embed_all_glyphs(data, index, &face, &[]).map_err(FontError::Unembeddable)
}

/// Embeds a subset of every glyph of `face`, face `index` of `data`, at `variations`, as the
/// PDF writer embeds the glyphs it uses, so that success covers each subset the PDF needs.
fn embed_all_glyphs(
    data: &[u8],
    index: u32,
    face: &ttf_parser::Face<'_>,
    variations: &[FontVariation],
) -> Result<(), subsetter::Error> {
    let glyphs: Vec<u16> = (0..face.number_of_glyphs()).collect();
    let coordinates: Vec<_> = variations
        .iter()
        .map(|variation| (subsetter::Tag::new(&variation.tag), variation.value))
        .collect();
    subsetter::subset_with_variations(
        data,
        index,
        &coordinates,
        &subsetter::GlyphRemapper::new_from_glyphs_sorted(&glyphs),
    )
    .map(|_| ())
}

/// An `@font-face` source skipped because it could not be fetched or is not a usable font, or
/// an instance of its font that cannot be embedded.
#[derive(Debug, thiserror::Error)]
#[error("font {reference}: {error}")]
pub struct FontFailure {
    pub base: crate::resources::ResourceBase,
    pub reference: String,
    #[source]
    pub error: FontError,
}

#[derive(Debug, thiserror::Error)]
pub enum FontError {
    #[error("{0}")]
    Resource(#[from] crate::resources::ResourceError),
    #[error("font ID {0:?} is absent from the font book")]
    UnknownFont(FontId),
    #[error("font ID {0:?} has not been resolved in this job")]
    UnresolvedFont(FontId),
    #[error("invalid font: {0}")]
    InvalidFont(#[source] ttf_parser::FaceParsingError),
    #[error("{0}")]
    File(#[from] sfnt::FontFileError),
    #[error("font cannot be embedded: {0}")]
    Unembeddable(#[source] subsetter::Error),
    #[error(
        "font instance {variations:?} cannot be embedded, so its text uses the default instance: {error}"
    )]
    UnembeddableInstance {
        variations: Vec<FontVariation>,
        #[source]
        error: subsetter::Error,
    },
    #[error("font weight must be between 1 and 1000: {0}")]
    InvalidWeight(f32),
    #[error("font width must be a finite nonnegative percentage: {0}")]
    InvalidWidth(f32),
    #[error("font slant must be between -90 and 90 degrees: {0}")]
    InvalidSlant(f32),
    #[error("no font face in the family list is available")]
    NoAvailableFace,
}

#[cfg(test)]
mod tests;

/// Whether the face's substitution or positioning features include `tag`.
pub(crate) fn has_feature(face: &ttf_parser::Face<'_>, tag: [u8; 4]) -> bool {
    let tag = ttf_parser::Tag::from_bytes(&tag);
    [face.tables().gsub, face.tables().gpos]
        .into_iter()
        .flatten()
        .any(|table| table.features.find(tag).is_some())
}