#!/usr/bin/env python3 """Generate WESP favicons: Cyrillic «К» on transparent background.""" from __future__ import annotations import sys from pathlib import Path from fontTools.ttLib import TTFont from PIL import Image, ImageDraw, ImageFont ROOT = Path(__file__).resolve().parents[1] STATIC = ROOT / "static" CYRILLIC_K = "\u041a" BRAND = (255, 255, 255, 255) FONT_CANDIDATES = [ "/System/Library/Fonts/Supplemental/Arial Bold.ttf", "/Library/Fonts/Arial Unicode.ttf", "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", "/usr/share/fonts/truetype/liberation/LiberationSans-Bold.ttf", ] def _pick_font_path() -> Path: for candidate in FONT_CANDIDATES: path = Path(candidate) if path.is_file(): return path raise FileNotFoundError("No suitable bold font found for Cyrillic «К»") def _write_svg(dest: Path) -> None: svg = """ К """ dest.write_text(svg, encoding="utf-8") def _draw_png(size: int, font_path: Path, dest: Path) -> None: img = Image.new("RGBA", (size, size), (0, 0, 0, 0)) draw = ImageDraw.Draw(img) font = ImageFont.truetype(str(font_path), round(size * 0.72)) bbox = draw.textbbox((0, 0), CYRILLIC_K, font=font) tw = bbox[2] - bbox[0] th = bbox[3] - bbox[1] x = (size - tw) / 2 - bbox[0] y = (size - th) / 2 - bbox[1] - size * 0.02 draw.text((x, y), CYRILLIC_K, font=font, fill=BRAND) img.save(dest, format="PNG") def main() -> int: font_path = _pick_font_path() _write_svg(STATIC / "favicon.svg") _draw_png(32, font_path, STATIC / "favicon-32.png") _draw_png(180, font_path, STATIC / "apple-touch-icon.png") print(f"Generated favicons using {font_path}") return 0 if __name__ == "__main__": sys.exit(main())