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.
10 # tested only with a single inkscape-generated SVG.
15 from xml.sax import make_parser
16 from xml.sax.handler import ContentHandler
18 def getattrs(attrs, *a):
21 res.append(float(attrs[x]))
30 (key, val) = x.split(':')
35 return int(float(x[:-2]) + .5)
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]
40 class parseXML(ContentHandler):
42 self.isPointsElement, self.isReboundsElement = 0, 0
44 self.find_bbox = False
47 def startElement(self, name, attrs):
51 box = getattrs(attrs, "x", "y", "width", "height")
52 if not self.bbox or contains(box, self.bbox):
57 (x, y, width, height) = getattrs(attrs, "x", "y", "width", "height")
64 styles = parsedict(attrs.get("style", ""))
66 (x, y) = getattrs(attrs, "x", "y")
70 styles = parsedict(attrs["style"])
72 elif name == "flowRoot":
73 self.flow = attrs["id"]
78 if "font-size" in styles:
79 font = ' font="Regular;%d"' % px(styles["font-size"])
82 print """\t\t<widget source="%s" render="Label" position="%d,%d" size="%d,%d" %s />""" % (id, x, y, width, height, font)
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])