Skip to content

Commit

Permalink
[instancer.solver] Better handling of peak==axisDef
Browse files Browse the repository at this point in the history
  • Loading branch information
behdad committed Aug 17, 2022
1 parent 9a74dec commit df8cea4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 24 deletions.
34 changes: 12 additions & 22 deletions Lib/fontTools/varLib/instancer/solver.py
Expand Up @@ -4,6 +4,8 @@

__all__ = ['rebaseTent']

EPSILON = 1 / (1 << 14)

def _reverse_negate(v):
return (-v[2], -v[1], -v[0])

Expand Down Expand Up @@ -130,6 +132,10 @@ def _solve(tent, axisLimit, negative=False):
# |
# crossing
else:
# A tent's peak cannot fall on axis default. Nudge it.
if upper == axisDef:
upper += EPSILON

# Downslope.
loc1 = (crossing, upper, axisMax)
scalar1 = 0
Expand Down Expand Up @@ -241,6 +247,10 @@ def _solve(tent, axisLimit, negative=False):
# axisDef
#
else:
# A tent's peak cannot fall on axis default. Nudge it.
if lower == axisDef:
lower -= EPSILON

# Downslope.
loc1 = (axisMin, lower, axisDef)
scalar1 = 0
Expand All @@ -252,27 +262,7 @@ def _solve(tent, axisLimit, negative=False):
out.append((scalar1 - gain, loc1))
out.append((scalar2 - gain, loc2))


# If peak ended up being zero, nudge it to the next value
EPSILON = 1 / (1 << 14)
new_out = []
for scalar,triple in out:
if scalar == 0:
continue

if triple is None:
new_out.append((scalar, triple))
continue

lower,peak,upper = triple
if peak == axisDef:
assert not (lower == upper == axisDef)

peak += EPSILON if lower == axisDef else -EPSILON

new_out.append((scalar, (lower,peak,upper)))

return new_out
return out


@lru_cache(128)
Expand All @@ -299,6 +289,6 @@ def rebaseTent(tent, axisLimit):
sols = _solve(tent, axisLimit)

n = lambda v: normalizeValue(v, axisLimit, extrapolate=True)
sols = [(scalar, (n(v[0]), n(v[1]), n(v[2])) if v is not None else None) for scalar,v in sols]
sols = [(scalar, (n(v[0]), n(v[1]), n(v[2])) if v is not None else None) for scalar,v in sols if scalar]

return sols
4 changes: 2 additions & 2 deletions Tests/varLib/instancer/solver_test.py
Expand Up @@ -219,9 +219,9 @@ class RebaseTentTest(object):
[
(1, None),
(-1, (0, 0.0001220703, 1)),
(-1, (0, 1, 1)),
(-1, (0.0001220703, 1, 1)),
(-1, (-1, -0.0001220703, 0)),
(-1, (-1, -1, 0)),
(-1, (-1, -1, -0.0001220703)),
]
),
],
Expand Down

0 comments on commit df8cea4

Please sign in to comment.