From 18f8a30305683d280e2b2b147dd598cdb50a0cb3 Mon Sep 17 00:00:00 2001 From: Cosimo Lupo Date: Fri, 22 Mar 2019 14:13:55 +0000 Subject: [PATCH] TupleVariation: add scaleDeltas and roundDeltas method --- Lib/fontTools/ttLib/tables/TupleVariation.py | 45 +++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/Lib/fontTools/ttLib/tables/TupleVariation.py b/Lib/fontTools/ttLib/tables/TupleVariation.py index e2ceba4d1..5eee56e57 100644 --- a/Lib/fontTools/ttLib/tables/TupleVariation.py +++ b/Lib/fontTools/ttLib/tables/TupleVariation.py @@ -306,7 +306,7 @@ class TupleVariation(object): elif type(c) is int: deltaX.append(c) elif c is not None: - raise ValueError("invalid type of delta: %s" % type(c)) + raise TypeError("invalid type of delta: %s" % type(c)) return self.compileDeltaValues_(deltaX) + self.compileDeltaValues_(deltaY) @staticmethod @@ -446,6 +446,49 @@ class TupleVariation(object): size += axisCount * 4 return size + def scaleDeltas(self, scalar): + if scalar == 1.0: + return # no change + # check if deltas are (x, y) as in gvar, or single values as in cvar + firstDelta = next((c for c in self.coordinates if c is not None), None) + if firstDelta is None: + return # nothing to scale + if type(firstDelta) is tuple and len(firstDelta) == 2: + 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 + ] + elif type(firstDelta) in (int, float): + 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 + ] + else: + raise TypeError("invalid type of delta: %s" % type(firstDelta)) + + def roundDeltas(self): + # check if deltas are (x, y) as in gvar, or single values as in cvar + firstDelta = next((c for c in self.coordinates if c is not None), None) + if firstDelta is None: + return # nothing to round + if type(firstDelta) is tuple and len(firstDelta) == 2: + self.coordinates = [ + (otRound(d[0]), otRound(d[1])) if d is not None else None + for d in self.coordinates + ] + elif type(firstDelta) in (int, float): + self.coordinates = [ + otRound(d) if d is not None else None for d in self.coordinates + ] + else: + raise TypeError("invalid type of delta: %s" % type(firstDelta)) + def decompileSharedTuples(axisTags, sharedTupleCount, data, offset): result = []