From 60601a6dba25750978efb5d1fdb8651d202bbb5b Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 24 Aug 2024 11:12:37 +0530 Subject: [PATCH] Fix #7772 --- kitty/core_text.m | 6 ++++-- kitty/freetype.c | 5 +++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/kitty/core_text.m b/kitty/core_text.m index f75e81935..86fa9415f 100644 --- a/kitty/core_text.m +++ b/kitty/core_text.m @@ -812,11 +812,13 @@ static CTFontRef nerd_font(CGFloat sz) { if (!PyArg_ParseTuple(args, "Ukk|k", &ptext, &canvas_width, &canvas_height, &fg)) return NULL; unsigned int cell_width, cell_height, baseline, underline_position, underline_thickness, strikethrough_position, strikethrough_thickness; cell_metrics((PyObject*)self, &cell_width, &cell_height, &baseline, &underline_position, &underline_thickness, &strikethrough_position, &strikethrough_thickness); + RAII_PyObject(pbuf, PyBytes_FromStringAndSize(NULL, sizeof(pixel) * canvas_width * canvas_height)); + if (!pbuf) return NULL; + memset(PyBytes_AS_STRING(pbuf), 0, PyBytes_GET_SIZE(pbuf)); + if (!cell_width || !cell_height) return Py_BuildValue("OII", pbuf, cell_width, cell_height); size_t num_chars = PyUnicode_GET_LENGTH(ptext); int num_chars_per_line = canvas_width / cell_width, num_of_lines = (int)ceil((float)num_chars / (float)num_chars_per_line); canvas_height = MIN(canvas_height, num_of_lines * cell_height); - RAII_PyObject(pbuf, PyBytes_FromStringAndSize(NULL, sizeof(pixel) * canvas_width * canvas_height)); - if (!pbuf) return NULL; __attribute__((cleanup(destroy_hb_buffer))) hb_buffer_t *hb_buffer = hb_buffer_create(); if (!hb_buffer_pre_allocate(hb_buffer, 4*num_chars)) { PyErr_NoMemory(); return NULL; } diff --git a/kitty/freetype.c b/kitty/freetype.c index eb36fae74..65b62191c 100644 --- a/kitty/freetype.c +++ b/kitty/freetype.c @@ -970,11 +970,12 @@ render_sample_text(Face *self, PyObject *args) { if (!PyArg_ParseTuple(args, "Ukk|k", &ptext, &canvas_width, &canvas_height, &fg)) return NULL; unsigned int cell_width, cell_height, baseline, underline_position, underline_thickness, strikethrough_position, strikethrough_thickness; cell_metrics((PyObject*)self, &cell_width, &cell_height, &baseline, &underline_position, &underline_thickness, &strikethrough_position, &strikethrough_thickness); - int num_chars_per_line = canvas_width / cell_width, num_of_lines = (int)ceil((float)PyUnicode_GET_LENGTH(ptext) / (float)num_chars_per_line); - canvas_height = MIN(canvas_height, num_of_lines * cell_height); RAII_PyObject(pbuf, PyBytes_FromStringAndSize(NULL, sizeof(pixel) * canvas_width * canvas_height)); if (!pbuf) return NULL; memset(PyBytes_AS_STRING(pbuf), 0, PyBytes_GET_SIZE(pbuf)); + if (!cell_width || !cell_height) return Py_BuildValue("OII", pbuf, cell_width, cell_height); + int num_chars_per_line = canvas_width / cell_width, num_of_lines = (int)ceil((float)PyUnicode_GET_LENGTH(ptext) / (float)num_chars_per_line); + canvas_height = MIN(canvas_height, num_of_lines * cell_height); __attribute__((cleanup(destroy_hb_buffer))) hb_buffer_t *hb_buffer = hb_buffer_create(); if (!hb_buffer_pre_allocate(hb_buffer, 4*PyUnicode_GET_LENGTH(ptext))) { PyErr_NoMemory(); return NULL; }