Example #1
0
    def test_transform_list_replace_item(self):
        transform_list = SVGTransformList.parse('translate(50,30) rotate(30)')
        t = SVGTransform(SVGTransform.SVG_TRANSFORM_SCALE, 1.5)
        o = transform_list.replace_item(t, 0)
        # -> "scale(...) rotate(...)"
        self.assertEqual(t, o)
        types = [x.type for x in transform_list]
        self.assertEqual([
            SVGTransform.SVG_TRANSFORM_SCALE, SVGTransform.SVG_TRANSFORM_ROTATE
        ], types)

        transform_list = SVGTransformList.parse('translate(50,30) rotate(30)')
        t = SVGTransform(SVGTransform.SVG_TRANSFORM_SCALE, 1.5)
        o = transform_list.replace_item(t, 1)
        # -> "translate(...) scale(...)"
        self.assertEqual(t, o)
        types = [x.type for x in transform_list]
        self.assertEqual([
            SVGTransform.SVG_TRANSFORM_TRANSLATE,
            SVGTransform.SVG_TRANSFORM_SCALE
        ], types)

        transform_list = SVGTransformList.parse('translate(50,30) rotate(30)')
        t = SVGTransform(SVGTransform.SVG_TRANSFORM_SCALE, 1.5)
        self.assertRaises(IndexError,
                          lambda: transform_list.replace_item(t, 2))

        self.assertRaises(TypeError,
                          lambda: transform_list.replace_item('scale(1.5)', 0))
Example #2
0
 def test_transform_list_parse02(self):
     t = ' translate ( 1500e-2 .5e+2 ) scale ( .5,.25 ) rotate ( 45.0.5.7 ) '
     transform_list = SVGTransformList.parse(t)
     self.assertEqual(3, len(transform_list))
     self.assertEqual('translate(15, 50)', transform_list[0].tostring())
     self.assertEqual('scale(0.5, 0.25)', transform_list[1].tostring())
     self.assertEqual('rotate(45, 0.5, 0.7)', transform_list[2].tostring())
Example #3
0
 def test_transform_list_parse03(self):
     t = "\ttranslate\t(\t1500e-2\t.5e+2\t)\nscale\t(\t.5,.25\t)\n" \
         "rotate\t(\t45.0.5.7\t)\n"
     transform_list = SVGTransformList.parse(t)
     self.assertEqual(3, len(transform_list))
     self.assertEqual('translate(15, 50)', transform_list[0].tostring())
     self.assertEqual('scale(0.5, 0.25)', transform_list[1].tostring())
     self.assertEqual('rotate(45, 0.5, 0.7)', transform_list[2].tostring())
Example #4
0
    def test_transform_list_initialize(self):
        transform_list = SVGTransformList.parse('translate(50,30) rotate(30)')
        self.assertEqual(2, len(transform_list))

        transform = SVGTransform(SVGTransform.SVG_TRANSFORM_SKEWX, 45)
        o = transform_list.initialize(transform)
        self.assertEqual(1, len(transform_list))
        self.assertEqual(transform, o)

        self.assertRaises(TypeError,
                          lambda: transform_list.initialize('skewX(45)', 0))
Example #5
0
    def test_transform_list_get_item(self):
        transform_list = SVGTransformList.parse('translate(50,30) rotate(30)')

        t = transform_list.get_item(0)
        self.assertEqual(SVGTransform.SVG_TRANSFORM_TRANSLATE, t.type)

        t = transform_list.get_item(1)
        self.assertEqual(SVGTransform.SVG_TRANSFORM_ROTATE, t.type)

        t = transform_list.get_item(-1)
        self.assertEqual(SVGTransform.SVG_TRANSFORM_ROTATE, t.type)

        # t = transform_list.get_item(2)
        self.assertRaises(IndexError, lambda: transform_list.get_item(2))
