Skip to content

Instantly share code, notes, and snippets.

Created July 24, 2009 14:17
Show Gist options
  • Save anonymous/154281 to your computer and use it in GitHub Desktop.
Save anonymous/154281 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# svg2font
# (c) 2009 Jakub Steiner
# (c) 2009 Novell, Inc.
ICONS_DIR = 'moblin'
BLANK_FONT = 'template.sfd'
OUTPUT_FONT = 'moblin-icons.ttf'
letters = 'abcdefghijklmnopqsrtuvwxyzABCDEFGHIJKLMOPQRSTUVWXYZ1234567890!@#$%^&*()[]\\;\',./{}|:"<>?'
glyphs = {}
# right-o, here we go
import fontforge
import os
import re
# open a blank font template
# TODO: dynamically generate the space character
font = fontforge.open(BLANK_FONT)
folder = os.listdir(ICONS_DIR+"/scalable/devices/")
def doInDir(arg, path, files):
global i,letters,glyphs
for f in files:
if (re.search("svg$",f)):
icon = os.path.join(path,f)
glyphs[letters[i]] = icon
i += 1
i = 0
os.path.walk(ICONS_DIR, doInDir, "*.svg")
for glyph,icon in glyphs.iteritems():
font.createMappedChar(glyph)
font[glyph].importOutlines(icon)
#hacky spacing
font[glyph].left_side_bearing = 15
font[glyph].right_side_bearing = 15
font[glyph].correctDirection()
font[glyph].autoInstr() #ttf autohint .autoHint() for PS
#print "DEBUG:", font[glyph].boundingBox()
print "imported %s as <<%s>> (U:%s)" % (os.path.split(icon)[1], font[glyph].glyphname, font[glyph].unicode)
# create TTF
font.generate(OUTPUT_FONT)
print "generated",OUTPUT_FONT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment