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