]> Pileus Git - ~andy/gtk/blob - gtk/gtkcssimageurl.c
Special case resource pixbuf files loading
[~andy/gtk] / gtk / gtkcssimageurl.c
1 /*
2  * Copyright © 2011 Red Hat Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 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  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  *
18  * Authors: Benjamin Otte <otte@gnome.org>
19  */
20
21 #include "config.h"
22
23 #include <string.h>
24
25 #include "gtkcssimageurlprivate.h"
26
27 #include "gtkcssprovider.h"
28
29 G_DEFINE_TYPE (GtkCssImageUrl, _gtk_css_image_url, GTK_TYPE_CSS_IMAGE)
30
31 static int
32 gtk_css_image_url_get_width (GtkCssImage *image)
33 {
34   GtkCssImageUrl *url = GTK_CSS_IMAGE_URL (image);
35
36   return cairo_image_surface_get_width (url->surface);
37 }
38
39 static int
40 gtk_css_image_url_get_height (GtkCssImage *image)
41 {
42   GtkCssImageUrl *url = GTK_CSS_IMAGE_URL (image);
43
44   return cairo_image_surface_get_height (url->surface);
45 }
46
47 static void
48 gtk_css_image_url_draw (GtkCssImage        *image,
49                         cairo_t            *cr,
50                         double              width,
51                         double              height)
52 {
53   GtkCssImageUrl *url = GTK_CSS_IMAGE_URL (image);
54
55   cairo_rectangle (cr, 0, 0, width, height);
56   cairo_scale (cr,
57                width / cairo_image_surface_get_width (url->surface),
58                height / cairo_image_surface_get_height (url->surface));
59   cairo_set_source_surface (cr, url->surface, 0, 0);
60   cairo_pattern_set_extend (cairo_get_source (cr), CAIRO_EXTEND_PAD);
61   cairo_fill (cr);
62 }
63
64 static gboolean
65 gtk_css_image_url_parse (GtkCssImage  *image,
66                          GtkCssParser *parser,
67                          GFile        *base)
68 {
69   GtkCssImageUrl *url = GTK_CSS_IMAGE_URL (image);
70   GdkPixbuf *pixbuf;
71   GFile *file;
72   cairo_t *cr;
73   GError *error = NULL;
74   GFileInputStream *input;
75
76   file = _gtk_css_parser_read_url (parser, base);
77   if (file == NULL)
78     return FALSE;
79
80   /* We special case resources here so we can use
81      gdk_pixbuf_new_from_resource, which in turn has some special casing
82      for GdkPixdata files to avoid duplicating the memory for the pixbufs */
83   if (g_file_has_uri_scheme (file, "resource"))
84     {
85       char *uri = g_file_get_uri (file);
86       char *resource_path = g_uri_unescape_string (uri + strlen ("resource://"), NULL);
87       g_print ("uri: %s, resource_path: %s\n", uri, resource_path);
88
89       pixbuf = gdk_pixbuf_new_from_resource (resource_path, &error);
90       g_free (resource_path);
91       g_free (uri);
92     }
93   else
94     {
95       input = g_file_read (file, NULL, &error);
96       if (input == NULL)
97         {
98           _gtk_css_parser_take_error (parser, error);
99           return FALSE;
100         }
101       g_object_unref (file);
102
103       pixbuf = gdk_pixbuf_new_from_stream (G_INPUT_STREAM (input), NULL, &error);
104       g_object_unref (input);
105     }
106
107   if (pixbuf == NULL)
108     {
109       _gtk_css_parser_take_error (parser, error);
110       return FALSE;
111     }
112
113   url->surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
114                                              gdk_pixbuf_get_width (pixbuf),
115                                              gdk_pixbuf_get_height (pixbuf));
116   cr = cairo_create (url->surface);
117   gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0);
118   cairo_paint (cr);
119   cairo_destroy (cr);
120   g_object_unref (pixbuf);
121
122   return TRUE;
123 }
124
125 static cairo_status_t
126 surface_write (void                *closure,
127                const unsigned char *data,
128                unsigned int         length)
129 {
130   g_byte_array_append (closure, data, length);
131
132   return CAIRO_STATUS_SUCCESS;
133 }
134
135 static void
136 gtk_css_image_url_print (GtkCssImage *image,
137                          GString     *string)
138 {
139 #if CAIRO_HAS_PNG_FUNCTIONS
140   GtkCssImageUrl *url = GTK_CSS_IMAGE_URL (image);
141   GByteArray *array;
142   char *base64;
143   
144   array = g_byte_array_new ();
145   cairo_surface_write_to_png_stream (url->surface, surface_write, array);
146   base64 = g_base64_encode (array->data, array->len);
147   g_byte_array_free (array, TRUE);
148
149   g_string_append (string, "url(\"data:image/png;base64,");
150   g_string_append (string, base64);
151   g_string_append (string, "\")");
152
153   g_free (base64);
154 #else
155   g_string_append (string, "none /* you need cairo png functions enabled to make this work */");
156 #endif
157 }
158
159 static void
160 gtk_css_image_url_dispose (GObject *object)
161 {
162   GtkCssImageUrl *url = GTK_CSS_IMAGE_URL (object);
163
164   if (url->surface)
165     {
166       cairo_surface_destroy (url->surface);
167       url->surface = NULL;
168     }
169
170   G_OBJECT_CLASS (_gtk_css_image_url_parent_class)->dispose (object);
171 }
172
173 static void
174 _gtk_css_image_url_class_init (GtkCssImageUrlClass *klass)
175 {
176   GtkCssImageClass *image_class = GTK_CSS_IMAGE_CLASS (klass);
177   GObjectClass *object_class = G_OBJECT_CLASS (klass);
178
179   image_class->get_width = gtk_css_image_url_get_width;
180   image_class->get_height = gtk_css_image_url_get_height;
181   image_class->draw = gtk_css_image_url_draw;
182   image_class->parse = gtk_css_image_url_parse;
183   image_class->print = gtk_css_image_url_print;
184
185   object_class->dispose = gtk_css_image_url_dispose;
186 }
187
188 static void
189 _gtk_css_image_url_init (GtkCssImageUrl *image_url)
190 {
191 }
192