forked from BrokenFlows/keymap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdraw.py
More file actions
executable file
·199 lines (155 loc) · 4.71 KB
/
draw.py
File metadata and controls
executable file
·199 lines (155 loc) · 4.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
from sys import argv
from importlib import import_module
# check for arguments, and use the first one as a keymap if possible
if len(argv) > 1:
KEYMAP = import_module(argv[1]).KEYMAP
else:
from keys_36.keymap_36 import KEYMAP
# define key-related dimensions
KEY_W = 55
KEY_H = 50
KEY_RX = 6
KEY_RY = 6
INNER_PAD_W = 2
INNER_PAD_H = 2
OUTER_PAD_W = KEY_W / 2
OUTER_PAD_H = KEY_H / 2
LINE_SPACING = 18
STYLE = """
svg {
font-family: SFMono-Regular,Consolas,Liberation Mono,Menlo,monospace;
font-size: 14px;
font-kerning: normal;
text-rendering: optimizeLegibility;
fill: #24292e;
}
rect {
fill: #f6f8fa;
}
.hold {
fill: #ff8080;
}
.combo_hold {
fill: #ff8080;
}
.combo {
fill: #79f2f2;
}
.number{
fill: #bfc2c7
}
.invisible{
opacity: 0;
}
.home{
fill: #ccffb9;
}
"""
# count the number of layers in the keymap
layers = 0
for layer in KEYMAP:
layers += 1
padding = layers + 1
# count the number of rows in a layer
rows = 1
for layer in KEYMAP:
for row in layer["left"]:
rows += 1
break
# Change this number to match the number of columns your keyboard/layout has.
cols = 7
# define dimensions for svg components
KEYSPACE_W = KEY_W + 2 * INNER_PAD_W
KEYSPACE_H = KEY_H + 2 * INNER_PAD_H
HAND_W = cols * KEYSPACE_W
HAND_H = rows * KEYSPACE_H
LAYER_W = 2 * HAND_W + OUTER_PAD_W
# added + KEY_H to make the layers not overlap due to the added extra thumb row
LAYER_H = HAND_H + KEY_H
BOARD_W = LAYER_W + 2 * OUTER_PAD_W
BOARD_H = layers * LAYER_H + padding * OUTER_PAD_H
def print_key(x, y, key, combo_flag):
key_class = "" # placeholder for class value
if type(key) is dict:
key_class = key["class"]
key = key["key"]
# print key shape
print(
f'<rect rx="{KEY_RX}" ry="{KEY_RY}" x="{x + INNER_PAD_W}" y="{y + INNER_PAD_H}" width="{KEY_W}" height="{KEY_H}" class="{key_class}" />'
)
# prepare text
words = key.split()
y += (KEYSPACE_H - (len(words) - 1) * LINE_SPACING) / 2
# prints text on key
for word in key.split():
print(
f'<text text-anchor="middle" dominant-baseline="middle" x="{x + KEYSPACE_W / 2}" y="{y}">{word}</text>'
)
y += LINE_SPACING
# print combo arc if past two keys are combos
if "combo" in key_class:
if combo_flag:
print(
f'<path fill="none" stroke="#5656A8" stroke-width="4" stroke-linecap="round" d="M{x+OUTER_PAD_W},{y-KEY_H+INNER_PAD_H*6} A{KEY_W},{KEY_W*1} 0,0,0 {x-OUTER_PAD_W},{y-KEY_H+INNER_PAD_H*6}" />'
)
def print_row(x, y, row):
for index, key in enumerate(row):
now_combo = False # flag when the current key is a combo
combo_flag = False # flag when th current and previous keys are combos
# placeholders for class values
key_class = ""
prev_class = ""
# if there's a key class, get it
if type(key) is dict:
key_class = key["class"]
# flag if the key is a sort of combo
if "combo" in key_class:
if index > 0:
prev = row[index-1]
now_combo = True
# check if the previous keys is also a combo key
if now_combo:
if type(prev) is dict:
prev_class = prev["class"]
if "combo" in prev_class:
combo_flag = True
# print the key and track space
print_key(x, y, key, combo_flag)
x += KEYSPACE_W
def print_block(x, y, block):
for row in block:
print_row(x, y, row)
y += KEYSPACE_H
def print_layer(x, y, layer):
# print left then right blocks
print_block(x, y, layer["left"])
print_block(
x + HAND_W + OUTER_PAD_W, y, layer["right"],
)
# count the number of rows in the blocks
rows = 0
for row in layer["left"]:
rows += 1
# print the thumbs below the main blocks
# account for thumb count and row count in thumb placement
# changed print_row to print_block for both to enable two thumb rows
print_block(
x + (cols-len(layer["thumbs"]["left"])) * KEYSPACE_W, y +
rows * KEYSPACE_H, layer["thumbs"]["left"],
)
print_block(
x + HAND_W + OUTER_PAD_W, y + rows *
KEYSPACE_H, layer["thumbs"]["right"],
)
def print_board(x, y, keymap):
x += OUTER_PAD_W
for layer in keymap:
y += OUTER_PAD_H
print_layer(x, y, layer)
y += LAYER_H
print(
f'<svg width="{BOARD_W}" height="{BOARD_H}" viewBox="0 0 {BOARD_W} {BOARD_H}" xmlns="http://www.w3.org/2000/svg">'
)
print(f"<style>{STYLE}</style>")
print_board(0, 0, KEYMAP)
print("</svg>")