Spaces:
Sleeping
Sleeping
File size: 11,311 Bytes
7d134e4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 |
from fontTools.varLib.models import supportScalar
from fontTools.misc.fixedTools import MAX_F2DOT14
from functools import lru_cache
__all__ = ["rebaseTent"]
EPSILON = 1 / (1 << 14)
def _reverse_negate(v):
return (-v[2], -v[1], -v[0])
def _solve(tent, axisLimit, negative=False):
axisMin, axisDef, axisMax, _distanceNegative, _distancePositive = axisLimit
lower, peak, upper = tent
# Mirror the problem such that axisDef <= peak
if axisDef > peak:
return [
(scalar, _reverse_negate(t) if t is not None else None)
for scalar, t in _solve(
_reverse_negate(tent),
axisLimit.reverse_negate(),
not negative,
)
]
# axisDef <= peak
# case 1: The whole deltaset falls outside the new limit; we can drop it
#
# peak
# 1.........................................o..........
# / \
# / \
# / \
# / \
# 0---|-----------|----------|-------- o o----1
# axisMin axisDef axisMax lower upper
#
if axisMax <= lower and axisMax < peak:
return [] # No overlap
# case 2: Only the peak and outermost bound fall outside the new limit;
# we keep the deltaset, update peak and outermost bound and and scale deltas
# by the scalar value for the restricted axis at the new limit, and solve
# recursively.
#
# |peak
# 1...............................|.o..........
# |/ \
# / \
# /| \
# / | \
# 0--------------------------- o | o----1
# lower | upper
# |
# axisMax
#
# Convert to:
#
# 1............................................
# |
# o peak
# /|
# /x|
# 0--------------------------- o o upper ----1
# lower |
# |
# axisMax
if axisMax < peak:
mult = supportScalar({"tag": axisMax}, {"tag": tent})
tent = (lower, axisMax, axisMax)
return [(scalar * mult, t) for scalar, t in _solve(tent, axisLimit)]
# lower <= axisDef <= peak <= axisMax
gain = supportScalar({"tag": axisDef}, {"tag": tent})
out = [(gain, None)]
# First, the positive side
# outGain is the scalar of axisMax at the tent.
outGain = supportScalar({"tag": axisMax}, {"tag": tent})
# Case 3a: Gain is more than outGain. The tent down-slope crosses
# the axis into negative. We have to split it into multiples.
#
# | peak |
# 1...................|.o.....|..............
# |/x\_ |
# gain................+....+_.|..............
# /| |y\|
# ................../.|....|..+_......outGain
# / | | | \
# 0---|-----------o | | | o----------1
# axisMin lower | | | upper
# | | |
# axisDef | axisMax
# |
# crossing
if gain >= outGain:
# Note that this is the branch taken if both gain and outGain are 0.
# Crossing point on the axis.
crossing = peak + (1 - gain) * (upper - peak)
loc = (max(lower, axisDef), peak, crossing)
scalar = 1
# The part before the crossing point.
out.append((scalar - gain, loc))
# The part after the crossing point may use one or two tents,
# depending on whether upper is before axisMax or not, in one
# case we need to keep it down to eternity.
# Case 3a1, similar to case 1neg; just one tent needed, as in
# the drawing above.
if upper >= axisMax:
loc = (crossing, axisMax, axisMax)
scalar = outGain
out.append((scalar - gain, loc))
# Case 3a2: Similar to case 2neg; two tents needed, to keep
# down to eternity.
#
# | peak |
# 1...................|.o................|...
# |/ \_ |
# gain................+....+_............|...
# /| | \xxxxxxxxxxy|
# / | | \_xxxxxyyyy|
# / | | \xxyyyyyy|
# 0---|-----------o | | o-------|--1
# axisMin lower | | upper |
# | | |
# axisDef | axisMax
# |
# 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
# Eternity justify.
loc2 = (upper, axisMax, axisMax)
scalar2 = 0
out.append((scalar1 - gain, loc1))
out.append((scalar2 - gain, loc2))
else:
# Special-case if peak is at axisMax.
if axisMax == peak:
upper = peak
# Case 3:
# We keep delta as is and only scale the axis upper to achieve
# the desired new tent if feasible.
#
# peak
# 1.....................o....................
# / \_|
# ..................../....+_.........outGain
# / | \
# gain..............+......|..+_.............
# /| | | \
# 0---|-----------o | | | o----------1
# axisMin lower| | | upper
# | | newUpper
# axisDef axisMax
#
newUpper = peak + (1 - gain) * (upper - peak)
assert axisMax <= newUpper # Because outGain > gain
# Disabled because ots doesn't like us:
# https://github.com/fonttools/fonttools/issues/3350
if False and newUpper <= axisDef + (axisMax - axisDef) * 2:
upper = newUpper
if not negative and axisDef + (axisMax - axisDef) * MAX_F2DOT14 < upper:
# we clamp +2.0 to the max F2Dot14 (~1.99994) for convenience
upper = axisDef + (axisMax - axisDef) * MAX_F2DOT14
assert peak < upper
loc = (max(axisDef, lower), peak, upper)
scalar = 1
out.append((scalar - gain, loc))
# Case 4: New limit doesn't fit; we need to chop into two tents,
# because the shape of a triangle with part of one side cut off
# cannot be represented as a triangle itself.
#
# | peak |
# 1.........|......o.|....................
# ..........|...../x\|.............outGain
# | |xxy|\_
# | /xxxy| \_
# | |xxxxy| \_
# | /xxxxy| \_
# 0---|-----|-oxxxxxx| o----------1
# axisMin | lower | upper
# | |
# axisDef axisMax
#
else:
loc1 = (max(axisDef, lower), peak, axisMax)
scalar1 = 1
loc2 = (peak, axisMax, axisMax)
scalar2 = outGain
out.append((scalar1 - gain, loc1))
# Don't add a dirac delta!
if peak < axisMax:
out.append((scalar2 - gain, loc2))
# Now, the negative side
# Case 1neg: Lower extends beyond axisMin: we chop. Simple.
#
# | |peak
# 1..................|...|.o.................
# | |/ \
# gain...............|...+...\...............
# |x_/| \
# |/ | \
# _/| | \
# 0---------------o | | o----------1
# lower | | upper
# | |
# axisMin axisDef
#
if lower <= axisMin:
loc = (axisMin, axisMin, axisDef)
scalar = supportScalar({"tag": axisMin}, {"tag": tent})
out.append((scalar - gain, loc))
# Case 2neg: Lower is betwen axisMin and axisDef: we add two
# tents to keep it down all the way to eternity.
#
# | |peak
# 1...|...............|.o.................
# | |/ \
# gain|...............+...\...............
# |yxxxxxxxxxxxxx/| \
# |yyyyyyxxxxxxx/ | \
# |yyyyyyyyyyyx/ | \
# 0---|-----------o | o----------1
# axisMin lower | upper
# |
# 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
# Eternity justify.
loc2 = (axisMin, axisMin, lower)
scalar2 = 0
out.append((scalar1 - gain, loc1))
out.append((scalar2 - gain, loc2))
return out
@lru_cache(128)
def rebaseTent(tent, axisLimit):
"""Given a tuple (lower,peak,upper) "tent" and new axis limits
(axisMin,axisDefault,axisMax), solves how to represent the tent
under the new axis configuration. All values are in normalized
-1,0,+1 coordinate system. Tent values can be outside this range.
Return value is a list of tuples. Each tuple is of the form
(scalar,tent), where scalar is a multipler to multiply any
delta-sets by, and tent is a new tent for that output delta-set.
If tent value is None, that is a special deltaset that should
be always-enabled (called "gain")."""
axisMin, axisDef, axisMax, _distanceNegative, _distancePositive = axisLimit
assert -1 <= axisMin <= axisDef <= axisMax <= +1
lower, peak, upper = tent
assert -2 <= lower <= peak <= upper <= +2
assert peak != 0
sols = _solve(tent, axisLimit)
n = lambda v: axisLimit.renormalizeValue(v)
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
|