8
INKSCAPE = '/usr/bin/inkscape'
9
SRC = os.path.join('.', 'src')
11
inkscape_process = None
13
def wait_for_prompt(process, command=None):
14
if command is not None:
15
process.stdin.write(command+'\n')
17
output = process.stdout.read(1)
18
output += process.stdout.read(1)
20
while output != "\n>":
22
output += process.stdout.read(1)
25
process = subprocess.Popen([INKSCAPE, '--shell'], bufsize=0, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
26
wait_for_prompt(process)
29
def inkscape_render_rect(icon_file, rect, output_file):
30
global inkscape_process
31
if inkscape_process is None:
32
inkscape_process = start_inkscape()
33
wait_for_prompt(inkscape_process, '%s -i %s -e %s' % (icon_file, rect, output_file))
35
class ContentHandler(xml.sax.ContentHandler):
41
def __init__(self, path, force=False):
42
self.stack = [self.ROOT]
43
self.inside = [self.ROOT]
46
self.state = self.ROOT
50
def endDocument(self):
53
def startElement(self, name, attrs):
54
if self.inside[-1] == self.ROOT:
56
self.stack.append(self.SVG)
57
self.inside.append(self.SVG)
59
elif self.inside[-1] == self.SVG:
60
if (name == "g" and attrs.has_key('inkscape:groupmode') and attrs.has_key('inkscape:label')
61
and attrs['inkscape:groupmode'] == 'layer' and attrs['inkscape:label'].startswith('baseplate')):
62
self.stack.append(self.LAYER)
63
self.inside.append(self.LAYER)
68
elif self.inside[-1] == self.LAYER:
69
if name == "text" and attrs.has_key('inkscape:label') and attrs['inkscape:label'] == 'context':
70
self.stack.append(self.TEXT)
71
self.inside.append(self.TEXT)
75
elif name == "text" and attrs.has_key('inkscape:label') and attrs['inkscape:label'] == 'icon-name':
76
self.stack.append(self.TEXT)
77
self.inside.append(self.TEXT)
82
self.rects.append(attrs)
84
self.stack.append(self.OTHER)
87
def endElement(self, name):
88
stacked = self.stack.pop()
89
if self.inside[-1] == stacked:
92
if stacked == self.TEXT and self.text is not None:
93
assert self.text in ['context', 'icon-name']
94
if self.text == 'context':
95
self.context = self.chars
96
elif self.text == 'icon-name':
97
self.icon_name = self.chars
99
elif stacked == self.LAYER:
100
assert self.icon_name
102
print '%s %s' % (self.context, self.icon_name)
103
for rect in self.rects:
104
width = rect['width']
105
height = rect['height']
108
dir = os.path.join("icons", "%sx%s" % (width, height), self.context)
109
outfile = os.path.join(dir, self.icon_name+'.png')
110
if not os.path.exists(dir):
112
# Do a time based check!
113
if self.force or not os.path.exists(outfile):
114
inkscape_render_rect(self.path, id, outfile)
115
sys.stdout.write('.')
117
stat_in = os.stat(self.path)
118
stat_out = os.stat(outfile)
119
if stat_in.st_mtime > stat_out.st_mtime:
120
inkscape_render_rect(self.path, id, outfile)
121
sys.stdout.write('.')
123
sys.stdout.write('-')
125
sys.stdout.write('\n')
128
def characters(self, chars):
129
self.chars += chars.strip()
131
if len(sys.argv) == 1:
132
if not os.path.exists('icons'):
134
print 'Rendering from SVGs in %s' % SRC
135
for file in os.listdir(SRC):
136
if file[-4:] == '.svg':
137
file = os.path.join(SRC, file)
138
handler = ContentHandler(file)
139
xml.sax.parse(open(file), handler)
141
file = os.path.join(SRC, sys.argv[1] + '.svg')
142
if os.path.exists(os.path.join(file)):
143
handler = ContentHandler(file, True)
144
xml.sax.parse(open(file), handler)
146
print "Error: No such file %s" % file