]> Pileus Git - ~andy/gtk/blob - gtk/gtkcssimageurl.c
Ensure we can load images via resource:// uris from CSS
[~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 "gtkcssimageurlprivate.h"
24
25 #include "gtkcssprovider.h"
26
27 G_DEFINE_TYPE (GtkCssImageUrl, _gtk_css_image_url, GTK_TYPE_CSS_IMAGE)
28
29 static int
30 gtk_css_image_url_get_width (GtkCssImage *image)
31 {
32   GtkCssImageUrl *url = GTK_CSS_IMAGE_URL (image);
33
34   return cairo_image_surface_get_width (url->surface);
35 }
36
37 static int
38 gtk_css_image_url_get_height (GtkCssImage *image)
39 {
40   GtkCssImageUrl *url = GTK_CSS_IMAGE_URL (image);
41
42   return cairo_image_surface_get_height (url->surface);
43 }
44
45 static void
46 gtk_css_image_url_draw (GtkCssImage        *image,
47                         cairo_t            *cr,
48                         double              width,
49                         double              height)
50 {
51   GtkCssImageUrl *url = GTK_CSS_IMAGE_URL (image);
52
53   cairo_rectangle (cr, 0, 0, width, height);
54   cairo_scale (cr,
55                width / cairo_image_surface_get_width (url->surface),
56                height / cairo_image_surface_get_height (url->surface));
57   cairo_set_source_surface (cr, url->surface, 0, 0);
58   cairo_pattern_set_extend (cairo_get_source (cr), CAIRO_EXTEND_PAD);
59   cairo_fill (cr);
60 }
61
62 static gboolean
63 gtk_css_image_url_parse (GtkCssImage  *image,
64                          GtkCssParser *parser,
65                          GFile        *base)
66 {
67   GtkCssImageUrl *url = GTK_CSS_IMAGE_URL (image);
68   GdkPixbuf *pixbuf;
69   GFile *file;
70   cairo_t *cr;
71   GError *error = NULL;
72   GFileInputStream *input;
73
74   file = _gtk_css_parser_read_url (parser, base);
75   if (file == NULL)
76     return FALSE;
77
78   input = g_file_read (file, NULL, &error);
79   if (input == NULL)
80     {
81       _gtk_css_parser_take_error (parser, error);
82       return FALSE;
83     }
84   g_object_unref (file);
85
86   pixbuf = gdk_pixbuf_new_from_stream (G_INPUT_STREAM (input), NULL, &error);
87   g_object_unref (input);
88
89   if (pixbuf == NULL)
90     {
91       _gtk_css_parser_take_error (parser, error);
92       return FALSE;
93     }
94
95   url->surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
96                                              gdk_pixbuf_get_width (pixbuf),
97                                              gdk_pixbuf_get_height (pixbuf));
98   cr = cairo_create (url->surface);
99   gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0);
100   cairo_paint (cr);
101   cairo_destroy (cr);
102   g_object_unref (pixbuf);
103
104   return TRUE;
105 }
106
107 static cairo_status_t
108 surface_write (void                *closure,
109                const unsigned char *data,
110                unsigned int         length)
111 {
112   g_byte_array_append (closure, data, length);
113
114   return CAIRO_STATUS_SUCCESS;
115 }
116
117 static void
118 gtk_css_image_url_print (GtkCssImage *image,
119                          GString     *string)
120 {
121 #if CAIRO_HAS_PNG_FUNCTIONS
122   GtkCssImageUrl *url = GTK_CSS_IMAGE_URL (image);
123   GByteArray *array;
124   char *base64;
125   
126   array = g_byte_array_new ();
127   cairo_surface_write_to_png_stream (url->surface, surface_write, array);
128   base64 = g_base64_encode (array->data, array->len);
129   g_byte_array_free (array, TRUE);
130
131   g_string_append (string, "url(\"data:image/png;base64,");
132   g_string_append (string, base64);
133   g_string_append (string, "\")");
134
135   g_free (base64);
136 #else
137   g_string_append (string, "none /* you need cairo png functions enabled to make this work */");
138 #endif
139 }
140
141 static void
142 gtk_css_image_url_dispose (GObject *object)
143 {
144   GtkCssImageUrl *url = GTK_CSS_IMAGE_URL (object);
145
146   if (url->surface)
147     {
148       cairo_surface_destroy (url->surface);
149       url->surface = NULL;
150     }
151
152   G_OBJECT_CLASS (_gtk_css_image_url_parent_class)->dispose (object);
153 }
154
155 static void
156 _gtk_css_image_url_class_init (GtkCssImageUrlClass *klass)
157 {
158   GtkCssImageClass *image_class = GTK_CSS_IMAGE_CLASS (klass);
159   GObjectClass *object_class = G_OBJECT_CLASS (klass);
160
161   image_class->get_width = gtk_css_image_url_get_width;
162   image_class->get_height = gtk_css_image_url_get_height;
163   image_class->draw = gtk_css_image_url_draw;
164   image_class->parse = gtk_css_image_url_parse;
165   image_class->print = gtk_css_image_url_print;
166
167   object_class->dispose = gtk_css_image_url_dispose;
168 }
169
170 static void
171 _gtk_css_image_url_init (GtkCssImageUrl *image_url)
172 {
173 }
174