#!/usr/bin/env python3 """生成站点图标全家桶(``public/favicon.svg`` / ``favicon.ico`` / ``public/icons/*.png``)。 为什么要一个脚本而不是把图扔进仓库就完事:图标是**跟着主题色走的**—— 底色就是 tailwind.config.js 里 primary 那一档(#14b8a6 → #0f766e, 与应用里那块 "OA" 磁贴同一支渐变),字形沿用侧边栏上用户已经认了很久的两个字母。 哪天改主色或改字形,跑一次这 script 就整套对齐,不用一张一张 P。 浏览器页签上那一枚过去指的是 ``public/logo.svg``——上游模板留下的一个深底 "S" 记号,跟系统左上角那块 "OA" 磁贴没有半点关系,看着就像两套产品。现在页签那一枚 (矢量 ``favicon.svg`` + 给老内核兜底的 ``favicon.ico``)与装到桌面上的 PNG 由下面同一套参数生成:同一个字、同一支渐变,两处不可能再各走各的。 只在改图标时跑一次,需要 Pillow(不装进 package.json,构建流程不碰它):: python3 scripts/gen-icons.py """ from __future__ import annotations from pathlib import Path from PIL import Image, ImageDraw, ImageFont PUBLIC_DIR = Path(__file__).resolve().parent.parent / "public" ICON_DIR = PUBLIC_DIR / "icons" #: 与 tailwind.config.js 的 primary-500 / primary-700 对齐(同一支 gradient-primary) TOP = (20, 184, 166) BOTTOM = (15, 118, 110) TEXT = (255, 255, 255) WORD = "OA" #: 字形占画布的比例:装到桌面那两张 PNG 与页签上那一枚共用这一把尺子 GLYPH_SCALE = 0.42 #: 页签图标的圆角:对齐侧边栏左上角那颗磁贴(``h-9 w-9 rounded-xl`` = 36px 上的 12px) FAVICON_RADIUS_RATIO = 1 / 3 #: 矢量图里的字体名:macOS / Windows 都装着 Arial,落到 Linux 上退无衬线粗体 SVG_FONT_STACK = "Arial, 'Helvetica Neue', Helvetica, sans-serif" #: macOS 自带的粗体无衬线;找不到就退回 PIL 自带的位图字体(会被放大,仅兜底) FONT_CANDIDATES = ( "/System/Library/Fonts/Supplemental/Arial Bold.ttf", "/Library/Fonts/Arial Bold.ttf", "C:\\Windows\\Fonts\\arialbd.ttf", ) def font(size: int) -> ImageFont.FreeTypeFont | ImageFont.ImageFont: for path in FONT_CANDIDATES: if Path(path).exists(): return ImageFont.truetype(path, size) return ImageFont.load_default() def gradient(size: int) -> Image.Image: """左上到右下的线性渐变,跟 CSS 的 135deg 同一走向。""" img = Image.new("RGB", (size, size), TOP) pixels = img.load() for y in range(size): for x in range(size): ratio = (x / size + y / size) / 2 pixels[x, y] = tuple( round(TOP[i] + (BOTTOM[i] - TOP[i]) * ratio) for i in range(3) ) return img def draw_word(img: Image.Image, size: int, scale: float) -> None: """把 "OA" 居中贴上去;scale 控制字形占画布的比例(maskable 要缩到安全区里)。""" draw = ImageDraw.Draw(img) text_font = font(int(size * scale)) box = draw.textbbox((0, 0), WORD, font=text_font) width, height = box[2] - box[0], box[3] - box[1] draw.text( ((size - width) / 2 - box[0], (size - height) / 2 - box[1]), WORD, font=text_font, fill=TEXT, ) def _hex(rgb: tuple[int, int, int]) -> str: return "#{:02x}{:02x}{:02x}".format(*rgb) def favicon_svg(size: int) -> str: """把同一块磁贴画成矢量,供浏览器页签用:16px 到 512px 都是一条清晰的边。 字形的位置按本机那份 Arial Bold 的真尺算出来(与上面 PNG 同一套算法:让墨迹 居中),横向则交给 ``text-anchor`` 自己找中——对面浏览器手上未必有 Arial (Linux 上常见),换一套字体也不会把字挤出画面。 """ text_font = font(int(size * GLYPH_SCALE)) box = ImageDraw.Draw(Image.new("RGB", (8, 8))).textbbox((0, 0), WORD, font=text_font) # 大写 "OA" 的墨迹高度就是 box[3] - box[1],基线落在"墨迹居中之后的下沿" baseline = round(size / 2 + (box[3] - box[1]) / 2, 1) return ( f'\n' " \n" f' \n' f' \n' f' \n' " \n" " \n" f' \n' f' {WORD}\n' "\n" ) def rounded(size: int, scale: float, radius_ratio: float | None) -> Image.Image: """radius_ratio=None → 铺满方图(iOS 自己会切圆角,maskable 也必须是方图)。""" img = gradient(size).convert("RGBA") draw_word(img, size, scale) if radius_ratio is None: return img mask = Image.new("L", (size, size), 0) ImageDraw.Draw(mask).rounded_rectangle( [0, 0, size - 1, size - 1], radius=int(size * radius_ratio), fill=255 ) img.putalpha(mask) return img def main() -> None: PUBLIC_DIR.mkdir(parents=True, exist_ok=True) ICON_DIR.mkdir(parents=True, exist_ok=True) targets = ( # (文件名, 边长, 字形比例, 圆角比例) ("icon-192.png", 192, GLYPH_SCALE, 0.22), ("icon-512.png", 512, GLYPH_SCALE, 0.22), # maskable:内容必须落在中间 80% 的安全区里,且不许有透明通道 ("icon-maskable-192.png", 192, 0.30, None), ("icon-maskable-512.png", 512, 0.30, None), # iOS 加到桌面用:方图、不透明,系统自己蒙圆角 ("apple-touch-icon.png", 180, 0.40, None), ) for name, size, scale, radius in targets: image = rounded(size, scale, radius) if radius is None: image = image.convert("RGB") image.save(ICON_DIR / name) print(f"写入 public/icons/{name} {size}x{size}") # 页签那一枚:矢量为主;同一份字节再落一个 logo.svg——已有环境把站点 logo # 存成了 /logo.svg(界面上保存过、或 OA_SITE_LOGO 指过来的),不改配置也换过来 svg = favicon_svg(512) for name in ("favicon.svg", "logo.svg"): (PUBLIC_DIR / name).write_text(svg, encoding="utf-8") print(f"写入 public/{name} 矢量 512x512") # 不认 SVG favicon 的那几档浏览器(老 Safari、Edge 旧内核、IE 模式)退回 .ico rounded(512, GLYPH_SCALE, FAVICON_RADIUS_RATIO).save( PUBLIC_DIR / "favicon.ico", format="ICO", sizes=[(16, 16), (24, 24), (32, 32), (48, 48)], ) print("写入 public/favicon.ico 16 / 24 / 32 / 48") if __name__ == "__main__": main()