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