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