po/de.po: fix some german translations (by satman)
[enigma2.git] / tools / svg2skin.py
1 #!/usr/bin/python
2 # don't expect too much.
3 # this is a really simple&stupid svg parser, which will use rectangles 
4 # and text fields to produce <widget> snippets for a skin.
5 # use object "id" fields for source names if you want.
6 # extracting font information is buggy.
7 # if you want text fields, please use flow text regions, instead of simple
8 # text. otherwise, width and height are unknown.
9 #
10 # tested only with a single inkscape-generated SVG.
11
12 import sys
13 import os
14 import string
15 from xml.sax import make_parser
16 from xml.sax.handler import ContentHandler
17
18 def getattrs(attrs, *a):
19         res = []
20         for x in a:
21                 res.append(float(attrs[x]))
22         return res
23
24 def parsedict(attrs):
25         if not attrs:
26                 return []
27         d = attrs.split(';')
28         r = { }
29         for x in d:
30                 (key, val) = x.split(':')
31                 r[key] = val
32         return r
33
34 def px(x):
35         return int(float(x[:-2]) + .5)
36
37 def contains(box_o, box_i):
38         return box_o[0] <= box_i[0] and box_o[1] <= box_i[1] and box_o[2] >= box_i[2] and box_o[3] >= box_i[3]
39
40 class parseXML(ContentHandler):
41         def __init__(self):
42                 self.isPointsElement, self.isReboundsElement = 0, 0
43                 self.bbox = None
44                 self.find_bbox = False
45                 self.flow = None
46
47         def startElement(self, name, attrs):
48                 if self.find_bbox:
49                         if name != "rect":
50                                 return
51                         box = getattrs(attrs, "x", "y", "width", "height")
52                         if not self.bbox or contains(box, self.bbox):
53                                 self.bbox = box
54                         return
55
56                 if name == "rect":
57                         (x, y, width, height) = getattrs(attrs, "x", "y", "width", "height")
58                         x -= self.bbox[0]
59                         y -= self.bbox[1]
60                         id = attrs["id"]
61                         if self.flow:
62                                 id = self.flow
63                                 self.flow = None
64                         styles = parsedict(attrs.get("style", ""))
65                 elif name == "text":
66                         (x, y) = getattrs(attrs, "x", "y")
67                         x -= self.bbox[0]
68                         y -= self.bbox[1]
69                         width, height = 0, 0
70                         styles = parsedict(attrs["style"])
71                         id = attrs["id"]
72                 elif name == "flowRoot":
73                         self.flow = attrs["id"]
74                         return
75                 else:
76                         return
77
78                 if "font-size" in styles:
79                         font = ' font="Regular;%d"' % px(styles["font-size"])
80                 else:
81                         font = ""
82                 print """\t\t<widget source="%s" render="Label" position="%d,%d" size="%d,%d" %s />""" % (id, x, y, width, height, font)
83
84 parser = make_parser()
85 contentHandler = parseXML()
86 parser.setContentHandler(contentHandler)
87 contentHandler.find_bbox = True
88 parser.parse(sys.argv[1])
89 bboxi = tuple([int(x) for x in contentHandler.bbox])
90 contentHandler.find_bbox = False
91 print '\t<screen name="" position="%d,%d" size="%d,%d" title="">' % bboxi
92 parser.parse(sys.argv[1])
93 print '\t</screen>'