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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
|
#!/usr/bin/env python
import os
import os.path
import subprocess
import logging as log
import re
class Inkscape2Edc(object):
cmd = "inkscape --without-gui"
def __init__(self, infile, outfile, group,
relative1_x=None, relative2_x=None,
relative1_y=None, relative2_y=None,
images_dir="",
show_max=True, show_min=True, show_mouse_events=True):
self.infile = infile
self.outfile = outfile
self.group = group
self.relative1_x = relative1_x
self.relative2_x = relative2_x
self.relative1_y = relative1_y
self.relative2_y = relative2_y
self.images_dir = images_dir
self.show_max = show_max
self.show_min = show_min
self.show_mouse_events = show_mouse_events
self.images = {}
self.sizes = {}
self.known_ids = tuple()
self.w = 0
self.h = 0
self.out = open(self.outfile, "wb")
self.basedir = os.path.dirname(self.outfile)
def _exec_cmd(self, *args):
s = " ".join(args)
cmd = "%s --file=%r %s" % (self.cmd, self.infile, s)
try:
p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, shell=True)
except Exception, e:
log.error("cmd=%r exception: %s", cmd, e)
return ""
out, err = p.communicate()
if err:
log.error("cmd=%r error: %s", cmd, err)
return out
def load_sizes(self):
sizes = self._exec_cmd("--query-all").split('\n')
self.sizes = {}
order = []
for line in sizes:
if not line:
continue
try:
oid, x, y, w, h = line.split(',')
except ValueError:
log.warn("could not parse size line: %r", line)
continue
x = float(x)
y = float(y)
w = float(w)
h = float(h)
self.sizes[oid] = (x, y, w, h)
order.append(oid)
self.known_ids = tuple(order)
self.w = float(self._exec_cmd("--query-width"))
self.h = float(self._exec_cmd("--query-height"))
def output_file_header(self):
self.out.write("""\
collections {
group {
name: "%(group)s";
""" % self.__dict__)
if self.show_min:
self.out.write(" min: %(w)d %(h)d;\n" % self.__dict__)
if self.show_max:
self.out.write(" max: %(w)d %(h)d;\n" % self.__dict__)
def output_file_section_parts_begin(self):
self.out.write(" parts {\n")
def output_file_section_parts_end(self):
self.out.write(" }\n")
def output_file_section_images_begin(self):
self.out.write(" images {\n")
def output_file_section_images_end(self):
self.out.write(" }\n")
def output_file_foot(self):
self.out.write("""\
}
}
""")
def output_image(self, oid):
img = os.path.join(self.images_dir, oid)
img += ".png"
self._exec_cmd("--export-id='%s'" % oid,
"--export-id-only",
"--export-png='%s'" % os.path.join(self.basedir, img))
self.out.write(' image: "%s" COMP;\n' % img)
self.images[oid] = img
def output_part_desc_rel(self, x, y, w, h):
def choose_rel(relative, value, value_max):
if relative is not None:
return relative
elif value <= abs(value_max - value):
return 0.0
else:
return 1.0
x2 = x + w - 1
y2 = y + h - 1
rx1 = choose_rel(self.relative1_x, x, w)
rx2 = choose_rel(self.relative2_x, x2, w)
ry1 = choose_rel(self.relative1_y, y, h)
ry2 = choose_rel(self.relative2_y, y2, h)
ox1 = x - self.w * rx1
ox2 = x2 - self.w * rx2
oy1 = y - self.h * ry1
oy2 = y2 - self.h * ry2
self.out.write("""\
rel1 {
relative: %03.1f %03.1f;
offset: %d %d;
}
rel2 {
relative: %03.1f %03.1f;
offset: %d %d;
}
""" % (rx1, ry1, ox1, oy1, rx2, ry2, ox2, oy2))
def output_part(self, oid):
try:
x, y, w, h = self.sizes[oid]
except KeyError:
log.error("no such object id: %s", oid)
return
info = {
"name": oid,
"x": x,
"y": y,
"w": w,
"h": h,
}
self.out.write("""
part {
name: "%(name)s";
type: IMAGE;
""" % info)
if self.show_mouse_events:
self.out.write(" mouse_events: 0;\n")
self.out.write("""\
description {
state: "default" 0.0;
""")
if self.show_min:
self.out.write(" min: %(w)d %(h)d;\n" % info)
if self.show_max:
self.out.write(" max: %(w)d %(h)d;\n" % info)
self.output_part_desc_rel(x, y, w, h)
self.out.write("""\
image.normal: "%s";
}
}
""" % (self.images[oid],))
def foreach_id(inkscape2edc, ids=None, re_exclude=None):
if ids:
for oid in inkscape2edc.known_ids:
if oid in ids:
yield oid
else:
for oid in inkscape2edc.known_ids:
if re_exclude is not None and re_exclude.match(oid):
continue
yield oid
if __name__ == "__main__":
import optparse
usage = "usage: %prog [options] <input.svg>"
parser = optparse.OptionParser(usage=usage)
parser.add_option("-i", "--id", action="append", default=[],
help=("Object ID to use, it will be the part name. "
"Multiple usage to use more object ids."))
parser.add_option("-e", "--exclude", action="store", default=None,
help=("Exclude regular expression."
"Matching IDs will be ignored."))
parser.add_option("-o", "--output", action="store", default=None,
help="Output file to use")
parser.add_option("-g", "--group", action="store", default=None,
help="Group name")
parser.add_option("-d", "--images-dir", action="store", default="",
help="Directory where to output images.")
parser.add_option("--no-min", action="store_true",
help="Do not output min values")
parser.add_option("--no-max", action="store_true",
help="Do not output max values")
parser.add_option("--no-mouse_events", action="store_true",
help="Do not output mouse_events lines")
parser.add_option("--relative1-y", action="store",
choices=("top", "bottom", "auto"),
default="auto",
help=("Choose what to use as base for rel1 y values, "
"top=0.0, bottom=1.0, auto=nearest"))
parser.add_option("--relative2-y", action="store",
choices=("top", "bottom", "auto"),
default="auto",
help=("Choose what to use as base for rel2 y values, "
"top=0.0, bottom=1.0, auto=nearest"))
parser.add_option("--relative1-x", action="store",
choices=("left", "right", "auto"),
default="auto",
help=("Choose what to use as base for rel1 x values, "
"left=0.0, right=1.0, auto=nearest"))
parser.add_option("--relative2-x", action="store",
choices=("left", "right", "auto"),
default="auto",
help=("Choose what to use as base for rel2 x values, "
"left=0.0, right=1.0, auto=nearest"))
options, args = parser.parse_args()
try:
infile = args[0]
except IndexError:
parser.print_help()
raise SystemExit("missing input file name")
fname = os.path.splitext(infile)[0]
if not options.output:
options.output = fname + ".edc"
if not options.group:
options.group = fname
rx_map = {"left": 0.0, "right": 1.0}
options.relative1_x = rx_map.get(options.relative1_x, None)
options.relative2_x = rx_map.get(options.relative2_x, None)
ry_map = {"top": 0.0, "bottom": 1.0}
options.relative1_y = ry_map.get(options.relative1_y, None)
options.relative2_y = ry_map.get(options.relative2_y, None)
o = Inkscape2Edc(infile, options.output, options.group,
relative1_x=options.relative1_x,
relative2_x=options.relative2_x,
relative1_y=options.relative1_y,
relative2_y=options.relative2_y,
images_dir=options.images_dir,
show_max=not options.no_max, show_min=not options.no_min,
show_mouse_events=not options.no_mouse_events)
re_exclude = None
if options.exclude:
re_exclude = re.compile(options.exclude)
if options.images_dir:
os.makedirs(options.images_dir)
o.load_sizes()
o.output_file_header()
o.output_file_section_images_begin()
for oid in foreach_id(o, options.id, re_exclude):
o.output_image(oid)
o.output_file_section_images_end()
o.output_file_section_parts_begin()
for oid in foreach_id(o, options.id, re_exclude):
o.output_part(oid)
o.output_file_section_parts_end()
o.output_file_foot()
|