Merge pull request #1249 from anthrotype/varlib-finder

[varLib] add a --master-finder command line option
This commit is contained in:
Cosimo Lupo 2018-04-25 16:14:04 +01:00 committed by GitHub
commit 55539f6651
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 85 additions and 7 deletions

View File

@ -777,27 +777,84 @@ def build(designspace_filename, master_finder=lambda s:s, exclude=[], optimize=T
return vf, model, master_ttfs
class MasterFinder(object):
def __init__(self, template):
self.template = template
def __call__(self, src_path):
fullname = os.path.abspath(src_path)
dirname, basename = os.path.split(fullname)
stem, ext = os.path.splitext(basename)
path = self.template.format(
fullname=fullname,
dirname=dirname,
basename=basename,
stem=stem,
ext=ext,
)
return os.path.normpath(path)
def main(args=None):
from argparse import ArgumentParser
from fontTools import configLogger
parser = ArgumentParser(prog='varLib')
parser.add_argument('designspace')
parser.add_argument('-o', metavar='OUTPUTFILE', dest='outfile', default=None, help='output file')
parser.add_argument('-x', metavar='TAG', dest='exclude', action='append', default=[], help='exclude table')
parser.add_argument('--disable-iup', dest='optimize', action='store_false', help='do not perform IUP optimization')
parser.add_argument(
'-o',
metavar='OUTPUTFILE',
dest='outfile',
default=None,
help='output file'
)
parser.add_argument(
'-x',
metavar='TAG',
dest='exclude',
action='append',
default=[],
help='exclude table'
)
parser.add_argument(
'--disable-iup',
dest='optimize',
action='store_false',
help='do not perform IUP optimization'
)
parser.add_argument(
'--master-finder',
default='master_ttf_interpolatable/{stem}.ttf',
help=(
'templated string used for finding binary font '
'files given the source file names defined in the '
'designspace document. The following special strings '
'are defined: {fullname} is the absolute source file '
'name; {basename} is the file name without its '
'directory; {stem} is the basename without the file '
'extension; {ext} is the source file extension; '
'{dirname} is the directory of the absolute file '
'name. The default value is "%(default)s".'
)
)
options = parser.parse_args(args)
# TODO: allow user to configure logging via command-line options
configLogger(level="INFO")
designspace_filename = options.designspace
finder = lambda s: s.replace('master_ufo', 'master_ttf_interpolatable').replace('.ufo', '.ttf')
finder = MasterFinder(options.master_finder)
outfile = options.outfile
if outfile is None:
outfile = os.path.splitext(designspace_filename)[0] + '-VF.ttf'
vf, model, master_ttfs = build(designspace_filename, finder, exclude=options.exclude, optimize=options.optimize)
vf, model, master_ttfs = build(
designspace_filename,
finder,
exclude=options.exclude,
optimize=options.optimize
)
log.info("Saving variation font %s", outfile)
vf.save(outfile)

View File

@ -190,7 +190,6 @@ class BuildTest(unittest.TestCase):
"""
suffix = '.ttf'
ds_path = self.get_test_input('Build.designspace')
ufo_dir = self.get_test_input('master_ufo')
ttx_dir = self.get_test_input('master_ttx_interpolatable_ttf')
self.temp_dir()
@ -202,9 +201,31 @@ class BuildTest(unittest.TestCase):
ds_copy = os.path.join(self.tempdir, 'BuildMain.designspace')
shutil.copy2(ds_path, ds_copy)
varLib_main([ds_copy])
# by default, varLib.main finds master TTFs inside a
# 'master_ttf_interpolatable' subfolder in current working dir
cwd = os.getcwd()
os.chdir(self.tempdir)
try:
varLib_main([ds_copy])
finally:
os.chdir(cwd)
varfont_path = os.path.splitext(ds_copy)[0] + '-VF' + suffix
self.assertTrue(os.path.exists(varfont_path))
# try again passing an explicit --master-finder
os.remove(varfont_path)
finder = "%s/master_ttf_interpolatable/{stem}.ttf" % self.tempdir
varLib_main([ds_copy, "--master-finder", finder])
self.assertTrue(os.path.exists(varfont_path))
# and also with explicit -o output option
os.remove(varfont_path)
varfont_path = os.path.splitext(varfont_path)[0] + "-o" + suffix
varLib_main([ds_copy, "-o", varfont_path, "--master-finder", finder])
self.assertTrue(os.path.exists(varfont_path))
varfont = TTFont(varfont_path)
tables = [table_tag for table_tag in varfont.keys() if table_tag != 'head']
expected_ttx_path = self.get_test_output('BuildMain.ttx')