Branchwell

Inkspring / app / build / generate-installer-background.py

1"""Render the Inkspring Finder background at 1x and 2x. Requires Pillow."""
2
3import argparse
4import math
5import subprocess
6from pathlib import Path
7
8from PIL import Image, ImageDraw, ImageFont
9
10FONT = "/System/Library/Fonts/SFNS.ttf"
11INK = "#24251f"
12SECONDARY = "#63584a"
13BORDER = "#c9bca9"
14ACCENT = "#a6482d"
15
16
17def draw_arrow(canvas: Image.Image, scale: int) -> None:
18 # Finder can use the 1x TIFF page. Render the arrow at 4x before downsampling
19 # either page so the diagonals and their joins have smooth edge coverage.
20 left, top, width, height = 315, 210, 89, 40
21 resolution = 4 * scale
22 mask = Image.new("L", (width * resolution, height * resolution))
23 draw = ImageDraw.Draw(mask)
24 radius = 1.5 * resolution
25 for path in ([(321, 230), (397, 230)], [(382, 215), (397, 230), (382, 245)]):
26 points = [((x - left) * resolution, (y - top) * resolution) for x, y in path]
27 draw.line(points, fill=255, width=3 * resolution)
28 for x, y in points:
29 draw.ellipse((x - radius, y - radius, x + radius, y + radius), fill=255)
30 coverage = mask.resize((width * scale, height * scale), Image.Resampling.LANCZOS)
31 canvas.paste(ACCENT, (left * scale, top * scale), coverage)
32
33
34def render(scale: int, target: Path) -> None:
35 width, height = 720 * scale, 460 * scale
36 canvas = Image.new("RGB", (width, height))
37 base, glow = (236, 228, 215), (251, 248, 241)
38 canvas.putdata([
39 tuple(round(start + (end - start) * amount) for start, end in zip(base, glow))
40 for y in range(height)
41 for x in range(width)
42 for amount in [math.exp(-(((x / scale - 360) / 410) ** 2
43 + ((y / scale - 230) / 300) ** 2))]
44 ])
45 draw = ImageDraw.Draw(canvas)
46
47 def points(*values: float) -> tuple[int, ...]:
48 return tuple(round(value * scale) for value in values)
49
50 def text(value: str, y: int, size: int, color: str, bold: bool = False) -> None:
51 font = ImageFont.truetype(FONT, size * scale)
52 font.set_variation_by_name("Bold" if bold else "Regular")
53 draw.text(points(360, y), value, font=font, fill=color, anchor="mt")
54
55 draw.rounded_rectangle(points(18, 18, 702, 442), radius=22 * scale, outline=BORDER, width=scale)
56 text("Install Inkspring", 48, 30, INK, bold=True)
57 text("Drag Inkspring to Applications to get started.", 95, 16, SECONDARY)
58 for left in (109, 439):
59 draw.rounded_rectangle(points(left, 154, left + 172, 320), radius=22 * scale, fill="#fdfaf5")
60
61 # Finder overlays the real movable app and Applications shortcut.
62 left, top, right, bottom, radius = 102, 147, 288, 327, 29
63 dots = []
64 for start, end, fixed, horizontal in (
65 (left + radius, right - radius, top, True),
66 (left + radius, right - radius, bottom, True),
67 (top + radius, bottom - radius, left, False),
68 (top + radius, bottom - radius, right, False),
69 ):
70 count = round((end - start) / 9)
71 for index in range(count + 1):
72 value = start + (end - start) * index / count
73 dots.append((value, fixed) if horizontal else (fixed, value))
74 for cx, cy, start in ((left + radius, top + radius, 180),
75 (right - radius, top + radius, 270),
76 (right - radius, bottom - radius, 0),
77 (left + radius, bottom - radius, 90)):
78 for angle in range(start + 15, start + 90, 20):
79 dots.append((cx + radius * math.cos(math.radians(angle)),
80 cy + radius * math.sin(math.radians(angle))))
81 for x, y in dots:
82 draw.ellipse(points(x - 1.15, y - 1.15, x + 1.15, y + 1.15), fill="#b75635")
83
84 draw_arrow(canvas, scale)
85 draw.line(points(88, 355, 632, 355), fill=BORDER, width=scale)
86 text("Eject the disk image when copying is complete.", 378, 15, INK)
87 text("Open Inkspring from your Applications folder.", 410, 14, SECONDARY)
88 canvas.save(target, dpi=(72 * scale, 72 * scale))
89
90
91def main() -> None:
92 parser = argparse.ArgumentParser(description=__doc__)
93 parser.add_argument("output_directory", type=Path)
94 output = parser.parse_args().output_directory
95 output.mkdir(parents=True, exist_ok=True)
96 standard, retina = output / "background.png", output / "background@2x.png"
97 render(1, standard)
98 render(2, retina)
99 subprocess.run(["tiffutil", "-cathidpicheck", str(standard), str(retina),
100 "-out", str(output / "installer-background.tiff")], check=True)
101
102
103if __name__ == "__main__":
104 main()