From 1aa50b73a100d5596a3c694830b8339ae5e0a75f Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 15 Jul 2022 11:45:43 +0530 Subject: [PATCH] Allow pixel based adjustments as well --- kitty/fonts/__init__.py | 1 + kitty/options/definition.py | 7 +++---- kitty/options/to-c.h | 10 +++++++--- kitty/options/utils.py | 3 +++ kitty/state.h | 4 +++- kitty_tests/options.py | 4 ++-- 6 files changed, 19 insertions(+), 10 deletions(-) diff --git a/kitty/fonts/__init__.py b/kitty/fonts/__init__.py index f2c4951bd..97f44ecdd 100644 --- a/kitty/fonts/__init__.py +++ b/kitty/fonts/__init__.py @@ -35,6 +35,7 @@ class ModificationType(Enum): class ModificationUnit(IntEnum): pt = 0 percent = 1 + pixel = 2 class ModificationValue(NamedTuple): diff --git a/kitty/options/definition.py b/kitty/options/definition.py index 73a95a241..01624e29d 100644 --- a/kitty/options/definition.py +++ b/kitty/options/definition.py @@ -221,13 +221,12 @@ opt('+modify_font', '', ctype='!modify_font', add_to_default=False, long_text=''' Modify font characteristics such as the position or thickness of the underline and strikethrough. -The modifications can be either plain numbers, in which case they are interpreted as -pts, or percentages, in which case they are interpreted as a percentage of the original value. -For example:: +The modifications can have the suffix :code:`px` for pixels or :code:`%` for percentage of original value. +No suffix means use pts. For example:: modify_font underline_position -2 modify_font underline_thickness 150% - modify_font strikethrough_thickness 50% + modify_font strikethrough_position 2px ''') opt('box_drawing_scale', '0.001, 1, 1.5, 2', diff --git a/kitty/options/to-c.h b/kitty/options/to-c.h index 7d159e2e6..82a7efa67 100644 --- a/kitty/options/to-c.h +++ b/kitty/options/to-c.h @@ -116,17 +116,21 @@ window_logo_path(PyObject *src, Options *opts) { STR_SETTER(default_window_logo) #undef STR_SETTER static void -parse_font_mod_size(PyObject *val, float *sz, bool *is_percent) { +parse_font_mod_size(PyObject *val, float *sz, AdjustmentUnit *unit) { PyObject *mv = PyObject_GetAttrString(val, "mod_value"); if (mv) { *sz = PyFloat_AsFloat(PyTuple_GET_ITEM(mv, 0)); - *is_percent = PyLong_AsLong(PyTuple_GET_ITEM(mv, 1)) == 1; + switch (PyLong_AsLong(PyTuple_GET_ITEM(mv, 1))) { + case 0: *unit = POINT; break; + case 1: *unit = PIXEL; break; + case 2: *unit = PERCENT; break; + } } } static void modify_font(PyObject *mf, Options *opts) { -#define S(which) { PyObject *v = PyDict_GetItemString(mf, #which); if (v) parse_font_mod_size(v, &opts->which.val, &opts->which.is_percent); } +#define S(which) { PyObject *v = PyDict_GetItemString(mf, #which); if (v) parse_font_mod_size(v, &opts->which.val, &opts->which.unit); } S(underline_position); S(underline_thickness); S(strikethrough_thickness); S(strikethrough_position); #undef S } diff --git a/kitty/options/utils.py b/kitty/options/utils.py index ecc73c856..d214ef536 100644 --- a/kitty/options/utils.py +++ b/kitty/options/utils.py @@ -824,6 +824,9 @@ def modify_font(val: str) -> Iterable[Tuple[str, FontModification]]: if sz.endswith('%'): munit = ModificationUnit.percent sz = sz[:-1] + elif sz.endswith('px'): + munit = ModificationUnit.pixel + sz = sz[:-2] try: mvalue = float(sz) except Exception: diff --git a/kitty/state.h b/kitty/state.h index d374ae6ca..0f911a76a 100644 --- a/kitty/state.h +++ b/kitty/state.h @@ -21,6 +21,8 @@ typedef struct { size_t len; } UrlPrefix; +typedef enum AdjustmentUnit { POINT, PERCENT, PIXEL } AdjustmentUnit; + typedef struct { monotonic_t visual_bell_duration, cursor_blink_interval, cursor_stop_blinking_after, mouse_hide_wait, click_interval; double wheel_scroll_multiplier, touch_scroll_multiplier; @@ -85,7 +87,7 @@ typedef struct { long macos_menubar_title_max_length; int macos_colorspace; struct { - float val; bool is_percent; + float val; AdjustmentUnit unit; } underline_position, underline_thickness, strikethrough_position, strikethrough_thickness; } Options; diff --git a/kitty_tests/options.py b/kitty_tests/options.py index f7ce7c268..58c02dfc7 100644 --- a/kitty_tests/options.py +++ b/kitty_tests/options.py @@ -56,11 +56,11 @@ class TestConfParsing(BaseTest): self.assertFalse(bad_lines) opts = p('pointer_shape_when_grabbed XXX', bad_line_num=1) self.ae(opts.pointer_shape_when_grabbed, defaults.pointer_shape_when_grabbed) - opts = p('modify_font underline_position -2', 'modify_font underline_thickness 150%', 'modify_font size Test -1') + opts = p('modify_font underline_position -2', 'modify_font underline_thickness 150%', 'modify_font size Test -1px') self.ae(opts.modify_font, { 'underline_position': FontModification(ModificationType.underline_position, ModificationValue(-2., ModificationUnit.pt)), 'underline_thickness': FontModification(ModificationType.underline_thickness, ModificationValue(150, ModificationUnit.percent)), - 'size:Test': FontModification(ModificationType.size, ModificationValue(-1., ModificationUnit.pt), 'Test'), + 'size:Test': FontModification(ModificationType.size, ModificationValue(-1., ModificationUnit.pixel), 'Test'), }) # test the aliasing options