kit-square.py
In web/
Preview
Code1.7 KiB"""Export a compact KIT wordmark fitted to a square favicon."""
from pathlib import Path
from copy import deepcopy
import sys
import struct
import subprocess
import xml.etree.ElementTree as E
ROOT = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(ROOT))
from source.logo import render, NS
logo = E.fromstring(render(False))
root = E.Element(NS + 'svg', {'width': '64', 'height': '64', 'viewBox': '0 0 64 64', 'role': 'img', 'aria-label': 'KIT'})
E.SubElement(root, NS + 'rect', {'width': '64', 'height': '64', 'rx': '14', 'fill': '#0067ed'})
# The outlined wordmark bounds are 66.51 by 19.79 units.
# Apply one uniform scale so every contour and letter gap is preserved.
scale = 60 / 66.51
height = 19.79 * scale
letters = E.SubElement(root, NS + 'g', {'transform': f'translate(2 {(64 - height) / 2}) scale({scale}) translate(-7.62 -50.66)'})
for face in list(logo)[-3:]:
face = deepcopy(face)
face.set('fill', 'white')
letters.append(face)
art = ROOT / 'art/favicon'
art.mkdir(parents=True, exist_ok=True)
svg = art / 'kit-square.svg'
svg.write_text(E.tostring(root, encoding='unicode') + '\n')
images = []
for size in [16, 32, 48, 64, 96, 128, 180, 192, 256, 512]:
png = ROOT / f'previews/favicon/kit-square-{size}.png'
subprocess.run(['rsvg-convert', '-w', str(size), '-h', str(size), '-o', str(png), str(svg)], check=True)
if size in [16, 32, 48, 64, 128, 256]:
images.append((size, png.read_bytes()))
offset = 6 + 16 * len(images)
entries = []
for size, data in images:
entries.append(struct.pack('<BBBBHHII', size % 256, size % 256, 0, 0, 1, 32, len(data), offset))
offset += len(data)
(art / 'kit-square.ico').write_bytes(struct.pack('<HHH', 0, 1, len(images)) + b''.join(entries) + b''.join(data for _, data in images))