1
1
mirror of https://github.com/wez/wezterm.git synced 2024-12-22 21:01:36 +03:00

fonts: improve font_rules behavior

We now always append the synthetic rules to the those in the config,
and include rules for generating half-bright font selection.
This commit is contained in:
Wez Furlong 2021-04-15 09:39:29 -07:00
parent d5886e6297
commit 1b12b0a23d
5 changed files with 185 additions and 30 deletions

View File

@ -129,6 +129,10 @@ impl FontWeight {
}
}
pub fn lighter(self) -> Self {
Self::from_opentype_weight(self.to_opentype_weight().saturating_sub(200))
}
pub fn bolder(self) -> Self {
Self::from_opentype_weight(self.to_opentype_weight() + 200)
}
@ -412,6 +416,22 @@ impl TextStyle {
}
}
pub fn make_half_bright(&self) -> Self {
Self {
foreground: self.foreground,
font: self
.font
.iter()
.map(|attr| {
let mut attr = attr.clone();
attr.weight = attr.weight.lighter();
attr.is_synthetic = true;
attr
})
.collect(),
}
}
/// Make a version of this style with italic enabled.
pub fn make_italic(&self) -> Self {
Self {

View File

@ -1385,40 +1385,51 @@ impl Config {
}
}
if cfg.font_rules.is_empty() {
// Expand out some reasonable default font rules
let reduced = self.font.reduce_first_font_to_family();
let mut bold = reduced.make_bold();
let mut italic = reduced.make_italic();
let mut bold_italic = bold.make_italic();
// Add some reasonable default font rules
let reduced = self.font.reduce_first_font_to_family();
// add the reduced version of the font as a fallback right after
// the bold/italic variants. Some users have installed just the
// regular weight variant of a font and we'd like to fall back
// to that weight in that case, rather than show them an error
bold.font.insert(1, reduced.font[0].clone());
italic.font.insert(1, reduced.font[0].clone());
bold_italic.font.insert(1, reduced.font[0].clone());
let italic = reduced.make_italic();
cfg.font_rules.push(StyleRule {
italic: Some(true),
font: italic,
..Default::default()
});
let bold = reduced.make_bold();
let bold_italic = bold.make_italic();
cfg.font_rules.push(StyleRule {
intensity: Some(wezterm_term::Intensity::Bold),
font: bold,
..Default::default()
});
let half_bright = reduced.make_half_bright();
let half_bright_italic = half_bright.make_italic();
cfg.font_rules.push(StyleRule {
italic: Some(true),
intensity: Some(wezterm_term::Intensity::Bold),
font: bold_italic,
..Default::default()
});
}
cfg.font_rules.push(StyleRule {
italic: Some(true),
intensity: Some(wezterm_term::Intensity::Half),
font: half_bright_italic,
..Default::default()
});
cfg.font_rules.push(StyleRule {
italic: Some(false),
intensity: Some(wezterm_term::Intensity::Half),
font: half_bright,
..Default::default()
});
cfg.font_rules.push(StyleRule {
italic: Some(false),
intensity: Some(wezterm_term::Intensity::Bold),
font: bold,
..Default::default()
});
cfg.font_rules.push(StyleRule {
italic: Some(true),
intensity: Some(wezterm_term::Intensity::Bold),
font: bold_italic,
..Default::default()
});
cfg.font_rules.push(StyleRule {
italic: Some(true),
intensity: Some(wezterm_term::Intensity::Normal),
font: italic,
..Default::default()
});
// Load any additional color schemes into the color_schemes map
cfg.load_color_schemes(&cfg.compute_color_scheme_dirs())

View File

@ -26,6 +26,7 @@ As features stabilize some brief notes about them will accumulate here.
* Fixed: half-pixel gaps in ligatured/double-wide glyphs depending on the font size [#614](https://github.com/wez/wezterm/issues/614)
* Fixed: Window could vanish if a tab closed while the rightmost tab was active(!) [#690](https://github.com/wez/wezterm/issues/690)
* Fixed: macOS: mouse cursor could get stuck in the hidden state. [#618](https://github.com/wez/wezterm/issues/618)
* Improved: [font_rules](config/lua/config/font_rules.md) behavior to always append reasonable default `font_rules` to those that you may have specified in your config. `font_rules` now also include defaults for half-bright text styles.
### 20210405-110924-a5bb5be8

View File

@ -86,3 +86,118 @@ return {
},
}
```
*Since: nightly builds only*
The default `font_rules` generated by wezterm are now always appended to the
rules that you've supplied (rather than used only if you didn't specify any),
and now also include rules for selecting lighter weight fonts when half-bright
text is rendered to the terminal.
That means that you shouldn't need to specify as many rules as in previously
releases.
You can run `wezterm ls-fonts` to summarize the font rules and the fonts that
match them:
```bash
$ wezterm ls-fonts
Primary font:
wezterm.font_with_fallback({
-- <built-in>, BuiltIn
"JetBrains Mono",
-- /home/wez/.fonts/NotoColorEmoji.ttf, FontConfig
"Noto Color Emoji",
-- <built-in>, BuiltIn
"Last Resort High-Efficiency",
})
When Intensity=Half Italic=true:
wezterm.font_with_fallback({
-- <built-in>, BuiltIn
{family="JetBrains Mono", weight="ExtraLight", italic=true},
-- /home/wez/.fonts/NotoColorEmoji.ttf, FontConfig
"Noto Color Emoji",
-- <built-in>, BuiltIn
"JetBrains Mono",
-- <built-in>, BuiltIn
"Last Resort High-Efficiency",
})
When Intensity=Half Italic=false:
wezterm.font_with_fallback({
-- <built-in>, BuiltIn
{family="JetBrains Mono", weight="ExtraLight"},
-- /home/wez/.fonts/NotoColorEmoji.ttf, FontConfig
"Noto Color Emoji",
-- <built-in>, BuiltIn
"JetBrains Mono",
-- <built-in>, BuiltIn
"Last Resort High-Efficiency",
})
When Intensity=Bold Italic=false:
wezterm.font_with_fallback({
-- <built-in>, BuiltIn
{family="JetBrains Mono", weight="Bold"},
-- /home/wez/.fonts/NotoColorEmoji.ttf, FontConfig
"Noto Color Emoji",
-- <built-in>, BuiltIn
"JetBrains Mono",
-- <built-in>, BuiltIn
"Last Resort High-Efficiency",
})
When Intensity=Bold Italic=true:
wezterm.font_with_fallback({
-- <built-in>, BuiltIn
{family="JetBrains Mono", weight="Bold", italic=true},
-- /home/wez/.fonts/NotoColorEmoji.ttf, FontConfig
"Noto Color Emoji",
-- <built-in>, BuiltIn
"JetBrains Mono",
-- <built-in>, BuiltIn
"Last Resort High-Efficiency",
})
When Intensity=Normal Italic=true:
wezterm.font_with_fallback({
-- <built-in>, BuiltIn
{family="JetBrains Mono", italic=true},
-- /home/wez/.fonts/NotoColorEmoji.ttf, FontConfig
"Noto Color Emoji",
-- <built-in>, BuiltIn
"JetBrains Mono",
-- <built-in>, BuiltIn
"Last Resort High-Efficiency",
})
```

8
test-data/styles.txt Normal file
View File

@ -0,0 +1,8 @@
Regular
Bold
Italic
Bold Italic
Half Bright
Half Bright Italic