From 58c94105a35a464202d870a7e6a631a13b3bdcb4 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 19 May 2024 11:47:22 +0530 Subject: [PATCH] Centralize FontSpec related code --- kitty/fonts/__init__.py | 24 ++++++++++++++++++++++++ kitty/options/utils.py | 20 +------------------- 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/kitty/fonts/__init__.py b/kitty/fonts/__init__.py index 68fa19bb9..8afb4cba1 100644 --- a/kitty/fonts/__init__.py +++ b/kitty/fonts/__init__.py @@ -3,6 +3,7 @@ from typing import TYPE_CHECKING, Dict, List, Literal, NamedTuple, Optional, Seq from kitty.types import run_once from kitty.typing import CoreTextFont, FontConfigPattern +from kitty.utils import shlex_split if TYPE_CHECKING: import re @@ -145,6 +146,29 @@ class FontSpec(NamedTuple): variable_name: Optional[str] = None created_from_string: str = '' + @classmethod + def from_setting(cls, spec: str) -> 'FontSpec': + if spec == 'auto': + return FontSpec(system='auto', created_from_string=spec) + items = tuple(shlex_split(spec)) + if '=' not in items[0]: + return FontSpec(system=spec, created_from_string=spec) + axes = {} + defined = {} + for item in items: + k, sep, v = item.partition('=') + if sep != '=': + raise ValueError(f'The font specification: {spec} is not valid as {item} does not contain an =') + if k in ('family', 'style', 'full_name', 'postscript_name', 'variable_name'): + defined[k] = v + else: + try: + axes[k] = float(v) + except Exception: + raise ValueError(f'The font specification: {spec} is not valid as {v} is not a number') + return FontSpec(axes=tuple(axes.items()), created_from_string=spec, **defined) + + @property def is_system(self) -> bool: return bool(self.system) diff --git a/kitty/options/utils.py b/kitty/options/utils.py index 2968523da..b61291252 100644 --- a/kitty/options/utils.py +++ b/kitty/options/utils.py @@ -1391,25 +1391,7 @@ def parse_mouse_map(val: str) -> Iterable[MouseMapping]: def parse_font_spec(spec: str) -> FontSpec: - if spec == 'auto': - return FontSpec(system='auto', created_from_string=spec) - items = tuple(shlex_split(spec)) - if '=' not in items[0]: - return FontSpec(system=spec, created_from_string=spec) - axes = {} - defined = {} - for item in items: - k, sep, v = item.partition('=') - if sep != '=': - raise ValueError(f'The font specification: {spec} is not valid as {item} does not contain an =') - if k in ('family', 'style', 'full_name', 'postscript_name', 'variable_name'): - defined[k] = v - else: - try: - axes[k] = float(v) - except Exception: - raise ValueError(f'The font specification: {spec} is not valid as {v} is not a number') - return FontSpec(axes=tuple(axes.items()), created_from_string=spec, **defined) + return FontSpec.from_setting(spec) def deprecated_hide_window_decorations_aliases(key: str, val: str, ans: Dict[str, Any]) -> None: