def test_ellipse01_path21_normalize(self): # See also: ellipse01.html d = 'M93.969,-85.505 A100,250 0 0 1 70.711,176.777' path_data = PathParser.parse(d) normalized = PathParser.normalize(path_data) exp = PathParser.tostring(normalized) expected = \ "M93.969,-85.505" \ " C96.024,-107.64 104.802,-23.844 97.457,56.022" \ " 90.111,135.889 67.575,201.684 70.711,176.777" self.assertEqual(expected, exp)
def test_arcs02_path04_bearing(self): # See also: arcs02.html # same as 'transform="rotate(90 125 75)"' d = 'M 125,75 B90 a100,50 0 1,1 100,50' path_data = PathParser.parse(d) normalized = PathParser.normalize(path_data) exp = PathParser.tostring(normalized) expected = \ "M125,75 C152.614,75 175,119.772 175,175" \ " 175,230.228 152.614,275 125,275 97.386,275 75,230.228 75,175" self.assertEqual(expected, exp)
def test_ellipse01_path33_normalize(self): # See also: ellipse01.html d = 'M-85.505,93.969 A250,100 0 0 0 176.777,70.711' path_data = PathParser.parse(d) normalized = PathParser.normalize(path_data) exp = PathParser.tostring(normalized) expected = \ "M-85.505,93.969" \ " C-107.64,96.024 -23.844,104.802 56.022,97.457" \ " 135.889,90.111 201.684,67.575 176.777,70.711" self.assertEqual(expected, exp)
def test_arcs02_path03_bearing(self): # See also: arcs02.html # same as 'transform="rotate(90 125 75)"' d = 'M 125,75 B90 a100,50 0 1,0 100,50' path_data = PathParser.parse(d) normalized = PathParser.normalize(path_data) exp = PathParser.tostring(normalized) expected = \ "M125,75 C125,19.772 102.614,-25 75,-25" \ " 47.386,-25 25,19.772 25,75 25,130.228 47.386,175 75,175" self.assertEqual(expected, exp)
def test_path_normalize_negative01(self): # cx=0 cy=0 r=100 # large_arc_flag=0, sweep_flag=1 # angle: 30 to -30 d = "M86.60254037844388,-49.99999999999999" \ " A100,100 0 0 1 86.60254037844386,50.000000000000014" path_data = PathParser.parse(d) normalized = PathParser.normalize(path_data) exp = PathParser.tostring(normalized) expected = 'M86.603,-50 C104.466,-19.06 104.466,19.06 86.603,50' self.assertEqual(expected, exp)
def test_arcs02_path01_normalize(self): # See also: arcs02.html # x_axis_rotation: 0 # large_arc_flag: 0 # sweep_flag: 0 d = 'M125,75 a100,50 0 0 0 100,50' path_data = PathParser.parse(d) normalized = PathParser.normalize(path_data) exp = PathParser.tostring(normalized) expected = 'M125,75 C125,102.614 169.772,125 225,125' self.assertEqual(expected, exp)
def test_arcs02_path02_normalize(self): # See also: arcs02.html # x_axis_rotation: 0 # large_arc_flag: 0 # sweep_flag: 1 d = 'M 125,75 a100,50 0 0,1 100,50' path_data = PathParser.parse(d) normalized = PathParser.normalize(path_data) exp = PathParser.tostring(normalized) expected = 'M125,75 C180.228,75 225,97.386 225,125' self.assertEqual(expected, exp)
def test_quad02_path03_normalize(self): # See also: quad02.html d = 'm200,300 q200,-250 400,0 t400,0 400,0' path_data = PathParser.parse(d) normalized = PathParser.normalize(path_data) d = PathParser.tostring(normalized) expected = "M200,300" \ " C333.333,133.333 466.667,133.333 600,300" \ " 733.333,466.667 866.667,466.667 1000,300" \ " 1133.333,133.333 1266.667,133.333 1400,300" self.assertEqual(expected, d)
def test_path_transform02(self): # See also: svg.svg # Mm Aa Hh Zz d = 'M-27-5a7,7,0,1,0,0,10h54a7,7,0,1,0,0-10z' path_data = PathParser.parse(d) matrix = DOMMatrix() matrix.rotate_self(rot_z=90) transformed = PathParser.transform(path_data, matrix) exp = PathParser.tostring(transformed) expected = 'M5,-27 A7,7 0 1 0 -5,-27 L-5,27 A7,7 0 1 0 5,27 Z' self.assertEqual(expected, exp)
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_ellipse01_path31_normalize(self): # See also: ellipse01.html d = 'M85.505,-93.969 A250,100 0 0 0 -176.777,-70.711' path_data = PathParser.parse(d) normalized = PathParser.normalize(path_data) exp = PathParser.tostring(normalized) expected = \ "M85.505,-93.969" \ " C107.64,-96.024 23.844,-104.802 -56.022,-97.457" \ " -135.889,-90.111 -201.684,-67.575 -176.777,-70.711" self.assertEqual(expected, exp)
def test_ellipse01_path22_normalize(self): # See also: ellipse01.html d = 'M50,216.506 A100,250 0 1 1 86.603,-125' path_data = PathParser.parse(d) normalized = PathParser.normalize(path_data) exp = PathParser.tostring(normalized) expected = \ "M50,216.506" \ " C-22.172,269.38 -68.138,215.917 -89.023,113.876" \ " -109.908,11.834 -100.359,-112.642 -65.857,-188.13" \ " -31.355,-263.618 19.262,-270.777 86.603,-125" self.assertEqual(expected, exp)
def test_path_transform03(self): # See also: svg.svg # Mm Aa Hh Zz d = 'M-27-5a7,7,0,1,0,0,10h54a7,7,0,1,0,0-10z' path_data = PathParser.parse(d) matrix = DOMMatrix() matrix.rotate_self(rot_z=135) transformed = PathParser.transform(path_data, matrix) exp = PathParser.tostring(transformed) expected = "M22.627,-15.556 A7,7 0 1 0 15.556,-22.627 L-22.627,15.556" \ " A7,7 0 1 0 -15.556,22.627 Z" self.assertEqual(expected, exp)
def test_cubic03_path01_transform(self): # See also: cubic03.html d = \ "M100,200 C120,100 155,100 175,200 S225,300 245,200 300,100 320,200" path_data = PathParser.parse(d) matrix = DOMMatrix() matrix.translate_self(-50, 50) transformed = PathParser.transform(path_data, matrix) d2 = PathParser.tostring(transformed) expected = \ "M50,250 C70,150 105,150 125,250 S175,350 195,250 250,150 270,250" self.assertEqual(expected, d2)
def test_cubic02_01_path06_transform(self): # See also: cubic02_01.html d = 'm600,800 c25,-100 125,-100 150,0 s125,100 150,0' path_data = PathParser.parse(d) # "M600,800 C625,700 725,700 750,800 775,900 875,900 900,800" matrix = DOMMatrix() matrix.translate_self(-50, 50) transformed = PathParser.transform(path_data, matrix) d2 = PathParser.tostring(transformed) expected = 'M550,850 C575,750 675,750 700,850 S825,950 850,850' self.assertEqual(expected, d2)
def test_cubic02_01_path06_normalize(self): # See also: cubic02_01.html d = 'm600,800 c25,-100 125,-100 150,0 s125,100 150,0' path_data = PathParser.parse(d) normalized = PathParser.normalize(path_data) d = PathParser.tostring(normalized) expected = 'M600,800 C625,700 725,700 750,800 775,900 875,900 900,800' self.assertEqual(expected, d) n = PathParser.get_total_length(normalized) expected = 457.37384033203125 self.assertAlmostEqual(expected, n, places=places)
def test_bearing01_transform01(self): # See also: bearing01.html d = 'M150,10 B36 h47 b72 h47 b72 h47 b72 h47 Z' path_data = PathParser.parse(d) matrix = DOMMatrix() matrix.translate_self(10, 10) transformed = PathParser.transform(path_data, matrix) d = PathParser.tostring(transformed) expected = \ "M160,20 L198.023799,47.625907 183.5,92.325563 136.5,92.325563" \ " 121.976201,47.625907 Z" self.assertEqual(expected, d)
def test_bearing01_transform02(self): # See also: bearing01.html d = 'M150,10 B36 h47 b72 h47 b72 h47 b72 h47 Z' path_data = PathParser.parse(d) matrix = DOMMatrix() matrix = matrix.flip_y() transformed = PathParser.transform(path_data, matrix) d = PathParser.tostring(transformed) 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_path_parse02(self): # skip invalid segments ("L", "L" and "l") # "M100,100 L200,100 l10,100" d = 'M100,100 L L L200,100 l l0,100' path_data = PathParser.parse(d) d = PathParser.tostring(path_data) expected = 'M100,100 L200,100 l0,100' self.assertEqual(expected, d) n = PathParser.get_total_length(path_data) expected = 200 self.assertAlmostEqual(expected, n)
def test_arcs01_path04_normalize(self): # See also: arcs01.html # x_axis_rotation: 0 # large_arc_flag: 0 # sweep_flag: 1 d = 'M950,175 a25,100 0 0,1 50,-25' path_data = PathParser.parse(d) normalized = PathParser.normalize(path_data) exp = PathParser.tostring(normalized) expected = \ "M950,175" \ " C946.243,157.787 951.287,97.252 963.733,72.361" \ " 976.178,47.47 991.312,67.648 1000,150" self.assertEqual(expected, exp)
def test_arcs01_path05_normalize(self): # See also: arcs01.html # x_axis_rotation: -30 # large_arc_flag: 0 # sweep_flag: 1 d = 'M950,175 a25,100 -30 0 1 50,-25' path_data = PathParser.parse(d) normalized = PathParser.normalize(path_data) exp = PathParser.tostring(normalized) expected = \ "M950,175" \ " C917.568,116.885 905.572,69.669 920.741,65.176" \ " 935.909,60.683 972.499,100.614 1000,150" self.assertEqual(expected, exp, msg=d)
def test_arcs01_path06_normalize(self): # See also: arcs01.html # center: (945.586, 226.768) # start: (950, 175) # end: (50, -25) # x_axis_rotation: 30 # large_arc_flag: 0 # sweep_flag: 1 d = 'M950,175 a25,100 +30 0,1 50,-25' path_data = PathParser.parse(d) normalized = PathParser.normalize(path_data) exp = PathParser.tostring(normalized) expected = \ "M950,175 C988.939,138.516 994.366,138.049 1000,150" self.assertEqual(expected, exp)
def test_path_normalize_negative02(self): # large_arc_flag=1, sweep_flag=1 # angle: -30 to 30 d = "M86.60254037844388,50.000000000000014" \ " A100,100 0 1 1 86.60254037844386,-49.99999999999999" path_data = PathParser.parse(d) normalized = PathParser.normalize(path_data) exp = PathParser.tostring(normalized) expected = \ "M86.603,50" \ " C63.972,89.197 17.836,108.307 -25.882,96.593" \ " -69.6,84.878 -100,45.261 -100,0" \ " -100,-45.261 -69.6,-84.878 -25.882,-96.593" \ " 17.836,-108.307 63.972,-89.197 86.603,-50" self.assertEqual(expected, exp)
def test_arcs02_path03_normalize(self): # See also: arcs02.html # x_axis_rotation: 0 # large_arc_flag: 1 # sweep_flag: 0 d = 'M 125,75 a100,50 0 1,0 100,50' path_data = PathParser.parse(d) normalized = PathParser.normalize(path_data) exp = PathParser.tostring(normalized) expected = \ "M125,75" \ " C69.772,75 25,97.386 25,125" \ " 25,152.614 69.772,175 125,175" \ " 180.228,175 225,152.614 225,125" self.assertEqual(expected, exp)
def test_arcs02_path04_normalize(self): # See also: arcs02.html # x_axis_rotation: 0 # large_arc_flag: 1 # sweep_flag: 1 d = 'M 125,75 a100,50 0 1,1 100,50' path_data = PathParser.parse(d) normalized = PathParser.normalize(path_data) exp = PathParser.tostring(normalized) expected = \ "M125,75" \ " C125,47.386 169.772,25 225,25" \ " 280.228,25 325,47.386 325,75" \ " 325,102.614 280.228,125 225,125" self.assertEqual(expected, exp)
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 test_cubic03_path01_normalize(self): # See also: cubic03.html # "M" "C" "S" "S" d = "M100,200" \ " C120,100 155,100 175,200 S225,300 245,200 300,100 320,200" # "C120,100 155,100 175,200" # p1=(175+175-155=195,200+200-100=300) # # "S225,300 245,200" -> "C195,300 225,300 245,200" # p1=(245+245-225=265,200+200-300=100) path_data = PathParser.parse(d) normalized = PathParser.normalize(path_data) d = PathParser.tostring(normalized) expected = "M100,200" \ " C120,100 155,100 175,200 195,300 225,300 245,200" \ " 265,100 300,100 320,200" self.assertEqual(expected, d)
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_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_arcs01_path06_normalize_negative(self): # See also: arcs01.html # center: (945.586, 226.768) # start: (950, 175) # end: (50, -25) # x_axis_rotation: 30 # large_arc_flag: 1 # sweep_flag: 0 d = 'M950,175 a25,100 +30 1,0 50,-25' path_data = PathParser.parse(d) normalized = PathParser.normalize(path_data) exp = PathParser.tostring(normalized) expected = \ "M950,175" \ " C961.201,155.808 930.521,197.64 909.893,240.907" \ " 889.265,284.175 885.058,315.52 900.052,314.23" \ " 915.046,312.941 944.611,279.415 969.213,235.806" \ " 993.814,192.196 1005.855,151.968 1000,150" self.assertEqual(expected, exp)