+#!/usr/bin/env python
+#
+# androink - Generate android resources for multiple resolutions
+#
+# Copyright (C) 2012 Federico Paolinelli <fedepaol@gmail.com>
+# Copyright (C) 2013 Antonio Ospite <ospite@studenti.unina.it>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+# Based on:
+# https://gist.github.com/fedepaol/4127778
+#
+# Following the directions of:
+# https://developer.android.com/guide/practices/screens_support.html
+
+import argparse
+import sys
+import os
+
+__description = """This script expects the svg to use Inkscape DPIs.
+
+This means that the resolution given in the drawing will be used for
+MDPI resolution and scaled to generate the other resolutions.
+
+"""
+__version = "0.1"
+__author_info = "Federico Paolinelli, Antonio Ospite"
+
+
+BASE_OPTIONS = ' --export-area-page --export-png '
+
+# Inkscape default DPI is 90, we use this for the MDPI resource
+BASELINE_DPI = 90
+
+LDPI = ('drawable-ldpi', BASELINE_DPI * 0.75)
+MDPI = ('drawable-mdpi', BASELINE_DPI * 1.0)
+HDPI = ('drawable-hdpi', BASELINE_DPI * 1.5)
+XHDPI = ('drawable-xhdpi', BASELINE_DPI * 2.0)
+
+resolutions = [LDPI, MDPI, HDPI, XHDPI]
+
+
+def export_file(file_name):
+ print 'exporting file', file_name
+ name_without_ext = os.path.splitext(file_name)[0]
+
+ for rel in resolutions:
+ dpispec, dpi = rel
+ res_path = os.path.join(args.res_folder, dpispec)
+
+ if not os.path.exists(res_path):
+ os.makedirs(res_path)
+
+ source_file = os.path.join(args.svg_folder, file_name)
+ target = os.path.join(res_path, name_without_ext + '.png')
+
+ command_list = [args.ink_path, '--export-area-page',
+ '-f', source_file,
+ '--export-png', target,
+ '--export-dpi', str(dpi)]
+
+ command = " ".join(command_list)
+
+ print 'executing', command
+ if not args.dry:
+ os.popen(command)
+
+
+def option_parser():
+ usage = "usage: %(prog)s [options]"
+
+ parser = argparse.ArgumentParser(usage=usage,
+ description=__description,
+ epilog=__author_info,
+ version='%(prog)s ' + __version,)
+
+ parser.add_argument('-R', '--res_folder', metavar="<dir>",
+ dest='res_folder', required=True,
+ help='path to the project res folder')
+
+ parser.add_argument('-S', '--svg_folder', metavar="<dir>",
+ dest='svg_folder', default='.',
+ help='folder that contains all the svg files to be converted')
+
+ parser.add_argument('-I', '--inkscape_path', metavar="<inkscape_path>",
+ dest='ink_path', default='inkscape',
+ help='path of Inkscape executable')
+
+ parser.add_argument('-D', '--dry_run',
+ dest='dry', action='store_const', const=True,
+ help='performs a dry run')
+
+ parser.add_argument('-F', '--single_file', metavar="<file>",
+ dest='file_name',
+ help='name of the file, if you want to convert only one file')
+
+ return parser
+
+
+if __name__ == "__main__":
+ parser = option_parser()
+ args = parser.parse_args()
+
+ if args.file_name:
+ export_file(args.file_name)
+ else:
+ files = os.listdir(args.svg_folder)
+ svg_files = filter(lambda x: x.lower().endswith('.svg'), files)
+
+ map(export_file, svg_files)