Cotton code
107 lines · 3.4 KB · PythonRaw
#!/usr/bin/env python3"""Put each app's chosen painted icon into its flavour's AppIcon sets.    python3 scripts/import-app-icons.py <polish directory> [<app>=<pick> ...]The polish directory holds the painted icons as <app>-<a|b|c>.png (1024 px,square, Codex, 12 Nov 2026). Each app's pick becomes its flavour'sAppIcon<App>-1024.png as painted (no alpha, no rounded corners: iOS roundsit), and AppIcon<App>Dev-1024.png the same with the red DEV band across itsfoot, the band Todos, Music and Keys wear since d50c1216e0: a #D6423A pillfrom x 261 to 762 and y 834 to 946, fully rounded, "DEV" in white boldcapitals 50 px high, tracked wide, centred on it.With no <app>=<pick> pairs it installs the coordinator's picks (12 Nov 2026).Books' pick is the owner's (14 Nov 2026): h, three cloth-bound books stackedin green, oxblood and navy with a gold ribbon, painted as books-h.png."""import sysfrom pathlib import Pathfrom PIL import Image, ImageDraw, ImageFontROOT = Path(__file__).resolve().parent.parentFLAVOURS = ROOT / "apps/cotton/App/Flavours"PICKS = {    "cinemas": "a",    "downloads": "b",    "developer": "a",    "calendar": "a",    "people": "b",    "messages": "b",    "notes": "b",    "todos": "a",    "money": "a",    "keys": "a",    "books": "h",    "photos": "a",    "journal": "a",    "health": "a",}SIDE = 1024BAND = (261, 834, 762, 946)BAND_RED = (214, 66, 58)CAP_HEIGHT = 50WORD_SPAN = 174  # from the D's left edge to the V's right edgeFONT = "/System/Library/Fonts/SFNS.ttf"def font_with_cap_height(px: int) -> ImageFont.FreeTypeFont:    size = px    while True:        font = ImageFont.truetype(FONT, size)        font.set_variation_by_name("Bold")        top, bottom = font.getbbox("H")[1], font.getbbox("H")[3]        if bottom - top >= px:            return font        size += 1def banded(icon: Image.Image) -> Image.Image:    out = icon.copy()    draw = ImageDraw.Draw(out)    x0, y0, x1, y1 = BAND    draw.rounded_rectangle((x0, y0, x1 - 1, y1 - 1), radius=(y1 - y0) // 2, fill=BAND_RED)    font = font_with_cap_height(CAP_HEIGHT)    letters = "DEV"    boxes = [font.getbbox(c) for c in letters]    widths = [b[2] - b[0] for b in boxes]    tracking = (WORD_SPAN - sum(widths)) / (len(letters) - 1)    x = (x0 + x1) / 2 - WORD_SPAN / 2    cap_top = boxes[0][1]    y = (y0 + y1) / 2 - CAP_HEIGHT / 2 - cap_top    for c, b, w in zip(letters, boxes, widths):        draw.text((x - b[0], y), c, font=font, fill=(255, 255, 255))        x += w + tracking    return outdef install(app: str, pick: str, polish: Path) -> None:    source = polish / f"{app}-{pick}.png"    icon = Image.open(source).convert("RGB")    if icon.size != (SIDE, SIDE):        icon = icon.resize((SIDE, SIDE), Image.LANCZOS)    name = f"AppIcon{app.title()}"    assets = FLAVOURS / app / "Assets.xcassets"    for set_name, image in ((name, icon), (f"{name}Dev", banded(icon))):        target = assets / f"{set_name}.appiconset" / f"{set_name}-1024.png"        if not target.parent.is_dir():            sys.exit(f"{target.parent} is missing")        image.save(target, optimize=True)        print(f"{app}: {target.relative_to(ROOT)}")def main() -> None:    if len(sys.argv) < 2:        sys.exit(__doc__)    polish = Path(sys.argv[1])    pairs = dict(arg.split("=", 1) for arg in sys.argv[2:]) or PICKS    for app, pick in pairs.items():        install(app, pick, polish)if __name__ == "__main__":    main()