androink.py: support both mdpi and hdpi as baseline densities
[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     ]
56
57 # for baseline hdpi consider 6 as the base ratio
58 densities_baseline_hdpi = [
59     ('drawable-ldpi', BASELINE_DPI * 0.5),  # 3/6
60     ('drawable-mdpi', BASELINE_DPI * 0.666666667),  # 4/6
61     ('drawable-hdpi', BASELINE_DPI * 1.0),  # 6/6
62     ('drawable-xhdpi', BASELINE_DPI * 1.333333333),  # 8/6
63     ]
64
65
66 def export_file(file_name):
67     print 'exporting file', file_name
68     name_without_ext = os.path.splitext(file_name)[0]
69
70     if args.baseline_density == 'mdpi':
71         densities = densities_baseline_mdpi
72     elif args.baseline_density == 'hdpi':
73         densities = densities_baseline_hdpi
74
75     for density in densities:
76         dpispec, dpi = density
77         res_path = os.path.join(args.res_folder, dpispec)
78
79         if not os.path.exists(res_path):
80             os.makedirs(res_path)
81
82         source_file = os.path.join(args.svg_folder, file_name)
83         target = os.path.join(res_path, name_without_ext + '.png')
84
85         command_list = [args.ink_path, '--export-area-page',
86                 '-f', source_file,
87                 '--export-png', target,
88                 '--export-dpi', str(dpi)]
89
90         command = " ".join(command_list)
91
92         print 'executing', command
93         if not args.dry:
94             os.popen(command)
95
96
97 def option_parser():
98     usage = "usage: %(prog)s [options]"
99
100     parser = argparse.ArgumentParser(usage=usage,
101             description=__description,
102             epilog=__author_info,
103             version='%(prog)s ' + __version,)
104
105     parser.add_argument('-R', '--res_folder',  metavar="<dir>",
106             dest='res_folder', required=True,
107             help='path to the project res folder')
108
109     parser.add_argument('-S', '--svg_folder', metavar="<dir>",
110             dest='svg_folder', default='.',
111             help='folder that contains all the svg files to be converted')
112
113     parser.add_argument('-I', '--inkscape_path', metavar="<inkscape_path>",
114             dest='ink_path', default='inkscape',
115             help='path of Inkscape executable')
116
117     parser.add_argument('-d', '--baseline-density', metavar="<mdpi|hdpi>",
118             dest='baseline_density', choices=['mdpi', 'hdpi'], default='mdpi',
119             help='the baseline density to generate the drawables from')
120
121     parser.add_argument('-D', '--dry_run',
122             dest='dry', action='store_const', const=True,
123             help='performs a dry run')
124
125     parser.add_argument('-F', '--single_file', metavar="<file>",
126             dest='file_name',
127             help='name of the file, if only one file is to be converted')
128
129     return parser
130
131
132 if __name__ == "__main__":
133     parser = option_parser()
134     args = parser.parse_args()
135
136     if args.file_name:
137         export_file(args.file_name)
138     else:
139         files = os.listdir(args.svg_folder)
140         svg_files = filter(lambda x: x.lower().endswith('.svg'), files)
141
142         map(export_file, svg_files)