diff --git a/docs/changelog.rst b/docs/changelog.rst index bb8553be1..621dc29f5 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -320,6 +320,10 @@ Detailed list of changes :code:`wheel_right` buttons, for example, to change the font size with :kbd:`Ctrl+scroll` +- Fix bold or italic Nerd Font icons sometimes being rendered using glyphs + from an unrelated font that happens to cover the character, for example in + the bold italic title of the active tab + - Fix closing an unfocused window that comes before the focused window in the layout sometimes moving focus to a different window diff --git a/kitty/fonts.c b/kitty/fonts.c index bc4f60db4..d38ad2db7 100644 --- a/kitty/fonts.c +++ b/kitty/fonts.c @@ -774,11 +774,41 @@ chars_as_utf8(const ListOfChars *lc, char *buf, size_t bufsz, char_type zero_cha return n; } +static bool +is_nerd_font_codepoint(char_type ch) { + // The Private Use Area ranges of the glyph sets in Nerd Fonts + switch (ch) { + START_ALLOW_CASE_RANGE + case 0xe000 ... 0xe00a: // Pomicons + case 0xe0a0 ... 0xe0d7: // Powerline and Powerline Extra + case 0xe200 ... 0xe2a9: // Font Awesome Extension + case 0xe300 ... 0xe3e3: // Weather + case 0xe5fa ... 0xe6b7: // Seti-UI and Custom + case 0xe700 ... 0xe8ef: // Devicons + case 0xea60 ... 0xec1e: // Codicons + case 0xed00 ... 0xf2ff: // Font Awesome + case 0xf300 ... 0xf381: // Font Logos + case 0xf400 ... 0xf533: // Octicons + case 0xf0001 ... 0xf1af0: // Material Design + return true; + END_ALLOW_CASE_RANGE + } + return false; +} + static ssize_t fallback_font(FontGroup *fg, const CPUCell *cpu_cell, const GPUCell *gpu_cell, const ListOfChars *lc) { bool bold = gpu_cell->attrs.bold; bool italic = gpu_cell->attrs.italic; bool emoji_presentation = has_emoji_presentation(cpu_cell, lc); + // Nerd Font icons are private use codepoints, whose glyph depends on the + // font, not on the style. Looking for a bold or italic font covering one + // tends to find an unrelated text font that happens to cover it, for + // example Liberation Serif Italic for the check mark U+f00c. So ignore the + // style, which also renders such icons the same way in every style. Other + // private use codepoints are left alone, as they may be used by fonts + // that do have styled variants. + if ((bold || italic) && is_nerd_font_codepoint(lc->chars[0])) bold = italic = false; char style = emoji_presentation ? 'a' : 'A'; if (bold) style += italic ? 3 : 2; else style += italic ? 1 : 0; diff --git a/kitty_tests/fonts.py b/kitty_tests/fonts.py index c095d3be4..d0907f8e7 100644 --- a/kitty_tests/fonts.py +++ b/kitty_tests/fonts.py @@ -615,6 +615,18 @@ class Rendering(FontBaseTest): else: self.skipTest('No font on this system has a fallback font for any of the tested codepoints') + def test_private_use_fallback_font_ignores_style(self): + # private use codepoints such as Nerd Font icons must use the same + # fallback font in every style, rather than whatever unrelated bold or + # italic font happens to cover the codepoint + for text in '\uf00c\uf071\uf110\ue654': + try: + regular = get_fallback_font(text, False, False) + except ValueError: + continue + for bold, italic in ((True, False), (False, True), (True, True)): + self.ae(get_fallback_font(text, bold, italic).postscript_name(), regular.postscript_name(), f'{text!r} {bold=} {italic=}') + @unittest.skipIf(is_macos, 'fontconfig is not used on macOS') def test_fallback_font_matching(self): # Fallback font matching narrows the candidates down using a cached,