Allow pixel based adjustments as well

This commit is contained in:
Kovid Goyal 2022-07-15 11:45:43 +05:30
parent c910f483bf
commit 1aa50b73a1
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
6 changed files with 19 additions and 10 deletions

View File

@ -35,6 +35,7 @@ class ModificationType(Enum):
class ModificationUnit(IntEnum): class ModificationUnit(IntEnum):
pt = 0 pt = 0
percent = 1 percent = 1
pixel = 2
class ModificationValue(NamedTuple): class ModificationValue(NamedTuple):

View File

@ -221,13 +221,12 @@ opt('+modify_font', '', ctype='!modify_font',
add_to_default=False, add_to_default=False,
long_text=''' long_text='''
Modify font characteristics such as the position or thickness of the underline and strikethrough. 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 The modifications can have the suffix :code:`px` for pixels or :code:`%` for percentage of original value.
pts, or percentages, in which case they are interpreted as a percentage of the original value. No suffix means use pts. For example::
For example::
modify_font underline_position -2 modify_font underline_position -2
modify_font underline_thickness 150% 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', opt('box_drawing_scale', '0.001, 1, 1.5, 2',

View File

@ -116,17 +116,21 @@ window_logo_path(PyObject *src, Options *opts) { STR_SETTER(default_window_logo)
#undef STR_SETTER #undef STR_SETTER
static void 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"); PyObject *mv = PyObject_GetAttrString(val, "mod_value");
if (mv) { if (mv) {
*sz = PyFloat_AsFloat(PyTuple_GET_ITEM(mv, 0)); *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 static void
modify_font(PyObject *mf, Options *opts) { 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); S(underline_position); S(underline_thickness); S(strikethrough_thickness); S(strikethrough_position);
#undef S #undef S
} }

View File

@ -824,6 +824,9 @@ def modify_font(val: str) -> Iterable[Tuple[str, FontModification]]:
if sz.endswith('%'): if sz.endswith('%'):
munit = ModificationUnit.percent munit = ModificationUnit.percent
sz = sz[:-1] sz = sz[:-1]
elif sz.endswith('px'):
munit = ModificationUnit.pixel
sz = sz[:-2]
try: try:
mvalue = float(sz) mvalue = float(sz)
except Exception: except Exception:

View File

@ -21,6 +21,8 @@ typedef struct {
size_t len; size_t len;
} UrlPrefix; } UrlPrefix;
typedef enum AdjustmentUnit { POINT, PERCENT, PIXEL } AdjustmentUnit;
typedef struct { typedef struct {
monotonic_t visual_bell_duration, cursor_blink_interval, cursor_stop_blinking_after, mouse_hide_wait, click_interval; monotonic_t visual_bell_duration, cursor_blink_interval, cursor_stop_blinking_after, mouse_hide_wait, click_interval;
double wheel_scroll_multiplier, touch_scroll_multiplier; double wheel_scroll_multiplier, touch_scroll_multiplier;
@ -85,7 +87,7 @@ typedef struct {
long macos_menubar_title_max_length; long macos_menubar_title_max_length;
int macos_colorspace; int macos_colorspace;
struct { struct {
float val; bool is_percent; float val; AdjustmentUnit unit;
} underline_position, underline_thickness, strikethrough_position, strikethrough_thickness; } underline_position, underline_thickness, strikethrough_position, strikethrough_thickness;
} Options; } Options;

View File

@ -56,11 +56,11 @@ class TestConfParsing(BaseTest):
self.assertFalse(bad_lines) self.assertFalse(bad_lines)
opts = p('pointer_shape_when_grabbed XXX', bad_line_num=1) opts = p('pointer_shape_when_grabbed XXX', bad_line_num=1)
self.ae(opts.pointer_shape_when_grabbed, defaults.pointer_shape_when_grabbed) 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, { self.ae(opts.modify_font, {
'underline_position': FontModification(ModificationType.underline_position, ModificationValue(-2., ModificationUnit.pt)), 'underline_position': FontModification(ModificationType.underline_position, ModificationValue(-2., ModificationUnit.pt)),
'underline_thickness': FontModification(ModificationType.underline_thickness, ModificationValue(150, ModificationUnit.percent)), '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 # test the aliasing options