rpg-westreiche/tools/fontpicker.py

56 lines
1.8 KiB
Python
Raw Normal View History

"""Findet das Verzeichnis mit den DejaVu-Schriften – Linux (Desktop) wie macOS.
Reihenfolge:
1. Umgebungsvariable ``WESTREICHE_FONT_DIR`` (manueller Override),
2. übliche Installationspfade (Linux-Distributionen, Homebrew-Cask auf macOS),
3. Rückfall: Suche per ``fc-list``/``mdfind``-freier Kandidatenliste im Home-Verzeichnis.
Die PDF-Skripte registrieren ihre Schriften damit unabhängig vom Betriebssystem.
"""
from __future__ import annotations
import os
from pathlib import Path
CANDIDATES = (
"/usr/share/fonts/truetype/dejavu", # Debian/Ubuntu
"/usr/share/fonts/dejavu", # Fedora/Arch
"/usr/share/fonts/TTF", # Arch
"/usr/local/share/fonts/dejavu",
"/opt/homebrew/share/fonts", # Homebrew (macOS, arm64)
"/usr/local/share/fonts", # Homebrew (macOS, intel)
"~/Library/Fonts", # macOS: `brew install --cask font-dejavu`
"/Library/Fonts",
"/System/Library/Fonts/Supplemental",
)
NEEDED = ("DejaVuSerif.ttf", "DejaVuSans.ttf")
HINT = (
"DejaVu-Schriften nicht gefunden. Installation:\n"
" macOS: brew install --cask font-dejavu\n"
" Debian: sudo apt install fonts-dejavu\n"
" oder: WESTREICHE_FONT_DIR=/pfad/zu/den/ttf exportieren"
)
def _enough(path: Path) -> bool:
return all((path / name).is_file() for name in NEEDED)
def font_dir() -> Path:
env = os.environ.get("WESTREICHE_FONT_DIR")
if env:
candidate = Path(env).expanduser()
if _enough(candidate):
return candidate
raise SystemExit(f"WESTREICHE_FONT_DIR={env} enthält keine DejaVu-Schriften.\n{HINT}")
for raw in CANDIDATES:
candidate = Path(raw).expanduser()
if _enough(candidate):
return candidate
raise SystemExit(HINT)