1from fontTools.pens.recordingPen import ( 2 RecordingPen, 3 DecomposingRecordingPen, 4 RecordingPointPen, 5) 6import pytest 7 8 9class _TestGlyph(object): 10 def draw(self, pen): 11 pen.moveTo((0.0, 0.0)) 12 pen.lineTo((0.0, 100.0)) 13 pen.curveTo((50.0, 75.0), (60.0, 50.0), (50.0, 0.0)) 14 pen.closePath() 15 16 def drawPoints(self, pen): 17 pen.beginPath(identifier="abc") 18 pen.addPoint((0.0, 0.0), "line", False, "start", identifier="0000") 19 pen.addPoint((0.0, 100.0), "line", False, None, identifier="0001") 20 pen.addPoint((50.0, 75.0), None, False, None, identifier="0002") 21 pen.addPoint((60.0, 50.0), None, False, None, identifier="0003") 22 pen.addPoint((50.0, 0.0), "curve", True, "last", identifier="0004") 23 pen.endPath() 24 25 26class RecordingPenTest(object): 27 def test_addComponent(self): 28 pen = RecordingPen() 29 pen.addComponent("a", (2, 0, 0, 3, -10, 5)) 30 assert pen.value == [("addComponent", ("a", (2, 0, 0, 3, -10, 5)))] 31 32 33class DecomposingRecordingPenTest(object): 34 def test_addComponent_decomposed(self): 35 pen = DecomposingRecordingPen({"a": _TestGlyph()}) 36 pen.addComponent("a", (2, 0, 0, 3, -10, 5)) 37 assert pen.value == [ 38 ("moveTo", ((-10.0, 5.0),)), 39 ("lineTo", ((-10.0, 305.0),)), 40 ("curveTo", ((90.0, 230.0), (110.0, 155.0), (90.0, 5.0))), 41 ("closePath", ()), 42 ] 43 44 def test_addComponent_missing_raises(self): 45 pen = DecomposingRecordingPen(dict()) 46 with pytest.raises(KeyError) as excinfo: 47 pen.addComponent("a", (1, 0, 0, 1, 0, 0)) 48 assert excinfo.value.args[0] == "a" 49 50 51class RecordingPointPenTest: 52 def test_record_and_replay(self): 53 pen = RecordingPointPen() 54 glyph = _TestGlyph() 55 glyph.drawPoints(pen) 56 pen.addComponent("a", (2, 0, 0, 2, -10, 5)) 57 58 assert pen.value == [ 59 ("beginPath", (), {"identifier": "abc"}), 60 ("addPoint", ((0.0, 0.0), "line", False, "start"), {"identifier": "0000"}), 61 ("addPoint", ((0.0, 100.0), "line", False, None), {"identifier": "0001"}), 62 ("addPoint", ((50.0, 75.0), None, False, None), {"identifier": "0002"}), 63 ("addPoint", ((60.0, 50.0), None, False, None), {"identifier": "0003"}), 64 ("addPoint", ((50.0, 0.0), "curve", True, "last"), {"identifier": "0004"}), 65 ("endPath", (), {}), 66 ("addComponent", ("a", (2, 0, 0, 2, -10, 5)), {}), 67 ] 68 69 pen2 = RecordingPointPen() 70 pen.replay(pen2) 71 72 assert pen2.value == pen.value 73