from PIL import Image, ImageDraw, ImageFont

F = '/usr/share/fonts/truetype/wlg/Montserrat.ttf'
def f(s, n='Bold'):
    ft = ImageFont.truetype(F, s)
    try: ft.set_variation_by_name(n)
    except Exception: pass
    return ft

TEAL=(0,81,81); AMBER=(243,174,61); SKY=(175,214,230); WHITE=(255,255,255)
W,H=1200,1500; M=100; RW=W-2*M; LAB=400

def wrap(d, text, font, maxw):
    lines, cur = [], ''
    for w in text.split():
        t = (cur+' '+w).strip()
        if d.textlength(t, font=font) <= maxw: cur = t
        else:
            if cur: lines.append(cur)
            cur = w
    if cur: lines.append(cur)
    return lines

def block(d, y, text, font, fill, lh, maxw=RW, x=M):
    for ln in wrap(d, text, font, maxw):
        d.text((x, y), ln, font=font, fill=fill); y += lh
    return y

C = dict(eyebrow='CASE',
   who='A global data centre operator. R&D hub, Warsaw.',
   rows=[('68%','of the roles closed,','against a plan of 150 hires'),
         ('0','of our hires left in their','first eighteen months')],
   body='They were opening an R&D hub in Warsaw and needed 150 IT specialists inside eighteen months. We closed sixty eight per cent of the roles. Then the part that mattered more: not one of the people we placed walked away in their first eighteen months. The contract was extended to cover hiring for the data centres themselves.',
   kicker='Filling the role is the easy half.')

def render(out, corner, cfont, ccol):
    im = Image.new('RGB',(W,H),TEAL); d = ImageDraw.Draw(im)
    d.rectangle([0,0,W,10], fill=AMBER)
    d.text((M,78), 'WLG', font=f(44,'Bold'), fill=AMBER)
    if corner:
        wpx = d.textlength(corner, font=cfont)
        d.text((W-M-wpx, 92), corner, font=cfont, fill=ccol)
    d.text((M,178), '  '.join(list(C['eyebrow'])), font=f(22,'SemiBold'), fill=SKY)
    block(d, 226, C['who'], f(38,'SemiBold'), WHITE, 52)
    y = 430
    for big, l1, l2 in C['rows']:
        d.text((M,y), big, font=f(96,'Bold'), fill=AMBER)
        d.text((LAB,y+12), l1, font=f(30,'Medium'), fill=WHITE)
        d.text((LAB,y+54), l2, font=f(30,'Medium'), fill=SKY)
        y += 170
    block(d, y+50, C['body'], f(30,'Regular'), WHITE, 46)
    kf = f(38,'SemiBold'); kl = wrap(d, C['kicker'], kf, RW); ky = 1352-50*len(kl)
    d.rectangle([M,ky-54,M+140,ky-48], fill=AMBER)
    block(d, ky, C['kicker'], kf, WHITE, 50)
    d.text((M,1418), 'Senior and director level technology hiring.  Poland and the Netherlands.',
           font=f(23,'Medium'), fill=SKY)
    im.save('/workspace/pub/'+out,'PNG'); print(out)

render('corner_a.png', '01 / 07', f(26,'Medium'), SKY)
render('corner_b.png', '  '.join(list('RETENTION')), f(22,'SemiBold'), AMBER)
