From e3af9f68d3645bc09f16e583f8a379b64713616a Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 29 Mar 2020 01:52:33 +0530 Subject: [PATCH] Linux: Fix selection of fonts with multiple width variants not preferring the normal width faces Fixes #2491 --- docs/changelog.rst | 3 +++ kitty/fast_data_types.pyi | 2 ++ kitty/fontconfig.c | 3 +++ kitty/fonts/fontconfig.py | 8 +++++--- 4 files changed, 13 insertions(+), 3 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index 6ca24a053..07e7e68a2 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -31,6 +31,9 @@ To update |kitty|, :doc:`follow the instructions `. - Fix a regression in 0.17 that broke the kitty @ launch remote command and also broke the --tab-title option when creating a new tab. (:iss:`2488`) +- Linux: Fix selection of fonts with multiple width variants not preferring + the normal width faces (:iss:`2491`) + 0.17.1 [2020-03-24] -------------------- diff --git a/kitty/fast_data_types.pyi b/kitty/fast_data_types.pyi index 9333a8eaa..580f29969 100644 --- a/kitty/fast_data_types.pyi +++ b/kitty/fast_data_types.pyi @@ -323,6 +323,7 @@ FC_MONO: int = 100 FC_DUAL: int FC_WEIGHT_REGULAR: int FC_WEIGHT_BOLD: int +FC_WIDTH_NORMAL: int FC_SLANT_ROMAN: int FC_SLANT_ITALIC: int BORDERS_PROGRAM: int @@ -402,6 +403,7 @@ class FontConfigPattern(TypedDict): style: str spacing: str weight: int + width: int slant: int hint_style: int subpixel: int diff --git a/kitty/fontconfig.c b/kitty/fontconfig.c index fa1e32a59..7156d9ee5 100644 --- a/kitty/fontconfig.c +++ b/kitty/fontconfig.c @@ -47,6 +47,7 @@ pattern_as_dict(FcPattern *pat) { S(FC_FULLNAME, full_name); S(FC_POSTSCRIPT_NAME, postscript_name); I(FC_WEIGHT, weight); + I(FC_WIDTH, width) I(FC_SLANT, slant); I(FC_HINT_STYLE, hint_style); I(FC_INDEX, index); @@ -247,5 +248,7 @@ init_fontconfig_library(PyObject *module) { PyModule_AddIntMacro(module, FC_DUAL); PyModule_AddIntMacro(module, FC_MONO); PyModule_AddIntMacro(module, FC_CHARCELL); + PyModule_AddIntMacro(module, FC_WIDTH_NORMAL); + return true; } diff --git a/kitty/fonts/fontconfig.py b/kitty/fonts/fontconfig.py index a50cd3e68..97e4b3202 100644 --- a/kitty/fonts/fontconfig.py +++ b/kitty/fonts/fontconfig.py @@ -8,7 +8,7 @@ from typing import Dict, Generator, List, Optional, Tuple, cast from kitty.fast_data_types import ( FC_DUAL, FC_MONO, FC_SLANT_ITALIC, FC_SLANT_ROMAN, FC_WEIGHT_BOLD, - FC_WEIGHT_REGULAR, fc_list, fc_match as fc_match_impl + FC_WEIGHT_REGULAR, FC_WIDTH_NORMAL, fc_list, fc_match as fc_match_impl ) from kitty.options_stub import Options from kitty.typing import FontConfigPattern @@ -73,11 +73,13 @@ def find_best_match(family: str, bold: bool = False, italic: bool = False, monos q = family_name_to_key(family) font_map = all_fonts_map(monospaced) - def score(candidate: FontConfigPattern) -> Tuple[int, int]: + def score(candidate: FontConfigPattern) -> Tuple[int, int, int]: bold_score = abs((FC_WEIGHT_BOLD if bold else FC_WEIGHT_REGULAR) - candidate.get('weight', 0)) italic_score = abs((FC_SLANT_ITALIC if italic else FC_SLANT_ROMAN) - candidate.get('slant', 0)) monospace_match = 0 if candidate.get('spacing') == 'MONO' else 1 - return bold_score + italic_score, monospace_match + width_score = abs(candidate.get('width', FC_WIDTH_NORMAL) - FC_WIDTH_NORMAL) + + return bold_score + italic_score, monospace_match, width_score # First look for an exact match for selector in ('ps_map', 'full_map', 'family_map'):