1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
#!/usr/bin/env python3
"""Build square PNG and WebP exports for every approved logo SVG."""
from __future__ import annotations
import json
from pathlib import Path
import shutil
import subprocess
ROOT = Path(__file__).resolve().parents[1]
LOGO_DIR = ROOT / "logos"
RASTER_DIR = LOGO_DIR / "raster"
SIZES = (16, 32, 48, 64, 128, 256, 512, 1024)
PRODUCTS = (
("packet-warriors", "Packet Warriors"),
("firefly", "Firefly"),
("xlx-observatory", "XLX Observatory"),
("fwdfix", "FwdFix"),
("gps-clock-dash", "GPS Clock Dash"),
)
VARIANTS = {
"flat-dark": "Flat / dark",
"flat-light": "Flat / light",
"mono-dark": "Monochrome / dark",
"mono-light": "Monochrome / light",
"metallic": "Metallic / optional",
"icon": "Favicon / app tile",
}
def asset_metadata(source: Path) -> dict[str, object]:
for slug, product in PRODUCTS:
prefix = f"{slug}-"
if source.stem.startswith(prefix):
variant = source.stem.removeprefix(prefix)
if variant not in VARIANTS:
break
return {
"slug": source.stem,
"product": product,
"productSlug": slug,
"variant": variant,
"variantLabel": VARIANTS[variant],
"svg": f"logos/{source.name}",
"preview": "light" if variant.endswith("light") else "dark",
}
raise ValueError(f"Unrecognized approved logo filename: {source.name}")
def render(magick: str, source: Path, target: Path, size: int, fmt: str) -> None:
target.parent.mkdir(parents=True, exist_ok=True)
command = [
magick,
"-background",
"none",
"-density",
"384",
str(source),
"-resize",
f"{size}x{size}",
"-gravity",
"center",
"-extent",
f"{size}x{size}",
"-strip",
]
if fmt == "png":
command.append(f"PNG32:{target}")
else:
command.extend(("-define", "webp:lossless=true", str(target)))
subprocess.run(command, check=True)
def main() -> None:
magick = shutil.which("magick")
if magick is None:
raise SystemExit("ImageMagick 7 (`magick`) is required to build logo exports.")
source_assets = [(source, asset_metadata(source)) for source in LOGO_DIR.glob("*.svg")]
product_order = {slug: index for index, (slug, _) in enumerate(PRODUCTS)}
variant_order = {variant: index for index, variant in enumerate(VARIANTS)}
source_assets.sort(
key=lambda item: (
product_order[str(item[1]["productSlug"])],
variant_order[str(item[1]["variant"])],
)
)
sources = [source for source, _ in source_assets]
assets = [asset for _, asset in source_assets]
# These two format trees contain generated files only. Recreate them so
# removed or renamed source assets cannot leave stale downloads behind.
for fmt in ("png", "webp"):
output_dir = RASTER_DIR / fmt
if output_dir.exists():
shutil.rmtree(output_dir)
for source, asset in zip(sources, assets, strict=True):
raster: dict[str, dict[str, str]] = {}
for size in SIZES:
raster[str(size)] = {}
for fmt in ("png", "webp"):
relative = Path("logos") / "raster" / fmt / str(size) / f"{source.stem}.{fmt}"
render(magick, source, ROOT / relative, size, fmt)
raster[str(size)][fmt] = relative.as_posix()
asset["raster"] = raster
payload = {
"version": 1,
"canvas": "square-transparent",
"sizes": list(SIZES),
"assets": assets,
}
manifest = json.dumps(payload, indent=2) + "\n"
RASTER_DIR.mkdir(parents=True, exist_ok=True)
(RASTER_DIR / "manifest.json").write_text(manifest, encoding="utf-8")
(LOGO_DIR / "logo-library.js").write_text(
"window.PW_LOGO_LIBRARY = " + json.dumps(payload, separators=(",", ":")) + ";\n",
encoding="utf-8",
)
print(f"Built {len(assets)} logos in {len(SIZES)} sizes as PNG and WebP.")
if __name__ == "__main__":
main()
|