100 lines · 4.5 KB · PythonRaw
#!/usr/bin/env python3"""Render Home's tool objects at phone size with each name printed on its object, from a JSON table. python3 scripts/gen-tools/preview_prints.py <table.json> <out.png> [tile_px]The table is the same shape as `ToolPrint.table` (Paper/ToolPrint.swift), one row per object: {"todos": {"cx":0.50,"cy":0.38,"w":0.54,"h":0.12,"rot":0,"face":"type","ink":"dark","text":"Todos"}, ...}`face` is type | cap | display | hand (the app's faces, read from apps/loom/Sources/CottonPaper/Resources/Fonts); `ink` isdark (multiply) or cream (screen) — the same two paints ProjectCaps uses. An object with no row is drawn wordless.Objects are read from the catalogue (`Media.xcassets/tool-<name>.imageset/tool-<name>.png`, 600x600 RGBA).Default tile is 252 px = 84 pt at 3x, which is the size Home draws — judge legibility THERE, not zoomed. This ishow the print boxes were tuned on 2026-09-16: change a number, re-render, look, repeat; then copy the numbers into`ToolPrint.table`. `ToolPrintTests` holds the boxes inside the square. Needs Pillow (`/usr/bin/python3` has it)."""import json, math, pathlib, sysfrom PIL import Image, ImageDraw, ImageFont, ImageChops, ImageFilterROOT = pathlib.Path(__file__).resolve().parents[2]FONT_DIR = ROOT / "apps/loom/Sources/CottonPaper/Resources/Fonts"CATALOGUE = ROOT / "apps/loom/Sources/CottonPaper/Media.xcassets"FONTS = { "type": FONT_DIR / "CourierPrime-Regular.ttf", "cap": FONT_DIR / "InstrumentSerif-Italic.ttf", "display": FONT_DIR / "FrauncesTitle-SemiBold.ttf", "hand": FONT_DIR / "Caveat-Regular.ttf",}DARK, CREAM, PLASTER = (28, 23, 20), (245, 237, 219), (236, 229, 216)def object_names(): return sorted(p.name[len("tool-"):-len(".imageset")] for p in CATALOGUE.glob("tool-*.imageset"))def fit_font(path, text, box_w, box_h): size = int(box_h * 0.9) while size > 6: f = ImageFont.truetype(str(path), size) if f.getlength(text) <= box_w and size * 1.05 <= box_h: return f size -= 1 return ImageFont.truetype(str(path), 6)def print_one(canvas, off, side, tile, spec): text = spec.get("text", "") if spec.get("upper"): text = text.upper() font = fit_font(FONTS[spec["face"]], text, spec["w"] * side, spec["h"] * side) layer = Image.new("RGBA", (tile, tile), (0, 0, 0, 0)) d = ImageDraw.Draw(layer) ink = DARK if spec.get("ink", "dark") == "dark" else CREAM cx, cy = off + spec["cx"] * side, off + spec["cy"] * side d.text((cx - font.getlength(text) / 2, cy - font.size * 1.05 / 2), text, font=font, fill=ink + (255,)) if spec.get("rot"): layer = layer.rotate(-spec["rot"], resample=Image.BICUBIC, center=(cx, cy)) base = canvas.convert("RGB") dark = spec.get("ink", "dark") == "dark" lay = Image.new("RGB", (tile, tile), (255, 255, 255) if dark else (0, 0, 0)) lay.paste(layer.convert("RGB"), mask=layer.split()[3]) blended = ImageChops.multiply(base, lay) if dark else ImageChops.screen(base, lay) out = Image.new("RGBA", (tile, tile), (0, 0, 0, 0)) out.paste(blended, mask=canvas.split()[3]) return outdef render(name, spec, tile): src = Image.open(CATALOGUE / f"tool-{name}.imageset" / f"tool-{name}.png").convert("RGBA") side = int(tile * 0.98) obj = src.resize((side, side), Image.LANCZOS) canvas = Image.new("RGBA", (tile, tile), (0, 0, 0, 0)) off = (tile - side) // 2 canvas.alpha_composite(obj, (off, off)) for sp in (spec if isinstance(spec, list) else [spec]) if spec else []: canvas = print_one(canvas, off, side, tile, sp) return canvasdef main(): if len(sys.argv) < 3: raise SystemExit(__doc__) table = json.load(open(sys.argv[1])) tile = int(sys.argv[3]) if len(sys.argv) > 3 else 252 names = object_names() gap, cols = 24, 4 rows = math.ceil(len(names) / cols) sheet = Image.new("RGBA", (cols * (tile + gap) + gap, rows * (tile + gap) + gap), PLASTER + (255,)) for i, n in enumerate(names): t = render(n, table.get(n), tile) x, y = gap + (i % cols) * (tile + gap), gap + (i // cols) * (tile + gap) shadow = Image.new("RGBA", t.size, (0, 0, 0, 0)) shadow.paste((0, 0, 0, 70), mask=t.split()[3]) sheet.alpha_composite(shadow.filter(ImageFilter.GaussianBlur(tile * 0.02)), (x + int(tile * 0.01), y + int(tile * 0.03))) sheet.alpha_composite(t, (x, y)) sheet.convert("RGB").save(sys.argv[2]) print("wrote", sys.argv[2])if __name__ == "__main__": main()