androink.py: add support for xxhdpi and xxxhdpi resources
[android/android-app-development-getting-started.git] / ic_launcher_template / androink.py
1 #!/usr/bin/env python
2 #
3 # androink - Generate android resources for multiple densities
4 #
5 # Copyright (C) 2012  Federico Paolinelli <fedepaol@gmail.com>
6 # Copyright (C) 2013  Antonio Ospite <ospite@studenti.unina.it>
7 #
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.
12 #
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.
17 #
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/>.
20
21 # Based on:
22 # https://gist.github.com/fedepaol/4127778
23 #
24 # Following the directions of:
25 # https://developer.android.com/guide/practices/screens_support.html
26
27 import argparse
28 import sys
29 import os
30
31 __description = """This script expects the svg to use Inkscape DPIs.
32
33 Either MDPI or HDPI can be used as the baseline density and the drawables will
34 scaled to generate the other densities.
35
36 """
37 __version = "0.1"
38 __author_info = "Federico Paolinelli, Antonio Ospite"
39
40
41 BASE_OPTIONS = ' --export-area-page --export-png '
42
43 # Inkscape default DPI is 90
44 BASELINE_DPI = 90
45
46 # densities are calculated following the scaling ratio
47 # 3:4:6:8 mapped to ldpi:mdpi:hdpi:xhdpi
48
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
57 ]
58
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
67 ]
68
69
70 def export_file(file_name):
71     print 'exporting file', file_name
72     name_without_ext = os.path.splitext(file_name)[0]
73
74     if args.baseline_density == 'mdpi':
75         densities = densities_baseline_mdpi
76     elif args.baseline_density == 'hdpi':
77         densities = densities_baseline_hdpi
78
79     for density in densities:
80         dpispec, dpi = density
81         res_path = os.path.join(args.res_folder, dpispec)
82
83         if not os.path.exists(res_path):
84             os.makedirs(res_path)
85
86         source_file = os.path.join(args.svg_folder, file_name)
87         target = os.path.join(res_path, name_without_ext + '.png')
88
89         command_list = [args.ink_path, '--export-area-page',
90                         '-f', source_file,
91                         '--export-png', target,
92                         '--export-dpi', str(dpi)]
93
94         command = " ".join(command_list)
95
96         print 'executing', command
97         if not args.dry:
98             os.popen(command)
99
100
101 def option_parser():
102     usage = "usage: %(prog)s [options]"
103
104     parser = argparse.ArgumentParser(usage=usage,
105                                      description=__description,
106                                      epilog=__author_info,
107                                      version='%(prog)s ' + __version,)
108
109     parser.add_argument('-R', '--res_folder',  metavar="<dir>",
110                         dest='res_folder', required=True,
111                         help='path to the project res folder')
112
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')
116
117     parser.add_argument('-I', '--inkscape_path', metavar="<inkscape_path>",
118                         dest='ink_path', default='inkscape',
119                         help='path of Inkscape executable')
120
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')
124
125     parser.add_argument('-D', '--dry_run',
126                         dest='dry', action='store_const', const=True,
127                         help='performs a dry run')
128
129     parser.add_argument('-F', '--single_file', metavar="<file>",
130                         dest='file_name',
131                         help='name of the file, if only one file is to be converted')
132
133     return parser
134
135
136 if __name__ == "__main__":
137     parser = option_parser()
138     args = parser.parse_args()
139
140     if args.file_name:
141         export_file(args.file_name)
142     else:
143         files = os.listdir(args.svg_folder)
144         svg_files = filter(lambda x: x.lower().endswith('.svg'), files)
145
146         map(export_file, svg_files)