""" hyperstitionstud_io.publishing.fonts — Cross-OS TrueType Font Resolver, `@lru_cache` Pillow Font Loader, and Shared Group 3 ("Stealth Basalt Plate & Micro-Pip Signal Ribbon") Pillow Drawing Primitives. Searches Debian/Ubuntu, Fedora/RHEL, Arch, Alpine, macOS, and user font directories so PDF and PNG rendering never crashes on OS-specific font paths. """ from __future__ import annotations import functools from pathlib import Path from typing import Any FONT_SEARCH_DIRS = [ Path("/usr/share/fonts/truetype/dejavu"), Path("/usr/share/fonts/dejavu-sans-fonts"), Path("/usr/share/fonts/dejavu-sans-mono-fonts"), Path("/usr/share/fonts/google-noto"), Path("/usr/share/fonts/TTF"), Path("/usr/share/fonts/dejavu"), Path.home() / ".local" / "share" / "fonts", Path.home() / "Library" / "Fonts", Path("/Library/Fonts"), Path("/System/Library/Fonts"), ] # Shared Group 3 Preferred Default Palette Tokens (90% Quiet Matte Basalt / 7% #38bdf8 Interactive / 3% Explicit Lock Beacons) GROUP3_PALETTE: dict[str, tuple[int, int, int]] = { "BG_VOID": (2, 6, 23), # #020617 "BG_WELL": (7, 13, 27), # #070d1b "BG_ODD": (12, 19, 36), # #0c1324 "BG_EVEN": (17, 26, 46), # #111a2e "BG_BEZEL": (24, 35, 56), # #182338 "BORDER_MUTED": (38, 52, 77), # #26344d "TXT_PRI": (226, 232, 240), # #e2e8f0 "TXT_SEC": (148, 163, 184), # #94a3b8 "TXT_DIM": (100, 116, 139), # #64748b "CYAN": (56, 189, 248), # #38bdf8 "GOLD": (245, 158, 11), # #f59e0b "EMERALD": (16, 185, 129), # #10b981 "ROSE": (244, 63, 94), # #f43f5e "VIOLET": (168, 85, 247), # #a855f7 } def chamfer_polygon_points( x0: int, y0: int, x1: int, y1: int, ctl: int = 10, ctr: int = 10, cbr: int = 4, cbl: int = 4, ) -> list[tuple[int, int]]: return [ (x0 + ctl, y0), (x1 - ctr, y0), (x1, y0 + ctr), (x1, y1 - cbr), (x1 - cbr, y1), (x0 + cbl, y1), (x0, y1 - cbl), (x0, y0 + ctl), ] def draw_group3_chamfered_plate( draw: Any, box: tuple[int, int, int, int], fill: tuple[int, int, int], attention_color: tuple[int, int, int] | None = None, pip_color: tuple[int, int, int] | None = None, ) -> None: x0, y0, x1, y1 = box out_col = attention_color if attention_color else GROUP3_PALETTE["BORDER_MUTED"] pts = chamfer_polygon_points(x0, y0, x1, y1, 10, 10, 4, 4) draw.polygon(pts, fill=fill, outline=out_col) band_h = min(y1 - y0, 26) for y in range(y0 + 3, y0 + band_h, 3): for x in range(x0 + 8 + (y % 2) * 3, x1 - 8, 6): draw.point((x, y), fill=(22, 33, 54)) draw.polygon( [(x0 + 10, y0 + 1), (x1 - 10, y0 + 1), (x1 - 7, y0 + 4), (x0 + 7, y0 + 4)], fill=GROUP3_PALETTE["BG_BEZEL"], ) pcol = attention_color or pip_color or (71, 85, 105) draw.rectangle((x0 + 14, y0 + 1, x0 + 48, y0 + 4), fill=pcol) def _reportlab_vera_fallback(filename: str) -> Path | None: try: import reportlab rl_fonts = Path(reportlab.__file__).resolve().parent / "fonts" low = filename.lower() if "bold" in low and ("oblique" in low or "italic" in low): cand = rl_fonts / "VeraBI.ttf" elif "bold" in low: cand = rl_fonts / "VeraBd.ttf" elif "oblique" in low or "italic" in low: cand = rl_fonts / "VeraIt.ttf" else: cand = rl_fonts / "Vera.ttf" if cand.is_file(): return cand except Exception: pass return None @functools.lru_cache(maxsize=64) def resolve_font_path(filename: str, fallback_filename: str | None = None) -> Path | None: cand_direct = Path(filename) if cand_direct.is_absolute() and cand_direct.is_file(): return cand_direct for d in FONT_SEARCH_DIRS: cand = d / filename if cand.is_file(): return cand sys_fonts = Path("/usr/share/fonts") if sys_fonts.is_dir(): for match in sys_fonts.rglob(filename): if match.is_file(): return match if fallback_filename: for d in FONT_SEARCH_DIRS: cand = d / fallback_filename if cand.is_file(): return cand if sys_fonts.is_dir(): for match in sys_fonts.rglob(fallback_filename): if match.is_file(): return match return _reportlab_vera_fallback(filename) @functools.lru_cache(maxsize=64) def load_pillow_font(filename: str, size: int) -> Any: from PIL import ImageFont p = resolve_font_path(filename, fallback_filename="DejaVuSans.ttf") if p is not None: return ImageFont.truetype(str(p), size) try: return ImageFont.load_default(size=size) except TypeError: return ImageFont.load_default()