1/* encodesymbolic.c
2 * Copyright (C) 2014 Alexander Larsson <alexl@redhat.com>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "config.h"
19
20#include <glib.h>
21#include <gdk/gdk.h>
22#include <glib/gi18n.h>
23
24#ifdef HAVE_UNISTD_H
25#include <unistd.h>
26#endif
27#ifdef G_OS_WIN32
28#include <io.h>
29#endif
30#include <errno.h>
31#include <stdlib.h>
32#include <locale.h>
33
34#include "gdkpixbufutilsprivate.h"
35
36static char *output_dir = NULL;
37
38static gboolean debug;
39
40static GOptionEntry args[] = {
41 { "output", 'o', 0, G_OPTION_ARG_FILENAME, &output_dir, N_("Output to this directory instead of cwd"), NULL },
42 { "debug", 0, 0, G_OPTION_ARG_NONE, &debug, N_("Generate debug output") },
43 { NULL }
44};
45
46int
47main (int argc, char **argv)
48{
49 char *path, *basename, *pngpath, *pngfile, *dot;
50 GOptionContext *context;
51 GdkPixbuf *symbolic;
52 GError *error;
53 int width, height;
54 char **sizev;
55 GFileOutputStream *out;
56 GFile *dest;
57 char *data;
58 gsize len;
59
60 setlocale (LC_ALL, locale: "");
61
62 bindtextdomain (GETTEXT_PACKAGE, GTK_LOCALEDIR);
63#ifdef HAVE_BIND_TEXTDOMAIN_CODESET
64 bind_textdomain_codeset (GETTEXT_PACKAGE, codeset: "UTF-8");
65#endif
66
67 g_set_prgname (prgname: "gtk-encode-symbolic-svg");
68
69 context = g_option_context_new (parameter_string: "[OPTION…] PATH WIDTHxHEIGHT");
70 g_option_context_add_main_entries (context, entries: args, GETTEXT_PACKAGE);
71
72 g_option_context_parse (context, argc: &argc, argv: &argv, NULL);
73
74 if (argc < 3)
75 {
76 g_printerr (format: "%s\n", g_option_context_get_help (context, FALSE, NULL));
77 return 1;
78 }
79
80 width = 0;
81 height = 0;
82 sizev = g_strsplit (string: argv[2], delimiter: "x", max_tokens: 0);
83 if (g_strv_length (str_array: sizev) == 2)
84 {
85 width = atoi(nptr: sizev[0]);
86 height = atoi(nptr: sizev[1]);
87 }
88 g_strfreev (str_array: sizev);
89
90 if (width == 0 || height == 0)
91 {
92 g_printerr (_("Invalid size %s\n"), argv[2]);
93 return 1;
94 }
95
96 path = argv[1];
97#ifdef G_OS_WIN32
98 path = g_locale_to_utf8 (path, -1, NULL, NULL, NULL);
99#endif
100
101 error = NULL;
102 if (!g_file_get_contents (filename: path, contents: &data, length: &len, error: &error))
103 {
104 g_printerr (_("Can’t load file: %s\n"), error->message);
105 return 1;
106 }
107
108 basename = g_path_get_basename (file_name: path);
109
110 symbolic = gtk_make_symbolic_pixbuf_from_data (data, len, width, height, scale: 1.0, debug_output_to: debug ? basename : NULL, error: &error);
111 if (symbolic == NULL)
112 {
113 g_printerr (_("Can’t load file: %s\n"), error->message);
114 return 1;
115 }
116
117 g_free (mem: data);
118
119 dot = strrchr (s: basename, c: '.');
120 if (dot != NULL)
121 *dot = 0;
122 pngfile = g_strconcat (string1: basename, ".symbolic.png", NULL);
123 g_free (mem: basename);
124
125 if (output_dir != NULL)
126 pngpath = g_build_filename (first_element: output_dir, pngfile, NULL);
127 else
128 pngpath = g_strdup (str: pngfile);
129
130 g_free (mem: pngfile);
131
132 dest = g_file_new_for_path (path: pngpath);
133
134
135 out = g_file_replace (file: dest,
136 NULL, FALSE,
137 flags: G_FILE_CREATE_REPLACE_DESTINATION,
138 NULL, error: &error);
139 if (out == NULL)
140 {
141 g_printerr (_("Can’t save file %s: %s\n"), pngpath, error->message);
142 return 1;
143 }
144
145 if (!gdk_pixbuf_save_to_stream (pixbuf: symbolic, G_OUTPUT_STREAM (out), type: "png", NULL, error: &error, NULL))
146 {
147 g_printerr (_("Can’t save file %s: %s\n"), pngpath, error->message);
148 return 1;
149 }
150
151 if (!g_output_stream_close (G_OUTPUT_STREAM (out), NULL, error: &error))
152 {
153 g_printerr (_("Can’t close stream"));
154 return 1;
155 }
156
157 g_object_unref (object: out);
158 g_free (mem: pngpath);
159
160 return 0;
161}
162

source code of gtk/tools/encodesymbolic.c