From c4d19afa75ef52f65483af2d392f3ac270d57448 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sat, 3 Sep 2022 08:34:19 -0700 Subject: [PATCH] adventures in shaping This commit is a result of debugging a problem with a particular font where a VS2 variation of a glyph produced incorrect shaping. Further investigation showed that this was specific to using freetype font functions and that switching to opentype functions produces the correct shaping information in that case. Further-further investigation showed that this difference in behavior was really due to the font file being out of spec and freetype returning unexpected data as a result. This commit allows switching to using harfbuzz's own opentype font+face (rather than freetype) and/or switching the freetype-based font to using opentype font functions. These two options don't yield equivalent results in the wezterm integration: a couple of our shaping tests fail due to the x-advances not being the same. Those can be drastically different in some cases (eg: I seem to get 0 for certain bitmap strikes, and for Roboto the value is off by a factor of about 1.5). This is incomplete and not something I want to turn on by default at the moment, but I don't want to lose this work, hence this commit. refs: https://github.com/wez/wezterm/issues/2475 refs: https://github.com/harfbuzz/harfbuzz/issues/3806 --- test-data/vs1-vs2.txt | 3 ++ wezterm-font/src/shaper/harfbuzz.rs | 59 +++++++++++++++++++++-------- 2 files changed, 46 insertions(+), 16 deletions(-) create mode 100644 test-data/vs1-vs2.txt diff --git a/test-data/vs1-vs2.txt b/test-data/vs1-vs2.txt new file mode 100644 index 000000000..f00926bdb --- /dev/null +++ b/test-data/vs1-vs2.txt @@ -0,0 +1,3 @@ +1d49c 𝒜 𝒜︀ 𝒜︁ +212c ℬ ℬ︀ ℬ︁ +1d49e 𝒞 𝒞︀ 𝒞︁ diff --git a/wezterm-font/src/shaper/harfbuzz.rs b/wezterm-font/src/shaper/harfbuzz.rs index 2cd142ecc..384eb4039 100644 --- a/wezterm-font/src/shaper/harfbuzz.rs +++ b/wezterm-font/src/shaper/harfbuzz.rs @@ -14,6 +14,16 @@ use thiserror::Error; use unicode_segmentation::UnicodeSegmentation; use wezterm_bidi::Direction; +// Changing these will switch to using harfbuzz's opentype functions. +// There's something awry with our integration in that mode: the advances +// aren't equivalent to its freetype functions and this manifests most +// prominently in proportional fonts as well as with fonts with bitmap +// strikes. +// Until we get to the bottom of this, these are compile-time rather +// than runtime configs. +const USE_OT_FUNCS: bool = false; +const USE_OT_FACE: bool = false; + #[derive(Clone, Debug)] struct Info { cluster: usize, @@ -138,13 +148,19 @@ impl HarfbuzzShaper { let handle = &self.handles[font_idx]; log::trace!("shaper wants {} {:?}", font_idx, handle); let face = self.lib.face_from_locator(&handle.handle)?; - let mut font = harfbuzz::Font::new(face.face); - let (load_flags, _) = ftwrap::compute_load_flags_from_config( - handle.freetype_load_flags, - handle.freetype_load_target, - handle.freetype_render_target, - ); - font.set_load_flags(load_flags); + + let font = if USE_OT_FACE { + harfbuzz::Font::from_locator(&handle.handle)? + } else { + let (load_flags, _) = ftwrap::compute_load_flags_from_config( + handle.freetype_load_flags, + handle.freetype_load_target, + handle.freetype_render_target, + ); + let mut font = harfbuzz::Font::new(face.face); + font.set_load_flags(load_flags); + font + }; let features = match &handle.harfbuzz_features { Some(features) => features @@ -220,22 +236,33 @@ impl HarfbuzzShaper { } } } - pair.face.set_font_size( - font_size * self.handles[font_idx].scale.unwrap_or(1.), - dpi, - )?; + let point_size = font_size * self.handles[font_idx].scale.unwrap_or(1.); + pair.face.set_font_size(point_size, dpi)?; + // Tell harfbuzz to recompute important font metrics! let mut font = pair.font.borrow_mut(); + + if USE_OT_FACE { + font.set_ppem(0, 0); + font.set_ptem(0.); + let scale = (point_size * 2f64.powf(6.)) as i32; + font.set_font_scale(scale, scale); + } + font.font_changed(); + + if USE_OT_FUNCS { + font.set_ot_funcs(); + } + shaped_any = pair.shaped_any; font.shape(&mut buf, pair.features.as_slice()); - /* - log::info!( - "shaped font_idx={} as: {}", + log::trace!( + "shaped font_idx={} {:?} as: {}", font_idx, - buf.serialize(Some(&pair.font)) + &s[range.start..range.end], + buf.serialize(Some(&*font)) ); - */ break; } None => {