From 98f289f511c8f819b6c81dfee6dfa192ed794973 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Mon, 23 Nov 2020 16:59:30 -0800 Subject: [PATCH] wezterm-font: improve fallback font scaling Use the scaling factor between the font metrics for the base font and those of the fallback font selected for a given glyph. The scenario is this: the base font is typically the first one selected from the font configuration. There may be multiple fallback fonts that are different sizes; for instance, the Font Awesome font has glyphs that are square in aspect and are thus about twice the width of a typical textual monospace font. Similarly, Noto Color Emoji is another square font but that has a single set of bitmap strikes at a fixed 128 px square. The shaper returns advance metrics in the scale of the containing font, and the rasterizer will target the supplied size and dpi. We need to scale these to match the base metrics. Previously we used a crude heuristic to decide whether to scale, and that happened to work for Noto Color Emoji but not for Font Awesome, whose metrics were just inside the bounds of the heuristic. This commit allows retrieving the metrics for a given font_idx so that we can compute the correct scale factor without any heuristics, and applies that to the rasterized glyph. refs: https://github.com/wez/wezterm/issues/342 --- termwiz/src/cell.rs | 5 +++ wezterm-font/src/lib.rs | 5 +++ wezterm-font/src/shaper/allsorts.rs | 11 ++++++ wezterm-font/src/shaper/harfbuzz.rs | 52 ++++++++++++++++------------- wezterm-font/src/shaper/mod.rs | 5 ++- wezterm-gui/src/gui/glyphcache.rs | 33 +++++++++--------- 6 files changed, 69 insertions(+), 42 deletions(-) diff --git a/termwiz/src/cell.rs b/termwiz/src/cell.rs index d7ae73b5c..c8208161b 100644 --- a/termwiz/src/cell.rs +++ b/termwiz/src/cell.rs @@ -652,5 +652,10 @@ mod test { eprintln!("xi emoji_mod_base: {}", c.is_emoji_modifier_base()); } assert_eq!(unicode_column_width(deaf_man), 2); + + // This is a codepoint in the private use area + let font_awesome_star = "\u{f005}"; + eprintln!("font_awesome_star {}", font_awesome_star.escape_debug()); + assert_eq!(unicode_column_width(font_awesome_star), 1); } } diff --git a/wezterm-font/src/lib.rs b/wezterm-font/src/lib.rs index 89c78aa58..78d455abc 100644 --- a/wezterm-font/src/lib.rs +++ b/wezterm-font/src/lib.rs @@ -41,6 +41,11 @@ impl LoadedFont { self.shaper.shape(text, self.font_size, self.dpi) } + pub fn metrics_for_idx(&self, font_idx: usize) -> anyhow::Result { + self.shaper + .metrics_for_idx(font_idx, self.font_size, self.dpi) + } + pub fn rasterize_glyph( &self, glyph_pos: u32, diff --git a/wezterm-font/src/shaper/allsorts.rs b/wezterm-font/src/shaper/allsorts.rs index cae60ba58..1a3b6a4c5 100644 --- a/wezterm-font/src/shaper/allsorts.rs +++ b/wezterm-font/src/shaper/allsorts.rs @@ -119,6 +119,17 @@ impl FontShaper for AllsortsShaper { Ok(results) } + fn metrics_for_idx(&self, font_idx: usize, size: f64, dpi: u32) -> anyhow::Result { + let font = self + .fonts + .get(font_idx) + .ok_or_else(|| anyhow::anyhow!("invalid font_idx {}", font_idx))?; + let font = font + .as_ref() + .ok_or_else(|| anyhow::anyhow!("failed to load font_idx {}", font_idx))?; + Ok(font.get_metrics(size, dpi)) + } + fn metrics(&self, size: f64, dpi: u32) -> anyhow::Result { for font in &self.fonts { if let Some(font) = font { diff --git a/wezterm-font/src/shaper/harfbuzz.rs b/wezterm-font/src/shaper/harfbuzz.rs index 2af10918c..48d176d50 100644 --- a/wezterm-font/src/shaper/harfbuzz.rs +++ b/wezterm-font/src/shaper/harfbuzz.rs @@ -334,6 +334,33 @@ impl FontShaper for HarfbuzzShaper { result } + fn metrics_for_idx(&self, font_idx: usize, size: f64, dpi: u32) -> anyhow::Result { + let mut pair = self + .load_fallback(font_idx)? + .ok_or_else(|| anyhow!("unable to load font idx {}!?", font_idx))?; + let (cell_width, cell_height) = pair.face.set_font_size(size, dpi)?; + let y_scale = unsafe { (*(*pair.face.face).size).metrics.y_scale as f64 / 65536.0 }; + let metrics = FontMetrics { + cell_height: PixelLength::new(cell_height), + cell_width: PixelLength::new(cell_width), + // Note: face.face.descender is useless, we have to go through + // face.face.size.metrics to get to the real descender! + descender: PixelLength::new( + unsafe { (*(*pair.face.face).size).metrics.descender as f64 } / 64.0, + ), + underline_thickness: PixelLength::new( + unsafe { (*pair.face.face).underline_thickness as f64 } * y_scale / 64., + ), + underline_position: PixelLength::new( + unsafe { (*pair.face.face).underline_position as f64 } * y_scale / 64., + ), + }; + + log::trace!("metrics: {:?}", metrics); + + Ok(metrics) + } + fn metrics(&self, size: f64, dpi: u32) -> anyhow::Result { // Returns the metrics for the selected font... but look out // for implausible sizes. @@ -366,29 +393,6 @@ impl FontShaper for HarfbuzzShaper { metrics_idx += 1; } - let mut pair = self - .load_fallback(metrics_idx)? - .ok_or_else(|| anyhow!("unable to load font idx {}!?", metrics_idx))?; - let (cell_width, cell_height) = pair.face.set_font_size(size, dpi)?; - let y_scale = unsafe { (*(*pair.face.face).size).metrics.y_scale as f64 / 65536.0 }; - let metrics = FontMetrics { - cell_height: PixelLength::new(cell_height), - cell_width: PixelLength::new(cell_width), - // Note: face.face.descender is useless, we have to go through - // face.face.size.metrics to get to the real descender! - descender: PixelLength::new( - unsafe { (*(*pair.face.face).size).metrics.descender as f64 } / 64.0, - ), - underline_thickness: PixelLength::new( - unsafe { (*pair.face.face).underline_thickness as f64 } * y_scale / 64., - ), - underline_position: PixelLength::new( - unsafe { (*pair.face.face).underline_position as f64 } * y_scale / 64., - ), - }; - - log::trace!("metrics: {:?}", metrics); - - Ok(metrics) + self.metrics_for_idx(metrics_idx, size, dpi) } } diff --git a/wezterm-font/src/shaper/mod.rs b/wezterm-font/src/shaper/mod.rs index a5222c8b4..97b2567df 100644 --- a/wezterm-font/src/shaper/mod.rs +++ b/wezterm-font/src/shaper/mod.rs @@ -34,7 +34,7 @@ pub struct GlyphInfo { pub type FallbackIdx = usize; /// Describes the key font metrics that we use in rendering -#[derive(Copy, Clone, Debug)] +#[derive(Copy, Clone, Debug, PartialEq)] pub struct FontMetrics { /// Width of a character cell in pixels pub cell_width: PixelLength, @@ -59,6 +59,9 @@ pub trait FontShaper { /// Compute the font metrics for the preferred font /// at the specified size. fn metrics(&self, size: f64, dpi: u32) -> anyhow::Result; + + /// Compute the metrics for a given fallback font at the specified size + fn metrics_for_idx(&self, font_idx: usize, size: f64, dpi: u32) -> anyhow::Result; } pub use config::FontShaperSelection; diff --git a/wezterm-gui/src/gui/glyphcache.rs b/wezterm-gui/src/gui/glyphcache.rs index daffe098b..aa5891fac 100644 --- a/wezterm-gui/src/gui/glyphcache.rs +++ b/wezterm-gui/src/gui/glyphcache.rs @@ -174,33 +174,32 @@ impl GlyphCache { info: &GlyphInfo, style: &TextStyle, ) -> anyhow::Result>> { - let metrics; + let base_metrics; + let idx_metrics; let glyph; { let font = self.fonts.resolve_font(style)?; - metrics = font.metrics(); + base_metrics = font.metrics(); glyph = font.rasterize_glyph(info.glyph_pos, info.font_idx)?; + + idx_metrics = font.metrics_for_idx(info.font_idx)?; } - let (cell_width, cell_height) = (metrics.cell_width, metrics.cell_height); - let scale = if PixelLength::new(glyph.height as f64) > cell_height * 1.5 { - // This is another way to detect oversize glyph images. - // Compute an aspect-preserving scale factor that makes the glyph - // fit in the appropriate number of cells. - // The GlyphInfo tells us how many horizontal cells the glyph occupies. - let y_scale = cell_height.get() / glyph.height as f64; - let x_scale = (cell_width.get() * info.num_cells as f64) / glyph.width as f64; + let y_scale = base_metrics.cell_height.get() / idx_metrics.cell_height.get(); + let x_scale = base_metrics.cell_width.get() / idx_metrics.cell_width.get(); - if PixelLength::new(y_scale * glyph.width as f64) > cell_width { - // The y scale would overflow the width, so use the x-scale - x_scale - } else { - y_scale - } + let scale = if y_scale * glyph.width as f64 + > base_metrics.cell_width.get() * info.num_cells as f64 + { + // y-scaling would make us too wide, so use the x-scale + x_scale } else { - 1.0f64 + y_scale }; + + let (cell_width, cell_height) = (base_metrics.cell_width, base_metrics.cell_height); + let glyph = if glyph.width == 0 || glyph.height == 0 { // a whitespace glyph CachedGlyph {