diff --git a/scripts/font/generate_font.py b/scripts/font/generate_font.py index 6697951d..519b7ccc 100755 --- a/scripts/font/generate_font.py +++ b/scripts/font/generate_font.py @@ -10,12 +10,12 @@ import tempfile import json import copy -SCRIPT_PATH = os.path.dirname(os.path.abspath(__file__)) -INPUT_SVG_DIR = os.path.join(SCRIPT_PATH, '..', '..', 'src') -OUTPUT_FONT_DIR = os.path.join(SCRIPT_PATH, '..', '..', 'dist', 'fonts') +SCRIPTS_FONT_DIR = os.path.dirname(os.path.abspath(__file__)) +INPUT_SVG_DIR = os.path.join(SCRIPTS_FONT_DIR, '..', '..', 'src', 'svg') +OUTPUT_FONT_DIR = os.path.join(SCRIPTS_FONT_DIR, '..', '..', 'dist', 'fonts') OUTPUT_IONICONS_FONT_DIR = os.path.join(OUTPUT_FONT_DIR, 'ionicons') -MANIFEST_PATH = os.path.join(SCRIPT_PATH, '..', 'manifest.json') -BUILD_DATA_PATH = os.path.join(SCRIPT_PATH, '..', 'build_data.json') +MANIFEST_PATH = os.path.join(SCRIPTS_FONT_DIR, '..', 'manifest.json') +BUILD_DATA_PATH = os.path.join(SCRIPTS_FONT_DIR, '..', 'build_data.json') AUTO_WIDTH = True KERNING = 15 @@ -160,59 +160,55 @@ for dirname, dirnames, filenames in os.walk(INPUT_SVG_DIR): build_hash = m.hexdigest() -if build_hash == manifest_data.get('build_hash'): - print "Source files unchanged, did not rebuild fonts" +manifest_data['build_hash'] = build_hash -else: - manifest_data['build_hash'] = build_hash +f.fontname = font_name +f.familyname = font_name +f.fullname = font_name - f.fontname = font_name - f.familyname = font_name - f.fullname = font_name +print 'Generate TTF Font File' +f.generate(fontfile + '.ttf') - print 'Generate TTF Font File' - f.generate(fontfile + '.ttf') +print 'Generate SVG Font File' +f.generate(fontfile + '.svg') - print 'Generate SVG Font File' - f.generate(fontfile + '.svg') +# Fix SVG header for webkit +# from: https://github.com/fontello/font-builder/blob/master/bin/fontconvert.py +svgfile = open(fontfile + '.svg', 'r+') +svgtext = svgfile.read() +svgfile.seek(0) +svgfile.write(svgtext.replace('''''', '''''')) +svgfile.close() - # Fix SVG header for webkit - # from: https://github.com/fontello/font-builder/blob/master/bin/fontconvert.py - svgfile = open(fontfile + '.svg', 'r+') - svgtext = svgfile.read() - svgfile.seek(0) - svgfile.write(svgtext.replace('''''', '''''')) - svgfile.close() +scriptPath = os.path.dirname(os.path.realpath(__file__)) +try: + subprocess.Popen([scriptPath + '/sfnt2woff', fontfile + '.ttf'], stdout=subprocess.PIPE) +except OSError: + # If the local version of sfnt2woff fails (i.e., on Linux), try to use the + # global version. This allows us to avoid forcing OS X users to compile + # sfnt2woff from source, simplifying install. + subprocess.call(['sfnt2woff', fontfile + '.ttf']) - scriptPath = os.path.dirname(os.path.realpath(__file__)) - try: - subprocess.Popen([scriptPath + '/sfnt2woff', fontfile + '.ttf'], stdout=subprocess.PIPE) - except OSError: - # If the local version of sfnt2woff fails (i.e., on Linux), try to use the - # global version. This allows us to avoid forcing OS X users to compile - # sfnt2woff from source, simplifying install. - subprocess.call(['sfnt2woff', fontfile + '.ttf']) +# eotlitetool.py script to generate IE7-compatible .eot fonts +print 'Generate EOT Font File' +subprocess.call('python ' + scriptPath + '/eotlitetool.py ' + fontfile + '.ttf -o ' + fontfile + '.eot', shell=True) +subprocess.call('mv ' + fontfile + '.eotlite ' + fontfile + '.eot', shell=True) - # eotlitetool.py script to generate IE7-compatible .eot fonts - print 'Generate EOT Font File' - subprocess.call('python ' + scriptPath + '/eotlitetool.py ' + fontfile + '.ttf -o ' + fontfile + '.eot', shell=True) - subprocess.call('mv ' + fontfile + '.eotlite ' + fontfile + '.eot', shell=True) +print 'Hint TTF file' +subprocess.call('ttfautohint -s -f -n ' + fontfile + '.ttf ' + fontfile + '-hinted.ttf > /dev/null 2>&1 && mv ' + fontfile + '-hinted.ttf ' + fontfile + '.ttf', shell=True) - print 'Hint TTF file' - subprocess.call('ttfautohint -s -f -n ' + fontfile + '.ttf ' + fontfile + '-hinted.ttf > /dev/null 2>&1 && mv ' + fontfile + '-hinted.ttf ' + fontfile + '.ttf', shell=True) +print 'Generate WOFF2 Font File' +subprocess.call('woff2_compress ' + fontfile + '.ttf', shell=True) - print 'Generate WOFF2 Font File' - subprocess.call('woff2_compress ' + fontfile + '.ttf', shell=True) +manifest_data['icons'] = sorted(manifest_data['icons'], key=lambda k: k['name']) +build_data['icons'] = sorted(build_data['icons'], key=lambda k: k['name']) - manifest_data['icons'] = sorted(manifest_data['icons'], key=lambda k: k['name']) - build_data['icons'] = sorted(build_data['icons'], key=lambda k: k['name']) +print "Save Manifest, Icons: %s" % ( len(manifest_data['icons']) ) +f = open(MANIFEST_PATH, 'w') +f.write( json.dumps(manifest_data, indent=2, separators=(',', ': ')) ) +f.close() - print "Save Manifest, Icons: %s" % ( len(manifest_data['icons']) ) - f = open(MANIFEST_PATH, 'w') - f.write( json.dumps(manifest_data, indent=2, separators=(',', ': ')) ) - f.close() - - print "Save Build, Icons: %s" % ( len(build_data['icons']) ) - f = open(BUILD_DATA_PATH, 'w') - f.write( json.dumps(build_data, indent=2, separators=(',', ': ')) ) - f.close() +print "Save Build, Icons: %s" % ( len(build_data['icons']) ) +f = open(BUILD_DATA_PATH, 'w') +f.write( json.dumps(build_data, indent=2, separators=(',', ': ')) ) +f.close()