#!/usr/bin/env python3 """Draw Home's cards the way SwiftUI draws them, so the layout is tuned by rendering rather than by compiling. python3 scripts/gen-cards/preview_card.py --out card.png --width 84 todos calendar notes people `ToolCard` places four things on a PHOTOGRAPH of a blank card — the object, its shadow, the rule and the name — and all four are fractions of the card's width. A fraction that is two points out is invisible in the source and obvious on the screen, and the loop that catches it is a compile, an install and a screenshot: about four minutes. This draws the same composition in about a second, at the size Home actually draws it, over the same asset the app loads and in the same typeface. It is `preview_prints.py`'s job for the deck (2026-09-16), and it exists for the same reason: judge the type at the size it will be READ, never zoomed. **The card's numbers are the app's.** They are read from the Swift constants the app draws with (`ToolCardKnobs`, and `CardStockKnobs` for the rule), so a change is one edit there and the preview shows it. `--width` is in POINTS and the render is at 3x, which is the phone. Needs Pillow (`python3 -m pip install --break-system-packages pillow`). """ import argparse import re import pathlib from PIL import Image, ImageDraw, ImageFilter, ImageFont ROOT = pathlib.Path(__file__).resolve().parents[2] MEDIA = ROOT / "apps/loom/Sources/CottonPaper/Media.xcassets" FACE = ROOT / "apps/loom/Sources/CottonPaper/Resources/Fonts/FrauncesText-Regular.ttf" SCALE = 3 # The card's proportions, read from the shipped paper theme's `scale` — the # same keys `ToolCardMetrics(scale:)` reads, so a preview cannot drift. SWIFT = [ROOT / "apps/cotton/Sources/CottonUI/Paper", ROOT / "apps/cotton/Sources/CottonUI/Cards"] def _swift(name: str) -> float: """A `static let : CGFloat = ` the app's Paper/ or Cards/ declares.""" for path in sorted(p for folder in SWIFT for p in folder.glob("*.swift")): found = re.search(rf"static let {name}: CGFloat = (-?[0-9.]+)", path.read_text()) if found: return float(found.group(1)) raise SystemExit(f"no Swift constant {name} under Paper/ or Cards/") ASPECT = _swift("toolCardAspect") OBJECT_FRACTION = _swift("toolCardObjectFraction") OBJECT_CENTRE = _swift("toolCardObjectCentre") SHADOW_BLUR = _swift("toolCardShadowBlurFraction") SHADOW_DROP = _swift("toolCardShadowDropFraction") SHADOW_OPACITY = _swift("toolCardShadowOpacity") RULE_CENTRE = _swift("toolCardRuleCentre") RULE_WIDTH = _swift("toolCardRuleWidth") RULE_HAIRLINE = _swift("toolCardRuleHairlineFraction") NAME_CENTRE = _swift("toolCardNameCentre") NAME_FRACTION = _swift("toolCardNameFraction") CONTENT_INSET = _swift("toolCardContentInset") # A mirror of `ToolCardTint.byTool`, so a preview shows the stock a tool is # really printed on. TINTS = { "notes": "cream", "journal": "cream", "documents": "cream", "todos": "sage", "calendar": "sage", "goals": "sage", "meetings": "sage", "alarms": "sage", "messages": "rose", "email": "rose", "calls": "rose", "people": "rose", "mood": "rose", "photos": "slate", "videos": "slate", "accounts": "tan", "locations": "tan", "news": "grey", "reader": "grey", "passwords": "grey", } # The money tool's object is filed under its own name, not its slug. OBJECTS = {"accounts": "money"} NAMES = {"accounts": "Money", "todos": "Todos", "calendar": "Calendar"} def load(name: str) -> Image.Image: return Image.open(MEDIA / f"{name}.imageset" / f"{name}.png").convert("RGBA") def card(key: str, width: int) -> Image.Image: height = int(round(width / ASPECT)) face = load(f"stock-{TINTS.get(key, 'cream')}").resize((width, height), Image.LANCZOS) out = face.copy() side = int(round(width * OBJECT_FRACTION)) try: obj = load(f"tool-{OBJECTS.get(key, key)}").resize((side, side), Image.LANCZOS) except FileNotFoundError: obj = None if obj is not None: x = (width - side) // 2 y = int(round(height * OBJECT_CENTRE)) - side // 2 # The object's shadow: its own alpha, blurred, dropped, laid down dark. shadow = Image.new("RGBA", (width, height), (0, 0, 0, 0)) ink = Image.new("RGBA", (side, side), (60, 50, 40, 255)) ink.putalpha(obj.getchannel("A").point(lambda v: int(v * SHADOW_OPACITY))) shadow.paste(ink, (x, y + int(round(width * SHADOW_DROP))), ink) shadow = shadow.filter(ImageFilter.GaussianBlur(radius=width * SHADOW_BLUR)) out = Image.alpha_composite(out, shadow) out.paste(obj, (x, y), obj) draw = ImageDraw.Draw(out) rule_y = height * RULE_CENTRE half = width * RULE_WIDTH / 2 draw.line( [(width / 2 - half, rule_y), (width / 2 + half, rule_y)], fill=(70, 58, 46, 150), width=max(1, int(round(width * RULE_HAIRLINE))) ) font = ImageFont.truetype(str(FACE), size=int(round(width * NAME_FRACTION))) label = NAMES.get(key, key.capitalize()) box = draw.textbbox((0, 0), label, font=font) draw.text( ((width - (box[2] - box[0])) / 2 - box[0], height * NAME_CENTRE - (box[3] - box[1]) / 2 - box[1]), label, font=font, fill=(52, 42, 34, 232) ) return out def main() -> None: parser = argparse.ArgumentParser() parser.add_argument("--out", required=True) parser.add_argument("--width", type=float, default=84, help="the card's width in POINTS") parser.add_argument("--gap", type=float, default=8, help="the deck's gap in points") parser.add_argument("--columns", type=int, default=4) parser.add_argument("keys", nargs="+") arguments = parser.parse_args() width = int(round(arguments.width * SCALE)) gap = int(round(arguments.gap * SCALE)) cards = [card(key, width) for key in arguments.keys] height = cards[0].height columns = min(arguments.columns, len(cards)) rows = (len(cards) + columns - 1) // columns # The plaster, near enough: Home's ground under the deck. sheet = Image.new("RGB", (columns * width + (columns + 1) * gap, rows * height + (rows + 1) * gap), (232, 226, 213)) for i, one in enumerate(cards): sheet.paste(one, (gap + (i % columns) * (width + gap), gap + (i // columns) * (height + gap)), one) sheet.save(arguments.out) print(f"{arguments.out}: {sheet.width}x{sheet.height} — {len(cards)} cards at {arguments.width} pt") if __name__ == "__main__": main()