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
 
  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
 
  66 def export_file(file_name):
 
  67     print 'exporting file', file_name
 
  68     name_without_ext = os.path.splitext(file_name)[0]
 
  70     if args.baseline_density == 'mdpi':
 
  71         densities = densities_baseline_mdpi
 
  72     elif args.baseline_density == 'hdpi':
 
  73         densities = densities_baseline_hdpi
 
  75     for density in densities:
 
  76         dpispec, dpi = density
 
  77         res_path = os.path.join(args.res_folder, dpispec)
 
  79         if not os.path.exists(res_path):
 
  82         source_file = os.path.join(args.svg_folder, file_name)
 
  83         target = os.path.join(res_path, name_without_ext + '.png')
 
  85         command_list = [args.ink_path, '--export-area-page',
 
  87                         '--export-png', target,
 
  88                         '--export-dpi', str(dpi)]
 
  90         command = " ".join(command_list)
 
  92         print 'executing', command
 
  98     usage = "usage: %(prog)s [options]"
 
 100     parser = argparse.ArgumentParser(usage=usage,
 
 101                                      description=__description,
 
 102                                      epilog=__author_info,
 
 103                                      version='%(prog)s ' + __version,)
 
 105     parser.add_argument('-R', '--res_folder',  metavar="<dir>",
 
 106                         dest='res_folder', required=True,
 
 107                         help='path to the project res folder')
 
 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')
 
 113     parser.add_argument('-I', '--inkscape_path', metavar="<inkscape_path>",
 
 114                         dest='ink_path', default='inkscape',
 
 115                         help='path of Inkscape executable')
 
 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')
 
 121     parser.add_argument('-D', '--dry_run',
 
 122                         dest='dry', action='store_const', const=True,
 
 123                         help='performs a dry run')
 
 125     parser.add_argument('-F', '--single_file', metavar="<file>",
 
 127                         help='name of the file, if only one file is to be converted')
 
 132 if __name__ == "__main__":
 
 133     parser = option_parser()
 
 134     args = parser.parse_args()
 
 137         export_file(args.file_name)
 
 139         files = os.listdir(args.svg_folder)
 
 140         svg_files = filter(lambda x: x.lower().endswith('.svg'), files)
 
 142         map(export_file, svg_files)