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,
67 GtkCssImageUrl *url = GTK_CSS_IMAGE_URL (image);
71 GFileInputStream *input;
73 url->file = _gtk_css_parser_read_url (parser);
74 if (url->file == NULL)
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"))
82 char *uri = g_file_get_uri (url->file);
83 char *resource_path = g_uri_unescape_string (uri + strlen ("resource://"), NULL);
85 pixbuf = gdk_pixbuf_new_from_resource (resource_path, &error);
86 g_free (resource_path);
91 input = g_file_read (url->file, NULL, &error);
94 _gtk_css_parser_take_error (parser, error);
98 pixbuf = gdk_pixbuf_new_from_stream (G_INPUT_STREAM (input), NULL, &error);
99 g_object_unref (input);
104 _gtk_css_parser_take_error (parser, error);
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);
115 g_object_unref (pixbuf);
120 static cairo_status_t
121 surface_write (void *closure,
122 const unsigned char *data,
125 g_byte_array_append (closure, data, length);
127 return CAIRO_STATUS_SUCCESS;
131 gtk_css_image_url_print (GtkCssImage *image,
134 #if CAIRO_HAS_PNG_FUNCTIONS
135 GtkCssImageUrl *url = GTK_CSS_IMAGE_URL (image);
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);
144 g_string_append (string, "url(\"data:image/png;base64,");
145 g_string_append (string, base64);
146 g_string_append (string, "\")");
150 g_string_append (string, "none /* you need cairo png functions enabled to make this work */");
155 gtk_css_image_url_dispose (GObject *object)
157 GtkCssImageUrl *url = GTK_CSS_IMAGE_URL (object);
159 g_clear_object (&url->file);
163 cairo_surface_destroy (url->surface);
167 G_OBJECT_CLASS (_gtk_css_image_url_parent_class)->dispose (object);
171 _gtk_css_image_url_class_init (GtkCssImageUrlClass *klass)
173 GtkCssImageClass *image_class = GTK_CSS_IMAGE_CLASS (klass);
174 GObjectClass *object_class = G_OBJECT_CLASS (klass);
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;
182 object_class->dispose = gtk_css_image_url_dispose;
186 _gtk_css_image_url_init (GtkCssImageUrl *image_url)