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
79 stderr
.write
"Output dir '{out_path}' does not exist (use --out)\n"
86 if opt_android
.value
then
87 resolutions
= [36, 48, 72, 96, 144, 192]
88 else if opt_ios
.value
then
89 resolutions
= [29, 57, 58, 80, 87, 114, 120, 180]
90 else if opt_large
.value
then
91 resolutions
= [512, 1024]
94 var android_res_name
= opt_android_name
.value
or else "icon"
97 var contents_path
= out_path
/ "Contents.json"
98 appiconset_contents_json
.write_to_file contents_path
101 for wh
in resolutions
do
103 if opt_android
.value
then
104 png_path
= out_path
/ "drawable-" + wh
.android_name
/ android_res_name
+ ".png"
105 var dir
= png_path
.dirname
106 if not dir
.file_exists
then dir
.mkdir
107 else if opt_ios
.value
then
108 png_path
= out_path
/ "{wh}.png"
110 png_path
= out_path
/ "{wh}.png"
113 var prog
= "inkscape"
114 var prog_args
= ["--without-gui", drawing
,
115 "-w={wh}", "-h={wh}", "--export-png={png_path}"]
116 if id
!= null then prog_args
.add
"--export-id={id}"
118 var proc
= new Process.from_a
(prog
, prog_args
)