#!/usr/bin/env python3
"""Validasi safe zone & overflow untuk carousel hc05 (dan semua slide).
Rule: teks minimal 20px dari SEMUA tepi (kiri/kanan/atas/bawah), tidak overlap.
"""
from PIL import Image, ImageDraw, ImageFont
import os, glob

FONT_DIR = os.path.expanduser("~/.fonts")
DA = os.path.expanduser("~/.openclaw/workspace-atun/studiosoft_hc05_carousel")

def load(f, size, weight="Bold"):
    for w in ([weight] if weight != "Bold" else ["Bold", "SemiBold", "Regular"]):
        p = os.path.join(FONT_DIR, f"Poppins-{w}.ttf")
        if os.path.exists(p):
            return ImageFont.truetype(p, size)
    return ImageFont.truetype(os.path.join(FONT_DIR, "Poppins-Bold.ttf"), size)

def measure(img_path, fontsize_min=20, margin=20):
    img = Image.open(img_path).convert("RGB")
    W, H = img.size
    d = ImageDraw.Draw(img)
    issues = []
    # Reconstruct draws by checking bounding boxes at known anchor positions is complex;
    # Instead: check overall ink coverage near edges (heuristic).
    px = img.load()
    # sample border zones
    for x in range(W):
        for y in list(range(margin)) + list(range(H-margin, H)):
            r,g,b = px[x,y][:3]
            # near-black or near-white text on green bg
            if abs(r-4)<40 and abs(g-120)<40 and abs(b-87)<40:
                continue  # bg
            if r<60 and g<60 and b<60:
                issues.append(("edge-hit", (x,y)))
                break
    print(f"{os.path.basename(img_path)}: {img.size}  edge_issues={len(issues)}")
    return issues

for p in sorted(glob.glob(os.path.join(DA, "*.png"))):
    measure(p)
print("DONE")
