partialInstancer: run black autoformatter
This commit is contained in:
parent
aa59dc92cf
commit
3c69682a16
@ -14,11 +14,7 @@ from __future__ import print_function, division, absolute_import
|
||||
from fontTools.misc.py23 import *
|
||||
from fontTools.misc.fixedTools import floatToFixedToFloat
|
||||
from fontTools.varLib import _GetCoordinates, _SetCoordinates
|
||||
from fontTools.varLib.models import (
|
||||
supportScalar,
|
||||
normalizeValue,
|
||||
piecewiseLinearMap,
|
||||
)
|
||||
from fontTools.varLib.models import supportScalar, normalizeValue, piecewiseLinearMap
|
||||
from fontTools.varLib.iup import iup_delta
|
||||
from fontTools.ttLib import TTFont
|
||||
from fontTools.ttLib.tables._g_l_y_f import GlyphCoordinates
|
||||
@ -84,8 +80,8 @@ def instantiateGvarGlyph(varfont, location, glyphname):
|
||||
def instantiateGvar(varfont, location):
|
||||
log.info("Instantiating glyf/gvar tables")
|
||||
|
||||
gvar = varfont['gvar']
|
||||
glyf = varfont['glyf']
|
||||
gvar = varfont["gvar"]
|
||||
glyf = varfont["glyf"]
|
||||
# Get list of glyph names in gvar sorted by component depth.
|
||||
# If a composite glyph is processed before its base glyph, the bounds may
|
||||
# be calculated incorrectly because deltas haven't been applied to the
|
||||
@ -94,9 +90,10 @@ def instantiateGvar(varfont, location):
|
||||
gvar.variations.keys(),
|
||||
key=lambda name: (
|
||||
glyf[name].getCompositeMaxpValues(glyf).maxComponentDepth
|
||||
if glyf[name].isComposite() else 0,
|
||||
name
|
||||
)
|
||||
if glyf[name].isComposite()
|
||||
else 0,
|
||||
name,
|
||||
),
|
||||
)
|
||||
for glyphname in glyphnames:
|
||||
instantiateGvarGlyph(varfont, location, glyphname)
|
||||
@ -109,22 +106,27 @@ def normalize(value, triple, avar_mapping):
|
||||
# Quantize to F2Dot14, to avoid surprise interpolations.
|
||||
return floatToFixedToFloat(value, 14)
|
||||
|
||||
|
||||
def normalizeAxisLimits(varfont, axis_limits):
|
||||
fvar = varfont['fvar']
|
||||
fvar = varfont["fvar"]
|
||||
bad_limits = axis_limits.keys() - {a.axisTag for a in fvar.axes}
|
||||
if bad_limits:
|
||||
raise ValueError('Cannot limit: {} not present in fvar'.format(bad_limits))
|
||||
raise ValueError("Cannot limit: {} not present in fvar".format(bad_limits))
|
||||
|
||||
axes = {a.axisTag: (a.minValue, a.defaultValue, a.maxValue)
|
||||
for a in fvar.axes if a.axisTag in axis_limits}
|
||||
axes = {
|
||||
a.axisTag: (a.minValue, a.defaultValue, a.maxValue)
|
||||
for a in fvar.axes
|
||||
if a.axisTag in axis_limits
|
||||
}
|
||||
|
||||
avar_segments = {}
|
||||
if 'avar' in varfont:
|
||||
avar_segments = varfont['avar'].segments
|
||||
if "avar" in varfont:
|
||||
avar_segments = varfont["avar"].segments
|
||||
for axis_tag, triple in axes.items():
|
||||
avar_mapping = avar_segments.get(axis_tag, None)
|
||||
axis_limits[axis_tag] = tuple(normalize(v, triple, avar_mapping)
|
||||
for v in axis_limits[axis_tag])
|
||||
axis_limits[axis_tag] = tuple(
|
||||
normalize(v, triple, avar_mapping) for v in axis_limits[axis_tag]
|
||||
)
|
||||
|
||||
|
||||
def sanityCheckVariableTables(varfont):
|
||||
@ -134,6 +136,7 @@ def sanityCheckVariableTables(varfont):
|
||||
if "glyf" not in varfont:
|
||||
raise ValueError("Can't have gvar without glyf")
|
||||
|
||||
|
||||
def instantiateVariableFont(varfont, axis_limits, inplace=False):
|
||||
sanityCheckVariableTables(varfont)
|
||||
|
||||
@ -143,8 +146,6 @@ def instantiateVariableFont(varfont, axis_limits, inplace=False):
|
||||
|
||||
log.info("Normalized limits: %s", axis_limits)
|
||||
|
||||
|
||||
|
||||
if "gvar" in varfont:
|
||||
# TODO: support range, stop dropping max value
|
||||
axis_limits = {tag: minv for tag, (minv, maxv) in axis_limits.items()}
|
||||
@ -160,7 +161,7 @@ def instantiateVariableFont(varfont, axis_limits, inplace=False):
|
||||
def parseLimits(limits):
|
||||
result = {}
|
||||
for limit_string in limits:
|
||||
match = re.match(r'^(\w{1,4})=([^:]+)(?:[:](.+))?$', limit_string)
|
||||
match = re.match(r"^(\w{1,4})=([^:]+)(?:[:](.+))?$", limit_string)
|
||||
if not match:
|
||||
parser.error("invalid location format: %r" % limit_string)
|
||||
tag = match.group(1).ljust(4)
|
||||
@ -184,40 +185,46 @@ def parseArgs(args):
|
||||
|
||||
parser = argparse.ArgumentParser(
|
||||
"fonttools varLib.partialInstancer",
|
||||
description="Partially instantiate a variable font"
|
||||
description="Partially instantiate a variable font",
|
||||
)
|
||||
parser.add_argument("input", metavar="INPUT.ttf", help="Input variable TTF file.")
|
||||
parser.add_argument(
|
||||
"input", metavar="INPUT.ttf", help="Input variable TTF file.")
|
||||
parser.add_argument(
|
||||
"locargs", metavar="AXIS=LOC", nargs="*",
|
||||
"locargs",
|
||||
metavar="AXIS=LOC",
|
||||
nargs="*",
|
||||
help="List of space separated locations. A location consist in "
|
||||
"the name of a variation axis, followed by '=' and a number or"
|
||||
"number:number. E.g.: wdth=100 or wght=75.0:125.0")
|
||||
"number:number. E.g.: wdth=100 or wght=75.0:125.0",
|
||||
)
|
||||
parser.add_argument(
|
||||
"-o", "--output", metavar="OUTPUT.ttf", default=None,
|
||||
help="Output instance TTF file (default: INPUT-instance.ttf).")
|
||||
"-o",
|
||||
"--output",
|
||||
metavar="OUTPUT.ttf",
|
||||
default=None,
|
||||
help="Output instance TTF file (default: INPUT-instance.ttf).",
|
||||
)
|
||||
logging_group = parser.add_mutually_exclusive_group(required=False)
|
||||
logging_group.add_argument(
|
||||
"-v", "--verbose", action="store_true", help="Run more verbosely.")
|
||||
"-v", "--verbose", action="store_true", help="Run more verbosely."
|
||||
)
|
||||
logging_group.add_argument(
|
||||
"-q", "--quiet", action="store_true", help="Turn verbosity off.")
|
||||
"-q", "--quiet", action="store_true", help="Turn verbosity off."
|
||||
)
|
||||
options = parser.parse_args(args)
|
||||
|
||||
infile = options.input
|
||||
outfile = (
|
||||
os.path.splitext(infile)[0] + '-partial.ttf'
|
||||
if not options.output else options.output)
|
||||
os.path.splitext(infile)[0] + "-partial.ttf"
|
||||
if not options.output
|
||||
else options.output
|
||||
)
|
||||
configLogger(
|
||||
level=(
|
||||
"DEBUG" if options.verbose
|
||||
else "ERROR" if options.quiet
|
||||
else "INFO"
|
||||
)
|
||||
level=("DEBUG" if options.verbose else "ERROR" if options.quiet else "INFO")
|
||||
)
|
||||
|
||||
axis_limits = parseLimits(options.locargs)
|
||||
if len(axis_limits) != len(options.locargs):
|
||||
raise ValueError('Specified multiple limits for the same axis')
|
||||
raise ValueError("Specified multiple limits for the same axis")
|
||||
return (infile, outfile, axis_limits)
|
||||
|
||||
|
||||
@ -236,4 +243,5 @@ def main(args=None):
|
||||
|
||||
if __name__ == "__main__":
|
||||
import sys
|
||||
|
||||
sys.exit(main())
|
||||
|
Loading…
x
Reference in New Issue
Block a user