From 2c92240f8de3f7c2dbc80dc4c213caabc0290bfa Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 30 Jul 2024 20:33:29 +0530 Subject: [PATCH] Code to render cocoa symbols as PNG --- kitty/cocoa_window.m | 13 +++++++++++++ kitty/fast_data_types.pyi | 1 + 2 files changed, 14 insertions(+) diff --git a/kitty/cocoa_window.m b/kitty/cocoa_window.m index 6a6d6524b..9bfbe456e 100644 --- a/kitty/cocoa_window.m +++ b/kitty/cocoa_window.m @@ -1111,6 +1111,18 @@ bundle_image_as_png(PyObject *self UNUSED, PyObject *args, PyObject *kw) {@autor return convert_image_to_png(icon, image_size, output_path); }} +static PyObject* +symbol_image_as_png(PyObject *self UNUSED, PyObject *args, PyObject *kw) {@autoreleasepool { + const char *b, *output_path = NULL; unsigned image_size = 256; + static const char* kwlist[] = {"symbol", "output_path", "image_size", NULL}; + if (!PyArg_ParseTupleAndKeywords(args, kw, "s|sI", (char**)kwlist, &b, &output_path, &image_size)) return NULL; + NSImage *img = [NSImage imageWithSystemSymbolName:@(b) accessibilityDescription:@""]; // autoreleased + if (!img) { + PyErr_Format(PyExc_KeyError, "Failed to find image for symbol name: %s", b); return NULL; + } + return convert_image_to_png(img, image_size, output_path); +}} + static PyMethodDef module_methods[] = { {"cocoa_get_lang", (PyCFunction)cocoa_get_lang, METH_NOARGS, ""}, {"cocoa_set_global_shortcut", (PyCFunction)cocoa_set_global_shortcut, METH_VARARGS, ""}, @@ -1122,6 +1134,7 @@ static PyMethodDef module_methods[] = { {"cocoa_set_app_icon", (PyCFunction)cocoa_set_app_icon, METH_VARARGS, ""}, {"cocoa_set_dock_icon", (PyCFunction)cocoa_set_dock_icon, METH_VARARGS, ""}, {"cocoa_bundle_image_as_png", (PyCFunction)(void(*)(void))bundle_image_as_png, METH_VARARGS | METH_KEYWORDS, ""}, + {"cocoa_symbol_image_as_png", (PyCFunction)(void(*)(void))symbol_image_as_png, METH_VARARGS | METH_KEYWORDS, ""}, {NULL, NULL, 0, NULL} /* Sentinel */ }; diff --git a/kitty/fast_data_types.pyi b/kitty/fast_data_types.pyi index f38aea6f9..7ac2a209b 100644 --- a/kitty/fast_data_types.pyi +++ b/kitty/fast_data_types.pyi @@ -572,6 +572,7 @@ def cocoa_send_notification( pass def cocoa_bundle_image_as_png(path_or_identifier: str, output_path: str = '', image_size: int = 256, is_identifier: bool = False) -> bytes: ... +def cocoa_symbol_image_as_png(symbol_name: str, output_path: str = '', image_size: int = 256) -> bytes: ... def cocoa_remove_delivered_notification(identifier: str) -> bool: ... def cocoa_live_delivered_notifications() -> bool: ...