forked from sympy/sympy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_logos.py
executable file
·266 lines (211 loc) · 9.01 KB
/
generate_logos.py
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
#!/usr/bin/env python
"""
This script creates logos of different formats from the source "sympy.svg"
Requirements:
rsvg-convert - for converting to *.png format (librsvg2-bin deb package)
imagemagick - for converting to *.ico favicon format
"""
from optparse import OptionParser
import xml.dom.minidom
import os.path
import logging
import subprocess
import sys
default_source_dir = os.path.join(os.path.dirname(__file__), "src/logo")
default_source_svg = "sympy.svg"
default_output_dir = os.path.join(os.path.dirname(__file__), "_build/logo")
# those are the options for resizing versions without tail or text
svg_sizes = {}
svg_sizes['notail'] = {"prefix":"notail", "dx":-70, "dy":-20, "size":690, "title":"SymPy Logo, with no tail"}
svg_sizes['notail-notext'] = {"prefix":"notailtext", "dx":-70, "dy":60, "size":690, "title":"SymPy Logo, with no tail, no text"}
svg_sizes['notext'] = {"prefix":"notext", "dx":-7, "dy":90, "size":750, "title":"SymPy Logo, with no text"}
# The list of identifiers of various versions
versions = ['notail', 'notail-notext', 'notext']
parser = OptionParser()
parser.set_usage("%s [options ...]")
parser.add_option("--source-dir", type="string", dest="source_dir",
help="Directory of the source *.svg file [default: %default]",
default=default_source_dir)
parser.add_option("--source-svg", type="string", dest="source_svg",
help="File name of the source *.svg file [default: %default]",
default=default_source_svg)
parser.add_option("--svg", action="store_true", dest="generate_svg",
help="Generate *.svg versions without tails and without text 'SymPy' [default: %default]",
default=False)
parser.add_option("--png", action="store_true", dest="generate_png",
help="Generate *.png versions [default: %default]",
default=False)
parser.add_option("--ico", action="store_true", dest="generate_ico",
help="Generate *.ico versions [default: %default]",
default=False)
parser.add_option("--clear", action="store_true", dest="clear",
help="Remove temporary files [default: %default]",
default=False)
parser.add_option("-a", "--all", action="store_true", dest="generate_all",
help="Shorthand for '--svg --png --ico --clear' options [default: %default]",
default=True)
parser.add_option("-s", "--sizes", type="string", dest="sizes",
help="Sizes of png pictures [default: %default]",
default="160,500")
parser.add_option("--icon-sizes", type="string", dest="icon_sizes",
help="Sizes of icons embedded in favicon file [default: %default]",
default="16,32,48,64")
parser.add_option("--output-dir", type="string", dest="output_dir",
help="Outpu dir [default: %default]",
default=default_output_dir)
parser.add_option("-d", "--debug", action="store_true", dest="debug",
default=False)
def main():
options, args = parser.parse_args()
if options.debug:
logging.basicConfig(level=logging.DEBUG)
fn_source = os.path.join(options.source_dir, options.source_svg)
if options.generate_svg or options.generate_all:
generate_notail_notext_versions(fn_source, options.output_dir)
if options.generate_png or options.generate_all:
sizes = options.sizes.split(",")
sizes = [int(s) for s in sizes]
convert_to_png(fn_source, options.output_dir, sizes)
if options.generate_ico or options.generate_all:
sizes = options.icon_sizes.split(",")
sizes = [int(s) for s in sizes]
convert_to_ico(fn_source, options.output_dir, sizes)
def generate_notail_notext_versions(fn_source, output_dir):
for ver in versions:
properties = svg_sizes[ver]
doc = load_svg(fn_source)
(notail, notext) = versionkey_to_boolean_tuple(ver)
g_tail = searchElementById(doc, "SnakeTail", "g")
if notail:
g_tail.setAttribute("display", "none")
g_text = searchElementById(doc, "SymPy_text", "g")
if notext:
g_text.setAttribute("display", "none")
g_logo = searchElementById(doc, "SympyLogo", "g")
dx = properties["dx"]
dy = properties["dy"]
transform = "translate(%d,%d)" % (dx, dy)
g_logo.setAttribute("transform", transform)
svg = searchElementById(doc, "svg_SympyLogo", "svg")
newsize = properties["size"]
svg.setAttribute("width", "%d" % newsize)
svg.setAttribute("height", "%d" % newsize)
title = svg.getElementsByTagName("title")[0]
title.firstChild.data = properties["title"]
desc = svg.getElementsByTagName("desc")[0]
desc.appendChild(doc.createTextNode("\n\nThis file is generated from %s !" % fn_source))
fn_out = get_svg_filename_from_versionkey(fn_source, ver)
fn_out = os.path.join(output_dir, fn_out)
save_svg(fn_out, doc)
def convert_to_png(fn_source, output_dir, sizes):
svgs = list(versions)
svgs.insert(0, '')
cmd = "rsvg-convert"
p = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
p.communicate()
if p.returncode == 127:
logging.error("%s: command not found" % cmd)
sys.exit(p.returncode)
for ver in svgs:
if ver == '':
fn_svg = fn_source
else:
fn_svg = get_svg_filename_from_versionkey(fn_source, ver)
fn_svg = os.path.join(output_dir, fn_svg)
basename = os.path.basename(fn_svg)
name, ext = os.path.splitext(basename)
for size in sizes:
fn_out = "%s-%dpx.png" % (name, size)
fn_out = os.path.join(output_dir, fn_out)
cmd = "rsvg-convert %s -f png -o %s -h %d -w %d" % (fn_svg, fn_out,
size, size)
p = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
p.communicate()
if p.returncode != 0:
logging.error("Return code is not 0: Command: %s" % cmd)
logging.error("return code: %s" % p.returncode)
sys.exit(p.returncode)
else:
logging.debug("command: %s" % cmd)
logging.debug("return code: %s" % p.returncode)
def convert_to_ico(fn_source, output_dir, sizes):
# firstly prepare *.png files, which will be embedded
# into the *.ico files.
convert_to_png(fn_source, output_dir, sizes)
svgs = list(versions)
svgs.insert(0, '')
cmd = "convert"
p = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
p.communicate()
if p.returncode == 127:
logging.error("%s: command not found" % cmd)
sys.exit(p.returncode)
for ver in svgs:
if ver == '':
fn_svg = fn_source
else:
fn_svg = get_svg_filename_from_versionkey(fn_source, ver)
fn_svg = os.path.join(output_dir, fn_svg)
basename = os.path.basename(fn_svg)
name, ext = os.path.splitext(basename)
# calculate the list of *.png files
pngs = []
for size in sizes:
fn_png= "%s-%dpx.png" % (name, size)
fn_png = os.path.join(output_dir, fn_png)
pngs.append(fn_png)
# convert them to *.ico
fn_out = "%s-favicon.ico" % name
fn_out = os.path.join(output_dir, fn_out)
cmd = "convert %s %s" % (" ".join(pngs), fn_out)
p = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
p.communicate()
if p.returncode != 0:
logging.error("Return code is not 0: Command: %s" % cmd)
logging.error("return code: %s" % p.returncode)
sys.exit(p.returncode)
else:
logging.debug("command: %s" % cmd)
logging.debug("return code: %s" % p.returncode)
def versionkey_to_boolean_tuple(ver):
notail = False
notext = False
vers = ver.split("-")
notail = 'notail' in vers
notext = 'notext' in vers
return (notail, notext)
def get_svg_filename_from_versionkey(fn_source, ver):
basename = os.path.basename(fn_source)
if ver == '':
return basename
name, ext = os.path.splitext(basename)
prefix = svg_sizes[ver]["prefix"]
fn_out = "%s-%s.svg" % (name, prefix)
return fn_out
def searchElementById(node, Id, tagname):
"""
Search element by id in all the children and descendants of node.
id is lower case, not ID which is usually used for getElementById
"""
nodes = node.getElementsByTagName(tagname)
for node in nodes:
an = node.getAttributeNode('id')
if an and an.nodeValue == Id:
return node
def load_svg(fn):
doc = xml.dom.minidom.parse(fn)
return doc
def save_svg(fn, doc):
with open(fn, "wb") as f:
xmlstr = doc.toxml("utf-8")
f.write(xmlstr)
logging.info(" File saved: %s" % fn)
main()