]> Pileus Git - ~andy/gtk/blob - gdk/gdkcairo.c
Implement GdkRegion in terms of cairo_region_t
[~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 #include "gdkdrawable.h"
22 #include "gdkinternals.h"
23 #include "gdkalias.h"
24
25 /**
26  * gdk_cairo_create:
27  * @drawable: a #GdkDrawable
28  * 
29  * Creates a Cairo context for drawing to @drawable.
30  *
31  * <note><para>
32  * Note that due to double-buffering, Cairo contexts created 
33  * in a GTK+ expose event handler cannot be cached and reused 
34  * between different expose events. 
35  * </para></note>
36  *
37  * Return value: A newly created Cairo context. Free with
38  *  cairo_destroy() when you are done drawing.
39  * 
40  * Since: 2.8
41  **/
42 cairo_t *
43 gdk_cairo_create (GdkDrawable *drawable)
44 {
45   cairo_surface_t *surface;
46   cairo_t *cr;
47     
48   g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);
49
50   surface = _gdk_drawable_ref_cairo_surface (drawable);
51   cr = cairo_create (surface);
52
53   if (GDK_DRAWABLE_GET_CLASS (drawable)->set_cairo_clip)
54     GDK_DRAWABLE_GET_CLASS (drawable)->set_cairo_clip (drawable, cr);
55     
56   cairo_surface_destroy (surface);
57
58   return cr;
59 }
60
61 /**
62  * gdk_cairo_reset_clip:
63  * @cr: a #cairo_t
64  * @drawable: a #GdkDrawable
65  *
66  * Resets the clip region for a Cairo context created by gdk_cairo_create().
67  *
68  * This resets the clip region to the "empty" state for the given drawable.
69  * This is required for non-native windows since a direct call to
70  * cairo_reset_clip() would unset the clip region inherited from the
71  * drawable (i.e. the window clip region), and thus let you e.g.
72  * draw outside your window.
73  *
74  * This is rarely needed though, since most code just create a new cairo_t
75  * using gdk_cairo_create() each time they want to draw something.
76  *
77  * Since: 2.18
78  **/
79 void
80 gdk_cairo_reset_clip (cairo_t            *cr,
81                       GdkDrawable        *drawable)
82 {
83   cairo_reset_clip (cr);
84
85   if (GDK_DRAWABLE_GET_CLASS (drawable)->set_cairo_clip)
86     GDK_DRAWABLE_GET_CLASS (drawable)->set_cairo_clip (drawable, cr);
87 }
88
89 /**
90  * gdk_cairo_set_source_color:
91  * @cr: a #cairo_t
92  * @color: a #GdkColor
93  * 
94  * Sets the specified #GdkColor as the source color of @cr.
95  *
96  * Since: 2.8
97  **/
98 void
99 gdk_cairo_set_source_color (cairo_t        *cr,
100                             const GdkColor *color)
101 {
102   g_return_if_fail (cr != NULL);
103   g_return_if_fail (color != NULL);
104     
105   cairo_set_source_rgb (cr,
106                         color->red / 65535.,
107                         color->green / 65535.,
108                         color->blue / 65535.);
109 }
110
111 /**
112  * gdk_cairo_rectangle:
113  * @cr: a #cairo_t
114  * @rectangle: a #GdkRectangle
115  * 
116  * Adds the given rectangle to the current path of @cr.
117  *
118  * Since: 2.8
119  **/
120 void
121 gdk_cairo_rectangle (cairo_t            *cr,
122                      const GdkRectangle *rectangle)
123 {
124   g_return_if_fail (cr != NULL);
125   g_return_if_fail (rectangle != NULL);
126
127   cairo_rectangle (cr,
128                    rectangle->x,     rectangle->y,
129                    rectangle->width, rectangle->height);
130 }
131
132 /**
133  * gdk_cairo_region:
134  * @cr: a #cairo_t
135  * @region: a #GdkRegion
136  * 
137  * Adds the given region to the current path of @cr.
138  *
139  * Since: 2.8
140  **/
141 void
142 gdk_cairo_region (cairo_t         *cr,
143                   const GdkRegion *region)
144 {
145   cairo_rectangle_int_t box;
146   gint n_boxes, i;
147
148   g_return_if_fail (cr != NULL);
149   g_return_if_fail (region != NULL);
150
151   n_boxes = cairo_region_num_rectangles (region);
152
153   for (i = 0; i < n_boxes; i++)
154     {
155       cairo_region_get_rectangle (region, i, &box);
156       cairo_rectangle (cr, box.x, box.y, box.width, box.height);
157     }
158 }
159
160 /**
161  * gdk_cairo_set_source_pixbuf:
162  * @cr: a #Cairo context
163  * @pixbuf: a #GdkPixbuf
164  * @pixbuf_x: X coordinate of location to place upper left corner of @pixbuf
165  * @pixbuf_y: Y coordinate of location to place upper left corner of @pixbuf
166  * 
167  * Sets the given pixbuf as the source pattern for the Cairo context.
168  * The pattern has an extend mode of %CAIRO_EXTEND_NONE and is aligned
169  * so that the origin of @pixbuf is @pixbuf_x, @pixbuf_y
170  *
171  * Since: 2.8
172  **/
173 void
174 gdk_cairo_set_source_pixbuf (cairo_t         *cr,
175                              const GdkPixbuf *pixbuf,
176                              double           pixbuf_x,
177                              double           pixbuf_y)
178 {
179   gint width = gdk_pixbuf_get_width (pixbuf);
180   gint height = gdk_pixbuf_get_height (pixbuf);
181   guchar *gdk_pixels = gdk_pixbuf_get_pixels (pixbuf);
182   int gdk_rowstride = gdk_pixbuf_get_rowstride (pixbuf);
183   int n_channels = gdk_pixbuf_get_n_channels (pixbuf);
184   int cairo_stride;
185   guchar *cairo_pixels;
186   cairo_format_t format;
187   cairo_surface_t *surface;
188   static const cairo_user_data_key_t key;
189   int j;
190
191   if (n_channels == 3)
192     format = CAIRO_FORMAT_RGB24;
193   else
194     format = CAIRO_FORMAT_ARGB32;
195
196   cairo_stride = cairo_format_stride_for_width (format, width);
197   cairo_pixels = g_malloc (height * cairo_stride);
198   surface = cairo_image_surface_create_for_data ((unsigned char *)cairo_pixels,
199                                                  format,
200                                                  width, height, cairo_stride);
201
202   cairo_surface_set_user_data (surface, &key,
203                                cairo_pixels, (cairo_destroy_func_t)g_free);
204
205   for (j = height; j; j--)
206     {
207       guchar *p = gdk_pixels;
208       guchar *q = cairo_pixels;
209
210       if (n_channels == 3)
211         {
212           guchar *end = p + 3 * width;
213           
214           while (p < end)
215             {
216 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
217               q[0] = p[2];
218               q[1] = p[1];
219               q[2] = p[0];
220 #else     
221               q[1] = p[0];
222               q[2] = p[1];
223               q[3] = p[2];
224 #endif
225               p += 3;
226               q += 4;
227             }
228         }
229       else
230         {
231           guchar *end = p + 4 * width;
232           guint t1,t2,t3;
233             
234 #define MULT(d,c,a,t) G_STMT_START { t = c * a + 0x7f; d = ((t >> 8) + t) >> 8; } G_STMT_END
235
236           while (p < end)
237             {
238 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
239               MULT(q[0], p[2], p[3], t1);
240               MULT(q[1], p[1], p[3], t2);
241               MULT(q[2], p[0], p[3], t3);
242               q[3] = p[3];
243 #else     
244               q[0] = p[3];
245               MULT(q[1], p[0], p[3], t1);
246               MULT(q[2], p[1], p[3], t2);
247               MULT(q[3], p[2], p[3], t3);
248 #endif
249               
250               p += 4;
251               q += 4;
252             }
253           
254 #undef MULT
255         }
256
257       gdk_pixels += gdk_rowstride;
258       cairo_pixels += cairo_stride;
259     }
260
261   cairo_set_source_surface (cr, surface, pixbuf_x, pixbuf_y);
262   cairo_surface_destroy (surface);
263 }
264
265 /**
266  * gdk_cairo_set_source_pixmap:
267  * @cr: a #Cairo context
268  * @pixmap: a #GdkPixmap
269  * @pixmap_x: X coordinate of location to place upper left corner of @pixmap
270  * @pixmap_y: Y coordinate of location to place upper left corner of @pixmap
271  * 
272  * Sets the given pixmap as the source pattern for the Cairo context.
273  * The pattern has an extend mode of %CAIRO_EXTEND_NONE and is aligned
274  * so that the origin of @pixmap is @pixmap_x, @pixmap_y
275  *
276  * Since: 2.10
277  **/
278 void
279 gdk_cairo_set_source_pixmap (cairo_t   *cr,
280                              GdkPixmap *pixmap,
281                              double     pixmap_x,
282                              double     pixmap_y)
283 {
284   cairo_surface_t *surface;
285   
286   surface = _gdk_drawable_ref_cairo_surface (GDK_DRAWABLE (pixmap));
287   cairo_set_source_surface (cr, surface, pixmap_x, pixmap_y);
288   cairo_surface_destroy (surface);
289 }
290
291
292 #define __GDK_CAIRO_C__
293 #include "gdkaliasdef.c"