contrib/inkscape_tools: add option to generate icons with different names
[nit.git] / contrib / inkscape_tools / src / svg_to_icons.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2012-2014 Alexis Laferrière <alexis.laf@xymus.net>
4 #
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
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
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.
16
17 # Tool to extract PNGs of different resolution from a SVG file
18 module svg_to_icons
19
20 import opts
21
22 redef class Int
23 # Android name for this resolution
24 fun resolution_name: String
25 do
26 if self == 36 then return "ldpi"
27 if self == 48 then return "mdpi"
28 if self == 72 then return "hdpi"
29 if self == 96 then return "xhdpi"
30 if self == 144 then return "xxhdpi"
31 if self == 192 then return "xxxhdpi"
32 abort
33 end
34 end
35
36 var opt_out = new OptionString("Where to output PNG files", "--out", "-o")
37 var opt_id = new OptionString("Extract only object with given ID", "--id", "-i")
38 var opt_android = new OptionBool("Generate in the file structure for Android", "--android", "-a")
39 var opt_android_name = new OptionString("Name of the resource for Android", "--name", "-n")
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")
42
43 var opt_context = new OptionContext
44 opt_context.add_option(opt_out, opt_id, opt_android, opt_android_name, opt_large, opt_help)
45
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"
54 print "Options:"
55 opt_context.usage
56 exit 1
57 end
58
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."
61 exit 1
62 end
63
64 var drawing = rest.first
65 if not drawing.file_exists then
66 stderr.write "Source drawing file '{drawing}' does not exist."
67 exit 1
68 end
69
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"
74 exit 1
75 end
76
77 var id = opt_id.value
78
79 var resolutions
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]
84 else abort
85
86 var android_res_name = opt_android_name.value or else "icon"
87
88 for wh in resolutions do
89 var png_path
90 if opt_android.value then
91 png_path = out_path / "drawable-" + wh.resolution_name / android_res_name + ".png"
92 var dir = png_path.dirname
93 if not dir.file_exists then dir.mkdir
94 else
95 png_path = "{out_path}/{wh}.png"
96 end
97
98 var prog = "inkscape"
99 var prog_args = ["--without-gui", drawing,
100 "-w={wh}", "-h={wh}", "--export-png={png_path}"]
101 if id != null then prog_args.add "--export-id={id}"
102
103 var proc = new Process.from_a(prog, prog_args)
104 proc.wait
105 end