recordingPen: add RecordingPointPen, like RecordingPen but for point pens

This commit is contained in:
Cosimo Lupo 2019-11-28 15:31:04 +00:00
parent 0fef59fd9e
commit b885a852ed
No known key found for this signature in database
GPG Key ID: 179A8F0895A02F4F

View File

@ -1,9 +1,15 @@
"""Pen recording operations that can be accessed or replayed."""
from fontTools.misc.py23 import *
from fontTools.pens.basePen import AbstractPen, DecomposingPen
from fontTools.pens.pointPen import AbstractPointPen
__all__ = ["replayRecording", "RecordingPen", "DecomposingRecordingPen"]
__all__ = [
"replayRecording",
"RecordingPen",
"DecomposingRecordingPen",
"RecordingPointPen",
]
def replayRecording(recording, pen):
@ -89,6 +95,51 @@ class DecomposingRecordingPen(DecomposingPen, RecordingPen):
skipMissingComponents = False
class RecordingPointPen(AbstractPointPen):
"""PointPen recording operations that can be accessed or replayed.
The recording can be accessed as pen.value; or replayed using
pointPen.replay(otherPointPen).
Usage example:
==============
from defcon import Font
from fontTools.pens.recordingPen import RecordingPointPen
glyph_name = 'a'
font_path = 'MyFont.ufo'
font = Font(font_path)
glyph = font[glyph_name]
pen = RecordingPointPen()
glyph.drawPoints(pen)
print(pen.value)
new_glyph = font.newGlyph('b')
pen.replay(new_glyph.getPointPen())
"""
def __init__(self):
self.value = []
def beginPath(self, **kwargs):
self.value.append(("beginPath", (), kwargs))
def endPath(self):
self.value.append(("endPath", (), {}))
def addPoint(self, pt, segmentType=None, smooth=False, name=None, **kwargs):
self.value.append(("addPoint", (pt, segmentType, smooth, name), kwargs))
def addComponent(self, baseGlyphName, transformation, **kwargs):
self.value.append(("addComponent", (baseGlyphName, transformation), kwargs))
def replay(self, pointPen):
for operator, args, kwargs in self.value:
getattr(pointPen, operator)(*args, **kwargs)
if __name__ == "__main__":
from fontTools.pens.basePen import _TestPen
pen = RecordingPen()