]> git.lizzy.rs Git - nothing.git/blob - devtools/svg2rects.py
Merge pull request #516 from tsoding/494
[nothing.git] / devtools / svg2rects.py
1 #!/usr/bin/env python3
2
3 import xml.etree.ElementTree as ET
4 import sys
5 import re
6
7
8 RECT_TAG_NAME = '{http://www.w3.org/2000/svg}rect'
9 TEXT_TAG_NAME = '{http://www.w3.org/2000/svg}text'
10
11
12 def list_as_sexpr(xs):
13     return "'(" + ' '.join(map(lambda x: '"' + x + '"', xs)) + ')'
14
15
16 def color_from_style(style):
17     m = re.match(".*fill:#([0-9a-z]{6}).*", style)
18     return m.group(1)
19
20
21 def svg_rects(svg_root):
22     return [rect for rect in svg_root.iter(RECT_TAG_NAME)]
23
24
25 def svg_texts(svg_root):
26     return [rect for rect in svg_root.iter(TEXT_TAG_NAME)]
27
28
29 def save_background(svg_root, output_file):
30     [background] = [rect
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))
35
36
37 def save_player(svg_root, output_file):
38     [player] = [rect
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))
45
46
47 def save_platforms(svg_root, output_file):
48     platforms = [rect
49                  for rect in svg_rects(svg_root)
50                  if rect.attrib['id'].startswith("rect")]
51
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))
60
61
62 def save_goals(svg_root, output_file):
63     goals = [rect
64              for rect in svg_rects(svg_root)
65              if rect.attrib['id'].startswith("goal")]
66
67     output_file.write("%d\n" % (len(goals)))
68
69     for goal in 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)
74
75
76 def save_lavas(svg_root, output_file):
77     lavas = [rect
78              for rect in svg_rects(svg_root)
79              if rect.attrib['id'].startswith('lava')]
80
81     output_file.write("%d\n" % (len(lavas)))
82
83     for lava in lavas:
84         x = lava.attrib['x']
85         y = lava.attrib['y']
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))
90
91
92 def save_backplatforms(svg_root, output_file):
93     platforms = [rect
94                  for rect in svg_rects(svg_root)
95                  if rect.attrib['id'].startswith("backrect")]
96
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))
105
106
107 def save_boxes(svg_root, output_file):
108     boxes = [rect
109              for rect in svg_rects(svg_root)
110              if rect.attrib['id'].startswith("box")]
111
112     output_file.write("%d\n" % (len(boxes)))
113     for box in boxes:
114         box_id = box.attrib['id']
115         x = box.attrib['x']
116         y = box.attrib['y']
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))
121
122
123 def save_labels(svg_root, output_file):
124     labels = [text
125               for text in svg_texts(svg_root)
126               if text.attrib['id'].startswith('label')]
127
128     output_file.write("%d\n" % (len(labels)))
129     for label in 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))
137
138
139 def save_scripts(svg_root, output_file):
140     scripts = [rect
141                for rect in svg_rects(svg_root)
142                if rect.attrib['id'].startswith('script')]
143
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         # TODO(#509): script may have more than one child
153         [title] = [child for child in script]
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)
161
162 def svg2rects(svg_file_name, output_file_name):
163     svg_tree = ET.parse(svg_file_name)
164     svg_root = svg_tree.getroot()
165
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)
176
177 def usage():
178     print("Usage: svg2rects.py <svg-file-name> <output-file-name>")
179
180
181 if __name__ == '__main__':
182     if len(sys.argv) < 3:
183         usage()
184         exit(1)
185
186     svg2rects(sys.argv[1], sys.argv[2])