From 18efee2c7bb9ba4afb66912240d183fc0b6b4214 Mon Sep 17 00:00:00 2001 From: Cosimo Lupo Date: Fri, 14 Jun 2019 10:59:41 +0100 Subject: [PATCH] TupleVariation: rename get{DeltaType,CoordWidth}; do not special-case scalar=0 we still need to branch between the case where coordinates are wrapped in (x, y) tuples or naked floats. --- Lib/fontTools/ttLib/tables/TupleVariation.py | 51 +++++++++----------- Tests/ttLib/tables/TupleVariation_test.py | 18 +++---- 2 files changed, 31 insertions(+), 38 deletions(-) diff --git a/Lib/fontTools/ttLib/tables/TupleVariation.py b/Lib/fontTools/ttLib/tables/TupleVariation.py index f77cf1780..1f1612d75 100644 --- a/Lib/fontTools/ttLib/tables/TupleVariation.py +++ b/Lib/fontTools/ttLib/tables/TupleVariation.py @@ -444,49 +444,43 @@ class TupleVariation(object): size += axisCount * 4 return size - def getDeltaType(self): - """ Check if deltas are (x, y) as in gvar, or single values as in cvar. - Returns a string ("gvar" or "cvar"), or None if empty. + def getCoordWidth(self): + """ Return 2 if coordinates are (x, y) as in gvar, 1 if single values + as in cvar, or 0 if empty. """ firstDelta = next((c for c in self.coordinates if c is not None), None) if firstDelta is None: - return # empty or has no impact + return 0 # empty or has no impact if type(firstDelta) is tuple and len(firstDelta) == 2: - return "gvar" + return 2 elif type(firstDelta) in (int, float): - return "cvar" + return 1 else: raise TypeError("invalid type of delta: %s" % type(firstDelta)) def scaleDeltas(self, scalar): if scalar == 1.0: return # no change - deltaType = self.getDeltaType() - if deltaType == "gvar": - if scalar == 0: - self.coordinates = [(0, 0)] * len(self.coordinates) - else: - self.coordinates = [ - (d[0] * scalar, d[1] * scalar) if d is not None else None - for d in self.coordinates - ] - else: - if scalar == 0: - self.coordinates = [0] * len(self.coordinates) - else: - self.coordinates = [ - d * scalar if d is not None else None - for d in self.coordinates - ] + coordWidth = self.getCoordWidth() + if coordWidth == 2: + self.coordinates = [ + (d[0] * scalar, d[1] * scalar) if d is not None else None + for d in self.coordinates + ] + elif coordWidth == 1: + self.coordinates = [ + d * scalar if d is not None else None + for d in self.coordinates + ] def roundDeltas(self): - deltaType = self.getDeltaType() - if deltaType == "gvar": + coordWidth = self.getCoordWidth() + if coordWidth == 2: self.coordinates = [ (otRound(d[0]), otRound(d[1])) if d is not None else None for d in self.coordinates ] - else: + elif coordWidth == 1: self.coordinates = [ otRound(d) if d is not None else None for d in self.coordinates ] @@ -494,7 +488,7 @@ class TupleVariation(object): def calcInferredDeltas(self, origCoords, endPts): from fontTools.varLib.iup import iup_delta - if self.getDeltaType() == "cvar": + if self.getCoordWidth() == 1: raise TypeError( "Only 'gvar' TupleVariation can have inferred deltas" ) @@ -538,13 +532,12 @@ class TupleVariation(object): return NotImplemented deltas1 = self.coordinates length = len(deltas1) - deltaType = self.getDeltaType() deltas2 = other.coordinates if len(deltas2) != length: raise ValueError( "cannot sum TupleVariation deltas with different lengths" ) - if deltaType == "gvar": + if self.getCoordWidth() == 2: for i, d2 in zip(range(length), deltas2): d1 = deltas1[i] try: diff --git a/Tests/ttLib/tables/TupleVariation_test.py b/Tests/ttLib/tables/TupleVariation_test.py index 5a29f18f8..d78810a9c 100644 --- a/Tests/ttLib/tables/TupleVariation_test.py +++ b/Tests/ttLib/tables/TupleVariation_test.py @@ -688,24 +688,24 @@ class TupleVariationTest(unittest.TestCase): content = writer.file.getvalue().decode("utf-8") return [line.strip() for line in content.splitlines()][1:] - def test_getDeltaType(self): + def test_getCoordWidth(self): empty = TupleVariation({}, []) - self.assertIsNone(empty.getDeltaType()) + self.assertEqual(empty.getCoordWidth(), 0) empty = TupleVariation({}, [None]) - self.assertIsNone(empty.getDeltaType()) + self.assertEqual(empty.getCoordWidth(), 0) gvarTuple = TupleVariation({}, [None, (0, 0)]) - self.assertEqual(gvarTuple.getDeltaType(), "gvar") + self.assertEqual(gvarTuple.getCoordWidth(), 2) cvarTuple = TupleVariation({}, [None, 0]) - self.assertEqual(cvarTuple.getDeltaType(), "cvar") + self.assertEqual(cvarTuple.getCoordWidth(), 1) cvarTuple.coordinates[1] *= 1.0 - self.assertEqual(cvarTuple.getDeltaType(), "cvar") + self.assertEqual(cvarTuple.getCoordWidth(), 1) with self.assertRaises(TypeError): - TupleVariation({}, [None, "a"]).getDeltaType() + TupleVariation({}, [None, "a"]).getCoordWidth() def test_scaleDeltas_cvar(self): var = TupleVariation({}, [100, None]) @@ -718,7 +718,7 @@ class TupleVariationTest(unittest.TestCase): self.assertIsNone(var.coordinates[1]) var.scaleDeltas(0.0) - self.assertEqual(var.coordinates, [0, 0]) + self.assertEqual(var.coordinates, [0, None]) def test_scaleDeltas_gvar(self): var = TupleVariation({}, [(100, 200), None]) @@ -732,7 +732,7 @@ class TupleVariationTest(unittest.TestCase): self.assertIsNone(var.coordinates[1]) var.scaleDeltas(0.0) - self.assertEqual(var.coordinates, [(0, 0), (0, 0)]) + self.assertEqual(var.coordinates, [(0, 0), None]) def test_roundDeltas_cvar(self): var = TupleVariation({}, [55.5, None, 99.9])