def test_vertical_lineto_rel_length(self): path_data = list() path_data.append(SVGPathSegment('m', 50, 50)) path_data.append(SVGPathSegment('v', -50)) n = PathParser.get_total_length(path_data) expected = 50 self.assertEqual(expected, n)
def test_vertical_lineto_abs_length(self): path_data = list() path_data.append(SVGPathSegment('M', 50, 50)) path_data.append(SVGPathSegment('V', 150)) n = PathParser.get_total_length(path_data) expected = 100 self.assertEqual(expected, n)
def test_ellipse01_path33_length(self): # See also: ellipse01.html # rx > ry # x_axis_rotation: 0 # large_arc_flag: 0 # sweep_flag: 0 path_data = list() cx = 0 cy = 0 rx = 250 ry = 100 ellipse = Ellipse(cx, cy, rx, ry) cpx, cpy = ellipse.point(270 - 20) path_data.append(SVGPathSegment('M', cpx, cpy)) rotation = 0 fa = 0 fs = 0 epx, epy = ellipse.point(270 + 45) path_data.append( SVGPathSegment('A', rx, ry, rotation, fa, fs, epx, epy)) # -> "M-85.505,93.969 A250,100 0 0 0 176.777,70.711" n = PathParser.get_total_length(path_data) # expected = 265.98455810546875 # firefox # expected = 265.98468017578125 expected = 265.9847106933594 self.assertAlmostEqual(expected, n, places=places)
def test_ellipse01_path34_length(self): # See also: ellipse01.html # rx > ry # x_axis_rotation: 0 # large_arc_flag: 0 # sweep_flag: 0 path_data = list() cx = 0 cy = 0 rx = 250 ry = 100 ellipse = Ellipse(cx, cy, rx, ry) cpx, cpy = ellipse.point(0 - 20) path_data.append(SVGPathSegment('M', cpx, cpy)) rotation = 0 fa = 0 fs = 0 epx, epy = ellipse.point(0 + 45) path_data.append( SVGPathSegment('A', rx, ry, rotation, fa, fs, epx, epy)) # -> 'M234.923,34.202 A250,100 0 0 0 176.777,-70.711' n = PathParser.get_total_length(path_data) # expected = 145.73971557617188 # firefox expected = 145.73988342285156 self.assertAlmostEqual(expected, n, places=places)
def test_ellipse01_path22_length(self): # See also: ellipse01.html # ry > rx # x_axis_rotation: 0 # large_arc_flag: 1 # sweep_flag: 1 path_data = list() cx = 0 cy = 0 rx = 100 ry = 250 ellipse = Ellipse(cx, cy, rx, ry) cpx, cpy = ellipse.point(-60) path_data.append(SVGPathSegment('M', cpx, cpy)) rotation = 0 fa = 1 fs = 1 epx, epy = ellipse.point(30) path_data.append( SVGPathSegment('A', rx, ry, rotation, fa, fs, epx, epy)) # d = PathParser.tostring(path_data) # -> "M50,216.506 A100,250 0 1 1 86.603,-125" n = PathParser.get_total_length(path_data) # expected = 799.613037109375 # firefox expected = 799.7357177734375 self.assertAlmostEqual(expected, n, places=places)
def test_ellipse01_path21_length(self): # See also: ellipse01.html # ry > rx # x_axis_rotation: 0 # large_arc_flag: 0 # sweep_flag: 1 path_data = list() cx = 0 cy = 0 rx = 100 ry = 250 ellipse = Ellipse(cx, cy, rx, ry) cpx, cpy = ellipse.point(20) path_data.append(SVGPathSegment('M', cpx, cpy)) rotation = 0 fa = 0 fs = 1 epx, epy = ellipse.point(-45) path_data.append( SVGPathSegment('A', rx, ry, rotation, fa, fs, epx, epy)) # d = PathParser.tostring(path_data) # -> "M93.969,-85.505 A100,250 0 0 1 70.711,176.777" n = PathParser.get_total_length(path_data) # expected = 265.98455810546875 # firefox expected = 265.9847106933594 self.assertAlmostEqual(expected, n, places=places)
def test_horizontal_lineto_abs_normalize(self): path_data = list() path_data.append(SVGPathSegment('M', 0, 50)) path_data.append(SVGPathSegment('H', 100)) normalize = PathParser.normalize(path_data) d = PathParser.tostring(normalize) expected = 'M0,50 L100,50' self.assertEqual(expected, d)
def test_horizontal_lineto_rel_normalize(self): path_data = list() path_data.append(SVGPathSegment('m', 100, 50)) path_data.append(SVGPathSegment('h', -100)) normalized = PathParser.normalize(path_data) d = PathParser.tostring(normalized) expected = 'M100,50 L0,50' self.assertEqual(expected, d)
def test_segment_null(self): segment = SVGPathSegment() self.assertTrue(not segment.isvalid()) self.assertIsNone(segment.type) self.assertIsNone(segment.isabsolute()) self.assertIsNone(segment.isrelative()) self.assertEqual((), segment.values) self.assertEqual((None, None), segment.end)
def test_vertical_lineto_rel_normalize(self): path_data = list() path_data.append(SVGPathSegment('m', 50, 50)) path_data.append(SVGPathSegment('v', -50)) normalized = PathParser.normalize(path_data) d = PathParser.tostring(normalized) expected = 'M50,50 L50,0' self.assertEqual(expected, d)
def test_path_curveto_rel(self): path_data = list() path_data.append(SVGPathSegment('m', 600, 800)) path_data.append(SVGPathSegment('c', 25, -100, 125, -100, 150, 0)) path_data.append(SVGPathSegment('s', 125, 100, 150, 0)) d = PathParser.tostring(path_data) expected = 'm600,800 c25,-100 125,-100 150,0 s125,100 150,0' self.assertEqual(expected, d)
def test_path_curveto_abs(self): path_data = list() path_data.append(SVGPathSegment('M', 100, 200)) path_data.append(SVGPathSegment('C', 100, 100, 250, 100, 250, 200)) path_data.append(SVGPathSegment('S', 400, 300, 400, 200)) d = PathParser.tostring(path_data) expected = 'M100,200 C100,100 250,100 250,200 S400,300 400,200' self.assertEqual(expected, d)
def test_moveto_rel_normalize(self): path_data = list() path_data.append(SVGPathSegment('m', 100, 100)) path_data.append(SVGPathSegment('m', 50, 0)) path_data.append(SVGPathSegment('m', 0, 50)) path_data.append(SVGPathSegment('z')) normalized = PathParser.normalize(path_data) d = PathParser.tostring(normalized) expected = 'M100,100 150,100 150,150 Z' self.assertEqual(expected, d)
def test_horizontal_lineto_rel_bbox(self): path_data = list() path_data.append(SVGPathSegment('m', 100, 50)) path_data.append(SVGPathSegment('h', -100)) normalized = PathParser.normalize(path_data) # "M100,50 L0,50" bbox = PathParser.get_bbox(normalized) self.assertAlmostEqual(0, bbox.x, places=places) self.assertAlmostEqual(50, bbox.y, places=places) self.assertAlmostEqual(100, bbox.width, places=places) self.assertAlmostEqual(0, bbox.height, places=places)
def test_moveto_rel_length(self): # implicit "lineto" command path_data = list() path_data.append(SVGPathSegment('m', 100, 100)) path_data.append(SVGPathSegment('m', 50, 0)) path_data.append(SVGPathSegment('m', 0, 50)) path_data.append(SVGPathSegment('m', -50, 0)) path_data.append(SVGPathSegment('z')) n = PathParser.get_total_length(path_data) expected = 200 self.assertEqual(expected, n)
def test_lineto_abs_normalize(self): path_data = list() path_data.append(SVGPathSegment('M', 100, 100)) path_data.append(SVGPathSegment('L', 300, 100)) path_data.append(SVGPathSegment('L', 200, 300)) path_data.append(SVGPathSegment('z')) normalized = PathParser.normalize(path_data) d = PathParser.tostring(normalized) expected = 'M100,100 L300,100 200,300 Z' self.assertEqual(expected, d)
def test_horizontal_lineto_rel_length(self): path_data = list() path_data.append(SVGPathSegment('m', 100, 50)) path_data.append(SVGPathSegment('h', -100)) d = PathParser.tostring(path_data) expected = 'm100,50 h-100' self.assertEqual(expected, d) n = PathParser.get_total_length(path_data) expected = 100 self.assertAlmostEqual(expected, n)
def test_path_tostring_null(self): # skip invalid segments path_data = list() path_data.append(SVGPathSegment('M')) path_data.append(SVGPathSegment('l')) path_data.append(SVGPathSegment('l')) path_data.append(SVGPathSegment('l')) path_data.append(SVGPathSegment('z')) d = PathParser.tostring(path_data) expected = 'z' self.assertEqual(expected, d)
def test_moveto_rel_bbox(self): # implicit "lineto" command path_data = list() path_data.append(SVGPathSegment('m', 100, 100)) path_data.append(SVGPathSegment('m', 50, 0)) path_data.append(SVGPathSegment('m', 0, 50)) path_data.append(SVGPathSegment('m', -50, 0)) path_data.append(SVGPathSegment('z')) normalized = PathParser.normalize(path_data) bbox = PathParser.get_bbox(normalized) self.assertEqual(100, bbox.x) self.assertEqual(100, bbox.y) self.assertEqual(50, bbox.width) self.assertEqual(50, bbox.height)
def test_lineto_abs_length(self): path_data = list() path_data.append(SVGPathSegment('M', 100, 100)) path_data.append(SVGPathSegment('L', 300, 100)) path_data.append(SVGPathSegment('L', 200, 300)) path_data.append(SVGPathSegment('z')) d = PathParser.tostring(path_data) expected = 'M100,100 L300,100 200,300 z' self.assertEqual(expected, d) n = PathParser.get_total_length(path_data) # sqrt((300 - 100) ** 2 + (100 - 100) ** 2) # + sqrt((200 - 300) ** 2 + (300 - 100) ** 2) # + sqrt((100 - 200) ** 2 + (100 - 300) ** 2) # = 647.2135954999579 expected = 647.2135954999579 self.assertAlmostEqual(expected, n)
def _cubic_to(x1, y1, x2, y2, x, y, user): x1 /= 64 y1 /= 64 x2 /= 64 y2 /= 64 x /= 64 y /= 64 user.append(SVGPathSegment('C', x1, y1, x2, y2, x, y))
def test_moveto_abs_length(self): # implicit lineto path_data = list() path_data.append(SVGPathSegment('M', 100, 100)) path_data.append(SVGPathSegment('M', 50, 0)) path_data.append(SVGPathSegment('M', 0, 50)) path_data.append(SVGPathSegment('z')) d = PathParser.tostring(path_data) expected = 'M100,100 50,0 0,50 z' self.assertEqual(expected, d) n = PathParser.get_total_length(path_data) # (100, 100) - (50, 0) - (0, 50) - (100, 100) # sqrt((50 - 100) ** 2 + (0 - 100) ** 2) # + sqrt((0 - 50) ** 2 + (50 - 0) ** 2) # + sqrt((100 - 0) ** 2 + (100 - 50) ** 2) # = 294.3174758686337 expected = 294.3174758686337 self.assertAlmostEqual(expected, n)
def test_lineto_rel_length(self): # See also: bearing01.html # "M150,10 B36 h47 b72 h47 b72 h47 b72 h47 Z" path_data = list() path_data.append(SVGPathSegment('m', 150, 10)) path_data.append( SVGPathSegment('l', 38.02379873562253, 27.62590685774624)) path_data.append( SVGPathSegment('l', -14.52379873562252, 44.69965626587222)) path_data.append(SVGPathSegment('l', -47, 0)) path_data.append( SVGPathSegment('l', -14.52379873562254, -44.69965626587221)) path_data.append(SVGPathSegment('z')) d = PathParser.tostring(path_data) expected = "m150,10" \ " l38.023799,27.625907" \ " -14.523799,44.699656" \ " -47,0" \ " -14.523799,-44.699656" \ " z" self.assertEqual(expected, d) n = PathParser.get_total_length(path_data) expected = 235 self.assertAlmostEqual(expected, n)
def test_lineto_rel_normalize(self): # See also: bearing01.html # "M150,10 B36 h47 b72 h47 b72 h47 b72 h47 Z" path_data = list() path_data.append(SVGPathSegment('m', 150, 10)) path_data.append( SVGPathSegment('l', 38.02379873562253, 27.62590685774624)) path_data.append( SVGPathSegment('l', -14.52379873562252, 44.69965626587222)) path_data.append(SVGPathSegment('l', -47, 0)) path_data.append( SVGPathSegment('l', -14.52379873562254, -44.69965626587221)) path_data.append(SVGPathSegment('z')) normalized = PathParser.normalize(path_data) d = PathParser.tostring(normalized) expected = \ "M150,10 L188.023799,37.625907 173.5,82.325563 126.5,82.325563" \ " 111.976201,37.625907 Z" self.assertEqual(expected, d)
def test_segment_vertical_lineto_rel(self): segment = SVGPathSegment('v') self.assertTrue(not segment.isvalid()) self.assertTrue(not segment.isabsolute()) self.assertTrue(segment.isrelative()) self.assertEqual((), segment.values) self.assertEqual((None, None), segment.end) d = segment.tostring() self.assertEqual('', d) segment = SVGPathSegment('v', -50) self.assertTrue(segment.isvalid()) self.assertTrue(not segment.isabsolute()) self.assertTrue(segment.isrelative()) self.assertEqual((-50,), segment.values) self.assertEqual((0, -50), segment.end) d = segment.tostring() self.assertEqual('v-50', d) segment = SVGPathSegment() segment.set_vertical_lineto_rel(150) self.assertTrue(segment.isvalid()) self.assertTrue(not segment.isabsolute()) self.assertTrue(segment.isrelative()) self.assertEqual((150,), segment.values) self.assertEqual((0, 150), segment.end) d = segment.tostring() self.assertEqual('v150', d)
def test_segment_moveto_rel(self): segment = SVGPathSegment('m') self.assertTrue(not segment.isvalid()) self.assertTrue(not segment.isabsolute()) self.assertTrue(segment.isrelative()) self.assertEqual((), segment.values) self.assertEqual((None, None), segment.end) exp = segment.tostring() self.assertEqual('', exp) segment = SVGPathSegment('m', 100, 50) self.assertTrue(segment.isvalid()) self.assertTrue(not segment.isabsolute()) self.assertTrue(segment.isrelative()) self.assertEqual((100, 50), segment.values) self.assertEqual((100, 50), segment.end) exp = segment.tostring() self.assertEqual('m100,50', exp) segment = SVGPathSegment() segment.set_moveto_rel(100, 100) segment.set_moveto_rel(50, 0) # overwrite self.assertTrue(segment.isvalid()) self.assertTrue(not segment.isabsolute()) self.assertTrue(segment.isrelative()) self.assertEqual((50, 0), segment.values) self.assertEqual((50, 0), segment.end) exp = segment.tostring() self.assertEqual('m50,0', exp)
def test_segment_lineto_abs(self): segment = SVGPathSegment('L') self.assertTrue(not segment.isvalid()) self.assertTrue(segment.isabsolute()) self.assertTrue(not segment.isrelative()) self.assertEqual((), segment.values) self.assertEqual((None, None), segment.end) d = segment.tostring() self.assertEqual('', d) segment = SVGPathSegment() segment.set_lineto_abs(100, 50) self.assertTrue(segment.isvalid()) self.assertTrue(segment.isabsolute()) self.assertTrue(not segment.isrelative()) self.assertEqual((100, 50), segment.values) self.assertEqual((100, 50), segment.end) d = segment.tostring() self.assertEqual('L100,50', d) segment = SVGPathSegment('L', -50, 0) self.assertTrue(segment.isvalid()) self.assertTrue(segment.isabsolute()) self.assertTrue(not segment.isrelative()) self.assertEqual((-50, 0), segment.values) self.assertEqual((-50, 0), segment.end) d = segment.tostring() self.assertEqual('L-50,0', d)
def test_segment_horizontal_lineto_rel(self): segment = SVGPathSegment('h') self.assertTrue(not segment.isvalid()) self.assertTrue(not segment.isabsolute()) self.assertTrue(segment.isrelative()) self.assertEqual((), segment.values) self.assertEqual((None, None), segment.end) d = segment.tostring() self.assertEqual('', d) segment = SVGPathSegment('h', 100) self.assertTrue(segment.isvalid()) self.assertTrue(not segment.isabsolute()) self.assertTrue(segment.isrelative()) self.assertEqual((100,), segment.values) self.assertEqual((100, 0), segment.end) d = segment.tostring() self.assertEqual('h100', d) segment = SVGPathSegment() segment.set_horizontal_lineto_rel(50) self.assertTrue(segment.isvalid()) self.assertTrue(not segment.isabsolute()) self.assertTrue(segment.isrelative()) self.assertEqual((50,), segment.values) self.assertEqual((50, 0), segment.end) d = segment.tostring() self.assertEqual('h50', d)
def test_segment_bearing_rel(self): segment = SVGPathSegment('b') self.assertEqual((), segment.values) self.assertEqual((None, None), segment.end) exp = segment.tostring() self.assertEqual('', exp) segment = SVGPathSegment() bearing = segment.get_bearing(-90) self.assertEqual(-90, bearing) self.assertEqual((), segment.values) self.assertEqual((None, None), segment.end) segment = SVGPathSegment() segment.set_bearing_rel(-45) bearing = segment.get_bearing(-90) self.assertEqual(-135, bearing) self.assertEqual((-45,), segment.values) self.assertEqual((None, None), segment.end) segment = SVGPathSegment('b', 45) self.assertEqual((45,), segment.values) bearing = segment.get_bearing(-90) self.assertEqual(-45, bearing) segment = SVGPathSegment('b', -45) exp = segment.tostring() self.assertEqual('b-45', exp)
def test_segment_bearing_abs(self): segment = SVGPathSegment('B') self.assertTrue(not segment.isvalid()) self.assertTrue(segment.isabsolute()) self.assertTrue(not segment.isrelative()) self.assertEqual((), segment.values) self.assertEqual((None, None), segment.end) exp = segment.tostring() self.assertEqual('', exp) segment = SVGPathSegment('B') bearing = segment.get_bearing(-90) self.assertEqual(-90, bearing) self.assertEqual((), segment.values) self.assertEqual((None, None), segment.end) segment = SVGPathSegment() segment.set_bearing_abs(-45) bearing = segment.get_bearing(-90) self.assertEqual(-45, bearing) self.assertEqual(segment.values, (-45,)) self.assertEqual((None, None), segment.end) segment = SVGPathSegment('B', 45) self.assertEqual((45,), segment.values) bearing = segment.get_bearing(-90) self.assertEqual(45, bearing) segment = SVGPathSegment('B', -45) exp = segment.tostring() self.assertEqual('B-45', exp)