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
23 fun android_path
: String do return "drawable-{resolution_name}/icon.png"
25 fun resolution_name
: String
27 if self == 36 then return "ldpi"
28 if self == 48 then return "mdpi"
29 if self == 72 then return "hdpi"
30 if self == 96 then return "xhdpi"
31 if self == 144 then return "xxhdpi"
32 if self == 192 then return "xxxhdpi"
37 var opt_out
= new OptionString("Where to output PNG files", "--out", "-o")
38 var opt_id
= new OptionString("Extract only object with given ID", "--id", "-i")
39 var opt_android
= new OptionBool("Generate in the file structure for Android", "--android", "-a")
40 var opt_large
= new OptionBool("Generate large icons (512 and 1024 px)", "--large", "-l")
41 var opt_help
= new OptionBool("Print this help message", "--help", "-h")
43 var opt_context
= new OptionContext
44 opt_context
.add_option
(opt_out
, opt_id
, opt_android
, opt_large
, opt_help
)
46 opt_context
.parse
(args
)
47 var rest
= opt_context
.rest
48 var errors
= opt_context
.errors
49 if rest
.length
!= 1 and not opt_help
.value
then errors
.add
"You must specify one source drawing file"
50 if opt_android
.value
== opt_large
.value
then errors
.add
"You must specify a single format (--android or --large)"
51 if not errors
.is_empty
or opt_help
.value
then
52 print errors
.join
("\n")
53 print
"Usage: svg_to_icons [Options] drawing.svg"
59 if not "inkscape".program_is_in_path
then
60 print
"This tool needs the external program `inkscape`, make sure it is installed and in your PATH."
64 var drawing
= rest
.first
65 if not drawing
.file_exists
then
66 stderr
.write
"Source drawing file '{drawing}' does not exist."
70 var out_path
= opt_out
.value
71 if out_path
== null then out_path
= "."
72 if not out_path
.file_exists
then
73 stderr
.write
"Output dir '{out_path}' does not exist (use --out)\n"
80 if opt_android
.value
then
81 resolutions
= [36, 48, 72, 96, 144, 192]
82 else if opt_large
.value
then
83 resolutions
= [512, 1024]
86 for wh
in resolutions
do
88 if opt_android
.value
then
89 png_path
= "{out_path}/{wh.android_path}"
90 var dir
= png_path
.dirname
91 if not dir
.file_exists
then dir
.mkdir
93 png_path
= "{out_path}/{wh}.png"
97 var prog_args
= ["--without-gui", drawing
,
98 "-w={wh}", "-h={wh}", "--export-png={png_path}"]
99 if id
!= null then prog_args
.add
"--export-id={id}"
101 var proc
= new Process.from_a
(prog
, prog_args
)