301772b5cded4f8d7278828b231fd2d15483178b
[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 fun android_path: String do return "drawable-{resolution_name}/icon.png"
24
25 fun resolution_name: String
26 do
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"
33 abort
34 end
35 end
36
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")
42
43 var opt_context = new OptionContext
44 opt_context.add_option(opt_out, opt_id, opt_android, 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 for wh in resolutions do
87 var png_path
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
92 else
93 png_path = "{out_path}/{wh}.png"
94 end
95
96 var prog = "inkscape"
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}"
100
101 var proc = new Process.from_a(prog, prog_args)
102 proc.wait
103 end