aboutsummaryrefslogtreecommitdiff
path: root/tools/svg2skin.py
blob: 3aed1a0ea1e780c99e59cb85298ee3f86e804a74 (plain)
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/python
# don't expect too much.
# this is a really simple&stupid svg parser, which will use rectangles 
# and text fields to produce <widget> snippets for a skin.
# use object "id" fields for source names if you want.
# extracting font information is buggy.
# if you want text fields, please use flow text regions, instead of simple
# text. otherwise, width and height are unknown.
#
# tested only with a single inkscape-generated SVG.

import sys
import os
import string
from xml.sax import make_parser
from xml.sax.handler import ContentHandler

def getattrs(attrs, *a):
	res = []
	for x in a:
		res.append(float(attrs[x]))
	return res

def parsedict(attrs):
	if not attrs:
		return []
	d = attrs.split(';')
	r = { }
	for x in d:
		(key, val) = x.split(':')
		r[key] = val
	return r

def px(x):
	return int(float(x[:-2]) + .5)

def contains(box_o, box_i):
	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]

class parseXML(ContentHandler):
	def __init__(self):
		self.isPointsElement, self.isReboundsElement = 0, 0
		self.bbox = None
		self.find_bbox = False
		self.flow = None

	def startElement(self, name, attrs):
		if self.find_bbox:
			if name != "rect":
				return
			box = getattrs(attrs, "x", "y", "width", "height")
			if not self.bbox or contains(box, self.bbox):
				self.bbox = box
			return

		if name == "rect":
			(x, y, width, height) = getattrs(attrs, "x", "y", "width", "height")
			x -= self.bbox[0]
			y -= self.bbox[1]
			id = attrs["id"]
			if self.flow:
				id = self.flow
				self.flow = None
			styles = parsedict(attrs.get("style", ""))
		elif name == "text":
			(x, y) = getattrs(attrs, "x", "y")
			x -= self.bbox[0]
			y -= self.bbox[1]
			width, height = 0, 0
			styles = parsedict(attrs["style"])
			id = attrs["id"]
		elif name == "flowRoot":
			self.flow = attrs["id"]
			return
		else:
			return

		if "font-size" in styles:
			font = ' font="Regular;%d"' % px(styles["font-size"])
		else:
			font = ""
		print """\t\t<widget source="%s" render="Label" position="%d,%d" size="%d,%d" %s />""" % (id, x, y, width, height, font)

parser = make_parser()
contentHandler = parseXML()
parser.setContentHandler(contentHandler)
contentHandler.find_bbox = True
parser.parse(sys.argv[1])
bboxi = tuple([int(x) for x in contentHandler.bbox])
contentHandler.find_bbox = False
print '\t<screen name="" position="%d,%d" size="%d,%d" title="">' % bboxi
parser.parse(sys.argv[1])
print '\t</screen>'