From 85883f09bc6a06595386be0b5ce01a0c67b9a9d4 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 7 Dec 2023 12:17:25 -0700 Subject: [PATCH] [varLib.models] Add getMasterScalars --- Lib/fontTools/varLib/models.py | 16 ++++++++++++++++ Tests/varLib/models_test.py | 8 ++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/Lib/fontTools/varLib/models.py b/Lib/fontTools/varLib/models.py index 33deabe04..51e036876 100644 --- a/Lib/fontTools/varLib/models.py +++ b/Lib/fontTools/varLib/models.py @@ -472,6 +472,22 @@ class VariationModel(object): for support in self.supports ] + def _getMasterScalarsRecurse(self, out, j, weights, scalar=1): + for i, weight in weights.items(): + influence = -weight * scalar + out[i] += influence + self._getMasterScalarsRecurse(out, i, self.deltaWeights[i], influence) + + def getMasterScalars(self, targetLocation): + out = [] + for i, (weights, support) in enumerate(zip(self.deltaWeights, self.supports)): + scalar = supportScalar(targetLocation, support) + out.append(scalar) + self._getMasterScalarsRecurse(out, i, weights, scalar) + + out = [out[self.mapping[i]] for i in range(len(out))] + return out + @staticmethod def interpolateFromDeltasAndScalars(deltas, scalars): v = None diff --git a/Tests/varLib/models_test.py b/Tests/varLib/models_test.py index 11ec1a1e8..bc8b45afd 100644 --- a/Tests/varLib/models_test.py +++ b/Tests/varLib/models_test.py @@ -397,7 +397,7 @@ class VariationModelTest(object): ) @pytest.mark.parametrize( - "locations, axisOrder, masterValues, instanceLocation, expectedValue", + "locations, axisOrder, masterValues, instanceLocation, expectedValue, masterScalars", [ ( [ @@ -422,6 +422,7 @@ class VariationModelTest(object): "axis_B": 0.5, }, 37.5, + [0.25, 0.0, 0.0, -0.25, 0.5, 0.5], ), ], ) @@ -432,8 +433,11 @@ class VariationModelTest(object): masterValues, instanceLocation, expectedValue, + masterScalars, ): model = VariationModel(locations, axisOrder=axisOrder) - interpolatedValue = model.interpolateFromMasters(instanceLocation, masterValues) + interpolatedValue = model.interpolateFromMasters(instanceLocation, masterValues) assert interpolatedValue == expectedValue + + assert masterScalars == model.getMasterScalars(instanceLocation)