mirror of
https://github.com/kovidgoyal/kitty.git
synced 2026-11-20 15:12:37 +00:00
Fix glyphs slightly wider than their cell being rendered smaller
A glyph whose bitmap is wider than its cell is rendered again at a smaller font size so that it fits, unless it is wider by no more than two pixels, in which case it is cropped. That allowance is absolute, so it covers less of the cell as the cell grows, and fonts with glyphs that overhang their advance by design end up on the wrong side of it: the texture healing alternates of Monaspace and 0xProto, or the d with caron of Cascadia Code. Such a glyph is drawn visibly smaller than its neighbors, at one font size and not at the next. Make the allowance proportional to the cell width: 2/9 of the cell, which is the 2px the rule was originally tuned for at the 9px cell of a typical monospace font at 11pt and 96 dpi, with a minimum of 2px for smaller cells. That keeps the widest Monaspace alternate, 14% wider than its cell, at full size. A glyph that overhangs by more, such as a Greek capital whose breathing mark and accent hang well outside the cell, is still rescaled, since cropping would remove the marks. Closes #10879
This commit is contained in:
parent
d6c2ea1160
commit
594c96f660
3 changed files with 50 additions and 4 deletions
|
|
@ -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]
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue