"""Build the kmalloc logo set: bird traced from the Ideogram PNG, wordmark from Terminess (Terminus, SIL OFL)."""
import os,sys
import numpy as np, potrace
from PIL import Image
from fontTools.ttLib import TTFont
from fontTools.pens.svgPathPen import SVGPathPen
from fontTools.pens.transformPen import TransformPen
from fontTools.pens.boundsPen import BoundsPen
OUT=sys.argv[1]; os.makedirs(OUT,exist_ok=True)
SRC=os.path.expanduser("~/Downloads/high-level-description-a-flat-vector-log_DrBVA7_jUzqgIXurP--M_g_M3vw3_xUQ0qxWmHjABJb2Q_cover.png")
FONT=os.path.expanduser("~/.local/share/fonts/nerd-fonts/Terminus/TerminessNerdFont-Bold.ttf")
INK_L,INK_D,CREAM,DARK,RUST="#111417","#F9F7ED","#F9F7ED","#111417","#D9480F"
# ---- bird: trace from the PNG, rows below the branch are the old wordmark and are dropped
a=np.asarray(Image.open(SRC).convert("RGB")).astype(int)
lum=0.2126*a[...,0]+0.7152*a[...,1]+0.0722*a[...,2]
orange=((a[...,0]-a[...,2])>90)&(a[...,0]>140)
dark=(lum<120)&(~orange)
CUT=644
dark[CUT:]=False; orange[CUT:]=False
ys,xs=np.where(dark|orange); x0,x1,y0,y1=xs.min()-6,xs.max()+7,ys.min()-6,ys.max()+7
S=4
def prep(m):
im=Image.fromarray((m[y0:y1,x0:x1]*255).astype("uint8")).resize(((x1-x0)*S,(y1-y0)*S),Image.BICUBIC)
return np.asarray(im)>127
def trace(mask):
pl=potrace.Bitmap(~mask).trace(turdsize=30,turnpolicy=potrace.POTRACE_TURNPOLICY_MINORITY,alphamax=1.0,opticurve=True,opttolerance=0.5)
parts=[]
for c in pl:
p=c.start_point; d=[f"M{p.x/S:.2f},{p.y/S:.2f}"]
for sg in c.segments:
if sg.is_corner: d.append(f"L{sg.c.x/S:.2f},{sg.c.y/S:.2f}L{sg.end_point.x/S:.2f},{sg.end_point.y/S:.2f}")
else: d.append(f"C{sg.c1.x/S:.2f},{sg.c1.y/S:.2f} {sg.c2.x/S:.2f},{sg.c2.y/S:.2f} {sg.end_point.x/S:.2f},{sg.end_point.y/S:.2f}")
d.append("Z"); parts.append("".join(d))
return "".join(parts)
bird=trace(prep(dark)); thorn=trace(prep(orange))
W=x1-x0; MH=y1-y0
# ---- wordmark from Terminess Bold outlines
f=TTFont(FONT); gs=f.getGlyphSet(); cm=f.getBestCmap()
def word_paths(text,scale,tx,ty):
d=[]; x=0
for ch in text:
g=cm[ord(ch)]; pen=SVGPathPen(gs,ntos=lambda v:f"{v:.2f}")
gs[g].draw(TransformPen(pen,(scale,0,0,-scale,tx+x*scale,ty))); d.append(pen.getCommands()); x+=f["hmtx"][g][0]
return "".join(d)
bp=BoundsPen(gs); x=0
xmin=1e9;xmax=-1e9;ymin=1e9;ymax=-1e9
for ch in "kmalloc":
g=cm[ord(ch)]; b=BoundsPen(gs); gs[g].draw(b); bx0,by0,bx1,by1=b.bounds
xmin=min(xmin,x+bx0); xmax=max(xmax,x+bx1); ymin=min(ymin,by0); ymax=max(ymax,by1); x+=f["hmtx"][g][0]
GAP=26
scale=(W-8)/(xmax-xmin)
tx=(W-(xmax-xmin)*scale)/2-xmin*scale
top=MH+GAP; ty=top+ymax*scale
word=word_paths("kmalloc",scale,tx,ty)
H=round(top+(ymax-ymin)*scale+8)
print("bird",W,MH,"| total",W,H,"| font scale",round(scale,4),"| text height",round((ymax-ymin)*scale,1))
def svg(ink_c,vb,bg=None,css=False,with_word=True):
X,Y,w,h=vb
r=f'' if bg else ""
st=f'' if css else ""
cls=' class="i"' if css else f' fill="{ink_c}"'
d=bird+(word if with_word else "")
return (f'')
full=(0,0,W,H); mark=(0,0,W,MH)
files={"logo-light.svg":svg(INK_L,full),"logo-dark.svg":svg(INK_D,full),"logo-auto.svg":svg(None,full,css=True),
"mark-light.svg":svg(INK_L,mark,with_word=False),"mark-dark.svg":svg(INK_D,mark,with_word=False),"mark-auto.svg":svg(None,mark,css=True,with_word=False)}
pad=40; s=max(W,MH)+2*pad; ox=(s-W)/2; oy=(s-MH)/2
def icon(ink_c,bg):
return (f'')
files["icon-light.svg"]=icon(INK_L,CREAM); files["icon-dark.svg"]=icon(INK_D,DARK)
files["favicon-auto.svg"]=(f'')
for n,c in files.items(): open(os.path.join(OUT,n),"w").write(c)
print(sorted(files))