From 237a664771898e44d722c35b1295e7c6360d9107 Mon Sep 17 00:00:00 2001 From: Cem Aksoylar Date: Sat, 23 Nov 2024 13:35:26 -0800 Subject: [PATCH] fix alignment, combos and add new aliases --- keymap_drawer/config.py | 9 ++------- keymap_drawer/draw/combo.py | 25 ++++++++++++------------- keymap_drawer/draw/draw.py | 24 ++++++++++++------------ keymap_drawer/draw/glyph.py | 6 +++++- keymap_drawer/keymap.py | 21 ++++++++++++++++----- 5 files changed, 47 insertions(+), 38 deletions(-) diff --git a/keymap_drawer/config.py b/keymap_drawer/config.py index a7b8906..82de622 100644 --- a/keymap_drawer/config.py +++ b/keymap_drawer/config.py @@ -79,9 +79,6 @@ class KeySidePars(BaseModel): legend_rel_x: float = 0 legend_rel_y: float = 0 - # FIXME: better naming of the configuration and explanation to indicate that this refers to the fraction of the width where the right label will be set with respect to the center (defined by the tap) - legend_right_rel_w: float = 4 - # draw key sides draw_key_sides: bool = False @@ -169,12 +166,10 @@ class KeySidePars(BaseModel): text.left { text-anchor: start; - dominant-baseline: auto; } text.right { - text-anchor: start; - dominant-baseline: auto; + text-anchor: end; } text.hold { @@ -192,7 +187,7 @@ class KeySidePars(BaseModel): } /* styling for hold/shifted label text in combo box */ - text.combo.hold, text.combo.shifted { + text.combo.hold, text.combo.shifted, text.combo.left, text.combo.right { font-size: 8px; } diff --git a/keymap_drawer/draw/combo.py b/keymap_drawer/draw/combo.py index 86a37a4..d0033ca 100644 --- a/keymap_drawer/draw/combo.py +++ b/keymap_drawer/draw/combo.py @@ -141,19 +141,6 @@ def print_combo(self, combo: ComboSpec, combo_ind: int) -> tuple[Point, Point]: classes=["combo", combo.type, combo.key.type], legend_type="tap", ) - # FIXME: combos were not test - self._draw_legend( - p - Point(self.cfg.combo_w / 2 - self.cfg.small_pad, 0), - [combo.key.left], - classes=["combo", combo.type, combo.key.type], - legend_type="left", - ) - self._draw_legend( - p + Point(self.cfg.combo_w / self.cfg.legend_right_rel_w - self.cfg.inner_pad_w - self.cfg.small_pad, 0), - [combo.key.right], - classes=["combo", combo.type, combo.key.type], - legend_type="right", - ) self._draw_legend( p + Point(0, self.cfg.combo_h / 2 - self.cfg.small_pad), [combo.key.hold], @@ -166,6 +153,18 @@ def print_combo(self, combo: ComboSpec, combo_ind: int) -> tuple[Point, Point]: classes=["combo", combo.type, combo.key.type], legend_type="shifted", ) + self._draw_legend( + p - Point(self.cfg.combo_w / 2 - self.cfg.small_pad, 0), + [combo.key.left], + classes=["combo", combo.type, combo.key.type], + legend_type="left", + ) + self._draw_legend( + p + Point(self.cfg.combo_w / 2 - self.cfg.small_pad, 0), + [combo.key.right], + classes=["combo", combo.type, combo.key.type], + legend_type="right", + ) if combo.rotation != 0.0: self.out.write("\n") diff --git a/keymap_drawer/draw/draw.py b/keymap_drawer/draw/draw.py index 37814cd..5e4aab5 100644 --- a/keymap_drawer/draw/draw.py +++ b/keymap_drawer/draw/draw.py @@ -94,18 +94,6 @@ def print_key(self, p_key: PhysicalKey, l_key: LayoutKey, key_ind: int) -> None: legend_type="tap", shift=shift, ) - self._draw_legend( - tap_shift - Point(w / 2 - self.cfg.inner_pad_w - self.cfg.small_pad, 0), - [l_key.left], - classes=["key", l_key.type], - legend_type="left" - ) - self._draw_legend( - tap_shift + Point(w / self.cfg.legend_right_rel_w - self.cfg.inner_pad_w - self.cfg.small_pad, 0), - [l_key.right], - classes=["key", l_key.type], - legend_type="right", - ) self._draw_legend( Point(0, h / 2 - self.cfg.inner_pad_h - self.cfg.small_pad), [l_key.hold], @@ -118,6 +106,18 @@ def print_key(self, p_key: PhysicalKey, l_key: LayoutKey, key_ind: int) -> None: classes=["key", l_key.type], legend_type="shifted", ) + self._draw_legend( + tap_shift - Point(w / 2 - self.cfg.inner_pad_w - self.cfg.small_pad, 0), + [l_key.left], + classes=["key", l_key.type], + legend_type="left", + ) + self._draw_legend( + tap_shift + Point(w / 2 - self.cfg.inner_pad_w - self.cfg.small_pad, 0), + [l_key.right], + classes=["key", l_key.type], + legend_type="right", + ) self.out.write("\n") diff --git a/keymap_drawer/draw/glyph.py b/keymap_drawer/draw/glyph.py index cd6e6cd..d4378bb 100644 --- a/keymap_drawer/draw/glyph.py +++ b/keymap_drawer/draw/glyph.py @@ -42,7 +42,11 @@ def init_glyphs(self) -> None: """Preprocess all glyphs in the keymap to get their name to SVG mapping.""" def find_key_glyph_names(key: LayoutKey) -> set[str]: - return {glyph for field in (key.tap, key.hold, key.shifted, key.left, key.right) if (glyph := self._legend_to_name(field))} + return { + glyph + for field in (key.tap, key.hold, key.shifted, key.left, key.right) + if (glyph := self._legend_to_name(field)) + } # find all named glyphs in the keymap names = set() diff --git a/keymap_drawer/keymap.py b/keymap_drawer/keymap.py index 9992025..b3af722 100644 --- a/keymap_drawer/keymap.py +++ b/keymap_drawer/keymap.py @@ -8,7 +8,7 @@ from itertools import chain from typing import Callable, Iterable, Literal -from pydantic import BaseModel, Field, field_validator, model_serializer, model_validator +from pydantic import AliasChoices, BaseModel, Field, field_validator, model_serializer, model_validator from keymap_drawer.config import Config from keymap_drawer.physical_layout import PhysicalLayout, layout_factory @@ -20,9 +20,9 @@ class LayoutKey(BaseModel, populate_by_name=True, coerce_numbers_to_str=True, ex can optionally have hold or shifted properties, left or right labels, or be "held" or be a "ghost" key. """ - tap: str = Field(alias="t", default="") - hold: str = Field(alias="h", default="") - shifted: str = Field(alias="s", default="") + tap: str = Field(validation_alias=AliasChoices("center", "t"), serialization_alias="t", default="") + hold: str = Field(validation_alias=AliasChoices("bottom", "h"), serialization_alias="h", default="") + shifted: str = Field(validation_alias=AliasChoices("shifted", "s"), serialization_alias="s", default="") left: str = "" right: str = "" @@ -46,7 +46,18 @@ def from_key_spec(cls, key_spec: dict | str | int | None) -> "LayoutKey": def serialize_model(self) -> str | dict[str, str]: """Custom serializer to output string-only for simple legends.""" if self.hold or self.shifted or self.left or self.right or self.type: - return {k: v for k, v in (("t", self.tap), ("h", self.hold), ("s", self.shifted), ("left", self.left), ("right", self.right), ("type", self.type)) if v} + return { + k: v + for k, v in ( + ("t", self.tap), + ("h", self.hold), + ("s", self.shifted), + ("left", self.left), + ("right", self.right), + ("type", self.type), + ) + if v + } return self.tap def full_serializer(self) -> dict[str, str]: