f_office_fonts/query.rs
1//! Finding a face by family and style.
2
3use crate::face::{Face, WEIGHT_BOLD, WEIGHT_REGULAR};
4use crate::{FaceInfo, FaceOrigin, FamilyName, FontDb, FontId};
5
6impl FontDb {
7 /// Finds a face of `family` that matches the style best: by name, as [`Self::query_with`]
8 /// finds it, and else by typographic family, as [`Self::query_typographic_with`] finds it.
9 ///
10 /// This never substitutes another family, because the rules for that belong to each
11 /// format. A format that has stand-ins for a missing family tries them between the two
12 /// steps, so that a face of another family in the same typographic family, such as Arial
13 /// Narrow for Arial, comes after them.
14 #[must_use]
15 pub fn query(&self, family: &FamilyName, bold: bool, italic: bool) -> Option<FontId> {
16 self.query_with(family, bold, italic, |_, _| true)
17 .or_else(|| self.query_typographic_with(family, bold, italic, |_, _| true))
18 }
19
20 /// Finds a face whose name is `family` and that matches the style best, among only the
21 /// faces that `accept` allows, given each face's ID and names. A target's font listing can
22 /// exclude a locally installed face before style selection, so that a local bold face
23 /// cannot hide a regular face that the target has, and a font choice can exclude a face
24 /// whose file did not load.
25 ///
26 /// A face belongs to `family` when one of its family names (name ID 1, in any language)
27 /// is `family`, as [`FamilyName`] compares names; that name links the up to four styles of
28 /// one family. Among those faces, one whose italic flag matches comes first. Then the face
29 /// whose weight is closest to the requested one comes first, which is 700 for bold and 400
30 /// otherwise. Of faces that are still equal, one that the caller added comes before a
31 /// built-in face ([`FaceOrigin`]), and then the first one added wins.
32 ///
33 /// When no face belongs to `family`, `family` may be the PostScript name of a face, compared
34 /// in the same way. The faces in the style group of that face (name ID 1) that have the
35 /// requested style then compete in the same way, where a face is bold from weight 600. When
36 /// none of them has the requested style, the face with the PostScript name is returned.
37 /// Else `family` may be the full name of a face (name ID 4), such as "Arial Bold", and that
38 /// face is returned whatever the requested style. Of several faces with the PostScript name
39 /// or the full name, the caller's comes first. Returns `None` when no kind of name matches;
40 /// a face whose typographic family (name ID 16) alone is `family` is not found here.
41 #[must_use]
42 pub fn query_with(
43 &self,
44 family: &FamilyName,
45 bold: bool,
46 italic: bool,
47 accept: impl Fn(FontId, &FaceInfo) -> bool,
48 ) -> Option<FontId> {
49 let members = self
50 .indexed()
51 .filter(|(id, face)| accept(*id, &face.info) && in_style_group(face, family));
52 best_style(members, (bold, italic), family)
53 .or_else(|| self.query_postscript(family, bold, italic, &accept))
54 .or_else(|| {
55 callers_first(self.indexed().filter(|(id, face)| {
56 accept(*id, &face.info)
57 && face.info.full_names.iter().any(|name| family.is(name))
58 }))
59 .map(|(id, _)| id)
60 })
61 }
62
63 /// Finds a face whose typographic family (name ID 16) is `family` and that matches the
64 /// style best, among only the faces that `accept` allows, as [`Self::query_with`] ranks
65 /// faces: the faces of other families that share a typographic family, such as Aptos
66 /// Display in the typographic family Aptos. A format tries it after [`Self::query_with`]
67 /// and after its stand-ins for a missing family.
68 #[must_use]
69 pub fn query_typographic_with(
70 &self,
71 family: &FamilyName,
72 bold: bool,
73 italic: bool,
74 accept: impl Fn(FontId, &FaceInfo) -> bool,
75 ) -> Option<FontId> {
76 let members = self
77 .indexed()
78 .filter(|(id, face)| accept(*id, &face.info) && in_family(&face.info, family));
79 best_style(members, (bold, italic), family)
80 }
81
82 fn query_postscript(
83 &self,
84 name: &FamilyName,
85 bold: bool,
86 italic: bool,
87 accept: &impl Fn(FontId, &FaceInfo) -> bool,
88 ) -> Option<FontId> {
89 let (id, named) = callers_first(self.indexed().filter(|(id, face)| {
90 let postscript = face.info.postscript_name.as_deref();
91 accept(*id, &face.info) && postscript.is_some_and(|postscript| name.is(postscript))
92 }))?;
93 let styled = self.indexed().filter(|(id, face)| {
94 accept(*id, &face.info)
95 && face.style_group == named.style_group
96 && has_style(&face.info, bold, italic)
97 });
98 Some(best_style(styled, (bold, italic), &named.style_group).unwrap_or(id))
99 }
100}
101
102/// Returns the face whose italic flag matches, then whose weight is closest, then whose
103/// family name (name ID 1) is `family`, and then that the caller added.
104fn best_style<'a>(
105 faces: impl Iterator<Item = (FontId, &'a Face)>,
106 (bold, italic): (bool, bool),
107 family: &FamilyName,
108) -> Option<FontId> {
109 let weight = if bold { WEIGHT_BOLD } else { WEIGHT_REGULAR };
110 faces
111 .min_by_key(|(_, face)| {
112 let info = &face.info;
113 (
114 info.italic != italic,
115 info.weight.abs_diff(weight),
116 face.style_group != *family,
117 face.origin != FaceOrigin::Caller,
118 )
119 })
120 .map(|(id, _)| id)
121}
122
123/// Returns the first face that the caller added, or else the first face.
124fn callers_first<'a>(
125 faces: impl Iterator<Item = (FontId, &'a Face)>,
126) -> Option<(FontId, &'a Face)> {
127 faces.min_by_key(|(_, face)| face.origin != FaceOrigin::Caller)
128}
129
130fn has_style(info: &FaceInfo, bold: bool, italic: bool) -> bool {
131 info.italic == italic && info.bold() == bold
132}
133
134fn in_style_group(face: &Face, family: &FamilyName) -> bool {
135 face.style_group_names.iter().any(|name| family.is(name))
136}
137
138fn in_family(info: &FaceInfo, family: &FamilyName) -> bool {
139 info.family_names.iter().any(|name| family.is(name))
140}