Remove max_n parameter
This is probably not useful as a parameter, so instead we hardcode the value into the main module.
This commit is contained in:
parent
856ffe1fca
commit
b568c934d0
@ -20,6 +20,8 @@ from fontTools.misc import bezierTools
|
|||||||
|
|
||||||
__all__ = ['curve_to_quadratic', 'curves_to_quadratic']
|
__all__ = ['curve_to_quadratic', 'curves_to_quadratic']
|
||||||
|
|
||||||
|
MAX_N = 100
|
||||||
|
|
||||||
|
|
||||||
class Cu2QuError(Exception):
|
class Cu2QuError(Exception):
|
||||||
pass
|
pass
|
||||||
@ -158,7 +160,7 @@ def curve_spline_dist(bezier, spline):
|
|||||||
return error
|
return error
|
||||||
|
|
||||||
|
|
||||||
def curve_to_quadratic(p, max_err, max_n):
|
def curve_to_quadratic(p, max_err):
|
||||||
"""Return a quadratic spline approximating this cubic bezier, and
|
"""Return a quadratic spline approximating this cubic bezier, and
|
||||||
the error of approximation.
|
the error of approximation.
|
||||||
Raise 'ApproxNotFoundError' if no suitable approximation can be found
|
Raise 'ApproxNotFoundError' if no suitable approximation can be found
|
||||||
@ -166,7 +168,7 @@ def curve_to_quadratic(p, max_err, max_n):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
spline, error = None, None
|
spline, error = None, None
|
||||||
for n in range(1, max_n + 1):
|
for n in range(1, MAX_N + 1):
|
||||||
spline = cubic_approx_spline(p, n)
|
spline = cubic_approx_spline(p, n)
|
||||||
if spline is None:
|
if spline is None:
|
||||||
continue
|
continue
|
||||||
@ -179,7 +181,7 @@ def curve_to_quadratic(p, max_err, max_n):
|
|||||||
return spline, error
|
return spline, error
|
||||||
|
|
||||||
|
|
||||||
def curves_to_quadratic(curves, max_errors, max_n):
|
def curves_to_quadratic(curves, max_errors):
|
||||||
"""Return quadratic splines approximating these cubic beziers, and
|
"""Return quadratic splines approximating these cubic beziers, and
|
||||||
the respective errors of approximation.
|
the respective errors of approximation.
|
||||||
Raise 'ApproxNotFoundError' if no suitable approximation can be found
|
Raise 'ApproxNotFoundError' if no suitable approximation can be found
|
||||||
@ -188,7 +190,7 @@ def curves_to_quadratic(curves, max_errors, max_n):
|
|||||||
|
|
||||||
splines = [None] * len(curves)
|
splines = [None] * len(curves)
|
||||||
errors = [None] * len(max_errors)
|
errors = [None] * len(max_errors)
|
||||||
for n in range(1, max_n + 1):
|
for n in range(1, MAX_N + 1):
|
||||||
splines = [cubic_approx_spline(c, n) for c in curves]
|
splines = [cubic_approx_spline(c, n) for c in curves]
|
||||||
if not all(splines):
|
if not all(splines):
|
||||||
continue
|
continue
|
||||||
|
@ -52,7 +52,6 @@ def fonts_to_quadratic(*fonts, **kwargs):
|
|||||||
|
|
||||||
report = kwargs.get('report', {})
|
report = kwargs.get('report', {})
|
||||||
dump_report = kwargs.get('dump_report', False)
|
dump_report = kwargs.get('dump_report', False)
|
||||||
max_n = kwargs.get('max_n', 10)
|
|
||||||
|
|
||||||
max_err_em = kwargs.get('max_err_em', 0.0025)
|
max_err_em = kwargs.get('max_err_em', 0.0025)
|
||||||
max_err = kwargs.get('max_err', None)
|
max_err = kwargs.get('max_err', None)
|
||||||
@ -62,7 +61,7 @@ def fonts_to_quadratic(*fonts, **kwargs):
|
|||||||
max_errors = [f.info.unitsPerEm * max_err_em for f in fonts]
|
max_errors = [f.info.unitsPerEm * max_err_em for f in fonts]
|
||||||
|
|
||||||
for glyph in FontCollection(fonts):
|
for glyph in FontCollection(fonts):
|
||||||
glyph_to_quadratic(glyph, max_errors, max_n, report)
|
glyph_to_quadratic(glyph, max_errors, report)
|
||||||
|
|
||||||
if dump_report:
|
if dump_report:
|
||||||
spline_lengths = report.keys()
|
spline_lengths = report.keys()
|
||||||
@ -72,7 +71,7 @@ def fonts_to_quadratic(*fonts, **kwargs):
|
|||||||
return report
|
return report
|
||||||
|
|
||||||
|
|
||||||
def glyph_to_quadratic(glyph, max_err, max_n, report):
|
def glyph_to_quadratic(glyph, max_err, report):
|
||||||
"""Convert a glyph's curves to quadratic, in place."""
|
"""Convert a glyph's curves to quadratic, in place."""
|
||||||
|
|
||||||
for contour in glyph:
|
for contour in glyph:
|
||||||
@ -81,13 +80,13 @@ def glyph_to_quadratic(glyph, max_err, max_n, report):
|
|||||||
segment = contour[i]
|
segment = contour[i]
|
||||||
if segment.type == 'curve':
|
if segment.type == 'curve':
|
||||||
segments.append(segment_to_quadratic(
|
segments.append(segment_to_quadratic(
|
||||||
contour, i, max_err, max_n, report))
|
contour, i, max_err, report))
|
||||||
else:
|
else:
|
||||||
segments.append(segment)
|
segments.append(segment)
|
||||||
replace_segments(contour, segments)
|
replace_segments(contour, segments)
|
||||||
|
|
||||||
|
|
||||||
def segment_to_quadratic(contour, segment_id, max_err, max_n, report):
|
def segment_to_quadratic(contour, segment_id, max_err, report):
|
||||||
"""Return a quadratic approximation of a cubic segment."""
|
"""Return a quadratic approximation of a cubic segment."""
|
||||||
|
|
||||||
segment = contour[segment_id]
|
segment = contour[segment_id]
|
||||||
@ -98,8 +97,7 @@ def segment_to_quadratic(contour, segment_id, max_err, max_n, report):
|
|||||||
# same contour
|
# same contour
|
||||||
prev_segment = contour[segment_id - 1]
|
prev_segment = contour[segment_id - 1]
|
||||||
points = points_to_quadratic(prev_segment.points[-1], segment.points[0],
|
points = points_to_quadratic(prev_segment.points[-1], segment.points[0],
|
||||||
segment.points[1], segment.points[2],
|
segment.points[1], segment.points[2], max_err)
|
||||||
max_err, max_n)
|
|
||||||
|
|
||||||
if isinstance(points[0][0], float): # just one spline
|
if isinstance(points[0][0], float): # just one spline
|
||||||
n = str(len(points))
|
n = str(len(points))
|
||||||
@ -113,17 +111,17 @@ def segment_to_quadratic(contour, segment_id, max_err, max_n, report):
|
|||||||
return as_quadratic(segment, points)
|
return as_quadratic(segment, points)
|
||||||
|
|
||||||
|
|
||||||
def points_to_quadratic(p0, p1, p2, p3, max_err, max_n):
|
def points_to_quadratic(p0, p1, p2, p3, max_err):
|
||||||
"""Return a quadratic spline approximating the cubic bezier defined by these
|
"""Return a quadratic spline approximating the cubic bezier defined by these
|
||||||
points (or collections of points).
|
points (or collections of points).
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if hasattr(p0, 'x'):
|
if hasattr(p0, 'x'):
|
||||||
curve = [(float(i.x), float(i.y)) for i in [p0, p1, p2, p3]]
|
curve = [(float(i.x), float(i.y)) for i in [p0, p1, p2, p3]]
|
||||||
return curve_to_quadratic(curve, max_err, max_n)[0]
|
return curve_to_quadratic(curve, max_err)[0]
|
||||||
|
|
||||||
curves = [[(float(i.x), float(i.y)) for i in p] for p in zip(p0, p1, p2, p3)]
|
curves = [[(float(i.x), float(i.y)) for i in p] for p in zip(p0, p1, p2, p3)]
|
||||||
return curves_to_quadratic(curves, max_err, max_n)[0]
|
return curves_to_quadratic(curves, max_err)[0]
|
||||||
|
|
||||||
|
|
||||||
def replace_segments(contour, segments):
|
def replace_segments(contour, segments):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user