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, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 * Authors: Benjamin Otte <otte@gnome.org>
25 #include "gtkcssimageurlprivate.h"
27 #include "gtkcssprovider.h"
29 G_DEFINE_TYPE (GtkCssImageUrl, _gtk_css_image_url, GTK_TYPE_CSS_IMAGE)
32 gtk_css_image_url_get_width (GtkCssImage *image)
34 GtkCssImageUrl *url = GTK_CSS_IMAGE_URL (image);
36 return cairo_image_surface_get_width (url->surface);
40 gtk_css_image_url_get_height (GtkCssImage *image)
42 GtkCssImageUrl *url = GTK_CSS_IMAGE_URL (image);
44 return cairo_image_surface_get_height (url->surface);
48 gtk_css_image_url_draw (GtkCssImage *image,
53 GtkCssImageUrl *url = GTK_CSS_IMAGE_URL (image);
55 cairo_rectangle (cr, 0, 0, width, height);
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);
65 gtk_css_image_url_parse (GtkCssImage *image,
69 GtkCssImageUrl *url = GTK_CSS_IMAGE_URL (image);
74 GFileInputStream *input;
76 file = _gtk_css_parser_read_url (parser, base);
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"))
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);
89 pixbuf = gdk_pixbuf_new_from_resource (resource_path, &error);
90 g_free (resource_path);
95 input = g_file_read (file, NULL, &error);
98 _gtk_css_parser_take_error (parser, error);
101 g_object_unref (file);
103 pixbuf = gdk_pixbuf_new_from_stream (G_INPUT_STREAM (input), NULL, &error);
104 g_object_unref (input);
109 _gtk_css_parser_take_error (parser, error);
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);
120 g_object_unref (pixbuf);
125 static cairo_status_t
126 surface_write (void *closure,
127 const unsigned char *data,
130 g_byte_array_append (closure, data, length);
132 return CAIRO_STATUS_SUCCESS;
136 gtk_css_image_url_print (GtkCssImage *image,
139 #if CAIRO_HAS_PNG_FUNCTIONS
140 GtkCssImageUrl *url = GTK_CSS_IMAGE_URL (image);
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);
149 g_string_append (string, "url(\"data:image/png;base64,");
150 g_string_append (string, base64);
151 g_string_append (string, "\")");
155 g_string_append (string, "none /* you need cairo png functions enabled to make this work */");
160 gtk_css_image_url_dispose (GObject *object)
162 GtkCssImageUrl *url = GTK_CSS_IMAGE_URL (object);
166 cairo_surface_destroy (url->surface);
170 G_OBJECT_CLASS (_gtk_css_image_url_parent_class)->dispose (object);
174 _gtk_css_image_url_class_init (GtkCssImageUrlClass *klass)
176 GtkCssImageClass *image_class = GTK_CSS_IMAGE_CLASS (klass);
177 GObjectClass *object_class = G_OBJECT_CLASS (klass);
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;
185 object_class->dispose = gtk_css_image_url_dispose;
189 _gtk_css_image_url_init (GtkCssImageUrl *image_url)