#!/usr/bin/env python3
"""
Generate single-slide DMark FB post — 2026-08-07
Article: StudioSoft: Strategi Digital Marketing Data-Driven untuk Pertumbuhan Brand
Template: DMark BG Orange 1080x1350, bg #F97316, white Poppins, 65px margin, safe zone 20px
"""
from PIL import Image, ImageDraw, ImageFont
import os

W, H = 1080, 1350
BG = "#F97316"
FG = "#FFFFFF"
MARGIN = 65
SAFE = 20

FP = {
    "Bold": "/usr/share/fonts/truetype/poppins/Poppins-Bold.ttf",
    "SemiBold": "/usr/share/fonts/truetype/poppins/Poppins-SemiBold.ttf",
    "Regular": "/usr/share/fonts/truetype/poppins/Poppins-Regular.ttf",
    "Light": "/usr/share/fonts/truetype/poppins/Poppins-Light.ttf",
}
def load_font(size, w="Bold"):
    try:
        return ImageFont.truetype(FP[w], size)
    except Exception:
        return ImageFont.load_default()

img = Image.new("RGB", (W, H), BG)
d = ImageDraw.Draw(img)

f_hdr = load_font(24, "Bold")
f_page = load_font(22, "Regular")
f_ftr = load_font(20, "Regular")
f_title = load_font(72, "Bold")
f_sub = load_font(36, "Regular")
f_body = load_font(30, "Regular")

LS, RS = SAFE, W - SAFE
TS, BS = SAFE, H - SAFE
CL = MARGIN

# Header
d.text((LS, TS), "DMARK", font=f_hdr, fill=FG)
pb = d.textbbox((0,0), "1/1", font=f_page)
d.text((RS-(pb[2]-pb[0]), TS), "1/1", font=f_page, fill=FG)

# Footer
d.text((LS, BS-20), "www.dmark.id", font=f_ftr, fill=FG)
ht = "#Branding #DigitalMarketing"
hb = d.textbbox((0,0), ht, font=f_ftr)
d.text((RS-(hb[2]-hb[0]), BS-20), ht, font=f_ftr, fill=FG)

# Content — vertically centered
title_lines = ["Strategi Digital", "Marketing yang", "Terukur dan Nyata"]
sub = "Berbasis data, bukan sekadar tren"
body_lines = [
    "Banyak agency menjanjikan hasil besar,",
    "hanya sedikit yang membuktikannya terukur.",
    "",
    "StudioSoft merancang strategi berdasarkan",
    "analisis mendalam: riset kata kunci, optimasi",
    "konten, sampai alur konversi website —",
    "semua disusun sebagai satu kesatuan agar",
    "bisnis tampil di halaman depan Google dan",
    "bertumbuh berkelanjutan.",
]
title_lh = d.textbbox((0,0), "Ay", font=f_title)[3]
sub_lh = d.textbbox((0,0), "Ay", font=f_sub)[3]
body_lh = d.textbbox((0,0), "Ay", font=f_body)[3]

g1, g2, g3 = 36, 40, 26
title_h = len(title_lines)*int(title_lh*1.16)
div_h = 24
body_h = len(body_lines)*int(body_lh*1.3)
total = title_h + g1 + div_h + g2 + sub_lh + g3 + body_h

y = MARGIN + ((H-2*MARGIN) - total)//2

for ln in title_lines:
    d.text((CL, y), ln, font=f_title, fill=FG)
    y += int(title_lh*1.16)
y += g1

dash, x, end = 30, CL, CL+300
while x < end:
    d.line([(x, y),(x+dash, y)], fill=FG, width=4)
    x += dash+20
y += div_h + g2

d.text((CL, y), sub, font=f_sub, fill=FG)
y += sub_lh + g3

for ln in body_lines:
    if ln:
        d.text((CL, y), ln, font=f_body, fill=FG)
    y += int(body_lh*1.3)

if y > BS:
    print("WARNING overflow", y, BS)

outdir = "/home/mantapman/.hermes/profiles/atun/workspace/dmark_fb_output"
os.makedirs(outdir, exist_ok=True)
out = os.path.join(outdir, "dmark_fb_20260807.png")
img.save(out)
print("SAVED:", out)
