wezterm-font: improve fallback font scaling

Use the scaling factor between the font metrics for the base font
and those of the fallback font selected for a given glyph.

The scenario is this: the base font is typically the first one selected
from the font configuration.  There may be multiple fallback fonts that
are different sizes; for instance, the Font Awesome font has glyphs that
are square in aspect and are thus about twice the width of a typical
textual monospace font.  Similarly, Noto Color Emoji is another square
font but that has a single set of bitmap strikes at a fixed 128 px
square.

The shaper returns advance metrics in the scale of the containing font,
and the rasterizer will target the supplied size and dpi.

We need to scale these to match the base metrics.

Previously we used a crude heuristic to decide whether to scale,
and that happened to work for Noto Color Emoji but not for Font Awesome,
whose metrics were just inside the bounds of the heuristic.

This commit allows retrieving the metrics for a given font_idx so
that we can compute the correct scale factor without any heuristics,
and applies that to the rasterized glyph.

refs: https://github.com/wez/wezterm/issues/342
This commit is contained in:
Wez Furlong committed 2020-11-23 16:59:30 -08:00
1 parent 65ac2269dd
commit 98f289f511
6 files changed
+69 -42

No files matched your search

+5
View File
@@ -652,5 +652,10 @@ mod test {
eprintln!("xi emoji_mod_base: {}", c.is_emoji_modifier_base());
}
assert_eq!(unicode_column_width(deaf_man), 2);
// This is a codepoint in the private use area
let font_awesome_star = "\u{f005}";
eprintln!("font_awesome_star {}", font_awesome_star.escape_debug());
assert_eq!(unicode_column_width(font_awesome_star), 1);
}
}
+5
View File
@@ -41,6 +41,11 @@ impl LoadedFont {
self.shaper.shape(text, self.font_size, self.dpi)
}
pub fn metrics_for_idx(&self, font_idx: usize) -> anyhow::Result<FontMetrics> {
self.shaper
.metrics_for_idx(font_idx, self.font_size, self.dpi)
}
pub fn rasterize_glyph(
&self,
glyph_pos: u32,
+11
View File
@@ -119,6 +119,17 @@ impl FontShaper for AllsortsShaper {
Ok(results)
}
fn metrics_for_idx(&self, font_idx: usize, size: f64, dpi: u32) -> anyhow::Result<FontMetrics> {
let font = self
.fonts
.get(font_idx)
.ok_or_else(|| anyhow::anyhow!("invalid font_idx {}", font_idx))?;
let font = font
.as_ref()
.ok_or_else(|| anyhow::anyhow!("failed to load font_idx {}", font_idx))?;
Ok(font.get_metrics(size, dpi))
}
fn metrics(&self, size: f64, dpi: u32) -> anyhow::Result<FontMetrics> {
for font in &self.fonts {
if let Some(font) = font {
+28 -24
View File
@@ -334,6 +334,33 @@ impl FontShaper for HarfbuzzShaper {
result
}
fn metrics_for_idx(&self, font_idx: usize, size: f64, dpi: u32) -> anyhow::Result<FontMetrics> {
let mut pair = self
.load_fallback(font_idx)?
.ok_or_else(|| anyhow!("unable to load font idx {}!?", font_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 {
cell_height: PixelLength::new(cell_height),
cell_width: PixelLength::new(cell_width),
// Note: face.face.descender is useless, we have to go through
// face.face.size.metrics to get to the real descender!
descender: PixelLength::new(
unsafe { (*(*pair.face.face).size).metrics.descender as f64 } / 64.0,
),
underline_thickness: PixelLength::new(
unsafe { (*pair.face.face).underline_thickness as f64 } * y_scale / 64.,
),
underline_position: PixelLength::new(
unsafe { (*pair.face.face).underline_position as f64 } * y_scale / 64.,
),
};
log::trace!("metrics: {:?}", metrics);
Ok(metrics)
}
fn metrics(&self, size: f64, dpi: u32) -> anyhow::Result<FontMetrics> {
// Returns the metrics for the selected font... but look out
// for implausible sizes.
@@ -366,29 +393,6 @@ impl FontShaper for HarfbuzzShaper {
metrics_idx += 1;
}
let mut pair = self
.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 {
cell_height: PixelLength::new(cell_height),
cell_width: PixelLength::new(cell_width),
// Note: face.face.descender is useless, we have to go through
// face.face.size.metrics to get to the real descender!
descender: PixelLength::new(
unsafe { (*(*pair.face.face).size).metrics.descender as f64 } / 64.0,
),
underline_thickness: PixelLength::new(
unsafe { (*pair.face.face).underline_thickness as f64 } * y_scale / 64.,
),
underline_position: PixelLength::new(
unsafe { (*pair.face.face).underline_position as f64 } * y_scale / 64.,
),
};
log::trace!("metrics: {:?}", metrics);
Ok(metrics)
self.metrics_for_idx(metrics_idx, size, dpi)
}
}
+4 -1
View File
@@ -34,7 +34,7 @@ pub struct GlyphInfo {
pub type FallbackIdx = usize;
/// Describes the key font metrics that we use in rendering
#[derive(Copy, Clone, Debug)]
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct FontMetrics {
/// Width of a character cell in pixels
pub cell_width: PixelLength,
@@ -59,6 +59,9 @@ pub trait FontShaper {
/// Compute the font metrics for the preferred font
/// at the specified size.
fn metrics(&self, size: f64, dpi: u32) -> anyhow::Result<FontMetrics>;
/// Compute the metrics for a given fallback font at the specified size
fn metrics_for_idx(&self, font_idx: usize, size: f64, dpi: u32) -> anyhow::Result<FontMetrics>;
}
pub use config::FontShaperSelection;
+16 -17
View File
@@ -174,33 +174,32 @@ impl<T: Texture2d> GlyphCache<T> {
info: &GlyphInfo,
style: &TextStyle,
) -> anyhow::Result<Rc<CachedGlyph<T>>> {
let metrics;
let base_metrics;
let idx_metrics;
let glyph;
{
let font = self.fonts.resolve_font(style)?;
metrics = font.metrics();
base_metrics = font.metrics();
glyph = font.rasterize_glyph(info.glyph_pos, info.font_idx)?;
idx_metrics = font.metrics_for_idx(info.font_idx)?;
}
let (cell_width, cell_height) = (metrics.cell_width, metrics.cell_height);
let scale = if PixelLength::new(glyph.height as f64) > cell_height * 1.5 {
// This is another way to detect oversize glyph images.
// Compute an aspect-preserving scale factor that makes the glyph
// fit in the appropriate number of cells.
// The GlyphInfo tells us how many horizontal cells the glyph occupies.
let y_scale = cell_height.get() / glyph.height as f64;
let x_scale = (cell_width.get() * info.num_cells as f64) / glyph.width as f64;
let y_scale = base_metrics.cell_height.get() / idx_metrics.cell_height.get();
let x_scale = base_metrics.cell_width.get() / idx_metrics.cell_width.get();
if PixelLength::new(y_scale * glyph.width as f64) > cell_width {
// The y scale would overflow the width, so use the x-scale
x_scale
} else {
y_scale
}
let scale = if y_scale * glyph.width as f64
> base_metrics.cell_width.get() * info.num_cells as f64
{
// y-scaling would make us too wide, so use the x-scale
x_scale
} else {
1.0f64
y_scale
};
let (cell_width, cell_height) = (base_metrics.cell_width, base_metrics.cell_height);
let glyph = if glyph.width == 0 || glyph.height == 0 {
// a whitespace glyph
CachedGlyph {