Skip to content

Commit 90ce68b

Browse files
committed
Clean up fontconfig generic family map population
Track duplicate families by ID instead of name. Also use filter_map to add all the generic families in one go. This also lets us use the question mark operator in the iterator body, leading to much cleaner code.
1 parent 28966d5 commit 90ce68b

File tree

1 file changed

+23
-28
lines changed

1 file changed

+23
-28
lines changed

fontique/src/backend/fontconfig.rs

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
use core::{
55
ffi::{CStr, c_char},
6-
iter::once,
76
marker::PhantomData,
87
ptr::NonNull,
98
};
@@ -488,7 +487,7 @@ impl SystemFonts {
488487

489488
// Populate the generic family map.
490489
let mut generic_families = GenericFamilyMap::default();
491-
for (generic_family, name) in GENERIC_FAMILY_NAMES {
490+
for (generic_family, name) in GENERIC_FAMILY_NAMES.iter().copied() {
492491
let mut pattern = Pattern::new().unwrap();
493492
pattern.add_string(FC_FAMILY, name);
494493
// TODO: do we need FcConfigSetDefaultSubstitute?
@@ -499,34 +498,30 @@ impl SystemFonts {
499498
// they provide no new Unicode coverage.
500499
let font_set = config.font_sort(&pattern, true).unwrap();
501500

502-
// There are a lot of duplicate font names in the substituted
501+
// There are a lot of duplicate font families in the substituted
503502
// pattern. Keep track of which ones have already been added to the
504503
// list.
505-
let mut added_names = HashSet::new();
506-
507-
for font in font_set.iter() {
508-
// Not sure if FcFontRenderPrepare performs any substitutions
509-
// relevant to fallback family name matching, but it's a good
510-
// idea to call it just in case.
511-
let Some(font) = config.font_render_prepare(&pattern, &font) else {
512-
continue;
513-
};
514-
// Generic families can have more than one name, but the only
515-
// one we care about is the first one.
516-
let Ok(name) = font.get_string(FC_FAMILY, 0) else {
517-
continue;
518-
};
519-
520-
if added_names.contains(name.as_ref()) {
521-
continue;
522-
}
523-
let Some(family_name) = name_map.get(name.as_ref()) else {
524-
continue;
525-
};
526-
527-
added_names.insert(name.into_owned());
528-
generic_families.append(*generic_family, once(family_name.id()));
529-
}
504+
let mut added_families = HashSet::new();
505+
506+
generic_families.append(
507+
generic_family,
508+
font_set.iter().filter_map(|font| {
509+
// Not sure if FcFontRenderPrepare performs any substitutions
510+
// relevant to fallback family name matching, but it's a good
511+
// idea to call it just in case.
512+
let font = config.font_render_prepare(&pattern, &font)?;
513+
// Generic families can have more than one name, but the only
514+
// one we care about is the first one.
515+
let name = font.get_string(FC_FAMILY, 0).ok()?;
516+
let family_name = name_map.get(name.as_ref())?;
517+
518+
if !added_families.insert(family_name.id()) {
519+
return None;
520+
}
521+
522+
Some(family_name.id())
523+
}),
524+
);
530525
}
531526

532527
Self {

0 commit comments

Comments
 (0)