"""Builds the Surch logo (SVG) from Poppins Bold glyph outlines.
Mark: the "c" of surch is a magnifying glass (search) wearing a graduation cap
(learning and growth): search your next step."""
from fontTools.ttLib import TTFont
from fontTools.pens.svgPathPen import SVGPathPen
from fontTools.pens.transformPen import TransformPen
import sys
F=TTFont('/usr/share/fonts/truetype/google-fonts/Poppins-Bold.ttf');gs=F.getGlyphSet();cmap=F.getBestCmap();upm=F['head'].unitsPerEm
SIZE=120;sc=SIZE/upm;BASE=150
def glyph(ch,x):
    g=gs[cmap[ord(ch)]];p=SVGPathPen(gs);g.draw(TransformPen(p,(sc,0,0,-sc,x,BASE)));return p.getCommands(),g.width*sc
def logo(ink,acc,cap,tassel):
    parts=[];x=14
    for ch in "sur":
        d,w=glyph(ch,x);parts.append(f'<path d="{d}" fill="{ink}"/>');x+=w-2
    # the "c": an open lens ring + handle (search)
    cx,cy,r=x+33,BASE-30,24
    parts.append(f'<path d="M{cx+r*0.72:.1f} {cy-r*0.69:.1f} A{r} {r} 0 1 0 {cx+r*0.72:.1f} {cy+r*0.69:.1f}" fill="none" stroke="{acc}" stroke-width="15" stroke-linecap="round"/>')
    parts.append(f'<path d="M{cx+r*0.78:.1f} {cy+r*0.78:.1f} L{cx+r*1.55:.1f} {cy+r*1.55:.1f}" stroke="{acc}" stroke-width="13" stroke-linecap="round"/>')
    # graduation cap resting on the lens (learning)
    tx,ty=cx-7,cy-r-15
    parts.append(f'<g transform="rotate(-12 {tx} {ty})"><path d="M{tx-34} {ty} L{tx} {ty-14} L{tx+34} {ty} L{tx} {ty+14} Z" fill="{cap}"/>'
                 f'<path d="M{tx-19} {ty+7} V{ty+19} Q{tx} {ty+30} {tx+19} {ty+19} V{ty+7} L{tx} {ty+14} Z" fill="{cap}"/>'
                 f'<path d="M{tx+26} {ty+3} V{ty+22}" stroke="{tassel}" stroke-width="4" stroke-linecap="round"/><circle cx="{tx+26}" cy="{ty+25}" r="5" fill="{tassel}"/></g>')
    x=cx+r+16
    d,w=glyph("h",x);parts.append(f'<path d="{d}" fill="{ink}"/>');x+=w
    parts.append(f'<circle cx="{x+12:.1f}" cy="{BASE-10}" r="11" fill="{acc}"/>')
    bx0,by0,bx1,by1=12,52,x+25,168          # drawing extents
    k=min(468/(bx1-bx0),188/(by1-by0));h=(by1-by0)*k
    tf=f'translate(2 {(192-h)/2:.1f}) scale({k:.4f}) translate({-bx0} {-by0})'
    return f'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 472 192" width="472" height="192"><g transform="{tf}">'+''.join(parts)+'</g></svg>'
open('surch-logo.svg','w').write(logo('#141414','#2e9cf0','#141414','#ffb27a'))
open('surch-logo-white.svg','w').write(logo('#ffffff','#7cc6f8','#ffffff','#ffb27a'))
# square app icon / favicon: the mark alone on the lavender pastel
mark=logo('#141414','#2e9cf0','#141414','#ffb27a')
print('ok')

def mark(acc,cap,tassel):
    """the lens + cap on its own, for small spaces (collapsed sidebar, favicon)"""
    cx,cy,r=44,58,24
    tx,ty=cx-7,cy-r-15
    return (f'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 96 96" width="96" height="96">'
      f'<path d="M{cx+r*0.72:.1f} {cy-r*0.69:.1f} A{r} {r} 0 1 0 {cx+r*0.72:.1f} {cy+r*0.69:.1f}" fill="none" stroke="{acc}" stroke-width="15" stroke-linecap="round"/>'
      f'<path d="M{cx+r*0.78:.1f} {cy+r*0.78:.1f} L{cx+r*1.45:.1f} {cy+r*1.45:.1f}" stroke="{acc}" stroke-width="13" stroke-linecap="round"/>'
      f'<g transform="rotate(-12 {tx} {ty})"><path d="M{tx-34} {ty} L{tx} {ty-14} L{tx+34} {ty} L{tx} {ty+14} Z" fill="{cap}"/>'
      f'<path d="M{tx-19} {ty+7} V{ty+19} Q{tx} {ty+30} {tx+19} {ty+19} V{ty+7} L{tx} {ty+14} Z" fill="{cap}"/>'
      f'<path d="M{tx+26} {ty+3} V{ty+22}" stroke="{tassel}" stroke-width="4" stroke-linecap="round"/><circle cx="{tx+26}" cy="{ty+25}" r="5" fill="{tassel}"/></g></svg>')
open('surch-mark.svg','w').write(mark('#2e9cf0','#141414','#ffb27a'))
open('surch-mark-white.svg','w').write(mark('#7cc6f8','#ffffff','#ffb27a'))
