From e59e17d773de7ddfd4604cbd8df70b45baa38209 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Thu, 8 Apr 2021 08:46:10 -0700 Subject: [PATCH] fonts: remove last use of ttf_parser::fonts_in_collection improve some diagnostics around fonts on windows --- wezterm-font/src/db.rs | 9 +++++--- wezterm-font/src/lib.rs | 6 ++--- wezterm-font/src/locator/gdi.rs | 41 ++++++++++----------------------- 3 files changed, 21 insertions(+), 35 deletions(-) diff --git a/wezterm-font/src/db.rs b/wezterm-font/src/db.rs index 70b6d3913..17a6b67fb 100644 --- a/wezterm-font/src/db.rs +++ b/wezterm-font/src/db.rs @@ -30,7 +30,8 @@ impl Entry { } }; - let face = Face::from_slice(&data, index)?; + let face = Face::from_slice(&data, index) + .with_context(|| format!("ttf_parser parsing {:?}", self.handle))?; let mut coverage = RangeSet::new(); for table in face.character_mapping_subtables() { @@ -51,7 +52,7 @@ impl Entry { let mut coverage = self.coverage.lock().unwrap(); if coverage.is_none() { let t = std::time::Instant::now(); - coverage.replace(self.compute_coverage()?); + coverage.replace(self.compute_coverage().context("compute_coverage")?); let elapsed = t.elapsed(); metrics::histogram!("font.compute.codepoint.coverage", elapsed); log::debug!( @@ -173,7 +174,9 @@ impl FontDatabase { let mut matches = vec![]; for entry in self.by_full_name.values() { - let covered = entry.coverage_intersection(&wanted_range)?; + let covered = entry + .coverage_intersection(&wanted_range) + .with_context(|| format!("coverage_interaction for {:?}", entry.parsed))?; let len = covered.len(); if len > 0 { matches.push((len, entry.handle.clone())); diff --git a/wezterm-font/src/lib.rs b/wezterm-font/src/lib.rs index e66314a40..eab0ea55c 100644 --- a/wezterm-font/src/lib.rs +++ b/wezterm-font/src/lib.rs @@ -212,7 +212,7 @@ impl FontConfigInner { match font_dirs.locate_fallback_for_codepoints(&no_glyphs) { Ok(ref mut handles) => extra_handles.append(handles), Err(err) => log::error!( - "Error: {} while resolving fallback for {} from font_dirs", + "Error: {:#} while resolving fallback for {} from font_dirs", err, fallback_str.escape_unicode() ), @@ -221,7 +221,7 @@ impl FontConfigInner { match built_in.locate_fallback_for_codepoints(&no_glyphs) { Ok(ref mut handles) => extra_handles.append(handles), Err(err) => log::error!( - "Error: {} while resolving fallback for {} for built-in fonts", + "Error: {:#} while resolving fallback for {} for built-in fonts", err, fallback_str.escape_unicode() ), @@ -230,7 +230,7 @@ impl FontConfigInner { match locator.locate_fallback_for_codepoints(&no_glyphs) { Ok(ref mut handles) => extra_handles.append(handles), Err(err) => log::error!( - "Error: {} while resolving fallback for {} from font-locator", + "Error: {:#} while resolving fallback for {} from font-locator", err, fallback_str.escape_unicode() ), diff --git a/wezterm-font/src/locator/gdi.rs b/wezterm-font/src/locator/gdi.rs index 22ec80689..b8c3b62b8 100644 --- a/wezterm-font/src/locator/gdi.rs +++ b/wezterm-font/src/locator/gdi.rs @@ -1,12 +1,11 @@ #![cfg(windows)] use crate::locator::{FontDataHandle, FontLocator}; -use crate::parser::FontMatch; +use crate::parser::{parse_and_collect_font_info, FontMatch}; use config::FontAttributes; use dwrote::{FontDescriptor, FontStretch, FontStyle, FontWeight}; use std::borrow::Cow; use std::collections::HashSet; -use ttf_parser::fonts_in_collection; use winapi::shared::windef::HFONT; use winapi::um::dwrite::*; use winapi::um::wingdi::{ @@ -140,6 +139,7 @@ fn attributes_to_descriptor(font_attr: &FontAttributes) -> FontDescriptor { } fn handle_from_descriptor( + attr: &FontAttributes, collection: &dwrote::FontCollection, descriptor: &FontDescriptor, ) -> Option { @@ -148,32 +148,13 @@ fn handle_from_descriptor( for file in face.get_files() { if let Some(path) = file.get_font_file_path() { let family_name = font.family_name(); + + let mut font_info = vec![]; log::debug!("{} -> {}", family_name, path.display()); - if let Ok(data) = std::fs::read(&path) { - let size = fonts_in_collection(&data).unwrap_or(1); - - let mut handle = FontDataHandle::Memory { - data: Cow::Owned(data), - name: family_name.clone(), - index: 0, - variation: 0, - }; - - for index in 0..size { - if let FontDataHandle::Memory { index: idx, .. } = &mut handle { - *idx = index; - } - let parsed = crate::parser::ParsedFont::from_locator(&handle).ok()?; - let names = parsed.names(); - if names.full_name == family_name || names.family.as_ref() == Some(&family_name) - { - // Switch to an OnDisk handle so that we don't hold - // all of the fallback fonts in memory - return Some(FontDataHandle::OnDisk { - path, - index, - variation: 0, - }); + if parse_and_collect_font_info(&path, &mut font_info).is_ok() { + for (parsed, handle) in font_info { + if parsed.matches_attributes(attr) != FontMatch::NoMatch { + return Some(handle); } } } @@ -218,7 +199,7 @@ impl FontLocator for GdiFontLocator { } } - match handle_from_descriptor(&collection, &descriptor) { + match handle_from_descriptor(font_attr, &collection, &descriptor) { Some(handle) => { log::debug!("Got {:?} from dwrote", handle); if try_handle(font_attr, handle, &mut fonts, loaded) { @@ -326,7 +307,9 @@ impl FontLocator for GdiFontLocator { resolved.insert(attr.clone()); let descriptor = attributes_to_descriptor(&attr); - if let Some(handle) = handle_from_descriptor(&collection, &descriptor) { + if let Some(handle) = + handle_from_descriptor(&attr, &collection, &descriptor) + { handles.push(handle); } }