def test_vh(self): parser = SvgParser() parser.animation = MagicMock() parser.animation.width = 512 parser.animation.height = 256 self.assertAlmostEqual(parser._parse_unit("1vh"), 2.56) self.assertAlmostEqual(parser._parse_unit("1.5vh"), 2.56*1.5)
def test_vw(self): parser = SvgParser() parser.animation = MagicMock() parser.animation.width = 512 parser.animation.height = 256 self.assertAlmostEqual(parser._parse_unit("1vw"), 5.12) self.assertAlmostEqual(parser._parse_unit("1.5vw"), 5.12*1.5)
def test_style(self): parser = SvgParser() et = ElementTree.Element("g") et.attrib["style"] = "fill: red; stroke:green;stroke-width:3px" self.assertDictEqual( parser.parse_style(et, {}), { "fill": "red", "stroke": "green", "stroke-width": "3px", } )
def test_inkscape_center(self): parser = SvgParser() element = ElementTree.Element("g") group = objects.Rect(NVector(100, 200), NVector(300, 400)) dest_transform = objects.Transform() element.attrib[parser.qualified("inkscape", "transform-center-x")] = 0 element.attrib[parser.qualified("inkscape", "transform-center-y")] = 0 parser.parse_transform(element, group, dest_transform) self.assert_transform( dest_transform, anchor_point=NVector(100, 200), position=NVector(100, 200), scale=NVector(100, 100), rotation=0, skew_axis=0, skew=0, ) element.attrib[parser.qualified("inkscape", "transform-center-x")] = 20 element.attrib[parser.qualified("inkscape", "transform-center-y")] = -30 parser.parse_transform(element, group, dest_transform) self.assert_transform( dest_transform, anchor_point=NVector(120, 230), position=NVector(120, 230), scale=NVector(100, 100), rotation=0, skew_axis=0, skew=0, )
def test_inherit(self): parser = SvgParser() et = ElementTree.Element("g") et.attrib["style"] = "stroke:green;stroke-width:3px" base = { "fill": "red" } self.assertDictEqual( parser.parse_style(et, base), { "fill": "red", "stroke": "green", "stroke-width": "3px", } ) self.assertDictEqual(base, { "fill": "red" })
def test_multi_trans(self): parser = SvgParser() element = ElementTree.Element("g") group = objects.Rect(NVector(100, 200), NVector(300, 400)) dest_transform = objects.Transform() element.attrib["transform"] = "scale(0.7, 0.5) rotate(45) translate(12, 34)" parser.parse_transform(element, group, dest_transform) self.assert_transform( dest_transform, anchor_point=NVector(0, 0), position=NVector(12, 34), scale=NVector(70, 50), rotation=45, skew_axis=0, skew=0, )
def test_naked(self): parser = SvgParser() parser.dpi = 96 self.assertEqual(parser._parse_unit(123), 123) self.assertEqual(parser._parse_unit(12.3), 12.3) self.assertEqual(parser._parse_unit("123"), 123) self.assertEqual(parser._parse_unit("12.3"), 12.3)
def test_Q(self): parser = SvgParser() parser.dpi = 96 self.assertAlmostEqual(parser._parse_unit("1Q"), 96/25.4/4) self.assertAlmostEqual(parser._parse_unit("1.5Q"), 96*1.5/25.4/4) parser.dpi = 100 self.assertAlmostEqual(parser._parse_unit("1Q"), 100/25.4/4)
def test_cm(self): parser = SvgParser() parser.dpi = 96 self.assertAlmostEqual(parser._parse_unit("1cm"), 96/2.54) self.assertAlmostEqual(parser._parse_unit("1.5cm"), 96*1.5/2.54) parser.dpi = 100 self.assertAlmostEqual(parser._parse_unit("1cm"), 100/2.54)
def test_pt(self): parser = SvgParser() parser.dpi = 96 self.assertAlmostEqual(parser._parse_unit("1pt"), 96/72) self.assertAlmostEqual(parser._parse_unit("1.5pt"), 96*1.5/72) parser.dpi = 100 self.assertAlmostEqual(parser._parse_unit("1pt"), 100/72)
def test_pc(self): parser = SvgParser() parser.dpi = 96 self.assertAlmostEqual(parser._parse_unit("1pc"), 96/6) self.assertAlmostEqual(parser._parse_unit("1.5pc"), 96*1.5/6) parser.dpi = 100 self.assertAlmostEqual(parser._parse_unit("1pc"), 100/6)
def test_in(self): parser = SvgParser() parser.dpi = 96 self.assertEqual(parser._parse_unit("1in"), 96) self.assertEqual(parser._parse_unit("1.5in"), 96*1.5) parser.dpi = 100 self.assertEqual(parser._parse_unit("1in"), 100)
def test_matrix(self): parser = SvgParser() element = ElementTree.Element("g") group = objects.Rect(NVector(100, 200), NVector(300, 400)) dest_transform = objects.Transform() m = TransformMatrix() m.scale(0.7, 0.5) m.rotate(-math.pi/4) m.translate(12, 34) element.attrib["transform"] = m.to_css_2d() parser.parse_transform(element, group, dest_transform) self.assert_transform( dest_transform, anchor_point=NVector(0, 0), position=NVector(12, 34), scale=NVector(70, 50), rotation=45, skew_axis=0, skew=0, )
def test_apply_common_style(self): parser = SvgParser() transform = MagicMock() parser.apply_common_style({"fill": "red"}, transform) self.assertEqual(transform.opacity.value, 100) parser.apply_common_style({"fill": "red", "opacity": "0.7"}, transform) self.assertEqual(transform.opacity.value, 70)
def test_apply_visibility(self): parser = SvgParser() object = MagicMock() object.hidden = False parser.apply_visibility({"fill": "red"}, object) self.assertFalse(object.hidden) parser.apply_visibility({"fill": "red", "display": "block"}, object) self.assertFalse(object.hidden) parser.apply_visibility({"fill": "red", "display": "inline"}, object) self.assertFalse(object.hidden) parser.apply_visibility({"fill": "red", "visibility": "visible"}, object) self.assertFalse(object.hidden) parser.apply_visibility({"fill": "red", "display": "none"}, object) self.assertTrue(object.hidden) parser.apply_visibility({"fill": "red", "visibility": "hidden"}, object) self.assertTrue(object.hidden)
def test_px(self): parser = SvgParser() parser.dpi = 96 self.assertEqual(parser._parse_unit("123px"), 123) self.assertEqual(parser._parse_unit("12.3px"), 12.3)