|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Build the legacy macOS icon without relying on system corner masking. |
| 3 | +
|
| 4 | +Run: scripts/macos-icon.sh [--check] |
| 5 | +
|
| 6 | +The wrapper pins uv, Python and Pillow. Verification compares the complete set |
| 7 | +of decoded RGBA representations, not PNG encoding or ICNS container bytes: |
| 8 | +Apple's system iconutil may encode the same pixels differently across macOS. |
| 9 | +The shared artwork and other platforms' icons are intentionally untouched. |
| 10 | +""" |
| 11 | + |
| 12 | +# /// script |
| 13 | +# requires-python = "==3.14.3" |
| 14 | +# dependencies = ["Pillow==12.2.0"] |
| 15 | +# /// |
| 16 | + |
| 17 | +import argparse |
| 18 | +from pathlib import Path |
| 19 | +import subprocess |
| 20 | +import tempfile |
| 21 | + |
| 22 | +from PIL import Image, ImageDraw |
| 23 | + |
| 24 | + |
| 25 | +ROOT = Path(__file__).resolve().parent.parent |
| 26 | +ICONS = ROOT / "desktop/src-tauri/icons" |
| 27 | + |
| 28 | + |
| 29 | +def generate(output): |
| 30 | + # An 824px tile on a 1024px canvas leaves the macOS optical inset. |
| 31 | + # Work at 4x resolution to antialias the rounded edge at every icon size. |
| 32 | + scale = 4 |
| 33 | + canvas = Image.new("RGBA", (1024 * scale, 1024 * scale)) |
| 34 | + with Image.open(ICONS / "buzz-source.png") as source: |
| 35 | + tile = source.convert("RGBA").resize( |
| 36 | + (824 * scale, 824 * scale), Image.Resampling.LANCZOS |
| 37 | + ) |
| 38 | + mask = Image.new("L", tile.size) |
| 39 | + ImageDraw.Draw(mask).rounded_rectangle( |
| 40 | + (0, 0, tile.width - 1, tile.height - 1), radius=185 * scale, fill=255 |
| 41 | + ) |
| 42 | + canvas.paste(tile, (100 * scale, 100 * scale), mask) |
| 43 | + |
| 44 | + with tempfile.TemporaryDirectory(prefix="buzz-macos-icon-") as temporary: |
| 45 | + iconset = Path(temporary) / "Buzz.iconset" |
| 46 | + iconset.mkdir() |
| 47 | + for size in (16, 32, 128, 256, 512): |
| 48 | + for density in (1, 2): |
| 49 | + suffix = "@2x" if density == 2 else "" |
| 50 | + canvas.resize( |
| 51 | + (size * density, size * density), Image.Resampling.LANCZOS |
| 52 | + ).save(iconset / f"icon_{size}x{size}{suffix}.png") |
| 53 | + subprocess.run( |
| 54 | + ["/usr/bin/iconutil", "-c", "icns", str(iconset), "-o", str(output)], |
| 55 | + check=True, |
| 56 | + ) |
| 57 | + |
| 58 | + |
| 59 | +def verify(candidate, reference): |
| 60 | + """Reject missing/extra representations and any decoded pixel drift.""" |
| 61 | + with tempfile.TemporaryDirectory(prefix="buzz-icon-check-") as temporary: |
| 62 | + sets = [] |
| 63 | + for name, icon in (("candidate", candidate), ("reference", reference)): |
| 64 | + iconset = Path(temporary) / f"{name}.iconset" |
| 65 | + subprocess.run( |
| 66 | + ["/usr/bin/iconutil", "-c", "iconset", str(icon), "-o", str(iconset)], |
| 67 | + check=True, |
| 68 | + ) |
| 69 | + sets.append({p.name: p for p in iconset.glob("*.png")}) |
| 70 | + if sets[0].keys() != sets[1].keys(): |
| 71 | + raise ValueError("macOS icon representation set drifted") |
| 72 | + for name in sorted(sets[0]): |
| 73 | + with Image.open(sets[0][name]) as a, Image.open(sets[1][name]) as b: |
| 74 | + if a.size != b.size or a.convert("RGBA").tobytes() != b.convert("RGBA").tobytes(): |
| 75 | + raise ValueError(f"macOS icon pixel drift: {name}") |
| 76 | + |
| 77 | + |
| 78 | +def main(): |
| 79 | + parser = argparse.ArgumentParser(description=__doc__) |
| 80 | + parser.add_argument("--check", action="store_true", help="verify without modifying the icon") |
| 81 | + parser.add_argument("--output", type=Path, default=ICONS / "icon.icns") |
| 82 | + args = parser.parse_args() |
| 83 | + if args.check: |
| 84 | + with tempfile.TemporaryDirectory(prefix="buzz-icon-regenerate-") as temporary: |
| 85 | + generated = Path(temporary) / "icon.icns" |
| 86 | + generate(generated) |
| 87 | + verify(args.output, generated) |
| 88 | + print("macOS icon: all decoded representations match") |
| 89 | + else: |
| 90 | + generate(args.output) |
| 91 | + |
| 92 | + |
| 93 | +if __name__ == "__main__": |
| 94 | + main() |
0 commit comments