Skip to content

Commit

Permalink
[instancer.avar] Fix it
Browse files Browse the repository at this point in the history
Seems to be working now.
  • Loading branch information
behdad committed May 2, 2024
1 parent ad1628e commit 64d3ccf
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 23 deletions.
3 changes: 3 additions & 0 deletions Lib/fontTools/varLib/instancer/__init__.py
Expand Up @@ -335,6 +335,9 @@ class _BaseAxisLimits(Mapping[str, AxisTriple]):
def __getitem__(self, key: str) -> AxisTriple:
return self._data[key]

def __setitem__(self, key, value):
self._data[key] = value

Check warning on line 339 in Lib/fontTools/varLib/instancer/__init__.py

View check run for this annotation

Codecov / codecov/patch

Lib/fontTools/varLib/instancer/__init__.py#L339

Added line #L339 was not covered by tests

def __iter__(self) -> Iterable[str]:
return iter(self._data)

Expand Down
78 changes: 55 additions & 23 deletions Lib/fontTools/varLib/instancer/avar.py
Expand Up @@ -5,7 +5,13 @@


def VarStore_getExtremes(
self, varIdx, identityAxisIndex=None, nullAxes=set(), cache=None
self,
varIdx,
fvarAxes,
axisLimits,
identityAxisIndex=None,
nullAxes=set(),
cache=None,
):

