3 import xml.etree.ElementTree as ET
8 RECT_TAG_NAME = '{http://www.w3.org/2000/svg}rect'
9 TEXT_TAG_NAME = '{http://www.w3.org/2000/svg}text'
12 def list_as_sexpr(xs):
13 return "'(" + ' '.join(map(lambda x: '"' + x + '"', xs)) + ')'
16 def color_from_style(style):
17 m = re.match(".*fill:#([0-9a-z]{6}).*", style)
21 def svg_rects(svg_root):
22 return [rect for rect in svg_root.iter(RECT_TAG_NAME)]
25 def svg_texts(svg_root):
26 return [rect for rect in svg_root.iter(TEXT_TAG_NAME)]
29 def save_background(svg_root, output_file):
31 for rect in svg_rects(svg_root)
32 if rect.attrib['id'] == 'background']
33 color = color_from_style(background.attrib['style'])
34 output_file.write("%s\n" % (color))
37 def save_player(svg_root, output_file):
39 for rect in svg_rects(svg_root)
40 if rect.attrib['id'] == 'player']
41 color = color_from_style(player.attrib['style'])
42 x = player.attrib['x']
43 y = player.attrib['y']
44 output_file.write("%s %s %s\n" % (x, y, color))
47 def save_platforms(svg_root, output_file):
49 for rect in svg_rects(svg_root)
50 if rect.attrib['id'].startswith("rect")]
52 output_file.write("%d\n" % (len(platforms)))
53 for platform in platforms:
54 x = platform.attrib['x']
55 y = platform.attrib['y']
56 w = platform.attrib['width']
57 h = platform.attrib['height']
58 color = color_from_style(platform.attrib['style'])
59 output_file.write("%s %s %s %s %s\n" % (x, y, w, h, color))
62 def save_goals(svg_root, output_file):
64 for rect in svg_rects(svg_root)
65 if rect.attrib['id'].startswith("goal")]
67 output_file.write("%d\n" % (len(goals)))
70 goal_id = goal.attrib['id'][len('goal'):]
71 record = (goal.attrib['id'], goal.attrib['x'], goal.attrib['y'],
72 color_from_style(goal.attrib['style']))
73 output_file.write("%s %s %s %s\n" % record)
76 def save_lavas(svg_root, output_file):
78 for rect in svg_rects(svg_root)
79 if rect.attrib['id'].startswith('lava')]
81 output_file.write("%d\n" % (len(lavas)))
86 w = lava.attrib['width']
87 h = lava.attrib['height']
88 color = color_from_style(lava.attrib['style'])
89 output_file.write("%s %s %s %s %s\n" % (x, y, w, h, color))
92 def save_backplatforms(svg_root, output_file):
94 for rect in svg_rects(svg_root)
95 if rect.attrib['id'].startswith("backrect")]
97 output_file.write("%d\n" % (len(platforms)))
98 for platform in platforms:
99 x = platform.attrib['x']
100 y = platform.attrib['y']
101 w = platform.attrib['width']
102 h = platform.attrib['height']
103 color = color_from_style(platform.attrib['style'])
104 output_file.write("%s %s %s %s %s\n" % (x, y, w, h, color))
107 def save_boxes(svg_root, output_file):
109 for rect in svg_rects(svg_root)
110 if rect.attrib['id'].startswith("box")]
112 output_file.write("%d\n" % (len(boxes)))
114 box_id = box.attrib['id']
117 w = box.attrib['width']
118 h = box.attrib['height']
119 color = color_from_style(box.attrib['style'])
120 output_file.write("%s %s %s %s %s %s\n" % (box_id, x, y, w, h, color))
123 def save_labels(svg_root, output_file):
125 for text in svg_texts(svg_root)
126 if text.attrib['id'].startswith('label')]
128 output_file.write("%d\n" % (len(labels)))
130 x = label.attrib['x']
131 y = label.attrib['y']
132 color = color_from_style(label.attrib['style'])
133 # TODO(#432): svg2rects doesn't handle newlines in labels
134 text = ' '.join([tspan.text for tspan in label])
135 output_file.write("%s %s %s\n" % (x, y, color))
136 output_file.write("%s\n" % (text))
139 def save_scripts(svg_root, output_file):
141 for rect in svg_rects(svg_root)
142 if rect.attrib['id'].startswith('script')]
144 output_file.write("%d\n" % (len(scripts)))
145 for script in scripts:
146 x = script.attrib['x']
147 y = script.attrib['y']
148 w = script.attrib['width']
149 h = script.attrib['height']
150 color = color_from_style(script.attrib['style'])
151 output_file.write("%s %s %s %s %s\n" % (x, y, w, h, color))
154 command_line = title.text.split()
155 with open(command_line[0], 'r') as script_file:
156 script_lines = script_file.read().splitlines()
157 output_file.write("%d\n" % (len(script_lines) + 1))
158 output_file.write("(set args %s)\n" % list_as_sexpr(command_line[1:]))
159 for script_line in script_lines:
160 output_file.write("%s\n" % script_line)
162 def svg2rects(svg_file_name, output_file_name):
163 svg_tree = ET.parse(svg_file_name)
164 svg_root = svg_tree.getroot()
166 with open(output_file_name, "w") as output_file:
167 save_background(svg_root, output_file)
168 save_player(svg_root, output_file)
169 save_platforms(svg_root, output_file)
170 save_goals(svg_root, output_file)
171 save_lavas(svg_root, output_file)
172 save_backplatforms(svg_root, output_file)
173 save_boxes(svg_root, output_file)
174 save_labels(svg_root, output_file)
175 save_scripts(svg_root, output_file)
178 print("Usage: svg2rects.py <svg-file-name> <output-file-name>")
181 if __name__ == '__main__':
182 if len(sys.argv) < 3:
186 svg2rects(sys.argv[1], sys.argv[2])