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