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
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
# this_file: src/fontlab_pens/recording.py
|
|
2
|
+
# Derived from fontTools Lib/fontTools/pens/recordingPen.py (MIT, (c) Just van Rossum; see NOTICE)
|
|
3
|
+
"""Pens that record drawing calls so they can be inspected or replayed.
|
|
4
|
+
|
|
5
|
+
`RecordingPen.value` and `RecordingPointPen.value` have exactly the fontTools
|
|
6
|
+
shape, which is what the parity tests compare.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
from collections.abc import Iterable, Iterator
|
|
12
|
+
from typing import Any
|
|
13
|
+
|
|
14
|
+
from fontlab_pens.base import AbstractPen, DecomposingPen
|
|
15
|
+
from fontlab_pens.point import AbstractPointPen, DecomposingPointPen
|
|
16
|
+
|
|
17
|
+
__all__ = [
|
|
18
|
+
"DecomposingRecordingPen",
|
|
19
|
+
"DecomposingRecordingPointPen",
|
|
20
|
+
"RecordingPen",
|
|
21
|
+
"RecordingPointPen",
|
|
22
|
+
"lerpRecordings",
|
|
23
|
+
"replayRecording",
|
|
24
|
+
]
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def replayRecording(recording: Iterable[tuple], pen: Any) -> None:
|
|
28
|
+
"""Replay `(method, args[, kwargs])` tuples onto any pen-like object."""
|
|
29
|
+
for entry in recording:
|
|
30
|
+
operator, operands = entry[:2]
|
|
31
|
+
kwargs = entry[2] if len(entry) == 3 else {}
|
|
32
|
+
getattr(pen, operator)(*operands, **kwargs)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class RecordingPen(AbstractPen):
|
|
36
|
+
"""Records segment-pen calls in `value`; `replay(pen)` (alias `draw`) redraws them."""
|
|
37
|
+
|
|
38
|
+
def __init__(self) -> None:
|
|
39
|
+
self.value: list[tuple] = []
|
|
40
|
+
|
|
41
|
+
def moveTo(self, p0):
|
|
42
|
+
self.value.append(("moveTo", (p0,)))
|
|
43
|
+
|
|
44
|
+
def lineTo(self, p1):
|
|
45
|
+
self.value.append(("lineTo", (p1,)))
|
|
46
|
+
|
|
47
|
+
def qCurveTo(self, *points):
|
|
48
|
+
self.value.append(("qCurveTo", points))
|
|
49
|
+
|
|
50
|
+
def curveTo(self, *points):
|
|
51
|
+
self.value.append(("curveTo", points))
|
|
52
|
+
|
|
53
|
+
def closePath(self):
|
|
54
|
+
self.value.append(("closePath", ()))
|
|
55
|
+
|
|
56
|
+
def endPath(self):
|
|
57
|
+
self.value.append(("endPath", ()))
|
|
58
|
+
|
|
59
|
+
def addComponent(self, glyphName, transformation):
|
|
60
|
+
self.value.append(("addComponent", (glyphName, transformation)))
|
|
61
|
+
|
|
62
|
+
def addVarComponent(
|
|
63
|
+
self, glyphName, transformation, location, *, resetUnspecifiedAxes=False
|
|
64
|
+
):
|
|
65
|
+
entry: tuple = ("addVarComponent", (glyphName, transformation, location))
|
|
66
|
+
if resetUnspecifiedAxes:
|
|
67
|
+
entry += ({"resetUnspecifiedAxes": True},)
|
|
68
|
+
self.value.append(entry)
|
|
69
|
+
|
|
70
|
+
def replay(self, pen: Any) -> None:
|
|
71
|
+
replayRecording(self.value, pen)
|
|
72
|
+
|
|
73
|
+
draw = replay
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
class DecomposingRecordingPen(DecomposingPen, RecordingPen):
|
|
77
|
+
"""`RecordingPen` that draws components decomposed, looked up in `glyphSet`.
|
|
78
|
+
|
|
79
|
+
Raises `MissingComponentError` for missing base glyphs unless
|
|
80
|
+
`skipMissingComponents=True`.
|
|
81
|
+
"""
|
|
82
|
+
|
|
83
|
+
skipMissingComponents = False
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
class RecordingPointPen(AbstractPointPen):
|
|
87
|
+
"""Records point-pen calls as `(method, args, kwargs)`; `replay` (alias `drawPoints`)."""
|
|
88
|
+
|
|
89
|
+
def __init__(self) -> None:
|
|
90
|
+
self.value: list[tuple] = []
|
|
91
|
+
|
|
92
|
+
def beginPath(self, identifier=None, **kwargs):
|
|
93
|
+
if identifier is not None:
|
|
94
|
+
kwargs["identifier"] = identifier
|
|
95
|
+
self.value.append(("beginPath", (), kwargs))
|
|
96
|
+
|
|
97
|
+
def endPath(self):
|
|
98
|
+
self.value.append(("endPath", (), {}))
|
|
99
|
+
|
|
100
|
+
def addPoint(
|
|
101
|
+
self, pt, segmentType=None, smooth=False, name=None, identifier=None, **kwargs
|
|
102
|
+
):
|
|
103
|
+
if identifier is not None:
|
|
104
|
+
kwargs["identifier"] = identifier
|
|
105
|
+
self.value.append(("addPoint", (pt, segmentType, smooth, name), kwargs))
|
|
106
|
+
|
|
107
|
+
def addComponent(self, baseGlyphName, transformation, identifier=None, **kwargs):
|
|
108
|
+
if identifier is not None:
|
|
109
|
+
kwargs["identifier"] = identifier
|
|
110
|
+
self.value.append(("addComponent", (baseGlyphName, transformation), kwargs))
|
|
111
|
+
|
|
112
|
+
def addVarComponent(
|
|
113
|
+
self, baseGlyphName, transformation, location, identifier=None, **kwargs
|
|
114
|
+
):
|
|
115
|
+
if identifier is not None:
|
|
116
|
+
kwargs["identifier"] = identifier
|
|
117
|
+
self.value.append(
|
|
118
|
+
("addVarComponent", (baseGlyphName, transformation, location), kwargs)
|
|
119
|
+
)
|
|
120
|
+
|
|
121
|
+
def replay(self, pointPen: Any) -> None:
|
|
122
|
+
for operator, args, kwargs in self.value:
|
|
123
|
+
getattr(pointPen, operator)(*args, **kwargs)
|
|
124
|
+
|
|
125
|
+
drawPoints = replay
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
class DecomposingRecordingPointPen(DecomposingPointPen, RecordingPointPen):
|
|
129
|
+
"""`RecordingPointPen` that draws components decomposed, looked up in `glyphSet`.
|
|
130
|
+
|
|
131
|
+
Raises `MissingComponentError` for missing base glyphs unless
|
|
132
|
+
`skipMissingComponents=True`.
|
|
133
|
+
"""
|
|
134
|
+
|
|
135
|
+
skipMissingComponents = False
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
def lerpRecordings(
|
|
139
|
+
recording1: list[tuple], recording2: list[tuple], factor: float = 0.5
|
|
140
|
+
) -> Iterator[tuple[str, list]]:
|
|
141
|
+
"""Yield a recording interpolated between two decomposed recordings.
|
|
142
|
+
|
|
143
|
+
`factor` 0 gives the first, 1 the second; values outside extrapolate.
|
|
144
|
+
Raises `ValueError` for mismatched lengths, operators or components.
|
|
145
|
+
"""
|
|
146
|
+
if len(recording1) != len(recording2):
|
|
147
|
+
raise ValueError(
|
|
148
|
+
f"Mismatched lengths: {len(recording1)} and {len(recording2)}"
|
|
149
|
+
)
|
|
150
|
+
for (op1, args1), (op2, args2) in zip(recording1, recording2, strict=True):
|
|
151
|
+
if op1 != op2:
|
|
152
|
+
raise ValueError(f"Mismatched operations: {op1}, {op2}")
|
|
153
|
+
if op1 == "addComponent":
|
|
154
|
+
raise ValueError("Cannot interpolate components")
|
|
155
|
+
mid_args = [
|
|
156
|
+
(x1 + (x2 - x1) * factor, y1 + (y2 - y1) * factor)
|
|
157
|
+
for (x1, y1), (x2, y2) in zip(args1, args2, strict=False)
|
|
158
|
+
]
|
|
159
|
+
yield (op1, mid_args)
|
fontlab_pens/reverse.py
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
# this_file: src/fontlab_pens/reverse.py
|
|
2
|
+
# Derived from fontTools Lib/fontTools/pens/reverseContourPen.py and the
|
|
3
|
+
# `pairwise` helper of Lib/fontTools/misc/arrayTools.py (MIT, (c) Just van Rossum; see NOTICE)
|
|
4
|
+
"""Segment filter pen that reverses contour direction."""
|
|
5
|
+
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
from collections.abc import Iterator, Sequence
|
|
9
|
+
from typing import Any
|
|
10
|
+
|
|
11
|
+
from fontlab_pens.base import AbstractPen
|
|
12
|
+
from fontlab_pens.filter import ContourFilterPen
|
|
13
|
+
|
|
14
|
+
__all__ = ["ReverseContourPen", "reversedContour"]
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def _pairwise(iterable: Sequence[Any], reverse: bool = False) -> Iterator[tuple[Any, Any]]:
|
|
18
|
+
"""Yield `(current, next)` pairs, wrapping the last item to the first."""
|
|
19
|
+
if not iterable:
|
|
20
|
+
return
|
|
21
|
+
it = reversed(iterable) if reverse else iter(iterable)
|
|
22
|
+
first = next(it, None)
|
|
23
|
+
a = first
|
|
24
|
+
for b in it:
|
|
25
|
+
yield (a, b)
|
|
26
|
+
a = b
|
|
27
|
+
yield (a, first)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class ReverseContourPen(ContourFilterPen):
|
|
31
|
+
"""Forwards outlines with each contour reversed; closed contours keep their start.
|
|
32
|
+
|
|
33
|
+
Components pass through unchanged.
|
|
34
|
+
"""
|
|
35
|
+
|
|
36
|
+
def __init__(self, outPen: AbstractPen, outputImpliedClosingLine: bool = False) -> None:
|
|
37
|
+
super().__init__(outPen)
|
|
38
|
+
self.outputImpliedClosingLine = outputImpliedClosingLine
|
|
39
|
+
|
|
40
|
+
def filterContour(self, contour):
|
|
41
|
+
return reversedContour(contour, self.outputImpliedClosingLine)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def reversedContour(
|
|
45
|
+
contour: list[tuple], outputImpliedClosingLine: bool = False
|
|
46
|
+
) -> Iterator[tuple]:
|
|
47
|
+
"""Yield a contour's `(operator, operands)` tuples with the direction reversed.
|
|
48
|
+
|
|
49
|
+
`contour` is consumed (modified in place).
|
|
50
|
+
"""
|
|
51
|
+
if not contour:
|
|
52
|
+
return
|
|
53
|
+
assert len(contour) > 1, "invalid contour"
|
|
54
|
+
|
|
55
|
+
contourType = contour.pop()[0]
|
|
56
|
+
assert contourType in ("endPath", "closePath")
|
|
57
|
+
closed = contourType == "closePath"
|
|
58
|
+
|
|
59
|
+
firstType, firstPts = contour.pop(0)
|
|
60
|
+
assert firstType in ("moveTo", "qCurveTo"), (
|
|
61
|
+
f"invalid initial segment type: {firstType!r}"
|
|
62
|
+
)
|
|
63
|
+
firstOnCurve = firstPts[-1]
|
|
64
|
+
if firstType == "qCurveTo":
|
|
65
|
+
# Off-curve-only TrueType contour.
|
|
66
|
+
assert firstOnCurve is None, "off-curve only paths must end with 'None'"
|
|
67
|
+
assert not contour, "only one qCurveTo allowed per off-curve path"
|
|
68
|
+
firstPts = (firstPts[0], *reversed(firstPts[1:-1]), None)
|
|
69
|
+
|
|
70
|
+
if not contour:
|
|
71
|
+
# Single segment: nothing to reverse.
|
|
72
|
+
closed = firstType != "moveTo"
|
|
73
|
+
yield firstType, firstPts
|
|
74
|
+
else:
|
|
75
|
+
lastType, lastPts = contour[-1]
|
|
76
|
+
lastOnCurve = lastPts[-1]
|
|
77
|
+
if closed:
|
|
78
|
+
yield firstType, firstPts
|
|
79
|
+
if firstOnCurve != lastOnCurve:
|
|
80
|
+
# Make the implied closing line explicit.
|
|
81
|
+
yield "lineTo", (lastOnCurve,)
|
|
82
|
+
contour[-1] = (lastType, (*tuple(lastPts[:-1]), firstOnCurve))
|
|
83
|
+
|
|
84
|
+
if len(contour) > 1:
|
|
85
|
+
secondType, secondPts = contour[0]
|
|
86
|
+
else:
|
|
87
|
+
secondType, secondPts = lastType, lastPts
|
|
88
|
+
|
|
89
|
+
# A line after the move becomes the implied closing line, unless
|
|
90
|
+
# it overlaps the move (then the duplicate point is kept).
|
|
91
|
+
if not outputImpliedClosingLine and secondType == "lineTo" and firstPts != secondPts:
|
|
92
|
+
del contour[0]
|
|
93
|
+
if contour:
|
|
94
|
+
contour[-1] = (lastType, (*tuple(lastPts[:-1]), *secondPts))
|
|
95
|
+
else:
|
|
96
|
+
# Open path: the last point becomes the first.
|
|
97
|
+
yield firstType, (lastOnCurve,)
|
|
98
|
+
contour[-1] = (lastType, (*tuple(lastPts[:-1]), firstOnCurve))
|
|
99
|
+
|
|
100
|
+
for (curType, curPts), (_, nextPts) in _pairwise(contour, reverse=True):
|
|
101
|
+
yield curType, (*reversed(curPts[:-1]), nextPts[-1])
|
|
102
|
+
|
|
103
|
+
yield "closePath" if closed else "endPath", ()
|
fontlab_pens/rounding.py
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
# this_file: src/fontlab_pens/rounding.py
|
|
2
|
+
# Derived from fontTools Lib/fontTools/pens/roundingPen.py and `otRound`/`noRound`
|
|
3
|
+
# of Lib/fontTools/misc/roundTools.py (MIT, (c) Just van Rossum; see NOTICE)
|
|
4
|
+
"""Filter pens that round coordinates and component offsets."""
|
|
5
|
+
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
import math
|
|
9
|
+
from collections.abc import Callable
|
|
10
|
+
|
|
11
|
+
from fontlab_pens.base import AbstractPen
|
|
12
|
+
from fontlab_pens.filter import FilterPen, FilterPointPen
|
|
13
|
+
from fontlab_pens.point import AbstractPointPen
|
|
14
|
+
from fontlab_pens.transform import Transform
|
|
15
|
+
|
|
16
|
+
__all__ = ["RoundingPen", "RoundingPointPen", "noRound", "otRound"]
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def noRound(value: float) -> float:
|
|
20
|
+
"""Return `value` unchanged."""
|
|
21
|
+
return value
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def otRound(value: float) -> int:
|
|
25
|
+
"""Round half up (towards +infinity), as the OpenType spec requires."""
|
|
26
|
+
return math.floor(value + 0.5)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class RoundingPen(FilterPen):
|
|
30
|
+
"""Rounds point coordinates and component offsets with `roundFunc`.
|
|
31
|
+
|
|
32
|
+
Component scale/skew values use `transformRoundFunc` (unrounded by default).
|
|
33
|
+
"""
|
|
34
|
+
|
|
35
|
+
def __init__(
|
|
36
|
+
self,
|
|
37
|
+
outPen: AbstractPen,
|
|
38
|
+
roundFunc: Callable[[float], float] = otRound,
|
|
39
|
+
transformRoundFunc: Callable[[float], float] = noRound,
|
|
40
|
+
) -> None:
|
|
41
|
+
super().__init__(outPen)
|
|
42
|
+
self.roundFunc = roundFunc
|
|
43
|
+
self.transformRoundFunc = transformRoundFunc
|
|
44
|
+
|
|
45
|
+
def moveTo(self, pt):
|
|
46
|
+
self._outPen.moveTo((self.roundFunc(pt[0]), self.roundFunc(pt[1])))
|
|
47
|
+
|
|
48
|
+
def lineTo(self, pt):
|
|
49
|
+
self._outPen.lineTo((self.roundFunc(pt[0]), self.roundFunc(pt[1])))
|
|
50
|
+
|
|
51
|
+
def curveTo(self, *points):
|
|
52
|
+
self._outPen.curveTo(*((self.roundFunc(x), self.roundFunc(y)) for x, y in points))
|
|
53
|
+
|
|
54
|
+
def qCurveTo(self, *points):
|
|
55
|
+
self._outPen.qCurveTo(
|
|
56
|
+
*(
|
|
57
|
+
None if pt is None else (self.roundFunc(pt[0]), self.roundFunc(pt[1]))
|
|
58
|
+
for pt in points
|
|
59
|
+
)
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
def addComponent(self, glyphName, transformation):
|
|
63
|
+
xx, xy, yx, yy, dx, dy = transformation
|
|
64
|
+
self._outPen.addComponent(
|
|
65
|
+
glyphName,
|
|
66
|
+
Transform(
|
|
67
|
+
self.transformRoundFunc(xx),
|
|
68
|
+
self.transformRoundFunc(xy),
|
|
69
|
+
self.transformRoundFunc(yx),
|
|
70
|
+
self.transformRoundFunc(yy),
|
|
71
|
+
self.roundFunc(dx),
|
|
72
|
+
self.roundFunc(dy),
|
|
73
|
+
),
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
class RoundingPointPen(FilterPointPen):
|
|
78
|
+
"""Point-pen counterpart of `RoundingPen`."""
|
|
79
|
+
|
|
80
|
+
def __init__(
|
|
81
|
+
self,
|
|
82
|
+
outPen: AbstractPointPen,
|
|
83
|
+
roundFunc: Callable[[float], float] = otRound,
|
|
84
|
+
transformRoundFunc: Callable[[float], float] = noRound,
|
|
85
|
+
) -> None:
|
|
86
|
+
super().__init__(outPen)
|
|
87
|
+
self.roundFunc = roundFunc
|
|
88
|
+
self.transformRoundFunc = transformRoundFunc
|
|
89
|
+
|
|
90
|
+
def addPoint(
|
|
91
|
+
self, pt, segmentType=None, smooth=False, name=None, identifier=None, **kwargs
|
|
92
|
+
):
|
|
93
|
+
self._outPen.addPoint(
|
|
94
|
+
(self.roundFunc(pt[0]), self.roundFunc(pt[1])),
|
|
95
|
+
segmentType=segmentType,
|
|
96
|
+
smooth=smooth,
|
|
97
|
+
name=name,
|
|
98
|
+
identifier=identifier,
|
|
99
|
+
**kwargs,
|
|
100
|
+
)
|
|
101
|
+
|
|
102
|
+
def addComponent(self, baseGlyphName, transformation, identifier=None, **kwargs):
|
|
103
|
+
xx, xy, yx, yy, dx, dy = transformation
|
|
104
|
+
self._outPen.addComponent(
|
|
105
|
+
baseGlyphName,
|
|
106
|
+
Transform(
|
|
107
|
+
self.transformRoundFunc(xx),
|
|
108
|
+
self.transformRoundFunc(xy),
|
|
109
|
+
self.transformRoundFunc(yx),
|
|
110
|
+
self.transformRoundFunc(yy),
|
|
111
|
+
self.roundFunc(dx),
|
|
112
|
+
self.roundFunc(dy),
|
|
113
|
+
),
|
|
114
|
+
identifier=identifier,
|
|
115
|
+
**kwargs,
|
|
116
|
+
)
|