From 776aedf97e66c8f0a07ebe17faa904c4e14a9cfd Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Mon, 12 Apr 2021 22:30:55 -0700 Subject: [PATCH] fonts: allow specifying weight/stretch/italic for each fallback font --- config/src/lib.rs | 2 +- config/src/lua.rs | 80 ++++++++++++++----- docs/changelog.md | 2 +- docs/config/lua/wezterm/font.md | 14 ++++ docs/config/lua/wezterm/font_with_fallback.md | 16 ++++ wezterm-font/src/lib.rs | 4 +- wezterm-gui/src/main.rs | 4 + 7 files changed, 96 insertions(+), 26 deletions(-) diff --git a/config/src/lib.rs b/config/src/lib.rs index 4f6dee3bf..191f7f2ab 100644 --- a/config/src/lib.rs +++ b/config/src/lib.rs @@ -252,7 +252,7 @@ pub fn assign_error_callback(cb: ErrorCallback) { factory.replace(cb); } -fn show_error(err: &str) { +pub fn show_error(err: &str) { let factory = SHOW_ERROR.lock().unwrap(); if let Some(cb) = factory.as_ref() { cb(err) diff --git a/config/src/lua.rs b/config/src/lua.rs index 30a9ca44c..9ebe73078 100644 --- a/config/src/lua.rs +++ b/config/src/lua.rs @@ -2,7 +2,7 @@ use crate::{FontAttributes, FontStretch, FontWeight, TextStyle}; use anyhow::anyhow; use bstr::BString; pub use luahelper::*; -use mlua::ToLua; +use mlua::{FromLua, ToLua}; use mlua::{Lua, Table, Value}; use serde::*; use smol::prelude::*; @@ -350,6 +350,32 @@ struct TextStyleAttributes { } impl_lua_conversion!(TextStyleAttributes); +#[derive(Debug, Default, Serialize, Deserialize, Clone, PartialEq, Eq, Hash)] +struct LuaFontAttributes { + /// The font family name + pub family: String, + /// Whether the font should be a bold variant + #[serde(default)] + pub weight: FontWeight, + #[serde(default)] + pub stretch: FontStretch, + /// Whether the font should be an italic variant + #[serde(default)] + pub italic: bool, +} +impl<'lua> FromLua<'lua> for LuaFontAttributes { + fn from_lua(value: Value<'lua>, _lua: &'lua Lua) -> Result { + match value { + Value::String(s) => { + let mut attr = LuaFontAttributes::default(); + attr.family = s.to_str()?.to_string(); + Ok(attr) + } + v => Ok(from_lua_value(v)?), + } + } +} + /// Given a simple font family name, returns a text style instance. /// The second optional argument is a list of the other TextStyle /// fields, which at the time of writing includes only the @@ -361,25 +387,30 @@ impl_lua_conversion!(TextStyleAttributes); /// `{ font = {{ family = "foo" }}, foreground="tomato"}` fn font<'lua>( _lua: &'lua Lua, - (family, map_defaults): (String, Option), + (mut attrs, map_defaults): (LuaFontAttributes, Option), ) -> mlua::Result { - let attrs = map_defaults.unwrap_or_else(TextStyleAttributes::default); let mut text_style = TextStyle::default(); - text_style.font.clear(); - text_style.font.push(FontAttributes { - family, - stretch: attrs.stretch, - weight: match attrs.bold { + + if let Some(map_defaults) = map_defaults { + attrs.weight = match map_defaults.bold { Some(true) => FontWeight::Bold, Some(false) => FontWeight::Regular, - None => attrs.weight.unwrap_or(FontWeight::Regular), - }, + None => map_defaults.weight.unwrap_or(FontWeight::Regular), + }; + attrs.stretch = map_defaults.stretch; + attrs.italic = map_defaults.italic; + text_style.foreground = map_defaults.foreground; + } + + text_style.font.push(FontAttributes { + family: attrs.family, + stretch: attrs.stretch, + weight: attrs.weight, italic: attrs.italic, is_fallback: false, is_synthetic: false, }); - text_style.foreground = attrs.foreground; Ok(text_style) } @@ -393,27 +424,32 @@ fn font<'lua>( /// as described by the `wezterm.font` documentation. fn font_with_fallback<'lua>( _lua: &'lua Lua, - (fallback, map_defaults): (Vec, Option), + (fallback, map_defaults): (Vec, Option), ) -> mlua::Result { - let attrs = map_defaults.unwrap_or_else(TextStyleAttributes::default); let mut text_style = TextStyle::default(); - text_style.font.clear(); - for (idx, family) in fallback.into_iter().enumerate() { - text_style.font.push(FontAttributes { - family, - stretch: attrs.stretch, - weight: match attrs.bold { + + for (idx, mut attrs) in fallback.into_iter().enumerate() { + if let Some(map_defaults) = &map_defaults { + attrs.weight = match map_defaults.bold { Some(true) => FontWeight::Bold, Some(false) => FontWeight::Regular, - None => attrs.weight.unwrap_or(FontWeight::Regular), - }, + None => map_defaults.weight.unwrap_or(FontWeight::Regular), + }; + attrs.stretch = map_defaults.stretch; + attrs.italic = map_defaults.italic; + text_style.foreground = map_defaults.foreground; + } + + text_style.font.push(FontAttributes { + family: attrs.family, + stretch: attrs.stretch, + weight: attrs.weight, italic: attrs.italic, is_fallback: idx != 0, is_synthetic: false, }); } - text_style.foreground = attrs.foreground; Ok(text_style) } diff --git a/docs/changelog.md b/docs/changelog.md index 8f66a42f9..08d8a4058 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -15,7 +15,7 @@ As features stabilize some brief notes about them will accumulate here. * Fixed: the selection wouldn't always clear when the intersecting lines change [#644](https://github.com/wez/wezterm/issues/644) * Fixed: vertical alignment issue with Iosevka on Windows [#661](https://github.com/wez/wezterm/issues/661) * Fixed: support for "Variable" fonts such as Cascadia Code and Inconsolata on all platforms [#655](https://github.com/wez/wezterm/issues/655) -* New: [wezterm.font](config/lua/wezterm/font.md) and [wezterm.font_with_fallback](config/lua/wezterm/font_with_fallback.md) *attributes* parameter now allows matching more granular font weights and font stretch. e.g.: `wezterm.font('Iosevka Term', {stretch="Expanded", weight="Regular"})` +* New: [wezterm.font](config/lua/wezterm/font.md) and [wezterm.font_with_fallback](config/lua/wezterm/font_with_fallback.md) *attributes* parameter now allows matching more granular font weights and font stretch. e.g.: `wezterm.font('Iosevka Term', {stretch="Expanded", weight="Regular"})`, as fallback can specify weight/stretch/style for each individual font in the fallback. * New: [freetype_render_target](config/lua/config/freetype_render_target.md) option for additional control over glyph rasterization. * Fixed: `wezterm ssh HOST` no longer overrides the `User` config specified by `~/.ssh/config` * Fixed: X11: detect when gnome DPI scaling changes [#667](https://github.com/wez/wezterm/issues/667) diff --git a/docs/config/lua/wezterm/font.md b/docs/config/lua/wezterm/font.md index 7672147d2..0a58c7b5c 100644 --- a/docs/config/lua/wezterm/font.md +++ b/docs/config/lua/wezterm/font.md @@ -57,3 +57,17 @@ return { font = wezterm.font('Iosevka Term', {stretch="Expanded", weight="Regular"}), } ``` + +An alternative form of specifying the font can be used, where the family and the attributes +are combined in the same lua table. This form is most useful when used together with +[wezterm.font_with_fallback](font_with_fallback.md) when you want to specify precise +weights for the different fallback fonts: + +```lua +local wezterm = require 'wezterm'; + +return { + font = wezterm.font({family='Iosevka Term', stretch="Expanded", weight="Regular"}), +} +``` + diff --git a/docs/config/lua/wezterm/font_with_fallback.md b/docs/config/lua/wezterm/font_with_fallback.md index d688c645b..85e26f56a 100644 --- a/docs/config/lua/wezterm/font_with_fallback.md +++ b/docs/config/lua/wezterm/font_with_fallback.md @@ -20,3 +20,19 @@ The *attributes* parameter behaves the same as that of [wezterm.font](font.md) in that it allows you to specify font weight and style attributes that you want to match. +*Since: nightly builds only* + +The attributes can now be specified per fallback font using this alternative +form where the family and attributes are specified as part of the same lua table: + +```lua +local wezterm = require 'wezterm'; + +return { + font = wezterm.font_with_fallback({ + {family="JetBrains Mono", weight="Medium"}, + {family="Terminus", weight="Bold"}, + "Noto Color Emoji"), +} +``` + diff --git a/wezterm-font/src/lib.rs b/wezterm-font/src/lib.rs index 59c6543fb..a70041874 100644 --- a/wezterm-font/src/lib.rs +++ b/wezterm-font/src/lib.rs @@ -303,7 +303,7 @@ impl FontConfigInner { .collect::(); if config.warn_about_missing_glyphs { - mux::connui::show_configuration_error_message(&format!( + config::show_error(&format!( "No fonts contain glyphs for these codepoints: {}.\n\ Placeholder 'Last Resort' glyphs are being displayed instead.\n\ You may wish to install additional fonts, or adjust your\n\ @@ -397,7 +397,7 @@ impl FontConfigInner { ) }; - mux::connui::show_configuration_error_message(&format!( + config::show_error(&format!( "{}. Fallback(s) are being used instead, and the terminal \ may not render as intended{}. See \ https://wezfurlong.org/wezterm/config/fonts.html for more information", diff --git a/wezterm-gui/src/main.rs b/wezterm-gui/src/main.rs index 85d84189f..ac1c4b92c 100644 --- a/wezterm-gui/src/main.rs +++ b/wezterm-gui/src/main.rs @@ -414,6 +414,10 @@ fn maybe_show_configuration_error_window() { } pub fn run_ls_fonts(config: config::ConfigHandle, _cmd: &LsFontsCommand) -> anyhow::Result<()> { + // Disable the normal config error UI window, as we don't have + // a fully baked GUI environment running + config::assign_error_callback(|err| eprintln!("{}", err)); + let font_config = wezterm_font::FontConfiguration::new(Some(config.clone()))?; println!("Primary font:");