diff --git a/kitty/core_text.m b/kitty/core_text.m index 79d5929f9..7e2c4d6fc 100644 --- a/kitty/core_text.m +++ b/kitty/core_text.m @@ -9,6 +9,7 @@ #include "cleanup.h" #include "fonts.h" #include "unicode-data.h" +#include "font-select.h" #include #include #include @@ -792,6 +793,7 @@ static struct RenderBuffers buffers = {0}; static void finalize(void) { + fs_finalize_selection(); free(ft_buffer.buf); ft_buffer.buf = NULL; ft_buffer.capacity = 0; @@ -1466,6 +1468,7 @@ add_font_file(PyObject UNUSED *_self, PyObject *args) { const unsigned char *path = NULL; Py_ssize_t sz; if (!PyArg_ParseTuple(args, "s#", &path, &sz)) return NULL; + fs_wait_for_selection_thread(); RAII_CoreFoundation(CFURLRef, url, CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault, path, sz, false)); if (CTFontManagerRegisterFontsForURL(url, kCTFontManagerScopeProcess, NULL)) Py_RETURN_TRUE; Py_RETURN_FALSE; @@ -1492,6 +1495,271 @@ set_builtin_nerd_font(PyObject UNUSED *self, PyObject *pypath) { return font_descriptor_to_python(builtin_nerd_font_descriptor); } +// Font selection backend, see font-select.h {{{ +// This code runs in the font selection thread, so it must not use Python. +// CoreText font descriptors and fonts are thread safe. + +static char * +ct_strdup(const char *s) { + char *ans = strdup(s ? s : ""); + if (!ans) fatal("Out of memory"); + return ans; +} + +static char * +cfstring_to_cstr(CFTypeRef src) { + if (!src || CFGetTypeID(src) != CFStringGetTypeID()) return ct_strdup(""); + CFStringRef s = (CFStringRef)src; + const char *fast = CFStringGetCStringPtr(s, kCFStringEncodingUTF8); + if (fast) return ct_strdup(fast); + const CFIndex sz = CFStringGetMaximumSizeForEncoding(CFStringGetLength(s), kCFStringEncodingUTF8) + 1; + char *buf = malloc(sz); + if (!buf) fatal("Out of memory"); + if (!CFStringGetCString(s, buf, sz, kCFStringEncodingUTF8)) buf[0] = 0; + return buf; +} + +static char * +copy_string_attribute(CTFontDescriptorRef desc, CFStringRef attribute) { + RAII_CoreFoundation(CFTypeRef, val, CTFontDescriptorCopyAttribute(desc, attribute)); + return cfstring_to_cstr(val); +} + +static bool +descriptor_is_monospace(CTFontDescriptorRef desc) { + // Same as coretext_all_fonts(), fonts without symbolic traits are considered monospace + RAII_CoreFoundation(CFDictionaryRef, traits, CTFontDescriptorCopyAttribute(desc, kCTFontTraitsAttribute)); + if (!traits) return true; + CFNumberRef value = (CFNumberRef)CFDictionaryGetValue(traits, kCTFontSymbolicTrait); + if (!value) return true; + unsigned long symbolic_traits = 0; + CFNumberGetValue(value, kCFNumberLongType, &symbolic_traits); + return (symbolic_traits & kCTFontTraitMonoSpace) != 0; +} + +// Must be kept in sync with font_descriptor_to_python() +static FSCandidate * +candidate_from_descriptor(FSContext *ctx, CTFontDescriptorRef desc) { + FSCandidate *c = fs_new_candidate(ctx); + CFRetain(desc); + c->handle = (void *)desc; + RAII_CoreFoundation(CFURLRef, url, CTFontDescriptorCopyAttribute(desc, kCTFontURLAttribute)); + uint8_t buf[4096]; + c->path = ct_strdup((url && CFURLGetFileSystemRepresentation(url, true, buf, sizeof(buf))) ? (const char *)buf : ""); + c->postscript_name = copy_string_attribute(desc, kCTFontNameAttribute); + c->family = copy_string_attribute(desc, kCTFontFamilyNameAttribute); + c->style = copy_string_attribute(desc, kCTFontStyleNameAttribute); + c->full_name = copy_string_attribute(desc, kCTFontDisplayNameAttribute); + RAII_CoreFoundation(CFDictionaryRef, traits, CTFontDescriptorCopyAttribute(desc, kCTFontTraitsAttribute)); + unsigned long symbolic_traits = 0; + float weight = 0, width = 0, slant = 0; + if (traits) { +#define get_number(key, output, type_) \ + { \ + CFNumberRef value = (CFNumberRef)CFDictionaryGetValue(traits, key); \ + if (value) CFNumberGetValue(value, type_, &output); \ + } + get_number(kCTFontSymbolicTrait, symbolic_traits, kCFNumberLongType); + get_number(kCTFontWeightTrait, weight, kCFNumberFloatType); + get_number(kCTFontWidthTrait, width, kCFNumberFloatType); + get_number(kCTFontSlantTrait, slant, kCFNumberFloatType); +#undef get_number + } + c->weight = weight; + c->width = width; + c->slant = slant; + c->bold = (symbolic_traits & kCTFontBoldTrait) != 0; + c->italic = (symbolic_traits & kCTFontItalicTrait) != 0; + c->monospace = (symbolic_traits & kCTFontTraitMonoSpace) != 0; + c->spacing_is_mono = c->monospace; + c->expanded = (symbolic_traits & kCTFontExpandedTrait) != 0; + c->condensed = (symbolic_traits & kCTFontCondensedTrait) != 0; + RAII_CoreFoundation(CFDictionaryRef, variation, CTFontDescriptorCopyAttribute(desc, kCTFontVariationAttribute)); + c->variable = variation != NULL; + c->variation_count = variation ? (unsigned)CFDictionaryGetCount(variation) : 0; + return c; +} + +typedef struct CTBackendData { + bool family_names_loaded; + size_t num_family_names; + char **family_names, **family_keys; +} CTBackendData; + +bool +fs_backend_init(FSContext *ctx UNUSED) { + return true; +} + +void +fs_backend_free_context_data(void *data) { + CTBackendData *d = data; + for (size_t i = 0; i < d->num_family_names; i++) { + free(d->family_names[i]); + free(d->family_keys[i]); + } + free(d->family_names); + free(d->family_keys); + free(d); +} + +void +fs_backend_free_candidate(FSCandidate *c) { + if (c->handle) CFRelease((CTFontDescriptorRef)c->handle); + c->handle = NULL; +} + +static CTBackendData * +ct_backend_data(FSContext *ctx) { + CTBackendData **d = (CTBackendData **)fs_backend_data(ctx); + if (!*d) { + *d = calloc(1, sizeof(CTBackendData)); + if (!*d) fatal("Out of memory"); + } + return *d; +} + +static void +load_family_names(CTBackendData *d) { + if (d->family_names_loaded) return; + d->family_names_loaded = true; + RAII_CoreFoundation(CFArrayRef, names, CTFontManagerCopyAvailableFontFamilyNames()); + if (!names) return; + const CFIndex count = CFArrayGetCount(names); + d->family_names = calloc(count + 1, sizeof(char *)); + d->family_keys = calloc(count + 1, sizeof(char *)); + if (!d->family_names || !d->family_keys) fatal("Out of memory"); + for (CFIndex i = 0; i < count; i++) { + d->family_names[d->num_family_names] = cfstring_to_cstr(CFArrayGetValueAtIndex(names, i)); + d->family_keys[d->num_family_names] = fs_name_to_key(d->family_names[d->num_family_names]); + d->num_family_names++; + } +} + +static bool +key_matches(const char *val, const char *key) { + char *k = fs_name_to_key(val); + const bool ans = strcmp(k, key) == 0; + free(k); + return ans; +} + +// Append the fonts whose attribute is value, that match key for kind +static size_t +append_matching(FSContext *ctx, CFStringRef attribute, const char *value, FSListKind kind, const char *key, bool monospaced, FSCandidateList *out) { + RAII_CoreFoundation(CFStringRef, v, CFStringCreateWithCString(NULL, value, kCFStringEncodingUTF8)); + if (!v) return 0; + const void *keys[] = {attribute}, *values[] = {v}; + RAII_CoreFoundation(CFDictionaryRef, attrs, CFDictionaryCreate(NULL, keys, values, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks)); + if (!attrs) return 0; + RAII_CoreFoundation(CTFontDescriptorRef, desc, CTFontDescriptorCreateWithAttributes(attrs)); + RAII_CoreFoundation(CFSetRef, mandatory, CFSetCreate(NULL, keys, 1, &kCFTypeSetCallBacks)); + if (!desc || !mandatory) return 0; + RAII_CoreFoundation(CFArrayRef, matches, CTFontDescriptorCreateMatchingFontDescriptors(desc, mandatory)); + if (!matches) return 0; + size_t num = 0; + for (CFIndex i = 0; i < CFArrayGetCount(matches); i++) { + CTFontDescriptorRef d = (CTFontDescriptorRef)CFArrayGetValueAtIndex(matches, i); + if (monospaced && !descriptor_is_monospace(d)) continue; + const FSCandidate *c = candidate_from_descriptor(ctx, d); + bool ok = false; + switch (kind) { + case FS_LIST_FAMILY: ok = key_matches(c->family, key); break; + case FS_LIST_POSTSCRIPT_NAME: ok = key_matches(c->postscript_name, key); break; + case FS_LIST_FULL_NAME: { + // The same key as the full_map in the Python create_font_map() + char *f = fs_name_to_key(c->family), *s = fs_name_to_key(c->style); + const size_t sz = strlen(f) + strlen(s) + 2; + char *full = malloc(sz); + if (!full) fatal("Out of memory"); + snprintf(full, sz, "%s %s", f, s); + ok = strcmp(full, key) == 0; + free(f); + free(s); + free(full); + } break; + } + if (ok) { + fs_list_append(out, c); + num++; + } + } + return num; +} + +bool +fs_backend_list(FSContext *ctx, FSListKind kind, const char *name, const char *key, bool monospaced, FSCandidateList *out) { + // monospace is not a font family on macOS, it is mapped to Menlo by the selection code + if (strcmp(key, "monospace") == 0) return true; + switch (kind) { + case FS_LIST_POSTSCRIPT_NAME: append_matching(ctx, kCTFontNameAttribute, name, kind, key, monospaced, out); break; + case FS_LIST_FAMILY: + if (!append_matching(ctx, kCTFontFamilyNameAttribute, name, kind, key, monospaced, out)) { + // The name may differ from the actual family name in case or whitespace + CTBackendData *d = ct_backend_data(ctx); + load_family_names(d); + for (size_t i = 0; i < d->num_family_names; i++) { + if (strcmp(d->family_keys[i], key) == 0 && strcmp(d->family_names[i], name) != 0) + append_matching(ctx, kCTFontFamilyNameAttribute, d->family_names[i], kind, key, monospaced, out); + } + } + break; + case FS_LIST_FULL_NAME: { + // The full name is the family name followed by the style name, so try every split point + char *family = ct_strdup(name); + for (size_t i = 0; name[i]; i++) { + if (name[i] != ' ') continue; + family[i] = 0; + append_matching(ctx, kCTFontFamilyNameAttribute, family, kind, key, monospaced, out); + family[i] = ' '; + } + free(family); + } break; + } + return true; +} + +static void +copy_font_table(CTFontRef font, CTFontTableTag tag, uint8_t **out, size_t *len) { + RAII_CoreFoundation(CFDataRef, data, CTFontCopyTable(font, tag, kCTFontTableOptionNoOptions)); + if (!data) return; + const CFIndex n = CFDataGetLength(data); + *out = malloc(n ? n : 1); + if (!*out) fatal("Out of memory"); + memcpy(*out, CFDataGetBytePtr(data), n); + *len = n; +} + +bool +fs_backend_font_tables(FSContext *ctx UNUSED, const FSCandidate *c, FSFontTables *out) { + memset(out, 0, sizeof(*out)); + RAII_CoreFoundation(CTFontRef, font, CTFontCreateWithFontDescriptor((CTFontDescriptorRef)c->handle, 12, NULL)); + if (!font) return false; + copy_font_table(font, kCTFontTableFvar, &out->fvar, &out->fvar_len); + copy_font_table(font, kCTFontTableSTAT, &out->stat, &out->stat_len); + copy_font_table(font, kCTFontTableName, &out->name, &out->name_len); + return true; +} + +PyObject * +fs_backend_descriptor(const FSFont *f) { + RAII_PyObject(ans, font_descriptor_to_python((CTFontDescriptorRef)f->base->handle)); + if (!ans) return NULL; + if (f->has_axes) { + RAII_PyObject(axis_map, PyDict_New()); + if (!axis_map) return NULL; + for (size_t i = 0; i < f->num_axes; i++) { + uint8_t tag[5]; + RAII_PyObject(val, PyFloat_FromDouble(f->axes[i].value)); + if (!val || PyDict_SetItemString(axis_map, tag_to_string(f->axes[i].tag, tag), val) != 0) return NULL; + } + if (PyDict_SetItemString(ans, "axis_map", axis_map) != 0) return NULL; + } + Py_INCREF(ans); + return ans; +} +// }}} + static PyMethodDef module_methods[] = { METHODB(coretext_all_fonts, METH_O), METHODB(add_font_file, METH_VARARGS), METHODB(set_builtin_nerd_font, METH_O), {NULL, NULL, 0, NULL} /* Sentinel */ }; diff --git a/kitty/data-types.c b/kitty/data-types.c index 7623609b6..b657ea5e5 100644 --- a/kitty/data-types.c +++ b/kitty/data-types.c @@ -901,6 +901,7 @@ extern bool init_fontconfig_library(PyObject *); extern bool init_crypto_library(PyObject *); extern bool init_desktop(PyObject *); extern bool init_fonts(PyObject *); +extern bool init_font_select(PyObject *); extern bool init_glfw(PyObject *m); extern bool init_dnd(PyObject *m); extern bool init_child(PyObject *m); @@ -977,6 +978,7 @@ PyInit_fast_data_types(void) { if (!init_freetype_render_ui_text(m)) return NULL; #endif if (!init_fonts(m)) return NULL; + if (!init_font_select(m)) return NULL; if (!init_utmp(m)) return NULL; if (!init_loop_utils(m)) return NULL; if (!init_crypto_library(m)) return NULL; diff --git a/kitty/fast_data_types.pyi b/kitty/fast_data_types.pyi index 35267245d..66384ba81 100644 --- a/kitty/fast_data_types.pyi +++ b/kitty/fast_data_types.pyi @@ -445,6 +445,23 @@ def fc_match_fallback( ) -> FontConfigPattern: ... def set_builtin_nerd_font(path: str) -> Union[CoreTextFont, FontConfigPattern]: ... +# family, style, postscript_name, full_name, system, variable_name, axes, has_features +FontSelectionSpec = Tuple[Optional[str], Optional[str], Optional[str], Optional[str], Optional[str], Optional[str], Tuple[Tuple[str, float], ...], bool] + +class FontSelectionResult(TypedDict): + medium: Union[CoreTextFont, FontConfigPattern] + bold: Union[CoreTextFont, FontConfigPattern] + italic: Union[CoreTextFont, FontConfigPattern] + bi: Union[CoreTextFont, FontConfigPattern] + # The descriptor and whether it is bold and italic for each symbol_map family + symbols: Tuple[Tuple[Union[CoreTextFont, FontConfigPattern], bool, bool], ...] + +def start_font_selection( + specs: Tuple[FontSelectionSpec, FontSelectionSpec, FontSelectionSpec, FontSelectionSpec], symbol_map_families: Tuple[str, ...], threaded: bool = True +) -> None: ... +def finish_font_selection() -> FontSelectionResult: ... +def variable_data_from_font_file(path: str, index: int = 0) -> VariableData: ... + class FeatureData(TypedDict): name: NotRequired[str] tooltip: NotRequired[str] diff --git a/kitty/font-select.c b/kitty/font-select.c new file mode 100644 index 000000000..a9855b280 --- /dev/null +++ b/kitty/font-select.c @@ -0,0 +1,1981 @@ +/* + * font-select.c + * Copyright (C) 2026 Kovid Goyal + * + * Distributed under terms of the GPL3 license. + */ + +// Selection of fonts from the font specifications in kitty.conf, in pure C so +// that it can run in a thread during startup. See font-select.h. The +// algorithm is a port of the previous Python implementation, kept as close +// to it as possible, so that the selected fonts do not change. + +#include "data-types.h" +#include "font-select.h" +#include "state.h" +#include "threading.h" +#include +#include +#include +#include +#include +#ifndef __APPLE__ +#include +#endif + +// Utilities {{{ +static void * +xcalloc(size_t n, size_t sz) { + void *ans = calloc(n ? n : 1, sz ? sz : 1); + if (!ans) fatal("Out of memory"); + return ans; +} + +static void * +xrealloc(void *p, size_t sz) { + void *ans = realloc(p, sz ? sz : 1); + if (!ans) fatal("Out of memory"); + return ans; +} + +static char * +xstrdup(const char *s) { + char *ans = strdup(s ? s : ""); + if (!ans) fatal("Out of memory"); + return ans; +} + +#define grow(array, count, capacity) \ + if ((count) >= (capacity)) { \ + (capacity) = (capacity) ? 2 * (capacity) : 8; \ + (array) = xrealloc((array), (capacity) * sizeof((array)[0])); \ + } + +static bool +is_space(unsigned char ch) { + return ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r' || ch == '\f' || ch == '\v'; +} + +static char +lower(char ch) { + return ('A' <= ch && ch <= 'Z') ? ch + 32 : ch; +} + +// Collapse runs of whitespace into a single space and strip leading and trailing whitespace +static char * +normalize_whitespace(const char *name) { + if (!name) name = ""; + char *ans = xcalloc(strlen(name) + 1, 1); + size_t o = 0; + bool pending_space = false; + for (; *name; name++) { + if (is_space(*name)) { + if (o) pending_space = true; + continue; + } + if (pending_space) { + ans[o++] = ' '; + pending_space = false; + } + ans[o++] = *name; + } + ans[o] = 0; + return ans; +} + +// Must match family_name_to_key() in kitty/fonts/__init__.py, except that only ASCII is lowercased +char * +fs_name_to_key(const char *name) { + char *ans = normalize_whitespace(name); + for (char *p = ans; *p; p++) *p = lower(*p); + return ans; +} + +static char * +lowercased(const char *s) { + char *ans = xstrdup(s); + for (char *p = ans; *p; p++) *p = lower(*p); + return ans; +} + +// Case insensitive (for ASCII) string equality +static bool +ieq(const char *a, const char *b) { + if (!a) a = ""; + if (!b) b = ""; + for (; *a && *b; a++, b++) { + if (lower(*a) != lower(*b)) return false; + } + return *a == *b; +} + +static bool +streq(const char *a, const char *b) { + return strcmp(a ? a : "", b ? b : "") == 0; +} + +static bool +is_empty(const char *s) { + return !s || !s[0]; +} + +static uint32_t +tag_from_bytes(const uint8_t *b) { + return ((uint32_t)b[0] << 24) | ((uint32_t)b[1] << 16) | ((uint32_t)b[2] << 8) | b[3]; +} + +static uint32_t +tag_from_string(const char *s) { + if (strlen(s) != 4) return 0; + return tag_from_bytes((const uint8_t *)s); +} + +static void +tag_to_str(uint32_t tag, char buf[5]) { + buf[0] = (char)(tag >> 24); + buf[1] = (char)(tag >> 16); + buf[2] = (char)(tag >> 8); + buf[3] = (char)tag; + buf[4] = 0; +} + +#define WGHT 0x77676874u // wght +#define ITAL 0x6974616cu // ital +#define SLNT 0x736c6e74u // slnt +#define WDTH 0x77647468u // wdth + +typedef struct AxisMap { + size_t count; + FSAxisValue items[FS_MAX_AXES]; +} AxisMap; + +static const double * +axis_map_get(const AxisMap *m, uint32_t tag) { + for (size_t i = 0; i < m->count; i++) { + if (m->items[i].tag == tag) return &m->items[i].value; + } + return NULL; +} + +static void +axis_map_set(AxisMap *m, uint32_t tag, double value) { + for (size_t i = 0; i < m->count; i++) { + if (m->items[i].tag == tag) { + m->items[i].value = value; + return; + } + } + if (m->count < FS_MAX_AXES) m->items[m->count++] = (FSAxisValue){.tag = tag, .value = value}; +} + +static bool +axis_maps_equal(const AxisMap *a, const AxisMap *b) { + if (a->count != b->count) return false; + for (size_t i = 0; i < a->count; i++) { + const double *v = axis_map_get(b, a->items[i].tag); + if (!v || *v != a->items[i].value) return false; + } + return true; +} + +static void +axis_map_from_values(AxisMap *m, const FSAxisValue *values, size_t count) { + m->count = 0; + for (size_t i = 0; i < count; i++) axis_map_set(m, values[i].tag, values[i].value); +} +// }}} + +// Reading font tables {{{ +static uint16_t +be16(const uint8_t *p) { + return ((uint16_t)p[0] << 8) | p[1]; +} + +static uint32_t +be32(const uint8_t *p) { + return ((uint32_t)p[0] << 24) | ((uint32_t)p[1] << 16) | ((uint32_t)p[2] << 8) | p[3]; +} + +static double +fixed(const uint8_t *p) { + return ((int32_t)be32(p)) / 65536.0; +} + +void +fs_free_font_tables(FSFontTables *t) { + free(t->fvar); + free(t->stat); + free(t->name); + memset(t, 0, sizeof(*t)); +} + +static bool +pread_all(int fd, uint8_t *buf, size_t sz, off_t offset) { + size_t done = 0; + while (done < sz) { + ssize_t n = pread(fd, buf + done, sz - done, offset + (off_t)done); + if (n < 0) { + if (errno == EINTR) continue; + return false; + } + if (n == 0) return false; + done += (size_t)n; + } + return true; +} + +bool +fs_read_font_tables_from_file(const char *path, int index, FSFontTables *t) { + memset(t, 0, sizeof(*t)); + int fd = open(path, O_RDONLY | O_CLOEXEC); + if (fd < 0) return false; + bool ok = false; + uint8_t header[12]; + uint8_t *records = NULL; + if (!pread_all(fd, header, sizeof(header), 0)) goto end; + off_t dir_offset = 0; + if (memcmp(header, "ttcf", 4) == 0) { + const uint32_t num_fonts = be32(header + 8), face = index & 0xffff; + uint8_t buf[4]; + if (face >= num_fonts || !pread_all(fd, buf, 4, 12 + 4 * (off_t)face)) goto end; + dir_offset = be32(buf); + if (!pread_all(fd, header, sizeof(header), dir_offset)) goto end; + } + const uint32_t version = be32(header); + if (version != 0x00010000 && memcmp(header, "OTTO", 4) != 0 && memcmp(header, "true", 4) != 0) { + // Not an sfnt font, so it has no variable font data + ok = true; + goto end; + } + const uint16_t num_tables = be16(header + 4); + records = xcalloc(num_tables, 16); + if (!pread_all(fd, records, (size_t)num_tables * 16, dir_offset + 12)) goto end; + for (uint16_t i = 0; i < num_tables; i++) { + const uint8_t *r = records + 16 * i; + uint8_t **dest = NULL; + size_t *dest_len = NULL; + if (memcmp(r, "fvar", 4) == 0) { + dest = &t->fvar; + dest_len = &t->fvar_len; + } else if (memcmp(r, "STAT", 4) == 0) { + dest = &t->stat; + dest_len = &t->stat_len; + } else if (memcmp(r, "name", 4) == 0) { + dest = &t->name; + dest_len = &t->name_len; + } + if (!dest || *dest) continue; + const uint32_t offset = be32(r + 8), length = be32(r + 12); + if (length > 64 * 1024 * 1024) continue; + *dest = xcalloc(length, 1); + if (!pread_all(fd, *dest, length, offset)) { + free(*dest); + *dest = NULL; + continue; + } + *dest_len = length; + } + ok = true; +end: + free(records); + close(fd); + if (!ok) fs_free_font_tables(t); + return ok; +} + +typedef struct NameRecord { + uint16_t platform_id, encoding_id, language_id, name_id, length; + const uint8_t *data; +} NameRecord; + +typedef struct NameTable { + NameRecord *records; + size_t count, capacity; +} NameTable; + +static void +parse_name_table(const uint8_t *table, size_t table_len, NameTable *nt) { + memset(nt, 0, sizeof(*nt)); + if (!table || table_len < 18) return; + const uint16_t num = be16(table + 2), storage_offset = be16(table + 4); + const uint8_t *storage = table + storage_offset, *limit = table + table_len; + if (storage >= limit) return; + const uint8_t *p = table + 6; + for (uint16_t i = 0; i < num && p + 12 <= limit; i++, p += 12) { + NameRecord r = {.platform_id = be16(p), .encoding_id = be16(p + 2), .language_id = be16(p + 4), .name_id = be16(p + 6), .length = be16(p + 8)}; + const uint8_t *s = storage + be16(p + 10); + if (s + r.length > limit) continue; + r.data = s; + grow(nt->records, nt->count, nt->capacity); + nt->records[nt->count++] = r; + } +} + +static size_t +encode_utf8(uint32_t ch, char *dest) { + if (ch < 0x80) { + dest[0] = (char)ch; + return 1; + } + if (ch < 0x800) { + dest[0] = (char)((ch >> 6) | 0xC0); + dest[1] = (char)((ch & 0x3F) | 0x80); + return 2; + } + if (ch < 0x10000) { + dest[0] = (char)((ch >> 12) | 0xE0); + dest[1] = (char)(((ch >> 6) & 0x3F) | 0x80); + dest[2] = (char)((ch & 0x3F) | 0x80); + return 3; + } + dest[0] = (char)((ch >> 18) | 0xF0); + dest[1] = (char)(((ch >> 12) & 0x3F) | 0x80); + dest[2] = (char)(((ch >> 6) & 0x3F) | 0x80); + dest[3] = (char)((ch & 0x3F) | 0x80); + return 4; +} + +static const uint16_t mac_roman[128] = { + 0x00C4, 0x00C5, 0x00C7, 0x00C9, 0x00D1, 0x00D6, 0x00DC, 0x00E1, 0x00E0, 0x00E2, 0x00E4, 0x00E3, 0x00E5, 0x00E7, 0x00E9, 0x00E8, 0x00EA, 0x00EB, 0x00ED, + 0x00EC, 0x00EE, 0x00EF, 0x00F1, 0x00F3, 0x00F2, 0x00F4, 0x00F6, 0x00F5, 0x00FA, 0x00F9, 0x00FB, 0x00FC, 0x2020, 0x00B0, 0x00A2, 0x00A3, 0x00A7, 0x2022, + 0x00B6, 0x00DF, 0x00AE, 0x00A9, 0x2122, 0x00B4, 0x00A8, 0x2260, 0x00C6, 0x00D8, 0x221E, 0x00B1, 0x2264, 0x2265, 0x00A5, 0x00B5, 0x2202, 0x2211, 0x220F, + 0x03C0, 0x222B, 0x00AA, 0x00BA, 0x03A9, 0x00E6, 0x00F8, 0x00BF, 0x00A1, 0x00AC, 0x221A, 0x0192, 0x2248, 0x2206, 0x00AB, 0x00BB, 0x2026, 0x00A0, 0x00C0, + 0x00C3, 0x00D5, 0x0152, 0x0153, 0x2013, 0x2014, 0x201C, 0x201D, 0x2018, 0x2019, 0x00F7, 0x25CA, 0x00FF, 0x0178, 0x2044, 0x20AC, 0x2039, 0x203A, 0xFB01, + 0xFB02, 0x2021, 0x00B7, 0x201A, 0x201E, 0x2030, 0x00C2, 0x00CA, 0x00C1, 0x00CB, 0x00C8, 0x00CD, 0x00CE, 0x00CF, 0x00CC, 0x00D3, 0x00D4, 0xF8FF, 0x00D2, + 0x00DA, 0x00DB, 0x00D9, 0x0131, 0x02C6, 0x02DC, 0x00AF, 0x02D8, 0x02D9, 0x02DA, 0x00B8, 0x02DD, 0x02DB, 0x02C7, +}; + +static char * +decode_name_record(const NameRecord *r) { + char *ans = xcalloc((size_t)r->length * 3 + 4, 1); + size_t o = 0; + if ((r->platform_id == 3 && r->encoding_id == 1) || r->platform_id == 0) { + for (size_t i = 0; i + 1 < r->length; i += 2) { + uint32_t ch = be16(r->data + i); + if (0xD800 <= ch && ch <= 0xDBFF && i + 3 < r->length) { + const uint32_t low = be16(r->data + i + 2); + if (0xDC00 <= low && low <= 0xDFFF) { + ch = 0x10000 + ((ch - 0xD800) << 10) + (low - 0xDC00); + i += 2; + } else ch = 0xFFFD; + } else if (0xD800 <= ch && ch <= 0xDFFF) ch = 0xFFFD; + o += encode_utf8(ch, ans + o); + } + } else if (r->platform_id == 1 && r->encoding_id == 0 && r->language_id == 0) { + for (size_t i = 0; i < r->length; i++) { + const uint8_t b = r->data[i]; + o += encode_utf8(b < 0x80 ? b : mac_roman[b - 0x80], ans + o); + } + } else { + for (size_t i = 0; i < r->length; i++) o += encode_utf8(r->data[i], ans + o); + } + ans[o] = 0; + return ans; +} + +static char * +get_best_name(const NameTable *nt, unsigned name_id) { + const NameRecord *only = NULL; + size_t count = 0; + for (size_t i = 0; i < nt->count; i++) { + if (nt->records[i].name_id == name_id) { + if (!count) only = nt->records + i; + count++; + } + } + if (!count) return xstrdup(""); + if (count == 1) return decode_name_record(only); + static const uint16_t priorities[][3] = {{3, 1, 1033}, {1, 0, 0}, {0, 6, 0}, {0, 4, 0}, {0, 3, 0}, {0, 2, 0}, {0, 1, 0}}; + for (size_t p = 0; p < arraysz(priorities); p++) { + for (size_t i = 0; i < nt->count; i++) { + const NameRecord *r = nt->records + i; + if (r->name_id == name_id && r->platform_id == priorities[p][0] && r->encoding_id == priorities[p][1] && r->language_id == priorities[p][2]) + return decode_name_record(r); + } + } + return xstrdup(""); +} + +static void +read_fvar(const uint8_t *table, size_t table_len, const NameTable *nt, FSVariableData *vd) { + if (!table || table_len < 28) return; + const uint8_t *limit = table + table_len; + const uint16_t offset_to_axes = be16(table + 4), num_axes = be16(table + 8), axis_size = be16(table + 10), num_instances = be16(table + 12), + instance_size = be16(table + 14); + const size_t size_of_coordinates = (size_t)num_axes * 4; + if (instance_size < size_of_coordinates + 4 || axis_size < 20) return; + const bool has_postscript_name = instance_size >= 6 + size_of_coordinates; + vd->axes = xcalloc(num_axes, sizeof(vd->axes[0])); + for (const uint8_t *pos = table + offset_to_axes; pos + axis_size <= limit && vd->num_axes < num_axes; pos += axis_size) { + FSVarAxis *a = vd->axes + vd->num_axes++; + a->tag = tag_from_bytes(pos); + a->minimum = fixed(pos + 4); + a->def = fixed(pos + 8); + a->maximum = fixed(pos + 12); + a->hidden = be16(pos + 16) & 1; + a->strid = get_best_name(nt, be16(pos + 18)); + } + vd->named_styles = xcalloc(num_instances, sizeof(vd->named_styles[0])); + for (const uint8_t *pos = table + offset_to_axes + (size_t)num_axes * axis_size; pos + instance_size <= limit && vd->num_named_styles < num_instances; + pos += instance_size) { + FSNamedStyle *ns = vd->named_styles + vd->num_named_styles++; + ns->name = get_best_name(nt, be16(pos)); + ns->values = xcalloc(vd->num_axes, sizeof(ns->values[0])); + for (size_t i = 0; i < vd->num_axes; i++) { + // use the same tag at most once, like a dict + bool found = false; + for (size_t j = 0; j < ns->num_values; j++) { + if (ns->values[j].tag == vd->axes[i].tag) { + ns->values[j].value = fixed(pos + 4 + 4 * i); + found = true; + } + } + if (!found) ns->values[ns->num_values++] = (FSAxisValue){.tag = vd->axes[i].tag, .value = fixed(pos + 4 + 4 * i)}; + } + const uint16_t psname_id = has_postscript_name ? be16(pos + 4 + size_of_coordinates) : 0xffff; + ns->psname = (psname_id != 0xffff && psname_id) ? get_best_name(nt, psname_id) : xstrdup(""); + } +} + +static void +read_stat(const uint8_t *table, size_t table_len, const NameTable *nt, FSVariableData *vd) { + if (!table || table_len < 20) return; + const uint8_t *limit = table + table_len; + const uint16_t major_version = be16(table), minor_version = be16(table + 2), size_of_design_axis_entry = be16(table + 4), + count_of_design_axis_entries = be16(table + 6); + const uint32_t offset_to_design_axes = be32(table + 8); + const uint16_t count_of_axis_value_entries = be16(table + 12); + const uint32_t offset_to_axis_values = be32(table + 14); + uint16_t elided_fallback_name_id = be16(table + 18); + if (major_version == 1 && minor_version < 1) elided_fallback_name_id = 0; + if (elided_fallback_name_id) { + free(vd->elided_fallback_name); + vd->elided_fallback_name = get_best_name(nt, elided_fallback_name_id); + } + if (size_of_design_axis_entry < 8) return; + vd->design_axes = xcalloc(count_of_design_axis_entries, sizeof(vd->design_axes[0])); + for (const uint8_t *pos = table + offset_to_design_axes; pos + size_of_design_axis_entry <= limit && vd->num_design_axes < count_of_design_axis_entries; + pos += size_of_design_axis_entry) { + FSDesignAxis *da = vd->design_axes + vd->num_design_axes++; + da->tag = tag_from_bytes(pos); + da->name = get_best_name(nt, be16(pos + 4)); + da->ordering = be16(pos + 6); + } + const uint8_t *offsets = table + offset_to_axis_values; + size_t count = 0; + for (const uint8_t *pos = offsets; pos + 2 <= limit && count < count_of_axis_value_entries; pos += 2, count++) { + const uint8_t *v = offsets + be16(pos); + if (v + 12 > limit) continue; + const uint16_t format = be16(v), axis_index = be16(v + 2), flags = be16(v + 4), value_name_id = be16(v + 6); + if (axis_index >= vd->num_design_axes) continue; + FSDesignValue dv = {.format = format, .flags = flags}; + switch (format) { + case 1: dv.value = fixed(v + 8); break; + case 2: + if (v + 20 > limit) continue; + dv.value = fixed(v + 8); + dv.minimum = fixed(v + 12); + dv.maximum = fixed(v + 16); + break; + case 3: + if (v + 16 > limit) continue; + dv.value = fixed(v + 8); + dv.linked_value = fixed(v + 12); + break; + default: continue; + } + dv.name = get_best_name(nt, value_name_id); + FSDesignAxis *da = vd->design_axes + axis_index; + grow(da->values, da->num_values, da->capacity); + da->values[da->num_values++] = dv; + } +} + +bool +fs_variable_data_from_tables(const FSFontTables *t, FSVariableData *vd) { + memset(vd, 0, sizeof(*vd)); + NameTable nt; + parse_name_table(t->name, t->name_len, &nt); + vd->elided_fallback_name = xstrdup(""); + read_fvar(t->fvar, t->fvar_len, &nt, vd); + read_stat(t->stat, t->stat_len, &nt, vd); + vd->variations_postscript_name_prefix = get_best_name(&nt, 25); + free(nt.records); + return true; +} + +void +fs_free_variable_data(FSVariableData *vd) { + free(vd->variations_postscript_name_prefix); + free(vd->elided_fallback_name); + for (size_t i = 0; i < vd->num_axes; i++) free(vd->axes[i].strid); + free(vd->axes); + for (size_t i = 0; i < vd->num_named_styles; i++) { + free(vd->named_styles[i].name); + free(vd->named_styles[i].psname); + free(vd->named_styles[i].values); + } + free(vd->named_styles); + for (size_t i = 0; i < vd->num_design_axes; i++) { + for (size_t j = 0; j < vd->design_axes[i].num_values; j++) free(vd->design_axes[i].values[j].name); + free(vd->design_axes[i].values); + free(vd->design_axes[i].name); + } + free(vd->design_axes); + memset(vd, 0, sizeof(*vd)); +} +// }}} + +// Context {{{ +typedef struct ListCacheEntry { + int kind; + bool monospaced; + char *key; + FSCandidateList list; +} ListCacheEntry; + +#define FS_LIST_VARIABLE_FAMILY 1000 + +typedef struct WeightRange { + double minimum, maximum, medium, bold; + bool valid; +} WeightRange; + +struct FSContext { + FSFontSpec specs[4]; + char **symbol_families; + size_t num_symbol_families; + + FSCandidate **candidates; + size_t num_candidates, candidates_capacity; + ListCacheEntry *lists; + size_t num_lists, lists_capacity; + struct { + char *path; + FSVariableData vd; + } *vds; + size_t num_vds, vds_capacity; + struct { + char *family; + WeightRange wr; + } *wrs; + size_t num_wrs, wrs_capacity; + struct { + char *path; + bool val; + } *actually_variable; + size_t num_actually_variable, actually_variable_capacity; +#ifndef __APPLE__ + struct { + char *family; + bool bold, italic; + int spacing; + const FSCandidate *ans; + } *matches; + size_t num_matches, matches_capacity; +#endif + + char *error; + char **messages; + size_t num_messages, messages_capacity; + void *backend_data; + + FSFont results[4]; + FSFont *symbols; + bool *symbol_bold, *symbol_italic; +}; + +void ** +fs_backend_data(FSContext *ctx) { + return &ctx->backend_data; +} + +FSCandidate * +fs_new_candidate(FSContext *ctx) { + grow(ctx->candidates, ctx->num_candidates, ctx->candidates_capacity); + FSCandidate *ans = xcalloc(1, sizeof(FSCandidate)); + ctx->candidates[ctx->num_candidates++] = ans; + return ans; +} + +bool +fs_list_append(FSCandidateList *l, const FSCandidate *c) { + grow(l->items, l->count, l->capacity); + l->items[l->count++] = c; + return true; +} + +static char * +vformat(const char *fmt, va_list ap) { + va_list ap2; + va_copy(ap2, ap); + int n = vsnprintf(NULL, 0, fmt, ap2); + va_end(ap2); + if (n < 0) return xstrdup(fmt); + char *ans = xcalloc((size_t)n + 1, 1); + vsnprintf(ans, (size_t)n + 1, fmt, ap); + return ans; +} + +void +fs_set_error(FSContext *ctx, const char *fmt, ...) { + if (ctx->error) return; // keep the first error + va_list ap; + va_start(ap, fmt); + ctx->error = vformat(fmt, ap); + va_end(ap); +} + +void +fs_add_message(FSContext *ctx, const char *fmt, ...) { + va_list ap; + va_start(ap, fmt); + grow(ctx->messages, ctx->num_messages, ctx->messages_capacity); + ctx->messages[ctx->num_messages++] = vformat(fmt, ap); + va_end(ap); +} + +static void +free_spec(FSFontSpec *s) { + free(s->family); + free(s->style); + free(s->postscript_name); + free(s->full_name); + free(s->system); + free(s->variable_name); + for (size_t i = 0; i < s->num_axes; i++) free(s->axes[i].name); + free(s->axes); + memset(s, 0, sizeof(*s)); +} + +static void +free_context(FSContext *ctx) { + if (!ctx) return; + for (size_t i = 0; i < arraysz(ctx->specs); i++) free_spec(ctx->specs + i); + for (size_t i = 0; i < ctx->num_symbol_families; i++) free(ctx->symbol_families[i]); + free(ctx->symbol_families); + for (size_t i = 0; i < ctx->num_lists; i++) { + free(ctx->lists[i].key); + free(ctx->lists[i].list.items); + } + free(ctx->lists); + for (size_t i = 0; i < ctx->num_vds; i++) { + free(ctx->vds[i].path); + fs_free_variable_data(&ctx->vds[i].vd); + } + free(ctx->vds); + for (size_t i = 0; i < ctx->num_wrs; i++) free(ctx->wrs[i].family); + free(ctx->wrs); + for (size_t i = 0; i < ctx->num_actually_variable; i++) free(ctx->actually_variable[i].path); + free(ctx->actually_variable); +#ifndef __APPLE__ + for (size_t i = 0; i < ctx->num_matches; i++) free(ctx->matches[i].family); + free(ctx->matches); +#endif + for (size_t i = 0; i < ctx->num_candidates; i++) { + FSCandidate *c = ctx->candidates[i]; + fs_backend_free_candidate(c); + free(c->path); + free(c->family); + free(c->style); + free(c->full_name); + free(c->postscript_name); + free(c); + } + free(ctx->candidates); + free(ctx->error); + for (size_t i = 0; i < ctx->num_messages; i++) free(ctx->messages[i]); + free(ctx->messages); + if (ctx->backend_data) fs_backend_free_context_data(ctx->backend_data); + free(ctx->symbols); + free(ctx->symbol_bold); + free(ctx->symbol_italic); + free(ctx); +} + +static const FSCandidateList * +list_fonts(FSContext *ctx, FSListKind kind, const char *name, bool monospaced) { + RAII_ALLOC(char, key, fs_name_to_key(name)); + for (size_t i = 0; i < ctx->num_lists; i++) { + ListCacheEntry *e = ctx->lists + i; + if (e->kind == (int)kind && e->monospaced == monospaced && strcmp(e->key, key) == 0) return &e->list; + } + FSCandidateList list = {0}; + if (key[0]) { + // Also pass the name as specified by the user, with only whitespace normalized, for case sensitive font databases + RAII_ALLOC(char, normalized, normalize_whitespace(name)); + if (!fs_backend_list(ctx, kind, normalized, key, monospaced, &list)) { + free(list.items); + list = (FSCandidateList){0}; + } + } + grow(ctx->lists, ctx->num_lists, ctx->lists_capacity); + ListCacheEntry *e = ctx->lists + ctx->num_lists++; + *e = (ListCacheEntry){.kind = kind, .monospaced = monospaced, .key = xstrdup(key), .list = list}; + return &e->list; +} + +static int +cmp_by_path(const void *a_, const void *b_) { + const FSCandidate *const *a = a_, *const *b = b_; + return strcmp((*a)->path ? (*a)->path : "", (*b)->path ? (*b)->path : ""); +} + +// The fonts of the specified family that are variable fonts +static const FSCandidateList * +variable_fonts(FSContext *ctx, const char *family, bool monospaced) { + RAII_ALLOC(char, key, fs_name_to_key(family)); + for (size_t i = 0; i < ctx->num_lists; i++) { + ListCacheEntry *e = ctx->lists + i; + if (e->kind == FS_LIST_VARIABLE_FAMILY && e->monospaced == monospaced && strcmp(e->key, key) == 0) return &e->list; + } + const FSCandidateList *all = list_fonts(ctx, FS_LIST_FAMILY, family, monospaced); + FSCandidateList list = {0}; + for (size_t i = 0; i < all->count; i++) { + if (all->items[i]->variable) fs_list_append(&list, all->items[i]); + } +#ifdef __APPLE__ + // CoreText makes a separate descriptor for each named style in each + // variable font file. Keep only the default style descriptor, which has an + // empty variation dictionary. If no default exists, pick the one with the + // smallest variation dictionary size. + if (list.count > 1) { + // stable sort by path + for (size_t i = 1; i < list.count; i++) { + const FSCandidate *x = list.items[i]; + size_t j = i; + while (j > 0 && cmp_by_path(&list.items[j - 1], &x) > 0) { + list.items[j] = list.items[j - 1]; + j--; + } + list.items[j] = x; + } + size_t o = 0; + for (size_t i = 0; i < list.count;) { + size_t j = i, best = i; + for (; j < list.count && cmp_by_path(&list.items[i], &list.items[j]) == 0; j++) { + if (list.items[j]->variation_count < list.items[best]->variation_count) best = j; + } + list.items[o++] = list.items[best]; + i = j; + } + list.count = o; + } +#else + (void)cmp_by_path; +#endif + grow(ctx->lists, ctx->num_lists, ctx->lists_capacity); + ListCacheEntry *e = ctx->lists + ctx->num_lists++; + *e = (ListCacheEntry){.kind = FS_LIST_VARIABLE_FAMILY, .monospaced = monospaced, .key = xstrdup(key), .list = list}; + return &e->list; +} + +static const FSVariableData * +variable_data(FSContext *ctx, const FSCandidate *c) { + const char *path = c->path ? c->path : ""; + for (size_t i = 0; i < ctx->num_vds; i++) { + if (strcmp(ctx->vds[i].path, path) == 0) return &ctx->vds[i].vd; + } + grow(ctx->vds, ctx->num_vds, ctx->vds_capacity); + ctx->vds[ctx->num_vds].path = xstrdup(path); + FSVariableData *vd = &ctx->vds[ctx->num_vds].vd; + ctx->num_vds++; + FSFontTables t = {0}; + if (!fs_backend_font_tables(ctx, c, &t)) memset(&t, 0, sizeof(t)); + fs_variable_data_from_tables(&t, vd); + fs_free_font_tables(&t); + return vd; +} +// }}} + +// Variable fonts {{{ +static FSFont +font_from_candidate(const FSCandidate *c) { + return (FSFont){.base = c, .named_style = -1}; +} + +static bool +same_candidate(const FSCandidate *a, const FSCandidate *b) { + if (a == b) return true; + if (!a || !b) return false; + return a->from_match == b->from_match && a->index == b->index && streq(a->path, b->path) && streq(a->postscript_name, b->postscript_name) && + streq(a->style, b->style) && streq(a->family, b->family) && a->variable == b->variable; +} + +static bool +fonts_equal(const FSFont *a, const FSFont *b) { + if (!a || !b) return a == b; + if (!same_candidate(a->base, b->base) || a->named_style != b->named_style || a->has_axes != b->has_axes || a->has_matrix != b->has_matrix) return false; + if (a->has_axes) { + AxisMap ma, mb; + axis_map_from_values(&ma, a->axes, a->num_axes); + axis_map_from_values(&mb, b->axes, b->num_axes); + if (!axis_maps_equal(&ma, &mb)) return false; + } + if (a->has_matrix && memcmp(a->matrix, b->matrix, sizeof(a->matrix)) != 0) return false; + return true; +} + +static bool +is_monospace(const FSFont *f) { + return f->base->monospace; +} + +static bool +is_variable(const FSFont *f) { + return f->base->variable; +} + +static int +find_named_style(const char *name, const FSVariableData *vd) { + for (size_t i = 0; i < vd->num_named_styles; i++) { + if (ieq(vd->named_styles[i].psname, name)) return (int)i; + } + for (size_t i = 0; i < vd->num_named_styles; i++) { + if (ieq(vd->named_styles[i].name, name)) return (int)i; + } + if (!is_empty(vd->elided_fallback_name)) { + RAII_ALLOC(char, q, lowercased(name)); + const char *efn = vd->elided_fallback_name; + const size_t efn_len = strlen(efn); + for (size_t i = 0; i < vd->num_named_styles; i++) { + // remove all occurrences of efn from the name + const char *src = vd->named_styles[i].name; + RAII_ALLOC(char, removed, xcalloc(strlen(src) + 1, 1)); + size_t o = 0; + while (*src) { + if (strncmp(src, efn, efn_len) == 0) src += efn_len; + else removed[o++] = *(src++); + } + removed[o] = 0; + RAII_ALLOC(char, eq, fs_name_to_key(removed)); + if (strcmp(q, eq) == 0) return (int)i; + } + } + return -1; +} + +#ifdef __APPLE__ +static void +set_font_axes(FSFont *font, const AxisMap *m) { + font->has_axes = true; + font->num_axes = m->count; + memcpy(font->axes, m->items, m->count * sizeof(m->items[0])); +} + +static bool +set_axis_values(const AxisMap *tag_map, FSFont *font, const FSVariableData *vd) { + AxisMap previous = {0}, new = {0}; + if (font->has_axes) axis_map_from_values(&previous, font->axes, font->num_axes); + new = previous; + for (size_t i = 0; i < vd->num_axes; i++) { + const double *val = axis_map_get(tag_map, vd->axes[i].tag); + if (val) axis_map_set(&new, vd->axes[i].tag, *val); + } + set_font_axes(font, &new); + return !axis_maps_equal(&new, &previous); +} + +static bool +set_named_style(const char *name, FSFont *font, const FSVariableData *vd) { + int idx = find_named_style(name, vd); + if (idx < 0) return false; + AxisMap m; + axis_map_from_values(&m, vd->named_styles[idx].values, vd->named_styles[idx].num_values); + return set_axis_values(&m, font, vd); +} + +static void +get_axis_values(const FSFont *font, const FSVariableData *vd UNUSED, AxisMap *ans) { + ans->count = 0; + if (font->has_axes) axis_map_from_values(ans, font->axes, font->num_axes); +} +#else +static void +current_axes(const FSFont *font, const FSVariableData *vd, AxisMap *ans) { + // in the order of the axes in vd + ans->count = 0; + for (size_t i = 0; i < vd->num_axes && i < FS_MAX_AXES; i++) { + ans->items[ans->count++] = + (FSAxisValue){.tag = vd->axes[i].tag, .value = (font->has_axes && font->num_axes && i < font->num_axes) ? font->axes[i].value : vd->axes[i].def}; + } +} + +static void +lift_axes_to_named_style_if_possible(FSFont *font, const FSVariableData *vd) { + AxisMap q, current; + current_axes(font, vd, ¤t); + axis_map_from_values(&q, current.items, current.count); + for (size_t i = 0; i < vd->num_named_styles; i++) { + AxisMap ns; + axis_map_from_values(&ns, vd->named_styles[i].values, vd->named_styles[i].num_values); + if (axis_maps_equal(&ns, &q)) { + font->has_axes = false; + font->num_axes = 0; + font->named_style = (int)i; + return; + } + } +} + +static bool +set_axis_values(const AxisMap *tag_map, FSFont *font, const FSVariableData *vd) { + AxisMap axes; + current_axes(font, vd, &axes); + bool changed = false; + for (size_t i = 0; i < axes.count; i++) { + const double *val = axis_map_get(tag_map, axes.items[i].tag); + if (val) { + changed = true; + axes.items[i].value = *val; + } + } + if (changed) { + font->has_axes = true; + font->num_axes = axes.count; + memcpy(font->axes, axes.items, axes.count * sizeof(axes.items[0])); + lift_axes_to_named_style_if_possible(font, vd); + } + return changed; +} + +static bool +set_named_style(const char *name, FSFont *font, const FSVariableData *vd) { + int idx = find_named_style(name, vd); + if (idx < 0) return false; + font->named_style = idx; + return true; +} + +static void +get_axis_values(const FSFont *font, const FSVariableData *vd, AxisMap *ans) { + ans->count = 0; + if (font->named_style > -1 && (size_t)font->named_style < vd->num_named_styles) { + const FSNamedStyle *ns = vd->named_styles + font->named_style; + axis_map_from_values(ans, ns->values, ns->num_values); + } + for (size_t i = 0; i < vd->num_axes; i++) { + const uint32_t tag = vd->axes[i].tag; + if (font->has_axes && i < font->num_axes) axis_map_set(ans, tag, font->axes[i].value); + else if (!axis_map_get(ans, tag)) axis_map_set(ans, tag, vd->axes[i].def); + } +} +#endif + +typedef struct OptionalDouble { + bool has; + double val; +} OptionalDouble; + +typedef struct FamilyAxisValues { + OptionalDouble regular_weight, regular_slant, regular_ital, regular_width, bold_weight, italic_slant, italic_ital; +} FamilyAxisValues; + +static OptionalDouble +opt_from_map(const AxisMap *m, uint32_t tag) { + const double *v = axis_map_get(m, tag); + return v ? (OptionalDouble){.has = true, .val = *v} : (OptionalDouble){0}; +} + +static OptionalDouble +family_axis_value(const FamilyAxisValues *f, uint32_t tag, bool bold, bool italic) { + switch (tag) { + case WGHT: return bold ? f->bold_weight : f->regular_weight; + case ITAL: return italic ? f->italic_ital : f->regular_ital; + case SLNT: return italic ? f->italic_slant : f->regular_slant; + case WDTH: return f->regular_width; + } + return (OptionalDouble){0}; +} + +static void +set_regular_values(FamilyAxisValues *f, const AxisMap *m) { + f->regular_weight = opt_from_map(m, WGHT); + f->regular_width = opt_from_map(m, WDTH); + f->regular_ital = opt_from_map(m, ITAL); + f->regular_slant = opt_from_map(m, SLNT); +} + +static void +set_bold_values(FamilyAxisValues *f, const AxisMap *m) { + f->bold_weight = opt_from_map(m, WGHT); +} + +static void +set_italic_values(FamilyAxisValues *f, const AxisMap *m) { + f->italic_ital = opt_from_map(m, ITAL); + f->italic_slant = opt_from_map(m, SLNT); +} + +static FSFont +find_medium_variant(FSContext *ctx, FSFont font) { + const FSVariableData *vd = variable_data(ctx, font.base); + for (size_t i = 0; i < vd->num_named_styles; i++) { + const FSNamedStyle *ns = vd->named_styles + i; + if (strcmp(ns->name, "Regular") == 0) { + set_named_style(is_empty(ns->psname) ? ns->name : ns->psname, &font, vd); + return font; + } + } + AxisMap axis_values = {0}; + for (size_t i = 0; i < vd->num_axes; i++) { + const uint32_t tag = vd->axes[i].tag; + for (size_t d = 0; d < vd->num_design_axes; d++) { + const FSDesignAxis *dax = vd->design_axes + d; + if (dax->tag != tag) continue; + for (size_t v = 0; v < dax->num_values; v++) { + const FSDesignValue *x = dax->values + v; + if ((x->format == 1 || x->format == 2) && strcmp(x->name, "Regular") == 0) { + axis_map_set(&axis_values, tag, x->value); + break; + } + } + } + } + if (axis_values.count) set_axis_values(&axis_values, &font, vd); + return font; +} + +static double +get_bold_design_weight(const FSDesignAxis *dax, double regular_weight) { + bool found = false; + double ans = regular_weight; + for (size_t i = 0; i < dax->num_values; i++) { + const FSDesignValue *x = dax->values + i; + if ((x->format == 1 || x->format == 2) && x->value > regular_weight) { + if (!found || x->value < ans) ans = x->value; + found = true; + } + } + return ans; +} + +static double +get_design_value_for(const FSDesignAxis *dax, const FSVarAxis *ax, bool bold, bool italic, const FamilyAxisValues *fav) { + OptionalDouble family_val = family_axis_value(fav, ax->tag, bold, italic); + if (family_val.has && ax->minimum <= family_val.val && family_val.val <= ax->maximum) return family_val.val; + const double def = ax->def; + static const char *bold_keys[] = {"semibold", "bold", "heavy", "black", NULL}, *regular_weight_keys[] = {"regular", "medium", NULL}, + *italic_keys[] = {"italic", "oblique", "slanted", "slant", NULL}, *upright_keys[] = {"regular", "normal", "medium", "upright", NULL}; + const char **keys; + if (dax->tag == WGHT) keys = bold ? bold_keys : regular_weight_keys; + else if (dax->tag == ITAL || dax->tag == SLNT) keys = italic ? italic_keys : upright_keys; + else return def; + // An insertion ordered dict of value -> priority + RAII_ALLOC(double, values, xcalloc(dax->num_values, sizeof(double))); + RAII_ALLOC(int, priorities, xcalloc(dax->num_values, sizeof(int))); + size_t count = 0; + for (size_t i = 0; i < dax->num_values; i++) { + const FSDesignValue *x = dax->values + i; + if (x->format != 1 && x->format != 2) continue; + int idx = -1; + for (int k = 0; keys[k]; k++) { + if (ieq(keys[k], x->name)) { + idx = k; + break; + } + } + if (idx < 0) continue; + size_t j = 0; + for (; j < count && values[j] != x->value; j++); + if (j == count) values[count++] = x->value; + priorities[j] = idx; + } + double ans = def; + if (count) { + size_t best = 0; + for (size_t j = 1; j < count; j++) { + if (priorities[j] < priorities[best]) best = j; + } + ans = values[best]; + } + if (bold && ax->tag == WGHT && fav->regular_weight.has && ans <= fav->regular_weight.val) ans = get_bold_design_weight(dax, fav->regular_weight.val); + return ans; +} +// }}} + +// Scoring {{{ +typedef struct Scorer { + bool bold, italic, monospaced, prefer_variable; +} Scorer; + +typedef struct Score { + int variable_score; + double style_score; + int monospace_score; + double width_score; + size_t idx; +} Score; + +static WeightRange +weight_range_for_family(FSContext *ctx, const char *family) { + for (size_t i = 0; i < ctx->num_wrs; i++) { + if (strcmp(ctx->wrs[i].family, family) == 0) return ctx->wrs[i].wr; + } + const FSCandidateList *faces = list_fonts(ctx, FS_LIST_FAMILY, family, true); +#ifdef __APPLE__ + const WeightRange sentinel = {.minimum = 99999, .maximum = -99999, .medium = -99999, .bold = -99999}; +#else + const WeightRange sentinel = {.minimum = 9223372036854775807.0, .maximum = -1, .medium = -1, .bold = -1}; + RAII_ALLOC(double, seen, xcalloc(faces->count, sizeof(double))); + size_t num_seen = 0; +#endif + WeightRange wr = sentinel; + for (size_t i = 0; i < faces->count; i++) { + const FSCandidate *face = faces->items[i]; + const double w = face->weight; + if (w < wr.minimum) wr.minimum = w; + if (w > wr.maximum) wr.maximum = w; +#ifndef __APPLE__ + size_t j = 0; + for (; j < num_seen && seen[j] != w; j++); + if (j == num_seen) seen[num_seen++] = w; +#endif + // the first word of the lowercased style + char first_word[32] = {0}; + const char *s = face->style ? face->style : ""; + while (*s && is_space(*s)) s++; + size_t n = 0; + while (*s && !is_space(*s) && n < sizeof(first_word) - 1) first_word[n++] = lower(*(s++)); + if (!n || (*s && !is_space(*s))) continue; + if (strcmp(first_word, "semibold") == 0) wr.bold = w; + else if (strcmp(first_word, "bold") == 0 && wr.bold == sentinel.bold) wr.bold = w; + else if (strcmp(first_word, "regular") == 0) wr.medium = w; + else if (strcmp(first_word, "medium") == 0 && wr.medium == sentinel.medium) wr.medium = w; + } + wr.valid = wr.minimum != sentinel.minimum && wr.maximum != sentinel.maximum && wr.medium != sentinel.medium && wr.bold != sentinel.bold; +#ifndef __APPLE__ + if (num_seen < 2) wr = sentinel; +#endif + grow(ctx->wrs, ctx->num_wrs, ctx->wrs_capacity); + ctx->wrs[ctx->num_wrs].family = xstrdup(family); + ctx->wrs[ctx->num_wrs].wr = wr; + ctx->num_wrs++; + return wr; +} + +static Score +score(const Scorer *s, const FSCandidate *c, const WeightRange *wr) { + Score ans = {.variable_score = (s->prefer_variable && c->variable) ? 0 : 1}; +#ifdef __APPLE__ + double bold_score = c->weight; // -1 to 1 with 0 being normal + if (!wr) { + if (bold_score < 0) bold_score = 2.0; // thinner than normal, reject + else if (s->bold) bold_score = fabs(bold_score - 0.3); // prefer semibold=0.3 to full bold = 0.4 + } else bold_score = fabs(bold_score - (s->bold ? wr->bold : wr->medium)); + // -1 to 1 with 0 being upright < 0 being backward slant, abs(slant) == 1 implies 30 deg rotation + double italic_score = c->slant; + if (s->italic) italic_score = italic_score < 0 ? 2.0 : fabs(1 - italic_score); + ans.style_score = bold_score + italic_score; + ans.monospace_score = c->monospace ? 0 : 1; + ans.width_score = (!c->expanded && !c->condensed) ? 0 : 1; +#else + double bold_score; + if (!wr) bold_score = fabs((s->bold ? FC_WEIGHT_SEMIBOLD : FC_WEIGHT_REGULAR) - c->weight); + else bold_score = fabs((s->bold ? wr->bold : wr->medium) - c->weight); + const double italic_score = fabs((s->italic ? FC_SLANT_ITALIC : FC_SLANT_ROMAN) - c->slant); + ans.monospace_score = s->monospaced ? (c->spacing_is_mono ? 0 : 1) : 0; + ans.width_score = fabs(c->width - FC_WIDTH_NORMAL); + ans.style_score = bold_score / 1000 + italic_score / 110; +#endif + return ans; +} + +static int +cmp_score(const void *a_, const void *b_) { + const Score *a = a_, *b = b_; +#define c(x) \ + if (a->x < b->x) return -1; \ + if (a->x > b->x) return 1; + c(variable_score); + c(style_score); + c(monospace_score); + c(width_score); + c(idx); +#undef c + return 0; +} + +// Returns a newly allocated array of sorted candidates that the caller must free +static const FSCandidate ** +sorted_candidates(FSContext *ctx, const Scorer *s, const FSCandidate *const *candidates, size_t count) { + WeightRange wr = {0}; + bool use_wr = false; + if (count) { + bool single_family = true; + for (size_t i = 1; i < count && single_family; i++) { + if (!streq(candidates[i]->family, candidates[0]->family)) single_family = false; + } + if (single_family) { + wr = weight_range_for_family(ctx, candidates[0]->family ? candidates[0]->family : ""); +#ifdef __APPLE__ + use_wr = wr.valid && wr.medium < 0; // Operator Mono is an example of this craziness +#else + use_wr = wr.valid && wr.medium < 100; // Operator Mono and Cascadia Code are examples +#endif + } + } + RAII_ALLOC(Score, scores, xcalloc(count, sizeof(Score))); + for (size_t i = 0; i < count; i++) { + scores[i] = score(s, candidates[i], use_wr ? &wr : NULL); + scores[i].idx = i; + } + qsort(scores, count, sizeof(Score), cmp_score); + const FSCandidate **ans = xcalloc(count, sizeof(FSCandidate *)); + for (size_t i = 0; i < count; i++) ans[i] = candidates[scores[i].idx]; + return ans; +} + +static const FSCandidate * +best_candidate(FSContext *ctx, const Scorer *s, const FSCandidate *const *candidates, size_t count) { + if (!count) return NULL; + RAII_ALLOC(const FSCandidate *, sorted, sorted_candidates(ctx, s, candidates, count)); + return sorted[0]; +} + +#ifndef __APPLE__ +// Return the best candidate that is not ignore_face or NULL +static const FSCandidate * +find_best_match_in_candidates(FSContext *ctx, const Scorer *s, const FSCandidateList *l, const FSFont *ignore_face) { + if (!l->count) return NULL; + RAII_ALLOC(const FSCandidate *, sorted, sorted_candidates(ctx, s, l->items, l->count)); + for (size_t i = 0; i < l->count; i++) { + FSFont f = font_from_candidate(sorted[i]); + if (!ignore_face || !fonts_equal(&f, ignore_face)) return sorted[i]; + } + return NULL; +} +#endif +// }}} + +// Selection {{{ +static bool +is_actually_variable_despite_fontconfigs_lies(FSContext *ctx, const FSFont *d) { +#ifdef __APPLE__ + (void)ctx; + (void)d; + return false; +#else + const char *path = d->base->path ? d->base->path : ""; + for (size_t i = 0; i < ctx->num_actually_variable; i++) { + if (strcmp(ctx->actually_variable[i].path, path) == 0) return ctx->actually_variable[i].val; + } + const FSCandidateList *l = variable_fonts(ctx, d->base->family, is_monospace(d)); + bool ans = false; + for (size_t i = 0; i < l->count && !ans; i++) { + if (streq(l->items[i]->path, path)) ans = true; + } + grow(ctx->actually_variable, ctx->num_actually_variable, ctx->actually_variable_capacity); + ctx->actually_variable[ctx->num_actually_variable].path = xstrdup(path); + ctx->actually_variable[ctx->num_actually_variable].val = ans; + ctx->num_actually_variable++; + return ans; +#endif +} + +static bool +font_is_variable(FSContext *ctx, const FSFont *f) { + return is_variable(f) || is_actually_variable_despite_fontconfigs_lies(ctx, f); +} + +static const FSCandidate *find_last_resort_text_font(FSContext *ctx, bool bold, bool italic, bool monospaced); + +#ifndef __APPLE__ +static const FSCandidate * +fc_match(FSContext *ctx, const char *family, bool bold, bool italic, int spacing) { + for (size_t i = 0; i < ctx->num_matches; i++) { + if (strcmp(ctx->matches[i].family, family) == 0 && ctx->matches[i].bold == bold && ctx->matches[i].italic == italic && + ctx->matches[i].spacing == spacing) + return ctx->matches[i].ans; + } + const FSCandidate *ans = fs_backend_fc_match(ctx, family, bold, italic, spacing); + grow(ctx->matches, ctx->num_matches, ctx->matches_capacity); + ctx->matches[ctx->num_matches].family = xstrdup(family); + ctx->matches[ctx->num_matches].bold = bold; + ctx->matches[ctx->num_matches].italic = italic; + ctx->matches[ctx->num_matches].spacing = spacing; + ctx->matches[ctx->num_matches].ans = ans; + ctx->num_matches++; + return ans; +} + +static const FSCandidate * +find_last_resort_text_font(FSContext *ctx, bool bold, bool italic, bool monospaced) { + // Use fc-match with a generic family + const FSCandidate *ans = fc_match(ctx, monospaced ? "monospace" : "sans-serif", bold, italic, FC_MONO); + if (!ans) fs_set_error(ctx, "Failed to find any font using fontconfig"); + return ans; +} + +static bool +find_best_match(FSContext *ctx, const char *family, bool bold, bool italic, bool monospaced, const FSFont *ignore_face, bool prefer_variable, FSFont *ans) { + const Scorer scorer = {.bold = bold, .italic = italic, .monospaced = monospaced, .prefer_variable = prefer_variable}; + // First look for an exact match + const FSListKind groups[] = {FS_LIST_POSTSCRIPT_NAME, FS_LIST_FULL_NAME, FS_LIST_FAMILY}; + for (size_t g = 0; g < arraysz(groups); g++) { + const FSCandidateList *cq = list_fonts(ctx, groups[g], family, monospaced); + if (!cq->count) continue; + // IBM Plex Mono has fullname of regular face == family_name under fontconfig + if (groups[g] == FS_LIST_FULL_NAME && streq(cq->items[0]->family, cq->items[0]->full_name)) continue; + const FSCandidate *exact_match = find_best_match_in_candidates(ctx, &scorer, cq, ignore_face); + if (exact_match) { + *ans = font_from_candidate(exact_match); + return true; + } + } + // Use fc-match to see if we can find a monospaced font that matches family + // When aliases are defined, spacing can cause the incorrect font to be + // returned, so check with and without spacing and use the one that matches. + const FSCandidate *mono_possibility = fc_match(ctx, family, false, false, FC_MONO); + const FSCandidate *dual_possibility = fc_match(ctx, family, false, false, FC_DUAL); + const FSCandidate *any_possibility = fc_match(ctx, family, false, false, 0); + const FSCandidate *tries[2] = {mono_possibility, dual_possibility}; + if (any_possibility && dual_possibility && fs_backend_fc_match_equal(any_possibility, dual_possibility)) { + tries[0] = dual_possibility; + tries[1] = mono_possibility; + } + for (size_t t = 0; t < arraysz(tries); t++) { + const FSCandidate *possibility = tries[t]; + if (!possibility) continue; + const struct { + const char *val; + FSListKind kind; + } q[] = { + {possibility->postscript_name, FS_LIST_POSTSCRIPT_NAME}, + {possibility->full_name, FS_LIST_FULL_NAME}, + {possibility->family, FS_LIST_FAMILY}, + }; + for (size_t k = 0; k < arraysz(q); k++) { + if (is_empty(q[k].val)) continue; + const FSCandidateList *candidates = list_fonts(ctx, q[k].kind, q[k].val, monospaced); + if (!candidates->count) continue; + if (candidates->count == 1) { + // happens if the family name is an alias, so we search with + // the actual family name to see if we can find all the + // fonts in the family. + const FSCandidateList *fc = list_fonts(ctx, FS_LIST_FAMILY, candidates->items[0]->family, monospaced); + if (fc->count > 1) candidates = fc; + } + *ans = font_from_candidate(best_candidate(ctx, &scorer, candidates->items, candidates->count)); + return true; + } + } + const FSCandidate *lr = find_last_resort_text_font(ctx, bold, italic, monospaced); + if (!lr) return false; + *ans = font_from_candidate(lr); + return true; +} +#else +static const FSCandidate * +find_last_resort_text_font(FSContext *ctx, bool bold, bool italic, bool monospaced) { + const FSCandidateList *candidates = list_fonts(ctx, FS_LIST_FAMILY, "Menlo", monospaced); + const Scorer scorer = {.bold = bold, .italic = italic, .monospaced = monospaced}; + const FSCandidate *ans = best_candidate(ctx, &scorer, candidates->items, candidates->count); + if (!ans) fs_set_error(ctx, "Failed to find the Menlo font"); + return ans; +} + +static bool +find_best_match(FSContext *ctx, const char *family, bool bold, bool italic, bool monospaced, const FSFont *ignore_face, bool prefer_variable, FSFont *ans) { + const Scorer scorer = {.bold = bold, .italic = italic, .monospaced = monospaced, .prefer_variable = prefer_variable}; + // First look for an exact match + const FSListKind groups[] = {FS_LIST_POSTSCRIPT_NAME, FS_LIST_FULL_NAME}; + for (size_t g = 0; g < arraysz(groups); g++) { + const FSCandidateList *candidates = list_fonts(ctx, groups[g], family, monospaced); + const FSCandidate *possible = best_candidate(ctx, &scorer, candidates->items, candidates->count); + if (possible) { + FSFont f = font_from_candidate(possible); + if (!fonts_equal(&f, ignore_face)) { + *ans = f; + return true; + } + } + } + // See if we have a variable font + if (!bold && !italic) { + const FSCandidateList *candidates = variable_fonts(ctx, family, monospaced); + const FSCandidate *possible = best_candidate(ctx, &scorer, candidates->items, candidates->count); + if (possible) { + FSFont f = font_from_candidate(possible); + if (!fonts_equal(&f, ignore_face)) { + *ans = find_medium_variant(ctx, f); + return true; + } + } + } + // Let CoreText choose the font if the family exists, otherwise fallback to Menlo + const FSCandidateList *candidates = list_fonts(ctx, FS_LIST_FAMILY, family, monospaced); + if (!candidates->count) { + if (!ieq(family, "monospace") && !ieq(family, "symbols nerd font mono")) + fs_add_message(ctx, "The font %s was not found, falling back to Menlo", family); + candidates = list_fonts(ctx, FS_LIST_FAMILY, "Menlo", monospaced); + } + const FSCandidate *c = best_candidate(ctx, &scorer, candidates->items, candidates->count); + if (!c) { + fs_set_error(ctx, "Failed to find the Menlo font"); + return false; + } + *ans = font_from_candidate(c); + return true; +} +#endif + +static FSFont +find_bold_italic_variant(FSContext *ctx, const FSFont *medium, bool bold, bool italic, const FamilyAxisValues *fav) { + // we first pick the best font file for bold/italic if there are more than + // one. For example SourceCodeVF has Italic and Upright faces with variable + // weights in each, so we rely on the OS font matcher to give us the best + // font file. + const bool monospaced = is_monospace(medium); + const FSCandidateList *unsorted = variable_fonts(ctx, medium->base->family, monospaced); + if (!unsorted->count) return *medium; + const Scorer scorer = {.bold = bold, .italic = italic, .monospaced = monospaced}; + const FSCandidate *best = best_candidate(ctx, &scorer, unsorted->items, unsorted->count); + const FSVariableData *vd = variable_data(ctx, best); + FSFont ans = font_from_candidate(best); + // now we need to specialise all axes in ans + AxisMap axis_values = {0}; + for (size_t i = 0; i < vd->num_axes; i++) { + const FSVarAxis *ax = vd->axes + i; + const FSDesignAxis *dax = NULL; + for (size_t d = 0; d < vd->num_design_axes; d++) { + if (vd->design_axes[d].tag == ax->tag) dax = vd->design_axes + d; + } + if (dax) axis_map_set(&axis_values, ax->tag, get_design_value_for(dax, ax, bold, italic, fav)); + } + if (axis_values.count) set_axis_values(&axis_values, &ans, vd); + return ans; +} + +static const FSCandidate * +find_best_variable_face(FSContext *ctx, const FSFontSpec *spec, bool bold, bool italic, bool monospaced, const FSCandidateList *candidates) { + if (spec->has_variable_name) { + for (size_t i = 0; i < candidates->count; i++) { + const FSVariableData *vd = variable_data(ctx, candidates->items[i]); + if (ieq(vd->variations_postscript_name_prefix, spec->variable_name)) return candidates->items[i]; + } + } + if (!is_empty(spec->style)) { + for (size_t i = 0; i < candidates->count; i++) { + const FSVariableData *vd = variable_data(ctx, candidates->items[i]); + for (size_t n = 0; n < vd->num_named_styles; n++) { + if (ieq(vd->named_styles[n].psname, spec->style)) return candidates->items[i]; + } + for (size_t n = 0; n < vd->num_named_styles; n++) { + if (ieq(vd->named_styles[n].name, spec->style)) return candidates->items[i]; + } + } + } + const Scorer scorer = {.bold = bold, .italic = italic, .monospaced = monospaced}; + return best_candidate(ctx, &scorer, candidates->items, candidates->count); +} + +static bool +apply_variation_to_pattern(FSContext *ctx, FSFont *pat, const FSFontSpec *spec) { + const FSVariableData *vd = variable_data(ctx, pat->base); + if (!is_empty(spec->style) && set_named_style(spec->style, pat, vd)) return true; + AxisMap axis_values = {0}; + for (size_t a = 0; a < spec->num_axes; a++) { + const char *qname = spec->axes[a].name; + const uint32_t qtag = tag_from_string(qname); + bool found = false; + for (size_t i = 0; i < vd->num_axes && !found; i++) { + char tag[5]; + tag_to_str(vd->axes[i].tag, tag); + if (strcmp(tag, qname) == 0) { + axis_map_set(&axis_values, qtag, spec->axes[a].value); + found = true; + } + } + if (found) continue; + // later axes with the same name take precedence, like a dict + uint32_t tag = 0; + for (size_t i = 0; i < vd->num_axes; i++) { + if (!is_empty(vd->axes[i].strid) && ieq(vd->axes[i].strid, qname)) tag = vd->axes[i].tag; + } + if (tag) axis_map_set(&axis_values, tag, spec->axes[a].value); + } + return set_axis_values(&axis_values, pat, vd); +} + +static bool +get_fine_grained_font( + FSContext *ctx, + const FSFontSpec *spec, + bool bold, + bool italic, + const FamilyAxisValues *fav, + const FSFont *resolved_medium_font, + bool *match_is_more_specific_than_family, + FSFont *ans) { + const bool monospaced = true; + const Scorer scorer = {.bold = bold, .italic = italic, .monospaced = monospaced}; + if (!is_empty(spec->postscript_name)) { + const FSCandidateList *l = list_fonts(ctx, FS_LIST_POSTSCRIPT_NAME, spec->postscript_name, monospaced); + const FSCandidate *q = best_candidate(ctx, &scorer, l->items, l->count); + if (q) { + *match_is_more_specific_than_family = true; + *ans = font_from_candidate(q); + return true; + } + } + if (!is_empty(spec->full_name)) { + const FSCandidateList *l = list_fonts(ctx, FS_LIST_FULL_NAME, spec->full_name, monospaced); + const FSCandidate *q = best_candidate(ctx, &scorer, l->items, l->count); + if (q) { + *match_is_more_specific_than_family = true; + *ans = font_from_candidate(q); + return true; + } + } + if (!is_empty(spec->family)) { + // First look for a variable font + const FSCandidateList *candidates = variable_fonts(ctx, spec->family, monospaced); + if (candidates->count) { + const FSCandidate *q = candidates->count == 1 ? candidates->items[0] : find_best_variable_face(ctx, spec, bold, italic, monospaced, candidates); + FSFont f = font_from_candidate(q); + if (apply_variation_to_pattern(ctx, &f, spec)) { + *match_is_more_specific_than_family = true; + *ans = f; + return true; + } + *ans = resolved_medium_font ? find_bold_italic_variant(ctx, resolved_medium_font, bold, italic, fav) : find_medium_variant(ctx, f); + return true; + } + // Now look for any font + const FSCandidateList *all = list_fonts(ctx, FS_LIST_FAMILY, spec->family, monospaced); + if (all->count) { + FSCandidateList filtered = {0}; + for (size_t i = 0; i < all->count; i++) { + if (is_empty(spec->style) || ieq(all->items[i]->style, spec->style)) fs_list_append(&filtered, all->items[i]); + } + const FSCandidate *q = best_candidate(ctx, &scorer, filtered.items, filtered.count); + free(filtered.items); + if (q) { + *ans = font_from_candidate(q); + return true; + } + } + } + const FSCandidate *lr = find_last_resort_text_font(ctx, bold, italic, monospaced); + if (!lr) return false; + *ans = font_from_candidate(lr); + return true; +} + +static bool +get_font_from_spec( + FSContext *ctx, + const FSFontSpec *spec, + bool bold, + bool italic, + const FamilyAxisValues *fav, + const FSFont *resolved_medium_font, + const FSFont *ignore_face, + bool *match_is_more_specific_than_family, + FSFont *ans) { + if (is_empty(spec->system)) return get_fine_grained_font(ctx, spec, bold, italic, fav, resolved_medium_font, match_is_more_specific_than_family, ans); + const char *family = spec->system; + if (strcmp(family, "auto") == 0) { + if (bold || italic) { + if (!resolved_medium_font) { + fs_set_error(ctx, "No resolved medium font when selecting the bold/italic font"); + return false; + } + family = resolved_medium_font->base->family ? resolved_medium_font->base->family : ""; + if (font_is_variable(ctx, resolved_medium_font)) { + *ans = find_bold_italic_variant(ctx, resolved_medium_font, bold, italic, fav); + return true; + } + } else family = "monospace"; + } + return find_best_match(ctx, family, bold, italic, true, ignore_face, false, ans); +} + +#ifndef __APPLE__ +static void +apply_synthetic_matrix(FSContext *ctx, FSFont *font, bool bold, bool italic) { + // fontconfig's FcFontList omits FC_MATRIX from its object set, so a roman + // font found there carries no synthetic-italic shear and its "italic" + // renders upright. For an italic slot whose chosen face is upright (no + // slant) and has no matrix yet, ask fc_match what fontconfig would do. + // fc_match returns a synthetic matrix only when there is no real italic + // to use, so a font that is already italic is never double-slanted. + if (!italic || font->has_matrix || font->base->has_matrix || font->base->slant != 0) return; + const FSCandidate *m = fc_match(ctx, font->base->family ? font->base->family : "", bold, italic, is_monospace(font) ? FC_MONO : -1); + if (m && m->has_matrix) { + font->has_matrix = true; + memcpy(font->matrix, m->matrix, sizeof(font->matrix)); + } +} +#endif + +static bool +get_font_files(FSContext *ctx) { + bool match_is_more_specific_than_family = false; + FamilyAxisValues fav = {0}; + FSFont medium; + if (!get_font_from_spec(ctx, &ctx->specs[0], false, false, &fav, NULL, NULL, &match_is_more_specific_than_family, &medium)) return false; + const bool medium_font_is_variable = font_is_variable(ctx, &medium); + if (!match_is_more_specific_than_family && medium_font_is_variable) medium = find_medium_variant(ctx, medium); + if (medium_font_is_variable) { + AxisMap av; + get_axis_values(&medium, variable_data(ctx, medium.base), &av); + set_regular_values(&fav, &av); + } + ctx->results[0] = medium; + // In the Python implementation the medium font was compared to candidates + // after its features were added, so it never compared equal to them. + const FSFont *ignore_face = (is_empty(ctx->specs[0].system) && ctx->specs[0].has_features) ? NULL : &medium; + static const bool styles[3][2] = {{true, false}, {false, true}, {true, true}}; + for (size_t i = 0; i < 3; i++) { + const bool bold = styles[i][0], italic = styles[i][1]; + bool unused = false; + FSFont font; + if (!get_font_from_spec(ctx, &ctx->specs[i + 1], bold, italic, &fav, &medium, ignore_face, &unused, &font)) return false; + // Set family axis values based on the values in font + if (!(bold && italic) && font_is_variable(ctx, &medium)) { + AxisMap av; + get_axis_values(&font, variable_data(ctx, font.base), &av); + if (italic) set_italic_values(&fav, &av); + else set_bold_values(&fav, &av); + } + ctx->results[i + 1] = font; + } +#ifndef __APPLE__ + apply_synthetic_matrix(ctx, &ctx->results[2], false, true); + apply_synthetic_matrix(ctx, &ctx->results[3], true, true); +#endif + return true; +} + +static bool +select_symbol_fonts(FSContext *ctx) { + if (!ctx->num_symbol_families) return true; + ctx->symbols = xcalloc(ctx->num_symbol_families, sizeof(FSFont)); + ctx->symbol_bold = xcalloc(ctx->num_symbol_families, sizeof(bool)); + ctx->symbol_italic = xcalloc(ctx->num_symbol_families, sizeof(bool)); + for (size_t i = 0; i < ctx->num_symbol_families; i++) { + FSFont *f = ctx->symbols + i; + if (!find_best_match(ctx, ctx->symbol_families[i], false, false, false, NULL, false, f)) return false; +#ifdef __APPLE__ + ctx->symbol_bold[i] = f->base->bold; + ctx->symbol_italic[i] = f->base->italic; +#else + ctx->symbol_bold[i] = f->base->weight >= FC_WEIGHT_SEMIBOLD; + ctx->symbol_italic[i] = f->base->slant != FC_SLANT_ROMAN; +#endif + } + return true; +} + +static void +run_selection(FSContext *ctx) { + if (!fs_backend_init(ctx)) { + fs_set_error(ctx, "Failed to initialize the font database"); + return; + } + if (get_font_files(ctx)) select_symbol_fonts(ctx); +} +// }}} + +// Python interface {{{ +PyObject *fs_backend_descriptor(const FSFont *f); + +static struct { + FSContext *ctx; + pthread_t thread; + bool thread_running; +} pending = {0}; + +static void * +selection_thread(void *x) { + set_thread_name("KittyFontSelect"); + run_selection((FSContext *)x); + return NULL; +} + +static void +wait_for_thread(void) { + if (!pending.thread_running) return; + const monotonic_t start = monotonic(); + int ret = pthread_join(pending.thread, NULL); + pending.thread_running = false; + if (ret != 0) fatal("Failed to join the font selection thread with error: %s", strerror(ret)); + debug_rendering("Waited %.2f ms for the font selection thread to finish\n", monotonic_t_to_s_double(monotonic() - start) * 1000); +} + +void +fs_wait_for_selection_thread(void) { + wait_for_thread(); +} + +void +fs_finalize_selection(void) { + wait_for_thread(); + free_context(pending.ctx); + pending.ctx = NULL; +} + +static char * +optional_string(PyObject *x, bool *is_set) { + if (is_set) *is_set = x != Py_None; + if (x == Py_None) return NULL; + const char *s = PyUnicode_AsUTF8(x); + return s ? xstrdup(s) : NULL; +} + +static bool +parse_spec(PyObject *src, FSFontSpec *spec) { + PyObject *family, *style, *postscript_name, *full_name, *system, *variable_name, *axes; + int has_features = 0; + if (!PyArg_ParseTuple(src, "OOOOOOO!p", &family, &style, &postscript_name, &full_name, &system, &variable_name, &PyTuple_Type, &axes, &has_features)) + return false; + spec->has_features = has_features; +#define S(x) \ + if (x != Py_None && !PyUnicode_Check(x)) { \ + PyErr_SetString(PyExc_TypeError, "The font specification field " #x " must be a string or None"); \ + return false; \ + } \ + spec->x = optional_string(x, NULL); \ + if (PyErr_Occurred()) return false; + S(family); + S(style); + S(postscript_name); + S(full_name); + S(system); + S(variable_name); +#undef S + spec->has_variable_name = variable_name != Py_None; + spec->num_axes = PyTuple_GET_SIZE(axes); + spec->axes = xcalloc(spec->num_axes, sizeof(spec->axes[0])); + for (size_t i = 0; i < spec->num_axes; i++) { + const char *name; + double value; + if (!PyArg_ParseTuple(PyTuple_GET_ITEM(axes, i), "sd", &name, &value)) return false; + spec->axes[i].name = xstrdup(name); + spec->axes[i].value = value; + } + return true; +} + +static PyObject * +start_font_selection(PyObject *self UNUSED, PyObject *args) { + PyObject *specs, *families; + int threaded = 1; + if (!PyArg_ParseTuple(args, "O!O!|p", &PyTuple_Type, &specs, &PyTuple_Type, &families, &threaded)) return NULL; + if (PyTuple_GET_SIZE(specs) != 4) { + PyErr_SetString(PyExc_ValueError, "Must specify exactly four font specifications"); + return NULL; + } + // discard any previous selection + fs_finalize_selection(); + FSContext *ctx = xcalloc(1, sizeof(FSContext)); + for (size_t i = 0; i < 4; i++) { + PyObject *s = PyTuple_GET_ITEM(specs, i); + if (!PyTuple_Check(s)) { + PyErr_SetString(PyExc_TypeError, "Font specifications must be tuples"); + free_context(ctx); + return NULL; + } + if (!parse_spec(s, ctx->specs + i)) { + free_context(ctx); + return NULL; + } + } + ctx->num_symbol_families = PyTuple_GET_SIZE(families); + ctx->symbol_families = xcalloc(ctx->num_symbol_families, sizeof(char *)); + for (size_t i = 0; i < ctx->num_symbol_families; i++) { + PyObject *f = PyTuple_GET_ITEM(families, i); + const char *s = PyUnicode_Check(f) ? PyUnicode_AsUTF8(f) : NULL; + if (!s) { + if (!PyErr_Occurred()) PyErr_SetString(PyExc_TypeError, "Symbol map font families must be strings"); + free_context(ctx); + return NULL; + } + ctx->symbol_families[i] = xstrdup(s); + } + pending.ctx = ctx; + if (threaded) { + int ret = pthread_create(&pending.thread, NULL, selection_thread, ctx); + if (ret == 0) { + pending.thread_running = true; + Py_RETURN_NONE; + } + log_error("Failed to start thread to select fonts, selecting synchronously, with error: %s", strerror(ret)); + } + run_selection(ctx); + Py_RETURN_NONE; +} + +static PyObject * +selection_result(const FSContext *ctx) { + if (ctx->error) { + PyErr_Format(PyExc_ValueError, "Failed to select fonts: %s", ctx->error); + return NULL; + } + RAII_PyObject(symbols, PyTuple_New(ctx->num_symbol_families)); + if (!symbols) return NULL; + for (size_t i = 0; i < ctx->num_symbol_families; i++) { + RAII_PyObject(d, fs_backend_descriptor(ctx->symbols + i)); + if (!d) return NULL; + PyObject *t = Py_BuildValue("OOO", d, ctx->symbol_bold[i] ? Py_True : Py_False, ctx->symbol_italic[i] ? Py_True : Py_False); + if (!t) return NULL; + PyTuple_SET_ITEM(symbols, i, t); + } + RAII_PyObject(medium, fs_backend_descriptor(ctx->results + 0)); + if (!medium) return NULL; + RAII_PyObject(bold, fs_backend_descriptor(ctx->results + 1)); + if (!bold) return NULL; + RAII_PyObject(italic, fs_backend_descriptor(ctx->results + 2)); + if (!italic) return NULL; + RAII_PyObject(bi, fs_backend_descriptor(ctx->results + 3)); + if (!bi) return NULL; + return Py_BuildValue("{sO sO sO sO sO}", "medium", medium, "bold", bold, "italic", italic, "bi", bi, "symbols", symbols); +} + +static PyObject * +finish_font_selection(PyObject *self UNUSED, PyObject *args UNUSED) { + if (!pending.ctx) { + PyErr_SetString(PyExc_RuntimeError, "No font selection is in progress"); + return NULL; + } + wait_for_thread(); + FSContext *ctx = pending.ctx; + pending.ctx = NULL; + for (size_t i = 0; i < ctx->num_messages; i++) log_error("%s", ctx->messages[i]); + PyObject *ans = selection_result(ctx); + free_context(ctx); + return ans; +} + +static PyObject * +axis_values_to_python(const FSAxisValue *values, size_t count) { + RAII_PyObject(ans, PyDict_New()); + if (!ans) return NULL; + for (size_t i = 0; i < count; i++) { + char tag[5]; + tag_to_str(values[i].tag, tag); + RAII_PyObject(v, PyFloat_FromDouble(values[i].value)); + if (!v || PyDict_SetItemString(ans, tag, v) != 0) return NULL; + } + Py_INCREF(ans); + return ans; +} + +static PyObject * +variable_data_to_python(const FSVariableData *vd) { + RAII_PyObject(axes, PyTuple_New(vd->num_axes)); + RAII_PyObject(named_styles, PyTuple_New(vd->num_named_styles)); + RAII_PyObject(design_axes, PyTuple_New(vd->num_design_axes)); + if (!axes || !named_styles || !design_axes) return NULL; + char tag[5]; + for (size_t i = 0; i < vd->num_axes; i++) { + const FSVarAxis *a = vd->axes + i; + tag_to_str(a->tag, tag); + PyObject *x = Py_BuildValue( + "{sd sd sd sO ss ss}", + "minimum", + a->minimum, + "maximum", + a->maximum, + "default", + a->def, + "hidden", + a->hidden ? Py_True : Py_False, + "tag", + tag, + "strid", + a->strid); + if (!x) return NULL; + PyTuple_SET_ITEM(axes, i, x); + } + for (size_t i = 0; i < vd->num_named_styles; i++) { + const FSNamedStyle *ns = vd->named_styles + i; + PyObject *x = Py_BuildValue("{sN ss ss}", "axis_values", axis_values_to_python(ns->values, ns->num_values), "name", ns->name, "psname", ns->psname); + if (!x) return NULL; + PyTuple_SET_ITEM(named_styles, i, x); + } + for (size_t i = 0; i < vd->num_design_axes; i++) { + const FSDesignAxis *da = vd->design_axes + i; + RAII_PyObject(values, PyList_New(da->num_values)); + if (!values) return NULL; + for (size_t j = 0; j < da->num_values; j++) { + const FSDesignValue *v = da->values + j; + PyObject *x = NULL; + switch (v->format) { + case 1: x = Py_BuildValue("{sI sI ss sd}", "format", v->format, "flags", v->flags, "name", v->name, "value", v->value); break; + case 2: + x = Py_BuildValue( + "{sI sI ss sd sd sd}", + "format", + v->format, + "flags", + v->flags, + "name", + v->name, + "value", + v->value, + "minimum", + v->minimum, + "maximum", + v->maximum); + break; + default: + x = Py_BuildValue( + "{sI sI ss sd sd}", "format", v->format, "flags", v->flags, "name", v->name, "value", v->value, "linked_value", v->linked_value); + break; + } + if (!x) return NULL; + PyList_SET_ITEM(values, j, x); + } + tag_to_str(da->tag, tag); + PyObject *x = Py_BuildValue("{ss ss sI sO}", "tag", tag, "name", da->name, "ordering", da->ordering, "values", values); + if (!x) return NULL; + PyTuple_SET_ITEM(design_axes, i, x); + } + return Py_BuildValue( + "{sO sO sO ss ss}", + "axes", + axes, + "named_styles", + named_styles, + "design_axes", + design_axes, + "variations_postscript_name_prefix", + vd->variations_postscript_name_prefix, + "elided_fallback_name", + vd->elided_fallback_name); +} + +static PyObject * +variable_data_from_font_file(PyObject *self UNUSED, PyObject *args) { + const char *path; + int index = 0; + if (!PyArg_ParseTuple(args, "s|i", &path, &index)) return NULL; + FSFontTables t = {0}; + if (!fs_read_font_tables_from_file(path, index, &t)) { + PyErr_SetFromErrnoWithFilename(PyExc_OSError, path); + return NULL; + } + FSVariableData vd; + fs_variable_data_from_tables(&t, &vd); + fs_free_font_tables(&t); + PyObject *ans = variable_data_to_python(&vd); + fs_free_variable_data(&vd); + return ans; +} + +static PyMethodDef module_methods[] = { + METHODB(start_font_selection, METH_VARARGS), + METHODB(finish_font_selection, METH_NOARGS), + METHODB(variable_data_from_font_file, METH_VARARGS), + {NULL, NULL, 0, NULL} /* Sentinel */ +}; + +bool +init_font_select(PyObject *module) { + if (PyModule_AddFunctions(module, module_methods) != 0) return false; + return true; +} +// }}} diff --git a/kitty/font-select.h b/kitty/font-select.h new file mode 100644 index 000000000..fdf21123f --- /dev/null +++ b/kitty/font-select.h @@ -0,0 +1,161 @@ +/* + * font-select.h + * Copyright (C) 2026 Kovid Goyal + * + * Distributed under terms of the GPL3 license. + */ + +#pragma once + +// Selection of the text fonts (and the symbol_map fonts) from the font +// specifications in kitty.conf. This is pure C code, it does not use Python, +// so that it can run in a thread while the rest of kitty starts up. The +// platform specific parts (listing fonts, matching fonts, reading font tables +// and converting the results to Python descriptors) are implemented by the +// backends in fontconfig.c and core_text.m. + +#include +#include +#include + +#define FS_MAX_AXES 64 + +typedef struct FSAxisValue { + uint32_t tag; + double value; +} FSAxisValue; + +// A font as reported by the platform font database +typedef struct FSCandidate { + void *handle; // FcPattern* or CTFontDescriptorRef, owned by the candidate + char *path, *family, *style, *full_name, *postscript_name; + int index; + // fontconfig: FC_WEIGHT, FC_SLANT, FC_WIDTH. CoreText: the normalized traits + double weight, slant, width; + bool variable; // fontconfig: FC_VARIABLE. CoreText: has a variation dictionary + bool monospace; // fontconfig: spacing is MONO or DUAL. CoreText: the monospace trait + bool spacing_is_mono; // fontconfig: spacing is MONO + bool expanded, condensed, bold, italic; // CoreText traits + unsigned variation_count; // CoreText: number of entries in the variation dictionary + bool from_match; // fontconfig: created by FcFontMatch rather than FcFontList + bool has_matrix; // fontconfig: has FC_MATRIX + double matrix[4]; +} FSCandidate; + +// A candidate font specialized with variable font axis values +typedef struct FSFont { + const FSCandidate *base; + int named_style; // fontconfig: index of named style or -1 + // fontconfig: the "axes" values in the order of the fvar axes + // CoreText: the "axis_map" dictionary + bool has_axes; + size_t num_axes; + FSAxisValue axes[FS_MAX_AXES]; + bool has_matrix; + double matrix[4]; +} FSFont; + +typedef struct FSCandidateList { + const FSCandidate **items; + size_t count, capacity; +} FSCandidateList; + +typedef enum FSListKind { FS_LIST_FAMILY, FS_LIST_POSTSCRIPT_NAME, FS_LIST_FULL_NAME } FSListKind; + +typedef struct FSFontSpec { + char *family, *style, *postscript_name, *full_name, *system, *variable_name; + bool has_variable_name; // variable_name can be specified as the empty string + bool has_features; // features are applied to the selected font by the Python code + size_t num_axes; + struct { + char *name; + double value; + } *axes; +} FSFontSpec; + +// Variable font data {{{ +typedef struct FSVarAxis { + double minimum, maximum, def; + bool hidden; + uint32_t tag; + char *strid; +} FSVarAxis; + +typedef struct FSNamedStyle { + char *name, *psname; + size_t num_values; + FSAxisValue *values; +} FSNamedStyle; + +typedef struct FSDesignValue { + unsigned format, flags; + char *name; + double value, minimum, maximum, linked_value; +} FSDesignValue; + +typedef struct FSDesignAxis { + uint32_t tag; + char *name; + unsigned ordering; + size_t num_values, capacity; + FSDesignValue *values; +} FSDesignAxis; + +typedef struct FSVariableData { + char *variations_postscript_name_prefix, *elided_fallback_name; + size_t num_axes, num_named_styles, num_design_axes; + FSVarAxis *axes; + FSNamedStyle *named_styles; + FSDesignAxis *design_axes; +} FSVariableData; + +// Raw font tables needed to compute variable font data, owned by the struct +typedef struct FSFontTables { + uint8_t *fvar, *stat, *name; + size_t fvar_len, stat_len, name_len; +} FSFontTables; + +void fs_free_font_tables(FSFontTables *t); +void fs_free_variable_data(FSVariableData *vd); +bool fs_variable_data_from_tables(const FSFontTables *t, FSVariableData *vd); +// Read the tables for face index (the low 16 bits of index) from an sfnt font file +bool fs_read_font_tables_from_file(const char *path, int index, FSFontTables *t); +// }}} + +typedef struct FSContext FSContext; + +// Helpers for backends {{{ +// The key used to compare font names: whitespace collapsed and stripped, lowercased. Caller must free. +char *fs_name_to_key(const char *name); +// Allocate a new candidate owned by the context +FSCandidate *fs_new_candidate(FSContext *ctx); +bool fs_list_append(FSCandidateList *l, const FSCandidate *c); +void fs_set_error(FSContext *ctx, const char *fmt, ...) __attribute__((format(printf, 2, 3))); +void fs_add_message(FSContext *ctx, const char *fmt, ...) __attribute__((format(printf, 2, 3))); +// Backend private data, freed with fs_backend_free_context_data() +void **fs_backend_data(FSContext *ctx); +// }}} + +// Backend API {{{ +// Initialize the font database, must be thread safe +bool fs_backend_init(FSContext *ctx); +void fs_backend_free_context_data(void *data); +void fs_backend_free_candidate(FSCandidate *c); +// Append the fonts whose name of the specified kind matches key (as returned by fs_name_to_key()). +// name is the name as specified by the user. For FS_LIST_FULL_NAME on +// CoreText the full name is the family name followed by the style name. +bool fs_backend_list(FSContext *ctx, FSListKind kind, const char *name, const char *key, bool monospaced, FSCandidateList *out); +bool fs_backend_font_tables(FSContext *ctx, const FSCandidate *c, FSFontTables *out); +#ifndef __APPLE__ +// Equivalent to the Python fc_match(), returns NULL on failure +const FSCandidate *fs_backend_fc_match(FSContext *ctx, const char *family, bool bold, bool italic, int spacing); +// Whether two candidates created by fs_backend_fc_match() are equal +bool fs_backend_fc_match_equal(const FSCandidate *a, const FSCandidate *b); +#endif +// }}} + +// Called by the backend finalize functions before the font database is +// finalized, waits for any selection running in a thread and frees it. +void fs_finalize_selection(void); +// Wait for any selection running in a thread to finish, call before modifying the font database +void fs_wait_for_selection_thread(void); diff --git a/kitty/fontconfig.c b/kitty/fontconfig.c index 40f89b397..fddc3a3d1 100644 --- a/kitty/fontconfig.c +++ b/kitty/fontconfig.c @@ -11,12 +11,15 @@ #include #include #include "freetype_render_ui_text.h" +#include "font-select.h" +#include #ifndef FC_COLOR #define FC_COLOR "color" #endif static bool initialized = false; +static pthread_mutex_t init_lock = PTHREAD_MUTEX_INITIALIZER; static void *libfontconfig_handle = NULL; static struct { PyObject *face, *descriptor; @@ -51,6 +54,8 @@ static struct { #define FcPatternGetCharSet dynamically_loaded_fc_symbol.PatternGetCharSet #define FcCharSetHasChar dynamically_loaded_fc_symbol.CharSetHasChar #define FcConfigGetCurrent dynamically_loaded_fc_symbol.ConfigGetCurrent +#define FcPatternReference dynamically_loaded_fc_symbol.PatternReference +#define FcPatternEqualSubset dynamically_loaded_fc_symbol.PatternEqualSubset static struct { FcBool (*Init)(void); @@ -82,6 +87,8 @@ static struct { FcResult (*PatternGetCharSet)(const FcPattern *p, const char *object, int n, FcCharSet **c); FcBool (*CharSetHasChar)(const FcCharSet *fcs, FcChar32 ucs4); FcConfig *(*ConfigGetCurrent)(void); + void (*PatternReference)(FcPattern *p); + FcBool (*PatternEqualSubset)(const FcPattern *pa, const FcPattern *pb, const FcObjectSet *os); } dynamically_loaded_fc_symbol = {0}; #define LOAD_FUNC(name) \ { \ @@ -139,22 +146,29 @@ load_fontconfig_lib(void) { LOAD_FUNC(PatternGetCharSet); LOAD_FUNC(CharSetHasChar); LOAD_FUNC(ConfigGetCurrent); + LOAD_FUNC(PatternReference); + LOAD_FUNC(PatternEqualSubset); } #undef LOAD_FUNC +// Can be called from the font selection thread, see font-select.c static void ensure_initialized(void) { + if (__atomic_load_n(&initialized, __ATOMIC_ACQUIRE)) return; + pthread_mutex_lock(&init_lock); if (!initialized) { load_fontconfig_lib(); if (!FcInit()) fatal("Failed to initialize fontconfig library"); - initialized = true; + __atomic_store_n(&initialized, true, __ATOMIC_RELEASE); } + pthread_mutex_unlock(&init_lock); } static void clear_fallback_candidates(void); static void finalize(void) { + fs_finalize_selection(); if (initialized) { Py_CLEAR(builtin_nerd_font.face); Py_CLEAR(builtin_nerd_font.descriptor); @@ -896,6 +910,7 @@ end: static PyObject * add_font_file(PyObject UNUSED *self, PyObject *args) { ensure_initialized(); + fs_wait_for_selection_thread(); const char *path = NULL; if (!PyArg_ParseTuple(args, "s", &path)) return NULL; if (FcConfigAppFontAddFile(NULL, (const unsigned char *)path)) { @@ -936,6 +951,310 @@ fc_match_fallback(PyObject UNUSED *self, PyObject *args, PyObject *kw) { #undef AP +// Font selection backend, see font-select.h {{{ +static char * +pattern_string(FcPattern *pat, const char *object) { + FcChar8 *out = NULL; + if (FcPatternGetString(pat, object, 0, &out) != FcResultMatch || !out) out = (FcChar8 *)""; + char *ans = strdup((const char *)out); + if (!ans) fatal("Out of memory"); + return ans; +} + +static int +pattern_int(FcPattern *pat, const char *object, int def) { + int ans; + return FcPatternGetInteger(pat, object, 0, &ans) == FcResultMatch ? ans : def; +} + +static bool +pattern_bool(FcPattern *pat, const char *object) { + FcBool ans; + return FcPatternGetBool(pat, object, 0, &ans) == FcResultMatch && ans; +} + +// Takes a new reference to pat +static FSCandidate * +candidate_from_pattern(FSContext *ctx, FcPattern *pat, bool from_match) { + FSCandidate *c = fs_new_candidate(ctx); + FcPatternReference(pat); + c->handle = pat; + c->from_match = from_match; + c->path = pattern_string(pat, FC_FILE); + c->family = pattern_string(pat, FC_FAMILY); + c->style = pattern_string(pat, FC_STYLE); + c->full_name = pattern_string(pat, FC_FULLNAME); + c->postscript_name = pattern_string(pat, FC_POSTSCRIPT_NAME); + c->index = pattern_int(pat, FC_INDEX, 0); + c->weight = pattern_int(pat, FC_WEIGHT, 0); + c->slant = pattern_int(pat, FC_SLANT, 0); + c->width = pattern_int(pat, FC_WIDTH, 0); + c->variable = pattern_bool(pat, FC_VARIABLE); + const int spacing = pattern_int(pat, FC_SPACING, 0); + c->monospace = spacing == FC_MONO || spacing == FC_DUAL; + c->spacing_is_mono = spacing == FC_MONO; + FcMatrix *mtx = NULL; + if (FcPatternGetMatrix(pat, FC_MATRIX, 0, &mtx) == FcResultMatch && mtx) { + c->has_matrix = true; + c->matrix[0] = mtx->xx; + c->matrix[1] = mtx->xy; + c->matrix[2] = mtx->yx; + c->matrix[3] = mtx->yy; + } + return c; +} + +// Listing fonts with FcFontList() is dominated by scanning all fonts, so +// list all fonts once and filter them in C, instead of listing the fonts +// matching each name. +typedef struct FCListEntry { + const FSCandidate *candidate; + char *keys[3]; // indexed by FSListKind +} FCListEntry; + +typedef struct FCFontList { + bool loaded; + FCListEntry *items; + size_t count, capacity; +} FCFontList; + +typedef struct FCBackendData { + FCFontList lists[2]; // indexed by monospaced +} FCBackendData; + +bool +fs_backend_init(FSContext *ctx UNUSED) { + ensure_initialized(); + return true; +} + +void +fs_backend_free_context_data(void *data) { + FCBackendData *d = data; + for (size_t l = 0; l < arraysz(d->lists); l++) { + for (size_t i = 0; i < d->lists[l].count; i++) { + for (size_t k = 0; k < arraysz(d->lists[l].items[i].keys); k++) free(d->lists[l].items[i].keys[k]); + } + free(d->lists[l].items); + } + free(d); +} + +void +fs_backend_free_candidate(FSCandidate *c) { + if (c->handle) FcPatternDestroy((FcPattern *)c->handle); + c->handle = NULL; +} + +static bool +list_with_spacing(FSContext *ctx, int spacing, bool allow_bitmapped_fonts, FCFontList *out) { + FcPattern *pat = FcPatternCreate(); + FcObjectSet *os = NULL; + FcFontSet *fs = NULL; + bool ok = false; + if (!pat) { + fs_set_error(ctx, "Out of memory creating fontconfig pattern"); + return false; + } +#define A(func, which, val) \ + if (!func(pat, which, val)) { \ + fs_set_error(ctx, "Failed to add %s to fontconfig pattern", which); \ + goto end; \ + } + if (!allow_bitmapped_fonts) { + A(FcPatternAddBool, FC_OUTLINE, FcTrue); + A(FcPatternAddBool, FC_SCALABLE, FcTrue); + } + if (spacing > -1) A(FcPatternAddInteger, FC_SPACING, spacing); +#undef A + // Must be kept in sync with fc_list() + os = FcObjectSetBuild( + FC_FILE, + FC_POSTSCRIPT_NAME, + FC_FAMILY, + FC_STYLE, + FC_FULLNAME, + FC_WEIGHT, + FC_WIDTH, + FC_SLANT, + FC_HINT_STYLE, + FC_INDEX, + FC_HINTING, + FC_SCALABLE, + FC_OUTLINE, + FC_COLOR, + FC_SPACING, + FC_VARIABLE, +#ifdef FC_NAMED_INSTANCE + FC_NAMED_INSTANCE, +#endif + NULL); + if (!os) { + fs_set_error(ctx, "Failed to create fontconfig object set"); + goto end; + } + fs = FcFontList(NULL, pat, os); + if (!fs) { + fs_set_error(ctx, "Failed to create fontconfig font set"); + goto end; + } + for (int i = 0; i < fs->nfont; i++) { + FcPattern *p = fs->fonts[i]; + FcChar8 *path = NULL; + // Only fonts with a path are usable + if (FcPatternGetString(p, FC_FILE, 0, &path) != FcResultMatch || !path || !path[0]) continue; + const FSCandidate *c = candidate_from_pattern(ctx, p, false); + if (out->count >= out->capacity) { + out->capacity = out->capacity ? 2 * out->capacity : 256; + out->items = realloc(out->items, out->capacity * sizeof(out->items[0])); + if (!out->items) fatal("Out of memory"); + } + // fontconfig patterns can have multiple values for these, use only the first, like the Python fc_list() + FCListEntry *e = out->items + out->count++; + e->candidate = c; + e->keys[FS_LIST_FAMILY] = fs_name_to_key(c->family); + e->keys[FS_LIST_POSTSCRIPT_NAME] = fs_name_to_key(c->postscript_name); + e->keys[FS_LIST_FULL_NAME] = fs_name_to_key(c->full_name); + } + ok = true; +end: + FcPatternDestroy(pat); + if (os) FcObjectSetDestroy(os); + if (fs) FcFontSetDestroy(fs); + return ok; +} + +bool +fs_backend_list(FSContext *ctx, FSListKind kind, const char *name UNUSED, const char *key, bool monospaced, FSCandidateList *out) { + FCBackendData **d = (FCBackendData **)fs_backend_data(ctx); + if (!*d) { + *d = calloc(1, sizeof(FCBackendData)); + if (!*d) fatal("Out of memory"); + } + FCFontList *l = (*d)->lists + (monospaced ? 1 : 0); + if (!l->loaded) { + l->loaded = true; + if (monospaced) { + if (!list_with_spacing(ctx, FC_DUAL, false, l) || !list_with_spacing(ctx, FC_MONO, false, l)) return false; + } else { + // allow non-monospaced and bitmapped fonts as these are used for symbol_map + if (!list_with_spacing(ctx, -1, true, l)) return false; + } + } + for (size_t i = 0; i < l->count; i++) { + if (strcmp(l->items[i].keys[kind], key) == 0) fs_list_append(out, l->items[i].candidate); + } + return true; +} + +const FSCandidate * +fs_backend_fc_match(FSContext *ctx, const char *family, bool bold, bool italic, int spacing) { + FcPattern *pat = FcPatternCreate(), *match = NULL; + const FSCandidate *ans = NULL; + if (!pat) { + fs_set_error(ctx, "Out of memory creating fontconfig pattern"); + return NULL; + } +#define A(func, which, val) \ + if (!func(pat, which, val)) { \ + fs_set_error(ctx, "Failed to add %s to fontconfig pattern", which); \ + goto end; \ + } + // Must be kept in sync with fc_match() + if (family && family[0]) A(FcPatternAddString, FC_FAMILY, (const FcChar8 *)family); + if (spacing >= FC_DUAL) { + // pass the family,monospace as the family parameter to fc-match, + // which will fallback to using monospace if the family does not match. + A(FcPatternAddString, FC_FAMILY, (const FcChar8 *)"monospace"); + A(FcPatternAddInteger, FC_SPACING, spacing); + } + A(FcPatternAddBool, FC_OUTLINE, FcTrue); + A(FcPatternAddBool, FC_SCALABLE, FcTrue); + if (bold) A(FcPatternAddInteger, FC_WEIGHT, FC_WEIGHT_BOLD); + if (italic) A(FcPatternAddInteger, FC_SLANT, FC_SLANT_ITALIC); +#undef A + FcResult result; + FcConfigSubstitute(NULL, pat, FcMatchPattern); + FcDefaultSubstitute(pat); + match = FcFontMatch(NULL, pat, &result); + if (!match) { + fs_set_error(ctx, "FcFontMatch() failed for family: %s", family ? family : ""); + goto end; + } + ans = candidate_from_pattern(ctx, match, true); +end: + FcPatternDestroy(pat); + if (match) FcPatternDestroy(match); + return ans; +} + +bool +fs_backend_fc_match_equal(const FSCandidate *a, const FSCandidate *b) { + // The properties used by pattern_as_dict() + FcObjectSet *os = FcObjectSetBuild( + FC_FILE, + FC_FAMILY, + FC_STYLE, + FC_FULLNAME, + FC_POSTSCRIPT_NAME, + FC_FONT_FEATURES, + FC_VARIABLE, +#ifdef FC_NAMED_INSTANCE + FC_NAMED_INSTANCE, +#endif + FC_WEIGHT, + FC_WIDTH, + FC_SLANT, + FC_HINT_STYLE, + FC_INDEX, + FC_RGBA, + FC_LCD_FILTER, + FC_HINTING, + FC_SCALABLE, + FC_OUTLINE, + FC_COLOR, + FC_SPACING, + FC_MATRIX, + NULL); + if (!os) return a == b; + const bool ans = FcPatternEqualSubset((const FcPattern *)a->handle, (const FcPattern *)b->handle, os); + FcObjectSetDestroy(os); + return ans; +} + +bool +fs_backend_font_tables(FSContext *ctx UNUSED, const FSCandidate *c, FSFontTables *out) { + if (!c->path || !c->path[0]) return false; + return fs_read_font_tables_from_file(c->path, c->index, out); +} + +PyObject * +fs_backend_descriptor(const FSFont *f) { + RAII_PyObject(ans, pattern_as_dict((FcPattern *)f->base->handle)); + if (!ans) return NULL; + if (f->named_style > -1) { + RAII_PyObject(ns, PyLong_FromLong(f->named_style)); + if (!ns || PyDict_SetItemString(ans, "named_style", ns) != 0) return NULL; + } + if (f->has_axes) { + RAII_PyObject(axes, PyTuple_New(f->num_axes)); + if (!axes) return NULL; + for (size_t i = 0; i < f->num_axes; i++) { + PyObject *v = PyFloat_FromDouble(f->axes[i].value); + if (!v) return NULL; + PyTuple_SET_ITEM(axes, i, v); + } + if (PyDict_SetItemString(ans, "axes", axes) != 0) return NULL; + } + if (f->has_matrix) { + RAII_PyObject(t, Py_BuildValue("(dddd)", f->matrix[0], f->matrix[1], f->matrix[2], f->matrix[3])); + if (!t || PyDict_SetItemString(ans, "matrix", t) != 0) return NULL; + } + Py_INCREF(ans); + return ans; +} +// }}} + static PyMethodDef module_methods[] = { {"fc_list", (PyCFunction)(void (*)(void))(fc_list), METH_VARARGS | METH_KEYWORDS, NULL}, METHODB(fc_match, METH_VARARGS), diff --git a/kitty/fonts/__init__.py b/kitty/fonts/__init__.py index 26a767557..d292b04c4 100644 --- a/kitty/fonts/__init__.py +++ b/kitty/fonts/__init__.py @@ -1,4 +1,3 @@ -from collections.abc import Sequence from enum import Enum, IntEnum, auto from typing import TYPE_CHECKING, Literal, NamedTuple, TypedDict, Union @@ -208,29 +207,6 @@ class FontSpec(NamedTuple): Descriptor = Union[FontConfigPattern, CoreTextFont] -class Score(NamedTuple): - variable_score: int - style_score: float - monospace_score: int - width_score: int - - -class Scorer[T]: - def __init__(self, bold: bool = False, italic: bool = False, monospaced: bool = True, prefer_variable: bool = False) -> None: - self.bold = bold - self.italic = italic - self.monospaced = monospaced - self.prefer_variable = prefer_variable - - def sorted_candidates(self, candidates: Sequence[T], dump: bool = False) -> list[T]: - raise NotImplementedError() - - def __repr__(self) -> str: - return f'{self.__class__.__name__}(bold={self.bold}, italic={self.italic}, monospaced={self.monospaced}, prefer_variable={self.prefer_variable})' - - __str__ = __repr__ - - @run_once def fnname_pat() -> 're.Pattern[str]': import re diff --git a/kitty/fonts/common.py b/kitty/fonts/common.py index 19f1d7ead..1fc40aeec 100644 --- a/kitty/fonts/common.py +++ b/kitty/fonts/common.py @@ -1,16 +1,15 @@ #!/usr/bin/env python # License: GPLv3 Copyright: 2024, Kovid Goyal -import copy -from typing import TYPE_CHECKING, Any, Literal, TypedDict, Union, cast +from typing import TYPE_CHECKING, Literal, TypedDict, Union, cast from kitty.constants import is_macos -from kitty.fast_data_types import ParsedFontFeature -from kitty.fonts import Descriptor, DesignAxis, FontSpec, NamedStyle, Scorer, VariableAxis, VariableData, family_name_to_key +from kitty.fast_data_types import ParsedFontFeature, finish_font_selection, start_font_selection +from kitty.fonts import Descriptor, FontSpec, NamedStyle, VariableData from kitty.options.types import Options if TYPE_CHECKING: - from kitty.fast_data_types import CoreTextFont, CTFace, FontConfigPattern + from kitty.fast_data_types import CTFace, FontSelectionSpec from kitty.fast_data_types import Face as FT_Face FontCollectionMapType = Literal['family_map', 'ps_map', 'full_map', 'variable_map'] @@ -18,52 +17,19 @@ if TYPE_CHECKING: Face = Union[FT_Face, CTFace] def all_fonts_map(monospaced: bool) -> FontMap: ... - def create_scorer(bold: bool = False, italic: bool = False, monospaced: bool = True, prefer_variable: bool = False) -> Scorer: ... - def find_best_match( - family: str, - bold: bool = False, - italic: bool = False, - monospaced: bool = True, - ignore_face: Descriptor | None = None, - prefer_variable: bool = False, - ) -> Descriptor: ... - def find_last_resort_text_font(bold: bool = False, italic: bool = False, monospaced: bool = True) -> Descriptor: ... def face_from_descriptor(descriptor: Descriptor, font_sz_in_pts: float | None = None, dpi_x: float | None = None, dpi_y: float | None = None) -> Face: ... def is_monospace(descriptor: Descriptor) -> bool: ... def is_variable(descriptor: Descriptor) -> bool: ... - def set_named_style(name: str, font: Descriptor, vd: VariableData) -> bool: ... - def set_axis_values(tag_map: dict[str, float], font: Descriptor, vd: VariableData) -> bool: ... - def get_axis_values(font: Descriptor, vd: VariableData) -> dict[str, float]: ... else: FontCollectionMapType = FontMap = None from kitty.fast_data_types import specialize_font_descriptor if is_macos: from kitty.fast_data_types import CTFace as Face - from kitty.fonts.core_text import ( - all_fonts_map, - create_scorer, - find_best_match, - find_last_resort_text_font, - get_axis_values, - is_monospace, - is_variable, - set_axis_values, - set_named_style, - ) + from kitty.fonts.core_text import all_fonts_map else: from kitty.fast_data_types import Face - from kitty.fonts.fontconfig import ( - all_fonts_map, - create_scorer, - find_best_match, - find_last_resort_text_font, - get_axis_values, - is_monospace, - is_variable, - set_axis_values, - set_named_style, - ) + from kitty.fonts.fontconfig import all_fonts_map def face_from_descriptor(descriptor: Descriptor, font_sz_in_pts: float | None = None, dpi_x: float | None = None, dpi_y: float | None = None) -> Face: if font_sz_in_pts is not None: @@ -76,55 +42,6 @@ cache_for_variable_data_by_path: dict[str, VariableData] = {} def clear_caches() -> None: cache_for_variable_data_by_path.clear() - actually_variable_cache.clear() - - -attr_map = {(False, False): 'font_family', (True, False): 'bold_font', (False, True): 'italic_font', (True, True): 'bold_italic_font'} - - -class Event: - is_set: bool = False - - -class FamilyAxisValues: - regular_weight: float | None = None - regular_slant: float | None = None - regular_ital: float | None = None - regular_width: float | None = None - - bold_weight: float | None = None - - italic_slant: float | None = None - italic_ital: float | None = None - - def get_wght(self, bold: bool, italic: bool) -> float | None: - return self.bold_weight if bold else self.regular_weight - - def get_ital(self, bold: bool, italic: bool) -> float | None: - return self.italic_ital if italic else self.regular_ital - - def get_slnt(self, bold: bool, italic: bool) -> float | None: - return self.italic_slant if italic else self.regular_slant - - def get_wdth(self, bold: bool, italic: bool) -> float | None: - return self.regular_width - - def get(self, tag: str, bold: bool, italic: bool) -> float | None: - f = getattr(self, f'get_{tag}', None) - return None if f is None else f(bold, italic) - - def set_regular_values(self, axis_values: dict[str, float]) -> None: - self.regular_weight = axis_values.get('wght') - self.regular_width = axis_values.get('wdth') - self.regular_ital = axis_values.get('ital') - self.regular_slant = axis_values.get('slnt') - - def set_bold_values(self, axis_values: dict[str, float]) -> None: - self.bold_weight = axis_values.get('wght') - - def set_italic_values(self, axis_values: dict[str, float]) -> None: - self.italic_ital = axis_values.get('ital') - self.italic_slant = axis_values.get('slnt') def get_variable_data_for_descriptor(d: Descriptor) -> VariableData: @@ -146,229 +63,6 @@ def get_variable_data_for_face(d: Face) -> VariableData: return ans -def find_best_match_in_candidates[T](candidates: list[T], scorer: Scorer[T], is_medium_face: bool, ignore_face: T | None = None) -> T | None: - if candidates: - for x in scorer.sorted_candidates(candidates): - if ignore_face is None or x != ignore_face: - return x - return None - - -def pprint(*a: Any, **kw: Any) -> None: - from pprint import pprint - - pprint(*a, **kw) - - -def find_medium_variant[T: (CoreTextFont | FontConfigPattern)](font: T) -> T: - font = copy.copy(font) - vd = get_variable_data_for_descriptor(font) - for i, ns in enumerate(vd['named_styles']): - if ns['name'] == 'Regular': - set_named_style(ns['psname'] or ns['name'], font, vd) - return font - axis_values = {} - for i, ax in enumerate(vd['axes']): - tag = ax['tag'] - for dax in vd['design_axes']: - if dax['tag'] == tag: - for x in dax['values']: - if x['format'] in (1, 2): - if x['name'] == 'Regular': - axis_values[tag] = x['value'] - break - if axis_values: - set_axis_values(axis_values, font, vd) - return font - - -def get_bold_design_weight(dax: DesignAxis, ax: VariableAxis, regular_weight: float) -> float: - ans = regular_weight - candidates = [] - for x in dax['values']: - if x['format'] in (1, 2): - if x['value'] > regular_weight: - candidates.append(x['value']) - if candidates: - ans = min(candidates) - return ans - - -def get_design_value_for(dax: DesignAxis, ax: VariableAxis, bold: bool, italic: bool, family_axis_values: FamilyAxisValues) -> float: - family_val = family_axis_values.get(ax['tag'], bold, italic) - if family_val is not None and ax['minimum'] <= family_val <= ax['maximum']: - return family_val - default = ax['default'] - if dax['tag'] == 'wght': - keys = ('semibold', 'bold', 'heavy', 'black') if bold else ('regular', 'medium') - elif dax['tag'] in ('ital', 'slnt'): - keys = ('italic', 'oblique', 'slanted', 'slant') if italic else ('regular', 'normal', 'medium', 'upright') - else: - return default - priorities = {} - for x in dax['values']: - if x['format'] in (1, 2): - q = x['name'].lower() - try: - idx = keys.index(q) - except ValueError: - continue - priorities[x['value']] = idx - ans = default - if priorities: - ans = sorted(priorities, key=priorities.__getitem__)[0] - if bold and ax['tag'] == 'wght' and family_axis_values.regular_weight is not None and ans <= family_axis_values.regular_weight: - ans = get_bold_design_weight(dax, ax, family_axis_values.regular_weight) - return ans - - -def find_bold_italic_variant(medium: Descriptor, bold: bool, italic: bool, family_axis_values: FamilyAxisValues) -> Descriptor: - # we first pick the best font file for bold/italic if there are more than - # one. For example SourceCodeVF has Italic and Upright faces with variable - # weights in each, so we rely on the OS font matcher to give us the best - # font file. - monospaced = is_monospace(medium) - unsorted = all_fonts_map(monospaced)['variable_map'][family_name_to_key(medium['family'])] - fonts = create_scorer(bold, italic, monospaced).sorted_candidates(unsorted) - vd = get_variable_data_for_descriptor(fonts[0]) - ans = fonts[0].copy() - # now we need to specialise all axes in ans - axis_values = {} - dax_map = {dax['tag']: dax for dax in vd['design_axes']} - for ax in vd['axes']: - tag = ax['tag'] - dax = dax_map.get(tag) - if dax is not None: - axis_values[tag] = get_design_value_for(dax, ax, bold, italic, family_axis_values) - if axis_values: - set_axis_values(axis_values, ans, vd) - return ans - - -def find_best_variable_face(spec: FontSpec, bold: bool, italic: bool, monospaced: bool, candidates: list[Descriptor]) -> Descriptor: - if spec.variable_name is not None: - q = spec.variable_name.lower() - for font in candidates: - vd = get_variable_data_for_descriptor(font) - if vd['variations_postscript_name_prefix'].lower() == q: - return font - if spec.style: - q = spec.style.lower() - for font in candidates: - vd = get_variable_data_for_descriptor(font) - for x in vd['named_styles']: - if x['psname'].lower() == q: - return font - for x in vd['named_styles']: - if x['name'].lower() == q: - return font - return create_scorer(bold, italic, monospaced).sorted_candidates(candidates)[0] - - -def get_fine_grained_font( - spec: FontSpec, - bold: bool = False, - italic: bool = False, - family_axis_values: FamilyAxisValues = FamilyAxisValues(), - resolved_medium_font: Descriptor | None = None, - monospaced: bool = True, - match_is_more_specific_than_family: Event = Event(), -) -> Descriptor: - font_map = all_fonts_map(monospaced) - is_medium_face = resolved_medium_font is None - scorer = create_scorer(bold, italic, monospaced) - if spec.postscript_name: - q = find_best_match_in_candidates(font_map['ps_map'].get(family_name_to_key(spec.postscript_name), []), scorer, is_medium_face) - if q: - match_is_more_specific_than_family.is_set = True - return q - if spec.full_name: - q = find_best_match_in_candidates(font_map['full_map'].get(family_name_to_key(spec.full_name), []), scorer, is_medium_face) - if q: - match_is_more_specific_than_family.is_set = True - return q - if spec.family: - key = family_name_to_key(spec.family) - # First look for a variable font - candidates = font_map['variable_map'].get(key, []) - if candidates: - q = candidates[0] if len(candidates) == 1 else find_best_variable_face(spec, bold, italic, monospaced, candidates) - q, applied = apply_variation_to_pattern(q, spec) - if applied: - match_is_more_specific_than_family.is_set = True - return q - return find_medium_variant(q) if resolved_medium_font is None else find_bold_italic_variant(resolved_medium_font, bold, italic, family_axis_values) - # Now look for any font - candidates = font_map['family_map'].get(key, []) - if candidates: - if spec.style: - qs = spec.style.lower() - candidates = [x for x in candidates if x['style'].lower() == qs] - q = find_best_match_in_candidates(candidates, scorer, is_medium_face) - if q: - return q - - return find_last_resort_text_font(bold, italic, monospaced) - - -def apply_variation_to_pattern(pat: Descriptor, spec: FontSpec) -> tuple[Descriptor, bool]: - vd = face_from_descriptor(pat).get_variable_data() - pat = pat.copy() - if spec.style: - if set_named_style(spec.style, pat, vd): - return pat, True - tag_map, name_map = {}, {} - for i, ax in enumerate(vd['axes']): - tag_map[ax['tag']] = i - if ax['strid']: - name_map[ax['strid'].lower()] = ax['tag'] - axis_values = {} - for axspec in spec.axes: - qname = axspec[0] - if qname in tag_map: - axis_values[qname] = axspec[1] - continue - tag = name_map.get(qname.lower()) - if tag: - axis_values[tag] = axspec[1] - return pat, set_axis_values(axis_values, pat, vd) - - -def get_font_from_spec( - spec: FontSpec, - bold: bool = False, - italic: bool = False, - family_axis_values: FamilyAxisValues = FamilyAxisValues(), - resolved_medium_font: Descriptor | None = None, - match_is_more_specific_than_family: Event = Event(), -) -> Descriptor: - if not spec.is_system: - ans = get_fine_grained_font( - spec, - bold, - italic, - resolved_medium_font=resolved_medium_font, - family_axis_values=family_axis_values, - match_is_more_specific_than_family=match_is_more_specific_than_family, - ) - if spec.features: - ans = ans.copy() - ans['features'] = spec.features - return ans - family = spec.system or '' - if family == 'auto': - if bold or italic: - assert resolved_medium_font is not None - family = resolved_medium_font['family'] - if is_variable(resolved_medium_font) or is_actually_variable_despite_fontconfigs_lies(resolved_medium_font): - v = find_bold_italic_variant(resolved_medium_font, bold, italic, family_axis_values=family_axis_values) - if v is not None: - return v - else: - family = 'monospace' - return find_best_match(family, bold, italic, ignore_face=resolved_medium_font) - - class FontFiles(TypedDict): medium: Descriptor bold: Descriptor @@ -376,86 +70,55 @@ class FontFiles(TypedDict): bi: Descriptor -actually_variable_cache: dict[str, bool] = {} +SelectedSymbolFont = tuple[Descriptor, bool, bool] +attr_map = {(False, False): 'font_family', (True, False): 'bold_font', (False, True): 'italic_font', (True, True): 'bold_italic_font'} -def is_actually_variable_despite_fontconfigs_lies(d: Descriptor) -> bool: - if d['descriptor_type'] != 'fontconfig': - return False - path = d['path'] - ans = actually_variable_cache.get(path) - if ans is not None: - return ans - m = all_fonts_map(is_monospace(d))['variable_map'] - for x in m.get(family_name_to_key(d['family']), ()): - if x['path'] == path: - actually_variable_cache[path] = True - return True - actually_variable_cache[path] = False - return False +def spec_for_selection(spec: FontSpec) -> 'FontSelectionSpec': + return spec.family, spec.style, spec.postscript_name, spec.full_name, spec.system, spec.variable_name, tuple(spec.axes), bool(spec.features) -def get_font_files(opts: Options) -> FontFiles: - ans: dict[str, Descriptor] = {} - match_is_more_specific_than_family = Event() - medium_font = get_font_from_spec(opts.font_family, match_is_more_specific_than_family=match_is_more_specific_than_family) - medium_font_is_variable = is_variable(medium_font) or is_actually_variable_despite_fontconfigs_lies(medium_font) - if not match_is_more_specific_than_family.is_set and medium_font_is_variable: - medium_font = find_medium_variant(medium_font) - family_axis_values = FamilyAxisValues() - if medium_font_is_variable: - family_axis_values.set_regular_values(get_axis_values(medium_font, get_variable_data_for_descriptor(medium_font))) - kd = {(False, False): 'medium', (True, False): 'bold', (False, True): 'italic', (True, True): 'bi'} +def start_font_files_selection(opts: Options, symbol_map_families: tuple[str, ...] = (), threaded: bool = False) -> None: + """ + Start selecting the fonts specified in opts. The selection is done by C + code, in a thread if threaded is True. Call finish_font_files_selection() + to get the results. + """ + specs = cast( + 'tuple[FontSelectionSpec, FontSelectionSpec, FontSelectionSpec, FontSelectionSpec]', + tuple(spec_for_selection(getattr(opts, attr)) for attr in attr_map.values()), + ) + start_font_selection(specs, symbol_map_families, threaded) + + +def finish_font_files_selection(opts: Options) -> tuple[FontFiles, tuple[SelectedSymbolFont, ...]]: + r = finish_font_selection() + + def with_features(font: Descriptor, spec: FontSpec) -> Descriptor: + if not spec.is_system and spec.features: + font = font.copy() + font['features'] = spec.features + return font + + medium_font = with_features(r['medium'], opts.font_family) + ans: dict[str, Descriptor] = {'medium': medium_font} for (bold, italic), attr in attr_map.items(): if bold or italic: + key = 'bi' if bold and italic else ('bold' if bold else 'italic') spec: FontSpec = getattr(opts, attr) - font = get_font_from_spec(spec, bold, italic, resolved_medium_font=medium_font, family_axis_values=family_axis_values) - # Set family axis values based on the values in font - if not (bold and italic) and (is_variable(medium_font) or is_actually_variable_despite_fontconfigs_lies(medium_font)): - av = get_axis_values(font, get_variable_data_for_descriptor(font)) - (family_axis_values.set_italic_values if italic else family_axis_values.set_bold_values)(av) + font = with_features(r[key], spec) if spec.is_auto and not font.get('features') and medium_font.get('features'): # Set font features based on medium face features font = font.copy() font['features'] = medium_font['features'] - else: - font = medium_font - key = kd[(bold, italic)] - ans[key] = font + ans[key] = font + ff: FontFiles = {'medium': ans['medium'], 'bold': ans['bold'], 'italic': ans['italic'], 'bi': ans['bi']} + return ff, r['symbols'] - def apply_synthetic_matrix(font: Descriptor, bold: bool, italic: bool) -> Descriptor: - # fontconfig's FcFontList (used by find_best_match) omits FC_MATRIX from - # its object set, so a roman font found there carries no synthetic-italic - # shear and its "italic" renders upright. Fira Code is the case (it ships - # no italic), in both its static and variable builds. The italic intent - # exists only here at selection finalize, not at face construction, so - # recover the matrix now: for an italic slot whose chosen face is upright - # (no slant) and has no matrix yet, ask fc_match what fontconfig would do. - # fc_match returns a synthetic matrix only when there is no real italic to - # use (no italic face and no slanted named instance or variable slant - # axis); when a real italic exists it returns no matrix, so a font that is - # already italic, static or variable, is never double-slanted. Face construction applies the matrix - # via FT_Set_Transform; specialize_font_descriptor preserves it when the - # descriptor is sized for rendering. Only the matrix is taken, so - # selection is unchanged. Covers the four configured faces; fc_match - # re-matches by family name (see commit message). - if italic and font['descriptor_type'] == 'fontconfig' and not font.get('matrix') and not font.get('slant'): - from kitty.fast_data_types import FC_MONO - from kitty.fonts.fontconfig import fc_match - mtx = fc_match(font['family'], bold, italic, FC_MONO if is_monospace(font) else -1).get('matrix') - if mtx: - new_font = font.copy() - new_font['matrix'] = mtx - return new_font - return font - - return { - 'medium': ans['medium'], - 'bold': ans['bold'], - 'italic': apply_synthetic_matrix(ans['italic'], False, True), - 'bi': apply_synthetic_matrix(ans['bi'], True, True), - } +def get_font_files(opts: Options) -> FontFiles: + start_font_files_selection(opts) + return finish_font_files_selection(opts)[0] def axis_values_are_equal(defaults: dict[str, float], a: dict[str, float], b: dict[str, float]) -> bool: diff --git a/kitty/fonts/core_text.py b/kitty/fonts/core_text.py index 5afeb9b0a..098fbf377 100644 --- a/kitty/fonts/core_text.py +++ b/kitty/fonts/core_text.py @@ -4,18 +4,13 @@ import itertools import operator from collections import defaultdict -from collections.abc import Generator, Iterable, Sequence +from collections.abc import Generator, Iterable from functools import lru_cache -from typing import NamedTuple -from kitty.fast_data_types import CTFace, coretext_all_fonts +from kitty.fast_data_types import coretext_all_fonts from kitty.typing_compat import CoreTextFont -from kitty.utils import log_error - -from . import Descriptor, ListedFont, Score, Scorer, VariableData, family_name_to_key - -attr_map = {(False, False): 'font_family', (True, False): 'bold_font', (False, True): 'italic_font', (True, True): 'bold_italic_font'} +from . import ListedFont, family_name_to_key FontMap = dict[str, dict[str, list[CoreTextFont]]] @@ -48,7 +43,6 @@ def create_font_map(all_fonts: Iterable[CoreTextFont]) -> FontMap: def clear_caches() -> None: all_fonts_map.cache_clear() - weight_range_for_family.cache_clear() @lru_cache(maxsize=2) @@ -82,142 +76,6 @@ def list_fonts() -> Generator[ListedFont, None, None]: } -class WeightRange(NamedTuple): - minimum: float = 99999 - maximum: float = -99999 - medium: float = -99999 - bold: float = -99999 - - @property - def is_valid(self) -> bool: - return self.minimum != wr.minimum and self.maximum != wr.maximum and self.medium != wr.medium and self.bold != wr.bold - - -wr = WeightRange() - - -@lru_cache -def weight_range_for_family(family: str) -> WeightRange: - faces = all_fonts_map(True)['family_map'].get(family_name_to_key(family), ()) - mini, maxi, medium, bold = wr.minimum, wr.maximum, wr.medium, wr.bold - for face in faces: - w = face['weight'] - mini, maxi = min(w, mini), max(w, maxi) - s = face['style'].lower() - if not s: - continue - s = s.split()[0] - if s == 'semibold': - bold = w - elif s == 'bold' and bold == wr.bold: - bold = w - elif s == 'regular': - medium = w - elif s == 'medium' and medium == wr.medium: - medium = w - return WeightRange(mini, maxi, medium, bold) - - -class CTScorer(Scorer[CoreTextFont]): - weight_range: WeightRange | None = None - - def score(self, candidate: Descriptor) -> Score: - assert candidate['descriptor_type'] == 'core_text' - variable_score = 0 if self.prefer_variable and candidate['variation'] is not None else 1 - bold_score = candidate['weight'] # -1 to 1 with 0 being normal - if self.weight_range is None: - if bold_score < 0: # thinner than normal, reject - bold_score = 2.0 - else: - if self.bold: - # prefer semibold=0.3 to full bold = 0.4 - bold_score = abs(bold_score - 0.3) - else: - anchor = self.weight_range.bold if self.bold else self.weight_range.medium - bold_score = abs(bold_score - anchor) - italic_score = candidate['slant'] # -1 to 1 with 0 being upright < 0 being backward slant, abs(slant) == 1 implies 30 deg rotation - if self.italic: - if italic_score < 0: - italic_score = 2.0 - else: - italic_score = abs(1 - italic_score) - monospace_match = 0 if candidate['monospace'] else 1 - is_regular_width = not candidate['expanded'] and not candidate['condensed'] - return Score(variable_score, bold_score + italic_score, monospace_match, 0 if is_regular_width else 1) - - def sorted_candidates(self, candidates: Sequence[CoreTextFont], dump: bool = False) -> list[CoreTextFont]: - self.weight_range = None - families = {x['family'] for x in candidates} - if len(families) == 1: - wr = weight_range_for_family(next(iter(families))) - if wr.is_valid and wr.medium < 0: # Operator Mono is an example of this craziness - self.weight_range = wr - candidates = sorted(candidates, key=self.score) - if dump: - print(self) - if self.weight_range: - print(self.weight_range) - for x in candidates: - assert x['descriptor_type'] == 'core_text' - print( - CTFace(descriptor=x).postscript_name(), f'bold={x["bold"]}', f'italic={x["italic"]}', f'weight={x["weight"]:.2f}', f'slant={x["slant"]:.2f}' - ) - print(' ', self.score(x)) - print() - return candidates - - -def create_scorer(bold: bool = False, italic: bool = False, monospaced: bool = True, prefer_variable: bool = False) -> Scorer: - return CTScorer(bold, italic, monospaced, prefer_variable) - - -def find_last_resort_text_font(bold: bool = False, italic: bool = False, monospaced: bool = True) -> CoreTextFont: - font_map = all_fonts_map(monospaced) - candidates = font_map['family_map']['menlo'] - return create_scorer(bold, italic, monospaced).sorted_candidates(candidates)[0] - - -def find_best_match( - family: str, bold: bool = False, italic: bool = False, monospaced: bool = True, ignore_face: CoreTextFont | None = None, prefer_variable: bool = False -) -> CoreTextFont: - q = family_name_to_key(family) - font_map = all_fonts_map(monospaced) - scorer = create_scorer(bold, italic, monospaced, prefer_variable=prefer_variable) - - # First look for an exact match - for selector in ('ps_map', 'full_map'): - candidates = font_map[selector].get(q) - if candidates: - candidates = scorer.sorted_candidates(candidates) - possible = candidates[0] - if possible != ignore_face: - return possible - - # See if we have a variable font - if not bold and not italic and font_map['variable_map'].get(q): - candidates = font_map['variable_map'][q] - candidates = scorer.sorted_candidates(candidates) - possible = candidates[0] - if possible != ignore_face: - from .common import find_medium_variant - - return find_medium_variant(possible) - - # Let CoreText choose the font if the family exists, otherwise - # fallback to Menlo - if q not in font_map['family_map']: - if family.lower() not in ('monospace', 'symbols nerd font mono'): - log_error(f'The font {family} was not found, falling back to Menlo') - q = 'menlo' - candidates = scorer.sorted_candidates(font_map['family_map'][q]) - return candidates[0] - - -def font_for_family(family: str) -> tuple[CoreTextFont, bool, bool]: - ans = find_best_match(family, monospaced=False) - return ans, ans['bold'], ans['italic'] - - def descriptor(f: ListedFont) -> CoreTextFont: d = f['descriptor'] assert d['descriptor_type'] == 'core_text' @@ -240,35 +98,3 @@ def prune_family_group(g: list[ListedFont]) -> list[ListedFont]: return False return [x for x in g if is_ok(descriptor(x))] - - -def set_axis_values(tag_map: dict[str, float], font: CoreTextFont, vd: VariableData) -> bool: - known_axes = {ax['tag'] for ax in vd['axes']} - previous = font.get('axis_map', {}) - new = previous.copy() - for tag in known_axes: - val = tag_map.get(tag) - if val is not None: - new[tag] = val - font['axis_map'] = new - return new != previous - - -def set_named_style(name: str, font: CoreTextFont, vd: VariableData) -> bool: - q = name.lower() - for i, ns in enumerate(vd['named_styles']): - if ns['psname'].lower() == q: - return set_axis_values(ns['axis_values'], font, vd) - for i, ns in enumerate(vd['named_styles']): - if ns['name'].lower() == q: - return set_axis_values(ns['axis_values'], font, vd) - if vd['elided_fallback_name']: - for i, ns in enumerate(vd['named_styles']): - eq = ' '.join(ns['name'].replace(vd['elided_fallback_name'], '').strip().split()).lower() - if q == eq: - return set_axis_values(ns['axis_values'], font, vd) - return False - - -def get_axis_values(font: CoreTextFont, vd: VariableData) -> dict[str, float]: - return font.get('axis_map', {}) diff --git a/kitty/fonts/fontconfig.py b/kitty/fonts/fontconfig.py index 4c50aafaf..3b3dc4a03 100644 --- a/kitty/fonts/fontconfig.py +++ b/kitty/fonts/fontconfig.py @@ -1,29 +1,20 @@ #!/usr/bin/env python # License: GPL v3 Copyright: 2016, Kovid Goyal -import sys -from collections.abc import Generator, Sequence +from collections.abc import Generator from functools import lru_cache -from typing import Literal, NamedTuple, Optional, cast +from typing import Literal from kitty.fast_data_types import ( FC_DUAL, FC_MONO, - FC_SLANT_ITALIC, - FC_SLANT_ROMAN, - FC_WEIGHT_REGULAR, - FC_WIDTH_NORMAL, - Face, clear_fallback_font_cache, fc_list, ) -from kitty.fast_data_types import ( - FC_WEIGHT_SEMIBOLD as FC_WEIGHT_BOLD, -) from kitty.fast_data_types import fc_match as fc_match_impl from kitty.typing_compat import FontConfigPattern -from . import Descriptor, ListedFont, Score, Scorer, VariableData, family_name_to_key +from . import ListedFont, family_name_to_key FontCollectionMapType = Literal['family_map', 'ps_map', 'full_map', 'variable_map'] FontMap = dict[FontCollectionMapType, dict[str, list[FontConfigPattern]]] @@ -48,7 +39,6 @@ def create_font_map(all_fonts: tuple[FontConfigPattern, ...]) -> FontMap: def clear_caches() -> None: all_fonts_map.cache_clear() fc_match.cache_clear() - weight_range_for_family.cache_clear() clear_fallback_font_cache() @@ -96,149 +86,6 @@ def fc_match(family: str, bold: bool, italic: bool, spacing: int = FC_MONO) -> F return fc_match_impl(family, bold, italic, spacing) -class WeightRange(NamedTuple): - minimum: int = sys.maxsize - maximum: int = -1 - medium: int = -1 - bold: int = -1 - - @property - def is_valid(self) -> bool: - return self.minimum != wr.minimum and self.maximum != wr.maximum and self.medium != wr.medium and self.bold != wr.bold - - -wr = WeightRange() - - -@lru_cache -def weight_range_for_family(family: str) -> WeightRange: - faces = all_fonts_map(True)['family_map'].get(family_name_to_key(family), ()) - mini, maxi, medium, bold = wr.minimum, wr.maximum, wr.medium, wr.bold - seen_weights = set() - for face in faces: - w = face['weight'] - mini, maxi = min(w, mini), max(w, maxi) - seen_weights.add(w) - s = face['style'].lower() - if not s: - continue - s = s.split()[0] - if s == 'semibold': - bold = w - elif s == 'bold' and bold == wr.bold: - bold = w - elif s == 'regular': - medium = w - elif s == 'medium' and medium == wr.medium: - medium = w - if len(seen_weights) < 2: - return wr - return WeightRange(mini, maxi, medium, bold) - - -class FCScorer(Scorer[FontConfigPattern]): - weight_range: WeightRange | None = None - - def score(self, candidate: Descriptor) -> Score: - assert candidate['descriptor_type'] == 'fontconfig' - variable_score = 0 if self.prefer_variable and candidate['variable'] else 1 - if self.weight_range is None: - bold_score = abs((FC_WEIGHT_BOLD if self.bold else FC_WEIGHT_REGULAR) - candidate['weight']) - else: - bold_score = abs((self.weight_range.bold if self.bold else self.weight_range.medium) - candidate['weight']) - italic_score = abs((FC_SLANT_ITALIC if self.italic else FC_SLANT_ROMAN) - candidate['slant']) - monospace_match = 0 - if self.monospaced: - monospace_match = 0 if candidate.get('spacing') == 'MONO' else 1 - width_score = abs(candidate['width'] - FC_WIDTH_NORMAL) - return Score(variable_score, bold_score / 1000 + italic_score / 110, monospace_match, width_score) - - def sorted_candidates(self, candidates: Sequence[FontConfigPattern], dump: bool = False) -> list[FontConfigPattern]: - self.weight_range = None - families = {x['family'] for x in candidates} - if len(families) == 1: - wr = weight_range_for_family(next(iter(families))) - if wr.is_valid and wr.medium < 100: # Operator Mono and Cascadia Code are examples - self.weight_range = wr - candidates = sorted(candidates, key=self.score) - if dump: - print(self) - if self.weight_range: - print(self.weight_range) - for x in candidates: - assert x['descriptor_type'] == 'fontconfig' - print(Face(descriptor=x).postscript_name(), f'weight={x["weight"]}', f'slant={x["slant"]}') - print(' ', self.score(x)) - print() - return candidates - - -def create_scorer(bold: bool = False, italic: bool = False, monospaced: bool = True, prefer_variable: bool = False) -> Scorer: - return FCScorer(bold, italic, monospaced, prefer_variable) - - -def find_last_resort_text_font(bold: bool = False, italic: bool = False, monospaced: bool = True) -> FontConfigPattern: - # Use fc-match with a generic family - family = 'monospace' if monospaced else 'sans-serif' - return fc_match(family, bold, italic) - - -def find_best_match( - family: str, - bold: bool = False, - italic: bool = False, - monospaced: bool = True, - ignore_face: FontConfigPattern | None = None, - prefer_variable: bool = False, -) -> FontConfigPattern: - from .common import find_best_match_in_candidates - - q = family_name_to_key(family) - font_map = all_fonts_map(monospaced) - scorer = create_scorer(bold, italic, monospaced, prefer_variable=prefer_variable) - is_medium_face = not bold and not italic - # First look for an exact match - groups: tuple[FontCollectionMapType, ...] = ('ps_map', 'full_map', 'family_map') - for which in groups: - m = font_map[which] - cq = m.get(q, []) - if cq: - if which == 'full_map' and cq[0]['family'] == cq[0]['full_name']: - continue # IBM Plex Mono has fullname of regular face == family_name under fontconfig - exact_match = find_best_match_in_candidates(cq, scorer, is_medium_face, ignore_face=ignore_face) - if exact_match: - return exact_match - - # Use fc-match to see if we can find a monospaced font that matches family - # When aliases are defined, spacing can cause the incorrect font to be - # returned, so check with and without spacing and use the one that matches. - mono_possibility = fc_match(family, False, False, FC_MONO) - dual_possibility = fc_match(family, False, False, FC_DUAL) - any_possibility = fc_match(family, False, False, 0) - tries = (dual_possibility, mono_possibility) if any_possibility == dual_possibility else (mono_possibility, dual_possibility) - for possibility in tries: - for key, map_key in (('postscript_name', 'ps_map'), ('full_name', 'full_map'), ('family', 'family_map')): - map_key = cast(FontCollectionMapType, map_key) - val: str | None = cast(Optional[str], possibility.get(key)) - if val: - candidates = font_map[map_key].get(family_name_to_key(val)) - if candidates: - if len(candidates) == 1: - # happens if the family name is an alias, so we search with - # the actual family name to see if we can find all the - # fonts in the family. - family_name_candidates = font_map['family_map'].get(family_name_to_key(candidates[0]['family'])) - if family_name_candidates and len(family_name_candidates) > 1: - candidates = family_name_candidates - return scorer.sorted_candidates(candidates)[0] - return find_last_resort_text_font(bold, italic, monospaced) - - -def font_for_family(family: str) -> tuple[FontConfigPattern, bool, bool]: - ans = find_best_match(family, monospaced=False) - return ans, ans.get('weight', 0) >= FC_WEIGHT_BOLD, ans.get('slant', FC_SLANT_ROMAN) != FC_SLANT_ROMAN - - def descriptor(f: ListedFont) -> FontConfigPattern: d = f['descriptor'] assert d['descriptor_type'] == 'fontconfig' @@ -255,65 +102,3 @@ def prune_family_group(g: list[ListedFont]) -> list[ListedFont]: return d['variable'] or d['path'] not in variable_paths return [x for x in g if is_ok(descriptor(x))] - - -def set_named_style(name: str, font: FontConfigPattern, vd: VariableData) -> bool: - q = name.lower() - for i, ns in enumerate(vd['named_styles']): - if ns['psname'].lower() == q: - font['named_style'] = i - return True - for i, ns in enumerate(vd['named_styles']): - if ns['name'].lower() == q: - font['named_style'] = i - return True - if vd['elided_fallback_name']: - for i, ns in enumerate(vd['named_styles']): - eq = ' '.join(ns['name'].replace(vd['elided_fallback_name'], '').strip().split()).lower() - if q == eq: - font['named_style'] = i - return True - return False - - -def lift_axes_to_named_style_if_possible(font: FontConfigPattern, vd: VariableData) -> bool: - axes = font.get('axes', tuple(ax['default'] for ax in vd['axes'])) - q = {vd['axes'][i]['tag']: val for i, val in enumerate(axes)} - for i, ns in enumerate(vd['named_styles']): - if ns['axis_values'] == q: - font.pop('axes', None) - font['named_style'] = i - return True - return False - - -def set_axis_values(tag_map: dict[str, float], font: FontConfigPattern, vd: VariableData) -> bool: - axes = list(font.get('axes', ())) or [ax['default'] for ax in vd['axes']] - changed = False - for i, ax in enumerate(vd['axes']): - val = tag_map.get(ax['tag']) - if val is not None: - changed = True - axes[i] = val - if changed: - font['axes'] = tuple(axes) - lift_axes_to_named_style_if_possible(font, vd) - return changed - - -def get_axis_values(font: FontConfigPattern, vd: VariableData) -> dict[str, float]: - ans: dict[str, float] = {} - ns = font.get('named_style') - if ns is not None: - if ns > -1 and ns < len(vd['named_styles']): - ans = vd['named_styles'][ns]['axis_values'] - - axis_values = font.get('axes', ()) - for i, ax in enumerate(vd['axes']): - tag = ax['tag'] - if i < len(axis_values): - ans[tag] = axis_values[i] - else: - if tag not in ans: - ans[tag] = ax['default'] - return ans diff --git a/kitty/fonts/render.py b/kitty/fonts/render.py index 34ff60e11..c7dcfd803 100644 --- a/kitty/fonts/render.py +++ b/kitty/fonts/render.py @@ -4,7 +4,7 @@ import os import sys from collections.abc import Callable, Generator -from typing import TYPE_CHECKING, Any, Literal, Union +from typing import TYPE_CHECKING, Any, Union from kitty.constants import fonts_dir, is_macos from kitty.fast_data_types import ( @@ -29,12 +29,7 @@ from kitty.typing_compat import CoreTextFont, FontConfigPattern from kitty.utils import log_error from . import family_name_to_key -from .common import get_font_files - -if is_macos: - from .core_text import font_for_family as font_for_family_macos -else: - from .fontconfig import font_for_family as font_for_family_fontconfig +from .common import finish_font_files_selection, start_font_files_selection if TYPE_CHECKING: from kitty.fast_data_types import CTFace, DecorationTypes, Face @@ -44,12 +39,9 @@ else: FontObject = Union[CoreTextFont, FontConfigPattern] current_faces: list[tuple[FontObject, bool, bool]] = [] builtin_nerd_font_descriptor: FontObject | None = None - - -def font_for_family(family: str) -> tuple[FontObject, bool, bool]: - if is_macos: - return font_for_family_macos(family) - return font_for_family_fontconfig(family) +# The options and symbol map families of a font selection started by +# set_font_family() whose results have not yet been used. +pending_selection: tuple[Options, tuple[str, ...], bool] | None = None def merge_ranges[_T]( @@ -142,22 +134,44 @@ def coalesce_symbol_maps[_T](maps: dict[tuple[int, int], _T]) -> dict[tuple[int, return dict(ans) -def create_symbol_map(opts: Options) -> tuple[tuple[int, int, int], ...]: +def create_symbol_map(opts: Options) -> tuple[tuple[tuple[int, int, int], ...], tuple[str, ...]]: val = coalesce_symbol_maps(opts.symbol_map) family_map: dict[str, int] = {} - count = 0 for family in val.values(): if family not in family_map: - font, bold, italic = font_for_family(family) - fkey = family_name_to_key(family) - if fkey in ('symbolsnfm', 'symbols nerd font mono') and font['postscript_name'] != 'SymbolsNFM' and builtin_nerd_font_descriptor: - font = builtin_nerd_font_descriptor - bold = italic = False - family_map[family] = count - count += 1 - current_faces.append((font, bold, italic)) + family_map[family] = len(family_map) sm = tuple((a, b, family_map[f]) for (a, b), f in val.items()) - return sm + return sm, tuple(family_map) + + +def resolve_pending_font_selection() -> None: + global pending_selection, current_faces, builtin_nerd_font_descriptor + if pending_selection is None: + return + opts, families, add_builtin_nerd_font = pending_selection + pending_selection = None + font_map, symbol_fonts = finish_font_files_selection(opts) + if add_builtin_nerd_font: + # This is done after the selection finishes, as it uses fontconfig, + # whose initialization by the selection thread it would otherwise wait for + builtin_nerd_font_path = os.path.join(fonts_dir, 'SymbolsNerdFontMono-Regular.ttf') + if os.path.exists(builtin_nerd_font_path): + builtin_nerd_font_descriptor = set_builtin_nerd_font(builtin_nerd_font_path) + else: + log_error(f'No builtin NERD font found in {fonts_dir}') + faces: list[tuple[FontObject, bool, bool]] = [ + (font_map['medium'], False, False), + (font_map['bold'], True, False), + (font_map['italic'], False, True), + (font_map['bi'], True, True), + ] + for family, (font, bold, italic) in zip(families, symbol_fonts): + fkey = family_name_to_key(family) + if fkey in ('symbolsnfm', 'symbols nerd font mono') and font['postscript_name'] != 'SymbolsNFM' and builtin_nerd_font_descriptor: + font = builtin_nerd_font_descriptor + bold = italic = False + faces.append((font, bold, italic)) + current_faces = faces def create_narrow_symbols(opts: Options) -> tuple[tuple[int, int, int], ...]: @@ -170,6 +184,7 @@ descriptor_overrides: dict[int, tuple[FontObject | str, bool, bool]] = {} def descriptor_for_idx(idx: int) -> tuple[FontObject | str, bool, bool]: ans = descriptor_overrides.get(idx) if ans is None: + resolve_pending_font_selection() return current_faces[idx] return ans @@ -197,29 +212,22 @@ def clear_font_caches() -> None: clear_platform_caches() -def set_font_family(opts: Options | None = None, override_font_size: float | None = None, add_builtin_nerd_font: bool = False) -> None: - global current_faces, builtin_nerd_font_descriptor +def set_font_family(opts: Options | None = None, override_font_size: float | None = None, add_builtin_nerd_font: bool = False, threaded: bool = False) -> None: + """ + Select the fonts specified in opts. With threaded=True the selection is + done in a thread and its results are used when the first font group is + created, so that other startup work can happen in parallel. + """ + global current_faces, pending_selection opts = opts or defaults sz = override_font_size or opts.font_size - font_map = get_font_files(opts) - current_faces = [(font_map['medium'], False, False)] - ftypes: list[Literal['bold', 'italic', 'bi']] = ['bold', 'italic', 'bi'] - indices = {k: 0 for k in ftypes} - for k in ftypes: - if k in font_map: - indices[k] = len(current_faces) - current_faces.append((font_map[k], 'b' in k, 'i' in k)) - before = len(current_faces) - if add_builtin_nerd_font: - builtin_nerd_font_path = os.path.join(fonts_dir, 'SymbolsNerdFontMono-Regular.ttf') - if os.path.exists(builtin_nerd_font_path): - builtin_nerd_font_descriptor = set_builtin_nerd_font(builtin_nerd_font_path) - else: - log_error(f'No builtin NERD font found in {fonts_dir}') - sm = create_symbol_map(opts) + sm, families = create_symbol_map(opts) ns = create_narrow_symbols(opts) - num_symbol_fonts = len(current_faces) - before - set_font_data(descriptor_for_idx, indices['bold'], indices['italic'], indices['bi'], num_symbol_fonts, sm, sz, ns) + current_faces = [] + pending_selection = opts, families, add_builtin_nerd_font + start_font_files_selection(opts, families, threaded) + # The descriptors for the bold, italic and bold-italic faces are at indices 1, 2 and 3 + set_font_data(descriptor_for_idx, 1, 2, 3, len(families), sm, sz, ns) if TYPE_CHECKING: diff --git a/kitty/main.py b/kitty/main.py index 430a9d659..e18eca753 100644 --- a/kitty/main.py +++ b/kitty/main.py @@ -432,7 +432,9 @@ class AppRunner: theme_colors.patch_opts(opts, args.debug_rendering) set_options(opts, is_wayland(), args.debug_rendering, args.debug_font_fallback) try: - set_font_family(opts, add_builtin_nerd_font=True) + # Fonts are selected in a thread, while the rest of startup happens, + # the results are used when the first OS Window is created + set_font_family(opts, add_builtin_nerd_font=True, threaded=True) _run_app(opts, args, bad_lines, talk_fd) finally: set_options(None) diff --git a/kitty_tests/fonts.py b/kitty_tests/fonts.py index b64203e19..9a4123a9f 100644 --- a/kitty_tests/fonts.py +++ b/kitty_tests/fonts.py @@ -183,6 +183,81 @@ class Selection(BaseTest): self.ae(face_from_descriptor(ff['medium']).applied_features(), {'dlig': 'dlig', 'test': 'test=3'}) self.ae(face_from_descriptor(ff['bold']).applied_features(), {'dlig': 'dlig', 'test': 'test=3'}) + def test_font_selection_api(self): + # Font selection is done in C, optionally in a thread, see font-select.c + from kitty.fast_data_types import finish_font_selection + from kitty.fonts.common import finish_font_files_selection, start_font_files_selection + + def key(d): + return tuple((k, d.get(k)) for k in ('path', 'index', 'named_style', 'axes', 'axis_map', 'matrix', 'postscript_name')) + + def keys(ff): + return tuple(key(ff[x]) for x in ('medium', 'bold', 'italic', 'bi')) + + def threaded(opts, symbol_map_families=()): + start_font_files_selection(opts, symbol_map_families, threaded=True) + return finish_font_files_selection(opts) + + with self.assertRaises(RuntimeError): + finish_font_selection() + families = sorted({v[0]['family'] for v in all_fonts_map(True)['family_map'].values()})[:6] + opts = Options() + for family in families + ['NoSuchFontXYZ']: + with self.subTest(family=family): + opts.font_family = parse_font_spec(f'family="{family}"') + expected = keys(get_font_files(opts)) + self.ae(expected, keys(threaded(opts)[0])) + if family.isascii(): + # names are matched ignoring case and extra whitespace + opts.font_family = parse_font_spec(f'family=" {family.swapcase().replace(" ", " ")} "') + self.ae(expected, keys(get_font_files(opts))) + # A system font specification + opts.font_family = parse_font_spec('monospace') + expected = keys(get_font_files(opts)) + self.ae(expected, keys(threaded(opts)[0])) + # Starting a new selection discards a running one + other = Options() + other.font_family = parse_font_spec('family="NoSuchFontXYZ"') + start_font_files_selection(other, threaded=True) + self.ae(expected, keys(threaded(opts)[0])) + # Fonts for symbol_map + symbol_families = ('monospace', families[0]) if is_macos else ('monospace', families[0], 'NoSuchFontXYZ') + ff, symbols = threaded(opts, symbol_families) + self.ae(expected, keys(ff)) + self.ae(len(symbols), len(symbol_families)) + for d, bold, italic in symbols: + self.assertTrue(d['path']) + self.assertIsInstance(bold, bool) + self.assertIsInstance(italic, bool) + # features are applied to the selected faces + opts.font_family = parse_font_spec(f'family="{families[0]}" features="+zero"') + ff = get_font_files(opts) + for x in ('medium', 'bold', 'italic', 'bi'): + self.ae(ff[x].get('features'), (ParsedFontFeature('+zero'),)) + + def test_variable_data_from_font_file(self): + # The pure C reading of variable font data used by font selection must + # match the data read via the platform font APIs + from kitty.fast_data_types import variable_data_from_font_file + + seen = set() + count = 0 + for descriptors in all_fonts_map(False)['variable_map'].values(): + for d in descriptors: + path = d['path'] + if path in seen or (is_macos and not path.lower().endswith(('.ttf', '.otf'))): + continue + seen.add(path) + index = d.get('index', 0) & 0xFFFF # type: ignore + expected = dict(face_from_descriptor(d).get_variable_data()) + expected.pop('multi_axis_styles', None) # type: ignore + expected['axes'] = tuple({k: v for k, v in ax.items() if k != 'name'} for ax in expected['axes']) # type: ignore + with self.subTest(path=path): + self.ae(expected, variable_data_from_font_file(path, index)) + count += 1 + if self.is_ci and not count: + raise AssertionError('No variable fonts found') + def test_invalid_font_feature(self): with self.assertRaises(ValueError) as cm: ParsedFontFeature('!!bad!!')