]> git.lizzy.rs Git - nothing.git/blob - devtools/svg2rects.py
Add TODO(#656)
[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 from svg.tags import *
7 from svg.selectors import *
8
9 def list_as_sexpr(xs):
10     return "'(" + ' '.join(map(lambda x: '"' + x + '"', xs)) + ')'
11
12 def color_from_style(style):
13     m = re.match(".*fill:#([0-9a-z]{6}).*", style)
14     return m.group(1)
15
16 def save_background(svg_root, output_file):
17     [background] = [rect
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))
22
23
24 def save_player(svg_root, output_file):
25     [player] = [rect
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)
33
34
35 def save_platforms(svg_root, output_file):
36     platforms = [rect
37                  for rect in svg_rects(svg_root)
38                  if rect.attrib['id'].startswith("rect")]
39
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))
48
49
50 def save_goals(svg_root, output_file):
51     goals = [rect
52              for rect in svg_rects(svg_root)
53              if rect.attrib['id'].startswith("goal")]
54
55     output_file.write("%d\n" % (len(goals)))
56
57     for goal in 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)
62
63
64 def save_lavas(svg_root, output_file):
65     lavas = [rect
66              for rect in svg_rects(svg_root)
67              if rect.attrib['id'].startswith('lava')]
68
69     output_file.write("%d\n" % (len(lavas)))
70
71     for lava in lavas:
72         x = lava.attrib['x']
73         y = lava.attrib['y']
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))
78
79
80 def save_backplatforms(svg_root, output_file):
81     platforms = [rect
82                  for rect in svg_rects(svg_root)
83                  if rect.attrib['id'].startswith("backrect")]
84
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))
93
94
95 def save_boxes(svg_root, output_file):
96     boxes = [rect
97              for rect in svg_rects(svg_root)
98              if rect.attrib['id'].startswith("box")]
99
100     output_file.write("%d\n" % (len(boxes)))
101     for box in boxes:
102         box_id = box.attrib['id']
103         x = box.attrib['x']
104         y = box.attrib['y']
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))
109
110
111 def save_labels(svg_root, output_file):
112     labels = [text
113               for text in svg_texts(svg_root)
114               if text.attrib['id'].startswith('label')]
115
116     output_file.write("%d\n" % (len(labels)))
117     for label in 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))
126
127
128 def save_script(script, output_file):
129     for title in script:
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)
137
138
139 def save_script_regions(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         save_script(script, output_file)
153
154
155 def svg2rects(svg_file_name, output_file_name):
156     svg_tree = ET.parse(svg_file_name)
157     svg_root = svg_tree.getroot()
158
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)
169
170 def usage():
171     print("Usage: svg2rects.py <svg-file-name> <output-file-name>")
172
173
174 if __name__ == '__main__':
175     if len(sys.argv) < 3:
176         usage()
177         exit(1)
178
179     svg2rects(sys.argv[1], sys.argv[2])