3 # androink - Generate android resources for multiple resolutions
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 This means that the resolution given in the drawing will be used for
34 MDPI resolution and scaled to generate the other resolutions.
38 __author_info = "Federico Paolinelli, Antonio Ospite"
41 BASE_OPTIONS = ' --export-area-page --export-png '
43 # Inkscape default DPI is 90, we use this for the MDPI resource
46 LDPI = ('drawable-ldpi', BASELINE_DPI * 0.75)
47 MDPI = ('drawable-mdpi', BASELINE_DPI * 1.0)
48 HDPI = ('drawable-hdpi', BASELINE_DPI * 1.5)
49 XHDPI = ('drawable-xhdpi', BASELINE_DPI * 2.0)
51 resolutions = [LDPI, MDPI, HDPI, XHDPI]
54 def export_file(file_name):
55 print 'exporting file', file_name
56 name_without_ext = os.path.splitext(file_name)[0]
58 for rel in resolutions:
60 res_path = os.path.join(args.res_folder, dpispec)
62 if not os.path.exists(res_path):
65 source_file = os.path.join(args.svg_folder, file_name)
66 target = os.path.join(res_path, name_without_ext + '.png')
68 command_list = [args.ink_path, '--export-area-page',
70 '--export-png', target,
71 '--export-dpi', str(dpi)]
73 command = " ".join(command_list)
75 print 'executing', command
81 usage = "usage: %(prog)s [options]"
83 parser = argparse.ArgumentParser(usage=usage,
84 description=__description,
86 version='%(prog)s ' + __version,)
88 parser.add_argument('-R', '--res_folder', metavar="<dir>",
89 dest='res_folder', required=True,
90 help='path to the project res folder')
92 parser.add_argument('-S', '--svg_folder', metavar="<dir>",
93 dest='svg_folder', default='.',
94 help='folder that contains all the svg files to be converted')
96 parser.add_argument('-I', '--inkscape_path', metavar="<inkscape_path>",
97 dest='ink_path', default='inkscape',
98 help='path of Inkscape executable')
100 parser.add_argument('-D', '--dry_run',
101 dest='dry', action='store_const', const=True,
102 help='performs a dry run')
104 parser.add_argument('-F', '--single_file', metavar="<file>",
106 help='name of the file, if only one file is to be converted')
111 if __name__ == "__main__":
112 parser = option_parser()
113 args = parser.parse_args()
116 export_file(args.file_name)
118 files = os.listdir(args.svg_folder)
119 svg_files = filter(lambda x: x.lower().endswith('.svg'), files)
121 map(export_file, svg_files)