| 1 | #!/usr/bin/env python3 |
| 2 | """Erzeugt den Figurensatz „Drechsler“ (schach/figuren/drechsler/*.svg). |
| 3 | |
| 4 | Eigener Satz dieses Programms: gedrechselte Staunton-/Dubrovnik-Formen, flach gezeichnet, |
| 5 | Buchsbaum gegen Nussbaum. Die Figuren sind Drehprofile – hier steht nur die rechte Hälfte |
| 6 | (Abstand von der Mittelachse, y), gespiegelt wird beim Schreiben. Nur der Springer ist frei |
| 7 | gezeichnet. |
| 8 | |
| 9 | python tools/figuren_drechsler.py # schreibt die zwölf Dateien |
| 10 | python tools/figuren_drechsler.py --blatt x.svg # zusätzlich ein Musterblatt auf Brettfeldern |
| 11 | python tools/figuren_drechsler.py --bearbeiten x.svg # alle Figuren in einer Datei, für Inkscape |
| 12 | """ |
| 13 | |
| 14 | import os |
| 15 | import re |
| 16 | import sys |
| 17 | |
| 18 | OUT = os.path.join(os.path.dirname(__file__), "..", "schach", "figuren", "drechsler") |
| 19 | CX = 50 # Mittelachse; Zeichenfläche 100 × 100 |
| 20 | FOOT = 95 # Standlinie |
| 21 | STROKE = 3.4 |
| 22 | |
| 23 | COLORS = { |
| 24 | "w": {"base": "#f7e8c9", "shade": "#dcbd8d", "light": "#fffaf0", "line": "#3a2a20", "detail": "#3a2a20"}, |
| 25 | "b": {"base": "#57443a", "shade": "#35271f", "light": "#806a5c", "line": "#17100c", "detail": "#9a8271"}, |
| 26 | } |
| 27 | |
| 28 | |
| 29 | def f(v): |
| 30 | return f"{v:.2f}".rstrip("0").rstrip(".") |
| 31 | |
| 32 | |
| 33 | def turned(start, segs): |
| 34 | """Geschlossener Pfad aus der rechten Profilhälfte. Segmente: ("L", dx, y), |
| 35 | ("C", dx1, y1, dx2, y2, dx, y), ("A", r, sweep, dx, y).""" |
| 36 | # Abstände erst runden, dann spiegeln: so sind beide Hälften Ziffer für Ziffer gleich weit von der Achse. |
| 37 | start = (round(start[0], 2), start[1]) |
| 38 | segs = [seg[:1] + tuple(round(v, 2) if i % 2 == 0 and seg[0] != "A" or seg[0] == "A" and i == 2 else v |
| 39 | for i, v in enumerate(seg[1:])) for seg in segs] |
| 40 | out = [f"M{f(CX + start[0])} {f(start[1])}"] |
| 41 | points = [start] |
| 42 | for seg in segs: |
| 43 | if seg[0] == "L": |
| 44 | out.append(f"L{f(CX + seg[1])} {f(seg[2])}") |
| 45 | elif seg[0] == "C": |
| 46 | out.append("C" + " ".join(f"{f(CX + seg[i])} {f(seg[i + 1])}" for i in (1, 3, 5))) |
| 47 | else: |
| 48 | out.append(f"A{f(seg[1])} {f(seg[1])} 0 0 {seg[2]} {f(CX + seg[3])} {f(seg[4])}") |
| 49 | points.append(seg[-2:]) |
| 50 | out.append(f"L{f(CX - points[-1][0])} {f(points[-1][1])}") |
| 51 | for i in range(len(segs) - 1, -1, -1): # linke Hälfte: rückwärts und gespiegelt |
| 52 | seg, (px, py) = segs[i], points[i] |
| 53 | if seg[0] == "L": |
| 54 | out.append(f"L{f(CX - px)} {f(py)}") |
| 55 | elif seg[0] == "C": |
| 56 | out.append(f"C{f(CX - seg[3])} {f(seg[4])} {f(CX - seg[1])} {f(seg[2])} {f(CX - px)} {f(py)}") |
| 57 | else: |
| 58 | out.append(f"A{f(seg[1])} {f(seg[1])} 0 0 {seg[2]} {f(CX - px)} {f(py)}") |
| 59 | return "".join(out) + "Z" |
| 60 | |
| 61 | |
| 62 | # Fuß und Halskrause sind einmal entworfen und bei allen Figuren dieselben: Rechtecke mit |
| 63 | # Halbkreis-Enden (Wulst, Krause) und eine Standplatte mit Viertelkreis-Schultern. |
| 64 | FOOT_W, BEAD_W, BEAD_R, BEAD_TOP = 27, 24.5, 2.75, 79.5 |
| 65 | BODY_W = 20 # Halbbreite, mit der jeder Körper im Wulst verschwindet |
| 66 | COLLAR_W, COLLAR_R = 17, 2.9 |
| 67 | |
| 68 | |
| 69 | def stadium(w, top, r): |
| 70 | return (f"M{f(CX - w + r)} {f(top)}H{f(CX + w - r)}A{r} {r} 0 0 1 {f(CX + w - r)} {f(top + 2 * r)}" |
| 71 | f"H{f(CX - w + r)}A{r} {r} 0 0 1 {f(CX - w + r)} {f(top)}Z") |
| 72 | |
| 73 | |
| 74 | def plinth(): |
| 75 | """Standplatte: Viertelkreis-Schultern, kleine Rundung an der Sohle. Der Wulst liegt darauf.""" |
| 76 | y = BEAD_TOP + 2 * BEAD_R |
| 77 | r, q = 5.5, 2 |
| 78 | return (f"M{f(CX - FOOT_W + r)} {f(y)}H{f(CX + FOOT_W - r)}A{r} {r} 0 0 1 {f(CX + FOOT_W)} {f(y + r)}" |
| 79 | f"V{f(FOOT - q)}A{q} {q} 0 0 1 {f(CX + FOOT_W - q)} {f(FOOT)}H{f(CX - FOOT_W + q)}" |
| 80 | f"A{q} {q} 0 0 1 {f(CX - FOOT_W)} {f(FOOT - q)}V{f(y + r)}A{r} {r} 0 0 1 {f(CX - FOOT_W + r)} {f(y)}Z") |
| 81 | |
| 82 | |
| 83 | def scaled(k, start, segs): |
| 84 | """Entwurfsmaße (Standlinie 91) auf das fertige Bild: um den Faktor k von der Standlinie aus.""" |
| 85 | def y(v): |
| 86 | return FOOT + (v - 91) * k |
| 87 | out = [] |
| 88 | for seg in segs: |
| 89 | if seg[0] == "L": |
| 90 | out.append(("L", seg[1] * k, y(seg[2]))) |
| 91 | elif seg[0] == "C": |
| 92 | out.append(("C", seg[1] * k, y(seg[2]), seg[3] * k, y(seg[4]), seg[5] * k, y(seg[6]))) |
| 93 | else: |
| 94 | out.append(("A", seg[1] * k, seg[2], seg[3] * k, y(seg[4]))) |
| 95 | return (start[0] * k, y(start[1])), out |
| 96 | |
| 97 | |
| 98 | def flare(neck, y0): |
| 99 | """Hohlkehle vom Hals hinab in den Wulst.""" |
| 100 | y1 = BEAD_TOP + 1 |
| 101 | return [("C", neck + 0.5, y0 + (y1 - y0) * 0.55, BODY_W - 3.5, y1 - 4, BODY_W, y1)] |
| 102 | |
| 103 | |
| 104 | def ball(r, cy): |
| 105 | """Kugelknauf von der Achse bis kurz vor den unteren Pol.""" |
| 106 | return (0, cy - r), [("A", r, 1, r * 0.72, cy + r * 0.7)] |
| 107 | |
| 108 | |
| 109 | KNOB_R = 5.2 |
| 110 | |
| 111 | |
| 112 | def knob(k, cy): |
| 113 | """Kugelknauf (Dame, Läufer): ein echter Kreis, bei beiden gleich groß. cy in Entwurfsmaßen.""" |
| 114 | y, r = FOOT + (cy - 91) * k, KNOB_R |
| 115 | return f"M{f(CX - r)} {f(y)}A{r} {r} 0 1 0 {f(CX + r)} {f(y)}A{r} {r} 0 1 0 {f(CX - r)} {f(y)}Z" |
| 116 | |
| 117 | |
| 118 | def glint(x0, x1, y): |
| 119 | """Schlaglicht: unterbricht eine waagerechte Kontur auf der Lichtseite.""" |
| 120 | r = STROKE / 2 # die Kontur endet beiderseits der Lücke rund |
| 121 | return (f'<path d="M{f(CX + x0)} {f(y)}H{f(CX + x1)}" class="g"/>' |
| 122 | f'<circle cx="{f(CX + x0)}" cy="{f(y)}" r="{f(r)}" class="k"/>' |
| 123 | f'<circle cx="{f(CX + x1)}" cy="{f(y)}" r="{f(r)}" class="k"/>') |
| 124 | |
| 125 | |
| 126 | def hline(w, y): |
| 127 | return f'<path d="M{f(CX - w)} {f(y)}H{f(CX + w)}" class="d"/>' |
| 128 | |
| 129 | |
| 130 | def turned_piece(k, start, head, neck, collar_y, bands=()): |
| 131 | """Kopf (Entwurfsmaße) + Hals + Hohlkehle; die Krause sitzt bei collar_y auf dem Hals.""" |
| 132 | start, segs = scaled(k, start, head) |
| 133 | cy = FOOT + (collar_y - 91) * k |
| 134 | segs += [("L", neck * k, cy), ("L", neck * k, cy + COLLAR_R)] + flare(neck * k, cy + COLLAR_R) |
| 135 | lines = "".join(hline(w * k, FOOT + (y - 91) * k) for w, y in bands) |
| 136 | n = neck * k |
| 137 | glints = glint(-n + 1.6, -n + 7.6, cy) + glint(-n + 0.8, -n + 6.8, cy + 2 * COLLAR_R) |
| 138 | return {"body": turned(start, segs), "lines": lines, "collar": stadium(COLLAR_W, cy, COLLAR_R), |
| 139 | "glints": glints} |
| 140 | |
| 141 | |
| 142 | def pawn(): |
| 143 | start, head = ball(12.5, 37.5) |
| 144 | return turned_piece(1.12, start, head + [("L", 8, 49)], 7.5, 49) |
| 145 | |
| 146 | |
| 147 | def bishop(): |
| 148 | k = 1.1 |
| 149 | head = [("L", 2.5, 24.5), ("C", 9, 28.5, 13.5, 36.5, 12.5, 43), ("C", 12, 46.5, 10, 48.5, 8.5, 50)] |
| 150 | piece = turned_piece(k, (0, 23.5), head, 7, 50) |
| 151 | piece["extra"] = (f'<path d="M{CX} {f(FOOT + (31.5 - 91) * k)}V{f(FOOT + (40 - 91) * k)}" ' |
| 152 | f'class="d" stroke-width="3.2"/>') |
| 153 | piece["knob"] = knob(k, 21) |
| 154 | return piece |
| 155 | |
| 156 | |
| 157 | def queen(): |
| 158 | # Statt Zacken eine weiche, flach gewölbte Kappe mit Knauf, die seitlich leicht überhängt; kaum breiter als die Krone des Königs. |
| 159 | k = 1.09 |
| 160 | head = [("C", 4, 21.6, 7.5, 19.6, 11.5, 20.5), ("C", 15, 21.3, 17.8, 24, 17.3, 27.2), |
| 161 | ("C", 16.9, 29.8, 14.8, 31.3, 12.6, 31.1), ("C", 11.4, 31, 10.6, 32, 10.4, 33.6), |
| 162 | ("C", 10.2, 37, 9.6, 40, 9, 44)] |
| 163 | piece = turned_piece(k, (0, 21.6), head, 7.2, 44) |
| 164 | piece["knob"] = knob(k, 17.6) |
| 165 | return piece |
| 166 | |
| 167 | |
| 168 | def king(): |
| 169 | head = [("L", 2.8, 6), ("L", 2.8, 10), ("L", 6.8, 10), ("L", 6.8, 15.5), ("L", 2.8, 15.5), ("L", 2.8, 20), |
| 170 | ("C", 8, 20, 13, 21.5, 16.5, 25), ("L", 17.5, 29.5), ("C", 15.5, 35, 11.5, 38.5, 10.5, 44)] |
| 171 | return turned_piece(1.04, (0, 6), head, 8.5, 44, bands=[(16, 29.5)]) |
| 172 | |
| 173 | |
| 174 | def rook(): |
| 175 | k = 1.14 |
| 176 | top = 91 - (91 - 25) * 1.12 / k # Oberkante auf Höhe des Bauernkopfs (Bauer: 25 bei 1,12) |
| 177 | wall = [("L", 18.5, top), ("L", 18.5, top + 13.5), ("C", 18.5, top + 17.5, 13.5, top + 18, 13, top + 21.5)] |
| 178 | notches = [("L", 4, top), ("L", 4, top + 6.5), ("L", 11, top + 6.5), ("L", 11, top)] |
| 179 | shapes = [] |
| 180 | for head in (notches + wall, wall): |
| 181 | start, segs = scaled(k, (0, top), head) |
| 182 | y1 = BEAD_TOP + 1 |
| 183 | segs.append(("C", 13 * k, 64, BODY_W - 3.5, y1 - 4, BODY_W, y1)) |
| 184 | shapes.append(turned(start, segs)) |
| 185 | # Licht und Schatten folgen dem Turm ohne Scharten – sonst klebt in jeder Zinne ein Fleck. |
| 186 | return {"body": shapes[0], "shading": shapes[1], "lines": hline(17.5 * k, FOOT + (top + 14 - 91) * k)} |
| 187 | |
| 188 | |
| 189 | KNIGHT = ("M33 75" |
| 190 | "C33 67 44 63 46.5 55.5" # Brust |
| 191 | "C47.5 52 45.5 49.5 42 50.5" # Kehle, mit Luft unterm Kinn |
| 192 | "C37 52 32.5 56.5 27.5 58.5" # Unterkiefer |
| 193 | "C21.5 61 15.5 57 15.5 51" # Maul |
| 194 | "C15.5 46 19 41.5 22.5 36.5" # Nasenrücken |
| 195 | "C27 29.5 33 24 40.5 21" # Stirn |
| 196 | "L42 9L50.5 17" # Ohr |
| 197 | "C63.5 20.5 74 36 74 53" # Mähne |
| 198 | "C74 62 72.5 69 70.5 75") # Rücken |
| 199 | MANE = "M51.5 24.5C58.5 30 63 40 63 53" |
| 200 | |
| 201 | |
| 202 | def scaled_path(d, k): |
| 203 | """Wie scaled(), für einen Pfad aus absoluten M/L/C-Befehlen.""" |
| 204 | nums = iter(re.findall(r"[MLC]|-?[\d.]+", d)) |
| 205 | out, is_x = [], True |
| 206 | for tok in nums: |
| 207 | if tok in "MLC": |
| 208 | out.append(tok) |
| 209 | is_x = True |
| 210 | continue |
| 211 | v = float(tok) |
| 212 | out.append(f(CX + (v - CX) * k if is_x else FOOT + (v - 91) * k) + " ") |
| 213 | is_x = not is_x |
| 214 | return "".join(out).replace(" M", "M").replace(" L", "L").replace(" C", "C").strip() |
| 215 | |
| 216 | |
| 217 | def knight(): |
| 218 | k = 1.02 |
| 219 | y1 = BEAD_TOP + 1 |
| 220 | head = scaled_path(KNIGHT, k) |
| 221 | body = f"M{f(CX + (33 - CX) * k)} {f(y1)}L" + head[1:] + f"L{f(CX + (70.5 - CX) * k)} {f(y1)}Z" |
| 222 | ex, ey = CX + (33 - CX) * k, FOOT + (33.5 - 91) * k |
| 223 | mx, my = CX + (19.5 - CX) * k, FOOT + (52 - 91) * k |
| 224 | extra = (f'<circle cx="{f(ex)}" cy="{f(ey)}" r="2.4" class="e"/>' |
| 225 | f'<path d="M{f(mx)} {f(my)}q1.2-1.6 2.6-1.2" class="d"/>' |
| 226 | f'<path d="{scaled_path(MANE, k)}" class="d" stroke-width="2.5"/>') |
| 227 | return {"body": body, "extra": extra} |
| 228 | |
| 229 | |
| 230 | PIECES = {"p": pawn, "r": rook, "n": knight, "b": bishop, "q": queen, "k": king} |
| 231 | SHADE, LIGHT, GLARE = 8, 3.4, 8.5 # Breite von Schattenband und Glanzlicht; Rillen beginnen hinter GLARE |
| 232 | |
| 233 | |
| 234 | def part(n, c, d, shading=None, lines=""): |
| 235 | """Ein Bauteil: Fläche mit Glanzlicht links und Schatten rechts, Rillen, Kontur.""" |
| 236 | defs = f'<path id="s{n}" d="{d}"/><clipPath id="c{n}"><use href="#s{n}"/></clipPath>' |
| 237 | form = f"s{n}" |
| 238 | if shading: |
| 239 | form = f"h{n}" |
| 240 | defs += f'<path id="{form}" d="{shading}"/>' |
| 241 | defs += f'<clipPath id="l{n}"><use href="#{form}" x="{LIGHT}"/></clipPath>' |
| 242 | if lines: |
| 243 | defs += f'<clipPath id="g{n}"><use href="#{form}" x="{GLARE}"/></clipPath>' |
| 244 | lines = f'<g clip-path="url(#g{n})">{lines}</g>' |
| 245 | return defs, ( |
| 246 | f'<g clip-path="url(#c{n})"><rect width="100" height="100" fill="{c["shade"]}"/>' |
| 247 | f'<use href="#{form}" x="-{SHADE}" fill="{c["light"]}"/>' |
| 248 | f'<g clip-path="url(#l{n})"><use href="#{form}" x="-{SHADE}" fill="{c["base"]}"/></g>{lines}</g>' |
| 249 | f'<use href="#s{n}" class="o"/>' |
| 250 | ) |
| 251 | |
| 252 | |
| 253 | def svg(color, piece): |
| 254 | c = COLORS[color] |
| 255 | p = PIECES[piece]() |
| 256 | parts = [part(1, c, p["body"], p.get("shading"), p.get("lines", ""))] |
| 257 | if "collar" in p: |
| 258 | parts.append(part(2, c, p["collar"])) |
| 259 | parts.append(part(3, c, plinth())) |
| 260 | parts.append(part(4, c, stadium(BEAD_W, BEAD_TOP, BEAD_R))) |
| 261 | if "knob" in p: |
| 262 | parts.append(part(5, c, p["knob"])) |
| 263 | glints = p.get("glints", "") + glint(-16, -9.6, BEAD_TOP) + glint(-17.6, -11.2, BEAD_TOP + 2 * BEAD_R) |
| 264 | defs = "".join(d for d, _ in parts) |
| 265 | drawn = parts[0][1] + p.get("extra", "") + "".join(g for _, g in parts[1:]) + glints |
| 266 | return ( |
| 267 | f'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">' |
| 268 | f'<style>.d{{fill:none;stroke:{c["detail"]};stroke-width:2.2;stroke-linecap:round}}.e{{fill:{c["detail"]}}}' |
| 269 | f'.g{{fill:none;stroke:{c["light"]};stroke-width:{STROKE + 0.1}}}.k{{fill:{c["line"]}}}' |
| 270 | f'.o{{fill:none;stroke:{c["line"]};stroke-width:{STROKE};stroke-linejoin:round}}</style>' |
| 271 | f'<defs>{defs}</defs>{drawn}</svg>\n' |
| 272 | ) |
| 273 | |
| 274 | |
| 275 | def sheet(path): |
| 276 | """Musterblatt: beide Farben auf hellen und dunklen Feldern.""" |
| 277 | cell, order = 100, "kqbnrp" |
| 278 | parts = [f'<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" ' |
| 279 | f'viewBox="0 0 {cell * 6} {cell * 4}" width="{cell * 12}" height="{cell * 8}">'] |
| 280 | for row in range(4): |
| 281 | for col, piece in enumerate(order): |
| 282 | color = "wb"[row % 2] |
| 283 | square = "#f0d9b5" if (row // 2 + col) % 2 == 0 else "#b58863" |
| 284 | parts.append(f'<rect x="{col * cell}" y="{row * cell}" width="{cell}" height="{cell}" fill="{square}"/>') |
| 285 | parts.append(f'<image x="{col * cell}" y="{row * cell}" width="{cell}" height="{cell}" ' |
| 286 | f'xlink:href="{os.path.abspath(os.path.join(OUT, color + piece + ".svg"))}"/>') |
| 287 | parts.append("</svg>") |
| 288 | with open(path, "w", encoding="utf-8") as fh: |
| 289 | fh.write("".join(parts)) |
| 290 | |
| 291 | |
| 292 | def editable(path): |
| 293 | """Alle zwölf Figuren in einer Datei, als benannte Gruppen zum Bearbeiten in Inkscape. |
| 294 | Die Brettfelder liegen in einer eigenen Ebene darunter.""" |
| 295 | cell, order = 100, "kqbnrp" |
| 296 | names = {"k": "König", "q": "Dame", "b": "Läufer", "n": "Springer", "r": "Turm", "p": "Bauer"} |
| 297 | styles, squares, groups = [], [], [] |
| 298 | for row, color in enumerate("wb"): |
| 299 | for col, piece in enumerate(order): |
| 300 | pre = color + piece |
| 301 | src = svg(color, piece) |
| 302 | style = re.search(r"<style>(.*?)</style>", src).group(1) |
| 303 | if col == 0: |
| 304 | styles.append(re.sub(r"\.(\w)\{", rf".{color}-\1{{", style)) |
| 305 | inner = re.search(r"</style>(.*)</svg>", src, re.S).group(1) |
| 306 | inner = re.sub(r'id="(\w+)"', rf'id="{pre}-\1"', inner) |
| 307 | inner = re.sub(r'(href="|url\()#(\w+)', rf"\1#{pre}-\2", inner) |
| 308 | inner = re.sub(r'class="(\w)"', rf'class="{color}-\1"', inner) |
| 309 | fill = "#f0d9b5" if (row + col) % 2 == 0 else "#b58863" |
| 310 | squares.append(f'<rect x="{col * cell}" y="{row * cell}" width="{cell}" height="{cell}" fill="{fill}"/>') |
| 311 | label = f'{names[piece]} {"weiß" if color == "w" else "schwarz"}' |
| 312 | groups.append(f'<g id="{pre}" inkscape:label="{label}" transform="translate({col * cell} {row * cell})">' |
| 313 | f'{inner}</g>\n') |
| 314 | with open(path, "w", encoding="utf-8") as fh: |
| 315 | fh.write( |
| 316 | f'<svg xmlns="http://www.w3.org/2000/svg" xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" ' |
| 317 | f'viewBox="0 0 {cell * 6} {cell * 2}" width="{cell * 12}" height="{cell * 4}">\n' |
| 318 | f'<style>{"".join(styles)}</style>\n' |
| 319 | f'<g id="brett" inkscape:groupmode="layer" inkscape:label="Brett">{"".join(squares)}</g>\n' |
| 320 | f'<g id="figuren" inkscape:groupmode="layer" inkscape:label="Figuren">\n{"".join(groups)}</g>\n</svg>\n') |
| 321 | |
| 322 | |
| 323 | def main(): |
| 324 | os.makedirs(OUT, exist_ok=True) |
| 325 | for color in COLORS: |
| 326 | for piece in PIECES: |
| 327 | with open(os.path.join(OUT, color + piece + ".svg"), "w", encoding="utf-8") as fh: |
| 328 | fh.write(svg(color, piece)) |
| 329 | if "--blatt" in sys.argv: |
| 330 | sheet(sys.argv[sys.argv.index("--blatt") + 1]) |
| 331 | if "--bearbeiten" in sys.argv: |
| 332 | editable(sys.argv[sys.argv.index("--bearbeiten") + 1]) |
| 333 | |
| 334 | |
| 335 | if __name__ == "__main__": |
| 336 | main() |