1
1
mirror of https://github.com/wez/wezterm.git synced 2024-11-30 14:49:26 +03:00

fonts: refine matching on macos

Potentially allow matching more ttc and variable fonts on this
platform.
This commit is contained in:
Wez Furlong 2021-04-08 17:37:40 -07:00
parent 4c1de197bd
commit 6e6d6a7f06

View File

@ -1,7 +1,7 @@
#![cfg(target_os = "macos")] #![cfg(target_os = "macos")]
use crate::locator::{FontDataHandle, FontDataSource, FontLocator}; use crate::locator::{FontDataSource, FontLocator};
use crate::parser::{FontMatch, ParsedFont}; use crate::parser::ParsedFont;
use config::{FontAttributes, FontWeight, FontWidth}; use config::{FontAttributes, FontWeight, FontWidth};
use core_foundation::array::CFArray; use core_foundation::array::CFArray;
use core_foundation::base::TCFType; use core_foundation::base::TCFType;
@ -57,23 +57,25 @@ fn descriptor_from_attr(attr: &FontAttributes) -> anyhow::Result<CTFontDescripto
/// In addition, it may point to a ttc; so we'll need to reference /// In addition, it may point to a ttc; so we'll need to reference
/// each contained font to figure out which one is the one that /// each contained font to figure out which one is the one that
/// the descriptor is referencing. /// the descriptor is referencing.
fn handle_from_descriptor(descriptor: &CTFontDescriptor) -> Option<ParsedFont> { fn handles_from_descriptor(descriptor: &CTFontDescriptor) -> Vec<ParsedFont> {
let path = descriptor.font_path()?; let mut result = vec![];
let family_name = descriptor.family_name(); if let Some(path) = descriptor.font_path() {
let family_name = descriptor.family_name();
let mut font_info = vec![]; let mut font_info = vec![];
let source = FontDataSource::OnDisk(path); let source = FontDataSource::OnDisk(path);
crate::parser::parse_and_collect_font_info(&source, &mut font_info).ok()?; let _ = crate::parser::parse_and_collect_font_info(&source, &mut font_info);
for parsed in font_info { for parsed in font_info {
if parsed.names().full_name == family_name if parsed.names().full_name == family_name
|| parsed.names().family.as_ref() == Some(&family_name) || parsed.names().family.as_ref() == Some(&family_name)
{ {
return Some(parsed); result.push(parsed);
}
} }
} }
None result
} }
impl FontLocator for CoreTextFontLocator { impl FontLocator for CoreTextFontLocator {
@ -86,11 +88,11 @@ impl FontLocator for CoreTextFontLocator {
for attr in fonts_selection { for attr in fonts_selection {
if let Ok(descriptor) = descriptor_from_attr(attr) { if let Ok(descriptor) = descriptor_from_attr(attr) {
if let Some(parsed) = handle_from_descriptor(&descriptor) { let handles = handles_from_descriptor(&descriptor);
if parsed.matches_attributes(attr) != FontMatch::NoMatch { let ranked = ParsedFont::rank_matches(attr, handles);
fonts.push(parsed); for parsed in ranked {
loaded.insert(attr.clone()); fonts.push(parsed);
} loaded.insert(attr.clone());
} }
} }
} }
@ -125,9 +127,7 @@ fn build_fallback_list_impl() -> anyhow::Result<Vec<ParsedFont>> {
let cascade = cascade_list_for_languages(&font, &langs); let cascade = cascade_list_for_languages(&font, &langs);
let mut fonts = vec![]; let mut fonts = vec![];
for descriptor in &cascade { for descriptor in &cascade {
if let Some(handle) = handle_from_descriptor(&descriptor) { fonts.append(&mut handles_from_descriptor(&descriptor));
fonts.push(handle);
}
} }
// Some of the fallback fonts are special fonts that don't exist on // Some of the fallback fonts are special fonts that don't exist on
@ -143,9 +143,7 @@ fn build_fallback_list_impl() -> anyhow::Result<Vec<ParsedFont>> {
is_synthetic: true, is_synthetic: true,
}; };
if let Ok(descriptor) = descriptor_from_attr(&symbols) { if let Ok(descriptor) = descriptor_from_attr(&symbols) {
if let Some(handle) = handle_from_descriptor(&descriptor) { fonts.append(&mut handles_from_descriptor(&descriptor));
fonts.push(handle);
}
} }
Ok(fonts) Ok(fonts)