This repository has been archived by the owner on Jan 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathandroid_export.py
executable file
·188 lines (151 loc) · 6.8 KB
/
android_export.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
#!/usr/bin/env python
# ***** BEGIN APACHE LICENSE BLOCK *****
#
# Copyright 2013 Christian Becker <[email protected]>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# ***** END APACHE LICENCE BLOCK *****
import optparse
import os
import subprocess
import sys
from copy import copy
try:
from subprocess import DEVNULL
except ImportError:
DEVNULL = open(os.devnull, 'w')
def checkForPath(command):
return 0 == subprocess.call([
command, "--version"
], stdout=DEVNULL, stderr=subprocess.STDOUT)
def error(msg):
sys.stderr.write((unicode(msg) + "\n").encode("UTF-8"))
sys.exit(1)
def export(svg, options):
for qualifier, dpi in options.densities:
export_density(svg, options, qualifier, dpi)
def export_density(svg, options, qualifier, dpi):
dir_type = "drawable"
if options.launcher_icon:
dir_type = "mipmap"
dir = "%s/%s-%s" % (options.resdir, dir_type, qualifier)
if not os.path.exists(dir):
os.makedirs(dir)
def export_resource(params, name):
png = "%s/%s.png" % (dir, name)
call_params = ["inkscape",
"--without-gui",
"--export-dpi=%s" % dpi,
"--export-png=%s" % png]
if isinstance(params, list):
call_params.extend(params)
else:
call_params.append(params)
call_params.append(svg)
subprocess.check_call(call_params, stdout=DEVNULL, stderr=subprocess.STDOUT)
if options.strip:
subprocess.check_call([
"convert", "-antialias", "-strip", png, png
], stdout=DEVNULL, stderr=subprocess.STDOUT)
if options.optimize:
subprocess.check_call([
"optipng", "-quiet", "-o7", png
], stdout=DEVNULL, stderr=subprocess.STDOUT)
if options.source == '"selected_ids"':
if options.scale and (options.scale > 0):
dpi *= options.scale
params = create_selection_params(options)
for id in options.ids:
current_params = ["--export-id=%s" % id]
current_params.extend(params)
filename = get_selection_filename(id, options)
export_resource(current_params, filename)
else:
export_resource("--export-area-page", options.resname)
def create_selection_params(options):
params = []
if options.only_selected:
params.append("--export-id-only")
if options.transparent_background:
params.append("-y 0")
return params
def get_selection_filename(id, options):
if len(options.ids) == 1 and options.resname:
return options.resname
return id
def check_boolstr(option, opt, value):
value = value.capitalize()
if value == "True":
return True
if value == "False":
return False
raise optparse.OptionValueError("option %s: invalid boolean value: %s" % (opt, value))
class Option(optparse.Option):
TYPES = optparse.Option.TYPES + ("boolstr",)
TYPE_CHECKER = copy(optparse.Option.TYPE_CHECKER)
TYPE_CHECKER["boolstr"] = check_boolstr
def append_density(option, opt_str, value, parser, *density):
if not value:
return
if getattr(parser.values, option.dest) is None:
setattr(parser.values, option.dest, [])
getattr(parser.values, option.dest).append(density)
class DensityGroup(optparse.OptionGroup):
def add_density_option(self, name, dpi):
self.add_option("--%s" % name, action="callback", type="boolstr", dest="densities", metavar="BOOL",
callback=append_density, callback_args=(name, dpi), help="Export %s variants" % name.upper())
parser = optparse.OptionParser(usage="usage: %prog [options] SVGfile", option_class=Option)
parser.add_option("--source", action="store", type="choice", choices=('"selected_ids"', '"page"'), help="Source of the drawable")
parser.add_option("--id", action="append", dest="ids", metavar="ID", help="ID attribute of objects to export, can be specified multiple times")
parser.add_option("--resdir", action="store", help="Resources directory")
parser.add_option("--resname", action="store", help="Resource name (when --source=page)")
parser.add_option("--launcher-icon", action="store", type="boolstr", help="Whether the icon is a launcher icon")
parser.add_option("--only-selected", action="store", type="boolstr", help="Export only selected (without any background or other elements)")
parser.add_option("--scale", action="store", type="float", help="Output image scale")
parser.add_option("--transparent-background", action="store", type="boolstr", help="Transparent background")
group = DensityGroup(parser, "Select which densities to export")
group.add_density_option("ldpi", 67.5)
group.add_density_option("mdpi", 90)
group.add_density_option("hdpi", 135)
group.add_density_option("xhdpi", 180)
group.add_density_option("xxhdpi", 270)
group.add_density_option("xxxhdpi", 360)
parser.add_option_group(group)
parser.add_option("--strip", action="store", type="boolstr", help="Use ImageMagick to reduce the image size")
parser.add_option("--optimize", action="store", type="boolstr", help="Use OptiPNG to reduce the image size")
(options, args) = parser.parse_args()
if len(args) != 1:
parser.error("Expected exactly one argument, got %d" % len(args))
svg = args[0]
if options.resdir is None:
error("No Android Resource directory specified")
if not os.path.isdir(options.resdir):
error("Wrong Android Resource directory specified:\n'%s' is no dir" % options.resdir)
if not os.access(options.resdir, os.W_OK):
error("Wrong Android Resource directory specified:\nCould not write to '%s'" % options.resdir)
if options.source not in ('"selected_ids"', '"page"'):
error("Select what to export (selected items or whole page)")
if options.source == '"selected_ids"' and options.ids is None:
error("Select at least one item to export")
if options.source == '"page"' and not options.resname:
error("Please enter a resource name")
if not options.densities:
error("Select at least one DPI variant to export")
if not checkForPath("inkscape"):
error("Make sure you have 'inkscape' on your PATH")
if options.strip and not checkForPath("convert"):
error("Make sure you have 'convert' on your PATH if you want to reduce the image size using ImageMagick")
if options.optimize and not checkForPath("optipng"):
error("Make sure you have 'optipng' on your PATH if you want to reduce the image size using OptiPNG")
export(svg, options)