Start work on rendering text for CSDs

This commit is contained in:
Kovid Goyal 2021-03-29 11:25:42 +05:30
parent 53f2df115e
commit cdbc096990
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
5 changed files with 68 additions and 1 deletions

View File

@ -216,6 +216,7 @@ extern bool init_cocoa(PyObject *module);
extern bool init_macos_process_info(PyObject *module);
#else
extern bool init_freetype_library(PyObject*);
extern bool init_freetype_render_ui_text(PyObject*);
#endif
@ -253,6 +254,7 @@ PyInit_fast_data_types(void) {
if (!init_freetype_library(m)) return NULL;
if (!init_fontconfig_library(m)) return NULL;
if (!init_desktop(m)) return NULL;
if (!init_freetype_render_ui_text(m)) return NULL;
#endif
if (!init_fonts(m)) return NULL;

View File

@ -168,6 +168,31 @@ end:
if (charset != NULL) FcCharSetDestroy(charset);
}
const char*
file_path_for_font(const char *family, bool bold, bool italic) {
const char *ans = NULL;
FcPattern *match = NULL;
FcPattern *pat = FcPatternCreate();
if (pat == NULL) { PyErr_NoMemory(); return NULL; }
if (family && strlen(family) > 0) AP(FcPatternAddString, FC_FAMILY, (const FcChar8*)family, "family");
if (bold) { AP(FcPatternAddInteger, FC_WEIGHT, FC_WEIGHT_BOLD, "weight"); }
if (italic) { AP(FcPatternAddInteger, FC_SLANT, FC_SLANT_ITALIC, "slant"); }
FcResult result;
FcConfigSubstitute(NULL, pat, FcMatchPattern);
FcDefaultSubstitute(pat);
match = FcFontMatch(NULL, pat, &result);
if (match == NULL) { PyErr_SetString(PyExc_KeyError, "FcFontMatch() failed"); goto end; }
FcChar8 *out;
if (FcPatternGetString(pat, FC_FILE, 0, &out) != FcResultMatch) { PyErr_SetString(PyExc_ValueError, "No path found in fontconfig result"); goto end; }
ans = strdup((char*)out);
if (!ans) { PyErr_NoMemory(); goto end; }
end:
if (match != NULL) FcPatternDestroy(match);
if (pat != NULL) FcPatternDestroy(pat);
return ans;
}
static PyObject*
fc_match(PyObject UNUSED *self, PyObject *args) {
char *family = NULL;

View File

@ -50,3 +50,8 @@ right_shift_canvas(pixel *canvas, size_t width, size_t height, size_t amt) {
zero_at_ptr_count(src, amt);
}
}
#ifndef __APPLE__
const char* file_path_for_font(const char *family, bool bold, bool italic);
#endif

View File

@ -0,0 +1,31 @@
/*
* freetype_render_ui_text.c
* Copyright (C) 2021 Kovid Goyal <kovid at kovidgoyal.net>
*
* Distributed under terms of the GPL3 license.
*/
#include "data-types.h"
#include "fonts.h"
static PyObject*
path_for_font(PyObject *self UNUSED, PyObject *args) {
const char *family = NULL; int bold = 0, italic = 0;
if (!PyArg_ParseTuple(args, "|zpp", &family, &bold, &italic)) return NULL;
const char *ans = file_path_for_font(family, bold, italic);
if (!ans) return NULL;
return PyUnicode_FromString(ans);
}
static PyMethodDef module_methods[] = {
METHODB(path_for_font, METH_VARARGS),
{NULL, NULL, 0, NULL} /* Sentinel */
};
bool
init_freetype_render_ui_text(PyObject *module) {
if (PyModule_AddFunctions(module, module_methods) != 0) return false;
return true;
}

View File

@ -669,7 +669,11 @@ def compile_c_extension(
def find_c_files() -> Tuple[List[str], List[str]]:
ans, headers = [], []
d = 'kitty'
exclude = {'fontconfig.c', 'freetype.c', 'desktop.c'} if is_macos else {'core_text.m', 'cocoa_window.m', 'macos_process_info.c'}
exclude = {
'fontconfig.c', 'freetype.c', 'desktop.c', 'freetype_render_ui_text.c'
} if is_macos else {
'core_text.m', 'cocoa_window.m', 'macos_process_info.c'
}
for x in sorted(os.listdir(d)):
ext = os.path.splitext(x)[1]
if ext in ('.c', '.m') and os.path.basename(x) not in exclude: