Skip to content

Instantly share code, notes, and snippets.

@LettError
Created December 4, 2023 11:19
Show Gist options
  • Save LettError/d1e306d0b3c7dec00cb3f9e171f31494 to your computer and use it in GitHub Desktop.
Save LettError/d1e306d0b3c7dec00cb3f9e171f31494 to your computer and use it in GitHub Desktop.
Drawbot output for responsive lettering UFO
#
# script for drawbot extension in robofont
# draws a fitted / interpolating image from a responsive lettering ufo
# [email protected] / 20231204
def ip(a, b, f):
return a + f * (b-a)
page = (659, 500) # image size
margin = 59 # nice to have a margin
f = CurrentFont()
nt = f["narrow-thin"].toMathGlyph()
wt = f["wide-thin"].toMathGlyph()
nb = f["narrow-bold"].toMathGlyph()
wb = f["wide-bold"].toMathGlyph()
# get the ratios
ntBounds = f["narrow-thin"].getLayer("bounds").bounds
wtBounds = f["wide-thin"].getLayer("bounds").bounds
nbBounds = f["narrow-bold"].getLayer("bounds").bounds
wbBounds = f["wide-bold"].getLayer("bounds").bounds
# get the bounds
xMin, yMin, xMax, yMax = ntBounds
narrowX = xMax-xMin
narrowY = yMax-yMin
xMin, yMin, xMax, yMax = wtBounds
wideX = xMax-xMin
wideY = yMax-yMin
f = CurrentFont()
names = ["narrow-thin", "wide-thin", "narrow-bold", "wide-bold"]
# what width and height are we aiming for
wantWidth = page[0]-2*margin
wantHeight = page[1]-2*margin
wantRatio = wantWidth / wantHeight
wantScale = wantHeight / narrowY
# have a responsive lettering ufo open as current font
# make sure we have the names we need for this to work
for n in names:
assert n in f
# this is how to find the widthFactor from the known parameters:
# ---
#wantRatio = (narrowX + widthFactor*(wideX-narrowX)) / narrowY
#wantRatio * narrowY = narrowX + widthFactor*(wideX-narrowX)
#(wantRatio * narrowY) - narrowX = widthFactor*(wideX-narrowX)
#((wantRatio * narrowY) - narrowX) / (wideX-narrowX) = widthFactor
# ---
widthFactor = ((wantRatio * narrowY) - narrowX) / (wideX-narrowX)
# these are the fitted thin and bold masters
w1 = ip(nt, wt, widthFactor)
w2 = ip(nb, wb, widthFactor)
weightFactor = 0.4
steps = 10
for s in range(steps+1):
newPage(*page)
weightFactor = s/steps
# draw the steps
with savedState():
fill(.4,1,0)
stroke(None)
translate(margin, margin)
scale(wantScale)
translate(0,-f.info.descender)
# this is how to calculate the weightfactor
w3 = ip(w1, w2, weightFactor)
drawGlyph(w3)
saveImage(f"{f.info.familyName}_{steps}.gif")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment