4
0

check-logo.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #!/usr/bin/env python3
  2. """Fails when the logo embedded in the app (or the shipped icons) drifted from the brand source.
  3. The source is resources/brand/lynx-share-logo.svg (the logo by w3teal); tools/make_logo.py writes the PNGs from it.
  4. Checks: LogoData.h carries the sha256 of its current source PNGs and decodes to PNGs of the right size;
  5. resources/lynx_icon_256.png, LYNX.ico, the Linux icons, the macOS icon sources and the website/README/Store logos still
  6. look like resources/lynx_badge.png (the app icon).
  7. Usage: python3 scripts/check-logo.py (from anywhere; exit code 1 on drift)"""
  8. import hashlib, io, os, re, sys
  9. from PIL import Image, ImageChops, ImageStat
  10. ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  11. def p(*a): return os.path.join(ROOT, *a)
  12. bad = []
  13. def header(path, array):
  14. s = open(p(path)).read()
  15. m = re.search(r"source-sha256: ([0-9a-f]{64})", s)
  16. body = s.split(array + "[] = {", 1)[1].split("};", 1)[0]
  17. data = bytes(int(x) for x in re.findall(r"\d+", body))
  18. return (m.group(1) if m else None), Image.open(io.BytesIO(data)).convert("RGBA")
  19. def sha(path): return hashlib.sha256(open(p(path), "rb").read()).hexdigest()
  20. h, img = header("src/ui/LogoData.h", "png512")
  21. if h != sha("resources/lynx_badge.png"): bad.append("LogoData.h is stale: rerun tools/make_logo_data.py")
  22. if img.size != (512, 512): bad.append("LogoData.h image is %s, expected 512x512" % (img.size,))
  23. text = open(p("src/ui/LogoData.h")).read()
  24. for tag, src in (("small", "resources/brand/lynx-share-logo-small.png"), ("tiny", "resources/brand/lynx-share-logo-tiny.png"),
  25. ("mark", "resources/lynx_logo.png"), ("mark-small", "resources/brand/lynx-share-mark-small.png")):
  26. m = re.search(r"// %s-sha256: ([0-9a-f]{64})" % tag, text)
  27. if not m or m.group(1) != sha(src): bad.append("LogoData.h %s picture is stale: rerun tools/make_logo_data.py" % tag)
  28. ref = Image.open(p("resources/lynx_badge.png")).convert("RGBA")
  29. def close(name, im, tol=6.0):
  30. a = ref.resize((128, 128), Image.LANCZOS)
  31. b = im.convert("RGBA").resize((128, 128), Image.LANCZOS)
  32. flat = lambda i: Image.alpha_composite(Image.new("RGBA", i.size, (0, 0, 0, 255)), i).convert("RGB")
  33. d = sum(ImageStat.Stat(ImageChops.difference(flat(a), flat(b))).mean) / 3
  34. if d > tol: bad.append("%s differs from lynx_badge.png (mean diff %.1f)" % (name, d))
  35. close("LogoData.h", img)
  36. close("resources/lynx_icon_256.png", Image.open(p("resources/lynx_icon_256.png")))
  37. ico = Image.open(p("resources/LYNX.ico")); close("resources/LYNX.ico", ico.ico.getimage(max(ico.ico.sizes())))
  38. for f in ("resources/macos/lynx-icon-1024.png", "resources/macos/lynx-icon-256.png"): # (Apple's grid: the tile at 824/1024)
  39. im = Image.open(p(f)).convert("RGBA"); m = round(im.width * 100 / 1024)
  40. close(f, im.crop((m, m, im.width - m, im.height - m)))
  41. for f in ("website/assets/img/logo.png", "docs/img/logo.png", "store/logo-300.png",
  42. "resources/linux/icons/hicolor/256x256/apps/lynx.png", "resources/linux/icons/hicolor/512x512/apps/lynx.png"):
  43. close(f, Image.open(p(f)))
  44. print("\n".join(bad) if bad else "logo OK")
  45. sys.exit(1 if bad else 0)