1
1
mirror of https://github.com/wez/wezterm.git synced 2024-12-24 22:01:47 +03:00

windows: properly fallback to JetBrains Mono

The default font locator on Windows is font-loader.  That will always
return *something* that matched, even if it didn't match what we asked
for very well.  This is problematic if for example we asked for
"JetBrains Mono" and that isn't installed.  font-loader will return
the data for "Consolas".  That's great if there is no other fallback
mechanism, but it prevented us from searching our built-in fonts.

This commit parsed the returned font and uses the same font
family matching that we use for our own font directory searching
to see if it matched well enough.

This commit also tidies up that matching routine so that it more
carefully matches sub-family characteristics such as bold, italic
and bold-italic.
This commit is contained in:
Wez Furlong 2020-10-17 12:54:29 -07:00
parent 370c3fbd4d
commit c32de40978
2 changed files with 15 additions and 6 deletions

View File

@ -45,8 +45,16 @@ impl FontLocator for FontLoaderFontLocator {
index: index as u32,
name: font_attr.family.clone(),
};
fonts.push(handle);
loaded.insert(font_attr.clone());
// The system may just decide to give us its fallback,
// eg: Consolas, so we need to parse the returned font
// here to see if we got what we asked for.
if let Ok(parsed) = crate::font::parser::ParsedFont::from_locator(&handle) {
if crate::font::parser::font_info_matches(font_attr, parsed.names()) {
fonts.push(handle);
loaded.insert(font_attr.clone());
}
}
}
}
Ok(fonts)

View File

@ -465,7 +465,7 @@ fn collect_font_info(
Ok(())
}
fn font_info_matches(attr: &FontAttributes, names: &Names) -> bool {
pub fn font_info_matches(attr: &FontAttributes, names: &Names) -> bool {
if attr.family == names.full_name {
true
} else if let Some(fam) = names.family.as_ref() {
@ -473,9 +473,10 @@ fn font_info_matches(attr: &FontAttributes, names: &Names) -> bool {
// this is a pretty rough approximation
if attr.family == *fam {
match names.sub_family.as_ref().map(String::as_str) {
Some("Italic") if attr.italic => true,
Some("Bold") if attr.bold => true,
Some("Regular") | None => true,
Some("Italic") if attr.italic && !attr.bold => true,
Some("Bold") if attr.bold && !attr.italic => true,
Some("Bold Italic") if attr.bold && attr.italic => true,
Some("Regular") | None if !attr.italic && !attr.bold => true,
_ => false,
}
} else {