Adjust font scaling math again
This commit introduces the knowledge about whether a font is scalable or was using bitmap strikes (eg: color emoji bitmaps). Then that information is used to help figure out whether and how to scale a glyph. refs: https://github.com/wez/wezterm/issues/685
This commit is contained in:
1 parent
9ccdc157a7
commit
8880979586
7 files changed
+128
-54
No files matched your search
@@ -1098,6 +1098,9 @@ pub struct Config {
|
||||
#[serde(default)]
|
||||
pub search_font_dirs_for_fallback: bool,
|
||||
|
||||
#[serde(default)]
|
||||
pub use_cap_height_to_scale_fallback_fonts: bool,
|
||||
|
||||
#[serde(default)]
|
||||
pub swallow_mouse_click_on_pane_focus: bool,
|
||||
|
||||
|
||||
Executable
+7
@@ -0,0 +1,7 @@
|
||||
#!/bin/zsh
|
||||
|
||||
echo "\u24e9 ."
|
||||
echo "\u24e9."
|
||||
echo "<--"
|
||||
echo 🛑
|
||||
|
||||
@@ -98,6 +98,13 @@ struct FaceSize {
|
||||
dpi: u32,
|
||||
cell_width: f64,
|
||||
cell_height: f64,
|
||||
is_scaled: bool,
|
||||
}
|
||||
|
||||
pub struct SelectedFontSize {
|
||||
pub width: f64,
|
||||
pub height: f64,
|
||||
pub is_scaled: bool,
|
||||
}
|
||||
|
||||
impl Face {
|
||||
@@ -283,10 +290,14 @@ impl Face {
|
||||
|
||||
/// This is a wrapper around set_char_size and select_size
|
||||
/// that accounts for some weirdness with eg: color emoji
|
||||
pub fn set_font_size(&mut self, point_size: f64, dpi: u32) -> anyhow::Result<(f64, f64)> {
|
||||
pub fn set_font_size(&mut self, point_size: f64, dpi: u32) -> anyhow::Result<SelectedFontSize> {
|
||||
if let Some(face_size) = self.size.as_ref() {
|
||||
if face_size.size == point_size && face_size.dpi == dpi {
|
||||
return Ok((face_size.cell_width, face_size.cell_height));
|
||||
return Ok(SelectedFontSize {
|
||||
width: face_size.cell_width,
|
||||
height: face_size.cell_height,
|
||||
is_scaled: face_size.is_scaled,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -302,10 +313,15 @@ impl Face {
|
||||
// the fallback code for set_pixel_sizes below.
|
||||
let size = (point_size * 64.0) as FT_F26Dot6;
|
||||
|
||||
let (cell_width, cell_height) = match self.set_char_size(size, size, dpi, dpi) {
|
||||
let selected_size = match self.set_char_size(size, size, dpi, dpi) {
|
||||
Ok(_) => {
|
||||
// Compute metrics for the nominal monospace cell
|
||||
self.cell_metrics()
|
||||
let (width, height) = self.cell_metrics();
|
||||
SelectedFontSize {
|
||||
width,
|
||||
height,
|
||||
is_scaled: true,
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
log::debug!("set_char_size: {:?}, will inspect strikes", err);
|
||||
@@ -352,18 +368,23 @@ impl Face {
|
||||
}
|
||||
let best = best.unwrap();
|
||||
self.select_size(best.idx)?;
|
||||
(f64::from(best.width), f64::from(best.height))
|
||||
SelectedFontSize {
|
||||
width: f64::from(best.width),
|
||||
height: f64::from(best.height),
|
||||
is_scaled: false,
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
self.size.replace(FaceSize {
|
||||
size: point_size,
|
||||
dpi,
|
||||
cell_width,
|
||||
cell_height,
|
||||
cell_width: selected_size.width,
|
||||
cell_height: selected_size.height,
|
||||
is_scaled: selected_size.is_scaled,
|
||||
});
|
||||
|
||||
Ok((cell_width, cell_height))
|
||||
Ok(selected_size)
|
||||
}
|
||||
|
||||
fn set_char_size(
|
||||
|
||||
@@ -227,6 +227,7 @@ impl AllsortsParsedFont {
|
||||
underline_thickness,
|
||||
underline_position,
|
||||
cap_height_ratio: None,
|
||||
is_scaled: true, // FIXME
|
||||
};
|
||||
|
||||
log::trace!("metrics: {:?}", metrics);
|
||||
|
||||
@@ -178,8 +178,8 @@ impl HarfbuzzShaper {
|
||||
{
|
||||
match self.load_fallback(font_idx).context("load_fallback")? {
|
||||
Some(mut pair) => {
|
||||
let (width, _height) = pair.face.set_font_size(font_size, dpi)?;
|
||||
cell_width = width;
|
||||
let size = pair.face.set_font_size(font_size, dpi)?;
|
||||
cell_width = size.width;
|
||||
shaped_any = pair.shaped_any;
|
||||
pair.font.shape(&mut buf, self.features.as_slice());
|
||||
}
|
||||
@@ -396,11 +396,11 @@ impl FontShaper for HarfbuzzShaper {
|
||||
return Ok(metrics.clone());
|
||||
}
|
||||
|
||||
let (cell_width, cell_height) = pair.face.set_font_size(size, dpi)?;
|
||||
let selected_size = 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),
|
||||
cell_height: PixelLength::new(selected_size.height),
|
||||
cell_width: PixelLength::new(selected_size.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(
|
||||
@@ -413,6 +413,7 @@ impl FontShaper for HarfbuzzShaper {
|
||||
unsafe { (*pair.face.face).underline_position as f64 } * y_scale / 64.,
|
||||
),
|
||||
cap_height_ratio: pair.face.cap_height(),
|
||||
is_scaled: selected_size.is_scaled,
|
||||
};
|
||||
|
||||
self.metrics.borrow_mut().insert(key, metrics.clone());
|
||||
@@ -450,15 +451,15 @@ impl FontShaper for HarfbuzzShaper {
|
||||
self.handles
|
||||
);
|
||||
while let Ok(Some(mut pair)) = self.load_fallback(metrics_idx) {
|
||||
let (_, cell_height) = pair.face.set_font_size(size, dpi)?;
|
||||
let diff = (theoretical_height - cell_height).abs();
|
||||
let selected_size = pair.face.set_font_size(size, dpi)?;
|
||||
let diff = (theoretical_height - selected_size.height).abs();
|
||||
let factor = diff / theoretical_height;
|
||||
if factor < 2.0 {
|
||||
log::trace!(
|
||||
"idx {} cell_height is {}, which is {} away from theoretical
|
||||
height (factor {}). Seems good enough",
|
||||
metrics_idx,
|
||||
cell_height,
|
||||
selected_size.height,
|
||||
diff,
|
||||
factor
|
||||
);
|
||||
@@ -470,7 +471,7 @@ impl FontShaper for HarfbuzzShaper {
|
||||
diff,
|
||||
factor,
|
||||
theoretical_height,
|
||||
cell_height
|
||||
selected_size.height
|
||||
);
|
||||
metrics_idx += 1;
|
||||
}
|
||||
|
||||
@@ -54,6 +54,11 @@ pub struct FontMetrics {
|
||||
|
||||
/// Fraction of the EM square occupied by the cap height
|
||||
pub cap_height_ratio: Option<f64>,
|
||||
|
||||
/// True if the font is scalable and this is a scaled metric.
|
||||
/// False if the font only has bitmap strikes and what we
|
||||
/// have here is a best approximation.
|
||||
pub is_scaled: bool,
|
||||
}
|
||||
|
||||
pub trait FontShaper {
|
||||
|
||||
@@ -465,38 +465,12 @@ impl<T: Texture2d> GlyphCache<T> {
|
||||
idx_metrics = font.metrics_for_idx(info.font_idx)?;
|
||||
}
|
||||
|
||||
let x_scale;
|
||||
let y_scale;
|
||||
|
||||
if info.font_idx == 0 {
|
||||
// The base font is the current font, so there's no additional metrics
|
||||
// based scaling, however, we may need to scale to accomodate num_cells
|
||||
x_scale = 1.0 / info.num_cells as f64;
|
||||
y_scale = 1.0;
|
||||
} else if let (Some(base_cap), Some(cap)) =
|
||||
(base_metrics.cap_height_ratio, idx_metrics.cap_height_ratio)
|
||||
{
|
||||
// If we know the cap height ratio for both fonts, we can scale
|
||||
// the second one to match the cap height of the first
|
||||
y_scale = base_cap / cap;
|
||||
x_scale = y_scale / info.num_cells as f64;
|
||||
} else {
|
||||
// Otherwise, we scale based on the ratio of the metrics for
|
||||
// the two fonts.
|
||||
|
||||
// If we know the cap height ratio for the first, we can adjust
|
||||
// the overall scale so that the second font isn't oversized.
|
||||
// Disable for now because it leads to emoji and other symbols
|
||||
// being under-sized :-/
|
||||
// https://github.com/wez/wezterm/issues/727
|
||||
let base_cap = 1.0; // base_metrics.cap_height_ratio.unwrap_or(1.);
|
||||
y_scale = base_cap * base_metrics.cell_height.get() / idx_metrics.cell_height.get();
|
||||
x_scale = base_cap * base_metrics.cell_width.get()
|
||||
/ (idx_metrics.cell_width.get() / info.num_cells as f64);
|
||||
}
|
||||
|
||||
let aspect = (idx_metrics.cell_width / idx_metrics.cell_height).get();
|
||||
let is_square_or_wide = aspect >= 0.9;
|
||||
|
||||
// 0.7 is used for this as that is ~ the threshold for \u24e9 on a mac,
|
||||
// which is looks squareish and for which it is desirable to allow to
|
||||
// overflow. 0.5 is the typical monospace font aspect ratio.
|
||||
let is_square_or_wide = aspect >= 0.7;
|
||||
|
||||
let allow_width_overflow = if is_square_or_wide {
|
||||
match self.fonts.config().allow_square_glyphs_to_overflow_width {
|
||||
@@ -508,13 +482,75 @@ impl<T: Texture2d> GlyphCache<T> {
|
||||
false
|
||||
};
|
||||
|
||||
let scale = if !allow_width_overflow
|
||||
&& 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
|
||||
// Maximum width allowed for this glyph based on its unicode width and
|
||||
// the dimensions of a cell
|
||||
let max_pixel_width = base_metrics.cell_width.get() * (info.num_cells as f64 + 0.25);
|
||||
|
||||
let scale;
|
||||
if info.font_idx == 0 {
|
||||
// We are the base font
|
||||
scale = if allow_width_overflow {
|
||||
1.0
|
||||
} else {
|
||||
// Scale the glyph to fit in its number of cells
|
||||
1.0 / info.num_cells as f64
|
||||
};
|
||||
} else if !idx_metrics.is_scaled {
|
||||
// A bitmap font that isn't scaled to the requested height.
|
||||
let y_scale = base_metrics.cell_height.get() / idx_metrics.cell_height.get();
|
||||
let y_scaled_width = y_scale * glyph.width as f64;
|
||||
|
||||
if allow_width_overflow || y_scaled_width <= max_pixel_width {
|
||||
// prefer height-wise scaling
|
||||
scale = y_scale;
|
||||
} else {
|
||||
// otherwise just make it fit the width
|
||||
scale = max_pixel_width / glyph.width as f64;
|
||||
}
|
||||
} else {
|
||||
y_scale
|
||||
// a scalable fallback font
|
||||
let y_scale = match (
|
||||
self.fonts.config().use_cap_height_to_scale_fallback_fonts,
|
||||
base_metrics.cap_height_ratio,
|
||||
idx_metrics.cap_height_ratio,
|
||||
) {
|
||||
(true, Some(base_cap), Some(cap)) => {
|
||||
// both fonts have cap-height metrics and we're in
|
||||
// use_cap_height_to_scale_fallback_fonts mode, so
|
||||
// scale based on their respective cap heights
|
||||
base_cap / cap
|
||||
}
|
||||
_ => {
|
||||
// Assume that the size we requested doesn't need
|
||||
// any additional scaling
|
||||
1.0
|
||||
}
|
||||
};
|
||||
|
||||
// How wide the glyph would be using the y_scale we produced
|
||||
let y_scaled_width = y_scale * glyph.width as f64;
|
||||
|
||||
if allow_width_overflow || y_scaled_width <= max_pixel_width {
|
||||
scale = y_scale;
|
||||
} else {
|
||||
scale = max_pixel_width / glyph.width as f64;
|
||||
}
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
{
|
||||
log::debug!(
|
||||
"{} allow_width_overflow={} is_square_or_wide={} aspect={} \
|
||||
y_scaled_width={} max_pixel_width={} glyph.width={} -> scale={}",
|
||||
info.text,
|
||||
allow_width_overflow,
|
||||
is_square_or_wide,
|
||||
aspect,
|
||||
y_scaled_width,
|
||||
max_pixel_width,
|
||||
glyph.width,
|
||||
scale
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
let (cell_width, cell_height) = (base_metrics.cell_width, base_metrics.cell_height);
|
||||
|
||||
Reference in new issue
Block a user