| 1 |
#include <cairo/cairo.h> |
| 2 |
#include <gio/gio.h> |
| 3 |
#include <gdk-pixbuf/gdk-pixbuf.h> |
| 4 |
#include <gdk/gdk.h> |
| 5 |
#include <glib.h> |
| 6 |
#include <string.h> |
| 7 |
#include <math.h> |
| 8 |
|
| 9 |
GFile *gnome_dir = NULL; |
| 10 |
GFile *hc_dir = NULL; |
| 11 |
|
| 12 |
static const gint icon_sizes[] = { |
| 13 |
16, 22, 24, 32, 48, 256 |
| 14 |
}; |
| 15 |
|
| 16 |
static char * |
| 17 |
replace_str (char *str, |
| 18 |
const char *substr, |
| 19 |
const char *new_substr) |
| 20 |
{ |
| 21 |
static char buf[4096]; |
| 22 |
char *ptr; |
| 23 |
|
| 24 |
/* if we didn't find the substring, return */ |
| 25 |
if (!(ptr = (char*)strstr (str, substr))) |
| 26 |
return str; |
| 27 |
|
| 28 |
/* copy up to the substring */ |
| 29 |
strncpy (buf, str, ptr - str); |
| 30 |
buf[ptr - str] = '\0'; |
| 31 |
|
| 32 |
if (strlen (substr) >=strlen (new_substr)) |
| 33 |
{ |
| 34 |
sprintf (buf + (ptr - str), "%s%s", new_substr, ptr + strlen (substr)); |
| 35 |
} |
| 36 |
else |
| 37 |
{ |
| 38 |
static char buf2[4096]; |
| 39 |
|
| 40 |
strncpy (buf2, str, ptr - str + strlen (substr)); |
| 41 |
buf2[ptr - str + strlen (substr)] = '\0'; |
| 42 |
|
| 43 |
sprintf (buf2 + (ptr - str), "%s%s", new_substr, ptr + strlen (substr)); |
| 44 |
strncpy (buf, buf2, strlen (buf)); |
| 45 |
} |
| 46 |
|
| 47 |
return buf; |
| 48 |
} |
| 49 |
|
| 50 |
static gchar * |
| 51 |
ensure_dest_path (GFile *file, |
| 52 |
gint icon_size) |
| 53 |
{ |
| 54 |
gchar *str, *str2, *size_string, *dest_path; |
| 55 |
GFile *dest_file, *dest_dir, *tmp; |
| 56 |
|
| 57 |
str = g_file_get_relative_path (gnome_dir, file); |
| 58 |
tmp = g_file_resolve_relative_path (hc_dir, str); |
| 59 |
g_free (str); |
| 60 |
|
| 61 |
str = g_file_get_path (tmp); |
| 62 |
size_string = g_strdup_printf ("%dx%d", icon_size, icon_size); |
| 63 |
str2 = replace_str (str, "-symbolic.svg", ".png"); |
| 64 |
dest_path = replace_str (str2, "scalable", size_string); |
| 65 |
|
| 66 |
dest_file = g_file_new_for_path (dest_path); |
| 67 |
dest_dir = g_file_get_parent (dest_file); |
| 68 |
|
| 69 |
g_file_make_directory_with_parents (dest_dir, NULL, NULL); |
| 70 |
|
| 71 |
g_object_unref (dest_file); |
| 72 |
g_object_unref (dest_dir); |
| 73 |
g_object_unref (tmp); |
| 74 |
g_free (str); |
| 75 |
g_free (size_string); |
| 76 |
|
| 77 |
return dest_path; |
| 78 |
} |
| 79 |
|
| 80 |
static void |
| 81 |
optimize_png (const gchar *png_path) |
| 82 |
{ |
| 83 |
gchar *cmd = g_strconcat ("zopflipng -ym ", png_path, " ", png_path, NULL); |
| 84 |
g_spawn_command_line_async (cmd, NULL); |
| 85 |
g_free (cmd); |
| 86 |
} |
| 87 |
|
| 88 |
static GdkPixbuf * |
| 89 |
get_recolored_svg (GFile *file, |
| 90 |
gint icon_size) |
| 91 |
{ |
| 92 |
gchar *data; |
| 93 |
GdkPixbuf *pixbuf; |
| 94 |
GInputStream *stream; |
| 95 |
gchar *file_data, *escaped_file_data; |
| 96 |
gsize file_len; |
| 97 |
|
| 98 |
if (!g_file_load_contents (file, NULL, &file_data, &file_len, NULL, NULL)) |
| 99 |
return NULL; |
| 100 |
|
| 101 |
escaped_file_data = g_markup_escape_text (file_data, file_len); |
| 102 |
g_free (file_data); |
| 103 |
|
| 104 |
data = g_strconcat ("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n" |
| 105 |
"<svg version=\"1.1\"\n" |
| 106 |
" xmlns=\"http://www.w3.org/2000/svg\"\n" |
| 107 |
" xmlns:xi=\"http://www.w3.org/2001/XInclude\"\n" |
| 108 |
" width=\"16\"\n" |
| 109 |
" height=\"16\">\n" |
| 110 |
" <style type=\"text/css\">\n" |
| 111 |
" rect,path {\n" |
| 112 |
" fill: black !important;\n" |
| 113 |
" }\n" |
| 114 |
" .warning {\n" |
| 115 |
" fill: #f57900 !important;\n" |
| 116 |
" }\n" |
| 117 |
" .error {\n" |
| 118 |
" fill: #cc0000 !important;\n" |
| 119 |
" }\n" |
| 120 |
" .success {\n" |
| 121 |
" fill: #4e9a06 !important;\n" |
| 122 |
" }\n" |
| 123 |
" </style>\n" |
| 124 |
" <xi:include href=\"data:text/xml,", escaped_file_data, "\"/>\n" |
| 125 |
"</svg>", |
| 126 |
NULL); |
| 127 |
g_free (escaped_file_data); |
| 128 |
stream = g_memory_input_stream_new_from_data (data, -1, g_free); |
| 129 |
pixbuf = gdk_pixbuf_new_from_stream_at_scale (stream, |
| 130 |
icon_size, icon_size, |
| 131 |
TRUE, NULL, NULL); |
| 132 |
g_object_unref (stream); |
| 133 |
|
| 134 |
return pixbuf; |
| 135 |
} |
| 136 |
|
| 137 |
/* taken from gdkcairo.c */ |
| 138 |
static gboolean |
| 139 |
_gdk_cairo_surface_extents (cairo_surface_t *surface, |
| 140 |
GdkRectangle *extents) |
| 141 |
{ |
| 142 |
double x1, x2, y1, y2; |
| 143 |
cairo_t *cr; |
| 144 |
|
| 145 |
g_return_val_if_fail (surface != NULL, FALSE); |
| 146 |
g_return_val_if_fail (extents != NULL, FALSE); |
| 147 |
|
| 148 |
cr = cairo_create (surface); |
| 149 |
cairo_clip_extents (cr, &x1, &y1, &x2, &y2); |
| 150 |
cairo_destroy (cr); |
| 151 |
|
| 152 |
x1 = floor (x1); |
| 153 |
y1 = floor (y1); |
| 154 |
x2 = ceil (x2); |
| 155 |
y2 = ceil (y2); |
| 156 |
x2 -= x1; |
| 157 |
y2 -= y1; |
| 158 |
|
| 159 |
if (x1 < G_MININT || x1 > G_MAXINT || |
| 160 |
y1 < G_MININT || y1 > G_MAXINT || |
| 161 |
x2 > G_MAXINT || y2 > G_MAXINT) |
| 162 |
{ |
| 163 |
extents->x = extents->y = extents->width = extents->height = 0; |
| 164 |
return FALSE; |
| 165 |
} |
| 166 |
|
| 167 |
extents->x = x1; |
| 168 |
extents->y = y1; |
| 169 |
extents->width = x2; |
| 170 |
extents->height = y2; |
| 171 |
|
| 172 |
return TRUE; |
| 173 |
} |
| 174 |
|
| 175 |
/* This function originally from Jean-Edouard Lachand-Robert, and |
| 176 |
* available at www.codeguru.com. Simplified for our needs, not sure |
| 177 |
* how much of the original code left any longer. Now handles just |
| 178 |
* one-bit deep bitmaps (in Window parlance, ie those that GDK calls |
| 179 |
* bitmaps (and not pixmaps), with zero pixels being transparent. |
| 180 |
* |
| 181 |
* Changed again here from the GDK version to use an 8-bit surface instead |
| 182 |
* of a 1-bit bitmap. |
| 183 |
*/ |
| 184 |
static cairo_region_t * |
| 185 |
_gdk_cairo_region_create_from_surface (cairo_surface_t *surface) |
| 186 |
{ |
| 187 |
cairo_region_t *region; |
| 188 |
GdkRectangle extents, rect; |
| 189 |
cairo_surface_t *image; |
| 190 |
cairo_t *cr; |
| 191 |
gint x, y, stride; |
| 192 |
guchar *data; |
| 193 |
|
| 194 |
_gdk_cairo_surface_extents (surface, &extents); |
| 195 |
|
| 196 |
if (cairo_surface_get_content (surface) == CAIRO_CONTENT_COLOR) |
| 197 |
return cairo_region_create_rectangle (&extents); |
| 198 |
|
| 199 |
if (cairo_surface_get_type (surface) != CAIRO_SURFACE_TYPE_IMAGE || |
| 200 |
cairo_image_surface_get_format (surface) != CAIRO_FORMAT_A8) |
| 201 |
{ |
| 202 |
/* coerce to an A8 image */ |
| 203 |
image = cairo_image_surface_create (CAIRO_FORMAT_A8, |
| 204 |
extents.width, extents.height); |
| 205 |
cr = cairo_create (image); |
| 206 |
cairo_set_source_surface (cr, surface, -extents.x, -extents.y); |
| 207 |
cairo_paint (cr); |
| 208 |
cairo_destroy (cr); |
| 209 |
} |
| 210 |
else |
| 211 |
image = cairo_surface_reference (surface); |
| 212 |
|
| 213 |
data = cairo_image_surface_get_data (image); |
| 214 |
stride = cairo_image_surface_get_stride (image); |
| 215 |
|
| 216 |
region = cairo_region_create (); |
| 217 |
|
| 218 |
for (y = 0; y < extents.height; y++) |
| 219 |
{ |
| 220 |
for (x = 0; x < extents.width; x++) |
| 221 |
{ |
| 222 |
/* Search for a continuous range of "non transparent pixels"*/ |
| 223 |
gint x0 = x; |
| 224 |
while (x < extents.width) |
| 225 |
{ |
| 226 |
guint8 alpha = data[x]; |
| 227 |
if (alpha < 24) |
| 228 |
/* This pixel is "transparent"*/ |
| 229 |
break; |
| 230 |
x++; |
| 231 |
} |
| 232 |
|
| 233 |
if (x > x0) |
| 234 |
{ |
| 235 |
/* Add the pixels (x0, y) to (x, y+1) as a new rectangle |
| 236 |
* in the region |
| 237 |
*/ |
| 238 |
rect.x = x0; |
| 239 |
rect.width = x - x0; |
| 240 |
rect.y = y; |
| 241 |
rect.height = 1; |
| 242 |
|
| 243 |
cairo_region_union_rectangle (region, &rect); |
| 244 |
} |
| 245 |
} |
| 246 |
data += stride; |
| 247 |
} |
| 248 |
|
| 249 |
cairo_surface_destroy (image); |
| 250 |
|
| 251 |
cairo_region_translate (region, extents.x, extents.y); |
| 252 |
|
| 253 |
return region; |
| 254 |
} |
| 255 |
|
| 256 |
static void |
| 257 |
write_png_theme (GList *svg_files, |
| 258 |
gint icon_size) |
| 259 |
{ |
| 260 |
GList *l; |
| 261 |
|
| 262 |
g_print ("Writing size: %dx%d\n", icon_size, icon_size); |
| 263 |
|
| 264 |
for (l = svg_files; l != NULL; l = l->next) |
| 265 |
{ |
| 266 |
GFile *file; |
| 267 |
gchar *dest_path; |
| 268 |
GdkPixbuf *pixbuf; |
| 269 |
gint border_offset; |
| 270 |
cairo_surface_t *surface; |
| 271 |
cairo_region_t *region; |
| 272 |
cairo_t *cr; |
| 273 |
|
| 274 |
file = l->data; |
| 275 |
border_offset = (gint) floor (icon_size / 16); |
| 276 |
pixbuf = get_recolored_svg (file, icon_size - 2.0 * border_offset); |
| 277 |
|
| 278 |
surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, |
| 279 |
icon_size, icon_size); |
| 280 |
cr = cairo_create (surface); |
| 281 |
|
| 282 |
gdk_cairo_set_source_pixbuf (cr, pixbuf, |
| 283 |
border_offset, border_offset); |
| 284 |
cairo_paint (cr); |
| 285 |
cairo_destroy (cr); |
| 286 |
|
| 287 |
region = _gdk_cairo_region_create_from_surface (surface); |
| 288 |
cairo_surface_destroy (surface); |
| 289 |
|
| 290 |
surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, |
| 291 |
icon_size, icon_size); |
| 292 |
cr = cairo_create (surface); |
| 293 |
|
| 294 |
cairo_save (cr); |
| 295 |
gdk_cairo_region (cr, region); |
| 296 |
|
| 297 |
cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 1.0); |
| 298 |
cairo_set_line_width (cr, 2.0 * border_offset); |
| 299 |
cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND); |
| 300 |
cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND); |
| 301 |
|
| 302 |
cairo_stroke (cr); |
| 303 |
cairo_restore (cr); |
| 304 |
|
| 305 |
gdk_cairo_set_source_pixbuf (cr, pixbuf, |
| 306 |
border_offset, border_offset); |
| 307 |
cairo_paint (cr); |
| 308 |
|
| 309 |
dest_path = ensure_dest_path (file, icon_size); |
| 310 |
cairo_surface_write_to_png (surface, dest_path); |
| 311 |
|
| 312 |
cairo_destroy (cr); |
| 313 |
cairo_surface_destroy (surface); |
| 314 |
cairo_region_destroy (region); |
| 315 |
g_object_unref (pixbuf); |
| 316 |
|
| 317 |
optimize_png (dest_path); |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
static void |
| 322 |
process (int argc, |
| 323 |
char **argv) |
| 324 |
{ |
| 325 |
GList *symbolic_icons = NULL; |
| 326 |
GQueue *descend_into_files; |
| 327 |
GFile *current_dir, *symbolic_theme, *file; |
| 328 |
gchar *str; |
| 329 |
gint idx; |
| 330 |
|
| 331 |
str = g_get_current_dir (); |
| 332 |
current_dir = g_file_new_for_path (str); |
| 333 |
g_free (str); |
| 334 |
|
| 335 |
symbolic_theme = g_file_new_for_commandline_arg (argv[1]); |
| 336 |
gnome_dir = g_file_resolve_relative_path (symbolic_theme, "Adwaita"); |
| 337 |
hc_dir = g_file_resolve_relative_path (current_dir, "icons"); |
| 338 |
g_object_unref (symbolic_theme); |
| 339 |
|
| 340 |
descend_into_files = g_queue_new (); |
| 341 |
g_queue_push_tail (descend_into_files, g_object_ref (gnome_dir)); |
| 342 |
while ((file = g_queue_pop_head (descend_into_files)) != NULL) |
| 343 |
{ |
| 344 |
GFileInfo *child_info; |
| 345 |
GFileEnumerator *enumerator = |
| 346 |
g_file_enumerate_children (file, "standard::name,standard::type,standard::content-type", |
| 347 |
G_FILE_QUERY_INFO_NONE, NULL, NULL); |
| 348 |
|
| 349 |
while ((child_info = g_file_enumerator_next_file (enumerator, NULL, NULL)) != NULL) |
| 350 |
{ |
| 351 |
if (g_file_info_get_file_type (child_info) == G_FILE_TYPE_DIRECTORY) |
| 352 |
g_queue_push_tail (descend_into_files, g_file_resolve_relative_path (file, g_file_info_get_name (child_info))); |
| 353 |
else if (g_content_type_is_a (g_file_info_get_content_type (child_info), "image/svg+xml") && |
| 354 |
strstr (g_file_info_get_name (child_info), "-symbolic.svg")) |
| 355 |
symbolic_icons = g_list_prepend (symbolic_icons, g_file_resolve_relative_path (file, g_file_info_get_name (child_info))); |
| 356 |
|
| 357 |
g_object_unref (child_info); |
| 358 |
} |
| 359 |
|
| 360 |
g_object_unref (enumerator); |
| 361 |
g_object_unref (file); |
| 362 |
} |
| 363 |
|
| 364 |
for (idx = 0; idx < G_N_ELEMENTS (icon_sizes); idx++) |
| 365 |
write_png_theme (symbolic_icons, icon_sizes[idx]); |
| 366 |
|
| 367 |
g_list_free_full (symbolic_icons, g_object_unref); |
| 368 |
g_queue_free (descend_into_files); |
| 369 |
g_clear_object (&gnome_dir); |
| 370 |
g_clear_object (&hc_dir); |
| 371 |
} |
| 372 |
|
| 373 |
int |
| 374 |
main (int argc, |
| 375 |
char **argv) |
| 376 |
{ |
| 377 |
if (argc == 1) |
| 378 |
{ |
| 379 |
g_critical ("Location of adwaita-icon-theme repo must be given"); |
| 380 |
return 0; |
| 381 |
} |
| 382 |
|
| 383 |
process (argc, argv); |
| 384 |
g_spawn_command_line_async ("./create-makefiles.sh", NULL); |
| 385 |
|
| 386 |
return 0; |
| 387 |
} |