"""Render compact PNG cards for AstrBot chat responses.""" from __future__ import annotations import hashlib from pathlib import Path from typing import Iterable from core.runtime_paths import resolve_data_path CARD_DIR = resolve_data_path("data/chat_cards") def render_help_card(lines: Iterable[str]) -> Path: resolved_lines = [str(line) for line in lines] return _render_card( key="help-v2-" + "|".join(resolved_lines), eyebrow="IMAGESTUDIO COMMANDS", title="影棚命令帮助", subtitle="使用 /影棚 或 /yp 调用", lines=resolved_lines, accent=(40, 169, 151), ) def render_status_card( *, state: str, version: str, database: str, api: str, details: Iterable[str] | None = None, ) -> Path: detail_lines = [str(line) for line in (details or []) if str(line).strip()] return _render_card( key=f"status-{state}-{version}-{database}-{api}-{'|'.join(detail_lines)}", eyebrow="IMAGESTUDIO STATUS", title=f"运行状态 · {state}", subtitle=f"版本 {version}", lines=[f"数据库 {database}", f"接口 {api}", *detail_lines], accent=(40, 169, 151) if state == "正常" else (224, 157, 55), ) def render_profile_card(*, name: str, base: str, appearance: str, style: str) -> Path: return _render_card( key=f"profile-{name}-{base}-{appearance}-{style}", eyebrow="IMAGESTUDIO QUICK PROFILE", title=name or "快速角色草稿", subtitle="确认后将创建角色档案与工作流", lines=[f"基础 {base or '—'}", f"外观 {appearance or '—'}", f"风格 {style or '—'}", "回复:确认 / 修改 / 放弃"], accent=(112, 91, 220), ) def _render_card(*, key: str, eyebrow: str, title: str, subtitle: str, lines: list[str], accent: tuple[int, int, int]) -> Path: from PIL import Image, ImageDraw, ImageFont CARD_DIR.mkdir(parents=True, exist_ok=True) target = CARD_DIR / f"{hashlib.sha256(key.encode('utf-8')).hexdigest()[:16]}.png" if target.exists(): return target width = 1000 height = 300 + max(len(lines), 1) * 66 image = Image.new("RGB", (width, height), (242, 247, 249)) draw = ImageDraw.Draw(image) draw.rounded_rectangle((28, 28, width - 28, height - 28), radius=34, fill=(255, 255, 255), outline=(211, 224, 228), width=2) draw.rounded_rectangle((28, 28, 52, height - 28), radius=12, fill=accent) fonts = _fonts(ImageFont) draw.text((86, 70), eyebrow, font=fonts["small"], fill=accent) draw.text((86, 112), title, font=fonts["title"], fill=(24, 36, 44)) draw.text((88, 180), subtitle, font=fonts["body"], fill=(101, 118, 128)) y = 245 for index, line in enumerate(lines): draw.rounded_rectangle((82, y, width - 82, y + 48), radius=12, fill=(244, 249, 250) if index % 2 == 0 else (248, 250, 252)) draw.text((102, y + 9), line, font=fonts["body"], fill=(43, 59, 68)) y += 66 image.save(target, format="PNG", optimize=True) return target def _fonts(image_font): candidates = [ Path("C:/Windows/Fonts/msyh.ttc"), Path("C:/Windows/Fonts/simhei.ttf"), Path("/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc"), Path("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf"), ] path = next((item for item in candidates if item.exists()), None) if path is None: return {"small": image_font.load_default(), "title": image_font.load_default(), "body": image_font.load_default()} return { "small": image_font.truetype(str(path), 20), "title": image_font.truetype(str(path), 42), "body": image_font.truetype(str(path), 25), }