fonts: allow specifying weight/stretch/italic for each fallback font
This commit is contained in:
1 parent
fcd7ceef5c
commit
776aedf97e
7 files changed
+96
-26
No files matched your search
+1
-1
@@ -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)
|
||||
|
||||
+58
-22
@@ -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<Self, mlua::Error> {
|
||||
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<TextStyleAttributes>),
|
||||
(mut attrs, map_defaults): (LuaFontAttributes, Option<TextStyleAttributes>),
|
||||
) -> mlua::Result<TextStyle> {
|
||||
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<String>, Option<TextStyleAttributes>),
|
||||
(fallback, map_defaults): (Vec<LuaFontAttributes>, Option<TextStyleAttributes>),
|
||||
) -> mlua::Result<TextStyle> {
|
||||
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)
|
||||
}
|
||||
|
||||
+1
-1
@@ -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)
|
||||
|
||||
@@ -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"}),
|
||||
}
|
||||
```
|
||||
|
||||
@@ -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"),
|
||||
}
|
||||
```
|
||||
|
||||
@@ -303,7 +303,7 @@ impl FontConfigInner {
|
||||
.collect::<String>();
|
||||
|
||||
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",
|
||||
|
||||
@@ -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:");
|
||||
|
||||
Reference in new issue
Block a user