]> Pileus Git - ~andy/gtk/blob - gtk/gtkcssimageurl.c
[l10n] Updated German translation
[~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   GFile *file;
70   cairo_t *cr;
71   GError *error = NULL;
72   GFileInputStream *input;
73
74   file = _gtk_css_parser_read_url (parser);
75   if (file == NULL)
76     return FALSE;
77
78   /* We special case resources here so we can use
79      gdk_pixbuf_new_from_resource, which in turn has some special casing
80      for GdkPixdata files to avoid duplicating the memory for the pixbufs */
81   if (g_file_has_uri_scheme (file, "resource"))
82     {
83       char *uri = g_file_get_uri (file);
84       char *resource_path = g_uri_unescape_string (uri + strlen ("resource://"), NULL);
85
86       pixbuf = gdk_pixbuf_new_from_resource (resource_path, &error);
87       g_free (resource_path);
88       g_free (uri);
89     }
90   else
91     {
92       input = g_file_read (file, NULL, &error);
93       if (input == NULL)
94         {
95           _gtk_css_parser_take_error (parser, error);
96           return FALSE;
97         }
98
99       pixbuf = gdk_pixbuf_new_from_stream (G_INPUT_STREAM (input), NULL, &error);
100       g_object_unref (input);
101     }
102   g_object_unref (file);
103
104   if (pixbuf == NULL)
105     {
106       _gtk_css_parser_take_error (parser, error);
107       return FALSE;
108     }
109
110   url->surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
111                                              gdk_pixbuf_get_width (pixbuf),
112                                              gdk_pixbuf_get_height (pixbuf));
113   cr = cairo_create (url->surface);
114   gdk_cairo_set_source_pixbuf (cr, pixbuf, 0, 0);
115   cairo_paint (cr);
116   cairo_destroy (cr);
117   g_object_unref (pixbuf);
118
119   return TRUE;
120 }
121
122 static cairo_status_t
123 surface_write (void                *closure,
124                const unsigned char *data,
125                unsigned int         length)
126 {
127   g_byte_array_append (closure, data, length);
128
129   return CAIRO_STATUS_SUCCESS;
130 }
131
132 static void
133 gtk_css_image_url_print (GtkCssImage *image,
134                          GString     *string)
135 {
136 #if CAIRO_HAS_PNG_FUNCTIONS
137   GtkCssImageUrl *url = GTK_CSS_IMAGE_URL (image);
138   GByteArray *array;
139   char *base64;
140   
141   array = g_byte_array_new ();
142   cairo_surface_write_to_png_stream (url->surface, surface_write, array);
143   base64 = g_base64_encode (array->data, array->len);
144   g_byte_array_free (array, TRUE);
145
146   g_string_append (string, "url(\"data:image/png;base64,");
147   g_string_append (string, base64);
148   g_string_append (string, "\")");
149
150   g_free (base64);
151 #else
152   g_string_append (string, "none /* you need cairo png functions enabled to make this work */");
153 #endif
154 }
155
156 static void
157 gtk_css_image_url_dispose (GObject *object)
158 {
159   GtkCssImageUrl *url = GTK_CSS_IMAGE_URL (object);
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