[qu2cu] Simplify API
Drop the one that was special-case of the other.
This commit is contained in:
parent
f58a17d6e9
commit
d0896ac296
@ -13,7 +13,7 @@
|
|||||||
# See the License for the specific language governing permissions and
|
# See the License for the specific language governing permissions and
|
||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
|
|
||||||
from fontTools.qu2cu import quadratics_to_curves
|
from fontTools.qu2cu import quadratic_to_curves
|
||||||
from fontTools.pens.filterPen import ContourFilterPen
|
from fontTools.pens.filterPen import ContourFilterPen
|
||||||
from fontTools.pens.reverseContourPen import ReverseContourPen
|
from fontTools.pens.reverseContourPen import ReverseContourPen
|
||||||
|
|
||||||
@ -48,7 +48,7 @@ class Qu2CuPen(ContourFilterPen):
|
|||||||
self.stats = stats
|
self.stats = stats
|
||||||
|
|
||||||
def _quadratics_to_curve(self, q):
|
def _quadratics_to_curve(self, q):
|
||||||
curves = quadratics_to_curves(q, self.max_err, self.all_cubic)
|
curves = quadratic_to_curves(q, self.max_err, self.all_cubic)
|
||||||
if self.stats is not None:
|
if self.stats is not None:
|
||||||
n = str(len(curves))
|
n = str(len(curves))
|
||||||
self.stats[n] = self.stats.get(n, 0) + 1
|
self.stats[n] = self.stats.get(n, 0) + 1
|
||||||
|
@ -18,7 +18,7 @@ def generate_curve():
|
|||||||
def setup_quadratic_to_curves():
|
def setup_quadratic_to_curves():
|
||||||
curve = generate_curve()
|
curve = generate_curve()
|
||||||
quadratics = curve_to_quadratic(curve, MAX_ERR)
|
quadratics = curve_to_quadratic(curve, MAX_ERR)
|
||||||
return quadratics, MAX_ERR
|
return [quadratics], MAX_ERR
|
||||||
|
|
||||||
|
|
||||||
def run_benchmark(module, function, setup_suffix="", repeat=10, number=20):
|
def run_benchmark(module, function, setup_suffix="", repeat=10, number=20):
|
||||||
|
@ -26,7 +26,7 @@ from fontTools.misc.bezierTools import splitCubicAtTC
|
|||||||
from collections import namedtuple
|
from collections import namedtuple
|
||||||
|
|
||||||
|
|
||||||
__all__ = ["quadratic_to_curves", "quadratics_to_curves"]
|
__all__ = ["quadratic_to_curves"]
|
||||||
|
|
||||||
|
|
||||||
if cython.compiled:
|
if cython.compiled:
|
||||||
@ -164,7 +164,7 @@ def add_implicit_on_curves(p):
|
|||||||
return q
|
return q
|
||||||
|
|
||||||
|
|
||||||
def quadratics_to_curves(pp, tolerance=0.5, all_cubic=False):
|
def quadratic_to_curves(pp, tolerance=0.5, all_cubic=False):
|
||||||
"""Convers a connecting list of quadratic splines to a list of quadratic
|
"""Convers a connecting list of quadratic splines to a list of quadratic
|
||||||
and cubic curves.
|
and cubic curves.
|
||||||
|
|
||||||
@ -207,41 +207,6 @@ def quadratics_to_curves(pp, tolerance=0.5, all_cubic=False):
|
|||||||
return curves
|
return curves
|
||||||
|
|
||||||
|
|
||||||
def quadratic_to_curves(q, tolerance=0.5, all_cubic=False):
|
|
||||||
"""Convers a quadratic spline to a list of quadratic and cubic curves.
|
|
||||||
|
|
||||||
The quadratic spline is specified as a list of points, each of which is
|
|
||||||
a 2-tuple of X,Y coordinates. The first and last points are on-curve points
|
|
||||||
and the rest are off-curve points, with an implied on-curve point in the
|
|
||||||
middle between every two consequtive off-curve points.
|
|
||||||
|
|
||||||
The output is a list of tuples. Each tuple is either of length three, for
|
|
||||||
a quadratic curve, or four, for a cubic curve. Each curve's last point
|
|
||||||
is the same as the next curve's first point.
|
|
||||||
|
|
||||||
q: quadratic spline
|
|
||||||
tolerance: absolute error tolerance; defaults to 0.5
|
|
||||||
all_cubic: if True, only cubic curves are generated; defaults to False
|
|
||||||
"""
|
|
||||||
is_complex = type(q[0]) is complex
|
|
||||||
if not is_complex:
|
|
||||||
q = [complex(x, y) for (x, y) in q]
|
|
||||||
|
|
||||||
costs = [0]
|
|
||||||
for i in range(len(q) - 2):
|
|
||||||
costs.append(i + 1)
|
|
||||||
costs.append(i + 2)
|
|
||||||
costs.append(len(q) - 1)
|
|
||||||
costs.append(len(q))
|
|
||||||
q = add_implicit_on_curves(q)
|
|
||||||
|
|
||||||
curves = spline_to_curves(q, costs, tolerance, all_cubic)
|
|
||||||
|
|
||||||
if not is_complex:
|
|
||||||
curves = [tuple((c.real, c.imag) for c in curve) for curve in curves]
|
|
||||||
return curves
|
|
||||||
|
|
||||||
|
|
||||||
Solution = namedtuple("Solution", ["num_points", "error", "start_index", "is_cubic"])
|
Solution = namedtuple("Solution", ["num_points", "error", "start_index", "is_cubic"])
|
||||||
|
|
||||||
|
|
||||||
@ -361,7 +326,7 @@ def main():
|
|||||||
"cu2qu tolerance %g. qu2cu tolerance %g." % (tolerance, reconstruct_tolerance)
|
"cu2qu tolerance %g. qu2cu tolerance %g." % (tolerance, reconstruct_tolerance)
|
||||||
)
|
)
|
||||||
print("One random cubic turned into %d quadratics." % len(quadratics))
|
print("One random cubic turned into %d quadratics." % len(quadratics))
|
||||||
curves = quadratic_to_curves(quadratics, reconstruct_tolerance)
|
curves = quadratic_to_curves([quadratics], reconstruct_tolerance)
|
||||||
print("Those quadratics turned back into %d cubics. " % len(curves))
|
print("Those quadratics turned back into %d cubics. " % len(curves))
|
||||||
print("Original curve:", curve)
|
print("Original curve:", curve)
|
||||||
print("Reconstructed curve(s):", curves)
|
print("Reconstructed curve(s):", curves)
|
||||||
|
@ -15,7 +15,7 @@
|
|||||||
import unittest
|
import unittest
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from fontTools.qu2cu import quadratic_to_curves, quadratics_to_curves
|
from fontTools.qu2cu import quadratic_to_curves
|
||||||
from fontTools.qu2cu.qu2cu import main as qu2cu_main
|
from fontTools.qu2cu.qu2cu import main as qu2cu_main
|
||||||
from fontTools.qu2cu.benchmark import main as benchmark_main
|
from fontTools.qu2cu.benchmark import main as benchmark_main
|
||||||
|
|
||||||
@ -81,11 +81,7 @@ class Qu2CuTest:
|
|||||||
for curve in expected
|
for curve in expected
|
||||||
]
|
]
|
||||||
|
|
||||||
if len(quadratics) == 1:
|
c = quadratic_to_curves(quadratics, tolerance, cubic_only)
|
||||||
c = quadratic_to_curves(quadratics[0], tolerance, cubic_only)
|
|
||||||
assert c == expected
|
|
||||||
|
|
||||||
c = quadratics_to_curves(quadratics, tolerance, cubic_only)
|
|
||||||
assert c == expected
|
assert c == expected
|
||||||
|
|
||||||
def test_roundtrip(self):
|
def test_roundtrip(self):
|
||||||
@ -97,7 +93,7 @@ class Qu2CuTest:
|
|||||||
tolerance = 1
|
tolerance = 1
|
||||||
|
|
||||||
splines = [curve_to_quadratic(c, tolerance) for c in curves]
|
splines = [curve_to_quadratic(c, tolerance) for c in curves]
|
||||||
reconsts = [quadratic_to_curves(spline, tolerance) for spline in splines]
|
reconsts = [quadratic_to_curves([spline], tolerance) for spline in splines]
|
||||||
|
|
||||||
for curve, reconst in zip(curves, reconsts):
|
for curve, reconst in zip(curves, reconsts):
|
||||||
assert len(reconst) == 1
|
assert len(reconst) == 1
|
||||||
|
Loading…
x
Reference in New Issue
Block a user