#!/usr/bin/env bash
# 目視確認用に、pptx を PDF → PNG（4枚ずつのグリッド）にする。日本語フォントを入れた LibreOffice のプロファイルを使う。
set -euo pipefail
cd "$(dirname "$0")"
PROFILE=/tmp/asc_lo_profile
if [ ! -d "$PROFILE/user/fonts" ]; then
  mkdir -p "$PROFILE/user/fonts"
  cp "/System/Library/Fonts/ヒラギノ角ゴシック W3.ttc" "/System/Library/Fonts/ヒラギノ角ゴシック W6.ttc" "$PROFILE/user/fonts/"
  cp /System/Library/AssetsV2/com_apple_MobileAsset_Font8/*/AssetData/YuGothic-*.otf "$PROFILE/user/fonts/" 2>/dev/null || true
fi
mkdir -p qa
for f in "$@"; do
  name=$(basename "$f" .pptx)
  /Applications/LibreOffice.app/Contents/MacOS/soffice -env:UserInstallation=file://$PROFILE --headless --convert-to pdf --outdir qa "$f" >/dev/null 2>&1
  ../.venv/bin/python - "$name" <<'PY'
import sys, pymupdf
from PIL import Image
name = sys.argv[1]
d = pymupdf.open(f"qa/{name}.pdf")
ims = []
for p in d:
    pm = p.get_pixmap(dpi=80)
    ims.append(Image.frombytes("RGB", (pm.width, pm.height), pm.samples))
for g in range(0, len(ims), 4):
    part = ims[g:g + 4]; w, h = part[0].size
    rows = (len(part) + 1) // 2
    sheet = Image.new("RGB", (w * 2 + 10, h * rows + 10 * (rows - 1)), "white")
    for i, im in enumerate(part):
        sheet.paste(im, ((i % 2) * (w + 10), (i // 2) * (h + 10)))
    sheet.save(f"qa/{name}-{g // 4 + 1}.png")
print(f"{name}: {len(ims)} slides")
PY
done
