Function to easily test decoration rendering

This commit is contained in:
Kovid Goyal
2025-02-10 16:31:03 +05:30
parent 32e34f3866
commit 23097ee184
3 changed files with 23 additions and 1 deletions

View File

@@ -1724,6 +1724,7 @@ def buffer_keys_in_window(os_window_id: int, tab_id: int, window_id: int, enable
def sprite_idx_to_pos(idx: int, xnum: int, ynum: int) -> tuple[int, int, int]: ...
def render_box_char(ch: int, width: int, height: int, scale: float = 1.0, dpi_x: float = 96.0, dpi_y: float = 96.0) -> bytes: ...
def run_at_exit_cleanup_functions() -> None: ...
def render_decoration(which: str, cell_width: int, cell_height: int, underline_position: int, underline_thickness: int) -> bytes: ...
class MousePosition(TypedDict):
cell_x: int

View File

@@ -2107,6 +2107,18 @@ alpha_blend(uint32_t fg, uint32_t bg) {
return (0xff000000) | (r << 16) | (g << 8) | b;
}
static PyObject*
render_decoration(PyObject *self UNUSED, PyObject *args) {
const char *which;
FontCellMetrics fcm = {0};
if (!PyArg_ParseTuple(args, "sIIII", &which, &fcm.cell_width, &fcm.cell_height, &fcm.underline_position, &fcm.underline_thickness)) return NULL;
PyObject *ans = PyBytes_FromStringAndSize(NULL, fcm.cell_width * fcm.cell_height);
if (!ans) return NULL;
memset(PyBytes_AS_STRING(ans), 0, PyBytes_GET_SIZE(ans));
if (strcmp(which, "curl") == 0) add_curl_underline((uint8_t*)PyBytes_AS_STRING(ans), fcm);
return ans;
}
static PyObject*
concat_cells(PyObject UNUSED *self, PyObject *args) {
// Concatenate cells returning RGBA data
@@ -2334,6 +2346,7 @@ static PyMethodDef module_methods[] = {
METHODB(sprite_map_set_layout, METH_VARARGS),
METHODB(test_sprite_position_increment, METH_NOARGS),
METHODB(concat_cells, METH_VARARGS),
METHODB(render_decoration, METH_VARARGS),
METHODB(set_send_sprite_to_gpu, METH_O),
METHODB(set_allow_use_of_box_fonts, METH_O),
METHODB(test_shape, METH_VARARGS),

View File

@@ -10,9 +10,11 @@ from typing import TYPE_CHECKING, Any, Literal, Union
from kitty.constants import fonts_dir, is_macos
from kitty.fast_data_types import (
Screen,
concat_cells,
create_test_font_group,
current_fonts,
get_fallback_font,
render_decoration,
set_builtin_nerd_font,
set_font_data,
set_options,
@@ -319,7 +321,6 @@ def test_render_string(
size: float = 64.0,
dpi: float = 96.0
) -> None:
from kitty.fast_data_types import concat_cells, current_fonts
cell_width, cell_height, cells = render_string(text, family, size, dpi)
rgb_data = concat_cells(cell_width, cell_height, True, tuple(cells))
@@ -373,3 +374,10 @@ def test_render_codepoint(chars: str = '😺', path: str = '/t/Noto-COLRv1.ttf',
print('Rendered:', char)
display_bitmap(bitmap, w, h)
print('\n')
def test_render_decoration(which: Literal['curl'], cell_width: int, cell_height: int, underline_position: int, underline_thickness: int) -> None:
buf = render_decoration(which, cell_width, cell_height, underline_position, underline_thickness)
cells = buf, buf, buf, buf, buf
rgb_data = concat_cells(cell_width, cell_height, False, cells)
display_bitmap(rgb_data, cell_width * len(cells), cell_height)