From e9b35d031883ea99de825b645c4c34c3d69eaa92 Mon Sep 17 00:00:00 2001 From: Simon Cozens Date: Wed, 3 Mar 2021 10:33:50 +0000 Subject: [PATCH] Split off box-splitting/support code to separate function --- Lib/fontTools/varLib/models.py | 54 +++++++++++++++++++--------------- 1 file changed, 30 insertions(+), 24 deletions(-) diff --git a/Lib/fontTools/varLib/models.py b/Lib/fontTools/varLib/models.py index acb7d2450..d50174b8a 100644 --- a/Lib/fontTools/varLib/models.py +++ b/Lib/fontTools/varLib/models.py @@ -281,34 +281,18 @@ class VariationModel(object): def _computeMasterSupports(self, axisPoints): supports = [] - locations = self.locations - # Compute min/max across each axis, use it as total range. - # TODO Take this as input from outside? - minV = {} - maxV = {} - for l in locations: - for k,v in l.items(): - minV[k] = min(v, minV.get(k, v)) - maxV[k] = max(v, maxV.get(k, v)) - - for i,loc in enumerate(locations): - box = {} - for axis,locV in loc.items(): - if locV > 0: - box[axis] = (0, locV, maxV[axis]) - else: - box[axis] = (minV[axis], locV, 0) - - locAxes = set(loc.keys()) + boxes = self._locationsToBoxes() + for i,box in enumerate(boxes): + locAxes = set(box.keys()) # Walk over previous masters now - for j,m in enumerate(locations[:i]): + for j,prev_box in enumerate(boxes[:i]): # Master with extra axes do not participte - if not set(m.keys()).issubset(locAxes): + if not set(prev_box.keys()).issubset(locAxes): continue # If it's NOT in the current box, it does not participate relevant = True for axis, (lower,peak,upper) in box.items(): - if axis not in m or not (m[axis] == peak or lower < m[axis] < upper): + if axis not in prev_box or not (prev_box[axis][1] == peak or lower < prev_box[axis][1] < upper): relevant = False break if not relevant: @@ -323,8 +307,8 @@ class VariationModel(object): bestAxes = {} bestRatio = -1 - for axis in m.keys(): - val = m[axis] + for axis in prev_box.keys(): + val = prev_box[axis][1] assert axis in box lower,locV,upper = box[axis] newLower, newUpper = lower, upper @@ -349,6 +333,28 @@ class VariationModel(object): self.supports = supports self._computeDeltaWeights() + def _locationsToBoxes(self): + locations = self.locations + # Compute min/max across each axis, use it as total range. + # TODO Take this as input from outside? + minV = {} + maxV = {} + for l in locations: + for k,v in l.items(): + minV[k] = min(v, minV.get(k, v)) + maxV[k] = max(v, maxV.get(k, v)) + + boxes = [] + for i,loc in enumerate(locations): + box = {} + for axis,locV in loc.items(): + if locV > 0: + box[axis] = (0, locV, maxV[axis]) + else: + box[axis] = (minV[axis], locV, 0) + boxes.append(box) + return boxes + def _computeDeltaWeights(self): deltaWeights = [] for i,loc in enumerate(self.locations):