From 4ec428ff75247faa2d68d2af0e4aca082aa0017f Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Mon, 9 Jan 2023 15:19:17 -0700 Subject: [PATCH] config: allow using bright but not bold text when brightening text In https://github.com/wez/wezterm/issues/2932 the user desired to have brightened text without the boldness, as they were accustomed to that behavior in a couple of other terminal emulators. This commit changes the `bold_brightens_ansi_colors` from a simple boolean to a tristate that allows for not changing the brightness, changing the brightness, and changing the brightness while adjusting the boldness down to normal levels. boolean values are accepted for backwards compatibility. --- config/src/config.rs | 43 ++++++++++++++++++- docs/changelog.md | 3 ++ .../lua/config/bold_brightens_ansi_colors.md | 12 ++++++ wezterm-font/src/lib.rs | 27 ++++++++++-- wezterm-gui/src/termwindow/render.rs | 6 +-- 5 files changed, 82 insertions(+), 9 deletions(-) diff --git a/config/src/config.rs b/config/src/config.rs index e77e35fd5..287cdeaeb 100644 --- a/config/src/config.rs +++ b/config/src/config.rs @@ -102,8 +102,8 @@ pub struct Config { /// When true (the default), PaletteIndex 0-7 are shifted to /// bright when the font intensity is bold. The brightening /// doesn't apply to text that is the default color. - #[dynamic(default = "default_true")] - pub bold_brightens_ansi_colors: bool, + #[dynamic(default)] + pub bold_brightens_ansi_colors: BoldBrightening, /// The color palette pub colors: Option, @@ -1729,6 +1729,45 @@ fn default_line_to_ele_shape_cache_size() -> usize { 1024 } +#[derive(Debug, ToDynamic, Clone, Copy, PartialEq, Eq)] +pub enum BoldBrightening { + /// Bold doesn't influence palette selection + No, + /// Bold Shifts palette from 0-7 to 8-15 and preserves bold font + BrightAndBold, + /// Bold Shifts palette from 0-7 to 8-15 and removes bold intensity + BrightOnly, +} + +impl FromDynamic for BoldBrightening { + fn from_dynamic( + value: &wezterm_dynamic::Value, + options: wezterm_dynamic::FromDynamicOptions, + ) -> Result { + match String::from_dynamic(value, options) { + Ok(s) => match s.as_str() { + "No" => Ok(Self::No), + "BrightAndBold" => Ok(Self::BrightAndBold), + "BrightOnly" => Ok(Self::BrightOnly), + s => Err(wezterm_dynamic::Error::Message(format!( + "`{s}` is not valid, use one of `None`, `BrightAndBold` or `BrightOnly`" + ))), + }, + Err(err) => match bool::from_dynamic(value, options) { + Ok(true) => Ok(Self::BrightAndBold), + Ok(false) => Ok(Self::No), + Err(_) => Err(err), + }, + } + } +} + +impl Default for BoldBrightening { + fn default() -> Self { + BoldBrightening::BrightAndBold + } +} + #[derive(Debug, FromDynamic, ToDynamic, Clone, Copy, PartialEq, Eq)] pub enum ImePreeditRendering { /// IME preedit is rendered by WezTerm itself diff --git a/docs/changelog.md b/docs/changelog.md index a4c05be1b..51539d4fc 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -49,6 +49,9 @@ As features stabilize some brief notes about them will accumulate here. [@Abdiramen](https://github.com/Abdiramen)! [#2889](https://github.com/wez/wezterm/pull/2889) [#2782](https://github.com/wez/wezterm/issues/2782) +* [bold_brightens_ansi_colors](config/lua/config/bold_brightens_ansi_colors.md) + now supports `"BrightOnly"` to use the bright color without selecting a bold + font. [#2932](https://github.com/wez/wezterm/issues/2932) #### Updated * Bundled harfbuzz updated to version 6.0.0 diff --git a/docs/config/lua/config/bold_brightens_ansi_colors.md b/docs/config/lua/config/bold_brightens_ansi_colors.md index 6736a30e1..9b3eee713 100644 --- a/docs/config/lua/config/bold_brightens_ansi_colors.md +++ b/docs/config/lua/config/bold_brightens_ansi_colors.md @@ -11,3 +11,15 @@ range of mature software; for instance, a lot of software assumes that Black+Bold renders as a Dark Grey which is legible on a Black background, but if this option is set to false, it would render as Black on Black. + +*Since: nightly builds only* + +This option can now have one of three values: + +* `"No"` - the bold attribute will not influence palette selection +* `"BrightAndBold"` - the bold attribute will select a bright version of palette indices 0-7 and preserve the bold attribute on the text, using both a bold font and a brighter color +* `"BrightOnly"` - the bold attribute will select a bright version of palette indices 0-7 but the intensity will be treated as normal and a non-bold font will be used for the text. + +You may use `true` or `false` for backwards compatibility. `true` is +equivalent to `"BrightAndBold"` and `false` is equivalent to `"No"`. + diff --git a/wezterm-font/src/lib.rs b/wezterm-font/src/lib.rs index 0d4feaed6..90bf599ff 100644 --- a/wezterm-font/src/lib.rs +++ b/wezterm-font/src/lib.rs @@ -5,8 +5,8 @@ use crate::rasterizer::{new_rasterizer, FontRasterizer}; use crate::shaper::{new_shaper, FontShaper, PresentationWidth}; use anyhow::{Context, Error}; use config::{ - configuration, ConfigHandle, FontAttributes, FontRasterizerSelection, FontStretch, FontStyle, - FontWeight, TextStyle, + configuration, BoldBrightening, ConfigHandle, FontAttributes, FontRasterizerSelection, + FontStretch, FontStyle, FontWeight, TextStyle, }; use rangeset::RangeSet; use std::cell::RefCell; @@ -19,7 +19,7 @@ use std::time::{Duration, Instant}; use termwiz::cell::Presentation; use thiserror::Error; use wezterm_bidi::Direction; -use wezterm_term::CellAttributes; +use wezterm_term::{CellAttributes, Intensity}; use wezterm_toast_notification::ToastNotification; mod hbwrap; @@ -995,8 +995,27 @@ impl FontConfigInner { }; } + let would_bright = match attrs.foreground() { + wezterm_term::color::ColorAttribute::PaletteIndex(idx) if idx < 8 => { + attrs.intensity() == Intensity::Bold + } + _ => false, + }; + for rule in &config.font_rules { - attr_match!(intensity, &rule); + if let Some(intensity) = rule.intensity { + let effective_intensity = match config.bold_brightens_ansi_colors { + BoldBrightening::BrightOnly if would_bright => Intensity::Normal, + BoldBrightening::No + | BoldBrightening::BrightAndBold + | BoldBrightening::BrightOnly => attrs.intensity(), + }; + if intensity != effective_intensity { + // Rule does not match + continue; + } + // matches so far + } attr_match!(underline, &rule); attr_match!(italic, &rule); attr_match!(blink, &rule); diff --git a/wezterm-gui/src/termwindow/render.rs b/wezterm-gui/src/termwindow/render.rs index 058da9e8a..ac42e1b7d 100644 --- a/wezterm-gui/src/termwindow/render.rs +++ b/wezterm-gui/src/termwindow/render.rs @@ -25,8 +25,8 @@ use ::window::glium::{BlendingFunction, LinearBlendingFactor, Surface}; use ::window::{glium, DeadKeyStatus, PointF, RectF, SizeF, ULength, WindowOps}; use anyhow::anyhow; use config::{ - ConfigHandle, Dimension, DimensionContext, FreeTypeLoadTarget, HsbTransform, TabBarColors, - TextStyle, VisualBellTarget, + BoldBrightening, ConfigHandle, Dimension, DimensionContext, FreeTypeLoadTarget, HsbTransform, + TabBarColors, TextStyle, VisualBellTarget, }; use euclid::num::Zero; use mux::pane::{Pane, PaneId, WithPaneLines}; @@ -3738,7 +3738,7 @@ fn resolve_fg_color_attr( } } wezterm_term::color::ColorAttribute::PaletteIndex(idx) - if idx < 8 && config.bold_brightens_ansi_colors => + if idx < 8 && config.bold_brightens_ansi_colors != BoldBrightening::No => { // For compatibility purposes, switch to a brighter version // of one of the standard ANSI colors when Bold is enabled.