4
0

make_logo.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. #!/usr/bin/env python3
  2. """Writes every LYNX SHARE logo picture from the brand's SVGs (the logo by w3teal, in the app-icon form chosen in 2026-11:
  3. w3teal's lynx in the red ring, a small green pepper stem on the ring, on a dark rounded-square tile).
  4. python3 tools/make_logo.py site # README, website, social preview, Store images
  5. python3 tools/make_logo.py app # app/document icons, macOS/Linux icons, embedded logo, splash source
  6. python3 tools/make_logo.py all
  7. Sources (resources/brand/):
  8. - lynx-share-logo.svg: the master. Groups: "tile" (the dark tile), "mark" (w3teal's lynx in the ring) and inside the
  9. mark "stem" (the pepper stem).
  10. - lynx-share-logo-small.svg: the same icon for 32 px and less: share lines and ring thicker, the mark larger in the tile
  11. (at those sizes the master's lines are thinner than a pixel). The stem is left out at 20 px and less, where it is
  12. only a stray pixel.
  13. Derived and written here: lynx-share-mark.svg (the master's mark without the tile, in the mark's own 512-unit frame) and
  14. the PNGs the app, the MSIX script and the site scripts read.
  15. Needs Pillow and the system librsvg + cairo (librsvg-2.so.2 / libcairo.so.2, present on most Linux desktops; Debian
  16. and Ubuntu: librsvg2-2). Nothing below is drawn by hand: replace the SVGs and run `all`.
  17. """
  18. import ctypes
  19. import os
  20. import re
  21. import sys
  22. from PIL import Image
  23. ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  24. BRAND = os.path.join(ROOT, "resources", "brand")
  25. MASTER = os.path.join(BRAND, "lynx-share-logo.svg")
  26. SMALL = os.path.join(BRAND, "lynx-share-logo-small.svg")
  27. SMALL_MAX = 32 # icons up to this size use the small-size form
  28. STEM_MIN = 24 # ... and below this size leave the stem out
  29. TILE = (27, 29, 34) # the tile's colour (#1b1d22)
  30. def p(*a):
  31. return os.path.join(ROOT, *a)
  32. # --- SVG -> RGBA through librsvg + cairo (ctypes, no Python packages beyond Pillow) -----------------------------------
  33. def render_svg(text, size):
  34. rsvg = ctypes.CDLL("librsvg-2.so.2")
  35. cairo = ctypes.CDLL("libcairo.so.2")
  36. class Rect(ctypes.Structure):
  37. _fields_ = [("x", ctypes.c_double), ("y", ctypes.c_double), ("w", ctypes.c_double), ("h", ctypes.c_double)]
  38. rsvg.rsvg_handle_new_from_data.restype = ctypes.c_void_p
  39. rsvg.rsvg_handle_new_from_data.argtypes = [ctypes.c_char_p, ctypes.c_size_t, ctypes.c_void_p]
  40. rsvg.rsvg_handle_render_document.argtypes = [ctypes.c_void_p, ctypes.c_void_p, ctypes.POINTER(Rect), ctypes.c_void_p]
  41. cairo.cairo_image_surface_create.restype = ctypes.c_void_p
  42. cairo.cairo_create.restype = ctypes.c_void_p
  43. cairo.cairo_create.argtypes = [ctypes.c_void_p]
  44. cairo.cairo_surface_flush.argtypes = [ctypes.c_void_p]
  45. cairo.cairo_image_surface_get_data.restype = ctypes.POINTER(ctypes.c_ubyte)
  46. cairo.cairo_image_surface_get_data.argtypes = [ctypes.c_void_p]
  47. cairo.cairo_image_surface_get_stride.argtypes = [ctypes.c_void_p]
  48. cairo.cairo_destroy.argtypes = [ctypes.c_void_p]
  49. cairo.cairo_surface_destroy.argtypes = [ctypes.c_void_p]
  50. data = text.encode()
  51. handle = rsvg.rsvg_handle_new_from_data(data, len(data), None)
  52. if not handle:
  53. sys.exit("make_logo: librsvg cannot read the SVG")
  54. surface = cairo.cairo_image_surface_create(0, size, size) # CAIRO_FORMAT_ARGB32
  55. cr = cairo.cairo_create(surface)
  56. if not rsvg.rsvg_handle_render_document(handle, cr, ctypes.byref(Rect(0, 0, size, size)), None):
  57. sys.exit("make_logo: librsvg could not render the SVG")
  58. cairo.cairo_surface_flush(surface)
  59. stride = cairo.cairo_image_surface_get_stride(surface)
  60. raw = ctypes.string_at(cairo.cairo_image_surface_get_data(surface), stride * size)
  61. cairo.cairo_destroy(cr)
  62. cairo.cairo_surface_destroy(surface)
  63. # premultiplied BGRA (little endian) -> straight RGBA
  64. return Image.frombuffer("RGBa", (size, size), raw, "raw", "BGRa", stride, 1).convert("RGBA")
  65. def without_group(svg, gid):
  66. """svg with the group <g id="gid" ...>...</g> removed (groups may nest)."""
  67. m = re.search(r'<g id="%s"[^>]*>' % gid, svg)
  68. if not m:
  69. sys.exit("make_logo: no group %s in the SVG" % gid)
  70. depth, i = 1, m.end()
  71. for t in re.finditer(r"<g[\s>]|</g>", svg[i:]):
  72. depth += -1 if t.group(0) == "</g>" else 1
  73. if depth == 0:
  74. return svg[:m.start()] + svg[i + t.end():]
  75. sys.exit("make_logo: group %s is not closed" % gid)
  76. def mark_only(svg):
  77. """The mark without its tile, in the mark's own 512-unit frame (its placement in the tile removed)."""
  78. svg = without_group(svg, "tile")
  79. return re.sub(r'<g id="mark" transform="[^"]*">', '<g id="mark">', svg, count=1)
  80. # --- the logo and its forms ------------------------------------------------------------------------------------------
  81. class Logo:
  82. def __init__(self):
  83. self.master = open(MASTER).read()
  84. self.small_svg = open(SMALL).read()
  85. self.mark_svg = mark_only(self.master).replace(
  86. "<title>", "<!-- generated by tools/make_logo.py from lynx-share-logo.svg: the mark without its tile -->\n<title>", 1)
  87. self.mark_small_svg = mark_only(self.small_svg)
  88. self._cache = {}
  89. def _render(self, svg, size):
  90. """svg at size x size: rendered at 1024 px (or 2x for big sizes) and scaled down, as every size before."""
  91. key = (svg, size)
  92. if key not in self._cache:
  93. work = max(1024, 2 * size)
  94. img = render_svg(svg, work)
  95. self._cache[key] = img if work == size else img.resize((size, size), Image.LANCZOS)
  96. return self._cache[key]
  97. def form(self, size):
  98. """The SVG used at an icon size (master, or the small-size form with or without the stem)."""
  99. if size > SMALL_MAX:
  100. return self.master
  101. return self.small_svg if size >= STEM_MIN else without_group(self.small_svg, "stem")
  102. def icon(self, size):
  103. """The app icon (tile + mark) at size x size, with transparent corners."""
  104. return self._render(self.form(size), size).copy()
  105. def icon_on_plate(self, size):
  106. """The app icon on a full square of the tile's colour (for places that cut their own corners: iOS)."""
  107. out = Image.new("RGBA", (size, size), TILE + (255,))
  108. out.alpha_composite(self.icon(size))
  109. return out
  110. def mark(self, size, small=False):
  111. """The mark (with its stem) without the tile, in its 512-unit frame (transparent)."""
  112. svg = self.mark_small_svg if small else self.mark_svg
  113. if small and size < STEM_MIN:
  114. svg = without_group(svg, "stem")
  115. return self._render(svg, size).copy()
  116. def placed(self, canvas, fraction):
  117. """The icon at `fraction` of a transparent square canvas, centred (macOS icon grid)."""
  118. out = Image.new("RGBA", (canvas, canvas), (0, 0, 0, 0))
  119. side = round(canvas * fraction)
  120. out.alpha_composite(self.icon(side), ((canvas - side) // 2,) * 2)
  121. return out
  122. def save(img, *path, **kw):
  123. full = p(*path)
  124. os.makedirs(os.path.dirname(full), exist_ok=True)
  125. img.save(full, optimize=True, **kw)
  126. print(" " + os.path.join(*path))
  127. def write_text(text, *path):
  128. full = p(*path)
  129. os.makedirs(os.path.dirname(full), exist_ok=True)
  130. with open(full, "w") as f:
  131. f.write(text)
  132. print(" " + os.path.join(*path))
  133. def brand(L):
  134. write_text(L.mark_svg, "resources", "brand", "lynx-share-mark.svg")
  135. # the small-size forms as pictures (the MSIX script and the site's header repaint read them)
  136. save(L._render(L.small_svg, 1024), "resources", "brand", "lynx-share-logo-small.png")
  137. save(L._render(without_group(L.small_svg, "stem"), 1024), "resources", "brand", "lynx-share-logo-tiny.png")
  138. save(L.mark(1024, small=True), "resources", "brand", "lynx-share-mark-small.png")
  139. # --- targets ---------------------------------------------------------------------------------------------------------
  140. def site(L):
  141. save(L.icon(256), "docs", "img", "logo.png")
  142. save(L.icon(256), "website", "assets", "img", "logo.png")
  143. save(L.icon(64), "website", "assets", "img", "favicon.png")
  144. save(L.icon(32), "website", "assets", "img", "favicon-32.png")
  145. save(L.icon(16), "website", "assets", "img", "favicon-16.png")
  146. save(L.icon_on_plate(180), "website", "assets", "img", "apple-touch-icon.png")
  147. write_text(L.master, "website", "assets", "img", "lynx-share-logo.svg")
  148. save(L.icon(300), "store", "logo-300.png")
  149. # Store box art: the icon on the app's near-black
  150. art = Image.new("RGBA", (1080, 1080), (17, 17, 20, 255))
  151. art.alpha_composite(L.icon(800), (140, 140))
  152. save(art.convert("RGB"), "store", "box-art-1080.png")
  153. # GitHub social preview: drawn whole (text, this icon, the website's hero picture) by scripts/social-preview.py
  154. import subprocess
  155. subprocess.run([sys.executable, p("scripts", "social-preview.py")], check=True)
  156. def app(L):
  157. save(L.icon(1024), "resources", "lynx_badge.png") # the icon at 1024 px: LogoData.h, MSIX tiles
  158. save(L.icon(256), "resources", "lynx_icon_256.png") # Linux packages
  159. save(L.mark(1024), "resources", "lynx_logo.png") # the mark alone (tools/make_logo_data.py: plugin headers)
  160. # macOS (CMake ICON_BIG / ICON_SMALL -> .icns): Apple's icon grid, the tile 824 of 1024 px with a transparent margin
  161. save(L.placed(1024, 824 / 1024), "resources", "macos", "lynx-icon-1024.png")
  162. save(L.placed(256, 824 / 1024), "resources", "macos", "lynx-icon-256.png")
  163. sizes = [16, 20, 24, 32, 40, 48, 64, 96, 128, 256]
  164. ico = [L.icon(s) for s in sizes]
  165. ico[-1].save(p("resources", "LYNX.ico"), format="ICO", sizes=[(s, s) for s in sizes], append_images=ico[:-1])
  166. print(" resources/LYNX.ico")
  167. for s in (16, 24, 32, 48, 64, 128, 256, 512):
  168. save(L.icon(s), "resources", "linux", "icons", "hicolor", "%dx%d" % (s, s), "apps", "lynx.png")
  169. def main():
  170. what = sys.argv[1] if len(sys.argv) > 1 else "all"
  171. if what not in ("site", "app", "all"):
  172. sys.exit(__doc__)
  173. L = Logo()
  174. brand(L)
  175. if what in ("site", "all"):
  176. site(L)
  177. if what in ("app", "all"):
  178. app(L)
  179. return 0
  180. if __name__ == "__main__":
  181. sys.exit(main())