#!/usr/bin/env python3 """The app's icons, from the server's pixel leaf (`web/static/img/logo-mark.svg`). scripts/gen-icons.py [] (default HEAD of ../LLeMbas, or LEMBAS_SERVER) The mark is a 16x16 grid of rects, so it converts exactly into Android vector drawables -- one path per colour -- and needs no PNG at any density: drawable/ic_launcher_foreground.xml the leaf, in the adaptive icon's 66dp safe zone drawable/ic_launcher_monochrome.xml its silhouette, for themed icons drawable/ic_notification.xml the same silhouette: a status-bar icon is alpha only """ from __future__ import annotations import os import re import subprocess import sys from collections import defaultdict from pathlib import Path ROOT = Path(__file__).resolve().parent.parent SERVER = Path(os.environ.get("LEMBAS_SERVER", ROOT.parent / "LLeMbas")) SOURCE = "src/lembas/web/static/img/logo-mark.svg" RES = ROOT / "app/src/main/res/drawable" HEADER = "" def rects(svg: str) -> list[tuple[int, int, int, int, str]]: found = [] for attrs in re.findall(r"]*)/>", svg): a = dict(re.findall(r'(\w+)="([^"]*)"', attrs)) found.append((int(a["x"]), int(a["y"]), int(a["width"]), int(a["height"]), a["fill"].upper())) if not found: sys.exit("gen-icons: no rects in the mark") return found def path(cells) -> str: return " ".join(f"M{x},{y}h{w}v{h}h-{w}z" for x, y, w, h, _ in cells) def vector(size: int, viewport: int, body: str) -> str: return ( f'\n{HEADER}\n' '\n' f"{body}\n" ) def main() -> None: ref = sys.argv[1] if len(sys.argv) > 1 else "HEAD" svg = subprocess.run( ["git", "-C", str(SERVER), "show", f"{ref}:{SOURCE}"], capture_output=True, text=True, check=True ).stdout cells = rects(svg) by_colour: dict[str, list] = defaultdict(list) for cell in cells: by_colour[cell[4]].append(cell) # 108dp adaptive canvas; the leaf scaled x3.5 (56dp) and centred, inside the 66dp safe zone. coloured = "".join( f' \n' for colour, group in sorted(by_colour.items()) ) group = f' \n{coloured} \n' silhouette = f' \n' mono = f' \n{silhouette} \n' RES.mkdir(parents=True, exist_ok=True) (RES / "ic_launcher_foreground.xml").write_text(vector(108, 108, group)) (RES / "ic_launcher_monochrome.xml").write_text(vector(108, 108, mono)) (RES / "ic_notification.xml").write_text(vector(24, 16, silhouette.replace(" ", " "))) print(f"gen-icons: {len(cells)} cells in {len(by_colour)} colours, from {ref}") if __name__ == "__main__": main()