From bc1d4bba355a38d080c7be31dc49e99d17821674 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sun, 11 Oct 2020 14:53:36 -0700 Subject: [PATCH] wezterm: avoid implausible font sizes in shaper metrics In this scenario: ```lua local wezterm = require "wezterm" return { font_size = 12.0, font_dirs = {"/home/wez/.fonts"}, font_locator = "ConfigDirsOnly", font = wezterm.font_with_fallback({ -- this is an invalid font "does not exist", "Noto Color Emoji", }), } ``` we'd take the metrics from the emoji font and the cell size would be crazy huge. With this change we filter out "outliers" based on the approximate pixel size for a font given its size and dpi. If a fallback position is significantly larger or smaller than the theoretical size then we skip it when considering the metrics. The result of this is that with the above configuration we end up with `{"Noto Color Emoji", "JetBrains Mono"}` as the font order; most of the textual glyphs will render from `JetBrains Mono` but the number glyphs prefer the `Noto Color Emoji` renditions which are typically double width. While the terminal looks a bit cartoonish as a result of that selection, the sizes are much more reasonable compared to the screenshot in #263. refs: #263 --- wezterm/src/font/shaper/harfbuzz.rs | 34 +++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/wezterm/src/font/shaper/harfbuzz.rs b/wezterm/src/font/shaper/harfbuzz.rs index 61f87fd5d..17461a346 100644 --- a/wezterm/src/font/shaper/harfbuzz.rs +++ b/wezterm/src/font/shaper/harfbuzz.rs @@ -267,9 +267,39 @@ impl FontShaper for HarfbuzzShaper { } fn metrics(&self, size: f64, dpi: u32) -> anyhow::Result { + // Returns the metrics for the selected font... but look out + // for implausible sizes. + // Ideally we wouldn't need this, but in the event that a user + // has a wonky configuration we don't want to pick something + // like a bitmap emoji font for the metrics or well end up + // with crazy huge cells. + // We do a sniff test based on the theoretical pixel height for + // the supplied size+dpi. + // If a given fallback slot deviates from the theoretical size + // by too much we'll skip to the next slot. + let theoretical_height = size * dpi as f64 / 72.0; + let mut metrics_idx = 0; + while let Ok(Some(mut pair)) = self.load_fallback(metrics_idx) { + let (_, cell_height) = pair.face.set_font_size(size, dpi)?; + let diff = (theoretical_height - cell_height).abs(); + let factor = diff / theoretical_height; + if factor < 2.0 { + break; + } + log::trace!( + "skip idx {} because diff={} factor={} theoretical_height={} cell_height={}", + metrics_idx, + diff, + factor, + theoretical_height, + cell_height + ); + metrics_idx += 1; + } + let mut pair = self - .load_fallback(0)? - .ok_or_else(|| anyhow!("unable to load first font!?"))?; + .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 {