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
// Copyright 2024 the Parley Authors
// SPDX-License-Identifier: Apache-2.0 OR MIT
//! Implementation of the CSS font matching algorithm.
use super::font::FontInfo;
use crate::{FontStyle, FontWeight, FontWidth};
use smallvec::SmallVec;
const DEFAULT_OBLIQUE_ANGLE: f32 = 14.0;
pub fn match_font(
set: &[FontInfo],
width: FontWidth,
style: FontStyle,
weight: FontWeight,
synthesize_style: bool,
) -> Option<usize> {
const OBLIQUE_THRESHOLD: f32 = DEFAULT_OBLIQUE_ANGLE;
match set.len() {
0 => return None,
1 => return Some(0),
_ => {}
}
#[derive(Copy, Clone)]
struct Candidate {
index: usize,
width: i32,
style: FontStyle,
weight: f32,
has_slnt: bool,
}
let mut set: SmallVec<[Candidate; 16]> = set
.iter()
.enumerate()
.map(|(i, font)| Candidate {
index: i,
width: (font.width().ratio() * 100.0) as i32,
style: font.style(),
weight: font.weight().value(),
has_slnt: font.has_slant_axis(),
})
.collect();
let width = (width.ratio() * 100.0) as i32;
let weight = weight.value();
// font-width is tried first:
let mut use_width = set[0].width;
if !set.iter().any(|f| f.width == width) {
// If the desired width value is less than or equal to 100%...
if width <= 100 {
// width values below the desired width value are checked in
// descending order...
if let Some(found) = set
.iter()
.filter(|f| f.width < width)
.max_by_key(|f| f.width)
{
use_width = found.width;
}
// followed by width values above the desired width value in
// ascending order until a match is found.
else if let Some(found) = set
.iter()
.filter(|f| f.width > width)
.min_by_key(|f| f.width)
{
use_width = found.width;
}
}
// Otherwise, ...
else {
// width values above the desired width value are checked in
// ascending order...
if let Some(found) = set
.iter()
.filter(|f| f.width > width)
.min_by_key(|f| f.width)
{
use_width = found.width;
}
// followed by width values below the desired width value in
// descending order until a match is found.
else if let Some(found) = set
.iter()
.filter(|f| f.width < width)
.max_by_key(|f| f.width)
{
use_width = found.width;
}
}
} else {
use_width = width;
}
set.retain(|f| f.width == use_width);
use core::cmp::Ordering::*;
let oblique_fonts = set.iter().filter_map(|f| oblique_style(f.style));
// font-style is tried next:
// NOTE: this code uses an oblique threshold of 14deg rather than
// the current value of 20deg in the spec.
// See: https://github.com/w3c/csswg-drafts/issues/2295
let mut use_style = style;
let mut _use_slnt = false;
if !set.iter().any(|f| f.style == use_style) {
// If the value of font-style is italic:
if style == FontStyle::Italic {
// oblique values greater than or equal to 14deg are checked in
// ascending order
if let Some(found) = oblique_fonts
.clone()
.filter(|(_, a)| *a >= OBLIQUE_THRESHOLD)
.min_by(|x, y| x.1.partial_cmp(&y.1).unwrap_or(Less))
{
use_style = found.0;
}
// followed by positive oblique values below 14deg in descending order
else if let Some(found) = oblique_fonts
.clone()
.filter(|(_, a)| *a > 0. && *a < OBLIQUE_THRESHOLD)
.max_by(|x, y| x.1.partial_cmp(&y.1).unwrap_or(Less))
{
use_style = found.0;
}
// If no match is found, oblique values less than or equal to 0deg
// are checked in descending order until a match is found.
else if let Some(found) = oblique_fonts
.clone()
.filter(|(_, a)| *a < 0.)
.max_by(|x, y| x.1.partial_cmp(&y.1).unwrap_or(Less))
{
use_style = found.0;
} else {
use_style = set[0].style;
}
}
// If the value of font-style is oblique...
else if let Some(angle) = oblique_angle(style) {
// and the requested angle is greater than or equal to 14deg
if angle >= OBLIQUE_THRESHOLD {
// oblique values greater than or equal to angle are checked in
// ascending order
if let Some(found) = oblique_fonts
.clone()
.filter(|(_, a)| *a >= angle)
.min_by(|x, y| x.1.partial_cmp(&y.1).unwrap_or(Less))
{
use_style = found.0;
}
// followed by positive oblique values below angle in descending order
else if let Some(found) = oblique_fonts
.clone()
.filter(|(_, a)| *a > 0. && *a < angle)
.max_by(|x, y| x.1.partial_cmp(&y.1).unwrap_or(Less))
{
use_style = found.0;
} else {
// If font-synthesis-style has the value auto, then for variable
// fonts with a slnt axis a match is created by setting the slnt
// value with the specified oblique value; otherwise, a fallback
// match is produced by geometric shearing to the specified
// oblique value.
if synthesize_style {
if set.iter().any(|f| f.has_slnt) {
_use_slnt = true;
} else {
use_style = if set.iter().any(|f| f.style == FontStyle::Normal) {
FontStyle::Normal
} else {
set[0].style
};
}
} else {
// Choose an italic style
if set.iter().any(|f| f.style == FontStyle::Italic) {
use_style = FontStyle::Italic;
}
// oblique values less than or equal to 0deg are checked in descending order
else if let Some(found) = oblique_fonts
.clone()
.filter(|(_, a)| *a <= 0.)
.max_by(|x, y| x.1.partial_cmp(&y.1).unwrap_or(Less))
{
use_style = found.0;
} else {
use_style = set[0].style;
}
}
}
}
// if the requested angle is greater than or equal to 0deg
// and less than 14deg
else if angle >= 0. {
// positive oblique values below angle in descending order
if let Some(found) = oblique_fonts
.clone()
.filter(|(_, a)| *a > 0. && *a < angle)
.max_by(|x, y| x.1.partial_cmp(&y.1).unwrap_or(Less))
{
use_style = found.0;
}
// followed by oblique values greater than or equal to angle in
// ascending order
else if let Some(found) = oblique_fonts
.clone()
.filter(|(_, a)| *a >= angle)
.min_by(|x, y| x.1.partial_cmp(&y.1).unwrap_or(Less))
{
use_style = found.0;
} else {
// If font-synthesis-style has the value auto, then for variable
// fonts with a slnt axis a match is created by setting the slnt
// value with the specified oblique value; otherwise, a fallback
// match is produced by geometric shearing to the specified
// oblique value.
if synthesize_style {
if set.iter().any(|f| f.has_slnt) {
_use_slnt = true;
} else {
use_style = if set.iter().any(|f| f.style == FontStyle::Normal) {
FontStyle::Normal
} else {
set[0].style
};
}
} else {
// Choose an italic style
if set.iter().any(|f| f.style == FontStyle::Italic) {
use_style = FontStyle::Italic;
}
// oblique values less than or equal to 0deg are checked in descending order
else if let Some(found) = oblique_fonts
.clone()
.filter(|(_, a)| *a <= 0.)
.max_by(|x, y| x.1.partial_cmp(&y.1).unwrap_or(Less))
{
use_style = found.0;
} else {
use_style = set[0].style;
}
}
}
}
// -14deg < angle < 0deg
else if angle > -OBLIQUE_THRESHOLD {
// negative oblique values above angle in ascending order
if let Some(found) = oblique_fonts
.clone()
.filter(|(_, a)| *a < 0. && *a > angle)
.min_by(|x, y| x.1.partial_cmp(&y.1).unwrap_or(Less))
{
use_style = found.0;
}
// followed by oblique values less than or equal to angle in
// descending order
else if let Some(found) = oblique_fonts
.clone()
.filter(|(_, a)| *a <= angle)
.max_by(|x, y| x.1.partial_cmp(&y.1).unwrap_or(Less))
{
use_style = found.0;
} else {
// If font-synthesis-style has the value auto, then for variable
// fonts with a slnt axis a match is created by setting the slnt
// value with the specified oblique value; otherwise, a fallback
// match is produced by geometric shearing to the specified
// oblique value.
if synthesize_style {
if set.iter().any(|f| f.has_slnt) {
_use_slnt = true;
} else {
use_style = if set.iter().any(|f| f.style == FontStyle::Normal) {
FontStyle::Normal
} else {
set[0].style
};
}
} else {
// Choose an italic style
if set.iter().any(|f| f.style == FontStyle::Italic) {
use_style = FontStyle::Italic;
}
// oblique values greater than or equal to 0deg are checked in ascending order
else if let Some(found) = oblique_fonts
.clone()
.filter(|(_, a)| *a >= 0.)
.min_by(|x, y| x.1.partial_cmp(&y.1).unwrap_or(Less))
{
use_style = found.0;
} else {
use_style = set[0].style;
}
}
}
}
// angle < -14 deg
else {
// oblique values less than or equal to angle are checked in
// descending order
if let Some(found) = oblique_fonts
.clone()
.filter(|(_, a)| *a <= angle)
.max_by(|x, y| x.1.partial_cmp(&y.1).unwrap_or(Less))
{
use_style = found.0;
}
// followed by negative oblique values above angle in ascending order
else if let Some(found) = oblique_fonts
.clone()
.filter(|(_, a)| *a < 0. && *a > angle)
.min_by(|x, y| x.1.partial_cmp(&y.1).unwrap_or(Less))
{
use_style = found.0;
} else {
// If font-synthesis-style has the value auto, then for variable
// fonts with a slnt axis a match is created by setting the slnt
// value with the specified oblique value; otherwise, a fallback
// match is produced by geometric shearing to the specified
// oblique value.
if synthesize_style {
if set.iter().any(|f| f.has_slnt) {
_use_slnt = true;
} else {
use_style = if set.iter().any(|f| f.style == FontStyle::Normal) {
FontStyle::Normal
} else {
set[0].style
};
}
} else {
// Choose an italic style
if set.iter().any(|f| f.style == FontStyle::Italic) {
use_style = FontStyle::Italic;
}
// oblique values greater than or equal to 0deg are checked in ascending order
else if let Some(found) = oblique_fonts
.clone()
.filter(|(_, a)| *a >= 0.)
.min_by(|x, y| x.1.partial_cmp(&y.1).unwrap_or(Less))
{
use_style = found.0;
} else {
use_style = set[0].style;
}
}
}
}
}
// If the value of font-style is normal...
else {
// oblique values greater than or equal to 0deg are checked in
// ascending order
if let Some(found) = oblique_fonts
.clone()
.filter(|(_, a)| *a >= 0.)
.min_by(|x, y| x.1.partial_cmp(&y.1).unwrap_or(Less))
{
use_style = found.0;
}
// followed by italic fonts
else if let Some(found) = set.iter().find(|f| f.style == FontStyle::Italic) {
use_style = found.style;
}
// followed by oblique values less than 0deg in descending order
else if let Some(found) = oblique_fonts
.clone()
.filter(|(_, a)| *a < 0.)
.max_by(|x, y| x.1.partial_cmp(&y.1).unwrap_or(Less))
{
use_style = found.0;
} else {
use_style = set[0].style;
}
}
}
set.retain(|f| f.style == use_style);
// font-weight is matched next:
if let Some(f) = set.iter().find(|f| f.weight == weight) {
return Some(f.index);
} else {
// If the desired weight is inclusively between 400 and 500...
if (400.0..=500.0).contains(&weight) {
// weights greater than or equal to the target weight are checked in ascending
// order until 500 is hit and checked
if let Some(found) = set
.iter()
.enumerate()
.filter(|f| f.1.weight >= weight && f.1.weight <= 500.0)
.min_by(|x, y| x.1.weight.partial_cmp(&y.1.weight).unwrap_or(Less))
{
return Some(found.1.index);
}
// followed by weights less than the target weight in descending order
if let Some(found) = set
.iter()
.enumerate()
.filter(|f| f.1.weight < weight)
.max_by(|x, y| x.1.weight.partial_cmp(&y.1.weight).unwrap_or(Less))
{
return Some(found.1.index);
}
// followed by weights greater than 500, until a match is found.
if let Some(found) = set
.iter()
.enumerate()
.filter(|f| f.1.weight > 500.0)
.min_by(|x, y| x.1.weight.partial_cmp(&y.1.weight).unwrap_or(Less))
{
return Some(found.1.index);
}
}
// If the desired weight is less than 400...
else if weight < 400.0 {
// weights less than or equal to the target weight are checked in descending
if let Some(found) = set
.iter()
.filter(|f| f.weight <= weight)
.max_by(|x, y| x.weight.partial_cmp(&y.weight).unwrap_or(Less))
{
return Some(found.index);
}
// followed by weights greater than the target weight in ascending order
if let Some(found) = set
.iter()
.filter(|f| f.weight > weight)
.min_by(|x, y| x.weight.partial_cmp(&y.weight).unwrap_or(Less))
{
return Some(found.index);
}
}
// If the desired weight is greater than 500...
else {
// weights greater than or equal to the target weight are checked in ascending
if let Some(found) = set
.iter()
.filter(|f| f.weight >= weight)
.min_by(|x, y| x.weight.partial_cmp(&y.weight).unwrap_or(Less))
{
return Some(found.index);
}
// followed by weights less than the target weight in descending order
if let Some(found) = set
.iter()
.filter(|f| f.weight < weight)
.max_by(|x, y| x.weight.partial_cmp(&y.weight).unwrap_or(Less))
{
return Some(found.index);
}
}
}
None
}
fn oblique_angle(style: FontStyle) -> Option<f32> {
match style {
FontStyle::Oblique(angle) => Some(angle.unwrap_or(DEFAULT_OBLIQUE_ANGLE)),
_ => None,
}
}
fn oblique_style(style: FontStyle) -> Option<(FontStyle, f32)> {
match style {
FontStyle::Oblique(angle) => Some((style, angle.unwrap_or(DEFAULT_OBLIQUE_ANGLE))),
_ => None,
}
}