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