diff --git a/kitty/core_text.m b/kitty/core_text.m index 12afa7833..4479cbc06 100644 --- a/kitty/core_text.m +++ b/kitty/core_text.m @@ -1663,6 +1663,22 @@ append_matching(FSContext *ctx, CFStringRef attribute, const char *value, FSList num++; } } + // CoreText makes a descriptor for the default instance of a variable font, + // with an empty variation dictionary, and one for each named instance. The + // default descriptor's style name is often Regular even when the default + // instance is, for example, ExtraLight. Put it before the named instances + // from the same file, so that weight_range_for_family() uses the weight of + // the actual Regular named instance. The order otherwise differs from + // that of coretext_all_fonts(), which is the font registry's internal order. + const FSCandidate **items = out->items + (out->count - num); + for (size_t i = 0; i < num; i++) { + const FSCandidate *c = items[i]; + if (!c->variable || c->variation_count) continue; + size_t first = 0; + while (first < i && strcmp(items[first]->path, c->path) != 0) first++; + memmove(items + first + 1, items + first, (i - first) * sizeof(items[0])); + items[first] = c; + } return num; } @@ -1673,8 +1689,12 @@ fs_backend_list(FSContext *ctx, FSListKind kind, const char *name, const char *k 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 + // CoreText matches family names case insensitively, but not whitespace insensitively. + // Symbols Nerd Font Mono is often used in symbol_map without being installed, as it + // is bundled with kitty, and its name is known, so avoid the ~25ms cost of listing + // all family names for it. + if (!append_matching(ctx, kCTFontFamilyNameAttribute, name, kind, key, monospaced, out) && strcmp(key, "symbols nerd font mono") != 0) { + // The name may differ from the actual family name in whitespace CTBackendData *d = ct_backend_data(ctx); load_family_names(d); for (size_t i = 0; i < d->num_family_names; i++) { diff --git a/kitty/glfw.c b/kitty/glfw.c index 19669016e..edea35cea 100644 --- a/kitty/glfw.c +++ b/kitty/glfw.c @@ -2551,6 +2551,7 @@ create_os_window(PyObject UNUSED *self, PyObject *args, PyObject *kw) { temp_window = glfwCreateWindow(640, 480, "temp", NULL, NULL); if (temp_window == NULL) glfw_failure(false); get_window_content_scale(temp_window, &xscale, &yscale, &xdpi, &ydpi); + startup_timing("Temporary window for DPI created"); } FONTS_DATA_HANDLE fonts_data = load_fonts_data(OPT(font_size), xdpi, ydpi); startup_timing("Fonts loaded"); diff --git a/kitty_tests/fonts.py b/kitty_tests/fonts.py index e26598459..83f7c71c6 100644 --- a/kitty_tests/fonts.py +++ b/kitty_tests/fonts.py @@ -258,10 +258,12 @@ class Selection(BaseTest): self.ae(len(render.current_faces), 5) self.ae(face_from_descriptor(render.current_faces[4][0]).postscript_name(), 'SymbolsNFM') # type: ignore # The builtin NERD font is used for fallback for NERD font icons, - # unless a font installed on the system has them, which is preferred + # unless a font installed on the system has them, which is + # preferred. Such fonts need not have NERD in their names, for + # example, PragmataPro has the NERD font icons. face = get_fallback_font('\U000f0001', False, False) if face.path != path: - self.assertIn('NerdFont', os.path.basename(face.path), f'Unexpected fallback font for a NERD font icon: {face.path}') + self.assertNotIn('LastResort', face.postscript_name(), f'Unexpected fallback font for a NERD font icon: {face.path}') else: self.ae(face.postscript_name(), 'SymbolsNFM') finally: