[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:
parent
67f0dbdc31
commit
ca7c35900b
@ -10,6 +10,10 @@ from fontTools.ttLib.tables._g_l_y_f import GlyphCoordinates
|
||||
from fontTools.varLib import _GetCoordinates, _SetCoordinates
|
||||
from fontTools.varLib.models import supportScalar, normalizeLocation
|
||||
import os.path
|
||||
import logging
|
||||
|
||||
|
||||
log = logging.getLogger("fontTools.varlib.mutator")
|
||||
|
||||
|
||||
def _iup_segment(coords, rc1, rd1, rc2, rd2):
|
||||
@ -99,33 +103,30 @@ def _iup_delta(delta, coords, ends):
|
||||
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:
|
||||
import sys
|
||||
args = sys.argv[1:]
|
||||
{'wght': 400, 'wdth': 100}
|
||||
|
||||
varfilename = args[0]
|
||||
locargs = args[1:]
|
||||
outfile = os.path.splitext(varfilename)[0] + '-instance.ttf'
|
||||
|
||||
loc = {}
|
||||
for arg in locargs:
|
||||
tag,val = arg.split('=')
|
||||
assert len(tag) <= 4
|
||||
loc[tag.ljust(4)] = float(val)
|
||||
print("Location:", loc)
|
||||
|
||||
print("Loading variable font")
|
||||
varfont = TTFont(varfilename)
|
||||
By default, a new TTFont object is returned. If ``inplace`` is True, the
|
||||
input varfont is modified and reduced to a static font.
|
||||
"""
|
||||
if not inplace:
|
||||
# make a copy to leave input varfont unmodified
|
||||
stream = BytesIO()
|
||||
varfont.save(stream)
|
||||
stream.seek(0)
|
||||
varfont = TTFont(stream)
|
||||
|
||||
fvar = varfont['fvar']
|
||||
axes = {a.axisTag:(a.minValue,a.defaultValue,a.maxValue) for a in fvar.axes}
|
||||
# TODO Apply avar
|
||||
# TODO Round to F2Dot14?
|
||||
loc = normalizeLocation(loc, axes)
|
||||
loc = normalizeLocation(location, axes)
|
||||
# Location is normalized now
|
||||
print("Normalized location:", loc)
|
||||
log.info("Normalized location: %s", loc)
|
||||
|
||||
gvar = varfont['gvar']
|
||||
glyf = varfont['glyf']
|
||||
@ -167,12 +168,41 @@ def main(args=None):
|
||||
for i, delta in deltas.items():
|
||||
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'):
|
||||
if tag in varfont:
|
||||
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)
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user