Example #6
0
    def test_transform_list_consolidate(self):
        # See also: RotateScale.html
        transform_list = SVGTransformList.parse('translate(50,30) rotate(30)')
        self.assertEqual(2, len(transform_list))

        t = transform_list.consolidate()
        self.assertEqual(1, len(transform_list))
        self.assertEqual(transform_list[0], t)
        m = DOMMatrix([
            0.8660254037844387, 0.49999999999999994, -0.49999999999999994,
            0.8660254037844387, 50, 30
        ])
        self.assertEqual(SVGTransform.SVG_TRANSFORM_MATRIX, t.type)
        self.assertTrue(t.matrix == m)
Example #7
0
    def test_transform_list_parse01(self):
        formatter.precision = 4

        t = "matrix(0.88889, 2.33334, 3.55555, 4.78787, 5.00004, 6.00006)" \
            " rotate(25.11155 0.0 0.0) scale(2.22222 2.22222)" \
            " skewX(-36.11224) skewY(180.55564)" \
            " translate(-100.44444 0.0)"
        transform_list = SVGTransformList.parse(t)
        self.assertEqual(6, len(transform_list))
        self.assertEqual('matrix(0.8889, 2.3333, 3.5556, 4.7879, 5, 6.0001)',
                         transform_list[0].tostring())
        self.assertEqual('rotate(25.1116)', transform_list[1].tostring())
        self.assertEqual('scale(2.2222)', transform_list[2].tostring())
        self.assertEqual('skewX(-36.1122)', transform_list[3].tostring())
        self.assertEqual('skewY(180.5556)', transform_list[4].tostring())
        self.assertEqual('translate(-100.4444)', transform_list[5].tostring())
Example #8
0
def add_group(document, tx, ty, text_content, transform=None, rotate=None):
    root = document.document_element

    group = document.create_element_ns(Element.SVG_NAMESPACE_URI, 'g')
    root.append(group)
    css_style = group.style
    css_style['font-family'] = 'DejaVu Sans'
    css_style['font-size'] = '36'
    transform_list = SVGTransformList([
        SVGTransform(SVGTransform.SVG_TRANSFORM_TRANSLATE, tx, ty),
    ])
    group.transform = transform_list

    rect = document.create_element_ns(Element.SVG_NAMESPACE_URI, 'rect')
    group.append(rect)
    css_style = rect.style
    css_style['fill'] = 'none'
    css_style['stroke'] = 'red'
    css_style['stroke-width'] = '1'
    css_style['stroke-dasharray'] = '2'

    circle = document.create_element_ns(Element.SVG_NAMESPACE_URI, 'circle')
    group.append(circle)
    css_style = circle.style
    css_style['fill'] = 'red'
    circle.attributes['r'] = '2'

    text = document.create_element_ns(Element.SVG_NAMESPACE_URI, 'text')
    group.append(text)
    css_style = text.style
    css_style['fill'] = 'skyblue'
    text.text_content = text_content
    if transform is not None:
        text.attributes['transform'] = transform
    if rotate is not None:
        text.attributes['rotate'] = rotate

    legend = document.create_element_ns(Element.SVG_NAMESPACE_URI, 'text')
    group.append(legend)
    css_style = legend.style
    css_style['font-size'] = '12'
    legend.text_content = 'transform: "{}", rotate: "{}"'.format(
        transform if transform is not None else '',
        rotate if rotate is not None else '')

    path = document.create_element_ns(Element.SVG_NAMESPACE_URI, 'path')
    group.append(path)
    css_style = path.style
    css_style['fill'] = 'none'
    css_style['stroke'] = 'blue'
    css_style['stroke-width'] = '1'
    path_data = text.get_path_data()
    if transform is not None:
        transform_list = SVGTransformList.parse(transform)
        matrix = transform_list.matrix
        path_data = PathParser.transform(path_data, matrix)
    path.set_path_data(path_data)

    bbox = path.get_bbox()
    rect.attributes['x'] = str(bbox.x)
    rect.attributes['y'] = str(bbox.y)
    rect.attributes['width'] = str(bbox.width)
    rect.attributes['height'] = str(bbox.height)
    legend.attributes['x'] = str(bbox.right + 10)

    return bbox
Example #9
0
    def test_transform_list_clear(self):
        transform_list = SVGTransformList.parse('translate(50,30) rotate(30)')
        self.assertEqual(2, len(transform_list))

        transform_list.clear()
        self.assertEqual(0, len(transform_list))