From b885a852ed8b90d972ad0d7efa6b1a8e0d647b97 Mon Sep 17 00:00:00 2001 From: Cosimo Lupo Date: Thu, 28 Nov 2019 15:31:04 +0000 Subject: [PATCH] recordingPen: add RecordingPointPen, like RecordingPen but for point pens --- Lib/fontTools/pens/recordingPen.py | 53 +++++++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/Lib/fontTools/pens/recordingPen.py b/Lib/fontTools/pens/recordingPen.py index dbbcc916c..b25011d6d 100644 --- a/Lib/fontTools/pens/recordingPen.py +++ b/Lib/fontTools/pens/recordingPen.py @@ -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()