generate.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #!/usr/bin/env python3
  2. """Regenerate Bambuddy's PWA / home-screen icons from the original logo artwork.
  3. The logo (frontend/public/img/favicon.png, 512x512) is placed unchanged on a
  4. dark tile with padding; it is only ever scaled down, never redrawn.
  5. Writes the PNGs into frontend/public/img by rendering SVG in a headless
  6. Chromium-based browser (Chrome, Chromium or Edge).
  7. Usage: python3 scripts/pwa-icons/generate.py [path-to-chrome]
  8. """
  9. import base64
  10. import shutil
  11. import subprocess
  12. import sys
  13. import tempfile
  14. from pathlib import Path
  15. HERE = Path(__file__).resolve().parent
  16. ROOT = HERE.parents[1]
  17. OUT_DIR = ROOT / "frontend/public/img"
  18. BROWSERS = [
  19. "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome",
  20. "/Applications/Chromium.app/Contents/MacOS/Chromium",
  21. "/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge",
  22. "google-chrome",
  23. "chromium",
  24. "chromium-browser",
  25. "microsoft-edge",
  26. ]
  27. LOGO = ROOT / "frontend/public/img/favicon.png"
  28. # Centre of the logo's visible artwork within its 512x512 canvas.
  29. MARK_CX, MARK_CY = 256, 254
  30. BACKGROUND = """
  31. <defs>
  32. <linearGradient id="bg" x1="0" y1="0" x2="0" y2="1">
  33. <stop offset="0" stop-color="#3a3d3b"/>
  34. <stop offset="1" stop-color="#1b1d1c"/>
  35. </linearGradient>
  36. <radialGradient id="glow" cx="0.5" cy="0.38" r="0.62">
  37. <stop offset="0" stop-color="#00ae42" stop-opacity="0.18"/>
  38. <stop offset="1" stop-color="#00ae42" stop-opacity="0"/>
  39. </radialGradient>
  40. </defs>
  41. """
  42. def mark_body() -> str:
  43. data = base64.b64encode(LOGO.read_bytes()).decode()
  44. return f'<image width="512" height="512" href="data:image/png;base64,{data}"/>'
  45. def icon_svg(size: int, mark_frac: float, rounded: bool) -> str:
  46. """mark_frac: width of the 512 mark relative to the icon edge."""
  47. scale = size * mark_frac / 512
  48. tx = size / 2 - MARK_CX * scale
  49. ty = size / 2 - MARK_CY * scale
  50. rx = size * 0.2237 if rounded else 0 # iOS-like squircle approximation
  51. return f"""<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 {size} {size}" width="{size}" height="{size}">
  52. {BACKGROUND}
  53. <rect width="{size}" height="{size}" rx="{rx}" fill="url(#bg)"/>
  54. <rect width="{size}" height="{size}" rx="{rx}" fill="url(#glow)"/>
  55. <g transform="translate({tx:.3f} {ty:.3f}) scale({scale:.5f})">{mark_body()}</g>
  56. </svg>
  57. """
  58. # name, size, mark fraction, rounded corners
  59. ICONS = [
  60. # iOS masks the square itself; keep the mark well inside (~72%).
  61. ("apple-touch-icon.png", 180, 0.72, False),
  62. # purpose "any": rounded tile with transparent corners.
  63. ("android-chrome-192x192.png", 192, 0.70, True),
  64. ("android-chrome-512x512.png", 512, 0.70, True),
  65. # purpose "maskable": full-bleed, mark inside the 80% safe-zone circle.
  66. ("maskable-192x192.png", 192, 0.56, False),
  67. ("maskable-512x512.png", 512, 0.56, False),
  68. ]
  69. def find_browser(arg: str | None) -> str:
  70. for cand in ([arg] if arg else []) + BROWSERS:
  71. if cand and (Path(cand).exists() or shutil.which(cand)):
  72. return cand
  73. sys.exit("No Chromium-based browser found; pass its path as the first argument.")
  74. def render(browser: str, svg: str, size: int, out: Path, tmp: Path) -> None:
  75. src = tmp / "icon.svg"
  76. src.write_text(svg)
  77. html = tmp / "icon.html"
  78. html.write_text(
  79. '<html><body style="margin:0;background:transparent">'
  80. f'<img src="icon.svg" width="{size}" height="{size}" style="display:block"></body></html>'
  81. )
  82. subprocess.run(
  83. [
  84. browser,
  85. "--headless=new",
  86. "--disable-gpu",
  87. "--hide-scrollbars",
  88. "--force-device-scale-factor=1",
  89. "--default-background-color=00000000",
  90. f"--window-size={size},{size}",
  91. f"--screenshot={out}",
  92. html.as_uri(),
  93. ],
  94. check=True,
  95. stdout=subprocess.DEVNULL,
  96. stderr=subprocess.DEVNULL,
  97. )
  98. def main() -> None:
  99. browser = find_browser(sys.argv[1] if len(sys.argv) > 1 else None)
  100. with tempfile.TemporaryDirectory() as t:
  101. tmp = Path(t)
  102. for name, size, frac, rounded in ICONS:
  103. out = tmp / name
  104. render(browser, icon_svg(size, frac, rounded), size, out, tmp)
  105. shutil.copyfile(out, OUT_DIR / name)
  106. print(f"wrote {name} ({size}x{size})")
  107. if __name__ == "__main__":
  108. main()