#!/usr/bin/env python3 """shared/icon.svg → shared/icon.icns, the macOS app icon (runs anywhere Qt does: no Mac needed). python3 tools/make_icns.py [--out shared/icon.icns] Re-run after changing icon.svg. A Mac icon sits on a 1024 canvas with the artwork about 824 px across (Apple's grid), so it lines up with its neighbours in the Dock; the PNGs inside are the standard sizes macOS 10.7+ reads (ic07 … ic14). The .icns format is a list of (type, length, PNG). """ import argparse import os import struct import sys ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.insert(0, ROOT) os.environ.setdefault("QT_QPA_PLATFORM", "offscreen") from routine.qt.QtCore import QBuffer, QByteArray, QIODevice, QRectF, Qt # noqa: E402 from routine.qt.QtGui import QGuiApplication, QIcon, QImage, QPainter # noqa: E402 # type → pixel size (ic11..ic14 are the @2x of 16, 32, 128, 256) TYPES = [(b"ic11", 32), (b"ic12", 64), (b"ic07", 128), (b"ic13", 256), (b"ic08", 256), (b"ic14", 512), (b"ic09", 512), (b"ic10", 1024)] ARTWORK = 824 / 1024 def render(svg, px): img = QImage(px, px, QImage.Format.Format_ARGB32) img.fill(Qt.GlobalColor.transparent) p = QPainter(img) p.setRenderHint(QPainter.RenderHint.Antialiasing) p.setRenderHint(QPainter.RenderHint.SmoothPixmapTransform) side = px * ARTWORK pm = QIcon(svg).pixmap(int(round(side)), int(round(side))) if pm.isNull(): raise SystemExit("Qt couldn't draw %s (no SVG support in this Qt?)" % svg) p.drawPixmap(QRectF((px - side) / 2, (px - side) / 2, side, side), pm, QRectF(pm.rect())) p.end() data = QByteArray() buf = QBuffer(data) buf.open(QIODevice.OpenModeFlag.WriteOnly) img.save(buf, "PNG") buf.close() return bytes(data) def icns(entries): body = b"".join(t + struct.pack(">I", len(png) + 8) + png for t, png in entries) return b"icns" + struct.pack(">I", len(body) + 8) + body def main(): ap = argparse.ArgumentParser(description=__doc__.split("\n")[0]) ap.add_argument("--svg", default=os.path.join(ROOT, "shared", "icon.svg")) ap.add_argument("--out", default=os.path.join(ROOT, "shared", "icon.icns")) a = ap.parse_args() app = QGuiApplication(sys.argv) # noqa: F841 — Qt needs one to draw cache = {} entries = [] for t, px in TYPES: if px not in cache: cache[px] = render(a.svg, px) entries.append((t, cache[px])) with open(a.out, "wb") as f: f.write(icns(entries)) print("wrote %s (%d bytes)" % (a.out, os.path.getsize(a.out))) if __name__ == "__main__": main()