# Transcript to carousel: sets eight slides (1080x1350, Inter) written from transcript.txt, checks that nothing overflows, counts words.
from PIL import Image, ImageDraw, ImageFont
import sys
FONT=sys.argv[1] if len(sys.argv)>1 else "Inter.ttf"
W,H,M=1080,1350,96; BW=W-2*M
def font(px,wg):
    f=ImageFont.truetype(FONT,px); ax=f.get_variation_axes(); f.set_variation_by_axes([wg if b"eight" in a["name"] else a["default"] for a in ax]); return f
def wrap(t,f,mw):
    out=[];cur=""
    for w in t.split():
        n=(cur+" "+w).strip()
        if f.getlength(n)<=mw: cur=n
        else: out.append(cur);cur=w
    if cur: out.append(cur)
    return out
SL=[("Is your kitchen knife dull?","A 10-second test and a 10-minute fix, from one stone."),
("The tomato test","Slice a ripe tomato without pressing down. If the skin wrinkles before the blade goes in, the knife is dull."),
("Skip the pull-through gadget","It works for a week and eats the edge. Buy one whetstone: 1000 grit on one side, 3000 on the other."),
("Set up the stone","Soak it for about ten minutes, until the bubbles stop. Put a wet towel under it so it cannot slide."),
("Find the angle with two coins","Aim for 15 to 20 degrees. Stack two coins under the spine, note the angle, remove them, lock your wrist."),
("Ten light strokes a side","Push edge first, as if shaving the stone. Pressure: the weight of the knife plus a little. Then five a side on 3000."),
("Feel for the burr","Run your thumb across the edge, never along it. A tiny lip of metal along the whole length means that side is done."),
("Steel weekly, stone every few months","A honing steel straightens the edge; it does not sharpen. Then run the tomato test again.")]
tw=len(open("transcript.txt").read().split())
log=["transcript to carousel: transcript.txt -> 8 slides, 1080x1350 px, Inter, headline 76 px bold, body 46 px regular, margin 96 px",
"control: a 300-character body with no spaces must overflow the 888 px box: "+("PASS" if font(46,400).getlength("x"*300)>BW else "FAIL"),
"transcript words: %d"%tw,"slide\theadline_chars\theadline_lines\tbody_words\tbody_lines\ttext_bottom_y\tfits"]
ims=[];tot=0
for i,(h,b) in enumerate(SL,1):
    im=Image.new("RGB",(W,H),(250,247,240)); d=ImageDraw.Draw(im)
    d.rectangle((0,0,W,18),fill=(200,70,40)); fh=font(76,800); fb=font(46,400); y=250
    hl=wrap(h,fh,BW); bl=wrap(b,fb,BW)
    for l in hl: d.text((M,y),l,font=fh,fill=(25,25,25)); y+=92
    y+=50
    for l in bl: d.text((M,y),l,font=fb,fill=(60,60,60)); y+=64
    d.text((M,H-130),"%d / %d"%(i,len(SL)),font=font(34,600),fill=(200,70,40)); d.text((W-M-470,H-130),"Example deck, invented talk",font=font(30,400),fill=(130,130,130))
    words=len(h.split())+len(b.split()); tot+=words
    log.append("%d\t%d\t%d\t%d\t%d\t%d\t%s"%(i,len(h),len(hl),len(b.split()),len(bl),y,"yes" if y<H-180 and len(hl)<=2 else "NO"))
    im.save("slide-%d.png"%i); ims.append(im)
log.append("words on all slides: %d = %.1f%% of the transcript's %d words"%(tot,tot/tw*100,tw))
log.append("longest body: %d words; shortest: %d words"%(max(len(b.split()) for _,b in SL),min(len(b.split()) for _,b in SL)))
sheet=Image.new("RGB",(4*540+5*30,2*675+3*30),(225,225,225))
for i,im in enumerate(ims): sheet.paste(im.resize((540,675),Image.LANCZOS),(30+(i%4)*570,30+(i//4)*705))
sheet.save("contact-sheet.png")
open("carousel.log","w").write("\n".join(log)+"\n"); print("\n".join(log))
