Esempio n. 1
0
    def test_transform_list_iadd(self):
        iterable = [
            SVGTransform(SVGTransform.SVG_TRANSFORM_TRANSLATE, 50, 30),
            SVGTransform(SVGTransform.SVG_TRANSFORM_ROTATE, 30),
        ]
        transform_list = SVGTransformList(iterable)

        iterable = [
            SVGTransform(SVGTransform.SVG_TRANSFORM_SKEWY, 30),
            SVGTransform(SVGTransform.SVG_TRANSFORM_TRANSLATE, 200, 30),
        ]
        transform_list += SVGTransformList(iterable)
        self.assertIsInstance(transform_list, SVGTransformList)
        self.assertEqual(4, len(transform_list))
        self.assertEqual(
            'translate(50, 30) rotate(30) skewY(30) translate(200, 30)',
            transform_list.tostring())

        iterable = [
            SVGTransform(SVGTransform.SVG_TRANSFORM_SKEWX, -30),
            SVGTransform(SVGTransform.SVG_TRANSFORM_ROTATE, -30),
        ]
        transform_list += iterable
        self.assertEqual(6, len(transform_list))

        iterable = (
            SVGTransform(SVGTransform.SVG_TRANSFORM_TRANSLATE, -50, -30),
            SVGTransform(SVGTransform.SVG_TRANSFORM_ROTATE, -30),
        )
        transform_list += iterable
        self.assertEqual(8, len(transform_list))
Esempio n. 2
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))
Esempio n. 3
0
    def test_transform_list_init(self):
        transform_list = SVGTransformList()
        expected = 0
        self.assertEqual(expected, len(transform_list))
        self.assertEqual(expected, transform_list.length)
        self.assertEqual(expected, transform_list.number_of_items)

        iterable = [
            SVGTransform(SVGTransform.SVG_TRANSFORM_TRANSLATE, 50, 30),
            SVGTransform(SVGTransform.SVG_TRANSFORM_ROTATE, 30),
        ]
        transform_list = SVGTransformList(iterable)
        expected = 2
        self.assertEqual(expected, len(transform_list))
        self.assertEqual(expected, transform_list.length)
        self.assertEqual(expected, transform_list.number_of_items)
        t = transform_list[0]
        self.assertEqual(SVGTransform.SVG_TRANSFORM_TRANSLATE, t.type)
        t = transform_list[1]
        self.assertEqual(SVGTransform.SVG_TRANSFORM_ROTATE, t.type)

        iterable = [
            'translate(50, 30)',
            'rotate(30)',
        ]
        self.assertRaises(TypeError, lambda: SVGTransformList(iterable))
Esempio n. 4
0
    def test_transform_list_remove_item(self):
        iterable = [
            SVGTransform(SVGTransform.SVG_TRANSFORM_TRANSLATE, 50, 30),
            SVGTransform(SVGTransform.SVG_TRANSFORM_ROTATE, 30),
        ]
        transform_list = SVGTransformList(iterable)
        i = 0
        t = transform_list[i]
        o = transform_list.remove_item(i)
        self.assertEqual(1, len(transform_list))
        self.assertTrue(o not in transform_list)
        self.assertEqual(t, o)
        self.assertEqual(SVGTransform.SVG_TRANSFORM_TRANSLATE, o.type)

        self.assertRaises(IndexError, lambda: transform_list.remove_item(1))
Esempio n. 5
0
    def test_transform_list_extend(self):
        transform_list = SVGTransformList()
        iterable = [
            SVGTransform(SVGTransform.SVG_TRANSFORM_TRANSLATE, 50, 30),
            SVGTransform(SVGTransform.SVG_TRANSFORM_ROTATE, 30),
        ]
        transform_list.extend(iterable)
        self.assertEqual(2, len(transform_list))

        iterable = [
            'translate(50, 30)',
            'rotate(30)',
        ]
        # transform_list.extend(iterable)
        self.assertRaises(TypeError, lambda: transform_list.extend(iterable))
Esempio n. 6
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())
Esempio n. 7
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())
Esempio n. 8
0
def _add_group(document, tx, ty, text_content, transform, path_data,
               font_family, font_size):
    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'] = font_family
    css_style['font-size'] = str(font_size)
    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 len(transform) > 0:
        text.attributes['transform'] = transform

    legend = document.create_element_ns(Element.SVG_NAMESPACE_URI, 'text')
    group.append(legend)
    css_style = legend.style
    css_style['font-size'] = str(font_size // 3)
    legend.text_content = 'transform: "{}"'.format(transform)

    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.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
Esempio n. 9
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))
Esempio n. 10
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))
Esempio n. 11
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)
Esempio n. 12
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())
Esempio n. 13
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
Esempio n. 14
0
 def test_transform_list_consolidate_none(self):
     transform_list = SVGTransformList()
     t = transform_list.consolidate()
     self.assertIsNone(t)
Esempio n. 15
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))