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
//! Finding a face by family and style.
use crate::face::{Face, WEIGHT_BOLD, WEIGHT_REGULAR};
use crate::{FaceInfo, FaceOrigin, FamilyName, FontDb, FontId};
impl FontDb {
/// Finds a face of `family` that matches the style best: by name, as [`Self::query_with`]
/// finds it, and else by typographic family, as [`Self::query_typographic_with`] finds it.
///
/// This never substitutes another family, because the rules for that belong to each
/// format. A format that has stand-ins for a missing family tries them between the two
/// steps, so that a face of another family in the same typographic family, such as Arial
/// Narrow for Arial, comes after them.
#[must_use]
pub fn query(&self, family: &FamilyName, bold: bool, italic: bool) -> Option<FontId> {
self.query_with(family, bold, italic, |_, _| true)
.or_else(|| self.query_typographic_with(family, bold, italic, |_, _| true))
}
/// Finds a face whose name is `family` and that matches the style best, among only the
/// faces that `accept` allows, given each face's ID and names. A target's font listing can
/// exclude a locally installed face before style selection, so that a local bold face
/// cannot hide a regular face that the target has, and a font choice can exclude a face
/// whose file did not load.
///
/// A face belongs to `family` when one of its family names (name ID 1, in any language)
/// is `family`, as [`FamilyName`] compares names; that name links the up to four styles of
/// one family. Among those faces, one whose italic flag matches comes first. Then the face
/// whose weight is closest to the requested one comes first, which is 700 for bold and 400
/// otherwise. Of faces that are still equal, one that the caller added comes before a
/// built-in face ([`FaceOrigin`]), and then the first one added wins.
///
/// When no face belongs to `family`, `family` may be the PostScript name of a face, compared
/// in the same way. The faces in the style group of that face (name ID 1) that have the
/// requested style then compete in the same way, where a face is bold from weight 600. When
/// none of them has the requested style, the face with the PostScript name is returned.
/// Else `family` may be the full name of a face (name ID 4), such as "Arial Bold", and that
/// face is returned whatever the requested style. Of several faces with the PostScript name
/// or the full name, the caller's comes first. Returns `None` when no kind of name matches;
/// a face whose typographic family (name ID 16) alone is `family` is not found here.
#[must_use]
pub fn query_with(
&self,
family: &FamilyName,
bold: bool,
italic: bool,
accept: impl Fn(FontId, &FaceInfo) -> bool,
) -> Option<FontId> {
let members = self
.indexed()
.filter(|(id, face)| accept(*id, &face.info) && in_style_group(face, family));
best_style(members, (bold, italic), family)
.or_else(|| self.query_postscript(family, bold, italic, &accept))
.or_else(|| {
callers_first(self.indexed().filter(|(id, face)| {
accept(*id, &face.info)
&& face.info.full_names.iter().any(|name| family.is(name))
}))
.map(|(id, _)| id)
})
}
/// Finds a face whose typographic family (name ID 16) is `family` and that matches the
/// style best, among only the faces that `accept` allows, as [`Self::query_with`] ranks
/// faces: the faces of other families that share a typographic family, such as Aptos
/// Display in the typographic family Aptos. A format tries it after [`Self::query_with`]
/// and after its stand-ins for a missing family.
#[must_use]
pub fn query_typographic_with(
&self,
family: &FamilyName,
bold: bool,
italic: bool,
accept: impl Fn(FontId, &FaceInfo) -> bool,
) -> Option<FontId> {
let members = self
.indexed()
.filter(|(id, face)| accept(*id, &face.info) && in_family(&face.info, family));
best_style(members, (bold, italic), family)
}
fn query_postscript(
&self,
name: &FamilyName,
bold: bool,
italic: bool,
accept: &impl Fn(FontId, &FaceInfo) -> bool,
) -> Option<FontId> {
let (id, named) = callers_first(self.indexed().filter(|(id, face)| {
let postscript = face.info.postscript_name.as_deref();
accept(*id, &face.info) && postscript.is_some_and(|postscript| name.is(postscript))
}))?;
let styled = self.indexed().filter(|(id, face)| {
accept(*id, &face.info)
&& face.style_group == named.style_group
&& has_style(&face.info, bold, italic)
});
Some(best_style(styled, (bold, italic), &named.style_group).unwrap_or(id))
}
}
/// Returns the face whose italic flag matches, then whose weight is closest, then whose
/// family name (name ID 1) is `family`, and then that the caller added.
fn best_style<'a>(
faces: impl Iterator<Item = (FontId, &'a Face)>,
(bold, italic): (bool, bool),
family: &FamilyName,
) -> Option<FontId> {
let weight = if bold { WEIGHT_BOLD } else { WEIGHT_REGULAR };
faces
.min_by_key(|(_, face)| {
let info = &face.info;
(
info.italic != italic,
info.weight.abs_diff(weight),
face.style_group != *family,
face.origin != FaceOrigin::Caller,
)
})
.map(|(id, _)| id)
}
/// Returns the first face that the caller added, or else the first face.
fn callers_first<'a>(
faces: impl Iterator<Item = (FontId, &'a Face)>,
) -> Option<(FontId, &'a Face)> {
faces.min_by_key(|(_, face)| face.origin != FaceOrigin::Caller)
}
fn has_style(info: &FaceInfo, bold: bool, italic: bool) -> bool {
info.italic == italic && info.bold() == bold
}
fn in_style_group(face: &Face, family: &FamilyName) -> bool {
face.style_group_names.iter().any(|name| family.is(name))
}
fn in_family(info: &FaceInfo, family: &FamilyName) -> bool {
info.family_names.iter().any(|name| family.is(name))
}