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