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 "gtkcssimagesurfaceprivate.h"
23 #include <math.h>
24
G_DEFINE_TYPE(GtkCssImageSurface,_gtk_css_image_surface,GTK_TYPE_CSS_IMAGE)25 G_DEFINE_TYPE (GtkCssImageSurface, _gtk_css_image_surface, GTK_TYPE_CSS_IMAGE)
26
27 static int
28 gtk_css_image_surface_get_width (GtkCssImage *image)
29 {
30 GtkCssImageSurface *surface = GTK_CSS_IMAGE_SURFACE (image);
31
32 return cairo_image_surface_get_width (surface->surface);
33 }
34
35 static int
gtk_css_image_surface_get_height(GtkCssImage * image)36 gtk_css_image_surface_get_height (GtkCssImage *image)
37 {
38 GtkCssImageSurface *surface = GTK_CSS_IMAGE_SURFACE (image);
39
40 return cairo_image_surface_get_height (surface->surface);
41 }
42
43 static void
gtk_css_image_surface_draw(GtkCssImage * image,cairo_t * cr,double width,double height)44 gtk_css_image_surface_draw (GtkCssImage *image,
45 cairo_t *cr,
46 double width,
47 double height)
48 {
49 GtkCssImageSurface *surface = GTK_CSS_IMAGE_SURFACE (image);
50 int image_width, image_height;
51
52 image_width = cairo_image_surface_get_width (surface->surface);
53 image_height = cairo_image_surface_get_height (surface->surface);
54
55 if (image_width == 0 || image_height == 0 || width <= 0 || height <= 0)
56 return;
57
58 /* Update cache image if size is different */
59 if (surface->cache == NULL ||
60 ABS (width - surface->width) > 0.001 ||
61 ABS (height - surface->height) > 0.001)
62 {
63 double xscale, yscale;
64 cairo_t *cache;
65
66 /* We need the device scale (HiDPI mode) to calculate the proper size in
67 * pixels for the image surface and set the cache device scale
68 */
69 cairo_surface_get_device_scale (cairo_get_target (cr), &xscale, &yscale);
70
71 /* Save original size to preserve precision */
72 surface->width = width;
73 surface->height = height;
74
75 /* Destroy old cache if any */
76 g_clear_pointer (&surface->cache, cairo_surface_destroy);
77
78 /* Image big enough to contain scaled image with subpixel precision */
79 surface->cache = cairo_surface_create_similar_image (surface->surface,
80 CAIRO_FORMAT_ARGB32,
81 ceil (width*xscale),
82 ceil (height*yscale));
83 cairo_surface_set_device_scale (surface->cache, xscale, yscale);
84 cache = cairo_create (surface->cache);
85 cairo_rectangle (cache, 0, 0, width, height);
86 cairo_scale (cache, width / image_width, height / image_height);
87 cairo_set_source_surface (cache, surface->surface, 0, 0);
88 cairo_fill (cache);
89
90 cairo_destroy (cache);
91 }
92
93 cairo_rectangle (cr, 0, 0, width, height);
94 cairo_set_source_surface (cr, surface->cache ? surface->cache : surface->surface, 0, 0);
95 cairo_fill (cr);
96 }
97
98 static cairo_status_t
surface_write(void * closure,const unsigned char * data,unsigned int length)99 surface_write (void *closure,
100 const unsigned char *data,
101 unsigned int length)
102 {
103 g_byte_array_append (closure, data, length);
104
105 return CAIRO_STATUS_SUCCESS;
106 }
107
108 static void
gtk_css_image_surface_print(GtkCssImage * image,GString * string)109 gtk_css_image_surface_print (GtkCssImage *image,
110 GString *string)
111 {
112 #if CAIRO_HAS_PNG_FUNCTIONS
113 GtkCssImageSurface *surface = GTK_CSS_IMAGE_SURFACE (image);
114 GByteArray *array;
115 char *base64;
116
117 array = g_byte_array_new ();
118 cairo_surface_write_to_png_stream (surface->surface, surface_write, array);
119 base64 = g_base64_encode (array->data, array->len);
120 g_byte_array_free (array, TRUE);
121
122 g_string_append (string, "url(\"data:image/png;base64,");
123 g_string_append (string, base64);
124 g_string_append (string, "\")");
125
126 g_free (base64);
127 #else
128 g_string_append (string, "none /* you need cairo png functions enabled to make this work */");
129 #endif
130 }
131
132 static void
gtk_css_image_surface_dispose(GObject * object)133 gtk_css_image_surface_dispose (GObject *object)
134 {
135 GtkCssImageSurface *surface = GTK_CSS_IMAGE_SURFACE (object);
136
137 if (surface->surface)
138 {
139 cairo_surface_destroy (surface->surface);
140 surface->surface = NULL;
141 }
142
143 g_clear_pointer (&surface->cache, cairo_surface_destroy);
144
145 G_OBJECT_CLASS (_gtk_css_image_surface_parent_class)->dispose (object);
146 }
147
148 static void
_gtk_css_image_surface_class_init(GtkCssImageSurfaceClass * klass)149 _gtk_css_image_surface_class_init (GtkCssImageSurfaceClass *klass)
150 {
151 GtkCssImageClass *image_class = GTK_CSS_IMAGE_CLASS (klass);
152 GObjectClass *object_class = G_OBJECT_CLASS (klass);
153
154 image_class->get_width = gtk_css_image_surface_get_width;
155 image_class->get_height = gtk_css_image_surface_get_height;
156 image_class->draw = gtk_css_image_surface_draw;
157 image_class->print = gtk_css_image_surface_print;
158
159 object_class->dispose = gtk_css_image_surface_dispose;
160 }
161
162 static void
_gtk_css_image_surface_init(GtkCssImageSurface * image_surface)163 _gtk_css_image_surface_init (GtkCssImageSurface *image_surface)
164 {
165 }
166
167 GtkCssImage *
_gtk_css_image_surface_new(cairo_surface_t * surface)168 _gtk_css_image_surface_new (cairo_surface_t *surface)
169 {
170 GtkCssImage *image;
171
172 g_return_val_if_fail (surface != NULL, NULL);
173
174 image = g_object_new (GTK_TYPE_CSS_IMAGE_SURFACE, NULL);
175
176 GTK_CSS_IMAGE_SURFACE (image)->surface = cairo_surface_reference (surface);
177
178 return image;
179 }
180
181 GtkCssImage *
_gtk_css_image_surface_new_for_pixbuf(GdkPixbuf * pixbuf)182 _gtk_css_image_surface_new_for_pixbuf (GdkPixbuf *pixbuf)
183 {
184 GtkCssImage *image;
185 cairo_surface_t *surface;
186
187 g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
188
189 surface = gdk_cairo_surface_create_from_pixbuf (pixbuf, 1, NULL);
190
191 image = _gtk_css_image_surface_new (surface);
192 cairo_surface_destroy (surface);
193
194 return image;
195 }
196
197