Code to render cocoa symbols as PNG

This commit is contained in:
Kovid Goyal
2024-07-30 20:33:29 +05:30
parent 07bc3ce871
commit 2c92240f8d
2 changed files with 14 additions and 0 deletions

View File

@@ -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 */
};

View File

@@ -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: ...