"""Finishing pass on the CFP PDF: the right logo, a calmer QR, three wording fixes.

Everything is measured off the current pages before anything is painted, and the QR work never
redraws modules by hand: page 1 reuses its own QR downscaled (same payload), page 2's template QR
is a freshly generated code whose payload was verified by decoding it back before it was placed.
"""

from PIL import Image, ImageDraw, ImageFont, ImageOps

GOLD = (216, 180, 106)
WHITE = (255, 255, 255)
GREY_LABEL = (143, 168, 181)          # the "Call opens" label grey, sampled

def sora(size, weight=700):
    f = ImageFont.truetype('Sora.ttf', size)
    f.set_variation_by_axes([weight])
    return f

def fraunces(size):
    f = ImageFont.truetype('Fraunces.ttf', size)
    f.set_variation_by_axes([28, 400, 0, 0])
    return f

logo = Image.open('meta-logo-512.png').convert('RGB')

# ================================================================ page 1
p1 = Image.open('patched-1.png').convert('RGB')
d1 = ImageDraw.Draw(p1)

# ---- logo: the YouTube badge sits on the photo, so it is covered, not erased. The official
# logo is a solid black square; rounded corners keep it reading as a badge on the photo.
box = (66, 56, 336, 326)              # the circle's bounding square, measured
size = box[2] - box[0]
lg = logo.resize((size, size), Image.LANCZOS)
mask = Image.new('L', (size, size), 0)
ImageDraw.Draw(mask).rounded_rectangle((0, 0, size - 1, size - 1), radius=26, fill=255)
p1.paste(lg, (box[0], box[1]), mask)

# ---- bottom strip: labels first. "Paper length" → "Contributed papers".
# label bbox measured: x1183, cap-top 2030, baseline ≈ 2058 (same row as the other labels)
navy = p1.getpixel((1183, 2140))
d1.rectangle((1178, 2018, 1400, 2068), fill=navy)
d1.text((1183, 2062), 'Contributed papers', font=sora(30, 700), fill=GREY_LABEL, anchor='ls')

# "Scopus" → "Scopus-indexed", white bold, right-aligned on the column's right edge (1725)
d1.rectangle((1440, 2072, 1732, 2126), fill=navy)
d1.text((1725, 2118), 'Scopus-indexed', font=sora(38, 700), fill=WHITE, anchor='rs')

# ---- QR: erase the 224px tile glued under Scopus, redraw at the page-2 size (190px tile),
# centred over the metaconferences.org caption, clear of the Proceedings block.
old = p1.crop((1416, 2103, 1640, 2327))            # keep the modules before erasing
d1.rectangle((1404, 2095, 1652, 2340), fill=navy)
tile = ImageOps.contain(old, (166, 166))            # same payload, smaller
qr_tile = Image.new('RGB', (190, 190), WHITE)
qr_tile.paste(tile, (12, 12))
# caption "metaconferences.org" spans ≈1413..1727 → centre 1570; sit between the info row
# and the caption line
p1.paste(qr_tile, (1570 - 95, 2140))

p1.save('final-1.png')
print('page 1 : logo, libellés, QR replacé')

# ================================================================ page 2
p2 = Image.open('patched-2.png').convert('RGB')
d2 = ImageDraw.Draw(p2)

# ---- logo in the header band: solid band behind, so erase then paste small official logo
band = p2.getpixel((400, 80))
d2.ellipse((52, 12, 196, 156), fill=band)           # the old round badge, generous
lg2 = logo.resize((116, 116), Image.LANCZOS)
mask2 = Image.new('L', (116, 116), 0)
ImageDraw.Draw(mask2).rounded_rectangle((0, 0, 115, 115), radius=14, fill=255)
p2.paste(lg2, (62, 26), mask2)

# ---- "Paper submission deadline" → "Submission deadline" (serif grey label, Key dates)
cream = p2.getpixel((800, 1010))
d2.rectangle((130, 1035, 560, 1082), fill=cream)
d2.text((133, 1075), 'Submission deadline', font=fraunces(34), fill=(90, 90, 88), anchor='ls')

# ---- the PAPER TEMPLATE QR now points at the templates anchor
new_qr = Image.open('qr-template.png').convert('RGB')
# the white tile of the right-hand QR: locate it precisely
px = p2.load()
xs = [x for x in range(1100, 1400) for y in range(2200, 2450) if px[x, y] == (255, 255, 255)]
ys = [y for y in range(2200, 2450) for x in range(1100, 1400) if px[x, y] == (255, 255, 255)]
tx0, ty0, tx1, ty1 = min(xs), min(ys), max(xs), max(ys)
tw = tx1 - tx0 + 1
inner = ImageOps.contain(new_qr, (tw - 24, tw - 24))
tile2 = Image.new('RGB', (tw, ty1 - ty0 + 1), WHITE)
tile2.paste(inner, ((tw - inner.width) // 2, (ty1 - ty0 + 1 - inner.height) // 2))
p2.paste(tile2, (tx0, ty0))
print('page 2 : logo, libellé, QR template %dpx à (%d,%d)' % (tw, tx0, ty0))

p2.save('final-2.png')

# ================================================================ PDF
p1.save('META2027_Call_for_Papers.pdf', save_all=True, append_images=[p2],
        resolution=216, quality=85)
import os
print('PDF : %d octets' % os.path.getsize('META2027_Call_for_Papers.pdf'))
