TableTower/tools/theme_art/svg_common.py
RobinFiredrake 3d0ba9c6b8 Nine more built-in themes: Web 1.0 and the eight suggested (Case File, Eldritch Archive, Comic Panel, Brass & Gears, Chrome Street, Star Chart, Cartographer, Storybook)
Generated stand-in art and sounds, ten more OFL fonts, PNG tiles in the theme generator; untrack __pycache__.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 15:40:10 -06:00

88 lines
3.3 KiB
Python

"""Shared helpers for the theme art generator: SVG text, numbers, colours, seeded random numbers."""
import os
REPO_ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
THEMES_DIR = os.path.join(REPO_ROOT, "themes")
# Shapes copied from the wardrobe mockup (one source for every module).
LEAF_PATH = "M0,-10 C6,-6 6,6 0,10 C-6,6 -6,-6 0,-10 Z"
MAPLE_LEAF_PATH = ("M0,-12 L3,-5 L9,-7 L6,-1 L11,2 L4,3 L5,10 L0,6 L-5,10 L-4,3 "
"L-11,2 L-6,-1 L-9,-7 L-3,-5 Z")
def num(value):
"""Short, stable text for a number (no trailing zeros)."""
text = f"{value:.2f}".rstrip("0").rstrip(".")
return "0" if text in ("-0", "") else text
def grey(lightness):
"""A grey hex colour (r = g = b) for a lightness from 0 to 1."""
level = max(0, min(255, round(lightness * 255)))
return f"#{level:02x}{level:02x}{level:02x}"
def svg_document(width, height, body, defs=""):
"""Wrap SVG body text in a root element with explicit size and viewBox."""
defs_text = f"<defs>{defs}</defs>" if defs else ""
return (f'<svg xmlns="http://www.w3.org/2000/svg" width="{num(width)}" height="{num(height)}" '
f'viewBox="0 0 {num(width)} {num(height)}">{defs_text}{body}</svg>\n')
def write_theme_file(theme_id, file_name, text):
"""Write one art file (SVG text or PNG bytes) into themes/<id>/ and return its path."""
folder = os.path.join(THEMES_DIR, theme_id)
os.makedirs(folder, exist_ok=True)
path = os.path.join(folder, file_name)
if isinstance(text, bytes):
with open(path, "wb") as handle:
handle.write(text)
else:
with open(path, "w", encoding="utf-8") as handle:
handle.write(text)
return path
def remove_theme_file(theme_id, file_name):
"""Delete a stale art file so a re-run matches the current plan."""
path = os.path.join(THEMES_DIR, theme_id, file_name)
if os.path.exists(path):
os.remove(path)
def css_colour(text):
"""Split a mockup colour ('#hex' or 'rgba(r,g,b,a)') into (hex, opacity)."""
text = text.strip()
if text.startswith("rgba"):
parts = [part.strip() for part in text[text.index("(") + 1:text.index(")")].split(",")]
red, green, blue = (int(float(part)) for part in parts[:3])
return f"#{red:02x}{green:02x}{blue:02x}", float(parts[3])
return text, 1.0
def wrapped_copies(inner, tile_width, tile_height):
"""Draw tile content nine times (shifted by the tile size) so shapes cut at an edge reappear opposite."""
copies = []
for shift_x in (-tile_width, 0, tile_width):
for shift_y in (-tile_height, 0, tile_height):
copies.append(f'<g transform="translate({num(shift_x)} {num(shift_y)})">{inner}</g>')
return "".join(copies)
class MockupRandom:
"""The mockup's seeded random numbers (mulberry32), so shapes land where the mockup put them."""
def __init__(self, seed):
self.state = seed & 0xFFFFFFFF
@staticmethod
def _multiply(first, second):
return (first * second) & 0xFFFFFFFF
def next(self):
self.state = (self.state + 0x6D2B79F5) & 0xFFFFFFFF
state = self.state
mixed = self._multiply(state ^ (state >> 15), 1 | state)
mixed = ((mixed + self._multiply(mixed ^ (mixed >> 7), 61 | mixed)) & 0xFFFFFFFF) ^ mixed
return ((mixed ^ (mixed >> 14)) & 0xFFFFFFFF) / 4294967296