if varIdx == NO_VARIATION_INDEX:
Expand All @@ -23,16 +29,6 @@ def VarStore_getExtremes(

regionList = self.VarRegionList

Check warning on line 30 in Lib/fontTools/varLib/instancer/avar.py

View check run for this annotation

Codecov / codecov/patch

Lib/fontTools/varLib/instancer/avar.py#L30

Added line #L30 was not covered by tests

if not hasattr(self, "_fvar_axes"):
self._fvar_axes = []
for i in range(regionList.RegionAxisCount):
axis = Axis()
axis.axisTag = str(i)
axis.minValue = -1.0
axis.defaultValue = 0.0
axis.maxValue = 1.0
self._fvar_axes.append(axis)

major = varIdx >> 16
minor = varIdx & 0xFFFF
varData = self.VarData[major]
Expand All @@ -53,9 +49,10 @@ def VarStore_getExtremes(
skip = True
break
thisAxes.add(i)
location[str(i)] = peak
location[fvarAxes[i].axisTag] = peak

Check warning on line 52 in Lib/fontTools/varLib/instancer/avar.py

View check run for this annotation

Codecov / codecov/patch

Lib/fontTools/varLib/instancer/avar.py#L49-L52

Added lines #L49 - L52 were not covered by tests
if skip:
continue
assert thisAxes, "Empty region in VarStore!"

Check warning on line 55 in Lib/fontTools/varLib/instancer/avar.py

View check run for this annotation

Codecov / codecov/patch

Lib/fontTools/varLib/instancer/avar.py#L54-L55

Added lines #L54 - L55 were not covered by tests

locs = [None]

Check warning on line 57 in Lib/fontTools/varLib/instancer/avar.py

View check run for this annotation

Codecov / codecov/patch

Lib/fontTools/varLib/instancer/avar.py#L57

Added line #L57 was not covered by tests
if identityAxisIndex in thisAxes:
Expand All @@ -68,18 +65,37 @@ def VarStore_getExtremes(

for loc in locs:
if loc is not None:
location[str(identityAxisIndex)] = loc

varStoreInstancer = VarStoreInstancer(varStore, self._fvar_axes, location)
location[fvarAxes[identityAxisIndex].axisTag] = loc

Check warning on line 68 in Lib/fontTools/varLib/instancer/avar.py

View check run for this annotation

Codecov / codecov/patch

Lib/fontTools/varLib/instancer/avar.py#L68

Added line #L68 was not covered by tests

scalar = 1

Check warning on line 70 in Lib/fontTools/varLib/instancer/avar.py

View check run for this annotation

Codecov / codecov/patch

Lib/fontTools/varLib/instancer/avar.py#L70

Added line #L70 was not covered by tests
for j, regionAxis in enumerate(region.VarRegionAxis):
peak = regionAxis.PeakCoord

Check warning on line 72 in Lib/fontTools/varLib/instancer/avar.py

View check run for this annotation

Codecov / codecov/patch

Lib/fontTools/varLib/instancer/avar.py#L72

Added line #L72 was not covered by tests
if peak == 0:
continue
axis = fvarAxes[j]
try:
limits = axisLimits[axis.axisTag]

Check warning on line 77 in Lib/fontTools/varLib/instancer/avar.py

View check run for this annotation

Codecov / codecov/patch

Lib/fontTools/varLib/instancer/avar.py#L74-L77

Added lines #L74 - L77 were not covered by tests
if peak > 0:
scalar *= limits[2] - limits[1]

Check warning on line 79 in Lib/fontTools/varLib/instancer/avar.py

View check run for this annotation

Codecov / codecov/patch

Lib/fontTools/varLib/instancer/avar.py#L79

Added line #L79 was not covered by tests
else:
scalar *= limits[1] - limits[0]
except KeyError:
pass

Check warning on line 83 in Lib/fontTools/varLib/instancer/avar.py

View check run for this annotation

Codecov / codecov/patch

Lib/fontTools/varLib/instancer/avar.py#L81-L83

Added lines #L81 - L83 were not covered by tests

varStoreInstancer = VarStoreInstancer(varStore, fvarAxes, location)
v = varStoreInstancer[varIdx] + (0 if loc is None else round(loc * 16384))

Check warning on line 86 in Lib/fontTools/varLib/instancer/avar.py

View check run for this annotation

Codecov / codecov/patch

Lib/fontTools/varLib/instancer/avar.py#L85-L86

Added lines #L85 - L86 were not covered by tests

assert thisAxes, "Empty region in VarStore!"
minOther, maxOther = self.getExtremes(

Check warning on line 88 in Lib/fontTools/varLib/instancer/avar.py

View check run for this annotation

Codecov / codecov/patch

Lib/fontTools/varLib/instancer/avar.py#L88

Added line #L88 was not covered by tests
varIdx, identityAxisIndex, nullAxes | thisAxes, cache
varIdx,
fvarAxes,
axisLimits,
identityAxisIndex,
nullAxes | thisAxes,
cache,
)

minV = min(minV, v + minOther)
maxV = max(maxV, v + maxOther)
minV = min(minV, (v + minOther) * scalar)
maxV = max(maxV, (v + maxOther) * scalar)

Check warning on line 98 in Lib/fontTools/varLib/instancer/avar.py

View check run for this annotation

Codecov / codecov/patch

Lib/fontTools/varLib/instancer/avar.py#L97-L98

Added lines #L97 - L98 were not covered by tests

cache[key] = (minV, maxV)

Check warning on line 100 in Lib/fontTools/varLib/instancer/avar.py

View check run for this annotation

Codecov / codecov/patch

Lib/fontTools/varLib/instancer/avar.py#L100

Added line #L100 was not covered by tests

Expand Down Expand Up @@ -107,13 +123,29 @@ def VarStore_getExtremes(
varIdxMap = avar.table.VarIdxMap
varStore = avar.table.VarStore

for axis in fvar.axes:
if axis.axisTag in limits:
continue
private = axis.flags & 0x1
if not private:
continue
# if private, pin at default
limits[axis.axisTag] = instancer.NormalizedAxisTripleAndDistances(0, 0, 0)

defaultDeltas = instancer.instantiateItemVariationStore(varStore, fvar.axes, limits)

for axisIdx, axis in enumerate(fvar.axes):
axes = fvar.axes

for axisIdx, axis in enumerate(axes):
varIdx = axisIdx
if varIdxMap is not None:
varIdx = varIdxMap[varIdx]
# Only for public axes
identityAxisIndex = None if axis.flags & 0x1 else axisIdx
minV, maxV = varStore.getExtremes(varIdx, identityAxisIndex)
print(axis.axisTag, defaultDeltas[varIdx] / 16384, (minV / 16384, maxV / 16384))
private = axis.flags & 0x1
identityAxisIndex = None if private else axisIdx
minV, maxV = varStore.getExtremes(varIdx, axes, limits, identityAxisIndex)
print(
"%s%s" % (axis.axisTag, "*" if private else ""),
defaultDeltas[varIdx] / 16384,
(minV / 16384, maxV / 16384),
)

0 comments on commit 64d3ccf

Please sign in to comment.