fontlab-python-pens 1.0.8__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- fontlab_pens/__init__.py +168 -0
- fontlab_pens/_native.py +227 -0
- fontlab_pens/base.py +332 -0
- fontlab_pens/filter.py +181 -0
- fontlab_pens/fontgate.py +276 -0
- fontlab_pens/fontlab.py +275 -0
- fontlab_pens/measure.py +423 -0
- fontlab_pens/point.py +536 -0
- fontlab_pens/qt.py +82 -0
- fontlab_pens/recording.py +159 -0
- fontlab_pens/reverse.py +103 -0
- fontlab_pens/rounding.py +116 -0
- fontlab_pens/stroke.py +420 -0
- fontlab_pens/transform.py +225 -0
- fontlab_python_pens-1.0.8.dist-info/METADATA +225 -0
- fontlab_python_pens-1.0.8.dist-info/RECORD +19 -0
- fontlab_python_pens-1.0.8.dist-info/WHEEL +4 -0
- fontlab_python_pens-1.0.8.dist-info/licenses/LICENSE +201 -0
- fontlab_python_pens-1.0.8.dist-info/licenses/NOTICE +46 -0
fontlab_pens/base.py
ADDED
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
# this_file: src/fontlab_pens/base.py
|
|
2
|
+
# Derived from fontTools Lib/fontTools/pens/basePen.py (MIT, (c) Just van Rossum; see NOTICE)
|
|
3
|
+
"""Segment-pen protocol: abstract pens, `BasePen` and segment decomposition.
|
|
4
|
+
|
|
5
|
+
A pen standardises how outlines are drawn: `outline.draw(pen)` makes the outline
|
|
6
|
+
call `moveTo`, `lineTo`, `curveTo`, `qCurveTo`, `closePath`/`endPath` and
|
|
7
|
+
`addComponent` on the pen. This module is a dependency-free port of the
|
|
8
|
+
fontTools module so FontLab code can speak the protocol without fontTools.
|
|
9
|
+
`BasePen` subclasses override `_moveTo`, `_lineTo`, `_curveToOne` (and
|
|
10
|
+
optionally `_qCurveToOne`, `_closePath`, `_endPath`); it splits super-beziers
|
|
11
|
+
and TrueType quadratic strings, including the off-curve-only `None` form.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
from __future__ import annotations
|
|
15
|
+
|
|
16
|
+
import logging
|
|
17
|
+
from collections.abc import Mapping, Sequence
|
|
18
|
+
from typing import Any
|
|
19
|
+
|
|
20
|
+
__all__ = [
|
|
21
|
+
"AbstractPen",
|
|
22
|
+
"BasePen",
|
|
23
|
+
"DecomposingPen",
|
|
24
|
+
"MissingComponentError",
|
|
25
|
+
"NullPen",
|
|
26
|
+
"OpenContourError",
|
|
27
|
+
"PenError",
|
|
28
|
+
"decomposeQuadraticSegment",
|
|
29
|
+
"decomposeSuperBezierSegment",
|
|
30
|
+
]
|
|
31
|
+
|
|
32
|
+
Point = tuple[float, float]
|
|
33
|
+
TransformLike = Sequence[float]
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
try: # optional: when fontTools is installed, its handlers also catch our errors
|
|
37
|
+
from fontTools.pens.basePen import PenError as _BasePenError
|
|
38
|
+
except ImportError: # fontTools is not a dependency
|
|
39
|
+
_BasePenError = Exception
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
class PenError(_BasePenError):
|
|
43
|
+
"""Represents an error during penning.
|
|
44
|
+
|
|
45
|
+
Subclasses `fontTools.pens.basePen.PenError` when fontTools is importable,
|
|
46
|
+
so `except fontTools.pens.basePen.PenError` catches errors from these pens.
|
|
47
|
+
"""
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
class OpenContourError(PenError):
|
|
51
|
+
"""Raised by pens that cannot handle open contours."""
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
class _LogMixin:
|
|
55
|
+
"""Gives a class a `log` property named `<module>.<class>` (fontTools' LogMixin)."""
|
|
56
|
+
|
|
57
|
+
@property
|
|
58
|
+
def log(self) -> logging.Logger:
|
|
59
|
+
cls = type(self)
|
|
60
|
+
return logging.getLogger(f"{cls.__module__}.{cls.__name__}")
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
class AbstractPen:
|
|
64
|
+
"""The segment-pen protocol; subclassing documents that a class implements it."""
|
|
65
|
+
|
|
66
|
+
def moveTo(self, pt: Point) -> None:
|
|
67
|
+
"""Begin a new sub path at `pt`; end it with `closePath` or `endPath`."""
|
|
68
|
+
raise NotImplementedError
|
|
69
|
+
|
|
70
|
+
def lineTo(self, pt: Point) -> None:
|
|
71
|
+
"""Draw a straight line from the current point to `pt`."""
|
|
72
|
+
raise NotImplementedError
|
|
73
|
+
|
|
74
|
+
def curveTo(self, *points: Point) -> None:
|
|
75
|
+
"""Draw a cubic bezier; more than two off-curves form a super-bezier."""
|
|
76
|
+
raise NotImplementedError
|
|
77
|
+
|
|
78
|
+
def qCurveTo(self, *points: Point | None) -> None:
|
|
79
|
+
"""Draw a TrueType quadratic string; the last point may be `None`."""
|
|
80
|
+
raise NotImplementedError
|
|
81
|
+
|
|
82
|
+
def closePath(self) -> None:
|
|
83
|
+
"""Close the current sub path."""
|
|
84
|
+
|
|
85
|
+
def endPath(self) -> None:
|
|
86
|
+
"""End the current sub path without closing it."""
|
|
87
|
+
|
|
88
|
+
def addComponent(self, glyphName: str, transformation: TransformLike) -> None:
|
|
89
|
+
"""Add a sub glyph with a 6-number affine transformation."""
|
|
90
|
+
raise NotImplementedError
|
|
91
|
+
|
|
92
|
+
def addVarComponent(
|
|
93
|
+
self,
|
|
94
|
+
glyphName: str,
|
|
95
|
+
transformation: Any,
|
|
96
|
+
location: dict[str, float],
|
|
97
|
+
*,
|
|
98
|
+
resetUnspecifiedAxes: bool = False,
|
|
99
|
+
) -> None:
|
|
100
|
+
"""Add a variable component; glyph sets decompose these, so pens raise."""
|
|
101
|
+
raise AttributeError
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
class NullPen(AbstractPen):
|
|
105
|
+
"""A pen that does nothing."""
|
|
106
|
+
|
|
107
|
+
def moveTo(self, pt):
|
|
108
|
+
pass
|
|
109
|
+
|
|
110
|
+
def lineTo(self, pt):
|
|
111
|
+
pass
|
|
112
|
+
|
|
113
|
+
def curveTo(self, *points):
|
|
114
|
+
pass
|
|
115
|
+
|
|
116
|
+
def qCurveTo(self, *points):
|
|
117
|
+
pass
|
|
118
|
+
|
|
119
|
+
def closePath(self):
|
|
120
|
+
pass
|
|
121
|
+
|
|
122
|
+
def endPath(self):
|
|
123
|
+
pass
|
|
124
|
+
|
|
125
|
+
def addComponent(self, glyphName, transformation):
|
|
126
|
+
pass
|
|
127
|
+
|
|
128
|
+
def addVarComponent(
|
|
129
|
+
self, glyphName, transformation, location, *, resetUnspecifiedAxes=False
|
|
130
|
+
):
|
|
131
|
+
pass
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
class MissingComponentError(KeyError):
|
|
135
|
+
"""A component points to a glyph that is not in the glyph set."""
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
class DecomposingPen(_LogMixin, AbstractPen):
|
|
139
|
+
"""Mixin whose `addComponent` draws the base glyph onto `self` as contours.
|
|
140
|
+
|
|
141
|
+
Missing base glyphs log a warning, or raise `MissingComponentError` when
|
|
142
|
+
`skipMissingComponents` is False. `reverseFlipped=True` reverses contours of
|
|
143
|
+
components whose transformation has a negative determinant.
|
|
144
|
+
"""
|
|
145
|
+
|
|
146
|
+
skipMissingComponents = True
|
|
147
|
+
MissingComponentError = MissingComponentError
|
|
148
|
+
|
|
149
|
+
def __init__(
|
|
150
|
+
self,
|
|
151
|
+
glyphSet: Mapping[str, Any] | None,
|
|
152
|
+
*args: Any,
|
|
153
|
+
skipMissingComponents: bool | None = None,
|
|
154
|
+
reverseFlipped: bool = False,
|
|
155
|
+
**kwargs: Any,
|
|
156
|
+
) -> None:
|
|
157
|
+
super().__init__(*args, **kwargs)
|
|
158
|
+
self.glyphSet = glyphSet
|
|
159
|
+
self.skipMissingComponents = (
|
|
160
|
+
self.__class__.skipMissingComponents
|
|
161
|
+
if skipMissingComponents is None
|
|
162
|
+
else skipMissingComponents
|
|
163
|
+
)
|
|
164
|
+
self.reverseFlipped = reverseFlipped
|
|
165
|
+
|
|
166
|
+
def addComponent(self, glyphName, transformation):
|
|
167
|
+
"""Transform the points of the base glyph and draw it onto self."""
|
|
168
|
+
# Imported here: transform/reverse import this module (cycle).
|
|
169
|
+
from fontlab_pens.reverse import ReverseContourPen
|
|
170
|
+
from fontlab_pens.transform import Identity, TransformPen
|
|
171
|
+
|
|
172
|
+
try:
|
|
173
|
+
glyph = self.glyphSet[glyphName]
|
|
174
|
+
except KeyError:
|
|
175
|
+
if not self.skipMissingComponents:
|
|
176
|
+
raise MissingComponentError(glyphName) from None
|
|
177
|
+
self.log.warning("glyph '%s' is missing from glyphSet; skipped", glyphName)
|
|
178
|
+
else:
|
|
179
|
+
pen = self
|
|
180
|
+
if transformation != Identity:
|
|
181
|
+
pen = TransformPen(pen, transformation)
|
|
182
|
+
if self.reverseFlipped:
|
|
183
|
+
a, b, c, d = transformation[:4]
|
|
184
|
+
if a * d - b * c < 0:
|
|
185
|
+
pen = ReverseContourPen(pen)
|
|
186
|
+
glyph.draw(pen)
|
|
187
|
+
|
|
188
|
+
def addVarComponent(
|
|
189
|
+
self, glyphName, transformation, location, *, resetUnspecifiedAxes=False
|
|
190
|
+
):
|
|
191
|
+
raise AttributeError
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
class BasePen(DecomposingPen):
|
|
195
|
+
"""Base class for drawing pens.
|
|
196
|
+
|
|
197
|
+
Override `_moveTo`, `_lineTo` and `_curveToOne`; optionally `_closePath`,
|
|
198
|
+
`_endPath`, `_qCurveToOne` and `addComponent`. Do not override the public
|
|
199
|
+
drawing methods: they track the current point and split compound segments.
|
|
200
|
+
"""
|
|
201
|
+
|
|
202
|
+
def __init__(self, glyphSet: Mapping[str, Any] | None = None) -> None:
|
|
203
|
+
super().__init__(glyphSet)
|
|
204
|
+
self.__currentPoint: Point | None = None
|
|
205
|
+
|
|
206
|
+
def _moveTo(self, pt):
|
|
207
|
+
raise NotImplementedError
|
|
208
|
+
|
|
209
|
+
def _lineTo(self, pt):
|
|
210
|
+
raise NotImplementedError
|
|
211
|
+
|
|
212
|
+
def _curveToOne(self, pt1, pt2, pt3):
|
|
213
|
+
raise NotImplementedError
|
|
214
|
+
|
|
215
|
+
def _closePath(self):
|
|
216
|
+
pass
|
|
217
|
+
|
|
218
|
+
def _endPath(self):
|
|
219
|
+
pass
|
|
220
|
+
|
|
221
|
+
def _qCurveToOne(self, pt1, pt2):
|
|
222
|
+
"""Draw one quadratic segment; the default elevates it to a cubic."""
|
|
223
|
+
pt0x, pt0y = self.__currentPoint
|
|
224
|
+
pt1x, pt1y = pt1
|
|
225
|
+
pt2x, pt2y = pt2
|
|
226
|
+
mid1x = pt0x + 0.66666666666666667 * (pt1x - pt0x)
|
|
227
|
+
mid1y = pt0y + 0.66666666666666667 * (pt1y - pt0y)
|
|
228
|
+
mid2x = pt2x + 0.66666666666666667 * (pt1x - pt2x)
|
|
229
|
+
mid2y = pt2y + 0.66666666666666667 * (pt1y - pt2y)
|
|
230
|
+
self._curveToOne((mid1x, mid1y), (mid2x, mid2y), pt2)
|
|
231
|
+
|
|
232
|
+
def _getCurrentPoint(self) -> Point | None:
|
|
233
|
+
"""Return the current point (for subclasses; not public protocol)."""
|
|
234
|
+
return self.__currentPoint
|
|
235
|
+
|
|
236
|
+
def closePath(self):
|
|
237
|
+
self._closePath()
|
|
238
|
+
self.__currentPoint = None
|
|
239
|
+
|
|
240
|
+
def endPath(self):
|
|
241
|
+
self._endPath()
|
|
242
|
+
self.__currentPoint = None
|
|
243
|
+
|
|
244
|
+
def moveTo(self, pt):
|
|
245
|
+
self._moveTo(pt)
|
|
246
|
+
self.__currentPoint = pt
|
|
247
|
+
|
|
248
|
+
def lineTo(self, pt):
|
|
249
|
+
self._lineTo(pt)
|
|
250
|
+
self.__currentPoint = pt
|
|
251
|
+
|
|
252
|
+
def curveTo(self, *points):
|
|
253
|
+
n = len(points) - 1 # number of control points
|
|
254
|
+
assert n >= 0
|
|
255
|
+
if n == 2:
|
|
256
|
+
self._curveToOne(*points)
|
|
257
|
+
self.__currentPoint = points[-1]
|
|
258
|
+
elif n > 2:
|
|
259
|
+
for pt1, pt2, pt3 in decomposeSuperBezierSegment(points):
|
|
260
|
+
self._curveToOne(pt1, pt2, pt3)
|
|
261
|
+
self.__currentPoint = pt3
|
|
262
|
+
elif n == 1:
|
|
263
|
+
self.qCurveTo(*points)
|
|
264
|
+
else:
|
|
265
|
+
self.lineTo(points[0])
|
|
266
|
+
|
|
267
|
+
def qCurveTo(self, *points):
|
|
268
|
+
n = len(points) - 1 # number of control points
|
|
269
|
+
assert n >= 0
|
|
270
|
+
if points[-1] is None:
|
|
271
|
+
# Off-curve-only TrueType contour: make the implied on-curve point
|
|
272
|
+
# between the last and first off-curves explicit and start there.
|
|
273
|
+
x, y = points[-2]
|
|
274
|
+
nx, ny = points[0]
|
|
275
|
+
impliedStartPoint = (0.5 * (x + nx), 0.5 * (y + ny))
|
|
276
|
+
self.__currentPoint = impliedStartPoint
|
|
277
|
+
self._moveTo(impliedStartPoint)
|
|
278
|
+
points = points[:-1] + (impliedStartPoint,)
|
|
279
|
+
if n > 0:
|
|
280
|
+
for pt1, pt2 in decomposeQuadraticSegment(points):
|
|
281
|
+
self._qCurveToOne(pt1, pt2)
|
|
282
|
+
self.__currentPoint = pt2
|
|
283
|
+
else:
|
|
284
|
+
self.lineTo(points[0])
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
def decomposeSuperBezierSegment(points: Sequence[Point]) -> list[tuple[Point, Point, Point]]:
|
|
288
|
+
"""Split a super-bezier (2+ off-curves, then the on-curve) into cubic segments.
|
|
289
|
+
|
|
290
|
+
The start point is not supplied. Returns `(pt1, pt2, pt3)` tuples for
|
|
291
|
+
`curveTo`; the split mirrors TrueType's implied points for smooth joins.
|
|
292
|
+
"""
|
|
293
|
+
n = len(points) - 1
|
|
294
|
+
assert n > 1
|
|
295
|
+
bezierSegments = []
|
|
296
|
+
pt1, pt2, pt3 = points[0], None, None
|
|
297
|
+
for i in range(2, n + 1):
|
|
298
|
+
nDivisions = min(i, 3, n - i + 2)
|
|
299
|
+
for j in range(1, nDivisions):
|
|
300
|
+
factor = j / nDivisions
|
|
301
|
+
temp1 = points[i - 1]
|
|
302
|
+
temp2 = points[i - 2]
|
|
303
|
+
temp = (
|
|
304
|
+
temp2[0] + factor * (temp1[0] - temp2[0]),
|
|
305
|
+
temp2[1] + factor * (temp1[1] - temp2[1]),
|
|
306
|
+
)
|
|
307
|
+
if pt2 is None:
|
|
308
|
+
pt2 = temp
|
|
309
|
+
else:
|
|
310
|
+
pt3 = (0.5 * (pt2[0] + temp[0]), 0.5 * (pt2[1] + temp[1]))
|
|
311
|
+
bezierSegments.append((pt1, pt2, pt3))
|
|
312
|
+
pt1, pt2, pt3 = temp, None, None
|
|
313
|
+
bezierSegments.append((pt1, points[-2], points[-1]))
|
|
314
|
+
return bezierSegments
|
|
315
|
+
|
|
316
|
+
|
|
317
|
+
def decomposeQuadraticSegment(points: Sequence[Point]) -> list[tuple[Point, Point]]:
|
|
318
|
+
"""Split a TrueType quadratic string into `(offCurve, onCurve)` segments.
|
|
319
|
+
|
|
320
|
+
Between two consecutive off-curves lies an implied on-curve midpoint. The
|
|
321
|
+
start point is not supplied.
|
|
322
|
+
"""
|
|
323
|
+
n = len(points) - 1
|
|
324
|
+
assert n > 0
|
|
325
|
+
quadSegments = []
|
|
326
|
+
for i in range(n - 1):
|
|
327
|
+
x, y = points[i]
|
|
328
|
+
nx, ny = points[i + 1]
|
|
329
|
+
impliedPt = (0.5 * (x + nx), 0.5 * (y + ny))
|
|
330
|
+
quadSegments.append((points[i], impliedPt))
|
|
331
|
+
quadSegments.append((points[-2], points[-1]))
|
|
332
|
+
return quadSegments
|
fontlab_pens/filter.py
ADDED
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
# this_file: src/fontlab_pens/filter.py
|
|
2
|
+
# Derived from fontTools Lib/fontTools/pens/filterPen.py (MIT, (c) Just van Rossum; see NOTICE)
|
|
3
|
+
"""Filter pens: pass drawing calls through to another pen, optionally altered.
|
|
4
|
+
|
|
5
|
+
Subclass these to transform, round or reverse outlines on the way to an output
|
|
6
|
+
pen. Components pass through unchanged unless a subclass overrides
|
|
7
|
+
`addComponent`.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
from fontlab_pens.base import AbstractPen
|
|
13
|
+
from fontlab_pens.point import AbstractPointPen
|
|
14
|
+
from fontlab_pens.recording import RecordingPen
|
|
15
|
+
|
|
16
|
+
__all__ = [
|
|
17
|
+
"ContourFilterPen",
|
|
18
|
+
"ContourFilterPointPen",
|
|
19
|
+
"FilterPen",
|
|
20
|
+
"FilterPointPen",
|
|
21
|
+
"OnCurveFirstPointPen",
|
|
22
|
+
]
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class _PassThruComponentsMixin:
|
|
26
|
+
def addComponent(self, glyphName, transformation, **kwargs):
|
|
27
|
+
self._outPen.addComponent(glyphName, transformation, **kwargs)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class FilterPen(_PassThruComponentsMixin, AbstractPen):
|
|
31
|
+
"""Segment pen that forwards every call unchanged; override to filter.
|
|
32
|
+
|
|
33
|
+
Tracks the last on-curve point in `current_pt`.
|
|
34
|
+
"""
|
|
35
|
+
|
|
36
|
+
def __init__(self, outPen: AbstractPen) -> None:
|
|
37
|
+
self._outPen = outPen
|
|
38
|
+
self.current_pt = None
|
|
39
|
+
|
|
40
|
+
def moveTo(self, pt):
|
|
41
|
+
self._outPen.moveTo(pt)
|
|
42
|
+
self.current_pt = pt
|
|
43
|
+
|
|
44
|
+
def lineTo(self, pt):
|
|
45
|
+
self._outPen.lineTo(pt)
|
|
46
|
+
self.current_pt = pt
|
|
47
|
+
|
|
48
|
+
def curveTo(self, *points):
|
|
49
|
+
self._outPen.curveTo(*points)
|
|
50
|
+
self.current_pt = points[-1]
|
|
51
|
+
|
|
52
|
+
def qCurveTo(self, *points):
|
|
53
|
+
self._outPen.qCurveTo(*points)
|
|
54
|
+
self.current_pt = points[-1]
|
|
55
|
+
|
|
56
|
+
def closePath(self):
|
|
57
|
+
self._outPen.closePath()
|
|
58
|
+
self.current_pt = None
|
|
59
|
+
|
|
60
|
+
def endPath(self):
|
|
61
|
+
self._outPen.endPath()
|
|
62
|
+
self.current_pt = None
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
class ContourFilterPen(_PassThruComponentsMixin, RecordingPen):
|
|
66
|
+
"""Buffers each contour, passes it through `filterContour`, then draws it."""
|
|
67
|
+
|
|
68
|
+
def __init__(self, outPen: AbstractPen) -> None:
|
|
69
|
+
super().__init__()
|
|
70
|
+
self._outPen = outPen
|
|
71
|
+
|
|
72
|
+
def closePath(self):
|
|
73
|
+
super().closePath()
|
|
74
|
+
self._flushContour()
|
|
75
|
+
|
|
76
|
+
def endPath(self):
|
|
77
|
+
super().endPath()
|
|
78
|
+
self._flushContour()
|
|
79
|
+
|
|
80
|
+
def _flushContour(self):
|
|
81
|
+
result = self.filterContour(self.value)
|
|
82
|
+
if result is not None:
|
|
83
|
+
self.value = result
|
|
84
|
+
self.replay(self._outPen)
|
|
85
|
+
self.value = []
|
|
86
|
+
|
|
87
|
+
def filterContour(self, contour):
|
|
88
|
+
"""Override: modify the `(operator, operands)` list in place or return a new one."""
|
|
89
|
+
return
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
class FilterPointPen(_PassThruComponentsMixin, AbstractPointPen):
|
|
93
|
+
"""Point pen that forwards every call unchanged; override to filter."""
|
|
94
|
+
|
|
95
|
+
def __init__(self, outPen: AbstractPointPen) -> None:
|
|
96
|
+
self._outPen = outPen
|
|
97
|
+
|
|
98
|
+
def beginPath(self, identifier=None, **kwargs):
|
|
99
|
+
kwargs = dict(kwargs)
|
|
100
|
+
if identifier is not None:
|
|
101
|
+
kwargs["identifier"] = identifier
|
|
102
|
+
self._outPen.beginPath(**kwargs)
|
|
103
|
+
|
|
104
|
+
def endPath(self):
|
|
105
|
+
self._outPen.endPath()
|
|
106
|
+
|
|
107
|
+
def addPoint(
|
|
108
|
+
self, pt, segmentType=None, smooth=False, name=None, identifier=None, **kwargs
|
|
109
|
+
):
|
|
110
|
+
kwargs = dict(kwargs)
|
|
111
|
+
if identifier is not None:
|
|
112
|
+
kwargs["identifier"] = identifier
|
|
113
|
+
self._outPen.addPoint(pt, segmentType, smooth, name, **kwargs)
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
class ContourFilterPointPen(_PassThruComponentsMixin, AbstractPointPen):
|
|
117
|
+
"""Buffers each point contour, passes it through `filterContour`, then draws it.
|
|
118
|
+
|
|
119
|
+
Contour items are `(pt, segmentType, smooth, name, kwargs)` tuples.
|
|
120
|
+
"""
|
|
121
|
+
|
|
122
|
+
def __init__(self, outPen: AbstractPointPen) -> None:
|
|
123
|
+
self._outPen = outPen
|
|
124
|
+
self.currentContour = None
|
|
125
|
+
self.currentContourKwargs = None
|
|
126
|
+
|
|
127
|
+
def beginPath(self, identifier=None, **kwargs):
|
|
128
|
+
if self.currentContour is not None:
|
|
129
|
+
raise ValueError("Path already begun")
|
|
130
|
+
kwargs = dict(kwargs)
|
|
131
|
+
if identifier is not None:
|
|
132
|
+
kwargs["identifier"] = identifier
|
|
133
|
+
self.currentContour = []
|
|
134
|
+
self.currentContourKwargs = kwargs
|
|
135
|
+
|
|
136
|
+
def endPath(self):
|
|
137
|
+
if self.currentContour is None:
|
|
138
|
+
raise ValueError("Path not begun")
|
|
139
|
+
self._flushContour()
|
|
140
|
+
self.currentContour = None
|
|
141
|
+
self.currentContourKwargs = None
|
|
142
|
+
|
|
143
|
+
def _flushContour(self):
|
|
144
|
+
result = self.filterContour(self.currentContour)
|
|
145
|
+
if result is not None:
|
|
146
|
+
self.currentContour = result
|
|
147
|
+
self._outPen.beginPath(**self.currentContourKwargs)
|
|
148
|
+
for pt, segmentType, smooth, name, kwargs in self.currentContour:
|
|
149
|
+
self._outPen.addPoint(pt, segmentType, smooth, name, **kwargs)
|
|
150
|
+
self._outPen.endPath()
|
|
151
|
+
|
|
152
|
+
def filterContour(self, contour):
|
|
153
|
+
"""Override: modify the point list in place or return a new one."""
|
|
154
|
+
return
|
|
155
|
+
|
|
156
|
+
def addPoint(
|
|
157
|
+
self, pt, segmentType=None, smooth=False, name=None, identifier=None, **kwargs
|
|
158
|
+
):
|
|
159
|
+
if self.currentContour is None:
|
|
160
|
+
raise ValueError("Path not begun")
|
|
161
|
+
kwargs = dict(kwargs)
|
|
162
|
+
if identifier is not None:
|
|
163
|
+
kwargs["identifier"] = identifier
|
|
164
|
+
self.currentContour.append((pt, segmentType, smooth, name, kwargs))
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
class OnCurveFirstPointPen(ContourFilterPointPen):
|
|
168
|
+
"""Rotates closed contours that start off-curve so they start on-curve.
|
|
169
|
+
|
|
170
|
+
Open contours and contours already starting on-curve pass unchanged.
|
|
171
|
+
"""
|
|
172
|
+
|
|
173
|
+
def filterContour(self, contour):
|
|
174
|
+
if not contour:
|
|
175
|
+
return None
|
|
176
|
+
is_closed = contour[0][1] != "move"
|
|
177
|
+
if is_closed and contour[0][1] is None:
|
|
178
|
+
for i, point in enumerate(contour):
|
|
179
|
+
if point[1] is not None:
|
|
180
|
+
return contour[i:] + contour[:i]
|
|
181
|
+
return None
|