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-pixbuf/gdk-pixdata.h>
22 #include <gdk/gdk.h>
23 #include <glib/gi18n.h>
24 
25 #ifdef HAVE_UNISTD_H
26 #include <unistd.h>
27 #endif
28 #ifdef G_OS_WIN32
29 #include <io.h>
30 #endif
31 #include <errno.h>
32 #include <stdlib.h>
33 #include <locale.h>
34 
35 static gchar *output_dir = NULL;
36 
37 static GOptionEntry args[] = {
38   { "output", 'o', 0, G_OPTION_ARG_FILENAME, &output_dir, N_("Output to this directory instead of cwd"), NULL },
39   { NULL }
40 };
41 
42 static GdkPixbuf *
load_symbolic_svg(char * file_data,gsize file_len,int width,int height,const GdkRGBA * fg,const GdkRGBA * success_color,const GdkRGBA * warning_color,const GdkRGBA * error_color,GError ** error)43 load_symbolic_svg (char *file_data, gsize file_len,
44                    int width,
45                    int height,
46                    const GdkRGBA  *fg,
47                    const GdkRGBA  *success_color,
48                    const GdkRGBA  *warning_color,
49                    const GdkRGBA  *error_color,
50                    GError        **error)
51 {
52   GInputStream *stream;
53   GdkPixbuf *pixbuf;
54   gchar *css_fg;
55   gchar *css_success;
56   gchar *css_warning;
57   gchar *css_error;
58   gchar *data;
59   gchar *svg_width, *svg_height;
60   gchar *escaped_file_data;
61 
62   css_fg = gdk_rgba_to_string (fg);
63 
64   css_success = css_warning = css_error = NULL;
65 
66   css_warning = gdk_rgba_to_string (warning_color);
67   css_error = gdk_rgba_to_string (error_color);
68   css_success = gdk_rgba_to_string (success_color);
69 
70   /* Fetch size from the original icon */
71   stream = g_memory_input_stream_new_from_data (file_data, file_len, NULL);
72   pixbuf = gdk_pixbuf_new_from_stream (stream, NULL, error);
73   g_object_unref (stream);
74 
75   if (!pixbuf)
76     return NULL;
77 
78   svg_width = g_strdup_printf ("%d", gdk_pixbuf_get_width (pixbuf));
79   svg_height = g_strdup_printf ("%d",gdk_pixbuf_get_height (pixbuf));
80   g_object_unref (pixbuf);
81 
82   escaped_file_data = g_base64_encode ((guchar *) file_data, file_len);
83 
84   data = g_strconcat ("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n"
85                       "<svg version=\"1.1\"\n"
86                       "     xmlns=\"http://www.w3.org/2000/svg\"\n"
87                       "     xmlns:xi=\"http://www.w3.org/2001/XInclude\"\n"
88                       "     width=\"", svg_width, "\"\n"
89                       "     height=\"", svg_height, "\">\n"
90                       "  <style type=\"text/css\">\n"
91                       "    rect,circle,path {\n"
92                       "      fill: ", css_fg," !important;\n"
93                       "    }\n"
94                       "    .warning {\n"
95                       "      fill: ", css_warning, " !important;\n"
96                       "    }\n"
97                       "    .error {\n"
98                       "      fill: ", css_error ," !important;\n"
99                       "    }\n"
100                       "    .success {\n"
101                       "      fill: ", css_success, " !important;\n"
102                       "    }\n"
103                       "  </style>\n"
104                       "  <xi:include href=\"data:text/xml;base64,", escaped_file_data, "\"/>\n"
105                       "</svg>",
106                       NULL);
107   g_free (escaped_file_data);
108   g_free (css_fg);
109   g_free (css_warning);
110   g_free (css_error);
111   g_free (css_success);
112   g_free (svg_width);
113   g_free (svg_height);
114 
115   stream = g_memory_input_stream_new_from_data (data, -1, g_free);
116   pixbuf = gdk_pixbuf_new_from_stream_at_scale (stream,
117                                                 width,
118                                                 height,
119                                                 TRUE,
120                                                 NULL,
121                                                 error);
122   g_object_unref (stream);
123 
124   return pixbuf;
125 }
126 
127 static void
extract_plane(GdkPixbuf * src,GdkPixbuf * dst,int from_plane,int to_plane)128 extract_plane (GdkPixbuf *src,
129                GdkPixbuf *dst,
130                int from_plane,
131                int to_plane)
132 {
133   guchar *src_data, *dst_data;
134   int width, height, src_stride, dst_stride;
135   guchar *src_row, *dst_row;
136   int x, y;
137 
138   width = gdk_pixbuf_get_width (src);
139   height = gdk_pixbuf_get_height (src);
140 
141   g_assert (width <= gdk_pixbuf_get_width (dst));
142   g_assert (height <= gdk_pixbuf_get_height (dst));
143 
144   src_stride = gdk_pixbuf_get_rowstride (src);
145   src_data = gdk_pixbuf_get_pixels (src);
146 
147   dst_data = gdk_pixbuf_get_pixels (dst);
148   dst_stride = gdk_pixbuf_get_rowstride (dst);
149 
150   for (y = 0; y < height; y++)
151     {
152       src_row = src_data + src_stride * y;
153       dst_row = dst_data + dst_stride * y;
154       for (x = 0; x < width; x++)
155         {
156           dst_row[to_plane] = src_row[from_plane];
157           src_row += 4;
158           dst_row += 4;
159         }
160     }
161 }
162 
163 static GdkPixbuf *
make_symbolic_pixbuf(char * file,int width,int height,GError ** error)164 make_symbolic_pixbuf (char *file,
165                       int width,
166                       int height,
167                       GError        **error)
168 
169 {
170   GdkRGBA r = { 1,0,0,1}, g = {0,1,0,1};
171   GdkPixbuf *loaded;
172   GdkPixbuf *pixbuf;
173   int plane;
174   gchar *file_data;
175   gsize file_len;
176 
177   if (!g_file_get_contents (file, &file_data, &file_len, error))
178     return NULL;
179 
180   pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, width, height);
181 
182   gdk_pixbuf_fill (pixbuf, 0);
183 
184   for (plane = 0; plane < 3; plane++)
185     {
186       /* Here we render the svg with all colors solid, this should
187        * always make the alpha channel the same and it should match
188        * the final alpha channel for all possible renderings. We
189        * Just use it as-is for final alpha.
190        *
191        * For the 3 non-fg colors, we render once each with that
192        * color as red, and every other color as green. The resulting
193        * red will describe the amount of that color is in the
194        * opaque part of the color. We store these as the rgb
195        * channels, with the color of the fg being implicitly
196        * the "rest", as all color fractions should add up to 1.
197        */
198       loaded = load_symbolic_svg (file_data, file_len, width, height,
199                                   &g,
200                                   plane == 0 ? &r : &g,
201                                   plane == 1 ? &r : &g,
202                                   plane == 2 ? &r : &g,
203                                   error);
204       if (loaded == NULL)
205         return NULL;
206 
207       if (plane == 0)
208         extract_plane (loaded, pixbuf, 3, 3);
209 
210       extract_plane (loaded, pixbuf, 0, plane);
211 
212       g_object_unref (loaded);
213     }
214 
215   g_free (file_data);
216 
217   return pixbuf;
218 }
219 
220 int
main(int argc,char ** argv)221 main (int argc, char **argv)
222 {
223   gchar *path, *basename, *pngpath, *pngfile, *dot;
224   GOptionContext *context;
225   GdkPixbuf *symbolic;
226   GError *error;
227   int width, height;
228   gchar **sizev;
229   GFileOutputStream *out;
230   GFile *dest;
231 
232   setlocale (LC_ALL, "");
233 
234 #ifdef ENABLE_NLS
235   bindtextdomain (GETTEXT_PACKAGE, GTK_LOCALEDIR);
236 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
237   bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
238 #endif
239 #endif
240 
241   g_set_prgname ("gtk-encode-symbolic-svg");
242 
243   context = g_option_context_new ("PATH WIDTHxHEIGHT");
244   g_option_context_add_main_entries (context, args, GETTEXT_PACKAGE);
245 
246   g_option_context_parse (context, &argc, &argv, NULL);
247 
248   if (argc < 3)
249     {
250       g_printerr ("%s\n", g_option_context_get_help (context, FALSE, NULL));
251       return 1;
252     }
253 
254   width = 0;
255   height = 0;
256   sizev = g_strsplit (argv[2], "x", 0);
257  if (g_strv_length (sizev) == 2)
258     {
259       width = atoi(sizev[0]);
260       height = atoi(sizev[1]);
261     }
262  g_strfreev (sizev);
263 
264   if (width == 0 || height == 0)
265     {
266       g_printerr (_("Invalid size %s\n"), argv[2]);
267       return 1;
268     }
269 
270   path = argv[1];
271 #ifdef G_OS_WIN32
272   path = g_locale_to_utf8 (path, -1, NULL, NULL, NULL);
273 #endif
274 
275   error = NULL;
276   symbolic = make_symbolic_pixbuf (path, width, height, &error);
277   if (symbolic == NULL)
278     {
279       g_printerr (_("Can't load file: %s\n"), error->message);
280       return 1;
281     }
282 
283   basename = g_path_get_basename (path);
284 
285   dot = strrchr (basename, '.');
286   if (dot != NULL)
287     *dot = 0;
288   pngfile = g_strconcat (basename, ".symbolic.png", NULL);
289   g_free (basename);
290 
291   if (output_dir != NULL)
292     pngpath = g_build_filename (output_dir, pngfile, NULL);
293   else
294     pngpath = g_strdup (pngfile);
295 
296   g_free (pngfile);
297 
298   dest = g_file_new_for_path (pngpath);
299 
300 
301   out = g_file_replace (dest,
302 			NULL, FALSE,
303 			G_FILE_CREATE_REPLACE_DESTINATION,
304 			NULL, &error);
305   if (out == NULL)
306     {
307       g_printerr (_("Can't save file %s: %s\n"), pngpath, error->message);
308       return 1;
309     }
310 
311   if (!gdk_pixbuf_save_to_stream (symbolic, G_OUTPUT_STREAM (out), "png", NULL, &error, NULL))
312     {
313       g_printerr (_("Can't save file %s: %s\n"), pngpath, error->message);
314       return 1;
315     }
316 
317   if (!g_output_stream_close (G_OUTPUT_STREAM (out), NULL, &error))
318     {
319       g_printerr (_("Can't close stream"));
320       return 1;
321     }
322 
323   g_object_unref (out);
324   g_free (pngpath);
325 
326   return 0;
327 }
328