Skip to content

Commit

Permalink
[instancer] Merge pinTupleVariationAxes and limitTupleVariationAxisRa…
Browse files Browse the repository at this point in the history
…nges
  • Loading branch information
behdad committed Aug 17, 2022
1 parent c47e7e6 commit 90fee5d
Showing 1 changed file with 22 additions and 38 deletions.
60 changes: 22 additions & 38 deletions Lib/fontTools/varLib/instancer/__init__.py
Expand Up @@ -183,17 +183,8 @@ def instantiateTupleVariationStore(
Returns:
List[float]: the overall delta adjustment after applicable deltas were summed.
"""
pinnedLocation, axisRanges = splitAxisLocationAndRanges(
axisLimits, rangeType=NormalizedAxisRange
)

newVariations = variations

if pinnedLocation:
newVariations = pinTupleVariationAxes(variations, pinnedLocation)

if axisRanges:
newVariations = limitTupleVariationAxisRanges(newVariations, axisRanges)
newVariations = changeTupleVariationsAxisLimits(variations, axisLimits)

mergedVariations = collections.OrderedDict()
for var in newVariations:
Expand All @@ -219,29 +210,11 @@ def instantiateTupleVariationStore(
return defaultVar.coordinates if defaultVar is not None else []


def pinTupleVariationAxes(variations, location):
newVariations = []
for var in variations:
# Compute the scalar support of the axes to be pinned at the desired location,
# excluding any axes that we are not pinning.
# If a TupleVariation doesn't mention an axis, it implies that the axis peak
# is 0 (i.e. the axis does not participate).
support = {axis: var.axes.pop(axis, (-1, 0, +1)) for axis in location}
scalar = supportScalar(location, support)
if scalar == 0.0:
# no influence, drop the TupleVariation
continue
if scalar != 1.0:
var.scaleDeltas(scalar)
newVariations.append(var)
return newVariations


def limitTupleVariationAxisRanges(variations, axisRanges):
for axisTag, axisRange in sorted(axisRanges.items()):
def changeTupleVariationsAxisLimits(variations, axisLimits):
for axisTag, axisLimit in sorted(axisLimits.items()):
newVariations = []
for var in variations:
newVariations.extend(limitTupleVariationAxisRange(var, axisTag, axisRange))
newVariations.extend(changeTupleVariationAxisLimit(var, axisTag, axisLimit))
variations = newVariations
return variations

Expand All @@ -250,28 +223,39 @@ def _negate(*values):
yield from (-1 * v for v in values)


def limitTupleVariationAxisRange(var, axisTag, axisRange):
assert isinstance(axisRange, NormalizedAxisRange)
def changeTupleVariationAxisLimit(var, axisTag, axisLimit):
assert isinstance(axisLimit, NormalizedAxisTent)

# if axis is fully pinned down, get it out of the way
if axisLimit.minimum == axisLimit.maximum:
support = {axisTag: var.axes.pop(axisTag, (-1, 0, 1))}
scalar = supportScalar({axisTag: axisLimit.default}, support)
if scalar == 0.0:
return []
if scalar != 1.0:
var.scaleDeltas(scalar)
return [var]

# skip when current axis is missing (i.e. doesn't participate), or when the
# 'tent' isn't fully on either the negative or positive side
lower, peak, upper = var.axes.get(axisTag, (-1, 0, 1))

if peak == 0 or lower > peak or peak > upper or (lower < 0 and upper > 0):
return [var]

negative = lower < 0
if negative:
if axisRange.minimum == -1.0:
if axisLimit.minimum == -1.0:
return [var]
elif axisRange.minimum == 0.0:
elif axisLimit.minimum == 0.0:
return []
else:
if axisRange.maximum == 1.0:
if axisLimit.maximum == 1.0:
return [var]
elif axisRange.maximum == 0.0:
elif axisLimit.maximum == 0.0:
return []

limit = axisRange.minimum if negative else axisRange.maximum
limit = axisLimit.minimum if negative else axisLimit.maximum

# Rebase axis bounds onto the new limit, which then becomes the new -1.0 or +1.0.
# The results are always positive, because both dividend and divisor are either
Expand Down

0 comments on commit 90fee5d

Please sign in to comment.