Skip to content

Commit

Permalink
[instancer] Start avar2 subsetting
Browse files Browse the repository at this point in the history
Just some code for now to get min/max delta of a
varIdx from a varStore...
  • Loading branch information
behdad committed Apr 18, 2024
1 parent 705acc9 commit 486a1f6
Showing 1 changed file with 84 additions and 0 deletions.
84 changes: 84 additions & 0 deletions Lib/fontTools/varLib/instancer/avar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
from fontTools.varLib.varStore import VarStoreInstancer, NO_VARIATION_INDEX
from fontTools.ttLib.tables.otTables import VarStore
from fontTools.ttLib.tables._f_v_a_r import Axis


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

if varIdx == NO_VARIATION_INDEX:
return 0, 0

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L9 was not covered by tests

if cache is None:
cache = {}

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L12 was not covered by tests

key = (varIdx, frozenset(nullAxes))

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L14 was not covered by tests
if key in cache:
return cache[key]

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L16 was not covered by tests

regionList = self.VarRegionList
fvar_axes = []

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

View check run for this annotation

Codecov / codecov/patch

Lib/fontTools/varLib/instancer/avar.py#L18-L19

Added lines #L18 - L19 were not covered by tests
for i in range(regionList.RegionAxisCount):
axis = Axis()
axis.axisTag = str(i)
axis.minValue = -1.0
axis.defaultValue = 0.0
axis.maxValue = 1.0
fvar_axes.append(axis)

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

View check run for this annotation

Codecov / codecov/patch

Lib/fontTools/varLib/instancer/avar.py#L21-L26

Added lines #L21 - L26 were not covered by tests

major = varIdx >> 16
minor = varIdx & 0xFFFF
varData = self.VarData[major]
regionIndices = varData.VarRegionIndex

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

View check run for this annotation

Codecov / codecov/patch

Lib/fontTools/varLib/instancer/avar.py#L28-L31

Added lines #L28 - L31 were not covered by tests

minV = 0
maxV = 0

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

View check run for this annotation

Codecov / codecov/patch

Lib/fontTools/varLib/instancer/avar.py#L33-L34

Added lines #L33 - L34 were not covered by tests
for regionIndex in regionIndices:
location = {}
region = regionList.Region[regionIndex]
skip = False
thisAxes = set()

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

View check run for this annotation

Codecov / codecov/patch

Lib/fontTools/varLib/instancer/avar.py#L36-L39

Added lines #L36 - L39 were not covered by tests
for i, regionAxis in enumerate(region.VarRegionAxis):
peak = regionAxis.PeakCoord

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L41 was not covered by tests
if peak == 0:
continue

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L43 was not covered by tests
if i in nullAxes:
skip = True
break
thisAxes.add(i)
location[str(i)] = peak

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

View check run for this annotation

Codecov / codecov/patch

Lib/fontTools/varLib/instancer/avar.py#L45-L48

Added lines #L45 - L48 were not covered by tests
if skip:
continue
instancer = VarStoreInstancer(varStore, fvar_axes, location)
v = instancer[varIdx]

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#L50-L52

Added lines #L50 - L52 were not covered by tests

assert thisAxes, "Empty region in VarStore!"
minOther, maxOther = self.getExtremes(varIdx, nullAxes | thisAxes, cache)

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

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

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

View check run for this annotation

Codecov / codecov/patch

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

Added lines #L57 - L58 were not covered by tests

cache[key] = (minV, maxV)

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L60 was not covered by tests

return minV, maxV

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L62 was not covered by tests


VarStore.getExtremes = VarStore_getExtremes


if __name__ == "__main__":
import sys
from fontTools.ttLib import TTFont

font = TTFont(sys.argv[1])
fvar = font["fvar"]
avar = font["avar"]

varIdxMap = avar.table.VarIdxMap
varStore = avar.table.VarStore

for axisIdx, axis in enumerate(fvar.axes):
varIdx = axisIdx
if varIdxMap is not None:
varIdx = varIdxMap[varIdx]
minV, maxV = varStore.getExtremes(varIdx)
print(axis.axisTag, (minV / 16384, maxV / 16384))

0 comments on commit 486a1f6

Please sign in to comment.