2020-03-31 17:09:26 +01:00
|
|
|
# Copyright 2016 Google Inc. All Rights Reserved.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
2022-12-07 16:09:27 -07:00
|
|
|
from fontTools.cu2qu import curve_to_quadratic, curves_to_quadratic
|
2016-03-13 09:58:57 +00:00
|
|
|
from fontTools.pens.basePen import AbstractPen, decomposeSuperBezierSegment
|
2017-10-30 18:56:02 +00:00
|
|
|
from fontTools.pens.reverseContourPen import ReverseContourPen
|
2018-11-01 16:41:08 +00:00
|
|
|
from fontTools.pens.pointPen import BasePointToSegmentPen
|
|
|
|
from fontTools.pens.pointPen import ReverseContourPointPen
|
2016-03-13 09:58:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Cu2QuPen(AbstractPen):
|
2016-03-16 10:14:05 +00:00
|
|
|
""" A filter pen to convert cubic bezier curves to quadratic b-splines
|
|
|
|
using the FontTools SegmentPen protocol.
|
|
|
|
|
2021-12-02 15:31:49 +00:00
|
|
|
Args:
|
|
|
|
|
|
|
|
other_pen: another SegmentPen used to draw the transformed outline.
|
|
|
|
max_err: maximum approximation error in font units. For optimal results,
|
|
|
|
if you know the UPEM of the font, we recommend setting this to a
|
|
|
|
value equal, or close to UPEM / 1000.
|
|
|
|
reverse_direction: flip the contours' direction but keep starting point.
|
|
|
|
stats: a dictionary counting the point numbers of quadratic segments.
|
|
|
|
ignore_single_points: don't emit contours containing only a single point
|
2017-10-31 17:57:53 +00:00
|
|
|
|
|
|
|
NOTE: The "ignore_single_points" argument is deprecated since v1.3.0,
|
2022-12-07 15:19:07 -07:00
|
|
|
which dropped Robofab support. It's no longer needed to special-case
|
2017-10-31 17:57:53 +00:00
|
|
|
UFO2-style anchors (aka "named points") when using ufoLib >= 2.0,
|
|
|
|
as these are no longer drawn onto pens as single-point contours,
|
|
|
|
but are handled separately as anchors.
|
2016-03-16 10:14:05 +00:00
|
|
|
"""
|
2016-03-13 09:58:57 +00:00
|
|
|
|
|
|
|
def __init__(self, other_pen, max_err, reverse_direction=False,
|
2016-03-16 10:14:05 +00:00
|
|
|
stats=None, ignore_single_points=False):
|
2016-03-13 09:58:57 +00:00
|
|
|
if reverse_direction:
|
|
|
|
self.pen = ReverseContourPen(other_pen)
|
|
|
|
else:
|
|
|
|
self.pen = other_pen
|
|
|
|
self.max_err = max_err
|
|
|
|
self.stats = stats
|
2017-10-31 17:57:53 +00:00
|
|
|
if ignore_single_points:
|
|
|
|
import warnings
|
|
|
|
warnings.warn("ignore_single_points is deprecated and "
|
|
|
|
"will be removed in future versions",
|
|
|
|
UserWarning, stacklevel=2)
|
2016-03-13 09:58:57 +00:00
|
|
|
self.ignore_single_points = ignore_single_points
|
|
|
|
self.start_pt = None
|
|
|
|
self.current_pt = None
|
|
|
|
|
2016-03-25 12:42:08 +01:00
|
|
|
def _check_contour_is_open(self):
|
|
|
|
if self.current_pt is None:
|
|
|
|
raise AssertionError("moveTo is required")
|
|
|
|
|
|
|
|
def _check_contour_is_closed(self):
|
|
|
|
if self.current_pt is not None:
|
|
|
|
raise AssertionError("closePath or endPath is required")
|
|
|
|
|
2016-03-13 09:58:57 +00:00
|
|
|
def _add_moveTo(self):
|
|
|
|
if self.start_pt is not None:
|
|
|
|
self.pen.moveTo(self.start_pt)
|
|
|
|
self.start_pt = None
|
|
|
|
|
|
|
|
def moveTo(self, pt):
|
2016-03-25 12:42:08 +01:00
|
|
|
self._check_contour_is_closed()
|
2016-03-13 09:58:57 +00:00
|
|
|
self.start_pt = self.current_pt = pt
|
|
|
|
if not self.ignore_single_points:
|
|
|
|
self._add_moveTo()
|
|
|
|
|
|
|
|
def lineTo(self, pt):
|
2016-03-25 12:42:08 +01:00
|
|
|
self._check_contour_is_open()
|
2016-03-13 09:58:57 +00:00
|
|
|
self._add_moveTo()
|
|
|
|
self.pen.lineTo(pt)
|
|
|
|
self.current_pt = pt
|
|
|
|
|
|
|
|
def qCurveTo(self, *points):
|
2016-03-25 12:42:08 +01:00
|
|
|
self._check_contour_is_open()
|
2016-03-16 10:38:22 +00:00
|
|
|
n = len(points)
|
|
|
|
if n == 1:
|
|
|
|
self.lineTo(points[0])
|
|
|
|
elif n > 1:
|
|
|
|
self._add_moveTo()
|
|
|
|
self.pen.qCurveTo(*points)
|
|
|
|
self.current_pt = points[-1]
|
|
|
|
else:
|
|
|
|
raise AssertionError("illegal qcurve segment point count: %d" % n)
|
2016-03-13 09:58:57 +00:00
|
|
|
|
|
|
|
def _curve_to_quadratic(self, pt1, pt2, pt3):
|
|
|
|
curve = (self.current_pt, pt1, pt2, pt3)
|
2016-07-27 13:21:47 -07:00
|
|
|
quadratic = curve_to_quadratic(curve, self.max_err)
|
2016-03-13 09:58:57 +00:00
|
|
|
if self.stats is not None:
|
2017-10-25 18:49:19 +01:00
|
|
|
n = str(len(quadratic) - 2)
|
2016-03-13 09:58:57 +00:00
|
|
|
self.stats[n] = self.stats.get(n, 0) + 1
|
|
|
|
self.qCurveTo(*quadratic[1:])
|
|
|
|
|
|
|
|
def curveTo(self, *points):
|
2016-03-25 12:42:08 +01:00
|
|
|
self._check_contour_is_open()
|
2016-03-16 10:38:22 +00:00
|
|
|
n = len(points)
|
|
|
|
if n == 3:
|
2016-03-13 09:58:57 +00:00
|
|
|
# this is the most common case, so we special-case it
|
|
|
|
self._curve_to_quadratic(*points)
|
2016-03-16 10:38:22 +00:00
|
|
|
elif n > 3:
|
2016-03-13 09:58:57 +00:00
|
|
|
for segment in decomposeSuperBezierSegment(points):
|
|
|
|
self._curve_to_quadratic(*segment)
|
2016-03-16 10:38:22 +00:00
|
|
|
elif n == 2:
|
2016-03-13 09:58:57 +00:00
|
|
|
self.qCurveTo(*points)
|
2016-03-16 10:38:22 +00:00
|
|
|
elif n == 1:
|
2016-03-13 09:58:57 +00:00
|
|
|
self.lineTo(points[0])
|
2016-03-16 10:38:22 +00:00
|
|
|
else:
|
|
|
|
raise AssertionError("illegal curve segment point count: %d" % n)
|
2016-03-13 09:58:57 +00:00
|
|
|
|
|
|
|
def closePath(self):
|
2016-03-25 12:42:08 +01:00
|
|
|
self._check_contour_is_open()
|
2016-03-13 09:58:57 +00:00
|
|
|
if self.start_pt is None:
|
|
|
|
# if 'start_pt' is _not_ None, we are ignoring single-point paths
|
|
|
|
self.pen.closePath()
|
|
|
|
self.current_pt = self.start_pt = None
|
|
|
|
|
|
|
|
def endPath(self):
|
2016-03-25 12:42:08 +01:00
|
|
|
self._check_contour_is_open()
|
2016-03-13 09:58:57 +00:00
|
|
|
if self.start_pt is None:
|
|
|
|
self.pen.endPath()
|
|
|
|
self.current_pt = self.start_pt = None
|
|
|
|
|
|
|
|
def addComponent(self, glyphName, transformation):
|
2016-03-25 12:42:08 +01:00
|
|
|
self._check_contour_is_closed()
|
2016-03-13 09:58:57 +00:00
|
|
|
self.pen.addComponent(glyphName, transformation)
|
|
|
|
|
|
|
|
|
|
|
|
class Cu2QuPointPen(BasePointToSegmentPen):
|
2016-03-16 10:14:05 +00:00
|
|
|
""" A filter pen to convert cubic bezier curves to quadratic b-splines
|
|
|
|
using the RoboFab PointPen protocol.
|
|
|
|
|
2021-12-02 15:31:49 +00:00
|
|
|
Args:
|
|
|
|
other_point_pen: another PointPen used to draw the transformed outline.
|
|
|
|
max_err: maximum approximation error in font units. For optimal results,
|
|
|
|
if you know the UPEM of the font, we recommend setting this to a
|
|
|
|
value equal, or close to UPEM / 1000.
|
|
|
|
reverse_direction: reverse the winding direction of all contours.
|
|
|
|
stats: a dictionary counting the point numbers of quadratic segments.
|
2016-03-16 10:14:05 +00:00
|
|
|
"""
|
2016-03-13 09:58:57 +00:00
|
|
|
|
|
|
|
def __init__(self, other_point_pen, max_err, reverse_direction=False,
|
|
|
|
stats=None):
|
2016-03-18 15:40:16 +00:00
|
|
|
BasePointToSegmentPen.__init__(self)
|
2016-03-13 09:58:57 +00:00
|
|
|
if reverse_direction:
|
|
|
|
self.pen = ReverseContourPointPen(other_point_pen)
|
|
|
|
else:
|
|
|
|
self.pen = other_point_pen
|
|
|
|
self.max_err = max_err
|
|
|
|
self.stats = stats
|
|
|
|
|
|
|
|
def _flushContour(self, segments):
|
|
|
|
assert len(segments) >= 1
|
|
|
|
closed = segments[0][0] != "move"
|
|
|
|
new_segments = []
|
|
|
|
prev_points = segments[-1][1]
|
|
|
|
prev_on_curve = prev_points[-1][0]
|
|
|
|
for segment_type, points in segments:
|
|
|
|
if segment_type == 'curve':
|
2016-03-25 19:24:54 +01:00
|
|
|
for sub_points in self._split_super_bezier_segments(points):
|
|
|
|
on_curve, smooth, name, kwargs = sub_points[-1]
|
|
|
|
bcp1, bcp2 = sub_points[0][0], sub_points[1][0]
|
|
|
|
cubic = [prev_on_curve, bcp1, bcp2, on_curve]
|
2016-07-27 13:21:47 -07:00
|
|
|
quad = curve_to_quadratic(cubic, self.max_err)
|
2016-03-25 19:24:54 +01:00
|
|
|
if self.stats is not None:
|
2017-10-25 18:49:19 +01:00
|
|
|
n = str(len(quad) - 2)
|
2016-03-25 19:24:54 +01:00
|
|
|
self.stats[n] = self.stats.get(n, 0) + 1
|
|
|
|
new_points = [(pt, False, None, {}) for pt in quad[1:-1]]
|
|
|
|
new_points.append((on_curve, smooth, name, kwargs))
|
|
|
|
new_segments.append(["qcurve", new_points])
|
|
|
|
prev_on_curve = sub_points[-1][0]
|
2016-03-13 09:58:57 +00:00
|
|
|
else:
|
|
|
|
new_segments.append([segment_type, points])
|
2016-03-25 19:24:54 +01:00
|
|
|
prev_on_curve = points[-1][0]
|
2016-03-13 09:58:57 +00:00
|
|
|
if closed:
|
2016-03-25 16:37:15 +01:00
|
|
|
# the BasePointToSegmentPen.endPath method that calls _flushContour
|
|
|
|
# rotates the point list of closed contours so that they end with
|
|
|
|
# the first on-curve point. We restore the original starting point.
|
2016-03-13 09:58:57 +00:00
|
|
|
new_segments = new_segments[-1:] + new_segments[:-1]
|
|
|
|
self._drawPoints(new_segments)
|
|
|
|
|
2016-03-25 19:24:54 +01:00
|
|
|
def _split_super_bezier_segments(self, points):
|
|
|
|
sub_segments = []
|
|
|
|
# n is the number of control points
|
|
|
|
n = len(points) - 1
|
|
|
|
if n == 2:
|
|
|
|
# a simple bezier curve segment
|
|
|
|
sub_segments.append(points)
|
|
|
|
elif n > 2:
|
|
|
|
# a "super" bezier; decompose it
|
|
|
|
on_curve, smooth, name, kwargs = points[-1]
|
|
|
|
num_sub_segments = n - 1
|
|
|
|
for i, sub_points in enumerate(decomposeSuperBezierSegment([
|
|
|
|
pt for pt, _, _, _ in points])):
|
|
|
|
new_segment = []
|
|
|
|
for point in sub_points[:-1]:
|
|
|
|
new_segment.append((point, False, None, {}))
|
|
|
|
if i == (num_sub_segments - 1):
|
|
|
|
# the last on-curve keeps its original attributes
|
|
|
|
new_segment.append((on_curve, smooth, name, kwargs))
|
|
|
|
else:
|
|
|
|
# on-curves of sub-segments are always "smooth"
|
|
|
|
new_segment.append((sub_points[-1], True, None, {}))
|
|
|
|
sub_segments.append(new_segment)
|
|
|
|
else:
|
|
|
|
raise AssertionError(
|
|
|
|
"expected 2 control points, found: %d" % n)
|
|
|
|
return sub_segments
|
|
|
|
|
2016-03-13 09:58:57 +00:00
|
|
|
def _drawPoints(self, segments):
|
|
|
|
pen = self.pen
|
|
|
|
pen.beginPath()
|
2016-06-28 09:00:55 +01:00
|
|
|
last_offcurves = []
|
|
|
|
for i, (segment_type, points) in enumerate(segments):
|
2016-03-13 09:58:57 +00:00
|
|
|
if segment_type in ("move", "line"):
|
|
|
|
assert len(points) == 1, (
|
|
|
|
"illegal line segment point count: %d" % len(points))
|
|
|
|
pt, smooth, name, kwargs = points[0]
|
|
|
|
pen.addPoint(pt, segment_type, smooth, name, **kwargs)
|
|
|
|
elif segment_type == "qcurve":
|
|
|
|
assert len(points) >= 2, (
|
|
|
|
"illegal qcurve segment point count: %d" % len(points))
|
2016-06-28 09:00:55 +01:00
|
|
|
offcurves = points[:-1]
|
|
|
|
if offcurves:
|
|
|
|
if i == 0:
|
|
|
|
# any off-curve points preceding the first on-curve
|
|
|
|
# will be appended at the end of the contour
|
|
|
|
last_offcurves = offcurves
|
|
|
|
else:
|
|
|
|
for (pt, smooth, name, kwargs) in offcurves:
|
|
|
|
pen.addPoint(pt, None, smooth, name, **kwargs)
|
2016-03-13 09:58:57 +00:00
|
|
|
pt, smooth, name, kwargs = points[-1]
|
2018-10-11 22:26:10 +01:00
|
|
|
if pt is None:
|
|
|
|
# special quadratic contour with no on-curve points:
|
|
|
|
# we need to skip the "None" point. See also the Pen
|
|
|
|
# protocol's qCurveTo() method and fontTools.pens.basePen
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
pen.addPoint(pt, segment_type, smooth, name, **kwargs)
|
2016-03-13 09:58:57 +00:00
|
|
|
else:
|
|
|
|
# 'curve' segments must have been converted to 'qcurve' by now
|
|
|
|
raise AssertionError(
|
|
|
|
"unexpected segment type: %r" % segment_type)
|
2016-06-28 09:00:55 +01:00
|
|
|
for (pt, smooth, name, kwargs) in last_offcurves:
|
|
|
|
pen.addPoint(pt, None, smooth, name, **kwargs)
|
2016-03-13 09:58:57 +00:00
|
|
|
pen.endPath()
|
|
|
|
|
|
|
|
def addComponent(self, baseGlyphName, transformation):
|
|
|
|
assert self.currentPath is None
|
|
|
|
self.pen.addComponent(baseGlyphName, transformation)
|
2022-12-07 16:09:27 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Cu2QuMultiPen:
|
|
|
|
"""A filter multi-pen to convert cubic bezier curves to quadratic b-splines
|
|
|
|
in a interpolation-compatible manner, using the FontTools SegmentPen protocol.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
|
|
|
other_pens: list of SegmentPens used to draw the transformed outlines.
|
|
|
|
max_err: maximum approximation error in font units. For optimal results,
|
|
|
|
if you know the UPEM of the font, we recommend setting this to a
|
|
|
|
value equal, or close to UPEM / 1000.
|
|
|
|
reverse_direction: flip the contours' direction but keep starting point.
|
|
|
|
|
|
|
|
This pen does not follow the normal SegmentPen protocol. Instead, its
|
|
|
|
moveTo/lineTo/qCurveTo/curveTo methods take a list of tuples that are
|
|
|
|
arguments that would normally be passed to a SegmentPen, one item for
|
|
|
|
each of the pens in other_pens.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, other_pens, max_err, reverse_direction=False):
|
|
|
|
if reverse_direction:
|
|
|
|
other_pens = [ReverseContourPen(pen, outputImpliedClosingLine=True) for pen in other_pens]
|
|
|
|
self.pens = other_pens
|
|
|
|
self.max_err = max_err
|
|
|
|
self.start_pts = None
|
|
|
|
self.current_pts = None
|
|
|
|
|
|
|
|
def _check_contour_is_open(self):
|
|
|
|
if self.current_pts is None:
|
|
|
|
raise AssertionError("moveTo is required")
|
|
|
|
|
|
|
|
def _check_contour_is_closed(self):
|
|
|
|
if self.current_pts is not None:
|
|
|
|
raise AssertionError("closePath or endPath is required")
|
|
|
|
|
|
|
|
def _add_moveTo(self):
|
|
|
|
if self.start_pts is not None:
|
|
|
|
for pt,pen in zip(self.start_pts, self.pens):
|
|
|
|
pen.moveTo(*pt)
|
|
|
|
self.start_pts = None
|
|
|
|
|
|
|
|
def moveTo(self, pts):
|
|
|
|
self._check_contour_is_closed()
|
|
|
|
self.start_pts = self.current_pts = pts
|
|
|
|
self._add_moveTo()
|
|
|
|
|
|
|
|
def lineTo(self, pts):
|
|
|
|
self._check_contour_is_open()
|
|
|
|
self._add_moveTo()
|
|
|
|
for pt,pen in zip(pts, self.pens):
|
|
|
|
pen.lineTo(*pt)
|
|
|
|
self.current_pts = pts
|
|
|
|
|
|
|
|
def qCurveTo(self, pointsList):
|
|
|
|
self._check_contour_is_open()
|
|
|
|
if len(pointsList[0]) == 1:
|
|
|
|
self.lineTo([points[0] for points in pointsList])
|
|
|
|
return
|
|
|
|
self._add_moveTo()
|
|
|
|
current_pts = []
|
|
|
|
for points,pen in zip(pointsList, self.pens):
|
|
|
|
pen.qCurveTo(*points)
|
|
|
|
current_pts.append((points[-1],))
|
|
|
|
self.current_pts = current_pts
|
|
|
|
|
|
|
|
def _curves_to_quadratic(self, pointsList):
|
|
|
|
curves = []
|
|
|
|
for current_pt,points in zip(self.current_pts, pointsList):
|
|
|
|
curves.append(current_pt + points)
|
|
|
|
quadratics = curves_to_quadratic(curves, [self.max_err] * len(curves))
|
|
|
|
pointsList = []
|
|
|
|
for quadratic in quadratics:
|
|
|
|
pointsList.append(quadratic[1:])
|
|
|
|
self.qCurveTo(pointsList)
|
|
|
|
|
|
|
|
def curveTo(self, pointsList):
|
|
|
|
self._check_contour_is_open()
|
|
|
|
self._curves_to_quadratic(pointsList)
|
|
|
|
|
|
|
|
def closePath(self):
|
|
|
|
self._check_contour_is_open()
|
|
|
|
if self.start_pts is None:
|
|
|
|
for pen in self.pens:
|
|
|
|
pen.closePath()
|
|
|
|
self.current_pts = self.start_pts = None
|
|
|
|
|
|
|
|
def endPath(self):
|
|
|
|
self._check_contour_is_open()
|
|
|
|
if self.start_pts is None:
|
|
|
|
for pen in self.pens:
|
|
|
|
pen.endPath()
|
|
|
|
self.current_pts = self.start_pts = None
|
|
|
|
|
|
|
|
def addComponent(self, glyphName, transformations):
|
|
|
|
self._check_contour_is_closed()
|
|
|
|
for trans,pen in zip(transformations, self.pens):
|
|
|
|
pen.addComponent(glyphName, trans)
|