Make the recoloring code more robust

Pass both width+height and scale, so we can handle the case
where width+height are zero and we are using the file, scaled.
This commit is contained in:
Matthias Clasen 2017-11-07 21:27:38 -05:00
parent 6ebd2d384b
commit 1da7dc890c
4 changed files with 28 additions and 10 deletions

View File

@ -104,7 +104,7 @@ main (int argc, char **argv)
return 1;
}
symbolic = gtk_make_symbolic_pixbuf_from_data (data, len, width, height, &error);
symbolic = gtk_make_symbolic_pixbuf_from_data (data, len, width, height, 1.0, &error);
if (symbolic == NULL)
{
g_printerr (_("Cant load file: %s\n"), error->message);

View File

@ -136,6 +136,7 @@ load_symbolic_svg (const char *file_data,
gsize file_len,
int width,
int height,
double scale,
const GdkRGBA *fg,
const GdkRGBA *success_color,
const GdkRGBA *warning_color,
@ -168,8 +169,13 @@ load_symbolic_svg (const char *file_data,
if (!pixbuf)
return NULL;
if (width == 0)
width = gdk_pixbuf_get_width (pixbuf) * scale;
if (height == 0)
height = gdk_pixbuf_get_height (pixbuf) * scale;
svg_width = g_strdup_printf ("%d", gdk_pixbuf_get_width (pixbuf));
svg_height = g_strdup_printf ("%d",gdk_pixbuf_get_height (pixbuf));
svg_height = g_strdup_printf ("%d", gdk_pixbuf_get_height (pixbuf));
g_object_unref (pixbuf);
escaped_file_data = g_markup_escape_text (file_data, file_len);
@ -253,18 +259,15 @@ gtk_make_symbolic_pixbuf_from_data (const char *file_data,
gsize file_len,
int width,
int height,
double scale,
GError **error)
{
GdkRGBA r = { 1,0,0,1}, g = {0,1,0,1};
GdkPixbuf *loaded;
GdkPixbuf *pixbuf;
GdkPixbuf *pixbuf = NULL;
int plane;
pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, width, height);
gdk_pixbuf_fill (pixbuf, 0);
for (plane = 0; plane < 3; plane++)
{
/* Here we render the svg with all colors solid, this should
@ -279,7 +282,7 @@ gtk_make_symbolic_pixbuf_from_data (const char *file_data,
* channels, with the color of the fg being implicitly
* the "rest", as all color fractions should add up to 1.
*/
loaded = load_symbolic_svg (file_data, file_len, width, height,
loaded = load_symbolic_svg (file_data, file_len, width, height, scale,
&g,
plane == 0 ? &r : &g,
plane == 1 ? &r : &g,
@ -288,6 +291,14 @@ gtk_make_symbolic_pixbuf_from_data (const char *file_data,
if (loaded == NULL)
return NULL;
if (pixbuf == NULL)
{
pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8,
gdk_pixbuf_get_width (loaded),
gdk_pixbuf_get_height (loaded));
gdk_pixbuf_fill (pixbuf, 0);
}
if (plane == 0)
extract_plane (loaded, pixbuf, 3, 3);
@ -303,6 +314,7 @@ GdkPixbuf *
gtk_make_symbolic_pixbuf_from_resource (const char *path,
int width,
int height,
double scale,
GError **error)
{
GBytes *bytes;
@ -316,7 +328,7 @@ gtk_make_symbolic_pixbuf_from_resource (const char *path,
data = g_bytes_get_data (bytes, &size);
pixbuf = gtk_make_symbolic_pixbuf_from_data (data, size, width, height, error);
pixbuf = gtk_make_symbolic_pixbuf_from_data (data, size, width, height, scale, error);
g_bytes_unref (bytes);
@ -327,6 +339,7 @@ GdkPixbuf *
gtk_make_symbolic_pixbuf_from_file (GFile *file,
int width,
int height,
double scale,
GError **error)
{
char *data;
@ -336,7 +349,7 @@ gtk_make_symbolic_pixbuf_from_file (GFile *file,
if (!g_file_load_contents (file, NULL, &data, &size, NULL, error))
return NULL;
pixbuf = gtk_make_symbolic_pixbuf_from_data (data, size, width, height, error);
pixbuf = gtk_make_symbolic_pixbuf_from_data (data, size, width, height, scale, error);
g_free (data);

View File

@ -34,14 +34,17 @@ GdkPixbuf *gtk_make_symbolic_pixbuf_from_data (const char *data,
gsize len,
int width,
int height,
double scale,
GError **error);
GdkPixbuf *gtk_make_symbolic_pixbuf_from_file (GFile *file,
int width,
int height,
double scale,
GError **error);
GdkPixbuf *gtk_make_symbolic_pixbuf_from_resource (const char *path,
int width,
int height,
double scale,
GError **error);
G_END_DECLS

View File

@ -3796,6 +3796,7 @@ icon_info_ensure_scale_and_pixbuf (GtkIconInfo *icon_info)
if (gtk_icon_info_is_symbolic (icon_info))
source_pixbuf = gtk_make_symbolic_pixbuf_from_resource (icon_info->filename,
size, size,
icon_info->desired_scale,
&icon_info->load_error);
else if (size == 0)
source_pixbuf = _gdk_pixbuf_new_from_resource_scaled (icon_info->filename,
@ -3836,6 +3837,7 @@ icon_info_ensure_scale_and_pixbuf (GtkIconInfo *icon_info)
if (gtk_icon_info_is_symbolic (icon_info) && icon_info->icon_file)
source_pixbuf = gtk_make_symbolic_pixbuf_from_file (icon_info->icon_file,
size, size,
icon_info->desired_scale,
&icon_info->load_error);
else if (size == 0)
source_pixbuf = _gdk_pixbuf_new_from_stream_scaled (stream,