[mutator] Add instantiateVariableFont public function

Towards addressing https://github.com/fonttools/fonttools/issues/1070

The new instantiateVariableFont function takes a variable TTFont and a
dictionary of axes locations and returns a static TTFont instance.

The current code modifies the varfont in-place. To adapt it for
generating multiple instances from the same varfont, for now I simply
resorted to making a copy (can be optionally disabled by setting
inplace=True).

Also, replaced print() with log.info().
This commit is contained in:
Cosimo Lupo 2017-10-15 17:43:06 +02:00
parent 67f0dbdc31
commit ca7c35900b

View File

@ -10,6 +10,10 @@ from fontTools.ttLib.tables._g_l_y_f import GlyphCoordinates
from fontTools.varLib import _GetCoordinates, _SetCoordinates from fontTools.varLib import _GetCoordinates, _SetCoordinates
from fontTools.varLib.models import supportScalar, normalizeLocation from fontTools.varLib.models import supportScalar, normalizeLocation
import os.path import os.path
import logging
log = logging.getLogger("fontTools.varlib.mutator")
def _iup_segment(coords, rc1, rd1, rc2, rd2): def _iup_segment(coords, rc1, rd1, rc2, rd2):
@ -99,33 +103,30 @@ def _iup_delta(delta, coords, ends):
return out return out
def main(args=None): def instantiateVariableFont(varfont, location, inplace=False):
""" Generate a static instance from a variable TTFont and a dictionary
defining the desired location along the variable font's axes.
The location values must be specified as user-space coordinates, e.g.:
if args is None: {'wght': 400, 'wdth': 100}
import sys
args = sys.argv[1:]
varfilename = args[0] By default, a new TTFont object is returned. If ``inplace`` is True, the
locargs = args[1:] input varfont is modified and reduced to a static font.
outfile = os.path.splitext(varfilename)[0] + '-instance.ttf' """
if not inplace:
loc = {} # make a copy to leave input varfont unmodified
for arg in locargs: stream = BytesIO()
tag,val = arg.split('=') varfont.save(stream)
assert len(tag) <= 4 stream.seek(0)
loc[tag.ljust(4)] = float(val) varfont = TTFont(stream)
print("Location:", loc)
print("Loading variable font")
varfont = TTFont(varfilename)
fvar = varfont['fvar'] fvar = varfont['fvar']
axes = {a.axisTag:(a.minValue,a.defaultValue,a.maxValue) for a in fvar.axes} axes = {a.axisTag:(a.minValue,a.defaultValue,a.maxValue) for a in fvar.axes}
# TODO Apply avar # TODO Apply avar
# TODO Round to F2Dot14? # TODO Round to F2Dot14?
loc = normalizeLocation(loc, axes) loc = normalizeLocation(location, axes)
# Location is normalized now # Location is normalized now
print("Normalized location:", loc) log.info("Normalized location: %s", loc)
gvar = varfont['gvar'] gvar = varfont['gvar']
glyf = varfont['glyf'] glyf = varfont['glyf']
@ -167,12 +168,41 @@ def main(args=None):
for i, delta in deltas.items(): for i, delta in deltas.items():
cvt[i] += int(round(delta)) cvt[i] += int(round(delta))
print("Removing variable tables") log.info("Removing variable tables")
for tag in ('avar','cvar','fvar','gvar','HVAR','MVAR','VVAR','STAT'): for tag in ('avar','cvar','fvar','gvar','HVAR','MVAR','VVAR','STAT'):
if tag in varfont: if tag in varfont:
del varfont[tag] del varfont[tag]
print("Saving instance font", outfile) return varfont
def main(args=None):
from fontTools import configLogger
if args is None:
import sys
args = sys.argv[1:]
varfilename = args[0]
locargs = args[1:]
outfile = os.path.splitext(varfilename)[0] + '-instance.ttf'
# TODO Allow to specify logging verbosity as command line option
configLogger(level=logging.INFO)
loc = {}
for arg in locargs:
tag,val = arg.split('=')
assert len(tag) <= 4
loc[tag.ljust(4)] = float(val)
log.info("Location: %s", loc)
log.info("Loading variable font")
varfont = TTFont(varfilename)
instantiateVariableFont(varfont, loc, inplace=True)
log.info("Saving instance font %s", outfile)
varfont.save(outfile) varfont.save(outfile)