Font examples

Make monospaced font

# convert to monospace font
from robofab.interface.all.dialogs import AskString

value = AskString("Monospace width:")

try:
    value = int(value)
except ValueError:
    value = None

if value:
    font = CurrentFont()
    for glyph in font:
        glyph.width = value

Open font in layer

"""
Import a font into a layer of the CurrentFont
"""

## open a font without UI
f = OpenFont(showUI=False)

## get the current font
cf = CurrentFont()

## create a layer name based on the familyName and styleName of the opend font
layerName = "%s_%s" %(f.info.familyName, f.info.styleName)

## loop over all glyphs in the font
for g in f:
    ## if the glyph doenst exist in the current font, create a new glyph
    if g.name not in cf:
        cf.newGlyph(g.name)
    ## get the layered glyph
    layerGLyph = cf[g.name].getLayer(layerName)
    
    ## get the point pen of the layered glyph
    pen = layerGLyph.getPointPen()
    ## draw the points of the imported glyph into the layerd glyph
    g.drawPoints(pen)

## we are done :)
print "done"
    

Clean glyph names with quotes in groups

FontLab uses the single quote character ' to indicate key glyphs in spacing groups. In RoboFont/UFO the concept of key glyphs does not exist. The script below cleans names of key glyphs in groups created with FontLab.

for f in AllFonts():
    hasEdits = False
    for name, members in f.groups.items():
        groupHasEdits = False
        new = []
        for m in members:
            if m[-1] == "'":
                groupHasEdits = True
                hasEdits = True
                new.append(m[:-1])
            else:
                new.append(m)
        f.groups[name]=new
        
    if hasEdits:
        print "edits made in ", f.info.fullName
        f.save()
    else:
        print "no edits made", f.info.fullName