Files
Reims-Go/scripts/brand/wordmark.py
Nwflower f4395a1f28 refactor(brand): generate the frontend logo copy and wordmark geometry
assets/logo.svg was duplicated byte for byte in frontend/public, and WordmarkLoader hand-copied the ten wordmark strokes from the Python generator, so the icon and the lettering could drift silently.

scripts/brand/wordmark.py now writes frontend/public/logo.svg and frontend/src/components/wordmark.gen.ts, and the loader imports that geometry. The unreferenced assets/wordmark-*.svg outputs are gone, and frontend/scripts/test-brand.mjs fails when either copy is edited by hand.
2026-12-04 23:24:36 -07:00

243 lines
10 KiB
Python

"""Draws the Reims-Go wordmark, the README banners and the frontend wordmark data.
The wordmark is monoline lettering: one stroke width, round caps, built
from circles, ellipses and straight lines so it matches the rounded app
icon. Run from the repository root:
python scripts/brand/wordmark.py
It writes .github/readme/banner-{light,dark}.svg,
frontend/src/components/wordmark.gen.ts and frontend/public/logo.svg, and
needs only the standard library.
assets/logo.svg is the source of the icon: this script copies it to
frontend/public/logo.svg and embeds it in the banners. The raster icons
assets/logo.png (embedded by main.go for the tray), build/appicon.png and
build/windows/icon.ico are exported from the same artwork by hand, so
re-export them whenever assets/logo.svg changes.
"""
import math
import re
from pathlib import Path
ROOT = Path(__file__).resolve().parents[2]
X = 72.0 # x-height, outer edge to outer edge
INK = {"light": "#16182e", "dark": "#f4f5fa"}
MUTED = {"light": "#a3a8bf", "dark": "#5d6382"}
GRADIENT = ("#3b8cff", "#6d5dfc", "#ff7a9a")
BOLT = ("#ffd43b", "#ff9f0a")
PAD = 4 # margin around the wordmark frame
# Roles the frontend loader can paint: CSS classes plus the brand gradient.
TS_INK = {"ink": "ink", "muted": "dash", "go": "brand"}
PATH_ELEMENT = re.compile(r'^<path d="([^"]+)"$')
CIRCLE_ELEMENT = re.compile(r'^<circle cx="([^"]+)" cy="([^"]+)" r="([^"]+)"$')
# The style the script writes. Variants used while choosing it:
# joiner: "dash" | "bolt" | "none"; go: "gradient" | "pill"
# dot: "accent" | "ink"; stroke: stroke width.
STYLE = dict(stroke=15.0, joiner="dash", go="gradient", dot="accent")
def f(v):
return ("%.2f" % v).rstrip("0").rstrip(".")
def arc(rx, ry, large, sweep, x, y):
return f"A{f(rx)} {f(ry)} 0 {large} {sweep} {f(x)} {f(y)}"
class Geo:
def __init__(self, w):
self.W, self.H = w, w / 2
self.TOP, self.BOT, self.MID = -X + w / 2, -w / 2, -X / 2
self.R = (X - w) / 2
# Glyphs take the geometry and the left outer edge x, and return
# (outer width, [(role, svg element without paint)]). Roles pick the paint.
def g_r(g, x):
s, rx = x + g.H, 24
return g.W + rx, [("ink", f'<path d="M{f(s)} {f(g.BOT)}V{f(g.MID)}{arc(rx, g.MID - g.TOP, 0, 1, s + rx, g.TOP)}"')]
def g_e(g, x):
c, a = x + g.H + g.R, math.radians(42)
ex, ey = c + g.R * math.cos(a), g.MID + g.R * math.sin(a)
return 2 * g.R + g.W, [("ink", f'<path d="M{f(c - g.R)} {f(g.MID)}H{f(c + g.R)}{arc(g.R, g.R, 1, 0, ex, ey)}"')]
def g_i(g, x):
s = x + g.H
return g.W, [("ink", f'<path d="M{f(s)} {f(g.TOP)}V{f(g.BOT)}"'),
("dot", f'<circle cx="{f(s)}" cy="{f(-X - 22)}" r="{f(g.H + 1.5)}"')]
def g_m(g, x):
s, rx, ry = x + g.H, 22, g.MID - g.TOP
return 4 * rx + g.W, [("ink", f'<path d="M{f(s)} {f(g.BOT)}V{f(g.MID)}{arc(rx, ry, 0, 1, s + 2 * rx, g.MID)}V{f(g.BOT)}'
f'M{f(s + 2 * rx)} {f(g.MID)}{arc(rx, ry, 0, 1, s + 4 * rx, g.MID)}V{f(g.BOT)}"')]
def g_s(g, x):
rx, ry = 21, (g.BOT - g.TOP) / 4
c, up, lo, a = x + g.H + rx, g.TOP + ry, g.BOT - ry, math.radians(38)
sx, sy = c + rx * math.cos(a), up - ry * math.sin(a)
tx, ty = c - rx * math.cos(a), lo + ry * math.sin(a)
return 2 * rx + g.W, [("ink", f'<path d="M{f(sx)} {f(sy)}{arc(rx, ry, 0, 0, c - rx, up)}{arc(rx, ry, 0, 0, c, g.MID)}'
f'{arc(rx, ry, 0, 1, c + rx, lo)}{arc(rx, ry, 0, 1, tx, ty)}"')]
def g_dash(g, x):
return 18 + g.W, [("muted", f'<path d="M{f(x + g.H)} {f(g.MID)}H{f(x + g.H + 18)}"')]
def g_bolt(g, x):
# The icon's bolt, scaled to the x-height and centred on it.
pts = [(800, 628), (708, 744), (768, 744), (748, 822), (846, 700), (784, 700)]
s = (X + 14) / 194
d = "M" + "L".join(f"{f(x + (px - 708) * s)} {f(g.MID + (py - 725) * s)}" for px, py in pts) + "Z"
return 138 * s, [("bolt", f'<path d="{d}"')]
def g_g(g, x):
c, hook, a = x + g.H + g.R, 22.5, math.radians(150)
hx, hy = c + g.R * math.cos(a), hook * math.sin(a)
return 2 * g.R + g.W, [("go", f'<path d="M{f(c + g.R)} {f(g.MID)}{arc(g.R, g.R, 1, 1, c + g.R, g.MID - 0.01)}"'),
("go", f'<path d="M{f(c + g.R)} {f(g.TOP)}V0{arc(g.R, hook, 0, 1, hx, hy)}"')]
def g_o(g, x):
c = x + g.H + g.R
return 2 * g.R + g.W, [("go", f'<path d="M{f(c + g.R)} {f(g.MID)}{arc(g.R, g.R, 1, 1, c + g.R, g.MID - 0.01)}"')]
def wordmark(theme, style=STYLE, gid="wm"):
"""Lays the wordmark out on the baseline y=0.
Returns a dict with the painted svg body, the total width, the ascent and
descent, the unpainted (role, element) strokes in pen order, and the x where
the "go" gradient starts.
"""
g = Geo(style["stroke"])
joiner = {"dash": [(g_dash, 10)], "bolt": [(g_bolt, 8)], "none": []}[style["joiner"]]
pill = style["go"] == "pill"
seq = [(g_r, 4), (g_e, 11), (g_i, 11), (g_m, 11), (g_s, 10 if joiner else 14)] + joiner + [(g_g, 11), (g_o, 0)]
paint = {
"ink": f'stroke="{INK[theme]}"',
"muted": f'stroke="{MUTED[theme]}"',
"dot": f'fill="{GRADIENT[2]}"' if style["dot"] == "accent" else f'fill="{INK[theme]}"',
"bolt": f'fill="url(#{gid}b)" stroke="none"',
"go": 'stroke="#ffffff"' if pill else f'stroke="url(#{gid})"',
}
out, strokes, x, go0 = [], [], 0.0, None
for draw, gap in seq:
if draw is g_g:
go0 = x + (14 if pill else 0)
x = go0
width, parts = draw(g, x)
strokes += parts
out += [f"{el} {paint[role]}/>" for role, el in parts]
x += width + gap
asc, desc = X + 22 + g.H + 1.5, 22.5 + g.H
pill_el = ""
if pill:
top, bot = -X - 12, desc + 8
x += 14
pill_el = (f'<rect x="{f(go0 - 14)}" y="{f(top)}" width="{f(x - go0 + 14)}" '
f'height="{f(bot - top)}" rx="{f((bot - top) * 0.36)}" fill="url(#{gid})"/>')
asc, desc = max(asc, -top), bot
stops = "".join(f'<stop offset="{i / 2:g}" stop-color="{c}"/>' for i, c in enumerate(GRADIENT))
bolt = "".join(f'<stop offset="{i}" stop-color="{c}"/>' for i, c in enumerate(BOLT))
defs = (f'<defs><linearGradient id="{gid}" gradientUnits="userSpaceOnUse" x1="{f(go0 - (14 if pill else 0))}" '
f'y1="0" x2="{f(x)}" y2="0">{stops}</linearGradient>'
f'<linearGradient id="{gid}b" x1="0" y1="0" x2="0" y2="1">{bolt}</linearGradient></defs>')
body = (f'{defs}{pill_el}<g fill="none" stroke-width="{f(g.W)}" stroke-linecap="round" '
f'stroke-linejoin="round">{"".join(out)}</g>')
return dict(body=body, width=x, ascent=asc, descent=desc, strokes=strokes, go_start=go0, pill=pill)
def banner(theme, style=STYLE):
logo = (ROOT / "assets/logo.svg").read_text(encoding="utf-8")
icon_body = re.sub(r"^.*?<svg[^>]*>", "", logo, flags=re.S).rsplit("</svg>", 1)[0]
wm = wordmark(theme, style)
icon, gap = 180, 26
w = icon + gap + wm["width"] + 6
return (f'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 {f(w)} {icon}" width="{f(w)}" '
f'height="{icon}" role="img" aria-label="Reims-Go">'
f'<svg x="0" y="0" width="{icon}" height="{icon}" viewBox="0 0 1024 1024">{icon_body}</svg>'
f'<g transform="translate({icon + gap} {f(icon / 2 + X / 2)})">{wm["body"]}</g></svg>\n')
def ts_strokes(strokes):
"""Turns the unpainted (role, element) strokes into TypeScript literals."""
lines = []
for role, el in strokes:
circle = CIRCLE_ELEMENT.match(el)
if circle:
cx, cy, r = circle.groups()
lines.append(f" {{ kind: 'dot', cx: {cx}, cy: {cy}, r: {r} }},")
continue
path = PATH_ELEMENT.match(el)
if path is None or role not in TS_INK:
raise ValueError(f"the frontend loader cannot paint stroke {el!r} with role {role!r}")
lines.append(f" {{ kind: 'path', ink: '{TS_INK[role]}', d: '{path.group(1)}' }},")
return "\n".join(lines)
def ts_module(style=STYLE):
"""Writes the geometry the frontend WordmarkLoader animates.
The loader paints strokes with CSS classes and one gradient, so it needs
the geometry and the gradient extent, not the per-theme colours.
"""
wm = wordmark("light", style)
width, height = wm["width"] + 2 * PAD, wm["ascent"] + wm["descent"] + 2 * PAD
gradient_x1 = wm["go_start"] - (14 if wm["pill"] else 0)
stops = ", ".join(f"'{c}'" for c in GRADIENT)
return (
"// Code generated by scripts/brand/wordmark.py; DO NOT EDIT.\n"
"// Run \"python scripts/brand/wordmark.py\" after changing the wordmark.\n"
"\n"
"export type WordmarkInk = 'ink' | 'dash' | 'brand'\n"
"\n"
"export type WordmarkStroke =\n"
" | { kind: 'path'; ink: WordmarkInk; d: string }\n"
" | { kind: 'dot'; cx: number; cy: number; r: number }\n"
"\n"
"/** viewBox of the wordmark, in generator units. */\n"
f"export const WORDMARK_VIEWBOX = {{ width: {f(width)}, height: {f(height)} }}\n"
"\n"
"/** Translation that puts the generator's baseline frame into the viewBox. */\n"
f"export const WORDMARK_ORIGIN = {{ x: {PAD}, y: {f(PAD + wm['ascent'])} }}\n"
"\n"
f"export const WORDMARK_STROKE_WIDTH = {f(style['stroke'])}\n"
"\n"
"/** Horizontal extent of the brand gradient on the baseline. */\n"
f"export const WORDMARK_GRADIENT = {{ x1: {f(gradient_x1)}, x2: {f(wm['width'])}, stops: [{stops}] }}\n"
"\n"
"/** Strokes in pen order, in the generator's baseline frame. */\n"
"export const WORDMARK_STROKES: WordmarkStroke[] = [\n"
f"{ts_strokes(wm['strokes'])}\n"
"]\n"
)
def main():
# Text outputs use the platform line endings so a rerun leaves the working
# tree clean under core.autocrlf=true as well as on Linux.
for theme in ("light", "dark"):
(ROOT / f".github/readme/banner-{theme}.svg").write_text(banner(theme), encoding="utf-8")
(ROOT / "frontend/src/components/wordmark.gen.ts").write_text(ts_module(), encoding="utf-8")
# The frontend serves the icon from public/, so keep that copy byte-identical.
(ROOT / "frontend/public/logo.svg").write_bytes((ROOT / "assets/logo.svg").read_bytes())
if __name__ == "__main__":
main()