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
This commit is contained in:
Wez Furlong committed 2022-02-01 09:23:57 -07:00
1 parent 832c8ad053
commit 5f2c905db8
9 files changed
+82 -21

No files matched your search

+9 -9
View File
@@ -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"
+7 -1
View File
@@ -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<Presentation>,
direction: Direction,
range: Option<Range<usize>>,
presentation_width: Option<&PresentationWidth>,
) -> anyhow::Result<Vec<GlyphInfo>> {
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::<ClearShapeCache>().is_some() => {
@@ -156,6 +158,7 @@ impl LoadedFont {
presentation: Option<Presentation>,
direction: Direction,
range: Option<Range<usize>>,
presentation_width: Option<&PresentationWidth>,
) -> anyhow::Result<Vec<GlyphInfo>> {
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<Presentation>,
direction: Direction,
range: Option<Range<usize>>,
presentation_width: Option<&PresentationWidth>,
) -> anyhow::Result<(bool, Vec<GlyphInfo>)> {
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}');
+24 -11
View File
@@ -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<Presentation>,
direction: Direction,
range: Range<usize>,
presentation_width: Option<&PresentationWidth>,
) -> anyhow::Result<Vec<GlyphInfo>> {
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<Presentation>,
direction: Direction,
range: Option<Range<usize>>,
presentation_width: Option<&PresentationWidth>,
) -> anyhow::Result<Vec<GlyphInfo>> {
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);
+27
View File
@@ -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<usize>) -> 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<termwiz::cell::Presentation>,
direction: Direction,
range: Option<Range<usize>>,
presentation_width: Option<&PresentationWidth>,
) -> anyhow::Result<Vec<GlyphInfo>>;
/// Compute the font metrics for the preferred font
+3
View File
@@ -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();
+5
View File
@@ -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<T>(
@@ -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]);
+1
View File
@@ -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();
+5
View File
@@ -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(
+1
View File
@@ -464,6 +464,7 @@ impl ConceptFrame {
None,
wezterm_bidi::Direction::LeftToRight,
None,
None,
)
.ok()?;