aboutsummaryrefslogtreecommitdiff
path: root/Lib/fontTools/misc
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/fontTools/misc')
-rw-r--r--Lib/fontTools/misc/__init__.py2
-rw-r--r--Lib/fontTools/misc/arrayTools.py128
-rw-r--r--Lib/fontTools/misc/bezierTools.py638
-rw-r--r--Lib/fontTools/misc/classifyTools.py1
-rw-r--r--Lib/fontTools/misc/cliTools.py1
-rw-r--r--Lib/fontTools/misc/dictTools.py1
-rw-r--r--Lib/fontTools/misc/eexec.py3
-rw-r--r--Lib/fontTools/misc/encodingTools.py1
-rw-r--r--Lib/fontTools/misc/etree.py6
-rw-r--r--Lib/fontTools/misc/filenames.py8
-rw-r--r--Lib/fontTools/misc/fixedTools.py28
-rw-r--r--Lib/fontTools/misc/intTools.py2
-rw-r--r--Lib/fontTools/misc/loggingTools.py7
-rw-r--r--Lib/fontTools/misc/macCreatorType.py3
-rw-r--r--Lib/fontTools/misc/macRes.py3
-rw-r--r--Lib/fontTools/misc/plistlib/__init__.py9
-rw-r--r--Lib/fontTools/misc/psCharStrings.py11
-rw-r--r--Lib/fontTools/misc/psLib.py19
-rw-r--r--Lib/fontTools/misc/psOperators.py2
-rw-r--r--Lib/fontTools/misc/py23.py2
-rw-r--r--Lib/fontTools/misc/roundTools.py58
-rw-r--r--Lib/fontTools/misc/sstruct.py4
-rw-r--r--Lib/fontTools/misc/symfont.py4
-rw-r--r--Lib/fontTools/misc/testTools.py5
-rw-r--r--Lib/fontTools/misc/textTools.py4
-rw-r--r--Lib/fontTools/misc/timeTools.py1
-rw-r--r--Lib/fontTools/misc/vector.py143
-rw-r--r--Lib/fontTools/misc/xmlReader.py1
-rw-r--r--Lib/fontTools/misc/xmlWriter.py8
29 files changed, 818 insertions, 285 deletions
diff --git a/Lib/fontTools/misc/__init__.py b/Lib/fontTools/misc/__init__.py
index b1760311..156cb232 100644
--- a/Lib/fontTools/misc/__init__.py
+++ b/Lib/fontTools/misc/__init__.py
@@ -1,3 +1 @@
"""Empty __init__.py file to signal Python this directory is a package."""
-
-from fontTools.misc.py23 import *
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):
diff --git a/Lib/fontTools/misc/bezierTools.py b/Lib/fontTools/misc/bezierTools.py
index 659de34e..2cf2640c 100644
--- a/Lib/fontTools/misc/bezierTools.py
+++ b/Lib/fontTools/misc/bezierTools.py
@@ -2,9 +2,12 @@
"""fontTools.misc.bezierTools.py -- tools for working with Bezier path segments.
"""
-from fontTools.misc.arrayTools import calcBounds
-from fontTools.misc.py23 import *
+from fontTools.misc.arrayTools import calcBounds, sectRect, rectArea
+from fontTools.misc.transform import Identity
import math
+from collections import namedtuple
+
+Intersection = namedtuple("Intersection", ["pt", "t1", "t2"])
__all__ = [
@@ -25,6 +28,14 @@ __all__ = [
"splitCubicAtT",
"solveQuadratic",
"solveCubic",
+ "quadraticPointAtT",
+ "cubicPointAtT",
+ "linePointAtT",
+ "segmentPointAtT",
+ "lineLineIntersections",
+ "curveLineIntersections",
+ "curveCurveIntersections",
+ "segmentSegmentIntersections",
]
@@ -42,23 +53,31 @@ def calcCubicArcLength(pt1, pt2, pt3, pt4, tolerance=0.005):
Returns:
Arc length value.
"""
- return calcCubicArcLengthC(complex(*pt1), complex(*pt2), complex(*pt3), complex(*pt4), tolerance)
+ return calcCubicArcLengthC(
+ complex(*pt1), complex(*pt2), complex(*pt3), complex(*pt4), tolerance
+ )
def _split_cubic_into_two(p0, p1, p2, p3):
- mid = (p0 + 3 * (p1 + p2) + p3) * .125
- deriv3 = (p3 + p2 - p1 - p0) * .125
- return ((p0, (p0 + p1) * .5, mid - deriv3, mid),
- (mid, mid + deriv3, (p2 + p3) * .5, p3))
+ mid = (p0 + 3 * (p1 + p2) + p3) * 0.125
+ deriv3 = (p3 + p2 - p1 - p0) * 0.125
+ return (
+ (p0, (p0 + p1) * 0.5, mid - deriv3, mid),
+ (mid, mid + deriv3, (p2 + p3) * 0.5, p3),
+ )
+
def _calcCubicArcLengthCRecurse(mult, p0, p1, p2, p3):
- arch = abs(p0-p3)
- box = abs(p0-p1) + abs(p1-p2) + abs(p2-p3)
- if arch * mult >= box:
- return (arch + box) * .5
- else:
- one,two = _split_cubic_into_two(p0,p1,p2,p3)
- return _calcCubicArcLengthCRecurse(mult, *one) + _calcCubicArcLengthCRecurse(mult, *two)
+ arch = abs(p0 - p3)
+ box = abs(p0 - p1) + abs(p1 - p2) + abs(p2 - p3)
+ if arch * mult >= box:
+ return (arch + box) * 0.5
+ else:
+ one, two = _split_cubic_into_two(p0, p1, p2, p3)
+ return _calcCubicArcLengthCRecurse(mult, *one) + _calcCubicArcLengthCRecurse(
+ mult, *two
+ )
+
def calcCubicArcLengthC(pt1, pt2, pt3, pt4, tolerance=0.005):
"""Calculates the arc length for a cubic Bezier segment.
@@ -70,7 +89,7 @@ def calcCubicArcLengthC(pt1, pt2, pt3, pt4, tolerance=0.005):
Returns:
Arc length value.
"""
- mult = 1. + 1.5 * tolerance # The 1.5 is a empirical hack; no math
+ mult = 1.0 + 1.5 * tolerance # The 1.5 is a empirical hack; no math
return _calcCubicArcLengthCRecurse(mult, pt1, pt2, pt3, pt4)
@@ -85,7 +104,7 @@ def _dot(v1, v2):
def _intSecAtan(x):
# In : sympy.integrate(sp.sec(sp.atan(x)))
# Out: x*sqrt(x**2 + 1)/2 + asinh(x)/2
- return x * math.sqrt(x**2 + 1)/2 + math.asinh(x)/2
+ return x * math.sqrt(x ** 2 + 1) / 2 + math.asinh(x) / 2
def calcQuadraticArcLength(pt1, pt2, pt3):
@@ -141,16 +160,16 @@ def calcQuadraticArcLengthC(pt1, pt2, pt3):
d = d1 - d0
n = d * 1j
scale = abs(n)
- if scale == 0.:
- return abs(pt3-pt1)
- origDist = _dot(n,d0)
+ if scale == 0.0:
+ return abs(pt3 - pt1)
+ origDist = _dot(n, d0)
if abs(origDist) < epsilon:
- if _dot(d0,d1) >= 0:
- return abs(pt3-pt1)
+ if _dot(d0, d1) >= 0:
+ return abs(pt3 - pt1)
a, b = abs(d0), abs(d1)
- return (a*a + b*b) / (a+b)
- x0 = _dot(d,d0) / origDist
- x1 = _dot(d,d1) / origDist
+ return (a * a + b * b) / (a + b)
+ x0 = _dot(d, d0) / origDist
+ x1 = _dot(d, d1) / origDist
Len = abs(2 * (_intSecAtan(x1) - _intSecAtan(x0)) * origDist / (scale * (x1 - x0)))
return Len
@@ -190,13 +209,17 @@ def approximateQuadraticArcLengthC(pt1, pt2, pt3):
# to be integrated with the best-matching fifth-degree polynomial
# approximation of it.
#
- #https://en.wikipedia.org/wiki/Gaussian_quadrature#Gauss.E2.80.93Legendre_quadrature
+ # https://en.wikipedia.org/wiki/Gaussian_quadrature#Gauss.E2.80.93Legendre_quadrature
# abs(BezierCurveC[2].diff(t).subs({t:T})) for T in sorted(.5, .5±sqrt(3/5)/2),
# weighted 5/18, 8/18, 5/18 respectively.
- v0 = abs(-0.492943519233745*pt1 + 0.430331482911935*pt2 + 0.0626120363218102*pt3)
- v1 = abs(pt3-pt1)*0.4444444444444444
- v2 = abs(-0.0626120363218102*pt1 - 0.430331482911935*pt2 + 0.492943519233745*pt3)
+ v0 = abs(
+ -0.492943519233745 * pt1 + 0.430331482911935 * pt2 + 0.0626120363218102 * pt3
+ )
+ v1 = abs(pt3 - pt1) * 0.4444444444444444
+ v2 = abs(
+ -0.0626120363218102 * pt1 - 0.430331482911935 * pt2 + 0.492943519233745 * pt3
+ )
return v0 + v1 + v2
@@ -220,14 +243,18 @@ def calcQuadraticBounds(pt1, pt2, pt3):
(0.0, 0.0, 100, 100)
"""
(ax, ay), (bx, by), (cx, cy) = calcQuadraticParameters(pt1, pt2, pt3)
- ax2 = ax*2.0
- ay2 = ay*2.0
+ ax2 = ax * 2.0
+ ay2 = ay * 2.0
roots = []
if ax2 != 0:
- roots.append(-bx/ax2)
+ roots.append(-bx / ax2)
if ay2 != 0:
- roots.append(-by/ay2)
- points = [(ax*t*t + bx*t + cx, ay*t*t + by*t + cy) for t in roots if 0 <= t < 1] + [pt1, pt3]
+ roots.append(-by / ay2)
+ points = [
+ (ax * t * t + bx * t + cx, ay * t * t + by * t + cy)
+ for t in roots
+ if 0 <= t < 1
+ ] + [pt1, pt3]
return calcBounds(points)
@@ -256,7 +283,9 @@ def approximateCubicArcLength(pt1, pt2, pt3, pt4):
>>> approximateCubicArcLength((0, 0), (50, 0), (100, -50), (-50, 0)) # cusp
154.80848416537057
"""
- return approximateCubicArcLengthC(complex(*pt1), complex(*pt2), complex(*pt3), complex(*pt4))
+ return approximateCubicArcLengthC(
+ complex(*pt1), complex(*pt2), complex(*pt3), complex(*pt4)
+ )
def approximateCubicArcLengthC(pt1, pt2, pt3, pt4):
@@ -276,11 +305,21 @@ def approximateCubicArcLengthC(pt1, pt2, pt3, pt4):
# abs(BezierCurveC[3].diff(t).subs({t:T})) for T in sorted(0, .5±(3/7)**.5/2, .5, 1),
# weighted 1/20, 49/180, 32/90, 49/180, 1/20 respectively.
- v0 = abs(pt2-pt1)*.15
- v1 = abs(-0.558983582205757*pt1 + 0.325650248872424*pt2 + 0.208983582205757*pt3 + 0.024349751127576*pt4)
- v2 = abs(pt4-pt1+pt3-pt2)*0.26666666666666666
- v3 = abs(-0.024349751127576*pt1 - 0.208983582205757*pt2 - 0.325650248872424*pt3 + 0.558983582205757*pt4)
- v4 = abs(pt4-pt3)*.15
+ v0 = abs(pt2 - pt1) * 0.15
+ v1 = abs(
+ -0.558983582205757 * pt1
+ + 0.325650248872424 * pt2
+ + 0.208983582205757 * pt3
+ + 0.024349751127576 * pt4
+ )
+ v2 = abs(pt4 - pt1 + pt3 - pt2) * 0.26666666666666666
+ v3 = abs(
+ -0.024349751127576 * pt1
+ - 0.208983582205757 * pt2
+ - 0.325650248872424 * pt3
+ + 0.558983582205757 * pt4
+ )
+ v4 = abs(pt4 - pt3) * 0.15
return v0 + v1 + v2 + v3 + v4
@@ -313,7 +352,13 @@ def calcCubicBounds(pt1, pt2, pt3, pt4):
yRoots = [t for t in solveQuadratic(ay3, by2, cy) if 0 <= t < 1]
roots = xRoots + yRoots
- points = [(ax*t*t*t + bx*t*t + cx * t + dx, ay*t*t*t + by*t*t + cy * t + dy) for t in roots] + [pt1, pt4]
+ points = [
+ (
+ ax * t * t * t + bx * t * t + cx * t + dx,
+ ay * t * t * t + by * t * t + cy * t + dy,
+ )
+ for t in roots
+ ] + [pt1, pt4]
return calcBounds(points)
@@ -356,8 +401,8 @@ def splitLine(pt1, pt2, where, isHorizontal):
pt1x, pt1y = pt1
pt2x, pt2y = pt2
- ax = (pt2x - pt1x)
- ay = (pt2y - pt1y)
+ ax = pt2x - pt1x
+ ay = pt2y - pt1y
bx = pt1x
by = pt1y
@@ -410,9 +455,10 @@ def splitQuadratic(pt1, pt2, pt3, where, isHorizontal):
((50, 50), (75, 50), (100, 0))
"""
a, b, c = calcQuadraticParameters(pt1, pt2, pt3)
- solutions = solveQuadratic(a[isHorizontal], b[isHorizontal],
- c[isHorizontal] - where)
- solutions = sorted([t for t in solutions if 0 <= t < 1])
+ solutions = solveQuadratic(
+ a[isHorizontal], b[isHorizontal], c[isHorizontal] - where
+ )
+ solutions = sorted(t for t in solutions if 0 <= t < 1)
if not solutions:
return [(pt1, pt2, pt3)]
return _splitQuadraticAtT(a, b, c, *solutions)
@@ -446,9 +492,10 @@ def splitCubic(pt1, pt2, pt3, pt4, where, isHorizontal):
((92.5259, 25), (95.202, 17.5085), (97.7062, 9.17517), (100, 1.77636e-15))
"""
a, b, c, d = calcCubicParameters(pt1, pt2, pt3, pt4)
- solutions = solveCubic(a[isHorizontal], b[isHorizontal], c[isHorizontal],
- d[isHorizontal] - where)
- solutions = sorted([t for t in solutions if 0 <= t < 1])
+ solutions = solveCubic(
+ a[isHorizontal], b[isHorizontal], c[isHorizontal], d[isHorizontal] - where
+ )
+ solutions = sorted(t for t in solutions if 0 <= t < 1)
if not solutions:
return [(pt1, pt2, pt3, pt4)]
return _splitCubicAtT(a, b, c, d, *solutions)
@@ -512,17 +559,17 @@ def _splitQuadraticAtT(a, b, c, *ts):
cx, cy = c
for i in range(len(ts) - 1):
t1 = ts[i]
- t2 = ts[i+1]
- delta = (t2 - t1)
+ t2 = ts[i + 1]
+ delta = t2 - t1
# calc new a, b and c
- delta_2 = delta*delta
+ delta_2 = delta * delta
a1x = ax * delta_2
a1y = ay * delta_2
- b1x = (2*ax*t1 + bx) * delta
- b1y = (2*ay*t1 + by) * delta
- t1_2 = t1*t1
- c1x = ax*t1_2 + bx*t1 + cx
- c1y = ay*t1_2 + by*t1 + cy
+ b1x = (2 * ax * t1 + bx) * delta
+ b1y = (2 * ay * t1 + by) * delta
+ t1_2 = t1 * t1
+ c1x = ax * t1_2 + bx * t1 + cx
+ c1y = ay * t1_2 + by * t1 + cy
pt1, pt2, pt3 = calcQuadraticPoints((a1x, a1y), (b1x, b1y), (c1x, c1y))
segments.append((pt1, pt2, pt3))
@@ -540,24 +587,26 @@ def _splitCubicAtT(a, b, c, d, *ts):
dx, dy = d
for i in range(len(ts) - 1):
t1 = ts[i]
- t2 = ts[i+1]
- delta = (t2 - t1)
+ t2 = ts[i + 1]
+ delta = t2 - t1
- delta_2 = delta*delta
- delta_3 = delta*delta_2
- t1_2 = t1*t1
- t1_3 = t1*t1_2
+ delta_2 = delta * delta
+ delta_3 = delta * delta_2
+ t1_2 = t1 * t1
+ t1_3 = t1 * t1_2
# calc new a, b, c and d
a1x = ax * delta_3
a1y = ay * delta_3
- b1x = (3*ax*t1 + bx) * delta_2
- b1y = (3*ay*t1 + by) * delta_2
- c1x = (2*bx*t1 + cx + 3*ax*t1_2) * delta
- c1y = (2*by*t1 + cy + 3*ay*t1_2) * delta
- d1x = ax*t1_3 + bx*t1_2 + cx*t1 + dx
- d1y = ay*t1_3 + by*t1_2 + cy*t1 + dy
- pt1, pt2, pt3, pt4 = calcCubicPoints((a1x, a1y), (b1x, b1y), (c1x, c1y), (d1x, d1y))
+ b1x = (3 * ax * t1 + bx) * delta_2
+ b1y = (3 * ay * t1 + by) * delta_2
+ c1x = (2 * bx * t1 + cx + 3 * ax * t1_2) * delta
+ c1y = (2 * by * t1 + cy + 3 * ay * t1_2) * delta
+ d1x = ax * t1_3 + bx * t1_2 + cx * t1 + dx
+ d1y = ay * t1_3 + by * t1_2 + cy * t1 + dy
+ pt1, pt2, pt3, pt4 = calcCubicPoints(
+ (a1x, a1y), (b1x, b1y), (c1x, c1y), (d1x, d1y)
+ )
segments.append((pt1, pt2, pt3, pt4))
return segments
@@ -569,8 +618,7 @@ def _splitCubicAtT(a, b, c, d, *ts):
from math import sqrt, acos, cos, pi
-def solveQuadratic(a, b, c,
- sqrt=sqrt):
+def solveQuadratic(a, b, c, sqrt=sqrt):
"""Solve a quadratic equation.
Solves *a*x*x + b*x + c = 0* where a, b and c are real.
@@ -590,13 +638,13 @@ def solveQuadratic(a, b, c,
roots = []
else:
# We have a linear equation with 1 root.
- roots = [-c/b]
+ roots = [-c / b]
else:
# We have a true quadratic equation. Apply the quadratic formula to find two roots.
- DD = b*b - 4.0*a*c
+ DD = b * b - 4.0 * a * c
if DD >= 0.0:
rDD = sqrt(DD)
- roots = [(-b+rDD)/2.0/a, (-b-rDD)/2.0/a]
+ roots = [(-b + rDD) / 2.0 / a, (-b - rDD) / 2.0 / a]
else:
# complex roots, ignore
roots = []
@@ -646,52 +694,52 @@ def solveCubic(a, b, c, d):
# returns unreliable results, so we fall back to quad.
return solveQuadratic(b, c, d)
a = float(a)
- a1 = b/a
- a2 = c/a
- a3 = d/a
+ a1 = b / a
+ a2 = c / a
+ a3 = d / a
- Q = (a1*a1 - 3.0*a2)/9.0
- R = (2.0*a1*a1*a1 - 9.0*a1*a2 + 27.0*a3)/54.0
+ Q = (a1 * a1 - 3.0 * a2) / 9.0
+ R = (2.0 * a1 * a1 * a1 - 9.0 * a1 * a2 + 27.0 * a3) / 54.0
- R2 = R*R
- Q3 = Q*Q*Q
+ R2 = R * R
+ Q3 = Q * Q * Q
R2 = 0 if R2 < epsilon else R2
Q3 = 0 if abs(Q3) < epsilon else Q3
R2_Q3 = R2 - Q3
- if R2 == 0. and Q3 == 0.:
- x = round(-a1/3.0, epsilonDigits)
+ if R2 == 0.0 and Q3 == 0.0:
+ x = round(-a1 / 3.0, epsilonDigits)
return [x, x, x]
- elif R2_Q3 <= epsilon * .5:
+ elif R2_Q3 <= epsilon * 0.5:
# The epsilon * .5 above ensures that Q3 is not zero.
- theta = acos(max(min(R/sqrt(Q3), 1.0), -1.0))
- rQ2 = -2.0*sqrt(Q)
- a1_3 = a1/3.0
- x0 = rQ2*cos(theta/3.0) - a1_3
- x1 = rQ2*cos((theta+2.0*pi)/3.0) - a1_3
- x2 = rQ2*cos((theta+4.0*pi)/3.0) - a1_3
+ theta = acos(max(min(R / sqrt(Q3), 1.0), -1.0))
+ rQ2 = -2.0 * sqrt(Q)
+ a1_3 = a1 / 3.0
+ x0 = rQ2 * cos(theta / 3.0) - a1_3
+ x1 = rQ2 * cos((theta + 2.0 * pi) / 3.0) - a1_3
+ x2 = rQ2 * cos((theta + 4.0 * pi) / 3.0) - a1_3
x0, x1, x2 = sorted([x0, x1, x2])
# Merge roots that are close-enough
if x1 - x0 < epsilon and x2 - x1 < epsilon:
- x0 = x1 = x2 = round((x0 + x1 + x2) / 3., epsilonDigits)
+ x0 = x1 = x2 = round((x0 + x1 + x2) / 3.0, epsilonDigits)
elif x1 - x0 < epsilon:
- x0 = x1 = round((x0 + x1) / 2., epsilonDigits)
+ x0 = x1 = round((x0 + x1) / 2.0, epsilonDigits)
x2 = round(x2, epsilonDigits)
elif x2 - x1 < epsilon:
x0 = round(x0, epsilonDigits)
- x1 = x2 = round((x1 + x2) / 2., epsilonDigits)
+ x1 = x2 = round((x1 + x2) / 2.0, epsilonDigits)
else:
x0 = round(x0, epsilonDigits)
x1 = round(x1, epsilonDigits)
x2 = round(x2, epsilonDigits)
return [x0, x1, x2]
else:
- x = pow(sqrt(R2_Q3)+abs(R), 1/3.0)
- x = x + Q/x
+ x = pow(sqrt(R2_Q3) + abs(R), 1 / 3.0)
+ x = x + Q / x
if R >= 0.0:
x = -x
- x = round(x - a1/3.0, epsilonDigits)
+ x = round(x - a1 / 3.0, epsilonDigits)
return [x]
@@ -699,6 +747,7 @@ def solveCubic(a, b, c, d):
# Conversion routines for points to parameters and vice versa
#
+
def calcQuadraticParameters(pt1, pt2, pt3):
x2, y2 = pt2
x3, y3 = pt3
@@ -753,17 +802,406 @@ def calcCubicPoints(a, b, c, d):
return (x1, y1), (x2, y2), (x3, y3), (x4, y4)
+#
+# Point at time
+#
+
+
+def linePointAtT(pt1, pt2, t):
+ """Finds the point at time `t` on a line.
+
+ Args:
+ pt1, pt2: Coordinates of the line as 2D tuples.
+ t: The time along the line.
+
+ Returns:
+ A 2D tuple with the coordinates of the point.
+ """
+ return ((pt1[0] * (1 - t) + pt2[0] * t), (pt1[1] * (1 - t) + pt2[1] * t))
+
+
+def quadraticPointAtT(pt1, pt2, pt3, t):
+ """Finds the point at time `t` on a quadratic curve.
+
+ Args:
+ pt1, pt2, pt3: Coordinates of the curve as 2D tuples.
+ t: The time along the curve.
+
+ Returns:
+ A 2D tuple with the coordinates of the point.
+ """
+ x = (1 - t) * (1 - t) * pt1[0] + 2 * (1 - t) * t * pt2[0] + t * t * pt3[0]
+ y = (1 - t) * (1 - t) * pt1[1] + 2 * (1 - t) * t * pt2[1] + t * t * pt3[1]
+ return (x, y)
+
+
+def cubicPointAtT(pt1, pt2, pt3, pt4, t):
+ """Finds the point at time `t` on a cubic curve.
+
+ Args:
+ pt1, pt2, pt3, pt4: Coordinates of the curve as 2D tuples.
+ t: The time along the curve.
+
+ Returns:
+ A 2D tuple with the coordinates of the point.
+ """
+ x = (
+ (1 - t) * (1 - t) * (1 - t) * pt1[0]
+ + 3 * (1 - t) * (1 - t) * t * pt2[0]
+ + 3 * (1 - t) * t * t * pt3[0]
+ + t * t * t * pt4[0]
+ )
+ y = (
+ (1 - t) * (1 - t) * (1 - t) * pt1[1]
+ + 3 * (1 - t) * (1 - t) * t * pt2[1]
+ + 3 * (1 - t) * t * t * pt3[1]
+ + t * t * t * pt4[1]
+ )
+ return (x, y)
+
+
+def segmentPointAtT(seg, t):
+ if len(seg) == 2:
+ return linePointAtT(*seg, t)
+ elif len(seg) == 3:
+ return quadraticPointAtT(*seg, t)
+ elif len(seg) == 4:
+ return cubicPointAtT(*seg, t)
+ raise ValueError("Unknown curve degree")
+
+
+#
+# Intersection finders
+#
+
+
+def _line_t_of_pt(s, e, pt):
+ sx, sy = s
+ ex, ey = e
+ px, py = pt
+ if not math.isclose(sx, ex):
+ return (px - sx) / (ex - sx)
+ if not math.isclose(sy, ey):
+ return (py - sy) / (ey - sy)
+ # Line is a point!
+ return -1
+
+
+def _both_points_are_on_same_side_of_origin(a, b, origin):
+ xDiff = (a[0] - origin[0]) * (b[0] - origin[0])
+ yDiff = (a[1] - origin[1]) * (b[1] - origin[1])
+ return not (xDiff <= 0.0 and yDiff <= 0.0)
+
+
+def lineLineIntersections(s1, e1, s2, e2):
+ """Finds intersections between two line segments.
+
+ Args:
+ s1, e1: Coordinates of the first line as 2D tuples.
+ s2, e2: Coordinates of the second line as 2D tuples.
+
+ Returns:
+ A list of ``Intersection`` objects, each object having ``pt``, ``t1``
+ and ``t2`` attributes containing the intersection point, time on first
+ segment and time on second segment respectively.
+
+ Examples::
+
+ >>> a = lineLineIntersections( (310,389), (453, 222), (289, 251), (447, 367))
+ >>> len(a)
+ 1
+ >>> intersection = a[0]
+ >>> intersection.pt
+ (374.44882952482897, 313.73458370177315)
+ >>> (intersection.t1, intersection.t2)
+ (0.45069111555824454, 0.5408153767394238)
+ """
+ s1x, s1y = s1
+ e1x, e1y = e1
+ s2x, s2y = s2
+ e2x, e2y = e2
+ if (
+ math.isclose(s2x, e2x) and math.isclose(s1x, e1x) and not math.isclose(s1x, s2x)
+ ): # Parallel vertical
+ return []
+ if (
+ math.isclose(s2y, e2y) and math.isclose(s1y, e1y) and not math.isclose(s1y, s2y)
+ ): # Parallel horizontal
+ return []
+ if math.isclose(s2x, e2x) and math.isclose(s2y, e2y): # Line segment is tiny
+ return []
+ if math.isclose(s1x, e1x) and math.isclose(s1y, e1y): # Line segment is tiny
+ return []
+ if math.isclose(e1x, s1x):
+ x = s1x
+ slope34 = (e2y - s2y) / (e2x - s2x)
+ y = slope34 * (x - s2x) + s2y
+ pt = (x, y)
+ return [
+ Intersection(
+ pt=pt, t1=_line_t_of_pt(s1, e1, pt), t2=_line_t_of_pt(s2, e2, pt)
+ )
+ ]
+ if math.isclose(s2x, e2x):
+ x = s2x
+ slope12 = (e1y - s1y) / (e1x - s1x)
+ y = slope12 * (x - s1x) + s1y
+ pt = (x, y)
+ return [
+ Intersection(
+ pt=pt, t1=_line_t_of_pt(s1, e1, pt), t2=_line_t_of_pt(s2, e2, pt)
+ )
+ ]
+
+ slope12 = (e1y - s1y) / (e1x - s1x)
+ slope34 = (e2y - s2y) / (e2x - s2x)
+ if math.isclose(slope12, slope34):
+ return []
+ x = (slope12 * s1x - s1y - slope34 * s2x + s2y) / (slope12 - slope34)
+ y = slope12 * (x - s1x) + s1y
+ pt = (x, y)
+ if _both_points_are_on_same_side_of_origin(
+ pt, e1, s1
+ ) and _both_points_are_on_same_side_of_origin(pt, s2, e2):
+ return [
+ Intersection(
+ pt=pt, t1=_line_t_of_pt(s1, e1, pt), t2=_line_t_of_pt(s2, e2, pt)
+ )
+ ]
+ return []
+
+
+def _alignment_transformation(segment):
+ # Returns a transformation which aligns a segment horizontally at the
+ # origin. Apply this transformation to curves and root-find to find
+ # intersections with the segment.
+ start = segment[0]
+ end = segment[-1]
+ angle = math.atan2(end[1] - start[1], end[0] - start[0])
+ return Identity.rotate(-angle).translate(-start[0], -start[1])
+
+
+def _curve_line_intersections_t(curve, line):
+ aligned_curve = _alignment_transformation(line).transformPoints(curve)
+ if len(curve) == 3:
+ a, b, c = calcQuadraticParameters(*aligned_curve)
+ intersections = solveQuadratic(a[1], b[1], c[1])
+ elif len(curve) == 4:
+ a, b, c, d = calcCubicParameters(*aligned_curve)
+ intersections = solveCubic(a[1], b[1], c[1], d[1])
+ else:
+ raise ValueError("Unknown curve degree")
+ return sorted(i for i in intersections if 0.0 <= i <= 1)
+
+
+def curveLineIntersections(curve, line):
+ """Finds intersections between a curve and a line.
+
+ Args:
+ curve: List of coordinates of the curve segment as 2D tuples.
+ line: List of coordinates of the line segment as 2D tuples.
+
+ Returns:
+ A list of ``Intersection`` objects, each object having ``pt``, ``t1``
+ and ``t2`` attributes containing the intersection point, time on first
+ segment and time on second segment respectively.
+
+ Examples::
+ >>> curve = [ (100, 240), (30, 60), (210, 230), (160, 30) ]
+ >>> line = [ (25, 260), (230, 20) ]
+ >>> intersections = curveLineIntersections(curve, line)
+ >>> len(intersections)
+ 3
+ >>> intersections[0].pt
+ (84.90010344084885, 189.87306176459828)
+ """
+ if len(curve) == 3:
+ pointFinder = quadraticPointAtT
+ elif len(curve) == 4:
+ pointFinder = cubicPointAtT
+ else:
+ raise ValueError("Unknown curve degree")
+ intersections = []
+ for t in _curve_line_intersections_t(curve, line):
+ pt = pointFinder(*curve, t)
+ intersections.append(Intersection(pt=pt, t1=t, t2=_line_t_of_pt(*line, pt)))
+ return intersections
+
+
+def _curve_bounds(c):
+ if len(c) == 3:
+ return calcQuadraticBounds(*c)
+ elif len(c) == 4:
+ return calcCubicBounds(*c)
+ raise ValueError("Unknown curve degree")
+
+
+def _split_segment_at_t(c, t):
+ if len(c) == 2:
+ s, e = c
+ midpoint = linePointAtT(s, e, t)
+ return [(s, midpoint), (midpoint, e)]
+ if len(c) == 3:
+ return splitQuadraticAtT(*c, t)
+ elif len(c) == 4:
+ return splitCubicAtT(*c, t)
+ raise ValueError("Unknown curve degree")
+
+
+def _curve_curve_intersections_t(
+ curve1, curve2, precision=1e-3, range1=None, range2=None
+):
+ bounds1 = _curve_bounds(curve1)
+ bounds2 = _curve_bounds(curve2)
+
+ if not range1:
+ range1 = (0.0, 1.0)
+ if not range2:
+ range2 = (0.0, 1.0)
+
+ # If bounds don't intersect, go home
+ intersects, _ = sectRect(bounds1, bounds2)
+ if not intersects:
+ return []
+
+ def midpoint(r):
+ return 0.5 * (r[0] + r[1])
+
+ # If they do overlap but they're tiny, approximate
+ if rectArea(bounds1) < precision and rectArea(bounds2) < precision:
+ return [(midpoint(range1), midpoint(range2))]
+
+ c11, c12 = _split_segment_at_t(curve1, 0.5)
+ c11_range = (range1[0], midpoint(range1))
+ c12_range = (midpoint(range1), range1[1])
+
+ c21, c22 = _split_segment_at_t(curve2, 0.5)
+ c21_range = (range2[0], midpoint(range2))
+ c22_range = (midpoint(range2), range2[1])
+
+ found = []
+ found.extend(
+ _curve_curve_intersections_t(
+ c11, c21, precision, range1=c11_range, range2=c21_range
+ )
+ )
+ found.extend(
+ _curve_curve_intersections_t(
+ c12, c21, precision, range1=c12_range, range2=c21_range
+ )
+ )
+ found.extend(
+ _curve_curve_intersections_t(
+ c11, c22, precision, range1=c11_range, range2=c22_range
+ )
+ )
+ found.extend(
+ _curve_curve_intersections_t(
+ c12, c22, precision, range1=c12_range, range2=c22_range
+ )
+ )
+
+ unique_key = lambda ts: (int(ts[0] / precision), int(ts[1] / precision))
+ seen = set()
+ unique_values = []
+
+ for ts in found:
+ key = unique_key(ts)
+ if key in seen:
+ continue
+ seen.add(key)
+ unique_values.append(ts)
+
+ return unique_values
+
+
+def curveCurveIntersections(curve1, curve2):
+ """Finds intersections between a curve and a curve.
+
+ Args:
+ curve1: List of coordinates of the first curve segment as 2D tuples.
+ curve2: List of coordinates of the second curve segment as 2D tuples.
+
+ Returns:
+ A list of ``Intersection`` objects, each object having ``pt``, ``t1``
+ and ``t2`` attributes containing the intersection point, time on first
+ segment and time on second segment respectively.
+
+ Examples::
+ >>> curve1 = [ (10,100), (90,30), (40,140), (220,220) ]
+ >>> curve2 = [ (5,150), (180,20), (80,250), (210,190) ]
+ >>> intersections = curveCurveIntersections(curve1, curve2)
+ >>> len(intersections)
+ 3
+ >>> intersections[0].pt
+ (81.7831487395506, 109.88904552375288)
+ """
+ intersection_ts = _curve_curve_intersections_t(curve1, curve2)
+ return [
+ Intersection(pt=segmentPointAtT(curve1, ts[0]), t1=ts[0], t2=ts[1])
+ for ts in intersection_ts
+ ]
+
+
+def segmentSegmentIntersections(seg1, seg2):
+ """Finds intersections between two segments.
+
+ Args:
+ seg1: List of coordinates of the first segment as 2D tuples.
+ seg2: List of coordinates of the second segment as 2D tuples.
+
+ Returns:
+ A list of ``Intersection`` objects, each object having ``pt``, ``t1``
+ and ``t2`` attributes containing the intersection point, time on first
+ segment and time on second segment respectively.
+
+ Examples::
+ >>> curve1 = [ (10,100), (90,30), (40,140), (220,220) ]
+ >>> curve2 = [ (5,150), (180,20), (80,250), (210,190) ]
+ >>> intersections = segmentSegmentIntersections(curve1, curve2)
+ >>> len(intersections)
+ 3
+ >>> intersections[0].pt
+ (81.7831487395506, 109.88904552375288)
+ >>> curve3 = [ (100, 240), (30, 60), (210, 230), (160, 30) ]
+ >>> line = [ (25, 260), (230, 20) ]
+ >>> intersections = segmentSegmentIntersections(curve3, line)
+ >>> len(intersections)
+ 3
+ >>> intersections[0].pt
+ (84.90010344084885, 189.87306176459828)
+
+ """
+ # Arrange by degree
+ swapped = False
+ if len(seg2) > len(seg1):
+ seg2, seg1 = seg1, seg2
+ swapped = True
+ if len(seg1) > 2:
+ if len(seg2) > 2:
+ intersections = curveCurveIntersections(seg1, seg2)
+ else:
+ intersections = curveLineIntersections(seg1, seg2)
+ elif len(seg1) == 2 and len(seg2) == 2:
+ intersections = lineLineIntersections(*seg1, *seg2)
+ else:
+ raise ValueError("Couldn't work out which intersection function to use")
+ if not swapped:
+ return intersections
+ return [Intersection(pt=i.pt, t1=i.t2, t2=i.t1) for i in intersections]
+
+
def _segmentrepr(obj):
"""
- >>> _segmentrepr([1, [2, 3], [], [[2, [3, 4], [0.1, 2.2]]]])
- '(1, (2, 3), (), ((2, (3, 4), (0.1, 2.2))))'
+ >>> _segmentrepr([1, [2, 3], [], [[2, [3, 4], [0.1, 2.2]]]])
+ '(1, (2, 3), (), ((2, (3, 4), (0.1, 2.2))))'
"""
try:
it = iter(obj)
except TypeError:
return "%g" % obj
else:
- return "(%s)" % ", ".join([_segmentrepr(x) for x in it])
+ return "(%s)" % ", ".join(_segmentrepr(x) for x in it)
def printSegments(segments):
@@ -773,7 +1211,9 @@ def printSegments(segments):
for segment in segments:
print(_segmentrepr(segment))
+
if __name__ == "__main__":
import sys
import doctest
+
sys.exit(doctest.testmod().failed)
diff --git a/Lib/fontTools/misc/classifyTools.py b/Lib/fontTools/misc/classifyTools.py
index 73101186..ae88a8f7 100644
--- a/Lib/fontTools/misc/classifyTools.py
+++ b/Lib/fontTools/misc/classifyTools.py
@@ -1,7 +1,6 @@
""" fontTools.misc.classifyTools.py -- tools for classifying things.
"""
-from fontTools.misc.py23 import *
class Classifier(object):
diff --git a/Lib/fontTools/misc/cliTools.py b/Lib/fontTools/misc/cliTools.py
index 4e5353b9..e8c17677 100644
--- a/Lib/fontTools/misc/cliTools.py
+++ b/Lib/fontTools/misc/cliTools.py
@@ -1,5 +1,4 @@
"""Collection of utilities for command-line interfaces and console scripts."""
-from fontTools.misc.py23 import *
import os
import re
diff --git a/Lib/fontTools/misc/dictTools.py b/Lib/fontTools/misc/dictTools.py
index 47528684..ae7932c9 100644
--- a/Lib/fontTools/misc/dictTools.py
+++ b/Lib/fontTools/misc/dictTools.py
@@ -1,6 +1,5 @@
"""Misc dict tools."""
-from fontTools.misc.py23 import *
__all__ = ['hashdict']
diff --git a/Lib/fontTools/misc/eexec.py b/Lib/fontTools/misc/eexec.py
index 36719a1c..71f733c1 100644
--- a/Lib/fontTools/misc/eexec.py
+++ b/Lib/fontTools/misc/eexec.py
@@ -12,7 +12,8 @@ the new key at the end of the operation.
"""
-from fontTools.misc.py23 import *
+from fontTools.misc.py23 import bytechr, bytesjoin, byteord
+
def _decryptChar(cipher, R):
cipher = byteord(cipher)
diff --git a/Lib/fontTools/misc/encodingTools.py b/Lib/fontTools/misc/encodingTools.py
index 438e484d..eccf951d 100644
--- a/Lib/fontTools/misc/encodingTools.py
+++ b/Lib/fontTools/misc/encodingTools.py
@@ -1,7 +1,6 @@
"""fontTools.misc.encodingTools.py -- tools for working with OpenType encodings.
"""
-from fontTools.misc.py23 import *
import fontTools.encodings.codecs
# Map keyed by platformID, then platEncID, then possibly langID
diff --git a/Lib/fontTools/misc/etree.py b/Lib/fontTools/misc/etree.py
index 2338f099..6e943e4b 100644
--- a/Lib/fontTools/misc/etree.py
+++ b/Lib/fontTools/misc/etree.py
@@ -11,7 +11,7 @@ or subclasses built-in ElementTree classes to add features that are
only availble in lxml, like OrderedDict for attributes, pretty_print and
iterwalk.
"""
-from fontTools.misc.py23 import basestring, unicode, tounicode, open
+from fontTools.misc.py23 import unicode, tostr
XML_DECLARATION = """<?xml version='1.0' encoding='%s'?>"""
@@ -242,7 +242,7 @@ except ImportError:
Reject all bytes input that contains non-ASCII characters.
"""
try:
- s = tounicode(s, encoding="ascii", errors="strict")
+ s = tostr(s, encoding="ascii", errors="strict")
except UnicodeDecodeError:
raise ValueError(
"Bytes strings can only contain ASCII characters. "
@@ -356,7 +356,7 @@ except ImportError:
if isinstance(tag, QName):
if tag.text not in qnames:
add_qname(tag.text)
- elif isinstance(tag, basestring):
+ elif isinstance(tag, str):
if tag not in qnames:
add_qname(tag)
elif tag is not None and tag is not Comment and tag is not PI:
diff --git a/Lib/fontTools/misc/filenames.py b/Lib/fontTools/misc/filenames.py
index f7eb247a..0f010008 100644
--- a/Lib/fontTools/misc/filenames.py
+++ b/Lib/fontTools/misc/filenames.py
@@ -16,7 +16,7 @@ by Tal Leming and is copyright (c) 2005-2016, The RoboFab Developers:
- Tal Leming
- Just van Rossum
"""
-from fontTools.misc.py23 import basestring, unicode
+
illegalCharacters = r"\" * + / : < > ? [ \ ] | \0".split(" ")
illegalCharacters += [chr(i) for i in range(1, 32)]
@@ -95,9 +95,9 @@ def userNameToFileName(userName, existing=[], prefix="", suffix=""):
>>> userNameToFileName("alt.con") == "alt._con"
True
"""
- # the incoming name must be a unicode string
- if not isinstance(userName, unicode):
- raise ValueError("The value for userName must be a unicode string.")
+ # the incoming name must be a str
+ if not isinstance(userName, str):
+ raise ValueError("The value for userName must be a string.")
# establish the prefix and suffix lengths
prefixLength = len(prefix)
suffixLength = len(suffix)
diff --git a/Lib/fontTools/misc/fixedTools.py b/Lib/fontTools/misc/fixedTools.py
index 931b665e..f0474abf 100644
--- a/Lib/fontTools/misc/fixedTools.py
+++ b/Lib/fontTools/misc/fixedTools.py
@@ -17,15 +17,13 @@ functions for converting between fixed-point, float and string representations.
The maximum value that can still fit in an F2Dot14. (1.99993896484375)
"""
-from fontTools.misc.py23 import *
-import math
+from .roundTools import otRound
import logging
log = logging.getLogger(__name__)
__all__ = [
"MAX_F2DOT14",
- "otRound",
"fixedToFloat",
"floatToFixed",
"floatToFixedToFloat",
@@ -41,30 +39,6 @@ __all__ = [
MAX_F2DOT14 = 0x7FFF / (1 << 14)
-def otRound(value):
- """Round float value to nearest integer towards ``+Infinity``.
-
- The OpenType spec (in the section on `"normalization" of OpenType Font Variations <https://docs.microsoft.com/en-us/typography/opentype/spec/otvaroverview#coordinate-scales-and-normalization>`_)
- defines the required method for converting floating point values to
- fixed-point. In particular it specifies the following rounding strategy:
-
- for fractional values of 0.5 and higher, take the next higher integer;
- for other fractional values, truncate.
-
- This function rounds the floating-point value according to this strategy
- in preparation for conversion to fixed-point.
-
- Args:
- value (float): The input floating-point value.
-
- Returns
- float: The rounded value.
- """
- # See this thread for how we ended up with this implementation:
- # https://github.com/fonttools/fonttools/issues/1248#issuecomment-383198166
- return int(math.floor(value + 0.5))
-
-
def fixedToFloat(value, precisionBits):
"""Converts a fixed-point number to a float given the number of
precision bits.
diff --git a/Lib/fontTools/misc/intTools.py b/Lib/fontTools/misc/intTools.py
index 9f4497ba..448e1627 100644
--- a/Lib/fontTools/misc/intTools.py
+++ b/Lib/fontTools/misc/intTools.py
@@ -1,5 +1,3 @@
-from fontTools.misc.py23 import *
-
__all__ = ['popCount']
diff --git a/Lib/fontTools/misc/loggingTools.py b/Lib/fontTools/misc/loggingTools.py
index 3281d429..d1baa839 100644
--- a/Lib/fontTools/misc/loggingTools.py
+++ b/Lib/fontTools/misc/loggingTools.py
@@ -1,4 +1,3 @@
-from fontTools.misc.py23 import *
import sys
import logging
import timeit
@@ -60,7 +59,7 @@ class LevelFormatter(logging.Formatter):
"only '%' percent style is supported in both python 2 and 3")
if fmt is None:
fmt = DEFAULT_FORMATS
- if isinstance(fmt, basestring):
+ if isinstance(fmt, str):
default_format = fmt
custom_formats = {}
elif isinstance(fmt, Mapping):
@@ -151,7 +150,7 @@ def configLogger(**kwargs):
handlers = [h]
# By default, the top-level library logger is configured.
logger = kwargs.pop("logger", "fontTools")
- if not logger or isinstance(logger, basestring):
+ if not logger or isinstance(logger, str):
# empty "" or None means the 'root' logger
logger = logging.getLogger(logger)
# before (re)configuring, reset named logger and its children (if exist)
@@ -436,7 +435,7 @@ class CapturingLogHandler(logging.Handler):
def __init__(self, logger, level):
super(CapturingLogHandler, self).__init__(level=level)
self.records = []
- if isinstance(logger, basestring):
+ if isinstance(logger, str):
self.logger = logging.getLogger(logger)
else:
self.logger = logger
diff --git a/Lib/fontTools/misc/macCreatorType.py b/Lib/fontTools/misc/macCreatorType.py
index c28ceb98..fb237200 100644
--- a/Lib/fontTools/misc/macCreatorType.py
+++ b/Lib/fontTools/misc/macCreatorType.py
@@ -1,5 +1,4 @@
-from fontTools.misc.py23 import *
-import sys
+from fontTools.misc.py23 import Tag, bytesjoin, strjoin
try:
import xattr
except ImportError:
diff --git a/Lib/fontTools/misc/macRes.py b/Lib/fontTools/misc/macRes.py
index 0053ee3a..2c15b347 100644
--- a/Lib/fontTools/misc/macRes.py
+++ b/Lib/fontTools/misc/macRes.py
@@ -1,4 +1,5 @@
-from fontTools.misc.py23 import *
+from fontTools.misc.py23 import bytesjoin, tostr
+from io import BytesIO
import struct
from fontTools.misc import sstruct
from collections import OrderedDict
diff --git a/Lib/fontTools/misc/plistlib/__init__.py b/Lib/fontTools/misc/plistlib/__init__.py
index d8391041..84dc4183 100644
--- a/Lib/fontTools/misc/plistlib/__init__.py
+++ b/Lib/fontTools/misc/plistlib/__init__.py
@@ -1,5 +1,4 @@
import collections.abc
-import sys
import re
from typing import (
Any,
@@ -24,10 +23,8 @@ from functools import singledispatch
from fontTools.misc import etree
-from fontTools.misc.py23 import (
- tounicode,
- tobytes,
-)
+from fontTools.misc.py23 import tostr
+
# By default, we
# - deserialize <data> elements as bytes and
@@ -368,7 +365,7 @@ def _dict_element(d: Mapping[str, PlistEncodable], ctx: SimpleNamespace) -> etre
continue
raise TypeError("keys must be strings")
k = etree.SubElement(el, "key")
- k.text = tounicode(key, "utf-8")
+ k.text = tostr(key, "utf-8")
el.append(_make_element(value, ctx))
ctx.indent_level -= 1
return el
diff --git a/Lib/fontTools/misc/psCharStrings.py b/Lib/fontTools/misc/psCharStrings.py
index 5f1427d0..cb675050 100644
--- a/Lib/fontTools/misc/psCharStrings.py
+++ b/Lib/fontTools/misc/psCharStrings.py
@@ -2,7 +2,7 @@
CFF dictionary data and Type1/Type2 CharStrings.
"""
-from fontTools.misc.py23 import *
+from fontTools.misc.py23 import bytechr, byteord, bytesjoin, strjoin
from fontTools.misc.fixedTools import (
fixedToFloat, floatToFixed, floatToFixedToStr, strToFixedToFloat,
)
@@ -997,7 +997,7 @@ class T2CharString(object):
# If present, remove return and endchar operators.
if program and program[-1] in ("return", "endchar"):
program = program[:-1]
- elif program and not isinstance(program[-1], basestring):
+ elif program and not isinstance(program[-1], str):
raise CharStringCompileError(
"T2CharString or Subr has items on the stack after last operator."
)
@@ -1010,7 +1010,7 @@ class T2CharString(object):
while i < end:
token = program[i]
i = i + 1
- if isinstance(token, basestring):
+ if isinstance(token, str):
try:
bytecode.extend(bytechr(b) for b in opcodes[token])
except KeyError:
@@ -1043,8 +1043,7 @@ class T2CharString(object):
self.program = None
def getToken(self, index,
- len=len, byteord=byteord, basestring=basestring,
- isinstance=isinstance):
+ len=len, byteord=byteord, isinstance=isinstance):
if self.bytecode is not None:
if index >= len(self.bytecode):
return None, 0, 0
@@ -1057,7 +1056,7 @@ class T2CharString(object):
return None, 0, 0
token = self.program[index]
index = index + 1
- isOperator = isinstance(token, basestring)
+ isOperator = isinstance(token, str)
return token, isOperator, index
def getBytes(self, index, nBytes):
diff --git a/Lib/fontTools/misc/psLib.py b/Lib/fontTools/misc/psLib.py
index e4748302..916755ce 100644
--- a/Lib/fontTools/misc/psLib.py
+++ b/Lib/fontTools/misc/psLib.py
@@ -1,6 +1,21 @@
-from fontTools.misc.py23 import *
+from fontTools.misc.py23 import bytechr, byteord, bytesjoin, tobytes, tostr
from fontTools.misc import eexec
-from .psOperators import *
+from .psOperators import (
+ PSOperators,
+ ps_StandardEncoding,
+ ps_array,
+ ps_boolean,
+ ps_dict,
+ ps_integer,
+ ps_literal,
+ ps_mark,
+ ps_name,
+ ps_operator,
+ ps_procedure,
+ ps_procmark,
+ ps_real,
+ ps_string,
+)
import re
from collections.abc import Callable
from string import whitespace
diff --git a/Lib/fontTools/misc/psOperators.py b/Lib/fontTools/misc/psOperators.py
index de278fd6..3b378f59 100644
--- a/Lib/fontTools/misc/psOperators.py
+++ b/Lib/fontTools/misc/psOperators.py
@@ -1,5 +1,3 @@
-from fontTools.misc.py23 import *
-
_accessstrings = {0: "", 1: "readonly", 2: "executeonly", 3: "noaccess"}
diff --git a/Lib/fontTools/misc/py23.py b/Lib/fontTools/misc/py23.py
index bced8007..9096e2ef 100644
--- a/Lib/fontTools/misc/py23.py
+++ b/Lib/fontTools/misc/py23.py
@@ -9,7 +9,7 @@ from io import StringIO as UnicodeIO
from types import SimpleNamespace
warnings.warn(
- "The py23 module has been deprecated and will be removed in the next release. "
+ "The py23 module has been deprecated and will be removed in a future release. "
"Please update your code.",
DeprecationWarning,
)
diff --git a/Lib/fontTools/misc/roundTools.py b/Lib/fontTools/misc/roundTools.py
new file mode 100644
index 00000000..c1d546f1
--- /dev/null
+++ b/Lib/fontTools/misc/roundTools.py
@@ -0,0 +1,58 @@
+"""
+Various round-to-integer helpers.
+"""
+
+import math
+import functools
+import logging
+
+log = logging.getLogger(__name__)
+
+__all__ = [
+ "noRound",
+ "otRound",
+ "maybeRound",
+ "roundFunc",
+]
+
+def noRound(value):
+ return value
+
+def otRound(value):
+ """Round float value to nearest integer towards ``+Infinity``.
+
+ The OpenType spec (in the section on `"normalization" of OpenType Font Variations <https://docs.microsoft.com/en-us/typography/opentype/spec/otvaroverview#coordinate-scales-and-normalization>`_)
+ defines the required method for converting floating point values to
+ fixed-point. In particular it specifies the following rounding strategy:
+
+ for fractional values of 0.5 and higher, take the next higher integer;
+ for other fractional values, truncate.
+
+ This function rounds the floating-point value according to this strategy
+ in preparation for conversion to fixed-point.
+
+ Args:
+ value (float): The input floating-point value.
+
+ Returns
+ float: The rounded value.
+ """
+ # See this thread for how we ended up with this implementation:
+ # https://github.com/fonttools/fonttools/issues/1248#issuecomment-383198166
+ return int(math.floor(value + 0.5))
+
+def maybeRound(v, tolerance, round=otRound):
+ rounded = round(v)
+ return rounded if abs(rounded - v) <= tolerance else v
+
+def roundFunc(tolerance, round=otRound):
+ if tolerance < 0:
+ raise ValueError("Rounding tolerance must be positive")
+
+ if tolerance == 0:
+ return noRound
+
+ if tolerance >= .5:
+ return round
+
+ return functools.partial(maybeRound, tolerance=tolerance, round=round)
diff --git a/Lib/fontTools/misc/sstruct.py b/Lib/fontTools/misc/sstruct.py
index 8b69a429..ba1f8788 100644
--- a/Lib/fontTools/misc/sstruct.py
+++ b/Lib/fontTools/misc/sstruct.py
@@ -46,7 +46,7 @@ calcsize(fmt)
it returns the size of the data in bytes.
"""
-from fontTools.misc.py23 import *
+from fontTools.misc.py23 import tobytes, tostr
from fontTools.misc.fixedTools import fixedToFloat as fi2fl, floatToFixed as fl2fi
import struct
import re
@@ -68,7 +68,7 @@ def pack(fmt, obj):
if name in fixes:
# fixed point conversion
value = fl2fi(value, fixes[name])
- elif isinstance(value, basestring):
+ elif isinstance(value, str):
value = tobytes(value)
elements.append(value)
data = struct.pack(*(formatstring,) + tuple(elements))
diff --git a/Lib/fontTools/misc/symfont.py b/Lib/fontTools/misc/symfont.py
index d3e5ce23..a1a87300 100644
--- a/Lib/fontTools/misc/symfont.py
+++ b/Lib/fontTools/misc/symfont.py
@@ -1,4 +1,3 @@
-from fontTools.misc.py23 import *
from fontTools.pens.basePen import BasePen
from functools import partial
from itertools import count
@@ -112,8 +111,7 @@ MomentXYPen = partial(GreenPen, func=x*y)
def printGreenPen(penName, funcs, file=sys.stdout):
print(
-'''from fontTools.misc.py23 import *
-from fontTools.pens.basePen import BasePen
+'''from fontTools.pens.basePen import BasePen
class %s(BasePen):
diff --git a/Lib/fontTools/misc/testTools.py b/Lib/fontTools/misc/testTools.py
index be9bc851..1b258e37 100644
--- a/Lib/fontTools/misc/testTools.py
+++ b/Lib/fontTools/misc/testTools.py
@@ -1,12 +1,13 @@
"""Helpers for writing unit tests."""
from collections.abc import Iterable
+from io import BytesIO
import os
import shutil
import sys
import tempfile
from unittest import TestCase as _TestCase
-from fontTools.misc.py23 import *
+from fontTools.misc.py23 import tobytes
from fontTools.misc.xmlWriter import XMLWriter
@@ -25,7 +26,7 @@ def parseXML(xmlSnippet):
xml = b"<root>"
if isinstance(xmlSnippet, bytes):
xml += xmlSnippet
- elif isinstance(xmlSnippet, unicode):
+ elif isinstance(xmlSnippet, str):
xml += tobytes(xmlSnippet, 'utf-8')
elif isinstance(xmlSnippet, Iterable):
xml += b"".join(tobytes(s, 'utf-8') for s in xmlSnippet)
diff --git a/Lib/fontTools/misc/textTools.py b/Lib/fontTools/misc/textTools.py
index 6a047ae6..072976af 100644
--- a/Lib/fontTools/misc/textTools.py
+++ b/Lib/fontTools/misc/textTools.py
@@ -1,7 +1,7 @@
"""fontTools.misc.textTools.py -- miscellaneous routines."""
-from fontTools.misc.py23 import *
+from fontTools.misc.py23 import bytechr, byteord, bytesjoin, strjoin, tobytes
import ast
import string
@@ -12,7 +12,7 @@ safeEval = ast.literal_eval
def readHex(content):
"""Convert a list of hex strings to binary data."""
- return deHexStr(strjoin(chunk for chunk in content if isinstance(chunk, basestring)))
+ return deHexStr(strjoin(chunk for chunk in content if isinstance(chunk, str)))
def deHexStr(hexdata):
diff --git a/Lib/fontTools/misc/timeTools.py b/Lib/fontTools/misc/timeTools.py
index 13613827..f4b84f6e 100644
--- a/Lib/fontTools/misc/timeTools.py
+++ b/Lib/fontTools/misc/timeTools.py
@@ -1,7 +1,6 @@
"""fontTools.misc.timeTools.py -- tools for working with OpenType timestamps.
"""
-from fontTools.misc.py23 import *
import os
import time
from datetime import datetime, timezone
diff --git a/Lib/fontTools/misc/vector.py b/Lib/fontTools/misc/vector.py
new file mode 100644
index 00000000..81c14841
--- /dev/null
+++ b/Lib/fontTools/misc/vector.py
@@ -0,0 +1,143 @@
+from numbers import Number
+import math
+import operator
+import warnings
+
+
+__all__ = ["Vector"]
+
+
+class Vector(tuple):
+
+ """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.
+ """
+
+ __slots__ = ()
+
+ def __new__(cls, values, keep=False):
+ if keep is not False:
+ warnings.warn(
+ "the 'keep' argument has been deprecated",
+ DeprecationWarning,
+ )
+ if type(values) == Vector:
+ # No need to create a new object
+ return values
+ return super().__new__(cls, values)
+
+ def __repr__(self):
+ return f"{self.__class__.__name__}({super().__repr__()})"
+
+ def _vectorOp(self, other, op):
+ if isinstance(other, Vector):
+ assert len(self) == len(other)
+ return self.__class__(op(a, b) for a, b in zip(self, other))
+ if isinstance(other, Number):
+ return self.__class__(op(v, other) for v in self)
+ raise NotImplementedError()
+
+ def _scalarOp(self, other, op):
+ if isinstance(other, Number):
+ return self.__class__(op(v, other) for v in self)
+ raise NotImplementedError()
+
+ def _unaryOp(self, op):
+ return self.__class__(op(v) for v in self)
+
+ def __add__(self, other):
+ return self._vectorOp(other, operator.add)
+
+ __radd__ = __add__
+
+ def __sub__(self, other):
+ return self._vectorOp(other, operator.sub)
+
+ def __rsub__(self, other):
+ return self._vectorOp(other, _operator_rsub)
+
+ def __mul__(self, other):
+ return self._scalarOp(other, operator.mul)
+
+ __rmul__ = __mul__
+
+ def __truediv__(self, other):
+ return self._scalarOp(other, operator.truediv)
+
+ def __rtruediv__(self, other):
+ return self._scalarOp(other, _operator_rtruediv)
+
+ def __pos__(self):
+ return self._unaryOp(operator.pos)
+
+ def __neg__(self):
+ return self._unaryOp(operator.neg)
+
+ def __round__(self, *, round=round):
+ return self._unaryOp(round)
+
+ def __eq__(self, other):
+ if isinstance(other, list):
+ # bw compat Vector([1, 2, 3]) == [1, 2, 3]
+ other = tuple(other)
+ return super().__eq__(other)
+
+ def __ne__(self, other):
+ return not self.__eq__(other)
+
+ def __bool__(self):
+ return any(self)
+
+ __nonzero__ = __bool__
+
+ def __abs__(self):
+ return math.sqrt(sum(x * x for x in self))
+
+ def length(self):
+ """Return the length of the vector. Equivalent to abs(vector)."""
+ return abs(self)
+
+ def normalized(self):
+ """Return the normalized vector of the vector."""
+ return self / abs(self)
+
+ def dot(self, other):
+ """Performs vector dot product, returning the sum of
+ ``a[0] * b[0], a[1] * b[1], ...``"""
+ assert len(self) == len(other)
+ return sum(a * b for a, b in zip(self, other))
+
+ # Deprecated methods/properties
+
+ def toInt(self):
+ warnings.warn(
+ "the 'toInt' method has been deprecated, use round(vector) instead",
+ DeprecationWarning,
+ )
+ return self.__round__()
+
+ @property
+ def values(self):
+ warnings.warn(
+ "the 'values' attribute has been deprecated, use "
+ "the vector object itself instead",
+ DeprecationWarning,
+ )
+ return list(self)
+
+ @values.setter
+ def values(self, values):
+ raise AttributeError(
+ "can't set attribute, the 'values' attribute has been deprecated",
+ )
+
+
+def _operator_rsub(a, b):
+ return operator.sub(b, a)
+
+
+def _operator_rtruediv(a, b):
+ return operator.truediv(b, a)
diff --git a/Lib/fontTools/misc/xmlReader.py b/Lib/fontTools/misc/xmlReader.py
index 406bba27..b2707e99 100644
--- a/Lib/fontTools/misc/xmlReader.py
+++ b/Lib/fontTools/misc/xmlReader.py
@@ -1,4 +1,3 @@
-from fontTools.misc.py23 import *
from fontTools import ttLib
from fontTools.misc.textTools import safeEval
from fontTools.ttLib.tables.DefaultTable import DefaultTable
diff --git a/Lib/fontTools/misc/xmlWriter.py b/Lib/fontTools/misc/xmlWriter.py
index 6ab8469e..fec127a9 100644
--- a/Lib/fontTools/misc/xmlWriter.py
+++ b/Lib/fontTools/misc/xmlWriter.py
@@ -1,6 +1,6 @@
"""xmlWriter.py -- Simple XML authoring class"""
-from fontTools.misc.py23 import *
+from fontTools.misc.py23 import byteord, strjoin, tobytes, tostr
import sys
import os
import string
@@ -34,8 +34,8 @@ class XMLWriter(object):
self.totype = tobytes
except TypeError:
# This better not fail.
- self.file.write(tounicode(''))
- self.totype = tounicode
+ self.file.write('')
+ self.totype = tostr
self.indentwhite = self.totype(indentwhite)
if newlinestr is None:
self.newlinestr = self.totype(os.linesep)
@@ -156,7 +156,7 @@ class XMLWriter(object):
return ""
data = ""
for attr, value in attributes:
- if not isinstance(value, (bytes, unicode)):
+ if not isinstance(value, (bytes, str)):
value = str(value)
data = data + ' %s="%s"' % (attr, escapeattr(value))
return data