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/point.py
ADDED
|
@@ -0,0 +1,536 @@
|
|
|
1
|
+
# this_file: src/fontlab_pens/point.py
|
|
2
|
+
# Derived from fontTools Lib/fontTools/pens/pointPen.py (MIT, (c) Just van Rossum; see NOTICE)
|
|
3
|
+
"""Point-pen protocol and the adapters between point and segment pens.
|
|
4
|
+
|
|
5
|
+
A point pen receives every point of a contour (`beginPath`, `addPoint`,
|
|
6
|
+
`endPath`, `addComponent`), so it keeps data segment pens drop: smooth flags,
|
|
7
|
+
point names and identifiers. FontLab nodes map onto points one to one, which is
|
|
8
|
+
why the native FontLab pens in this package are point pens first.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
|
|
13
|
+
import math
|
|
14
|
+
from collections.abc import Mapping
|
|
15
|
+
from enum import StrEnum
|
|
16
|
+
from typing import Any
|
|
17
|
+
|
|
18
|
+
from fontlab_pens.base import (
|
|
19
|
+
AbstractPen,
|
|
20
|
+
MissingComponentError,
|
|
21
|
+
PenError,
|
|
22
|
+
_LogMixin,
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
__all__ = [
|
|
26
|
+
"AbstractPointPen",
|
|
27
|
+
"BasePointToSegmentPen",
|
|
28
|
+
"DecomposingPointPen",
|
|
29
|
+
"GuessSmoothPointPen",
|
|
30
|
+
"PointToSegmentPen",
|
|
31
|
+
"ReverseContourPointPen",
|
|
32
|
+
"ReverseFlipped",
|
|
33
|
+
"SegmentToPointPen",
|
|
34
|
+
]
|
|
35
|
+
|
|
36
|
+
Point = tuple[float, float]
|
|
37
|
+
# [(pt, smooth, name, kwargs)]
|
|
38
|
+
SegmentPointList = list[tuple[Point | None, bool, str | None, Any]]
|
|
39
|
+
SegmentList = list[tuple[str | None, SegmentPointList]]
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
class ReverseFlipped(StrEnum):
|
|
43
|
+
"""How decomposition treats flipped (negative-determinant) components.
|
|
44
|
+
|
|
45
|
+
NO keeps direction; KEEP_START reverses keeping the start point;
|
|
46
|
+
ON_CURVE_FIRST reverses and then rotates so the contour starts on-curve.
|
|
47
|
+
"""
|
|
48
|
+
|
|
49
|
+
NO = "no"
|
|
50
|
+
KEEP_START = "keep_start"
|
|
51
|
+
ON_CURVE_FIRST = "on_curve_first"
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
class AbstractPointPen:
|
|
55
|
+
"""Base class for all point pens."""
|
|
56
|
+
|
|
57
|
+
def beginPath(self, identifier: str | None = None, **kwargs: Any) -> None:
|
|
58
|
+
"""Start a new sub path."""
|
|
59
|
+
raise NotImplementedError
|
|
60
|
+
|
|
61
|
+
def endPath(self) -> None:
|
|
62
|
+
"""End the current sub path."""
|
|
63
|
+
raise NotImplementedError
|
|
64
|
+
|
|
65
|
+
def addPoint(
|
|
66
|
+
self,
|
|
67
|
+
pt: Point,
|
|
68
|
+
segmentType: str | None = None,
|
|
69
|
+
smooth: bool = False,
|
|
70
|
+
name: str | None = None,
|
|
71
|
+
identifier: str | None = None,
|
|
72
|
+
**kwargs: Any,
|
|
73
|
+
) -> None:
|
|
74
|
+
"""Add a point; `segmentType` is None for off-curves."""
|
|
75
|
+
raise NotImplementedError
|
|
76
|
+
|
|
77
|
+
def addComponent(
|
|
78
|
+
self,
|
|
79
|
+
baseGlyphName: str,
|
|
80
|
+
transformation: tuple[float, float, float, float, float, float],
|
|
81
|
+
identifier: str | None = None,
|
|
82
|
+
**kwargs: Any,
|
|
83
|
+
) -> None:
|
|
84
|
+
"""Add a sub glyph."""
|
|
85
|
+
raise NotImplementedError
|
|
86
|
+
|
|
87
|
+
def addVarComponent(
|
|
88
|
+
self,
|
|
89
|
+
glyphName: str,
|
|
90
|
+
transformation: Any,
|
|
91
|
+
location: dict[str, float],
|
|
92
|
+
identifier: str | None = None,
|
|
93
|
+
*,
|
|
94
|
+
resetUnspecifiedAxes: bool = False,
|
|
95
|
+
**kwargs: Any,
|
|
96
|
+
) -> None:
|
|
97
|
+
"""Add a variable component; glyph sets decompose these, so pens raise."""
|
|
98
|
+
raise AttributeError
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
class BasePointToSegmentPen(AbstractPointPen):
|
|
102
|
+
"""Collects points per contour and hands `_flushContour` a segment list.
|
|
103
|
+
|
|
104
|
+
Each segment is `(segmentType, [(pt, smooth, name, kwargs), ...])`. An open
|
|
105
|
+
contour starts with a "move" segment; a closed one has none and its start
|
|
106
|
+
is the last point of the last segment. An off-curve-only contour yields one
|
|
107
|
+
"qcurve" segment ending in a `None` point.
|
|
108
|
+
"""
|
|
109
|
+
|
|
110
|
+
def __init__(self) -> None:
|
|
111
|
+
self.currentPath: list | None = None
|
|
112
|
+
|
|
113
|
+
def beginPath(self, identifier=None, **kwargs):
|
|
114
|
+
if self.currentPath is not None:
|
|
115
|
+
raise PenError("Path already begun.")
|
|
116
|
+
self.currentPath = []
|
|
117
|
+
|
|
118
|
+
def _flushContour(self, segments: SegmentList) -> None:
|
|
119
|
+
"""Override: receives the segments of each non-empty sub path."""
|
|
120
|
+
raise NotImplementedError
|
|
121
|
+
|
|
122
|
+
def endPath(self) -> None:
|
|
123
|
+
if self.currentPath is None:
|
|
124
|
+
raise PenError("Path not begun.")
|
|
125
|
+
points = self.currentPath
|
|
126
|
+
self.currentPath = None
|
|
127
|
+
if not points:
|
|
128
|
+
return
|
|
129
|
+
if len(points) == 1:
|
|
130
|
+
pt, segmentType, smooth, name, kwargs = points[0]
|
|
131
|
+
self._flushContour([("move", [(pt, smooth, name, kwargs)])])
|
|
132
|
+
return
|
|
133
|
+
segments: SegmentList = []
|
|
134
|
+
if points[0][1] == "move":
|
|
135
|
+
pt, segmentType, smooth, name, kwargs = points[0]
|
|
136
|
+
segments.append(("move", [(pt, smooth, name, kwargs)]))
|
|
137
|
+
points.pop(0)
|
|
138
|
+
else:
|
|
139
|
+
# Closed: rotate so the list ends with the first on-curve point.
|
|
140
|
+
firstOnCurve = None
|
|
141
|
+
for i in range(len(points)):
|
|
142
|
+
if points[i][1] is not None:
|
|
143
|
+
firstOnCurve = i
|
|
144
|
+
break
|
|
145
|
+
if firstOnCurve is None:
|
|
146
|
+
points.append((None, "qcurve", None, None, None))
|
|
147
|
+
else:
|
|
148
|
+
points = points[firstOnCurve + 1 :] + points[: firstOnCurve + 1]
|
|
149
|
+
|
|
150
|
+
currentSegment: SegmentPointList = []
|
|
151
|
+
for pt, segmentType, smooth, name, kwargs in points:
|
|
152
|
+
currentSegment.append((pt, smooth, name, kwargs))
|
|
153
|
+
if segmentType is None:
|
|
154
|
+
continue
|
|
155
|
+
segments.append((segmentType, currentSegment))
|
|
156
|
+
currentSegment = []
|
|
157
|
+
|
|
158
|
+
self._flushContour(segments)
|
|
159
|
+
|
|
160
|
+
def addPoint(
|
|
161
|
+
self, pt, segmentType=None, smooth=False, name=None, identifier=None, **kwargs
|
|
162
|
+
):
|
|
163
|
+
if self.currentPath is None:
|
|
164
|
+
raise PenError("Path not begun")
|
|
165
|
+
self.currentPath.append((pt, segmentType, smooth, name, kwargs))
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
class PointToSegmentPen(BasePointToSegmentPen):
|
|
169
|
+
"""Adapts the point-pen protocol to a segment pen (drops names and kwargs).
|
|
170
|
+
|
|
171
|
+
A closed contour's final line back to the start is implied and omitted,
|
|
172
|
+
unless `outputImpliedClosingLine` is set or the last point duplicates the
|
|
173
|
+
start (then the line is kept so the duplicate point survives).
|
|
174
|
+
"""
|
|
175
|
+
|
|
176
|
+
def __init__(self, segmentPen: AbstractPen, outputImpliedClosingLine: bool = False) -> None:
|
|
177
|
+
BasePointToSegmentPen.__init__(self)
|
|
178
|
+
self.pen = segmentPen
|
|
179
|
+
self.outputImpliedClosingLine = outputImpliedClosingLine
|
|
180
|
+
|
|
181
|
+
def _flushContour(self, segments):
|
|
182
|
+
if not segments:
|
|
183
|
+
raise PenError("Must have at least one segment.")
|
|
184
|
+
pen = self.pen
|
|
185
|
+
if segments[0][0] == "move":
|
|
186
|
+
closed = False
|
|
187
|
+
points = segments[0][1]
|
|
188
|
+
if len(points) != 1:
|
|
189
|
+
raise PenError(f"Illegal move segment point count: {len(points)}")
|
|
190
|
+
movePt, _, _, _ = points[0]
|
|
191
|
+
del segments[0]
|
|
192
|
+
else:
|
|
193
|
+
closed = True
|
|
194
|
+
segmentType, points = segments[-1]
|
|
195
|
+
movePt, _, _, _ = points[-1]
|
|
196
|
+
if movePt is not None:
|
|
197
|
+
# None: off-curve-only quadratic contour, which has no moveTo.
|
|
198
|
+
pen.moveTo(movePt)
|
|
199
|
+
outputImpliedClosingLine = self.outputImpliedClosingLine
|
|
200
|
+
nSegments = len(segments)
|
|
201
|
+
lastPt = movePt
|
|
202
|
+
for i in range(nSegments):
|
|
203
|
+
segmentType, points = segments[i]
|
|
204
|
+
points = [pt for pt, _, _, _ in points]
|
|
205
|
+
if segmentType == "line":
|
|
206
|
+
if len(points) != 1:
|
|
207
|
+
raise PenError(f"Illegal line segment point count: {len(points)}")
|
|
208
|
+
pt = points[0]
|
|
209
|
+
# See https://github.com/googlefonts/fontmake/issues/572.
|
|
210
|
+
if (
|
|
211
|
+
i + 1 != nSegments
|
|
212
|
+
or outputImpliedClosingLine
|
|
213
|
+
or not closed
|
|
214
|
+
or pt == lastPt
|
|
215
|
+
):
|
|
216
|
+
pen.lineTo(pt)
|
|
217
|
+
lastPt = pt
|
|
218
|
+
elif segmentType == "curve":
|
|
219
|
+
pen.curveTo(*points)
|
|
220
|
+
lastPt = points[-1]
|
|
221
|
+
elif segmentType == "qcurve":
|
|
222
|
+
pen.qCurveTo(*points)
|
|
223
|
+
lastPt = points[-1]
|
|
224
|
+
else:
|
|
225
|
+
raise PenError(f"Illegal segmentType: {segmentType}")
|
|
226
|
+
if closed:
|
|
227
|
+
pen.closePath()
|
|
228
|
+
else:
|
|
229
|
+
pen.endPath()
|
|
230
|
+
|
|
231
|
+
def addComponent(self, glyphName, transform, identifier=None, **kwargs):
|
|
232
|
+
self.pen.addComponent(glyphName, transform)
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
class SegmentToPointPen(AbstractPen):
|
|
236
|
+
"""Adapts the segment-pen protocol to a point pen.
|
|
237
|
+
|
|
238
|
+
With `guessSmooth` (default) the output passes through `GuessSmoothPointPen`.
|
|
239
|
+
"""
|
|
240
|
+
|
|
241
|
+
def __init__(self, pointPen: AbstractPointPen, guessSmooth: bool = True) -> None:
|
|
242
|
+
self.pen = GuessSmoothPointPen(pointPen) if guessSmooth else pointPen
|
|
243
|
+
self.contour: list[tuple[Point, str | None]] | None = None
|
|
244
|
+
|
|
245
|
+
def _flushContour(self) -> None:
|
|
246
|
+
pen = self.pen
|
|
247
|
+
pen.beginPath()
|
|
248
|
+
for pt, segmentType in self.contour:
|
|
249
|
+
pen.addPoint(pt, segmentType=segmentType)
|
|
250
|
+
pen.endPath()
|
|
251
|
+
|
|
252
|
+
def moveTo(self, pt):
|
|
253
|
+
self.contour = [(pt, "move")]
|
|
254
|
+
|
|
255
|
+
def lineTo(self, pt):
|
|
256
|
+
if self.contour is None:
|
|
257
|
+
raise PenError("Contour missing required initial moveTo")
|
|
258
|
+
self.contour.append((pt, "line"))
|
|
259
|
+
|
|
260
|
+
def curveTo(self, *pts):
|
|
261
|
+
if not pts:
|
|
262
|
+
raise TypeError("Must pass in at least one point")
|
|
263
|
+
if self.contour is None:
|
|
264
|
+
raise PenError("Contour missing required initial moveTo")
|
|
265
|
+
for pt in pts[:-1]:
|
|
266
|
+
self.contour.append((pt, None))
|
|
267
|
+
self.contour.append((pts[-1], "curve"))
|
|
268
|
+
|
|
269
|
+
def qCurveTo(self, *pts):
|
|
270
|
+
if not pts:
|
|
271
|
+
raise TypeError("Must pass in at least one point")
|
|
272
|
+
if pts[-1] is None:
|
|
273
|
+
self.contour = []
|
|
274
|
+
elif self.contour is None:
|
|
275
|
+
raise PenError("Contour missing required initial moveTo")
|
|
276
|
+
for pt in pts[:-1]:
|
|
277
|
+
self.contour.append((pt, None))
|
|
278
|
+
if pts[-1] is not None:
|
|
279
|
+
self.contour.append((pts[-1], "qcurve"))
|
|
280
|
+
|
|
281
|
+
def closePath(self):
|
|
282
|
+
if self.contour is None:
|
|
283
|
+
raise PenError("Contour missing required initial moveTo")
|
|
284
|
+
# Drop a last point duplicating the first only when both are on-curve;
|
|
285
|
+
# off-curve-only blobs keep every point (fonttools#4014).
|
|
286
|
+
if (
|
|
287
|
+
len(self.contour) > 1
|
|
288
|
+
and (self.contour[0][0] == self.contour[-1][0])
|
|
289
|
+
and self.contour[0][1] is not None
|
|
290
|
+
and self.contour[-1][1] is not None
|
|
291
|
+
):
|
|
292
|
+
self.contour[0] = self.contour[-1]
|
|
293
|
+
del self.contour[-1]
|
|
294
|
+
else:
|
|
295
|
+
# The closing line is implied: the first point becomes a "line".
|
|
296
|
+
pt, tp = self.contour[0]
|
|
297
|
+
if tp == "move":
|
|
298
|
+
self.contour[0] = pt, "line"
|
|
299
|
+
self._flushContour()
|
|
300
|
+
self.contour = None
|
|
301
|
+
|
|
302
|
+
def endPath(self):
|
|
303
|
+
if self.contour is None:
|
|
304
|
+
raise PenError("Contour missing required initial moveTo")
|
|
305
|
+
self._flushContour()
|
|
306
|
+
self.contour = None
|
|
307
|
+
|
|
308
|
+
def addComponent(self, glyphName, transform):
|
|
309
|
+
if self.contour is not None:
|
|
310
|
+
raise PenError("Components must be added before or after contours")
|
|
311
|
+
self.pen.addComponent(glyphName, transform)
|
|
312
|
+
|
|
313
|
+
|
|
314
|
+
class GuessSmoothPointPen(AbstractPointPen):
|
|
315
|
+
"""Filter that marks on-curves smooth when their neighbours are collinear.
|
|
316
|
+
|
|
317
|
+
Only points next to at least one off-curve are considered; `error` is the
|
|
318
|
+
angle tolerance in radians.
|
|
319
|
+
"""
|
|
320
|
+
|
|
321
|
+
def __init__(self, outPen: AbstractPointPen, error: float = 0.05) -> None:
|
|
322
|
+
self._outPen = outPen
|
|
323
|
+
self._error = error
|
|
324
|
+
self._points: list | None = None
|
|
325
|
+
|
|
326
|
+
def _flushContour(self):
|
|
327
|
+
if self._points is None:
|
|
328
|
+
raise PenError("Path not begun")
|
|
329
|
+
points = self._points
|
|
330
|
+
nPoints = len(points)
|
|
331
|
+
if not nPoints:
|
|
332
|
+
return
|
|
333
|
+
if points[0][1] == "move":
|
|
334
|
+
indices = range(1, nPoints - 1)
|
|
335
|
+
elif nPoints > 1:
|
|
336
|
+
# Closed: start at -1 so negative indexing wraps around.
|
|
337
|
+
indices = range(-1, nPoints - 1)
|
|
338
|
+
else:
|
|
339
|
+
indices = []
|
|
340
|
+
for i in indices:
|
|
341
|
+
pt, segmentType, _, name, kwargs = points[i]
|
|
342
|
+
if segmentType is None:
|
|
343
|
+
continue
|
|
344
|
+
prev = i - 1
|
|
345
|
+
next = i + 1
|
|
346
|
+
if points[prev][1] is not None and points[next][1] is not None:
|
|
347
|
+
continue
|
|
348
|
+
pt = points[i][0]
|
|
349
|
+
prevPt = points[prev][0]
|
|
350
|
+
nextPt = points[next][0]
|
|
351
|
+
if pt != prevPt and pt != nextPt:
|
|
352
|
+
dx1, dy1 = pt[0] - prevPt[0], pt[1] - prevPt[1]
|
|
353
|
+
dx2, dy2 = nextPt[0] - pt[0], nextPt[1] - pt[1]
|
|
354
|
+
a1 = math.atan2(dy1, dx1)
|
|
355
|
+
a2 = math.atan2(dy2, dx2)
|
|
356
|
+
if abs(a1 - a2) < self._error:
|
|
357
|
+
points[i] = pt, segmentType, True, name, kwargs
|
|
358
|
+
|
|
359
|
+
for pt, segmentType, smooth, name, kwargs in points:
|
|
360
|
+
self._outPen.addPoint(pt, segmentType, smooth, name, **kwargs)
|
|
361
|
+
|
|
362
|
+
def beginPath(self, identifier=None, **kwargs):
|
|
363
|
+
if self._points is not None:
|
|
364
|
+
raise PenError("Path already begun")
|
|
365
|
+
self._points = []
|
|
366
|
+
if identifier is not None:
|
|
367
|
+
kwargs["identifier"] = identifier
|
|
368
|
+
self._outPen.beginPath(**kwargs)
|
|
369
|
+
|
|
370
|
+
def endPath(self):
|
|
371
|
+
self._flushContour()
|
|
372
|
+
self._outPen.endPath()
|
|
373
|
+
self._points = None
|
|
374
|
+
|
|
375
|
+
def addPoint(
|
|
376
|
+
self, pt, segmentType=None, smooth=False, name=None, identifier=None, **kwargs
|
|
377
|
+
):
|
|
378
|
+
if self._points is None:
|
|
379
|
+
raise PenError("Path not begun")
|
|
380
|
+
if identifier is not None:
|
|
381
|
+
kwargs["identifier"] = identifier
|
|
382
|
+
self._points.append((pt, segmentType, False, name, kwargs))
|
|
383
|
+
|
|
384
|
+
def addComponent(self, glyphName, transformation, identifier=None, **kwargs):
|
|
385
|
+
if self._points is not None:
|
|
386
|
+
raise PenError("Components must be added before or after contours")
|
|
387
|
+
if identifier is not None:
|
|
388
|
+
kwargs["identifier"] = identifier
|
|
389
|
+
self._outPen.addComponent(glyphName, transformation, **kwargs)
|
|
390
|
+
|
|
391
|
+
def addVarComponent(
|
|
392
|
+
self, glyphName, transformation, location, identifier=None, **kwargs
|
|
393
|
+
):
|
|
394
|
+
if self._points is not None:
|
|
395
|
+
raise PenError("VarComponents must be added before or after contours")
|
|
396
|
+
if identifier is not None:
|
|
397
|
+
kwargs["identifier"] = identifier
|
|
398
|
+
self._outPen.addVarComponent(glyphName, transformation, location, **kwargs)
|
|
399
|
+
|
|
400
|
+
|
|
401
|
+
class ReverseContourPointPen(AbstractPointPen):
|
|
402
|
+
"""Passes outlines on with every contour's direction reversed.
|
|
403
|
+
|
|
404
|
+
Closed contours keep their first point first; components pass unchanged.
|
|
405
|
+
"""
|
|
406
|
+
|
|
407
|
+
def __init__(self, outputPointPen: AbstractPointPen) -> None:
|
|
408
|
+
self.pen = outputPointPen
|
|
409
|
+
self.currentContour: list | None = None
|
|
410
|
+
|
|
411
|
+
def _flushContour(self):
|
|
412
|
+
pen = self.pen
|
|
413
|
+
contour = self.currentContour
|
|
414
|
+
if not contour:
|
|
415
|
+
pen.beginPath(identifier=self.currentContourIdentifier)
|
|
416
|
+
pen.endPath()
|
|
417
|
+
return
|
|
418
|
+
|
|
419
|
+
closed = contour[0][1] != "move"
|
|
420
|
+
if not closed:
|
|
421
|
+
lastSegmentType = "move"
|
|
422
|
+
else:
|
|
423
|
+
# Move the first point to the end so it is first again after reversal.
|
|
424
|
+
contour.append(contour.pop(0))
|
|
425
|
+
firstOnCurve = None
|
|
426
|
+
for i in range(len(contour)):
|
|
427
|
+
if contour[i][1] is not None:
|
|
428
|
+
firstOnCurve = i
|
|
429
|
+
break
|
|
430
|
+
if firstOnCurve is None:
|
|
431
|
+
lastSegmentType = None
|
|
432
|
+
else:
|
|
433
|
+
lastSegmentType = contour[firstOnCurve][1]
|
|
434
|
+
|
|
435
|
+
contour.reverse()
|
|
436
|
+
if not closed:
|
|
437
|
+
# Open paths must start with a move: drop leading off-curves.
|
|
438
|
+
while contour[0][1] is None:
|
|
439
|
+
contour.pop(0)
|
|
440
|
+
pen.beginPath(identifier=self.currentContourIdentifier)
|
|
441
|
+
for pt, nextSegmentType, smooth, name, kwargs in contour:
|
|
442
|
+
if nextSegmentType is not None:
|
|
443
|
+
segmentType = lastSegmentType
|
|
444
|
+
lastSegmentType = nextSegmentType
|
|
445
|
+
else:
|
|
446
|
+
segmentType = None
|
|
447
|
+
pen.addPoint(pt, segmentType=segmentType, smooth=smooth, name=name, **kwargs)
|
|
448
|
+
pen.endPath()
|
|
449
|
+
|
|
450
|
+
def beginPath(self, identifier=None, **kwargs):
|
|
451
|
+
if self.currentContour is not None:
|
|
452
|
+
raise PenError("Path already begun")
|
|
453
|
+
self.currentContour = []
|
|
454
|
+
self.currentContourIdentifier = identifier
|
|
455
|
+
self.onCurve = []
|
|
456
|
+
|
|
457
|
+
def endPath(self):
|
|
458
|
+
if self.currentContour is None:
|
|
459
|
+
raise PenError("Path not begun")
|
|
460
|
+
self._flushContour()
|
|
461
|
+
self.currentContour = None
|
|
462
|
+
|
|
463
|
+
def addPoint(
|
|
464
|
+
self, pt, segmentType=None, smooth=False, name=None, identifier=None, **kwargs
|
|
465
|
+
):
|
|
466
|
+
if self.currentContour is None:
|
|
467
|
+
raise PenError("Path not begun")
|
|
468
|
+
if identifier is not None:
|
|
469
|
+
kwargs["identifier"] = identifier
|
|
470
|
+
self.currentContour.append((pt, segmentType, smooth, name, kwargs))
|
|
471
|
+
|
|
472
|
+
def addComponent(self, glyphName, transform, identifier=None, **kwargs):
|
|
473
|
+
if self.currentContour is not None:
|
|
474
|
+
raise PenError("Components must be added before or after contours")
|
|
475
|
+
self.pen.addComponent(glyphName, transform, identifier=identifier, **kwargs)
|
|
476
|
+
|
|
477
|
+
|
|
478
|
+
class DecomposingPointPen(_LogMixin, AbstractPointPen):
|
|
479
|
+
"""Mixin whose `addComponent` draws base glyphs onto `self` via `drawPoints`.
|
|
480
|
+
|
|
481
|
+
Missing base glyphs log a warning, or raise `MissingComponentError` when
|
|
482
|
+
`skipMissingComponents` is False. `reverseFlipped` is a bool or a
|
|
483
|
+
`ReverseFlipped` value (True means KEEP_START).
|
|
484
|
+
"""
|
|
485
|
+
|
|
486
|
+
skipMissingComponents = True
|
|
487
|
+
MissingComponentError = MissingComponentError
|
|
488
|
+
|
|
489
|
+
def __init__(
|
|
490
|
+
self,
|
|
491
|
+
glyphSet: Mapping[str, Any] | None,
|
|
492
|
+
*args: Any,
|
|
493
|
+
skipMissingComponents: bool | None = None,
|
|
494
|
+
reverseFlipped: bool | ReverseFlipped = False,
|
|
495
|
+
**kwargs: Any,
|
|
496
|
+
) -> None:
|
|
497
|
+
super().__init__(*args, **kwargs)
|
|
498
|
+
self.glyphSet = glyphSet
|
|
499
|
+
self.skipMissingComponents = (
|
|
500
|
+
self.__class__.skipMissingComponents
|
|
501
|
+
if skipMissingComponents is None
|
|
502
|
+
else skipMissingComponents
|
|
503
|
+
)
|
|
504
|
+
if reverseFlipped is False:
|
|
505
|
+
self.reverseFlipped = ReverseFlipped.NO
|
|
506
|
+
elif reverseFlipped is True:
|
|
507
|
+
self.reverseFlipped = ReverseFlipped.KEEP_START
|
|
508
|
+
else:
|
|
509
|
+
self.reverseFlipped = ReverseFlipped(reverseFlipped)
|
|
510
|
+
|
|
511
|
+
def addComponent(self, baseGlyphName, transformation, identifier=None, **kwargs):
|
|
512
|
+
"""Transform the base glyph's points and draw them onto self (ignores kwargs)."""
|
|
513
|
+
# Imported here: transform/filter import this module (cycle).
|
|
514
|
+
from fontlab_pens.filter import OnCurveFirstPointPen
|
|
515
|
+
from fontlab_pens.transform import Identity, TransformPointPen
|
|
516
|
+
|
|
517
|
+
try:
|
|
518
|
+
glyph = self.glyphSet[baseGlyphName]
|
|
519
|
+
except KeyError:
|
|
520
|
+
if not self.skipMissingComponents:
|
|
521
|
+
raise MissingComponentError(baseGlyphName) from None
|
|
522
|
+
self.log.warning(
|
|
523
|
+
"glyph '%s' is missing from glyphSet; skipped", baseGlyphName
|
|
524
|
+
)
|
|
525
|
+
else:
|
|
526
|
+
pen = self
|
|
527
|
+
if transformation != Identity:
|
|
528
|
+
pen = TransformPointPen(pen, transformation)
|
|
529
|
+
if self.reverseFlipped != ReverseFlipped.NO:
|
|
530
|
+
a, b, c, d = transformation[:4]
|
|
531
|
+
if a * d - b * c < 0:
|
|
532
|
+
pen = ReverseContourPointPen(pen)
|
|
533
|
+
if self.reverseFlipped == ReverseFlipped.ON_CURVE_FIRST:
|
|
534
|
+
# Wrapped last so it runs first during drawPoints.
|
|
535
|
+
pen = OnCurveFirstPointPen(pen)
|
|
536
|
+
glyph.drawPoints(pen)
|
fontlab_pens/qt.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
# this_file: src/fontlab_pens/qt.py
|
|
2
|
+
# Derived from fontTools Lib/fontTools/pens/qtPen.py (MIT, (c) Just van Rossum; see NOTICE)
|
|
3
|
+
"""Pen that draws into a Qt `QPainterPath`, plus a reader for on/off point lists.
|
|
4
|
+
|
|
5
|
+
`PythonQt.QtGui` is imported inside `QPainterPathPen.__init__`, so importing
|
|
6
|
+
this module works outside FontLab.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
from collections.abc import Mapping, Sequence
|
|
12
|
+
from typing import Any
|
|
13
|
+
|
|
14
|
+
from fontlab_pens.base import AbstractPen, BasePen
|
|
15
|
+
|
|
16
|
+
__all__ = ["QPainterPathPen", "draw_onoff_contours"]
|
|
17
|
+
|
|
18
|
+
Point = tuple[float, float]
|
|
19
|
+
OnOffPoint = tuple[float, float, bool]
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class QPainterPathPen(BasePen):
|
|
23
|
+
"""Draw into a `QPainterPath` (`.path`); fontTools' `QtPen`.
|
|
24
|
+
|
|
25
|
+
Pass `path` to draw into an existing one, else a new path is created.
|
|
26
|
+
"""
|
|
27
|
+
|
|
28
|
+
def __init__(self, glyphSet: Mapping[str, Any] | None = None, path: Any = None) -> None:
|
|
29
|
+
super().__init__(glyphSet)
|
|
30
|
+
if path is None:
|
|
31
|
+
from PythonQt.QtGui import QPainterPath
|
|
32
|
+
|
|
33
|
+
path = QPainterPath()
|
|
34
|
+
self.path = path
|
|
35
|
+
|
|
36
|
+
def _moveTo(self, pt):
|
|
37
|
+
self.path.moveTo(*pt)
|
|
38
|
+
|
|
39
|
+
def _lineTo(self, pt):
|
|
40
|
+
self.path.lineTo(*pt)
|
|
41
|
+
|
|
42
|
+
def _curveToOne(self, pt1, pt2, pt3):
|
|
43
|
+
self.path.cubicTo(*pt1, *pt2, *pt3)
|
|
44
|
+
|
|
45
|
+
def _qCurveToOne(self, pt1, pt2):
|
|
46
|
+
self.path.quadTo(*pt1, *pt2)
|
|
47
|
+
|
|
48
|
+
def _closePath(self):
|
|
49
|
+
self.path.closeSubpath()
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def draw_onoff_contours(contours: Sequence[Sequence[OnOffPoint]], pen: AbstractPen) -> None:
|
|
53
|
+
"""Draw closed `[(x, y, on), ...]` contours into `pen`.
|
|
54
|
+
|
|
55
|
+
Each contour starts at its first on-curve point. Runs of off-curves before an
|
|
56
|
+
on-curve become `curveTo` (2 or more) or `qCurveTo` (1); none is a line. A
|
|
57
|
+
contour with no on-curve point is a TrueType `qCurveTo(*offs, None)`; an
|
|
58
|
+
empty one is skipped.
|
|
59
|
+
"""
|
|
60
|
+
for points in contours:
|
|
61
|
+
if not points:
|
|
62
|
+
continue
|
|
63
|
+
start = next((i for i, p in enumerate(points) if p[2]), None)
|
|
64
|
+
if start is None:
|
|
65
|
+
pen.qCurveTo(*[(x, y) for x, y, _ in points], None)
|
|
66
|
+
pen.closePath()
|
|
67
|
+
continue
|
|
68
|
+
ring = list(points[start:]) + list(points[:start])
|
|
69
|
+
pen.moveTo(ring[0][:2])
|
|
70
|
+
controls: list[Point] = []
|
|
71
|
+
for x, y, on in ring[1:] + ring[:1]:
|
|
72
|
+
if not on:
|
|
73
|
+
controls.append((x, y))
|
|
74
|
+
continue
|
|
75
|
+
if len(controls) == 1:
|
|
76
|
+
pen.qCurveTo(controls[0], (x, y))
|
|
77
|
+
elif controls:
|
|
78
|
+
pen.curveTo(*controls, (x, y))
|
|
79
|
+
else:
|
|
80
|
+
pen.lineTo((x, y))
|
|
81
|
+
controls = []
|
|
82
|
+
pen.closePath()
|