From 5f2c905db86353040c63ee9335cf69ca9fd5e99c Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Tue, 1 Feb 2022 09:15:50 -0700 Subject: [PATCH] shaping: feed presentation width from the terminal through to glyph info This allows unicode_version to be respected again when rendering. The updated emoji-presentation.sh script now highlights this slightly better by putting `.` characters after the emoji; unicode version 14 emoji presentation will show the `.` in the 3rd column, while earlier versions will show it in the 2nd column for glyphs that are sensitive to the version. refs: #1607 refs: #1563 --- test-data/emoji-presentation.sh | 18 ++++++------- wezterm-font/src/lib.rs | 8 +++++- wezterm-font/src/shaper/harfbuzz.rs | 35 +++++++++++++++++-------- wezterm-font/src/shaper/mod.rs | 27 +++++++++++++++++++ wezterm-gui/src/main.rs | 3 +++ wezterm-gui/src/shapecache.rs | 5 ++++ wezterm-gui/src/termwindow/box_model.rs | 1 + wezterm-gui/src/termwindow/render.rs | 5 ++++ window/src/os/wayland/frame.rs | 1 + 9 files changed, 82 insertions(+), 21 deletions(-) diff --git a/test-data/emoji-presentation.sh b/test-data/emoji-presentation.sh index f2eb3e475..a258e6d33 100755 --- a/test-data/emoji-presentation.sh +++ b/test-data/emoji-presentation.sh @@ -8,17 +8,17 @@ for version in 9 14 ; do echo "\n### Unicode Version $version\n" printf "\e]1337;UnicodeVersion=$version\e\\" - echo -e "\u270c Victory hand, text presentation by default" - echo -e "\u270c\ufe0e Victory hand, explicit text presentation" - echo -e "\u270c\ufe0f Victory hand, explicit emoji presentation" + echo -e "\u270c. Victory hand, text presentation by default" + echo -e "\u270c\ufe0e. Victory hand, explicit text presentation" + echo -e "\u270c\ufe0f. Victory hand, explicit emoji presentation" - echo -e "\u270a Raised fist, emoji presentation by default" - echo -e "\u270a\ufe0e Raised fist, explicit text presentation (invalid; no effect)" - echo -e "\u270a\ufe0f Raised fist, explicit emoji presentation" + echo -e "\u270a. Raised fist, emoji presentation by default" + echo -e "\u270a\ufe0e. Raised fist, explicit text presentation (invalid; no effect)" + echo -e "\u270a\ufe0f. Raised fist, explicit emoji presentation" - echo -e "\u2716 Multiply, text presentation by default" - echo -e "\u2716\ufe0e Multiply, explicit text presentation" - echo -e "\u2716\ufe0f Multiply, explicit emoji presentation" + echo -e "\u2716. Multiply, text presentation by default" + echo -e "\u2716\ufe0e. Multiply, explicit text presentation" + echo -e "\u2716\ufe0f. Multiply, explicit emoji presentation" echo -e "\U0001F468\u200D\U0001F467\u200D\U0001F466" echo "👨\n12" diff --git a/wezterm-font/src/lib.rs b/wezterm-font/src/lib.rs index 3ae2e1be9..e02d9068b 100644 --- a/wezterm-font/src/lib.rs +++ b/wezterm-font/src/lib.rs @@ -2,7 +2,7 @@ use crate::db::FontDatabase; use crate::locator::{new_locator, FontLocator}; use crate::parser::ParsedFont; use crate::rasterizer::{new_rasterizer, FontRasterizer}; -use crate::shaper::{new_shaper, FontShaper}; +use crate::shaper::{new_shaper, FontShaper, PresentationWidth}; use anyhow::{Context, Error}; use config::{ configuration, ConfigHandle, FontAttributes, FontRasterizerSelection, FontStretch, FontWeight, @@ -118,6 +118,7 @@ impl LoadedFont { presentation: Option, direction: Direction, range: Option>, + presentation_width: Option<&PresentationWidth>, ) -> anyhow::Result> { loop { let (tx, rx) = channel(); @@ -131,6 +132,7 @@ impl LoadedFont { presentation, direction, range.clone(), + presentation_width, ) { Ok(tuple) => tuple, Err(err) if err.downcast_ref::().is_some() => { @@ -156,6 +158,7 @@ impl LoadedFont { presentation: Option, direction: Direction, range: Option>, + presentation_width: Option<&PresentationWidth>, ) -> anyhow::Result> { let (_async_resolve, res) = self.shape_impl( text, @@ -164,6 +167,7 @@ impl LoadedFont { presentation, direction, range, + presentation_width, )?; Ok(res) } @@ -176,6 +180,7 @@ impl LoadedFont { presentation: Option, direction: Direction, range: Option>, + presentation_width: Option<&PresentationWidth>, ) -> anyhow::Result<(bool, Vec)> { let mut no_glyphs = vec![]; @@ -200,6 +205,7 @@ impl LoadedFont { presentation, direction, range, + presentation_width, ); no_glyphs.retain(|&c| c != '\u{FE0F}' && c != '\u{FE0E}'); diff --git a/wezterm-font/src/shaper/harfbuzz.rs b/wezterm-font/src/shaper/harfbuzz.rs index edba0f194..2c37a6a0e 100644 --- a/wezterm-font/src/shaper/harfbuzz.rs +++ b/wezterm-font/src/shaper/harfbuzz.rs @@ -1,5 +1,5 @@ use crate::parser::ParsedFont; -use crate::shaper::{FallbackIdx, FontMetrics, FontShaper, GlyphInfo}; +use crate::shaper::{FallbackIdx, FontMetrics, FontShaper, GlyphInfo, PresentationWidth}; use crate::units::*; use crate::{ftwrap, hbwrap as harfbuzz}; use anyhow::{anyhow, Context}; @@ -25,13 +25,8 @@ struct Info { y_offset: harfbuzz::hb_position_t, } -fn make_glyphinfo(text: &str, font_idx: usize, info: &Info) -> GlyphInfo { +fn make_glyphinfo(text: &str, num_cells: u8, font_idx: usize, info: &Info) -> GlyphInfo { let is_space = text == " "; - // TODO: this is problematic if the actual text in - // the terminal specified a different unicode version. - // We need to find a way to plumb that version through shaping - // so that it can be used here. - let num_cells = unicode_column_width(text, None) as u8; GlyphInfo { #[cfg(any(debug_assertions, test))] text: text.into(), @@ -189,6 +184,7 @@ impl HarfbuzzShaper { presentation: Option, direction: Direction, range: Range, + presentation_width: Option<&PresentationWidth>, ) -> anyhow::Result> { let mut buf = harfbuzz::Buffer::new()?; // We deliberately omit setting the script and leave it to harfbuzz @@ -291,6 +287,7 @@ impl HarfbuzzShaper { None, direction, range, + presentation_width, ); } } @@ -397,6 +394,7 @@ impl HarfbuzzShaper { presentation, direction, sub_range.clone(), + presentation_width, ) { Ok(shape) => Ok(shape), Err(e) => { @@ -410,6 +408,7 @@ impl HarfbuzzShaper { presentation, direction, 0..substr.len(), + presentation_width, ) } }?; @@ -438,13 +437,19 @@ impl HarfbuzzShaper { len = nom_width; } - let glyph = if len > 0 { - let text = &substr[next_idx..next_idx + len]; - make_glyphinfo(text, font_idx, info) + let text = if len > 0 { + &substr[next_idx..next_idx + len] } else { - make_glyphinfo("__", font_idx, info) + "__" }; + let num_cells = match presentation_width { + Some(pw) if len > 0 => pw.num_cells(next_idx..next_idx + len), + _ => unicode_column_width(text, None) as u8, + }; + + let glyph = make_glyphinfo(text, num_cells, font_idx, info); + if glyph.x_advance != PixelLength::new(0.0) { // log::error!("glyph: {:?}, nominal width: {:?}/{:?} = {:?}", glyph, glyph.x_advance, cell_width, nom_width); cluster.push(glyph); @@ -488,6 +493,7 @@ impl FontShaper for HarfbuzzShaper { presentation: Option, direction: Direction, range: Option>, + presentation_width: Option<&PresentationWidth>, ) -> anyhow::Result> { let range = range.unwrap_or_else(|| 0..text.len()); @@ -502,6 +508,7 @@ impl FontShaper for HarfbuzzShaper { presentation, direction, range, + presentation_width, ); metrics::histogram!("shape.harfbuzz", start.elapsed()); /* @@ -664,6 +671,7 @@ mod test { None, Direction::LeftToRight, None, + None, ) .unwrap(); assert!(no_glyphs.is_empty(), "{:?}", no_glyphs); @@ -722,6 +730,7 @@ mod test { None, Direction::LeftToRight, None, + None, ) .unwrap(); assert!(no_glyphs.is_empty(), "{:?}", no_glyphs); @@ -758,6 +767,7 @@ mod test { None, Direction::LeftToRight, None, + None, ) .unwrap(); assert!(no_glyphs.is_empty(), "{:?}", no_glyphs); @@ -804,6 +814,7 @@ mod test { None, Direction::LeftToRight, None, + None, ) .unwrap(); assert!(no_glyphs.is_empty(), "{:?}", no_glyphs); @@ -863,6 +874,7 @@ mod test { None, Direction::LeftToRight, None, + None, ) .unwrap(); assert!(no_glyphs.is_empty(), "{:?}", no_glyphs); @@ -922,6 +934,7 @@ mod test { None, Direction::LeftToRight, None, + None, ) .unwrap(); assert!(no_glyphs.is_empty(), "{:?}", no_glyphs); diff --git a/wezterm-font/src/shaper/mod.rs b/wezterm-font/src/shaper/mod.rs index ca8e8cb31..1741420b0 100644 --- a/wezterm-font/src/shaper/mod.rs +++ b/wezterm-font/src/shaper/mod.rs @@ -2,6 +2,7 @@ use crate::parser::ParsedFont; use crate::units::PixelLength; use std::ops::Range; use termwiz::cell::Presentation; +use termwiz::cellcluster::CellCluster; pub mod harfbuzz; pub use wezterm_bidi::Direction; @@ -72,6 +73,31 @@ pub struct FontMetrics { pub presentation: Presentation, } +pub struct PresentationWidth<'a> { + cluster: &'a CellCluster, +} + +impl<'a> PresentationWidth<'a> { + pub fn with_cluster(cluster: &'a CellCluster) -> Self { + Self { cluster } + } + + pub fn num_cells(&self, cluster_range: Range) -> u8 { + let mut width = 0; + let mut done_cells = vec![]; + + for byte_idx in cluster_range { + let cell_idx = self.cluster.byte_to_cell_idx(byte_idx); + if done_cells.contains(&cell_idx) { + continue; + } + done_cells.push(cell_idx); + width += self.cluster.byte_to_cell_width(byte_idx); + } + width + } +} + pub trait FontShaper { /// Shape text and return a vector of GlyphInfo fn shape( @@ -83,6 +109,7 @@ pub trait FontShaper { presentation: Option, direction: Direction, range: Option>, + presentation_width: Option<&PresentationWidth>, ) -> anyhow::Result>; /// Compute the font metrics for the preferred font diff --git a/wezterm-gui/src/main.rs b/wezterm-gui/src/main.rs index a91dd2c5b..e8bb3020a 100644 --- a/wezterm-gui/src/main.rs +++ b/wezterm-gui/src/main.rs @@ -20,6 +20,7 @@ use termwiz::cell::CellAttributes; use termwiz::surface::{Line, SEQ_ZERO}; use wezterm_bidi::Direction; use wezterm_client::domain::{ClientDomain, ClientDomainConfig}; +use wezterm_font::shaper::PresentationWidth; use wezterm_gui_subcommands::*; use wezterm_toast_notification::*; @@ -687,12 +688,14 @@ pub fn run_ls_fonts(config: config::ConfigHandle, cmd: &LsFontsCommand) -> anyho for cluster in cell_clusters { let style = font_config.match_style(&config, &cluster.attrs); let font = font_config.resolve_font(style)?; + let presentation_width = PresentationWidth::with_cluster(&cluster); let infos = font .blocking_shape( &cluster.text, Some(cluster.presentation), cluster.direction, None, + Some(&presentation_width), ) .unwrap(); diff --git a/wezterm-gui/src/shapecache.rs b/wezterm-gui/src/shapecache.rs index 2302eaa61..73b2a632a 100644 --- a/wezterm-gui/src/shapecache.rs +++ b/wezterm-gui/src/shapecache.rs @@ -262,6 +262,7 @@ mod test { use termwiz::cell::CellAttributes; use termwiz::surface::{Line, SEQ_ZERO}; use wezterm_bidi::Direction; + use wezterm_font::shaper::PresentationWidth; use wezterm_font::{FontConfiguration, LoadedFont}; fn cluster_and_shape( @@ -280,6 +281,7 @@ mod test { let cell_clusters = line.cluster(None, None); assert_eq!(cell_clusters.len(), 1); let cluster = &cell_clusters[0]; + let presentation_width = PresentationWidth::with_cluster(&cluster); let infos = font .shape( &cluster.text, @@ -288,6 +290,7 @@ mod test { None, Direction::LeftToRight, None, + Some(&presentation_width), ) .unwrap(); let glyphs = infos @@ -406,6 +409,7 @@ mod test { let line = Line::from_text(&text, &CellAttributes::default(), SEQ_ZERO); let cell_clusters = line.cluster(None, None); let cluster = &cell_clusters[0]; + let presentation_width = PresentationWidth::with_cluster(&cluster); measurer.measure(|| { let _x = font @@ -416,6 +420,7 @@ mod test { None, Direction::LeftToRight, None, + Some(&presentation_width), ) .unwrap(); // println!("{:?}", &x[0..2]); diff --git a/wezterm-gui/src/termwindow/box_model.rs b/wezterm-gui/src/termwindow/box_model.rs index 0d417fb29..ec5ddc10e 100644 --- a/wezterm-gui/src/termwindow/box_model.rs +++ b/wezterm-gui/src/termwindow/box_model.rs @@ -548,6 +548,7 @@ impl super::TermWindow { element.presentation, direction, None, + None, )?; let mut computed_cells = vec![]; let mut glyph_cache = context.gl_state.glyph_cache.borrow_mut(); diff --git a/wezterm-gui/src/termwindow/render.rs b/wezterm-gui/src/termwindow/render.rs index c1295b708..68a4cc638 100644 --- a/wezterm-gui/src/termwindow/render.rs +++ b/wezterm-gui/src/termwindow/render.rs @@ -34,6 +34,7 @@ use termwiz::cell::{unicode_column_width, Blink}; use termwiz::cellcluster::CellCluster; use termwiz::surface::{CursorShape, CursorVisibility}; use wezterm_bidi::Direction; +use wezterm_font::shaper::PresentationWidth; use wezterm_font::units::{IntPixelLength, PixelLength}; use wezterm_font::{ClearShapeCache, GlyphInfo, LoadedFont}; use wezterm_term::color::{ColorAttribute, ColorPalette, RgbColor}; @@ -2736,6 +2737,9 @@ impl super::TermWindow { None => self.fonts.resolve_font(style)?, }; let window = self.window.as_ref().unwrap().clone(); + + let presentation_width = PresentationWidth::with_cluster(&cluster); + match font.shape( &cluster.text, move || window.notify(TermWindowNotif::InvalidateShapeCache), @@ -2743,6 +2747,7 @@ impl super::TermWindow { Some(cluster.presentation), cluster.direction, None, // FIXME: need more paragraph context + Some(&presentation_width), ) { Ok(info) => { let glyphs = self.glyph_infos_to_glyphs( diff --git a/window/src/os/wayland/frame.rs b/window/src/os/wayland/frame.rs index a5684ee9a..dd90bf49b 100644 --- a/window/src/os/wayland/frame.rs +++ b/window/src/os/wayland/frame.rs @@ -464,6 +464,7 @@ impl ConceptFrame { None, wezterm_bidi::Direction::LeftToRight, None, + None, ) .ok()?;