| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
from __future__ import absolute_import, print_function, unicode_literals |
| 6 |
import glob |
| 7 |
import os |
| 8 |
import re |
| 9 |
import sys |
| 10 |
import argparse |
| 11 |
import fontforge |
| 12 |
|
| 13 |
argparser = argparse.ArgumentParser(description= |
| 14 |
"Generate a font file out of a source. Best invoked from the Makefile.") |
| 15 |
argparser.add_argument( |
| 16 |
'input', |
| 17 |
help='Input font source, e.g. "/some/path/Bla.sfdir"') |
| 18 |
argparser.add_argument( |
| 19 |
'output', |
| 20 |
help='Output font file, e.g. "/some/path/Bla.otf"') |
| 21 |
argparser.add_argument( |
| 22 |
'version', |
| 23 |
help='Version string to embed into the font file, e.g. "v1.1"') |
| 24 |
argparser.add_argument( |
| 25 |
'--reloadgpos', action="store_true", |
| 26 |
help='Clean GPOS lookups and reimport ' |
| 27 |
'them from the feature files before generating ' |
| 28 |
'font files. For Developement.') |
| 29 |
argparser.add_argument( |
| 30 |
'--reloadgsub', action="store_true", |
| 31 |
help='Clean GSUB lookups and reimport ' |
| 32 |
'them from the feature files before generating ' |
| 33 |
'font files. For Developement.') |
| 34 |
args = argparser.parse_args() |
| 35 |
|
| 36 |
def reloadfeature(feature): |
| 37 |
"""Read and merge a certain feature file. Example: For |
| 38 |
EBGaramond12-Regular.sfdir, reloadfeature("GPOS") would merge |
| 39 |
featurefiles/12-Regular_GPOS.fea and reloadfeature("features") would merge |
| 40 |
featurefiles/12-Regular_features.fea (GSUB table).""" |
| 41 |
|
| 42 |
|
| 43 |
font_source_name = os.path.split(args.input)[1] |
| 44 |
inferred_style = re.search('\d+-\w+', font_source_name).group(0) |
| 45 |
featurepath = "featurefiles/" + inferred_style + "_%s.fea" |
| 46 |
featurefile = featurepath % feature |
| 47 |
|
| 48 |
if os.path.exists(featurefile): |
| 49 |
font.mergeFeature(featurefile) |
| 50 |
else: |
| 51 |
sys.stderr.write("Could not find file " + featurefile + |
| 52 |
", so one or more --reload*s is not doing anything.\n") |
| 53 |
|
| 54 |
font = fontforge.open(args.input) |
| 55 |
font.version = args.version |
| 56 |
font.encoding = 'UnicodeFull' |
| 57 |
font.selection.all() |
| 58 |
font.removeOverlap() |
| 59 |
font.autoHint() |
| 60 |
|
| 61 |
for svg in glob.glob(args.input + "/*svg"): |
| 62 |
letter = os.path.splitext(os.path.basename(svg))[0] |
| 63 |
font.createMappedChar(letter).importOutlines(svg) |
| 64 |
|
| 65 |
if args.reloadgpos: |
| 66 |
for lookup in font.gpos_lookups: |
| 67 |
font.removeLookup(lookup) |
| 68 |
reloadfeature("GPOS") |
| 69 |
|
| 70 |
if args.reloadgsub: |
| 71 |
for lookup in font.gsub_lookups: |
| 72 |
font.removeLookup(lookup) |
| 73 |
reloadfeature("features") |
| 74 |
|
| 75 |
extension = os.path.splitext(args.output)[1] |
| 76 |
if extension == '.ttf': |
| 77 |
font.correctReferences() |
| 78 |
|
| 79 |
|
| 80 |
font.round() |
| 81 |
|
| 82 |
font.generate(args.output) |