]> Pileus Git - ~andy/gtk/blob - gdk/gdkcairo.c
stylecontext: Do invalidation on first resize container
[~andy/gtk] / gdk / gdkcairo.c
1 /* GDK - The GIMP Drawing Kit
2  * Copyright (C) 2005 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 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
18 #include "config.h"
19
20 #include "gdkcairo.h"
21
22 #include "gdkinternals.h"
23
24 #include <math.h>
25
26 /**
27  * SECTION:cairo_interaction
28  * @Short_description: Functions to support using cairo
29  * @Title: Cairo Interaction
30  *
31  * <link href="http://cairographics.org">Cairo</link> is a graphics
32  * library that supports vector graphics and image compositing that
33  * can be used with GDK. GTK+ does all of its drawing using cairo.
34  *
35  * GDK does not wrap the cairo API, instead it allows to create cairo
36  * contexts which can be used to draw on #GdkWindows. Additional
37  * functions allow use #GdkRectangles with cairo and to use #GdkColors,
38  * #GdkRGBAs, #GdkPixbufs and #GdkWindows as sources for drawing
39  * operations.
40  */
41
42
43 /**
44  * gdk_cairo_get_clip_rectangle:
45  * @cr: a cairo context
46  * @rect: (out) (allow-none): return location for the clip, or %NULL
47  *
48  * This is a convenience function around cairo_clip_extents().
49  * It rounds the clip extents to integer coordinates and returns
50  * a boolean indicating if a clip area exists.
51  *
52  * Returns: %TRUE if a clip rectangle exists, %FALSE if all of @cr is
53  *     clipped and all drawing can be skipped
54  */
55 gboolean
56 gdk_cairo_get_clip_rectangle (cairo_t      *cr,
57                               GdkRectangle *rect)
58 {
59   double x1, y1, x2, y2;
60   gboolean clip_exists;
61
62   cairo_clip_extents (cr, &x1, &y1, &x2, &y2);
63
64   clip_exists = x1 < x2 && y1 < y2;
65
66   if (rect)
67     {
68       x1 = floor (x1);
69       y1 = floor (y1);
70       x2 = ceil (x2);
71       y2 = ceil (y2);
72
73       rect->x      = CLAMP (x1,      G_MININT, G_MAXINT);
74       rect->y      = CLAMP (y1,      G_MININT, G_MAXINT);
75       rect->width  = CLAMP (x2 - x1, G_MININT, G_MAXINT);
76       rect->height = CLAMP (y2 - y1, G_MININT, G_MAXINT);
77     }
78
79   return clip_exists;
80 }
81
82 /**
83  * gdk_cairo_set_source_color:
84  * @cr: a cairo context
85  * @color: a #GdkColor
86  *
87  * Sets the specified #GdkColor as the source color of @cr.
88  *
89  * Since: 2.8
90  *
91  * Deprecated: 3.4: Use gdk_cairo_set_source_rgba() instead
92  */
93 void
94 gdk_cairo_set_source_color (cairo_t        *cr,
95                             const GdkColor *color)
96 {
97   g_return_if_fail (cr != NULL);
98   g_return_if_fail (color != NULL);
99     
100   cairo_set_source_rgb (cr,
101                         color->red / 65535.,
102                         color->green / 65535.,
103                         color->blue / 65535.);
104 }
105
106 /**
107  * gdk_cairo_set_source_rgba:
108  * @cr: a cairo context
109  * @rgba: a #GdkRGBA
110  *
111  * Sets the specified #GdkRGBA as the source color of @cr.
112  *
113  * Since: 3.0
114  */
115 void
116 gdk_cairo_set_source_rgba (cairo_t       *cr,
117                            const GdkRGBA *rgba)
118 {
119   g_return_if_fail (cr != NULL);
120   g_return_if_fail (rgba != NULL);
121
122   cairo_set_source_rgba (cr,
123                          rgba->red,
124                          rgba->green,
125                          rgba->blue,
126                          rgba->alpha);
127 }
128
129 /**
130  * gdk_cairo_rectangle:
131  * @cr: a cairo context
132  * @rectangle: a #GdkRectangle
133  *
134  * Adds the given rectangle to the current path of @cr.
135  *
136  * Since: 2.8
137  */
138 void
139 gdk_cairo_rectangle (cairo_t            *cr,
140                      const GdkRectangle *rectangle)
141 {
142   g_return_if_fail (cr != NULL);
143   g_return_if_fail (rectangle != NULL);
144
145   cairo_rectangle (cr,
146                    rectangle->x,     rectangle->y,
147                    rectangle->width, rectangle->height);
148 }
149
150 /**
151  * gdk_cairo_region:
152  * @cr: a cairo context
153  * @region: a #cairo_region_t
154  *
155  * Adds the given region to the current path of @cr.
156  *
157  * Since: 2.8
158  */
159 void
160 gdk_cairo_region (cairo_t         *cr,
161                   const cairo_region_t *region)
162 {
163   cairo_rectangle_int_t box;
164   gint n_boxes, i;
165
166   g_return_if_fail (cr != NULL);
167   g_return_if_fail (region != NULL);
168
169   n_boxes = cairo_region_num_rectangles (region);
170
171   for (i = 0; i < n_boxes; i++)
172     {
173       cairo_region_get_rectangle (region, i, &box);
174       cairo_rectangle (cr, box.x, box.y, box.width, box.height);
175     }
176 }
177
178 /**
179  * gdk_cairo_set_source_pixbuf:
180  * @cr: a cairo context
181  * @pixbuf: a #GdkPixbuf
182  * @pixbuf_x: X coordinate of location to place upper left corner of @pixbuf
183  * @pixbuf_y: Y coordinate of location to place upper left corner of @pixbuf
184  *
185  * Sets the given pixbuf as the source pattern for @cr.
186  *
187  * The pattern has an extend mode of %CAIRO_EXTEND_NONE and is aligned
188  * so that the origin of @pixbuf is @pixbuf_x, @pixbuf_y.
189  *
190  * Since: 2.8
191  */
192 void
193 gdk_cairo_set_source_pixbuf (cairo_t         *cr,
194                              const GdkPixbuf *pixbuf,
195                              gdouble          pixbuf_x,
196                              gdouble          pixbuf_y)
197 {
198   gint width = gdk_pixbuf_get_width (pixbuf);
199   gint height = gdk_pixbuf_get_height (pixbuf);
200   guchar *gdk_pixels = gdk_pixbuf_get_pixels (pixbuf);
201   int gdk_rowstride = gdk_pixbuf_get_rowstride (pixbuf);
202   int n_channels = gdk_pixbuf_get_n_channels (pixbuf);
203   int cairo_stride;
204   guchar *cairo_pixels;
205   cairo_format_t format;
206   cairo_surface_t *surface;
207   static const cairo_user_data_key_t key;
208   int j;
209
210   if (n_channels == 3)
211     format = CAIRO_FORMAT_RGB24;
212   else
213     format = CAIRO_FORMAT_ARGB32;
214
215   cairo_stride = cairo_format_stride_for_width (format, width);
216   cairo_pixels = g_malloc (height * cairo_stride);
217   surface = cairo_image_surface_create_for_data ((unsigned char *)cairo_pixels,
218                                                  format,
219                                                  width, height, cairo_stride);
220
221   cairo_surface_set_user_data (surface, &key,
222                                cairo_pixels, (cairo_destroy_func_t)g_free);
223
224   for (j = height; j; j--)
225     {
226       guchar *p = gdk_pixels;
227       guchar *q = cairo_pixels;
228
229       if (n_channels == 3)
230         {
231           guchar *end = p + 3 * width;
232
233           while (p < end)
234             {
235 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
236               q[0] = p[2];
237               q[1] = p[1];
238               q[2] = p[0];
239 #else
240               q[1] = p[0];
241               q[2] = p[1];
242               q[3] = p[2];
243 #endif
244               p += 3;
245               q += 4;
246             }
247         }
248       else
249         {
250           guchar *end = p + 4 * width;
251           guint t1,t2,t3;
252
253 #define MULT(d,c,a,t) G_STMT_START { t = c * a + 0x7f; d = ((t >> 8) + t) >> 8; } G_STMT_END
254
255           while (p < end)
256             {
257 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
258               MULT(q[0], p[2], p[3], t1);
259               MULT(q[1], p[1], p[3], t2);
260               MULT(q[2], p[0], p[3], t3);
261               q[3] = p[3];
262 #else
263               q[0] = p[3];
264               MULT(q[1], p[0], p[3], t1);
265               MULT(q[2], p[1], p[3], t2);
266               MULT(q[3], p[2], p[3], t3);
267 #endif
268
269               p += 4;
270               q += 4;
271             }
272
273 #undef MULT
274         }
275
276       gdk_pixels += gdk_rowstride;
277       cairo_pixels += cairo_stride;
278     }
279
280   cairo_set_source_surface (cr, surface, pixbuf_x, pixbuf_y);
281   cairo_surface_destroy (surface);
282 }
283
284 /**
285  * gdk_cairo_set_source_window:
286  * @cr: a cairo context
287  * @window: a #GdkWindow
288  * @x: X coordinate of location to place upper left corner of @window
289  * @y: Y coordinate of location to place upper left corner of @window
290  *
291  * Sets the given window as the source pattern for @cr.
292  *
293  * The pattern has an extend mode of %CAIRO_EXTEND_NONE and is aligned
294  * so that the origin of @window is @x, @y. The window contains all its
295  * subwindows when rendering.
296  *
297  * Note that the contents of @window are undefined outside of the
298  * visible part of @window, so use this function with care.
299  *
300  * Since: 2.24
301  */
302 void
303 gdk_cairo_set_source_window (cairo_t   *cr,
304                              GdkWindow *window,
305                              gdouble    x,
306                              gdouble    y)
307 {
308   cairo_surface_t *surface;
309
310   g_return_if_fail (cr != NULL);
311   g_return_if_fail (GDK_IS_WINDOW (window));
312
313   surface = _gdk_window_ref_cairo_surface (window);
314   cairo_set_source_surface (cr, surface, x, y);
315   cairo_surface_destroy (surface);
316 }
317
318 /*
319  * _gdk_cairo_surface_extents:
320  * @surface: surface to measure
321  * @extents: (out): rectangle to put the extents
322  *
323  * Measures the area covered by @surface and puts it into @extents.
324  *
325  * Note that this function respects device offsets set on @surface.
326  * If @surface is unbounded, the resulting extents will be empty and
327  * not be a maximal sized rectangle. This is to avoid careless coding.
328  * You must explicitly check the return value of you want to handle
329  * that case.
330  *
331  * Returns: %TRUE if the extents fit in a #GdkRectangle, %FALSE if not
332  */
333 gboolean
334 _gdk_cairo_surface_extents (cairo_surface_t *surface,
335                             GdkRectangle    *extents)
336 {
337   double x1, x2, y1, y2;
338   cairo_t *cr;
339
340   g_return_val_if_fail (surface != NULL, FALSE);
341   g_return_val_if_fail (extents != NULL, FALSE);
342
343   cr = cairo_create (surface);
344   cairo_clip_extents (cr, &x1, &y1, &x2, &y2);
345   cairo_destroy (cr);
346
347   x1 = floor (x1);
348   y1 = floor (y1);
349   x2 = ceil (x2);
350   y2 = ceil (y2);
351   x2 -= x1;
352   y2 -= y1;
353
354   if (x1 < G_MININT || x1 > G_MAXINT ||
355       y1 < G_MININT || y1 > G_MAXINT ||
356       x2 > G_MAXINT || y2 > G_MAXINT)
357     {
358       extents->x = extents->y = extents->width = extents->height = 0;
359       return FALSE;
360     }
361
362   extents->x = x1;
363   extents->y = y1;
364   extents->width = x2;
365   extents->height = y2;
366
367   return TRUE;
368 }
369
370 /* This function originally from Jean-Edouard Lachand-Robert, and
371  * available at www.codeguru.com. Simplified for our needs, not sure
372  * how much of the original code left any longer. Now handles just
373  * one-bit deep bitmaps (in Window parlance, ie those that GDK calls
374  * bitmaps (and not pixmaps), with zero pixels being transparent.
375  */
376 /**
377  * gdk_cairo_region_create_from_surface:
378  * @surface: a cairo surface
379  *
380  * Creates region that describes covers the area where the given
381  * @surface is more than 50% opaque.
382  *
383  * This function takes into account device offsets that might be
384  * set with cairo_surface_set_device_offset().
385  *
386  * Returns: A #cairo_region_t; must be freed with cairo_region_destroy()
387  */
388 cairo_region_t *
389 gdk_cairo_region_create_from_surface (cairo_surface_t *surface)
390 {
391   cairo_region_t *region;
392   GdkRectangle extents, rect;
393   cairo_surface_t *image;
394   cairo_t *cr;
395   gint x, y, stride;
396   guchar *data;
397
398   _gdk_cairo_surface_extents (surface, &extents);
399
400   if (cairo_surface_get_content (surface) == CAIRO_CONTENT_COLOR)
401     return cairo_region_create_rectangle (&extents);
402
403   if (cairo_surface_get_type (surface) != CAIRO_SURFACE_TYPE_IMAGE ||
404       cairo_image_surface_get_format (surface) != CAIRO_FORMAT_A1)
405     {
406       /* coerce to an A1 image */
407       image = cairo_image_surface_create (CAIRO_FORMAT_A1,
408                                           extents.width, extents.height);
409       cr = cairo_create (image);
410       cairo_set_source_surface (cr, surface, -extents.x, -extents.y);
411       cairo_paint (cr);
412       cairo_destroy (cr);
413     }
414   else
415     image = cairo_surface_reference (surface);
416
417   data = cairo_image_surface_get_data (image);
418   stride = cairo_image_surface_get_stride (image);
419
420   region = cairo_region_create ();
421
422   for (y = 0; y < extents.height; y++)
423     {
424       for (x = 0; x < extents.width; x++)
425         {
426           /* Search for a continuous range of "non transparent pixels"*/
427           gint x0 = x;
428           while (x < extents.width)
429             {
430 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
431               if (((data[x / 8] >> (x%8)) & 1) == 0)
432 #else
433               if (((data[x / 8] >> (7-(x%8))) & 1) == 0)
434 #endif
435                 /* This pixel is "transparent"*/
436                 break;
437               x++;
438             }
439
440           if (x > x0)
441             {
442               /* Add the pixels (x0, y) to (x, y+1) as a new rectangle
443                * in the region
444                */
445               rect.x = x0;
446               rect.width = x - x0;
447               rect.y = y;
448               rect.height = 1;
449
450               cairo_region_union_rectangle (region, &rect);
451             }
452         }
453       data += stride;
454     }
455
456   cairo_surface_destroy (image);
457
458   cairo_region_translate (region, extents.x, extents.y);
459
460   return region;
461 }