From 98c85d2923a202eb7a2c1d484daf3a46f1d4a97f Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 30 Jun 2024 13:24:36 +0530 Subject: [PATCH] Use builtin NERD fonts Prevents users from having to install their own NERD font. System fonts are still used preferentially on Linux but on macOS the builtin one is used preferentially. Cant find any CoreText API to change this. Still has to be implemented on macOS. And need to add code to the build system to bundle the font when building. --- .gitignore | 1 + docs/changelog.rst | 2 ++ kitty/constants.py | 1 + kitty/fonts/render.py | 12 ++++++++++-- kitty/main.py | 3 ++- 5 files changed, 16 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 658da8c6d..b3609e1a0 100644 --- a/.gitignore +++ b/.gitignore @@ -12,6 +12,7 @@ /dependencies /tags /build/ +/fonts/ /linux-package/ /kitty.app/ /glad/out/ diff --git a/docs/changelog.rst b/docs/changelog.rst index b87709cb6..7fd30752e 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -72,6 +72,8 @@ Detailed list of changes - A new ``choose-fonts`` kitten that provides a UI with font previews to ease selection of fonts. Also has support for font features and variable fonts. +- Add NERD fonts builtin so that users don't have to install them to use NERD symbols in kitty + - Wayland: Allow fractional scales less than one (:pull:`7549`) - Wayland: Fix specifying the output name for the panel kitten not working (:iss:`7573`) diff --git a/kitty/constants.py b/kitty/constants.py index 9bbeaf564..74e91df17 100644 --- a/kitty/constants.py +++ b/kitty/constants.py @@ -177,6 +177,7 @@ terminfo_dir = os.path.join(kitty_base_dir, 'terminfo') logo_png_file = os.path.join(kitty_base_dir, 'logo', 'kitty.png') beam_cursor_data_file = os.path.join(kitty_base_dir, 'logo', 'beam-cursor.png') shell_integration_dir = os.path.join(kitty_base_dir, 'shell-integration') +fonts_dir = os.path.join(kitty_base_dir, 'fonts') try: shell_path = pwd.getpwuid(os.geteuid()).pw_shell or '/bin/sh' except KeyError: diff --git a/kitty/fonts/render.py b/kitty/fonts/render.py index 74d477836..5e8d4342a 100644 --- a/kitty/fonts/render.py +++ b/kitty/fonts/render.py @@ -2,12 +2,13 @@ # License: GPL v3 Copyright: 2016, Kovid Goyal import ctypes +import os import sys from functools import partial from math import ceil, cos, floor, pi from typing import TYPE_CHECKING, Any, Callable, Dict, Generator, List, Literal, Optional, Tuple, Union, cast -from kitty.constants import is_macos +from kitty.constants import fonts_dir, is_macos from kitty.fast_data_types import ( NUM_UNDERLINE_STYLES, Screen, @@ -197,6 +198,14 @@ def set_font_family(opts: Optional[Options] = None, override_font_size: Optional ) +def add_application_fonts() -> None: + for font in ('SymbolsNerdFontMono-Regular.ttf',): + path = os.path.join(fonts_dir, font) + if os.path.exists(path): + if not add_font_file(path): + log_error(f'Failed to add application font: {path}') + + if TYPE_CHECKING: CBufType = ctypes.Array[ctypes.c_ubyte] else: @@ -528,7 +537,6 @@ def test_fallback_font(qtext: Optional[str] = None, bold: bool = False, italic: def showcase() -> None: - add_font_file f = 'monospace' if is_macos else 'Liberation Mono' test_render_string('He\u0347\u0305llo\u0337, w\u0302or\u0306l\u0354d!', family=f) test_render_string('你好,世界', family=f) diff --git a/kitty/main.py b/kitty/main.py index 571f3de83..b07817eaf 100644 --- a/kitty/main.py +++ b/kitty/main.py @@ -44,7 +44,7 @@ from .fast_data_types import ( set_options, ) from .fonts.box_drawing import set_scale -from .fonts.render import dump_font_debug, set_font_family +from .fonts.render import add_application_fonts, dump_font_debug, set_font_family from .options.types import Options from .options.utils import DELETE_ENV_VAR from .os_window_size import edge_spacing, initial_window_size_func @@ -249,6 +249,7 @@ class AppRunner: set_options(opts, is_wayland(), args.debug_rendering, args.debug_font_fallback) try: set_font_family(opts) + add_application_fonts() _run_app(opts, args, bad_lines, talk_fd) finally: set_options(None)