~ubuntu-branches/ubuntu/trusty/unity-control-center/trusty

« back to all changes in this revision

Viewing changes to panels/color/icons/render-icons.py

  • Committer: Package Import Robot
  • Author(s): Robert Ancell
  • Date: 2014-01-08 16:29:18 UTC
  • Revision ID: package-import@ubuntu.com-20140108162918-g29dd08tr913y2qh
Tags: upstream-14.04.0
ImportĀ upstreamĀ versionĀ 14.04.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
 
 
3
import os
 
4
import sys
 
5
import xml.sax
 
6
import subprocess
 
7
 
 
8
INKSCAPE = '/usr/bin/inkscape'
 
9
SRC = os.path.join('.', 'src')
 
10
 
 
11
inkscape_process = None
 
12
 
 
13
def wait_for_prompt(process, command=None):
 
14
    if command is not None:
 
15
        process.stdin.write(command+'\n')
 
16
 
 
17
    output = process.stdout.read(1)
 
18
    output += process.stdout.read(1)
 
19
    
 
20
    while output != "\n>":
 
21
        output = output[-1:]
 
22
        output += process.stdout.read(1)
 
23
 
 
24
def start_inkscape():
 
25
    process = subprocess.Popen([INKSCAPE, '--shell'], bufsize=0, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
 
26
    wait_for_prompt(process)
 
27
    return process
 
28
 
 
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))
 
34
 
 
35
class ContentHandler(xml.sax.ContentHandler):
 
36
    ROOT = 0
 
37
    SVG = 1
 
38
    LAYER = 2
 
39
    OTHER = 3
 
40
    TEXT = 4
 
41
    def __init__(self, path, force=False):
 
42
        self.stack = [self.ROOT]
 
43
        self.inside = [self.ROOT]
 
44
        self.path = path
 
45
        self.rects = []
 
46
        self.state = self.ROOT
 
47
        self.chars = ""
 
48
        self.force = force
 
49
 
 
50
    def endDocument(self):
 
51
        pass
 
52
 
 
53
    def startElement(self, name, attrs):
 
54
        if self.inside[-1] == self.ROOT:
 
55
            if name == "svg":
 
56
                self.stack.append(self.SVG)
 
57
                self.inside.append(self.SVG)
 
58
                return
 
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)
 
64
                self.context = None
 
65
                self.icon_name = None
 
66
                self.rects = []
 
67
                return
 
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)
 
72
                self.text='context'
 
73
                self.chars = ""
 
74
                return
 
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)
 
78
                self.text='icon-name'
 
79
                self.chars = ""
 
80
                return
 
81
            elif name == "rect":
 
82
                self.rects.append(attrs)
 
83
 
 
84
        self.stack.append(self.OTHER)
 
85
 
 
86
 
 
87
    def endElement(self, name):
 
88
        stacked = self.stack.pop()
 
89
        if self.inside[-1] == stacked:
 
90
            self.inside.pop()
 
91
 
 
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
 
98
            self.text = None
 
99
        elif stacked == self.LAYER:
 
100
            assert self.icon_name
 
101
            assert self.context
 
102
            print '%s %s' % (self.context, self.icon_name)
 
103
            for rect in self.rects:
 
104
                width = rect['width']
 
105
                height = rect['height']
 
106
                id = rect['id']
 
107
 
 
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):
 
111
                    os.makedirs(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('.')
 
116
                else:
 
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('.')
 
122
                    else:
 
123
                        sys.stdout.write('-')
 
124
                sys.stdout.flush()
 
125
            sys.stdout.write('\n')
 
126
            sys.stdout.flush()
 
127
 
 
128
    def characters(self, chars):
 
129
        self.chars += chars.strip()
 
130
 
 
131
if len(sys.argv) == 1:
 
132
    if not os.path.exists('icons'):
 
133
        os.mkdir('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)
 
140
else:
 
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)
 
145
    else:
 
146
        print "Error: No such file %s" % file
 
147
        sys.exit(1)
 
148
 
 
149