| 1 |
#!/usr/bin/fontforge -script |
| 2 |
# -*- mode: python; -*- |
| 3 |
# Taken from https://github.com/pteromys/svgs2ttf |
| 4 |
# Modified to run without argument to generate kid3material.ttf. |
| 5 |
|
| 6 |
import sys |
| 7 |
import os.path |
| 8 |
import json |
| 9 |
|
| 10 |
IMPORT_OPTIONS = ('removeoverlap', 'correctdir') |
| 11 |
|
| 12 |
try: |
| 13 |
unicode |
| 14 |
except NameError: |
| 15 |
unicode = str |
| 16 |
|
| 17 |
def loadConfig(filename='font.json'): |
| 18 |
with open(filename) as f: |
| 19 |
return json.load(f) |
| 20 |
|
| 21 |
def setProperties(font, config): |
| 22 |
props = config['props'] |
| 23 |
lang = props.pop('lang', 'English (US)') |
| 24 |
family = props.pop('family', None) |
| 25 |
style = props.pop('style', 'Regular') |
| 26 |
props['encoding'] = props.get('encoding', 'UnicodeFull') |
| 27 |
if family is not None: |
| 28 |
font.familyname = family |
| 29 |
font.fontname = family + '-' + style |
| 30 |
font.fullname = family + ' ' + style |
| 31 |
for k, v in config['props'].items(): |
| 32 |
if hasattr(font, k): |
| 33 |
if isinstance(v, list): |
| 34 |
v = tuple(v) |
| 35 |
setattr(font, k, v) |
| 36 |
else: |
| 37 |
font.appendSFNTName(lang, k, v) |
| 38 |
for t in config.get('sfnt_names', []): |
| 39 |
font.appendSFNTName(str(t[0]), str(t[1]), unicode(t[2])) |
| 40 |
|
| 41 |
def addGlyphs(font, config): |
| 42 |
for k, v in config['glyphs'].items(): |
| 43 |
g = font.createMappedChar(int(k, 0)) |
| 44 |
# Get outlines |
| 45 |
src = '%s.svg' % k |
| 46 |
if not isinstance(v, dict): |
| 47 |
v = {'src': v or src} |
| 48 |
src = '%s%s%s' % (config.get('input', '.'), os.path.sep, v.pop('src', src)) |
| 49 |
g.importOutlines(src, IMPORT_OPTIONS) |
| 50 |
g.removeOverlap() |
| 51 |
# Copy attributes |
| 52 |
for k2, v2 in v.items(): |
| 53 |
if hasattr(g, k2): |
| 54 |
if isinstance(v2, list): |
| 55 |
v2 = tuple(v2) |
| 56 |
setattr(g, k2, v2) |
| 57 |
|
| 58 |
def processConfig(config): |
| 59 |
font = fontforge.font() |
| 60 |
setProperties(font, config) |
| 61 |
addGlyphs(font, config) |
| 62 |
for outfile in config['output']: |
| 63 |
sys.stderr.write('Generating %s...\n' % outfile) |
| 64 |
font.generate(outfile) |
| 65 |
|
| 66 |
def main(config_file): |
| 67 |
config = loadConfig(config_file) |
| 68 |
os.chdir(os.path.dirname(config_file) or '.') |
| 69 |
processConfig(config) |
| 70 |
|
| 71 |
if __name__ == '__main__': |
| 72 |
if len(sys.argv) > 1: |
| 73 |
main(sys.argv[1]) |
| 74 |
else: |
| 75 |
sys.stdout.write('Generating font for kid3\n') |
| 76 |
config = { |
| 77 |
'output': ['kid3material.ttf'], |
| 78 |
'glyphs': {'0x3e': 'chevron_right.svg', '0x3d': 'menu.svg', |
| 79 |
'0x4d': 'modified.svg', '0x3c': 'chevron_left.svg', |
| 80 |
'0x5e': 'expand_less.svg', '0x53': 'select_all.svg', |
| 81 |
'0x2b': 'add.svg', '0x3a': 'more_vert.svg', |
| 82 |
'0x58': 'clear.svg', '0x2f': 'create.svg', |
| 83 |
'0x2d': 'remove.svg', '0x56': 'expand_more.svg'}, |
| 84 |
'props': {'em': 1024, 'ascent': 960, 'descent': 64, |
| 85 |
'family': 'kid3material'} |
| 86 |
} |
| 87 |
processConfig(config) |
| 88 |
#sys.stderr.write("\nUsage: %s something.json\n" % sys.argv[0] ) |
| 89 |
|
| 90 |
# vim: set filetype=python: |