3 import xml.etree.ElementTree as ET
7 from svg.selectors import *
10 return "'(" + ' '.join(map(lambda x: '"' + x + '"', xs)) + ')'
12 def color_from_style(style):
13 m = re.match(".*fill:#([0-9a-z]{6}).*", style)
16 def save_background(svg_root, output_file):
18 for rect in svg_rects(svg_root)
19 if rect.attrib['id'] == 'background']
20 color = color_from_style(background.attrib['style'])
21 output_file.write("%s\n" % (color))
24 def save_player(svg_root, output_file):
26 for rect in svg_rects(svg_root)
27 if rect.attrib['id'] == 'player']
28 color = color_from_style(player.attrib['style'])
29 x = player.attrib['x']
30 y = player.attrib['y']
31 output_file.write("%s %s %s\n" % (x, y, color))
32 save_script(player, output_file)
35 def save_platforms(svg_root, output_file):
37 for rect in svg_rects(svg_root)
38 if rect.attrib['id'].startswith("rect")]
40 output_file.write("%d\n" % (len(platforms)))
41 for platform in platforms:
42 x = platform.attrib['x']
43 y = platform.attrib['y']
44 w = platform.attrib['width']
45 h = platform.attrib['height']
46 color = color_from_style(platform.attrib['style'])
47 output_file.write("%s %s %s %s %s\n" % (x, y, w, h, color))
50 def save_goals(svg_root, output_file):
52 for rect in svg_rects(svg_root)
53 if rect.attrib['id'].startswith("goal")]
55 output_file.write("%d\n" % (len(goals)))
58 goal_id = goal.attrib['id'][len('goal'):]
59 record = (goal.attrib['id'], goal.attrib['x'], goal.attrib['y'],
60 color_from_style(goal.attrib['style']))
61 output_file.write("%s %s %s %s\n" % record)
64 def save_lavas(svg_root, output_file):
66 for rect in svg_rects(svg_root)
67 if rect.attrib['id'].startswith('lava')]
69 output_file.write("%d\n" % (len(lavas)))
74 w = lava.attrib['width']
75 h = lava.attrib['height']
76 color = color_from_style(lava.attrib['style'])
77 output_file.write("%s %s %s %s %s\n" % (x, y, w, h, color))
80 def save_backplatforms(svg_root, output_file):
82 for rect in svg_rects(svg_root)
83 if rect.attrib['id'].startswith("backrect")]
85 output_file.write("%d\n" % (len(platforms)))
86 for platform in platforms:
87 x = platform.attrib['x']
88 y = platform.attrib['y']
89 w = platform.attrib['width']
90 h = platform.attrib['height']
91 color = color_from_style(platform.attrib['style'])
92 output_file.write("%s %s %s %s %s\n" % (x, y, w, h, color))
95 def save_boxes(svg_root, output_file):
97 for rect in svg_rects(svg_root)
98 if rect.attrib['id'].startswith("box")]
100 output_file.write("%d\n" % (len(boxes)))
102 box_id = box.attrib['id']
105 w = box.attrib['width']
106 h = box.attrib['height']
107 color = color_from_style(box.attrib['style'])
108 output_file.write("%s %s %s %s %s %s\n" % (box_id, x, y, w, h, color))
111 def save_labels(svg_root, output_file):
113 for text in svg_texts(svg_root)
114 if text.attrib['id'].startswith('label')]
116 output_file.write("%d\n" % (len(labels)))
118 label_id = label.attrib['id']
119 x = label.attrib['x']
120 y = label.attrib['y']
121 color = color_from_style(label.attrib['style'])
122 # TODO(#432): svg2rects doesn't handle newlines in labels
123 text = ' '.join([tspan.text for tspan in label])
124 output_file.write("%s %s %s %s\n" % (label_id, x, y, color))
125 output_file.write("%s\n" % (text))
128 def save_script(script, output_file):
130 command_line = title.text.split()
131 with open(command_line[0], 'r') as script_file:
132 script_lines = script_file.read().splitlines()
133 output_file.write("%d\n" % (len(script_lines) + 1))
134 output_file.write("(set args %s)\n" % list_as_sexpr(command_line[1:]))
135 for script_line in script_lines:
136 output_file.write("%s\n" % script_line)
139 def save_script_regions(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))
152 save_script(script, output_file)
155 def svg2rects(svg_file_name, output_file_name):
156 svg_tree = ET.parse(svg_file_name)
157 svg_root = svg_tree.getroot()
159 with open(output_file_name, "w") as output_file:
160 save_background(svg_root, output_file)
161 save_player(svg_root, output_file)
162 save_platforms(svg_root, output_file)
163 save_goals(svg_root, output_file)
164 save_lavas(svg_root, output_file)
165 save_backplatforms(svg_root, output_file)
166 save_boxes(svg_root, output_file)
167 save_labels(svg_root, output_file)
168 save_script_regions(svg_root, output_file)
171 print("Usage: svg2rects.py <svg-file-name> <output-file-name>")
174 if __name__ == '__main__':
175 if len(sys.argv) < 3:
179 svg2rects(sys.argv[1], sys.argv[2])