1from fontTools.pens.basePen import \ 2 AbstractPen, BasePen, decomposeSuperBezierSegment, decomposeQuadraticSegment 3from fontTools.pens.pointPen import AbstractPointPen 4from fontTools.misc.loggingTools import CapturingLogHandler 5import unittest 6 7 8class _TestPen(BasePen): 9 def __init__(self): 10 BasePen.__init__(self, glyphSet={}) 11 self._commands = [] 12 13 def __repr__(self): 14 return " ".join(self._commands) 15 16 def getCurrentPoint(self): 17 return self._getCurrentPoint() 18 19 def _moveTo(self, pt): 20 self._commands.append("%s %s moveto" % (pt[0], pt[1])) 21 22 def _lineTo(self, pt): 23 self._commands.append("%s %s lineto" % (pt[0], pt[1])) 24 25 def _curveToOne(self, bcp1, bcp2, pt): 26 self._commands.append("%s %s %s %s %s %s curveto" % 27 (bcp1[0], bcp1[1], 28 bcp2[0], bcp2[1], 29 pt[0], pt[1])) 30 31 def _closePath(self): 32 self._commands.append("closepath") 33 34 def _endPath(self): 35 self._commands.append("endpath") 36 37 38class _TestGlyph: 39 def draw(self, pen): 40 pen.moveTo((0.0, 0.0)) 41 pen.lineTo((0.0, 100.0)) 42 pen.curveTo((50.0, 75.0), (60.0, 50.0), (50.0, 25.0), (0.0, 0.0)) 43 pen.closePath() 44 45 46class BasePenTest(unittest.TestCase): 47 def test_moveTo(self): 48 pen = _TestPen() 49 pen.moveTo((0.5, -4.3)) 50 self.assertEqual("0.5 -4.3 moveto", repr(pen)) 51 self.assertEqual((0.5, -4.3), pen.getCurrentPoint()) 52 53 def test_lineTo(self): 54 pen = _TestPen() 55 pen.moveTo((4, 5)) 56 pen.lineTo((7, 8)) 57 self.assertEqual("4 5 moveto 7 8 lineto", repr(pen)) 58 self.assertEqual((7, 8), pen.getCurrentPoint()) 59 60 def test_curveTo_zeroPoints(self): 61 pen = _TestPen() 62 pen.moveTo((0.0, 0.0)) 63 self.assertRaises(AssertionError, pen.curveTo) 64 65 def test_curveTo_onePoint(self): 66 pen = _TestPen() 67 pen.moveTo((0.0, 0.0)) 68 pen.curveTo((1.0, 1.1)) 69 self.assertEqual("0.0 0.0 moveto 1.0 1.1 lineto", repr(pen)) 70 self.assertEqual((1.0, 1.1), pen.getCurrentPoint()) 71 72 def test_curveTo_twoPoints(self): 73 pen = _TestPen() 74 pen.moveTo((0.0, 0.0)) 75 pen.curveTo((6.0, 3.0), (3.0, 6.0)) 76 self.assertEqual("0.0 0.0 moveto 4.0 2.0 5.0 4.0 3.0 6.0 curveto", 77 repr(pen)) 78 self.assertEqual((3.0, 6.0), pen.getCurrentPoint()) 79 80 def test_curveTo_manyPoints(self): 81 pen = _TestPen() 82 pen.moveTo((0.0, 0.0)) 83 pen.curveTo((1.0, 1.1), (2.0, 2.1), (3.0, 3.1), (4.0, 4.1)) 84 self.assertEqual("0.0 0.0 moveto " 85 "1.0 1.1 1.5 1.6 2.0 2.1 curveto " 86 "2.5 2.6 3.0 3.1 4.0 4.1 curveto", repr(pen)) 87 self.assertEqual((4.0, 4.1), pen.getCurrentPoint()) 88 89 def test_qCurveTo_zeroPoints(self): 90 pen = _TestPen() 91 pen.moveTo((0.0, 0.0)) 92 self.assertRaises(AssertionError, pen.qCurveTo) 93 94 def test_qCurveTo_onePoint(self): 95 pen = _TestPen() 96 pen.moveTo((0.0, 0.0)) 97 pen.qCurveTo((77.7, 99.9)) 98 self.assertEqual("0.0 0.0 moveto 77.7 99.9 lineto", repr(pen)) 99 self.assertEqual((77.7, 99.9), pen.getCurrentPoint()) 100 101 def test_qCurveTo_manyPoints(self): 102 pen = _TestPen() 103 pen.moveTo((0.0, 0.0)) 104 pen.qCurveTo((6.0, 3.0), (3.0, 6.0)) 105 self.assertEqual("0.0 0.0 moveto 4.0 2.0 5.0 4.0 3.0 6.0 curveto", 106 repr(pen)) 107 self.assertEqual((3.0, 6.0), pen.getCurrentPoint()) 108 109 def test_qCurveTo_onlyOffCurvePoints(self): 110 pen = _TestPen() 111 pen.moveTo((0.0, 0.0)) 112 pen.qCurveTo((6.0, -6.0), (12.0, 12.0), (18.0, -18.0), None) 113 self.assertEqual("0.0 0.0 moveto " 114 "12.0 -12.0 moveto " 115 "8.0 -8.0 7.0 -3.0 9.0 3.0 curveto " 116 "11.0 9.0 13.0 7.0 15.0 -3.0 curveto " 117 "17.0 -13.0 16.0 -16.0 12.0 -12.0 curveto", repr(pen)) 118 self.assertEqual((12.0, -12.0), pen.getCurrentPoint()) 119 120 def test_closePath(self): 121 pen = _TestPen() 122 pen.lineTo((3, 4)) 123 pen.closePath() 124 self.assertEqual("3 4 lineto closepath", repr(pen)) 125 self.assertEqual(None, pen.getCurrentPoint()) 126 127 def test_endPath(self): 128 pen = _TestPen() 129 pen.lineTo((3, 4)) 130 pen.endPath() 131 self.assertEqual("3 4 lineto endpath", repr(pen)) 132 self.assertEqual(None, pen.getCurrentPoint()) 133 134 def test_addComponent(self): 135 pen = _TestPen() 136 pen.glyphSet["oslash"] = _TestGlyph() 137 pen.addComponent("oslash", (2, 3, 0.5, 2, -10, 0)) 138 self.assertEqual("-10.0 0.0 moveto " 139 "40.0 200.0 lineto " 140 "127.5 300.0 131.25 290.0 125.0 265.0 curveto " 141 "118.75 240.0 102.5 200.0 -10.0 0.0 curveto " 142 "closepath", repr(pen)) 143 self.assertEqual(None, pen.getCurrentPoint()) 144 145 def test_addComponent_skip_missing(self): 146 pen = _TestPen() 147 with CapturingLogHandler(pen.log, "WARNING") as captor: 148 pen.addComponent("nonexistent", (1, 0, 0, 1, 0, 0)) 149 captor.assertRegex("glyph '.*' is missing from glyphSet; skipped") 150 151 152class DecomposeSegmentTest(unittest.TestCase): 153 def test_decomposeSuperBezierSegment(self): 154 decompose = decomposeSuperBezierSegment 155 self.assertRaises(AssertionError, decompose, []) 156 self.assertRaises(AssertionError, decompose, [(0, 0)]) 157 self.assertRaises(AssertionError, decompose, [(0, 0), (1, 1)]) 158 self.assertEqual([((0, 0), (1, 1), (2, 2))], 159 decompose([(0, 0), (1, 1), (2, 2)])) 160 self.assertEqual( 161 [((0, 0), (2, -2), (4, 0)), ((6, 2), (8, 8), (12, -12))], 162 decompose([(0, 0), (4, -4), (8, 8), (12, -12)])) 163 164 def test_decomposeQuadraticSegment(self): 165 decompose = decomposeQuadraticSegment 166 self.assertRaises(AssertionError, decompose, []) 167 self.assertRaises(AssertionError, decompose, [(0, 0)]) 168 self.assertEqual([((0,0), (4, 8))], decompose([(0, 0), (4, 8)])) 169 self.assertEqual([((0,0), (2, 4)), ((4, 8), (9, -9))], 170 decompose([(0, 0), (4, 8), (9, -9)])) 171 self.assertEqual( 172 [((0, 0), (2.0, 4.0)), ((4, 8), (6.5, -0.5)), ((9, -9), (10, 10))], 173 decompose([(0, 0), (4, 8), (9, -9), (10, 10)])) 174 175 176if __name__ == '__main__': 177 import sys 178 sys.exit(unittest.main()) 179