contrib/inkscape_tools: add iOS target format
[nit.git] / contrib / inkscape_tools / src / svg_to_icons.nit
index 917c197..bae4f7d 100644 (file)
@@ -19,9 +19,11 @@ module svg_to_icons
 
 import opts
 
+import xcode_assets
+
 redef class Int
        # Android name for this resolution
-       fun resolution_name: String
+       fun android_name: String
        do
                if self == 36 then return "ldpi"
                if self == 48 then return "mdpi"
@@ -36,18 +38,22 @@ end
 var opt_out = new OptionString("Where to output PNG files", "--out", "-o")
 var opt_id = new OptionString("Extract only object with given ID", "--id", "-i")
 var opt_android = new OptionBool("Generate in the file structure for Android", "--android", "-a")
+var opt_ios = new OptionBool("Generate in the file structure for iOS", "--ios")
 var opt_android_name = new OptionString("Name of the resource for Android", "--name", "-n")
 var opt_large = new OptionBool("Generate large icons (512 and 1024 px)", "--large", "-l")
 var opt_help = new OptionBool("Print this help message", "--help", "-h")
 
 var opt_context = new OptionContext
-opt_context.add_option(opt_out, opt_id, opt_android, opt_android_name, opt_large, opt_help)
+opt_context.add_option(opt_out, opt_id, opt_android, opt_ios, opt_android_name, opt_large, opt_help)
 
 opt_context.parse(args)
 var rest = opt_context.rest
 var errors = opt_context.errors
 if rest.length != 1 and not opt_help.value then errors.add "You must specify one source drawing file"
-if opt_android.value == opt_large.value then errors.add "You must specify a single format (--android or --large)"
+if [opt_android.value, opt_ios.value, opt_large.value].count(true) > 1 then
+       errors.add "You must specify a single format (--android, --ios or --large)"
+end
+
 if not errors.is_empty or opt_help.value then
        print errors.join("\n")
        print "Usage: svg_to_icons [Options] drawing.svg"
@@ -79,20 +85,29 @@ var id = opt_id.value
 var resolutions
 if opt_android.value then
        resolutions = [36, 48, 72, 96, 144, 192]
+else if opt_ios.value then
+       resolutions = [29, 57, 58, 80, 87, 114, 120, 180]
 else if opt_large.value then
        resolutions = [512, 1024]
 else abort
 
 var android_res_name = opt_android_name.value or else "icon"
 
+if opt_ios.value then
+       var contents_path = out_path / "Contents.json"
+       appiconset_contents_json.write_to_file contents_path
+end
+
 for wh in resolutions do
        var png_path
        if opt_android.value then
-               png_path = out_path / "drawable-" + wh.resolution_name / android_res_name + ".png"
+               png_path = out_path / "drawable-" + wh.android_name / android_res_name + ".png"
                var dir = png_path.dirname
                if not dir.file_exists then dir.mkdir
+       else if opt_ios.value then
+               png_path = out_path / "{wh}.png"
        else
-               png_path = "{out_path}/{wh}.png"
+               png_path = out_path / "{wh}.png"
        end
 
        var prog = "inkscape"