From 23097ee1846946913516475e926ec6fa4d0e40b8 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 10 Feb 2025 16:31:03 +0530 Subject: [PATCH] Function to easily test decoration rendering --- kitty/fast_data_types.pyi | 1 + kitty/fonts.c | 13 +++++++++++++ kitty/fonts/render.py | 10 +++++++++- 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/kitty/fast_data_types.pyi b/kitty/fast_data_types.pyi index 80d50383e..b22e6bd66 100644 --- a/kitty/fast_data_types.pyi +++ b/kitty/fast_data_types.pyi @@ -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 diff --git a/kitty/fonts.c b/kitty/fonts.c index e2e5a420f..6ea6eb6ee 100644 --- a/kitty/fonts.c +++ b/kitty/fonts.c @@ -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), diff --git a/kitty/fonts/render.py b/kitty/fonts/render.py index 1a20e6cb4..1254d4663 100644 --- a/kitty/fonts/render.py +++ b/kitty/fonts/render.py @@ -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)