[bezierTools] Add calcCubicArcLength() and calcCubicArcLengthC()
Fixes https://github.com/fonttools/fonttools/issues/1142
This commit is contained in:
parent
0cae12bdbd
commit
2d4e76ff0b
@ -13,10 +13,12 @@ __all__ = [
|
|||||||
"approximateCubicArcLengthC",
|
"approximateCubicArcLengthC",
|
||||||
"approximateQuadraticArcLength",
|
"approximateQuadraticArcLength",
|
||||||
"approximateQuadraticArcLengthC",
|
"approximateQuadraticArcLengthC",
|
||||||
|
"calcCubicArcLength",
|
||||||
|
"calcCubicArcLengthC",
|
||||||
"calcQuadraticArcLength",
|
"calcQuadraticArcLength",
|
||||||
"calcQuadraticArcLengthC",
|
"calcQuadraticArcLengthC",
|
||||||
"calcQuadraticBounds",
|
|
||||||
"calcCubicBounds",
|
"calcCubicBounds",
|
||||||
|
"calcQuadraticBounds",
|
||||||
"splitLine",
|
"splitLine",
|
||||||
"splitQuadratic",
|
"splitQuadratic",
|
||||||
"splitCubic",
|
"splitCubic",
|
||||||
@ -27,6 +29,32 @@ __all__ = [
|
|||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
def calcCubicArcLength(pt1, pt2, pt3, pt4, tolerance=0.005):
|
||||||
|
"""Return the arc length for a cubic bezier segment."""
|
||||||
|
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))
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
def calcCubicArcLengthC(pt1, pt2, pt3, pt4, tolerance=0.005):
|
||||||
|
"""Return the arc length for a cubic bezier segment using complex points."""
|
||||||
|
mult = 1. + 1.5 * tolerance # The 1.5 is a empirical hack; no math
|
||||||
|
return _calcCubicArcLengthCRecurse(mult, pt1, pt2, pt3, pt4)
|
||||||
|
|
||||||
|
|
||||||
epsilonDigits = 6
|
epsilonDigits = 6
|
||||||
epsilon = 1e-10
|
epsilon = 1e-10
|
||||||
|
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
from __future__ import print_function, division, absolute_import
|
from __future__ import print_function, division, absolute_import
|
||||||
from fontTools.misc.py23 import *
|
from fontTools.misc.py23 import *
|
||||||
from fontTools.pens.basePen import BasePen
|
from fontTools.pens.basePen import BasePen
|
||||||
from fontTools.misc.bezierTools import splitQuadraticAtT, splitCubicAtT, approximateQuadraticArcLengthC, calcQuadraticArcLengthC, approximateCubicArcLengthC
|
from fontTools.misc.bezierTools import approximateQuadraticArcLengthC, calcQuadraticArcLengthC, approximateCubicArcLengthC, calcCubicArcLengthC
|
||||||
import math
|
import math
|
||||||
|
|
||||||
|
|
||||||
@ -14,18 +14,12 @@ __all__ = ["PerimeterPen"]
|
|||||||
def _distance(p0, p1):
|
def _distance(p0, p1):
|
||||||
return math.hypot(p0[0] - p1[0], p0[1] - p1[1])
|
return math.hypot(p0[0] - p1[0], p0[1] - p1[1])
|
||||||
|
|
||||||
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))
|
|
||||||
|
|
||||||
class PerimeterPen(BasePen):
|
class PerimeterPen(BasePen):
|
||||||
|
|
||||||
def __init__(self, glyphset=None, tolerance=0.005):
|
def __init__(self, glyphset=None, tolerance=0.005):
|
||||||
BasePen.__init__(self, glyphset)
|
BasePen.__init__(self, glyphset)
|
||||||
self.value = 0
|
self.value = 0
|
||||||
self._mult = 1.+1.5*tolerance # The 1.5 is a empirical hack; no math
|
self.tolerance = tolerance
|
||||||
|
|
||||||
# Choose which algorithm to use for quadratic and for cubic.
|
# Choose which algorithm to use for quadratic and for cubic.
|
||||||
# Quadrature is faster but has fixed error characteristic with no strong
|
# Quadrature is faster but has fixed error characteristic with no strong
|
||||||
@ -55,15 +49,8 @@ class PerimeterPen(BasePen):
|
|||||||
p0 = self._getCurrentPoint()
|
p0 = self._getCurrentPoint()
|
||||||
self._addQuadratic(complex(*p0), complex(*p1), complex(*p2))
|
self._addQuadratic(complex(*p0), complex(*p1), complex(*p2))
|
||||||
|
|
||||||
def _addCubicRecursive(self, p0, p1, p2, p3):
|
def _addCubicRecursive(self, c0, c1, c2, c3):
|
||||||
arch = abs(p0-p3)
|
self.value += calcCubicArcLengthC(c0, c1, c2, c3, self.tolerance)
|
||||||
box = abs(p0-p1) + abs(p1-p2) + abs(p2-p3)
|
|
||||||
if arch * self._mult >= box:
|
|
||||||
self.value += (arch + box) * .5
|
|
||||||
else:
|
|
||||||
one,two = _split_cubic_into_two(p0,p1,p2,p3)
|
|
||||||
self._addCubicRecursive(*one)
|
|
||||||
self._addCubicRecursive(*two)
|
|
||||||
|
|
||||||
def _addCubicQuadrature(self, c0, c1, c2, c3):
|
def _addCubicQuadrature(self, c0, c1, c2, c3):
|
||||||
self.value += approximateCubicArcLengthC(c0, c1, c2, c3)
|
self.value += approximateCubicArcLengthC(c0, c1, c2, c3)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user