From c91f0323db9bfcf9426b00b44bce7bb676c3b65d Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Tue, 10 Oct 2017 12:43:15 +0200 Subject: [PATCH] Rename CVTValues to arrayTools.Vector This is generic, and useful in other cases. --- Lib/fontTools/misc/arrayTools.py | 33 ++++++++++++++++++++++++++++ Lib/fontTools/ttLib/tables/_c_v_t.py | 32 --------------------------- Lib/fontTools/varLib/__init__.py | 4 ++-- 3 files changed, 35 insertions(+), 34 deletions(-) diff --git a/Lib/fontTools/misc/arrayTools.py b/Lib/fontTools/misc/arrayTools.py index a3b495b23..44c94e120 100644 --- a/Lib/fontTools/misc/arrayTools.py +++ b/Lib/fontTools/misc/arrayTools.py @@ -6,6 +6,7 @@ from __future__ import print_function, division, absolute_import from fontTools.misc.py23 import * +from numbers import Number import math def calcBounds(array): @@ -119,6 +120,38 @@ def intRect(rect1): yMax = int(math.ceil(yMax)) return (xMin, yMin, xMax, yMax) +class Vector(object): + """A math-like vector.""" + + def __init__(self, values, keep=False): + self.values = values if keep else list(values) + + def __getitem__(self, index): + return self.values[index] + + def __len__(self): + return len(self.values) + + def __repr__(self): + return "Vector(%s)" % self.values + + def __isub__(self, other): + if isinstance(other, Vector): + assert len(self.values) == len(other.values) + a = self.values + b = other.values + self.values = [a[i] - b[i] for i in range(len(self.values))] + return self + if isinstance(other, Number): + self.values = [v - other for v in self.values] + return self + return NotImplemented + + def __mul__(self, other): + if isinstance(other, Number): + return Vector([v * other for v in self.values], keep=True) + return NotImplemented + def _test(): """ diff --git a/Lib/fontTools/ttLib/tables/_c_v_t.py b/Lib/fontTools/ttLib/tables/_c_v_t.py index 15094f551..4fbee7bfc 100644 --- a/Lib/fontTools/ttLib/tables/_c_v_t.py +++ b/Lib/fontTools/ttLib/tables/_c_v_t.py @@ -1,5 +1,4 @@ from __future__ import print_function, division, absolute_import -from numbers import Number from fontTools.misc.py23 import * from fontTools.misc.textTools import safeEval from . import DefaultTable @@ -48,34 +47,3 @@ class table__c_v_t(DefaultTable.DefaultTable): def __delitem__(self, index): del self.values[index] - - -class CVTValues(object): - """ This is used in varLib for calculating control value deltas""" - - def __init__(self, values): - self.values = list(values) - - def __getitem__(self, index): - return self.values[index] - - def __len__(self): - return len(self.values) - - def __repr__(self): - return u"CVTValues(%s)" % self.values - - def __isub__(self, other): - if isinstance(other, CVTValues): - assert len(self.values) == len(other) - self.values = [self.values[i] - other.values[i] for i in range(len(self.values))] - return self - if isinstance(other, Number): - self.values = [v - other for v in self.values] - return self - return NotImplemented - - def __mul__(self, other): - if isinstance(other, Number): - return CVTValues([v * other for v in self.values]) - return NotImplemented diff --git a/Lib/fontTools/varLib/__init__.py b/Lib/fontTools/varLib/__init__.py index 3c5c1f207..5d003c7ee 100644 --- a/Lib/fontTools/varLib/__init__.py +++ b/Lib/fontTools/varLib/__init__.py @@ -21,9 +21,9 @@ API *will* change in near future. from __future__ import print_function, division, absolute_import from __future__ import unicode_literals from fontTools.misc.py23 import * +from fontTools.misc.arrayTools import Vector from fontTools.ttLib import TTFont, newTable from fontTools.ttLib.tables._n_a_m_e import NameRecord -from fontTools.ttLib.tables._c_v_t import CVTValues from fontTools.ttLib.tables._f_v_a_r import Axis, NamedInstance from fontTools.ttLib.tables._g_l_y_f import GlyphCoordinates from fontTools.ttLib.tables.TupleVariation import TupleVariation @@ -511,7 +511,7 @@ def _add_cvar(font, model, master_ttfs, tolerance=0.5): cvar.version = 1 cvar.variations = [] - allCVTs = [CVTValues(m["cvt "].values) for m in master_ttfs] + allCVTs = [Vector(m["cvt "].values) for m in master_ttfs] num_cvts0 = len(allCVTs[0]) if (any(len(c) != num_cvts0 for c in allCVTs)):