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