Files
site/WESP_REL/scripts/generate_favicons.py
T
2026-07-17 12:57:18 +03:00

67 lines
2.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/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 = """<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32" role="img" aria-label="Комптон">
<text x="16" y="24.5" text-anchor="middle"
font-family="Arial, Helvetica, 'DejaVu Sans', sans-serif"
font-size="22" font-weight="700" fill="#ffffff">К</text>
</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())