Files
Bela/tools/render_cards.py
T
TMCandClaude Opus 5.5 90acbcb4e6 Bela v0: local game against bots, counting training, settings with self-update
- engine: Croatian Bela rules (deal, trump call, declarations, play rules, scoring),
  heuristic bot, counting drill; unit tests and a 1000-game simulation
- app: Jetpack Compose table with partner and two bot opponents, Hungarian deck,
  card-counting training, settings with in-app update and a QR code for sharing
- tools: card import/render/upscale scripts and the launcher icon generator

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-15 20:40:57 +01:00

81 lines
3.2 KiB
Python

"""
Render the SVG cards in card_assets/ to WebP drawables for the Android app.
python tools/render_cards.py
Writes, into app/src/main/res/drawable-nodpi/:
card_<suit>_<rank>.webp one per SVG card (e.g. card_srce_as.webp)
suit_<suit>.webp the suit symbol on its own, cut from the suit's 7 card
Needs Inkscape 1.x (default install path, or set INKSCAPE) and Pillow. Run it again whenever the SVGs change.
"""
import os
import re
import shutil
import subprocess
import sys
import tempfile
from pathlib import Path
from PIL import Image
ROOT = Path(__file__).resolve().parent.parent
SOURCE = ROOT / "card_assets"
TARGET = ROOT / "app" / "src" / "main" / "res" / "drawable-nodpi"
INKSCAPE = os.environ.get("INKSCAPE", r"C:\Program Files\Inkscape\bin\inkscape.com")
CARD_WIDTH = 720 # px; largest on-screen card ~200 dp (~700 px at xxxhdpi). Keep in sync with SOURCE_WIDTH in CardArt.kt
ICON_SIZE = 128 # px; suit icons are shown at ~20-30 dp
SUITS = ["srce", "zvono", "list", "zir"]
RANKS = ["7", "8", "9", "10", "decko", "dama", "kralj", "as"]
WEBP_QUALITY = 95
def suit_icon_svg(card_svg: str, suit: str) -> str:
"""A 55 x 55 SVG holding only the suit symbol defined in a card's <defs>."""
match = re.search(r'(<g id="simbol_%s">.*?</g>)\s*</defs>' % suit, card_svg, re.S)
if not match:
sys.exit(f"No simbol_{suit} group found")
return (
'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 55 55" width="55" height="55">'
f"<defs>{match.group(1)}</defs><use href=\"#simbol_{suit}\"/></svg>"
)
def main() -> None:
missing = [f"{s}_{r}.svg" for s in SUITS for r in RANKS if not (SOURCE / f"{s}_{r}.svg").exists()]
if missing:
sys.exit("Missing cards: " + ", ".join(missing))
if not Path(INKSCAPE).exists():
sys.exit(f"Inkscape not found at {INKSCAPE}; set INKSCAPE")
TARGET.mkdir(parents=True, exist_ok=True)
with tempfile.TemporaryDirectory() as tmp:
tmp = Path(tmp)
jobs = [] # (svg path, png path, width, height, final webp name)
for suit in SUITS:
for rank in RANKS:
jobs.append((SOURCE / f"{suit}_{rank}.svg", tmp / f"card_{suit}_{rank}.png", CARD_WIDTH, None, f"card_{suit}_{rank}.webp"))
icon_svg = tmp / f"suit_{suit}.svg"
icon_svg.write_text(suit_icon_svg((SOURCE / f"{suit}_7.svg").read_text(encoding="utf-8"), suit), encoding="utf-8")
jobs.append((icon_svg, tmp / f"suit_{suit}.png", ICON_SIZE, ICON_SIZE, f"suit_{suit}.webp"))
# One Inkscape process for everything: much faster than one launch per file.
actions = []
for svg, png, width, height, _ in jobs:
actions += [f"file-open:{svg}", f"export-filename:{png}", f"export-width:{width}"]
if height:
actions.append(f"export-height:{height}")
actions += ["export-do", "file-close"]
subprocess.run([INKSCAPE, "--actions=" + ";".join(actions)], check=True)
for _, png, _, _, name in jobs:
with Image.open(png) as image:
image.save(TARGET / name, "WEBP", quality=WEBP_QUALITY, method=6)
print(f"{name} {(TARGET / name).stat().st_size // 1024} KB")
if __name__ == "__main__":
main()