Branchwell

Textley / app / Packaging / macOS / generate-background.py

1"""Render original installer artwork at 1x/2x. Requires Pillow, not used in CI."""
2from pathlib import Path
3import math
4import subprocess
5import tempfile
6
7from PIL import Image, ImageDraw, ImageFont
8
9ROOT = Path(__file__).resolve().parent
10FONT = Path("/System/Library/Fonts/SFNS.ttf")
11
12
13def render(scale: int, target: Path) -> None:
14 width, height = 720 * scale, 460 * scale
15 canvas = Image.new("RGB", (width, height))
16 pixels = canvas.load()
17 # A lavender field that keeps Finder’s dark icon labels readable.
18 for y in range(height):
19 for x in range(width):
20 glow = math.exp(-(((x / scale - 360) / 410) ** 2
21 + ((y / scale - 240) / 300) ** 2))
22 pixels[x, y] = tuple(round(base + glow * lift)
23 for base, lift in zip((220, 210, 244), (25, 25, 10)))
24 draw = ImageDraw.Draw(canvas)
25
26 def box(bounds):
27 return tuple(round(value * scale) for value in bounds)
28
29 def text(value: str, y: int, size: int, color: str, weight: int = 400):
30 font = ImageFont.truetype(str(FONT), size * scale)
31 # SF's weight axis uses its own range. Select named instances when present.
32 names = font.get_variation_names()
33 desired = b"Bold" if weight >= 600 else b"Regular"
34 if desired in names:
35 font.set_variation_by_name(desired)
36 draw.text((360 * scale, y * scale), value, font=font,
37 fill=color, anchor="mt")
38
39 draw.rounded_rectangle(box((18, 18, 702, 442)), radius=22 * scale,
40 outline="#A99BC7", width=scale)
41 text("Install Textley", 48, 30, "#2A2140", 700)
42 text("Drag Textley to Applications to get started.", 95, 16, "#4C3E64")
43
44 # The real draggable Finder icons are placed over these two quiet wells.
45 draw.rounded_rectangle(box((117, 154, 273, 316)), radius=22 * scale,
46 fill="#FAF7FF")
47 draw.rounded_rectangle(box((447, 154, 603, 316)), radius=22 * scale,
48 fill="#FAF7FF", outline="#8C6BC0", width=scale)
49 # Rounded dotted outline: evenly spaced dots along four sides and corner arcs.
50 left, top, right, bottom, radius = 110, 147, 280, 323, 29
51 dots = []
52 for start, stop, fixed, horizontal in (
53 (left + radius, right - radius, top, True),
54 (left + radius, right - radius, bottom, True),
55 (top + radius, bottom - radius, left, False),
56 (top + radius, bottom - radius, right, False),
57 ):
58 count = round((stop - start) / 9)
59 for index in range(count + 1):
60 value = start + (stop - start) * index / count
61 dots.append((value, fixed) if horizontal else (fixed, value))
62 for cx, cy, begin in ((left + radius, top + radius, 180),
63 (right - radius, top + radius, 270),
64 (right - radius, bottom - radius, 0),
65 (left + radius, bottom - radius, 90)):
66 for angle in range(begin + 15, begin + 90, 20):
67 dots.append((cx + radius * math.cos(math.radians(angle)),
68 cy + radius * math.sin(math.radians(angle))))
69 for x, y in dots:
70 draw.ellipse(box((x - 1.15, y - 1.15, x + 1.15, y + 1.15)), fill="#6E4BA7")
71
72 # Rounded arrow pointing from the app to the actual Applications shortcut.
73 draw.line(box((321, 230, 396, 230)), fill="#6E4BA7", width=3 * scale)
74 draw.line(box((381, 215, 397, 230, 381, 245)), fill="#6E4BA7", width=3 * scale)
75 draw.line(box((88, 355, 632, 355)), fill="#A99BC7", width=scale)
76 text("Eject this disk image when copying is complete.", 378, 15, "#37294F")
77 text("Open Textley from your Applications folder.", 410, 14, "#55476E")
78 canvas.save(target, dpi=(72 * scale, 72 * scale))
79
80
81if __name__ == "__main__":
82 with tempfile.TemporaryDirectory(prefix="textley-dmg-artwork-") as directory:
83 normal = Path(directory) / "background.png"
84 retina = Path(directory) / "background@2x.png"
85 render(1, normal)
86 render(2, retina)
87 subprocess.run(["tiffutil", "-cathidpicheck", normal, retina, "-out",
88 ROOT / "background.tiff"], check=True)