2 * Copyright © 2011 Red Hat Inc.
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.
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.
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/>.
17 * Authors: Benjamin Otte <otte@gnome.org>
24 #include "gtkcssimageurlprivate.h"
26 #include "gtkcssprovider.h"
28 G_DEFINE_TYPE (GtkCssImageUrl, _gtk_css_image_url, GTK_TYPE_CSS_IMAGE)
31 gtk_css_image_url_get_width (GtkCssImage *image)
33 GtkCssImageUrl *url = GTK_CSS_IMAGE_URL (image);
35 return cairo_image_surface_get_width (url->surface);
39 gtk_css_image_url_get_height (GtkCssImage *image)
41 GtkCssImageUrl *url = GTK_CSS_IMAGE_URL (image);
43 return cairo_image_surface_get_height (url->surface);
47 gtk_css_image_url_draw (GtkCssImage *image,
52 GtkCssImageUrl *url = GTK_CSS_IMAGE_URL (image);
54 cairo_rectangle (cr, 0, 0, width, height);
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);
64 gtk_css_image_url_parse (GtkCssImage *image,
68 GtkCssImageUrl *url = GTK_CSS_IMAGE_URL (image);
73 GFileInputStream *input;
75 file = _gtk_css_parser_read_url (parser, base);
79 /* We special case resources here so we can use
80 gdk_pixbuf_new_from_resource, which in turn has some special casing
81 for GdkPixdata files to avoid duplicating the memory for the pixbufs */
82 if (g_file_has_uri_scheme (file, "resource"))
84 char *uri = g_file_get_uri (file);
85 char *resource_path = g_uri_unescape_string (uri + strlen ("resource://"), NULL);
87 pixbuf = gdk_pixbuf_new_from_resource (resource_path, &error);
88 g_free (resource_path);
93 input = g_file_read (file, NULL, &error);
96 _gtk_css_parser_take_error (parser, error);
100 pixbuf = gdk_pixbuf_new_from_stream (G_INPUT_STREAM (input), NULL, &error);
101 g_object_unref (input);
103 g_object_unref (file);
107 _gtk_css_parser_take_error (parser, error);
111 url->surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
112 gdk_pixbuf_get_width (pixbuf),
113 gdk_pixbuf_get_height (pixbuf));
114 cr = cairo_create (url->surface);
115 gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0);
118 g_object_unref (pixbuf);
123 static cairo_status_t
124 surface_write (void *closure,
125 const unsigned char *data,
128 g_byte_array_append (closure, data, length);
130 return CAIRO_STATUS_SUCCESS;
134 gtk_css_image_url_print (GtkCssImage *image,
137 #if CAIRO_HAS_PNG_FUNCTIONS
138 GtkCssImageUrl *url = GTK_CSS_IMAGE_URL (image);
142 array = g_byte_array_new ();
143 cairo_surface_write_to_png_stream (url->surface, surface_write, array);
144 base64 = g_base64_encode (array->data, array->len);
145 g_byte_array_free (array, TRUE);
147 g_string_append (string, "url(\"data:image/png;base64,");
148 g_string_append (string, base64);
149 g_string_append (string, "\")");
153 g_string_append (string, "none /* you need cairo png functions enabled to make this work */");
158 gtk_css_image_url_dispose (GObject *object)
160 GtkCssImageUrl *url = GTK_CSS_IMAGE_URL (object);
164 cairo_surface_destroy (url->surface);
168 G_OBJECT_CLASS (_gtk_css_image_url_parent_class)->dispose (object);
172 _gtk_css_image_url_class_init (GtkCssImageUrlClass *klass)
174 GtkCssImageClass *image_class = GTK_CSS_IMAGE_CLASS (klass);
175 GObjectClass *object_class = G_OBJECT_CLASS (klass);
177 image_class->get_width = gtk_css_image_url_get_width;
178 image_class->get_height = gtk_css_image_url_get_height;
179 image_class->draw = gtk_css_image_url_draw;
180 image_class->parse = gtk_css_image_url_parse;
181 image_class->print = gtk_css_image_url_print;
183 object_class->dispose = gtk_css_image_url_dispose;
187 _gtk_css_image_url_init (GtkCssImageUrl *image_url)