lib/fontcustom/scripts/generate.py in fontcustom-0.1.4 vs lib/fontcustom/scripts/generate.py in fontcustom-1.0.0.pre

- old
+ new

@@ -1,10 +1,11 @@ import fontforge import os import md5 -import json import subprocess +import tempfile +import json try: import argparse parser = argparse.ArgumentParser(description='Convert a directory of svg and eps files into a unified font file.') parser.add_argument('dir', metavar='directory', type=unicode, nargs=2, help='directory of vector files') @@ -25,10 +26,14 @@ indir = posargs[0] outdir = posargs[1] f = fontforge.font() f.encoding = 'UnicodeFull' +f.design_size = 16 +f.em = 512 +f.ascent = 448 +f.descent = 64 m = md5.new() cp = 0xf100 files = [] @@ -42,31 +47,38 @@ if ext in ['.svg', '.eps']: if ext in ['.svg']: # hack removal of <switch> </switch> tags svgfile = open(filePath, 'r+') + tmpsvgfile = tempfile.NamedTemporaryFile(suffix=ext, delete=False) svgtext = svgfile.read() svgfile.seek(0) # replace the <switch> </switch> tags with 'nothing' svgtext = svgtext.replace('<switch>', '') svgtext = svgtext.replace('</switch>', '') - # remove all contents of file so that we can write out the new contents - svgfile.truncate() - svgfile.write(svgtext) + tmpsvgfile.file.write(svgtext) svgfile.close() + tmpsvgfile.file.close() + + filePath = tmpsvgfile.name # end hack m.update(filename + str(size) + ';') glyph = f.createChar(cp) glyph.importOutlines(filePath) - glyph.left_side_bearing = KERNING - glyph.right_side_bearing = KERNING + # if we created a temporary file, let's clean it up + if tmpsvgfile: + os.unlink(tmpsvgfile.name) + # glyph.left_side_bearing = KERNING + # glyph.right_side_bearing = KERNING + glyph.width = 512 + # possible optimization? # glyph.simplify() # glyph.round() files.append(name) @@ -105,5 +117,9 @@ subprocess.call('python ' + scriptPath + '/eotlitetool.py ' + fontfile + '.ttf -o ' + fontfile + '.eot', shell=True) subprocess.call('mv ' + fontfile + '.eotlite ' + fontfile + '.eot', shell=True) # Hint the TTF file subprocess.call('ttfautohint -s -n ' + fontfile + '.ttf ' + fontfile + '-hinted.ttf > /dev/null 2>&1 && mv ' + fontfile + '-hinted.ttf ' + fontfile + '.ttf', shell=True) + +# Describe output in JSON +outname = os.path.basename(fontfile) +print json.dumps({'fonts': [outname + '.ttf', outname + '.woff', outname + '.eot', outname + '.svg'], 'glyphs': files, 'file_name': outname})