diff --git a/docs/changelog.rst b/docs/changelog.rst index ad8dec896..5530312ac 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -252,6 +252,10 @@ Detailed list of changes - X11: Report X errors instead of exiting, fixing exits when clipboard requestors destroy their window early (:iss:`9248`) +- Linux: Fix glyphs that are slightly wider than their cell, such as the + texture healing alternates of the Monaspace and 0xProto fonts, being rendered + smaller than the surrounding text at some font sizes + 0.50.0 [2026-11-03] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/kitty/freetype.c b/kitty/freetype.c index 2ae303190..48bd99788 100644 --- a/kitty/freetype.c +++ b/kitty/freetype.c @@ -769,10 +769,15 @@ render_bitmap( size_t extra = ans->width - max_width; if (italic && extra < cell_width / 2) { trim_borders(ans, extra); - } else if (extra == 2 && num_cells == 1) { - // there exist fonts that have bitmaps just a couple of pixels - // wider than their advances, rather than rescale, which looks - // bad, we just crop the bitmap on the right. See https://github.com/kovidgoyal/kitty/issues/352 + } else if (num_cells == 1 && (extra <= 2 || extra * 9 <= cell_width * 2)) { + // there exist fonts that have glyphs a little wider than their + // advances, such as wide letters that borrow space from a narrow + // neighbor. Rather than rescale, which looks bad, we just crop the + // bitmap to the cell. The allowance is 2/9 of the cell width, + // which is 2px for the 9px cell of a typical monospace font at + // 11pt and 96 dpi, the conditions this was originally tuned for, + // with a minimum of 2px for smaller cells. + // See https://github.com/kovidgoyal/kitty/issues/352 } else if (rescale && self->is_scalable && extra > 1) { FT_F26Dot6 char_width = self->char_width, char_height = self->char_height; float ar = (float)max_width / (float)ans->width; diff --git a/kitty_tests/fonts.py b/kitty_tests/fonts.py index 60df45a5d..ad82acd7f 100644 --- a/kitty_tests/fonts.py +++ b/kitty_tests/fonts.py @@ -505,6 +505,43 @@ class Rendering(FontBaseTest): _, rw, rh = without_matrix.render_codepoint(0x1F40D) self.assertGreater(mh, 0.5 * rh, f'color emoji shrunk by FC_MATRIX: {mh}px vs {rh}px (#10144)') + def glyph_ink_height(self, font_name, char, ppem): + "The number of pixel rows char occupies when drawn in a single cell" + with setup_for_testing(size=ppem, dpi=72.0, main_face_path=self.path_for_font(font_name)) as (sprites, cell_width, cell_height): + s = Screen(None, 1, 4) + s.draw(char) + line = s.line(0) + test_render_line(line) + pos = sprite_idx_to_pos(line.sprite_at(0) & 0x7FFFFFFF, setup_for_testing.xnum, setup_for_testing.ynum) + pixels = array.array('I', sprites[pos]) + rows = [y for y in range(cell_height) if any(pixels[y * cell_width : (y + 1) * cell_width])] + return rows[-1] - rows[0] + 1 if rows else 0 + + @unittest.skipIf(is_macos, 'The rule under test is in the FreeType renderer') + def test_small_glyph_overhang_is_not_rescaled(self): + # Fonts have glyphs whose outlines overhang their advance a little, such + # as the texture healing alternates of Monaspace and 0xProto. These must + # be drawn at full size, not shrunk to fit the cell. The expected height + # is the height of the glyph's bounding box in font units, times + # ppem / units per em, rounded down. + for font_name, char, ppem, full_height in ( + ('CascadiaCode-Regular.otf', 'ď', 40, 31), # 1588 * 40 / 2048, outline is 7% wider than the cell + ('LiberationMono-Regular.ttf', 'Ƴ', 32, 21), # 1370 * 32 / 2048, 10% wider + ('FiraCode-Medium.otf', 'œ', 64, 36), # 1111 * 64 / 1950, 3% wider + # 935 * 120 / 1000, 15% wider, like the widest texture healing alternates of Monaspace + ('iosevka-regular.ttf', 'ꭦ', 120, 112), + ): + with self.subTest(font=font_name, char=char): + self.assertGreaterEqual(self.glyph_ink_height(font_name, char, ppem), full_height) + + @unittest.skipIf(is_macos, 'The rule under test is in the FreeType renderer') + def test_large_glyph_overhang_is_rescaled(self): + # Ὦ has its breathing mark and accent to the left of the letter, outside + # its cell, so cropping would cut them off. Its outline is 1594 units + # wide and 1766 tall with an advance of 1229, so scaled to fit the cell + # it is at most 1766 * (1229 / 1594) * 32 / 2048 = 21.3 pixels tall. + self.assertLessEqual(self.glyph_ink_height('LiberationMono-Regular.ttf', 'Ὦ', 32), 22) + def test_shaping(self): def ss(text, font=None):