mirror of
https://github.com/kovidgoyal/kitty.git
synced 2026-11-19 22:55:29 +00:00
CoreText: Fix glyphs slightly wider than their cell being rendered smaller
Port the overhang allowance from the FreeType renderer: a glyph wider than its single cell by at most 2/9 of the cell width, with a minimum of 2px, is cropped rather than rescaled. Previously any glyph whose right edge was more than 1px past the cell was rescaled, shrinking glyphs such as the d with caron of Cascadia Code and the texture healing alternates of Monaspace. Widths are measured per glyph, as with FreeType, so that combining marks do not cause their base glyph to be shrunk. The left overhang of a glyph is now taken into account, and a glyph that is rescaled is moved so that its ink starts at the left edge of the cell, so that marks that hang to the left of it, as in polytonic Greek capitals, are no longer cut off. Glyphs that extend past the right edge of the cell are moved left as far as their left bearing allows, as with FreeType. Also fix the tests for this to run on macOS: face_from_path() ignored the font size on macOS and the tests treated pixels with only color bits set as ink. Fix concat_cells() for 32 bit sprites, broken since the SIMD compositing change, which treated the 0xRRGGBBAA sprite pixels as RGBA bytes, producing an opaque white image.
This commit is contained in:
parent
594c96f660
commit
fd795df1ff
4 changed files with 111 additions and 43 deletions
|
|
@ -252,10 +252,13 @@ 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
|
||||
- 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
|
||||
|
||||
- macOS: Fix accents and breathing marks that hang to the left of wide glyphs,
|
||||
such as polytonic Greek capitals, being cut off
|
||||
|
||||
|
||||
0.50.0 [2026-11-03]
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
|
|
|||
|
|
@ -755,12 +755,12 @@ face_from_descriptor(PyObject *descriptor, FONTS_DATA_HANDLE fg) {
|
|||
}
|
||||
|
||||
PyObject *
|
||||
face_from_path(const char *path, int UNUSED index, FONTS_DATA_HANDLE fg UNUSED) {
|
||||
face_from_path(const char *path, int UNUSED index, FONTS_DATA_HANDLE fg) {
|
||||
RAII_CoreFoundation(CFStringRef, s, CFStringCreateWithCString(NULL, path, kCFStringEncodingUTF8));
|
||||
RAII_CoreFoundation(CFURLRef, url, CFURLCreateWithFileSystemPath(kCFAllocatorDefault, s, kCFURLPOSIXPathStyle, false));
|
||||
RAII_CoreFoundation(CGDataProviderRef, dp, CGDataProviderCreateWithURL(url));
|
||||
RAII_CoreFoundation(CGFontRef, cg_font, CGFontCreateWithDataProvider(dp));
|
||||
RAII_CoreFoundation(CTFontRef, ct_font, CTFontCreateWithGraphicsFont(cg_font, 0.0, NULL, NULL));
|
||||
RAII_CoreFoundation(CTFontRef, ct_font, CTFontCreateWithGraphicsFont(cg_font, fg ? scaled_point_sz(fg) : 0.0, NULL, NULL));
|
||||
return (PyObject *)ct_face(ct_font, NULL);
|
||||
}
|
||||
|
||||
|
|
@ -1185,14 +1185,51 @@ do_render(
|
|||
ensure_render_space(canvas_width, cell_height, num_glyphs);
|
||||
CGRect br = CTFontGetBoundingRectsForGlyphs(ct_font, kCTFontOrientationHorizontal, buffers.glyphs, buffers.boxes, num_glyphs);
|
||||
const bool debug_rendering = false;
|
||||
if (allow_resize) {
|
||||
// Resize glyphs that would bleed into neighboring cells, by scaling the font size
|
||||
float right = 0;
|
||||
for (unsigned i = 0; i < num_glyphs; i++) right = MAX(right, buffers.boxes[i].origin.x + buffers.boxes[i].size.width);
|
||||
if (!bold && !italic && right > canvas_width + 1) {
|
||||
if (debug_rendering) printf("resizing glyphs, right: %f canvas_width: %u\n", right, canvas_width);
|
||||
CGFloat x = 0, y = 0;
|
||||
CGFloat scale = CTFontGetSize(ct_font) / units_per_em;
|
||||
for (unsigned i = 0; i < num_glyphs; i++) {
|
||||
buffers.positions[i].x = x + hb_positions[i].x_offset * scale;
|
||||
buffers.positions[i].y = y + hb_positions[i].y_offset * scale;
|
||||
if (debug_rendering)
|
||||
printf(
|
||||
"x=%f y=%f origin=%f width=%f x_advance=%f x_offset=%f y_advance=%f y_offset=%f\n",
|
||||
buffers.positions[i].x,
|
||||
buffers.positions[i].y,
|
||||
buffers.boxes[i].origin.x,
|
||||
buffers.boxes[i].size.width,
|
||||
hb_positions[i].x_advance * scale,
|
||||
hb_positions[i].x_offset * scale,
|
||||
hb_positions[i].y_advance * scale,
|
||||
hb_positions[i].y_offset * scale);
|
||||
x += hb_positions[i].x_advance * scale;
|
||||
y += hb_positions[i].y_advance * scale;
|
||||
}
|
||||
// The horizontal extent of the ink of the glyphs, as positioned in the
|
||||
// canvas, and the width of the widest glyph
|
||||
CGFloat left = canvas_width, right = 0, max_width = 0;
|
||||
for (unsigned i = 0; i < num_glyphs; i++) {
|
||||
if (buffers.boxes[i].size.width <= 0) continue;
|
||||
left = MIN(left, buffers.positions[i].x + buffers.boxes[i].origin.x);
|
||||
right = MAX(right, buffers.positions[i].x + buffers.boxes[i].origin.x + buffers.boxes[i].size.width);
|
||||
max_width = MAX(max_width, buffers.boxes[i].size.width);
|
||||
}
|
||||
CGFloat dx = 0;
|
||||
if (right > left) {
|
||||
// Glyphs are checked individually, so that combining marks hanging
|
||||
// outside the cell dont cause the base glyph to be shrunk
|
||||
CGFloat extra = max_width - canvas_width;
|
||||
// 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
|
||||
// glyph to the cell. The allowance is 2/9 of the cell width, with a
|
||||
// minimum of 2px, the same as in the FreeType renderer.
|
||||
// See https://github.com/kovidgoyal/kitty/issues/352
|
||||
const bool crop = num_cells == 1 && (extra <= 2 || extra * 9 <= cell_width * 2);
|
||||
if (allow_resize && !bold && !italic && extra > 1 && !crop) {
|
||||
// Resize glyphs that would bleed into neighboring cells, by scaling the font size
|
||||
if (debug_rendering) printf("resizing glyphs, max_width: %f canvas_width: %u\n", max_width, canvas_width);
|
||||
CGFloat sz = CTFontGetSize(ct_font);
|
||||
sz *= canvas_width / right;
|
||||
sz *= canvas_width / max_width;
|
||||
CTFontRef new_font = CTFontCreateCopyWithAttributes(ct_font, sz, NULL, NULL);
|
||||
bool ret = do_render(
|
||||
new_font,
|
||||
|
|
@ -1214,25 +1251,18 @@ do_render(
|
|||
CFRelease(new_font);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
CGFloat x = 0, y = 0;
|
||||
CGFloat scale = CTFontGetSize(ct_font) / units_per_em;
|
||||
for (unsigned i = 0; i < num_glyphs; i++) {
|
||||
buffers.positions[i].x = x + hb_positions[i].x_offset * scale;
|
||||
buffers.positions[i].y = y + hb_positions[i].y_offset * scale;
|
||||
if (debug_rendering)
|
||||
printf(
|
||||
"x=%f y=%f origin=%f width=%f x_advance=%f x_offset=%f y_advance=%f y_offset=%f\n",
|
||||
buffers.positions[i].x,
|
||||
buffers.positions[i].y,
|
||||
buffers.boxes[i].origin.x,
|
||||
buffers.boxes[i].size.width,
|
||||
hb_positions[i].x_advance * scale,
|
||||
hb_positions[i].x_offset * scale,
|
||||
hb_positions[i].y_advance * scale,
|
||||
hb_positions[i].y_offset * scale);
|
||||
x += hb_positions[i].x_advance * scale;
|
||||
y += hb_positions[i].y_advance * scale;
|
||||
// Colored glyphs are placed by render_color_glyph()
|
||||
if (!*was_colored) {
|
||||
if (!allow_resize && left < 0) {
|
||||
// A rescaled glyph, such as a letter with marks that hang to
|
||||
// the left of its cell, shift it so the marks are not cut off
|
||||
dx = -left;
|
||||
} else if (right > canvas_width && left > 0 && num_glyphs < 4) {
|
||||
// Move the glyphs back into the canvas, as far as their left bearing allows
|
||||
dx = -MIN(left, right - canvas_width);
|
||||
}
|
||||
for (unsigned i = 0; i < num_glyphs; i++) buffers.positions[i].x += dx;
|
||||
}
|
||||
}
|
||||
if (*was_colored) {
|
||||
render_color_glyph(ct_font, (uint8_t *)canvas, info[0].codepoint, cell_width * num_cells, cell_height, baseline);
|
||||
|
|
@ -1245,7 +1275,7 @@ do_render(
|
|||
ri->rendered_width = (unsigned)ceil(x);
|
||||
ri->x = 0;
|
||||
// FiraCode ligatures result in negative origins
|
||||
if (br.origin.x > 0) ri->x = (int)br.origin.x;
|
||||
if (br.origin.x + dx > 0) ri->x = (int)(br.origin.x + dx);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -2670,13 +2670,29 @@ concat_cells(PyObject UNUSED *self, PyObject *args) {
|
|||
PyObject *ans = PyBytes_FromStringAndSize(NULL, (size_t)4 * cell_width * cell_height * num_cells);
|
||||
if (ans == NULL) return PyErr_NoMemory();
|
||||
pixel *dest = (pixel *)PyBytes_AS_STRING(ans);
|
||||
RAII_ALLOC(uint8_t, rgba, is_32_bit ? malloc((size_t)4 * cell_width) : NULL);
|
||||
if (is_32_bit && !rgba) {
|
||||
Py_DECREF(ans);
|
||||
return PyErr_NoMemory();
|
||||
}
|
||||
for (r = 0; r < cell_height; r++) {
|
||||
for (c = 0; c < num_cells; c++) {
|
||||
void *s = ((uint8_t *)PyBytes_AS_STRING(PyTuple_GET_ITEM(cells, c)));
|
||||
if (is_32_bit) {
|
||||
// sprite pixels are 0xRRGGBBAA, convert them to RGBA bytes
|
||||
pixel *src = (pixel *)s + cell_width * r;
|
||||
for (i = 0; i < cell_width; i++) dest[i] = (pixel)bgcolor;
|
||||
blend_over_opaque((uint8_t *)dest, 4, (const uint8_t *)src, cell_width);
|
||||
uint8_t *d = (uint8_t *)dest;
|
||||
for (i = 0; i < cell_width; i++) {
|
||||
rgba[4 * i] = (src[i] >> 24) & 0xff;
|
||||
rgba[4 * i + 1] = (src[i] >> 16) & 0xff;
|
||||
rgba[4 * i + 2] = (src[i] >> 8) & 0xff;
|
||||
rgba[4 * i + 3] = src[i] & 0xff;
|
||||
d[4 * i] = (bgcolor >> 16) & 0xff;
|
||||
d[4 * i + 1] = (bgcolor >> 8) & 0xff;
|
||||
d[4 * i + 2] = bgcolor & 0xff;
|
||||
d[4 * i + 3] = 0xff;
|
||||
}
|
||||
blend_over_opaque(d, 4, rgba, cell_width);
|
||||
dest += cell_width;
|
||||
} else {
|
||||
uint8_t *src = (uint8_t *)s + cell_width * r;
|
||||
|
|
|
|||
|
|
@ -505,19 +505,31 @@ 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"
|
||||
def test_concat_cells(self):
|
||||
from kitty.fast_data_types import concat_cells
|
||||
|
||||
# sprite pixels are 0xRRGGBBAA and the result is opaque RGBA bytes
|
||||
cell = array.array('I', (0xFF000080, 0xFFFFFF00, 0x00FF00FF)).tobytes()
|
||||
self.ae(concat_cells(3, 1, True, (cell,)), bytes((0x80, 0, 0, 0xFF, 0, 0, 0, 0xFF, 0, 0xFF, 0, 0xFF)))
|
||||
self.ae(concat_cells(1, 1, True, (cell[:4], cell[4:8])), bytes((0x80, 0, 0, 0xFF, 0, 0, 0, 0xFF)))
|
||||
self.ae(concat_cells(2, 1, True, (cell[:8],), 0x0000FF), bytes((0x80, 0, 0x7F, 0xFF, 0, 0, 0xFF, 0xFF)))
|
||||
|
||||
def glyph_ink(self, font_name, char, ppem):
|
||||
"The number of pixel rows and columns char occupies when drawn in a single cell, and the cell width"
|
||||
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
|
||||
# the low byte of each pixel is its alpha, the color bits are set even for empty pixels on macOS
|
||||
alpha = bytes(p & 0xFF for p in array.array('I', sprites[pos]))
|
||||
rows = [y for y in range(cell_height) if any(alpha[y * cell_width : (y + 1) * cell_width])]
|
||||
cols = [x for x in range(cell_width) if any(alpha[x::cell_width])]
|
||||
height = rows[-1] - rows[0] + 1 if rows else 0
|
||||
width = cols[-1] - cols[0] + 1 if cols else 0
|
||||
return height, width, cell_width
|
||||
|
||||
@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
|
||||
|
|
@ -532,15 +544,22 @@ class Rendering(FontBaseTest):
|
|||
('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)
|
||||
self.assertGreaterEqual(self.glyph_ink(font_name, char, ppem)[0], 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)
|
||||
# it is at most 1766 * (1229 / 1594) * 32 / 2048 = 21.3 pixels tall,
|
||||
# which, depending on where it falls relative to the pixel grid, covers
|
||||
# at most 23 rows. Drawn at full size it covers at least 27.
|
||||
height, width, cell_width = self.glyph_ink('LiberationMono-Regular.ttf', 'Ὦ', 32)
|
||||
self.assertLessEqual(height, 23)
|
||||
if is_macos:
|
||||
# The rescaled glyph is moved into the cell so that the marks are
|
||||
# not cut off and it spans the whole cell. The FreeType renderer
|
||||
# still crops whatever lies to the left of the cell.
|
||||
self.assertGreaterEqual(width, cell_width - 1)
|
||||
|
||||
def test_shaping(self):
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue