From 717a2157f602dd171ccec4deddadf58b7502f887 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sun, 30 May 2021 20:52:10 -0700 Subject: [PATCH] fonts: synthesize dim when a light weight font is unavailable --- docs/changelog.md | 2 +- wezterm-font/src/lib.rs | 14 ++++++++++++++ wezterm-font/src/parser.rs | 8 ++++++++ wezterm-gui/src/glyphcache.rs | 6 ++++++ wezterm-gui/src/termwindow/render.rs | 13 ++++++++++--- 5 files changed, 39 insertions(+), 4 deletions(-) diff --git a/docs/changelog.md b/docs/changelog.md index d1ebedd74..0195a612b 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -25,7 +25,7 @@ As features stabilize some brief notes about them will accumulate here. * Fixed: errors loading custom color schemes are now logged to the error log [#794](https://github.com/wez/wezterm/issues/794) * Fixed: OSC 7 (current working directory) now works with paths that contain spaces and other special characters. Thanks to [@Arvedui](https://github.com/Arvedui)! [#799](https://github.com/wez/wezterm/pull/799) * Changed: the homebrew tap is now a Cask that installs to the /Applications directory on macOS. Thanks to [@laggardkernel](https://github.com/laggardkernel)! -* New: bold and/or italics are now synthesized for fonts when the matching font is not actually italic or doesn't match the requested weight. [#815](https://github.com/wez/wezterm/issues/815) +* New: bold/dim and/or italics are now synthesized for fonts when the matching font is not actually italic or doesn't match the requested weight. [#815](https://github.com/wez/wezterm/issues/815) * Updated: conpty.dll to v1.9.1445.0; fixes color bar artifacts when resizing window and allows win32 console applications to use mouse events * Fixed: Windows: pane could linger after the process has died, closing only when a new pane/tab event occurs * Fixed: Windows: first character after `wezterm ssh` keyboard authention was swallowed [#771](https://github.com/wez/wezterm/issues/771) diff --git a/wezterm-font/src/lib.rs b/wezterm-font/src/lib.rs index f4be8b781..02d4f4f0b 100644 --- a/wezterm-font/src/lib.rs +++ b/wezterm-font/src/lib.rs @@ -123,6 +123,20 @@ impl LoadedFont { .metrics_for_idx(font_idx, self.font_size, self.dpi) } + pub fn brightness_adjust(&self, font_idx: usize) -> f32 { + let synthesize_dim = self + .handles + .borrow() + .get(font_idx) + .map(|p| p.synthesize_dim) + .unwrap_or(false); + if synthesize_dim { + 0.5 + } else { + 1.0 + } + } + pub fn rasterize_glyph( &self, glyph_pos: u32, diff --git a/wezterm-font/src/parser.rs b/wezterm-font/src/parser.rs index ba8a5051a..5181be3c8 100644 --- a/wezterm-font/src/parser.rs +++ b/wezterm-font/src/parser.rs @@ -23,6 +23,7 @@ pub struct ParsedFont { coverage: Mutex>, pub synthesize_italic: bool, pub synthesize_bold: bool, + pub synthesize_dim: bool, } impl std::fmt::Debug for ParsedFont { @@ -47,6 +48,7 @@ impl Clone for ParsedFont { italic: self.italic, synthesize_italic: self.synthesize_italic, synthesize_bold: self.synthesize_bold, + synthesize_dim: self.synthesize_dim, handle: self.handle.clone(), cap_height: self.cap_height.clone(), coverage: Mutex::new(self.coverage.lock().unwrap().clone()), @@ -139,6 +141,8 @@ impl ParsedFont { } if p.synthesize_bold { code.push_str(" -- Will synthesize bold\n"); + } else if p.synthesize_dim { + code.push_str(" -- Will synthesize dim\n"); } if p.weight == FontWeight::Regular && p.stretch == FontStretch::Normal && !p.italic { @@ -176,6 +180,7 @@ impl ParsedFont { italic, synthesize_italic: false, synthesize_bold: false, + synthesize_dim: false, handle, coverage: Mutex::new(RangeSet::new()), cap_height, @@ -384,6 +389,9 @@ impl ParsedFont { self.synthesize_bold = attr.weight > FontWeight::Regular && attr.weight > self.weight && self.weight <= FontWeight::Regular; + self.synthesize_dim = attr.weight < FontWeight::Regular + && attr.weight < self.weight + && self.weight >= FontWeight::Regular; self } } diff --git a/wezterm-gui/src/glyphcache.rs b/wezterm-gui/src/glyphcache.rs index c696c1850..9f205d182 100644 --- a/wezterm-gui/src/glyphcache.rs +++ b/wezterm-gui/src/glyphcache.rs @@ -99,6 +99,7 @@ impl<'a> std::hash::Hash for (dyn GlyphKeyTrait + 'a) { /// The image data may be None for whitespace glyphs. pub struct CachedGlyph { pub has_color: bool, + pub brightness_adjust: f32, pub x_offset: PixelLength, pub y_offset: PixelLength, pub bearing_x: PixelLength, @@ -431,6 +432,7 @@ impl GlyphCache { style ); Rc::new(CachedGlyph { + brightness_adjust: 1.0, has_color: false, texture: None, x_offset: PixelLength::zero(), @@ -455,6 +457,7 @@ impl GlyphCache { ) -> anyhow::Result>> { let base_metrics; let idx_metrics; + let brightness_adjust; let glyph; { @@ -463,6 +466,7 @@ impl GlyphCache { glyph = font.rasterize_glyph(info.glyph_pos, info.font_idx)?; idx_metrics = font.metrics_for_idx(info.font_idx)?; + brightness_adjust = font.brightness_adjust(info.font_idx); } let aspect = (idx_metrics.cell_width / idx_metrics.cell_height).get(); @@ -558,6 +562,7 @@ impl GlyphCache { let glyph = if glyph.width == 0 || glyph.height == 0 { // a whitespace glyph CachedGlyph { + brightness_adjust: 1.0, has_color: glyph.has_color, texture: None, x_offset: info.x_offset * scale, @@ -598,6 +603,7 @@ impl GlyphCache { let tex = self.atlas.allocate(&raw_im)?; let g = CachedGlyph { + brightness_adjust, has_color: glyph.has_color, texture: Some(tex), x_offset, diff --git a/wezterm-gui/src/termwindow/render.rs b/wezterm-gui/src/termwindow/render.rs index 443646a09..879af7052 100644 --- a/wezterm-gui/src/termwindow/render.rs +++ b/wezterm-gui/src/termwindow/render.rs @@ -13,8 +13,7 @@ use ::window::glium::uniforms::{ use ::window::glium::{uniform, BlendingFunction, LinearBlendingFactor, Surface}; use ::window::WindowOps; use anyhow::anyhow; -use config::ConfigHandle; -use config::TextStyle; +use config::{ConfigHandle, HsbTransform, TextStyle}; use mux::pane::Pane; use mux::renderable::{RenderableDimensions, StableCursorPosition}; use mux::tab::{PositionedPane, PositionedSplit, SplitDirection}; @@ -1002,7 +1001,15 @@ impl super::TermWindow { quad.set_texture_adjust(left, top, right, bottom); quad.set_underline(underline_tex_rect); quad.set_underline_color(underline_color); - quad.set_hsv(hsv); + quad.set_hsv(if glyph.brightness_adjust != 1.0 { + let hsv = hsv.unwrap_or_else(|| HsbTransform::default()); + Some(HsbTransform { + brightness: hsv.brightness * glyph.brightness_adjust, + ..hsv + }) + } else { + hsv + }); quad.set_has_color(glyph.has_color); quad.set_cursor( gl_state