Select fonts in C, in a thread, during startup

Font selection was done in Python on the main thread, taking ~15ms at
startup on Linux and much more on macOS, where all installed fonts were
listed from CoreText and converted to Python, taking ~165ms.

Port the selection algorithm to pure C in font-select.c, with platform
backends in fontconfig.c and core_text.m, so that it can run in a thread
started by set_font_family(). The results are used when the first font
group is created, typically when the first OS Window is created. The
data for variable fonts is read from the font tables by C code that does
not use Python or FreeType.

The fontconfig backend lists all monospace fonts once and filters them
in C, as listing fonts is dominated by scanning all fonts. The CoreText
backend queries only the needed font families and PostScript names
instead of all installed fonts.

On Linux, the selected fonts are identical to those selected by the
previous Python implementation for ~1400 font specifications, including
variable fonts. The CoreText backend has not yet been compiled or tested.
This commit is contained in:
Kovid Goyal 2026-11-06 09:24:15 +05:30
parent 04f1abd9c5
commit d47c2bd9f1
No known key found for this signature in database
GPG key ID: 06BC317B515ACE7C
13 changed files with 2927 additions and 844 deletions

View file

@ -9,6 +9,7 @@
#include "cleanup.h"
#include "fonts.h"
#include "unicode-data.h"
#include "font-select.h"
#include <structmember.h>
#include <stdint.h>
#include <math.h>
@ -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 */
};

View file

@ -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;

View file

@ -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]

1981
kitty/font-select.c Normal file

File diff suppressed because it is too large Load diff

161
kitty/font-select.h Normal file
View file

@ -0,0 +1,161 @@
/*
* font-select.h
* Copyright (C) 2026 Kovid Goyal <kovid at kovidgoyal.net>
*
* 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 <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#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);

View file

@ -11,12 +11,15 @@
#include <fontconfig/fontconfig.h>
#include <dlfcn.h>
#include "freetype_render_ui_text.h"
#include "font-select.h"
#include <pthread.h>
#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),

View file

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

View file

@ -1,16 +1,15 @@
#!/usr/bin/env python
# License: GPLv3 Copyright: 2024, Kovid Goyal <kovid at kovidgoyal.net>
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:

View file

@ -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', {})

View file

@ -1,29 +1,20 @@
#!/usr/bin/env python
# License: GPL v3 Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net>
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

View file

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

View file

@ -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)

View file

@ -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!!')