diff options
Diffstat (limited to 'Lib/fontTools/misc/arrayTools.py')
-rw-r--r-- | Lib/fontTools/misc/arrayTools.py | 128 |
1 files changed, 24 insertions, 104 deletions
diff --git a/Lib/fontTools/misc/arrayTools.py b/Lib/fontTools/misc/arrayTools.py index e76ced7f..c20a9eda 100644 --- a/Lib/fontTools/misc/arrayTools.py +++ b/Lib/fontTools/misc/arrayTools.py @@ -2,11 +2,11 @@ so on. """ -from fontTools.misc.py23 import * -from fontTools.misc.fixedTools import otRound -from numbers import Number +from fontTools.misc.roundTools import otRound +from fontTools.misc.vector import Vector as _Vector import math -import operator +import warnings + def calcBounds(array): """Calculate the bounding rectangle of a 2D points array. @@ -228,6 +228,19 @@ def rectCenter(rect): (xMin, yMin, xMax, yMax) = rect return (xMin+xMax)/2, (yMin+yMax)/2 +def rectArea(rect): + """Determine rectangle area. + + Args: + rect: Bounding rectangle, expressed as tuples + ``(xMin, yMin, xMax, yMax)``. + + Returns: + The area of the rectangle. + """ + (xMin, yMin, xMax, yMax) = rect + return (yMax - yMin) * (xMax - xMin) + def intRect(rect): """Round a rectangle to integer values. @@ -248,107 +261,14 @@ def intRect(rect): return (xMin, yMin, xMax, yMax) -class Vector(object): - """A math-like vector. - - Represents an n-dimensional numeric vector. ``Vector`` objects support - vector addition and subtraction, scalar multiplication and division, - negation, rounding, and comparison tests. - - Attributes: - values: Sequence of values stored in the vector. - """ +class Vector(_Vector): - def __init__(self, values, keep=False): - """Initialize a vector. If ``keep`` is true, values will be copied.""" - 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 _vectorOp(self, other, op): - if isinstance(other, Vector): - assert len(self.values) == len(other.values) - a = self.values - b = other.values - return [op(a[i], b[i]) for i in range(len(self.values))] - if isinstance(other, Number): - return [op(v, other) for v in self.values] - raise NotImplementedError - - def _scalarOp(self, other, op): - if isinstance(other, Number): - return [op(v, other) for v in self.values] - raise NotImplementedError - - def _unaryOp(self, op): - return [op(v) for v in self.values] - - def __add__(self, other): - return Vector(self._vectorOp(other, operator.add), keep=True) - def __iadd__(self, other): - self.values = self._vectorOp(other, operator.add) - return self - __radd__ = __add__ - - def __sub__(self, other): - return Vector(self._vectorOp(other, operator.sub), keep=True) - def __isub__(self, other): - self.values = self._vectorOp(other, operator.sub) - return self - def __rsub__(self, other): - return other + (-self) - - def __mul__(self, other): - return Vector(self._scalarOp(other, operator.mul), keep=True) - def __imul__(self, other): - self.values = self._scalarOp(other, operator.mul) - return self - __rmul__ = __mul__ - - def __truediv__(self, other): - return Vector(self._scalarOp(other, operator.truediv), keep=True) - def __itruediv__(self, other): - self.values = self._scalarOp(other, operator.truediv) - return self - - def __pos__(self): - return Vector(self._unaryOp(operator.pos), keep=True) - def __neg__(self): - return Vector(self._unaryOp(operator.neg), keep=True) - def __round__(self): - return Vector(self._unaryOp(round), keep=True) - def toInt(self): - """Synonym for ``round``.""" - return self.__round__() - - def __eq__(self, other): - if type(other) == Vector: - return self.values == other.values - else: - return self.values == other - def __ne__(self, other): - return not self.__eq__(other) - - def __bool__(self): - return any(self.values) - __nonzero__ = __bool__ - - def __abs__(self): - return math.sqrt(sum([x*x for x in self.values])) - def dot(self, other): - """Performs vector dot product, returning sum of - ``a[0] * b[0], a[1] * b[1], ...``""" - a = self.values - b = other.values if type(other) == Vector else b - assert len(a) == len(b) - return sum([a[i] * b[i] for i in range(len(a))]) + def __init__(self, *args, **kwargs): + warnings.warn( + "fontTools.misc.arrayTools.Vector has been deprecated, please use " + "fontTools.misc.vector.Vector instead.", + DeprecationWarning, + ) def pairwise(iterable, reverse=False): |