File: svg.py

package info (click to toggle)
python-leather 0.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 652 kB
  • sloc: python: 2,385; makefile: 117; sh: 5
file content (39 lines) | stat: -rw-r--r-- 834 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
"""
Helpers for working with SVG.
"""

import xml.etree.ElementTree as ET

HEADER = '<?xml version="1.0" standalone="no"?>\n' + \
    '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"\n' + \
    '"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">\n'


def stringify(root):
    """
    Convert an SVG XML tree to a unicode string.
    """
    return ET.tostring(root, encoding='unicode')


def save(f, root):
    """
    Save an SVG XML tree to a file.
    """
    f.write(HEADER)
    f.write(stringify(root))


def translate(x, y):
    """
    Generate an SVG transform statement representing a simple translation.
    """
    return 'translate(%i %i)' % (x, y)


def rotate(deg, x, y):
    """
    Generate an SVG transform statement representing rotation around a given
    point.
    """
    return 'rotate(%i %i %i)' % (deg, x, y)