From a2004a2a7d897d945f967e4ba8b2e9dc5ec86c41 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Mon, 28 Mar 2022 18:05:13 -0700 Subject: [PATCH] fonts: workaround broken symbol fonts with 0 advances The broot icon font has glyphs with horizontal advance set to 0. That would cause us to consider the glyph to be zero width, so handle that as a special case. Note that it is legit for certain cells to end up with a zero advance/width during shaping if they represent combining characters: this is more common in Arabic scripts. refs: https://github.com/wez/wezterm/issues/1787 --- wezterm-font/src/ftwrap.rs | 7 ++++++- wezterm-font/src/shaper/harfbuzz.rs | 13 ++++++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/wezterm-font/src/ftwrap.rs b/wezterm-font/src/ftwrap.rs index e656cfede..8eb10da7e 100644 --- a/wezterm-font/src/ftwrap.rs +++ b/wezterm-font/src/ftwrap.rs @@ -765,6 +765,7 @@ impl Face { / (f64::from(0x1_0000) * 64.0); let mut width = 0.0; + let mut num_examined = 0; for i in 32..128 { let glyph_pos = FT_Get_Char_Index(self.face, i); if glyph_pos == 0 { @@ -772,6 +773,7 @@ impl Face { } let res = FT_Load_Glyph(self.face, glyph_pos, FT_LOAD_COLOR as i32); if succeeded(res) { + num_examined += 1; let glyph = &(*(*self.face).glyph); if glyph.metrics.horiAdvance as f64 > width { width = glyph.metrics.horiAdvance as f64; @@ -784,6 +786,7 @@ impl Face { for glyph_pos in 1..8 { let res = FT_Load_Glyph(self.face, glyph_pos, FT_LOAD_COLOR as i32); if succeeded(res) { + num_examined += 1; let glyph = &(*(*self.face).glyph); if glyph.metrics.horiAdvance as f64 > width { width = glyph.metrics.horiAdvance as f64; @@ -792,7 +795,9 @@ impl Face { } if width == 0.0 { log::error!( - "Couldn't find any glyphs for metrics, so guessing width == height" + "Couldn't find usable advance metrics out of {} glyphs \ + sampled from the font, so guessing width == height", + num_examined, ); width = height * 64.; } diff --git a/wezterm-font/src/shaper/harfbuzz.rs b/wezterm-font/src/shaper/harfbuzz.rs index 5386d567d..91a261fc0 100644 --- a/wezterm-font/src/shaper/harfbuzz.rs +++ b/wezterm-font/src/shaper/harfbuzz.rs @@ -468,11 +468,18 @@ impl HarfbuzzShaper { for info in infos.iter() { // Proportional width based on relative pixel dimensions vs. other glyphs in // this same cluster - let weighted_cell_width = (cluster_info.cell_width as f64 * info.x_advance as f64 - / total_width) - .ceil() as u8; // Note that weighted_cell_width can legitimately compute as zero here // for the case where a combining mark composes over another glyph + // However, some symbol fonts have broken advance metrics and we don't + // want those glyphs to end up with zero width, so if this run is zero + // width then we round up to 1 cell. + // + let weighted_cell_width = if total_width == 0. { + 1 + } else { + (cluster_info.cell_width as f64 * info.x_advance as f64 / total_width).ceil() + as u8 + }; let weighted_cell_width = weighted_cell_width.min(remaining_cells); remaining_cells = remaining_cells.saturating_sub(weighted_cell_width);