3 # androink - Generate android resources for multiple densities
5 # Copyright (C) 2012 Federico Paolinelli <fedepaol@gmail.com>
6 # Copyright (C) 2013 Antonio Ospite <ospite@studenti.unina.it>
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
22 # https://gist.github.com/fedepaol/4127778
24 # Following the directions of:
25 # https://developer.android.com/guide/practices/screens_support.html
31 __description = """This script expects the svg to use Inkscape DPIs.
33 Either MDPI or HDPI can be used as the baseline density and the drawables will
34 scaled to generate the other densities.
38 __author_info = "Federico Paolinelli, Antonio Ospite"
41 BASE_OPTIONS = ' --export-area-page --export-png '
43 # Inkscape default DPI is 90
46 # densities are calculated following the scaling ratio
47 # 3:4:6:8 mapped to ldpi:mdpi:hdpi:xhdpi
49 # for baseline mdpi consider 4 as the base ratio
50 densities_baseline_mdpi = [
51 ('drawable-ldpi', BASELINE_DPI * 0.75), # 3/4
52 ('drawable-mdpi', BASELINE_DPI * 1.0), # 4/4
53 ('drawable-hdpi', BASELINE_DPI * 1.5), # 6/4
54 ('drawable-xhdpi', BASELINE_DPI * 2.0), # 8/4
55 ('drawable-xxhdpi', BASELINE_DPI * 3.0), # 12/4
56 ('drawable-xxxhdpi', BASELINE_DPI * 4.0), # 16/4
59 # for baseline hdpi consider 6 as the base ratio
60 densities_baseline_hdpi = [
61 ('drawable-ldpi', BASELINE_DPI * 0.5), # 3/6
62 ('drawable-mdpi', BASELINE_DPI * 0.666666667), # 4/6
63 ('drawable-hdpi', BASELINE_DPI * 1.0), # 6/6
64 ('drawable-xhdpi', BASELINE_DPI * 1.333333333), # 8/6
65 ('drawable-xxhdpi', BASELINE_DPI * 2.0), # 12/6
66 ('drawable-xxxhdpi', BASELINE_DPI * 2.666666667), # 16/6
70 def export_file(file_name):
71 print 'exporting file', file_name
72 name_without_ext = os.path.splitext(file_name)[0]
74 if args.baseline_density == 'mdpi':
75 densities = densities_baseline_mdpi
76 elif args.baseline_density == 'hdpi':
77 densities = densities_baseline_hdpi
79 for density in densities:
80 dpispec, dpi = density
81 res_path = os.path.join(args.res_folder, dpispec)
83 if not os.path.exists(res_path):
86 source_file = os.path.join(args.svg_folder, file_name)
87 target = os.path.join(res_path, name_without_ext + '.png')
89 command_list = [args.ink_path, '--export-area-page',
91 '--export-png', target,
92 '--export-dpi', str(dpi)]
94 command = " ".join(command_list)
96 print 'executing', command
102 usage = "usage: %(prog)s [options]"
104 parser = argparse.ArgumentParser(usage=usage,
105 description=__description,
106 epilog=__author_info,
107 version='%(prog)s ' + __version,)
109 parser.add_argument('-R', '--res_folder', metavar="<dir>",
110 dest='res_folder', required=True,
111 help='path to the project res folder')
113 parser.add_argument('-S', '--svg_folder', metavar="<dir>",
114 dest='svg_folder', default='.',
115 help='folder that contains all the svg files to be converted')
117 parser.add_argument('-I', '--inkscape_path', metavar="<inkscape_path>",
118 dest='ink_path', default='inkscape',
119 help='path of Inkscape executable')
121 parser.add_argument('-d', '--baseline-density', metavar="<mdpi|hdpi>",
122 dest='baseline_density', choices=['mdpi', 'hdpi'], default='mdpi',
123 help='the baseline density to generate the drawables from')
125 parser.add_argument('-D', '--dry_run',
126 dest='dry', action='store_const', const=True,
127 help='performs a dry run')
129 parser.add_argument('-F', '--single_file', metavar="<file>",
131 help='name of the file, if only one file is to be converted')
136 if __name__ == "__main__":
137 parser = option_parser()
138 args = parser.parse_args()
141 export_file(args.file_name)
143 files = os.listdir(args.svg_folder)
144 svg_files = filter(lambda x: x.lower().endswith('.svg'), files)
146 map(export_file, svg_files)