1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2012-2014 Alexis Laferrière <alexis.laf@xymus.net>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 # Tool to extract PNGs of different resolution from a SVG file
25 # Android name for this resolution
26 fun android_name
: String
28 if self == 36 then return "ldpi"
29 if self == 48 then return "mdpi"
30 if self == 72 then return "hdpi"
31 if self == 96 then return "xhdpi"
32 if self == 144 then return "xxhdpi"
33 if self == 192 then return "xxxhdpi"
38 var opt_out
= new OptionString("Where to output PNG files", "--out", "-o")
39 var opt_id
= new OptionString("Extract only object with given ID", "--id", "-i")
40 var opt_android
= new OptionBool("Generate in the file structure for Android", "--android", "-a")
41 var opt_ios
= new OptionBool("Generate in the file structure for iOS", "--ios")
42 var opt_android_name
= new OptionString("Name of the resource for Android", "--name", "-n")
43 var opt_large
= new OptionBool("Generate large icons (512 and 1024 px)", "--large", "-l")
44 var opt_help
= new OptionBool("Print this help message", "--help", "-h")
46 var opt_context
= new OptionContext
47 opt_context
.add_option
(opt_out
, opt_id
, opt_android
, opt_ios
, opt_android_name
, opt_large
, opt_help
)
49 opt_context
.parse
(args
)
50 var rest
= opt_context
.rest
51 var errors
= opt_context
.errors
52 if rest
.length
!= 1 and not opt_help
.value
then errors
.add
"You must specify one source drawing file"
53 if [opt_android
.value
, opt_ios
.value
, opt_large
.value
].count
(true) > 1 then
54 errors
.add
"You must specify a single format (--android, --ios or --large)"
57 if not errors
.is_empty
or opt_help
.value
then
58 print errors
.join
("\n")
59 print
"Usage: svg_to_icons [Options] drawing.svg"
65 if not "inkscape".program_is_in_path
then
66 print
"This tool needs the external program `inkscape`, make sure it is installed and in your PATH."
70 var drawing
= rest
.first
71 if not drawing
.file_exists
then
72 stderr
.write
"Source drawing file '{drawing}' does not exist."
76 var out_path
= opt_out
.value
77 if out_path
== null then out_path
= "."
78 if not out_path
.file_exists
then out_path
.mkdir
79 if not out_path
.file_exists
then
80 print_error
"Failed to create output dir '{out_path}'"
87 if opt_android
.value
then
88 resolutions
= [36, 48, 72, 96, 144, 192]
89 else if opt_ios
.value
then
90 resolutions
= [29, 58, 76, 80, 87, 120, 152, 180]
91 else if opt_large
.value
then
92 resolutions
= [512, 1024]
95 var android_res_name
= opt_android_name
.value
or else "icon"
98 var contents_path
= out_path
/ "Contents.json"
99 appiconset_contents_json
.write_to_file contents_path
102 for wh
in resolutions
do
104 if opt_android
.value
then
105 png_path
= out_path
/ "drawable-" + wh
.android_name
/ android_res_name
+ ".png"
106 var dir
= png_path
.dirname
107 if not dir
.file_exists
then dir
.mkdir
108 else if opt_ios
.value
then
109 png_path
= out_path
/ "{wh}.png"
111 png_path
= out_path
/ "{wh}.png"
114 var prog
= "inkscape"
115 var prog_args
= ["--without-gui", drawing
,
116 "-w={wh}", "-h={wh}", "--export-png={png_path}"]
117 if id
!= null then prog_args
.add
"--export-id={id}"
119 var proc
= new Process.from_a
(prog
, prog_args
)