def test_parse_points(self): """ Sequences of points are parsed from svg paths. """ pp = PathParser(None) self.assertEqual(pp.parse_points(''), ([], '')) self.assertEqual(pp.parse_points('1 2 3.3 4.4 M'), ([(1, 2), (3.3, 4.4)], 'M'))
def test_get_path_point(self): """ get_path_point returns correct points """ pp = PathParser(None) pp.cur_point = (1, 2) self.assertEqual(pp.get_path_point((3, 4), False), (3, 4)) self.assertEqual(pp.get_path_point((3, 4), True), (4, 6))
def test_parse_s(self): """ shorthand bezier segments are parsed correctly """ pp = PathParser(None) pp.prev_cmd = 's' rest = pp.parse_s('6 12 18 24 -6 -12 -36 -42 rest', False) self.assertEqual(rest, 'rest') self.assertEqual(pp.start_point, (0.0, 0.0)) self.assertEqual(pp.cur_point, (-36.0, -42.0)) self.assertEqual(len(pp.shapes), 2) self.assertEqual(pp.shapes[0].type, 'bezier') self.assertEqual(pp.shapes[0].p1.x, 0) self.assertEqual(pp.shapes[0].p1.y, 0) self.assertEqual(pp.shapes[0].control1.x, 0) self.assertEqual(pp.shapes[0].control1.y, 0) self.assertEqual(pp.shapes[0].control2.x, 8) self.assertEqual(pp.shapes[0].control2.y, -15) self.assertEqual(pp.shapes[0].p2.x, 23) self.assertEqual(pp.shapes[0].p2.y, -30) self.assertEqual(pp.shapes[1].type, 'bezier') self.assertEqual(pp.shapes[1].p1.x, 23) self.assertEqual(pp.shapes[1].p1.y, -30) self.assertEqual(pp.shapes[1].control1.x, 38) self.assertEqual(pp.shapes[1].control1.y, -45) self.assertEqual(pp.shapes[1].control2.x, -8) self.assertEqual(pp.shapes[1].control2.y, 15) self.assertEqual(pp.shapes[1].p2.x, -45) self.assertEqual(pp.shapes[1].p2.y, 53) pp = PathParser(None) rest = pp.parse_s('6 12 18 24 -6 -12 -36 -42 rest', True) self.assertEqual(rest, 'rest') self.assertEqual(pp.start_point, (0.0, 0.0)) self.assertEqual(pp.cur_point, (-18.0, -18.0)) self.assertEqual(len(pp.shapes), 2) self.assertEqual(pp.shapes[0].type, 'bezier') self.assertEqual(pp.shapes[0].p1.x, 0) self.assertEqual(pp.shapes[0].p1.y, 0) self.assertEqual(pp.shapes[0].control1.x, 0) self.assertEqual(pp.shapes[0].control1.y, 0) self.assertEqual(pp.shapes[0].control2.x, 8) self.assertEqual(pp.shapes[0].control2.y, -15) self.assertEqual(pp.shapes[0].p2.x, 30) self.assertEqual(pp.shapes[0].p2.y, -45) self.assertEqual(pp.shapes[1].type, 'bezier') self.assertEqual(pp.shapes[1].p1.x, 30) self.assertEqual(pp.shapes[1].p1.y, -45) self.assertEqual(pp.shapes[1].control1.x, 53) self.assertEqual(pp.shapes[1].control1.y, -75) self.assertEqual(pp.shapes[1].control2.x, 23) self.assertEqual(pp.shapes[1].control2.y, -30) self.assertEqual(pp.shapes[1].p2.x, -23) self.assertEqual(pp.shapes[1].p2.y, 23)
def test_parse_q(self): """ quadratic bezier segments are parsed correctly """ pp = PathParser(None) rest = pp.parse_q('6 12 18 24 -6 -12 -36 -42 rest', False) self.assertEqual(rest, 'rest') self.assertEqual(pp.start_point, (0.0, 0.0)) self.assertEqual(pp.cur_point, (-36.0, -42.0)) self.assertEqual(len(pp.shapes), 2) self.assertEqual(pp.shapes[0].type, 'bezier') self.assertEqual(pp.shapes[0].p1.x, 0) self.assertEqual(pp.shapes[0].p1.y, 0) self.assertEqual(pp.shapes[0].control1.x, 5) self.assertEqual(pp.shapes[0].control1.y, -10) self.assertEqual(pp.shapes[0].control2.x, 13) self.assertEqual(pp.shapes[0].control2.y, -20) self.assertEqual(pp.shapes[0].p2.x, 23) self.assertEqual(pp.shapes[0].p2.y, -30) self.assertEqual(pp.shapes[1].type, 'bezier') self.assertEqual(pp.shapes[1].p1.x, 23) self.assertEqual(pp.shapes[1].p1.y, -30) self.assertEqual(pp.shapes[1].control1.x, 3) self.assertEqual(pp.shapes[1].control1.y, 0) self.assertEqual(pp.shapes[1].control2.x, -20) self.assertEqual(pp.shapes[1].control2.y, 28) self.assertEqual(pp.shapes[1].p2.x, -45) self.assertEqual(pp.shapes[1].p2.y, 53) pp = PathParser(None) rest = pp.parse_q('6 12 18 24 -6 -12 -36 -42 rest', True) self.assertEqual(rest, 'rest') self.assertEqual(pp.start_point, (0.0, 0.0)) self.assertEqual(pp.cur_point, (-18.0, -18.0)) self.assertEqual(len(pp.shapes), 2) self.assertEqual(pp.shapes[0].type, 'bezier') self.assertEqual(pp.shapes[0].p1.x, 0) self.assertEqual(pp.shapes[0].p1.y, 0) self.assertEqual(pp.shapes[0].control1.x, 5) self.assertEqual(pp.shapes[0].control1.y, -10) self.assertEqual(pp.shapes[0].control2.x, 15) self.assertEqual(pp.shapes[0].control2.y, -25) self.assertEqual(pp.shapes[0].p2.x, 30) self.assertEqual(pp.shapes[0].p2.y, -45) self.assertEqual(pp.shapes[1].type, 'bezier') self.assertEqual(pp.shapes[1].p1.x, 30) self.assertEqual(pp.shapes[1].p1.y, -45) self.assertEqual(pp.shapes[1].control1.x, 25) self.assertEqual(pp.shapes[1].control1.y, -35) self.assertEqual(pp.shapes[1].control2.x, 8) self.assertEqual(pp.shapes[1].control2.y, -13) self.assertEqual(pp.shapes[1].p2.x, -23) self.assertEqual(pp.shapes[1].p2.y, 23)
def test_parse_t(self): """ shorthand quadratic bezier segments are parsed correctly """ pp = PathParser(None) rest = pp.parse_t('6 12 -6 -12 rest', False) self.assertEqual(rest, 'rest') self.assertEqual(pp.start_point, (0.0, 0.0)) self.assertEqual(pp.cur_point, (-6.0, -12.0)) self.assertEqual(len(pp.shapes), 2) self.assertEqual(pp.shapes[0].type, 'bezier') self.assertEqual(pp.shapes[0].p1.x, 0) self.assertEqual(pp.shapes[0].p1.y, 0) self.assertEqual(pp.shapes[0].control1.x, 0) self.assertEqual(pp.shapes[0].control1.y, 0) self.assertEqual(pp.shapes[0].control2.x, 3) self.assertEqual(pp.shapes[0].control2.y, -5) self.assertEqual(pp.shapes[0].p2.x, 8) self.assertEqual(pp.shapes[0].p2.y, -15) self.assertEqual(pp.shapes[1].type, 'bezier') self.assertEqual(pp.shapes[1].p1.x, 8) self.assertEqual(pp.shapes[1].p1.y, -15) self.assertEqual(pp.shapes[1].control1.x, 13) self.assertEqual(pp.shapes[1].control1.y, -25) self.assertEqual(pp.shapes[1].control2.x, 8) self.assertEqual(pp.shapes[1].control2.y, -15) self.assertEqual(pp.shapes[1].p2.x, -8) self.assertEqual(pp.shapes[1].p2.y, 15) pp = PathParser(None) rest = pp.parse_t('6 12 -6 -12 rest', True) self.assertEqual(rest, 'rest') self.assertEqual(pp.start_point, (0.0, 0.0)) self.assertEqual(pp.cur_point, (-0.0, -0.0)) self.assertEqual(len(pp.shapes), 2) self.assertEqual(pp.shapes[0].type, 'bezier') self.assertEqual(pp.shapes[0].p1.x, 0) self.assertEqual(pp.shapes[0].p1.y, 0) self.assertEqual(pp.shapes[0].control1.x, 0) self.assertEqual(pp.shapes[0].control1.y, 0) self.assertEqual(pp.shapes[0].control2.x, 3) self.assertEqual(pp.shapes[0].control2.y, -5) self.assertEqual(pp.shapes[0].p2.x, 8) self.assertEqual(pp.shapes[0].p2.y, -15) self.assertEqual(pp.shapes[1].type, 'bezier') self.assertEqual(pp.shapes[1].p1.x, 8) self.assertEqual(pp.shapes[1].p1.y, -15) self.assertEqual(pp.shapes[1].control1.x, 13) self.assertEqual(pp.shapes[1].control1.y, -25) self.assertEqual(pp.shapes[1].control2.x, 10) self.assertEqual(pp.shapes[1].control2.y, -20) self.assertEqual(pp.shapes[1].p2.x, 0) self.assertEqual(pp.shapes[1].p2.y, 0)
def test_parse_l(self): """ lineto segments are parsed correctly """ pp = PathParser(None) rest = pp.parse_l('72 720 144 288 0 0 rest', False) self.assertEqual(rest, 'rest') self.assertEqual(pp.start_point, (0.0, 0.0)) self.assertEqual(pp.cur_point, (0, 0)) self.assertEqual(len(pp.shapes), 3) self.assertEqual(pp.shapes[0].type, 'line') self.assertEqual(pp.shapes[0].p1.x, 0) self.assertEqual(pp.shapes[0].p1.y, 0) self.assertEqual(pp.shapes[0].p2.x, 90) self.assertEqual(pp.shapes[0].p2.y, -900) self.assertEqual(pp.shapes[1].type, 'line') self.assertEqual(pp.shapes[1].p1.x, 90) self.assertEqual(pp.shapes[1].p1.y, -900) self.assertEqual(pp.shapes[1].p2.x, 180) self.assertEqual(pp.shapes[1].p2.y, -360) self.assertEqual(pp.shapes[2].type, 'line') self.assertEqual(pp.shapes[2].p1.x, 180) self.assertEqual(pp.shapes[2].p1.y, -360) self.assertEqual(pp.shapes[2].p2.x, 0) self.assertEqual(pp.shapes[2].p2.y, 0) pp = PathParser(None) rest = pp.parse_l('72 720 144 288 0 0 rest', True) self.assertEqual(rest, 'rest') self.assertEqual(pp.start_point, (0.0, 0.0)) self.assertEqual(pp.cur_point, (216.0, 1008.0)) self.assertEqual(len(pp.shapes), 3) self.assertEqual(pp.shapes[0].type, 'line') self.assertEqual(pp.shapes[0].p1.x, 0) self.assertEqual(pp.shapes[0].p1.y, 0) self.assertEqual(pp.shapes[0].p2.x, 90) self.assertEqual(pp.shapes[0].p2.y, -900) self.assertEqual(pp.shapes[1].type, 'line') self.assertEqual(pp.shapes[1].p1.x, 90) self.assertEqual(pp.shapes[1].p1.y, -900) self.assertEqual(pp.shapes[1].p2.x, 270) self.assertEqual(pp.shapes[1].p2.y, -1260) self.assertEqual(pp.shapes[2].type, 'line') self.assertEqual(pp.shapes[2].p1.x, 270) self.assertEqual(pp.shapes[2].p1.y, -1260) self.assertEqual(pp.shapes[2].p2.x, 270) self.assertEqual(pp.shapes[2].p2.y, -1260)
def test_parse_v(self): """ vertical lineto segments are parsed correctly """ pp = PathParser(None) rest = pp.parse_v('72 144 288 rest', False) self.assertEqual(rest, 'rest') self.assertEqual(pp.start_point, (0.0, 0.0)) self.assertEqual(pp.cur_point, (0, 288)) self.assertEqual(len(pp.shapes), 3) self.assertEqual(pp.shapes[0].type, 'line') self.assertEqual(pp.shapes[0].p1.x, 0) self.assertEqual(pp.shapes[0].p1.y, 0) self.assertEqual(pp.shapes[0].p2.x, 0) self.assertEqual(pp.shapes[0].p2.y, -90) self.assertEqual(pp.shapes[1].type, 'line') self.assertEqual(pp.shapes[1].p1.x, 0) self.assertEqual(pp.shapes[1].p1.y, -90) self.assertEqual(pp.shapes[1].p2.x, 0) self.assertEqual(pp.shapes[1].p2.y, -180) self.assertEqual(pp.shapes[2].type, 'line') self.assertEqual(pp.shapes[2].p1.x, 0) self.assertEqual(pp.shapes[2].p1.y, -180) self.assertEqual(pp.shapes[2].p2.x, 0) self.assertEqual(pp.shapes[2].p2.y, -360) pp = PathParser(None) rest = pp.parse_v('72 72 72 rest', True) self.assertEqual(rest, 'rest') self.assertEqual(pp.start_point, (0.0, 0.0)) self.assertEqual(pp.cur_point, (0.0, 216.0)) self.assertEqual(len(pp.shapes), 3) self.assertEqual(pp.shapes[0].type, 'line') self.assertEqual(pp.shapes[0].p1.x, 0) self.assertEqual(pp.shapes[0].p1.y, 0) self.assertEqual(pp.shapes[0].p2.x, 0) self.assertEqual(pp.shapes[0].p2.y, -90) self.assertEqual(pp.shapes[1].type, 'line') self.assertEqual(pp.shapes[1].p1.x, 0) self.assertEqual(pp.shapes[1].p1.y, -90) self.assertEqual(pp.shapes[1].p2.x, 0) self.assertEqual(pp.shapes[1].p2.y, -180) self.assertEqual(pp.shapes[2].type, 'line') self.assertEqual(pp.shapes[2].p1.x, 0) self.assertEqual(pp.shapes[2].p1.y, -180) self.assertEqual(pp.shapes[2].p2.x, 0) self.assertEqual(pp.shapes[2].p2.y, -270)
def test_parse_z(self): """ closepath segments are parsed correctly """ pp = PathParser(None) pp.cur_point = (72, 144) pp.start_point = (-72, -144) rest = pp.parse_z('rest', False) self.assertEqual(rest, 'rest') self.assertEqual(pp.start_point, (-72, -144)) self.assertEqual(pp.cur_point, (-72, -144)) self.assertEqual(len(pp.shapes), 1) self.assertEqual(pp.shapes[0].type, 'line') self.assertEqual(pp.shapes[0].p1.x, 90) self.assertEqual(pp.shapes[0].p1.y, -180) self.assertEqual(pp.shapes[0].p2.x, -90) self.assertEqual(pp.shapes[0].p2.y, 180)
def test_parse_nums(self): """ Numbers are parsed from svg paths. """ pp = PathParser(None) self.assertEqual(pp.parse_nums(''), ([], '')) self.assertEqual(pp.parse_nums('1'), ([1.0], '')) self.assertEqual(pp.parse_nums('1.2 3.4'), ([1.2, 3.4], '')) self.assertEqual(pp.parse_nums('1.2,3.4 5.6'), ([1.2, 3.4, 5.6], '')) self.assertEqual(pp.parse_nums('1.2,3.4 5.6L12'), ([1.2, 3.4, 5.6], 'L12')) self.assertEqual(pp.parse_nums('1.2,3.4 5.6 L12'), ([1.2, 3.4, 5.6], 'L12'))