1 /* GDK - The GIMP Drawing Kit
2 * Copyright (C) 2005 Red Hat, Inc.
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.
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.
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.
24 #include "gdkdrawable.h"
25 #include "gdkinternals.h"
30 * SECTION:cairo_interaction
31 * @Short_description: Functions to support using Cairo
32 * @Title: Cairo Interaction
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.
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.
46 * gdk_cairo_get_clip_rectangle:
47 * @cr: a cairo context
48 * @rect: (out) (allow-none): return location for the clip, or %NULL
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.
54 * Returns: %TRUE if a clip rectangle exists, %FALSE if all of @cr is
55 * clipped and all drawing can be skipped.
58 gdk_cairo_get_clip_rectangle (cairo_t *cr,
61 double x1, y1, x2, y2;
64 cairo_clip_extents (cr, &x1, &y1, &x2, &y2);
66 clip_exists = x1 < x2 && y1 < y2;
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);
85 * gdk_cairo_set_source_color:
89 * Sets the specified #GdkColor as the source color of @cr.
94 gdk_cairo_set_source_color (cairo_t *cr,
95 const GdkColor *color)
97 g_return_if_fail (cr != NULL);
98 g_return_if_fail (color != NULL);
100 cairo_set_source_rgb (cr,
102 color->green / 65535.,
103 color->blue / 65535.);
107 gdk_cairo_set_source_rgba (cairo_t *cr,
110 g_return_if_fail (cr != NULL);
111 g_return_if_fail (rgba != NULL);
113 cairo_set_source_rgba (cr,
121 * gdk_cairo_rectangle:
123 * @rectangle: a #GdkRectangle
125 * Adds the given rectangle to the current path of @cr.
130 gdk_cairo_rectangle (cairo_t *cr,
131 const GdkRectangle *rectangle)
133 g_return_if_fail (cr != NULL);
134 g_return_if_fail (rectangle != NULL);
137 rectangle->x, rectangle->y,
138 rectangle->width, rectangle->height);
144 * @region: a #cairo_region_t
146 * Adds the given region to the current path of @cr.
151 gdk_cairo_region (cairo_t *cr,
152 const cairo_region_t *region)
154 cairo_rectangle_int_t box;
157 g_return_if_fail (cr != NULL);
158 g_return_if_fail (region != NULL);
160 n_boxes = cairo_region_num_rectangles (region);
162 for (i = 0; i < n_boxes; i++)
164 cairo_region_get_rectangle (region, i, &box);
165 cairo_rectangle (cr, box.x, box.y, box.width, box.height);
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
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
183 gdk_cairo_set_source_pixbuf (cairo_t *cr,
184 const GdkPixbuf *pixbuf,
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);
194 guchar *cairo_pixels;
195 cairo_format_t format;
196 cairo_surface_t *surface;
197 static const cairo_user_data_key_t key;
201 format = CAIRO_FORMAT_RGB24;
203 format = CAIRO_FORMAT_ARGB32;
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,
209 width, height, cairo_stride);
211 cairo_surface_set_user_data (surface, &key,
212 cairo_pixels, (cairo_destroy_func_t)g_free);
214 for (j = height; j; j--)
216 guchar *p = gdk_pixels;
217 guchar *q = cairo_pixels;
221 guchar *end = p + 3 * width;
225 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
240 guchar *end = p + 4 * width;
243 #define MULT(d,c,a,t) G_STMT_START { t = c * a + 0x7f; d = ((t >> 8) + t) >> 8; } G_STMT_END
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);
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);
266 gdk_pixels += gdk_rowstride;
267 cairo_pixels += cairo_stride;
270 cairo_set_source_surface (cr, surface, pixbuf_x, pixbuf_y);
271 cairo_surface_destroy (surface);
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
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.
286 * Note that the contents of @window are undefined outside of the
287 * visible part of @window, so use this function with care.
292 gdk_cairo_set_source_window (cairo_t *cr,
297 cairo_surface_t *surface;
299 g_return_if_fail (cr != NULL);
300 g_return_if_fail (GDK_IS_WINDOW (window));
302 surface = _gdk_drawable_ref_cairo_surface (GDK_DRAWABLE (window));
303 cairo_set_source_surface (cr, surface, x, y);
304 cairo_surface_destroy (surface);
308 * _gdk_cairo_surface_extents:
309 * @surface: surface to measure
310 * @extents: (out): rectangle to put the extents
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
319 * Returns: %TRUE if the extents fit in a #GdkRectangle, %FALSE if not.
322 _gdk_cairo_surface_extents (cairo_surface_t *surface,
323 GdkRectangle *extents)
325 double x1, x2, y1, y2;
328 g_return_val_if_fail (surface != NULL, FALSE);
329 g_return_val_if_fail (extents != NULL, FALSE);
331 cr = cairo_create (surface);
332 cairo_clip_extents (cr, &x1, &y1, &x2, &y2);
342 if (x1 < G_MININT || x1 > G_MAXINT ||
343 y1 < G_MININT || y1 > G_MAXINT ||
344 x2 > G_MAXINT || y2 > G_MAXINT)
346 extents->x = extents->y = extents->width = extents->height = 0;
353 extents->height = y2;
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.
365 * gdk_cairo_region_create_from_surface:
366 * @surface: A surface
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().
372 * Returns: A #cairo_region_t. This must be freed with cairo_region_destroy()
376 gdk_cairo_region_create_from_surface (cairo_surface_t *surface)
378 cairo_region_t *region;
379 GdkRectangle extents, rect;
380 cairo_surface_t *image;
385 _gdk_cairo_surface_extents (surface, &extents);
387 if (cairo_surface_get_content (surface) == CAIRO_CONTENT_COLOR)
388 return cairo_region_create_rectangle (&extents);
390 if (cairo_surface_get_type (surface) != CAIRO_SURFACE_TYPE_IMAGE ||
391 cairo_image_surface_get_format (surface) != CAIRO_FORMAT_A1)
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);
402 image = cairo_surface_reference (surface);
404 data = cairo_image_surface_get_data (image);
405 stride = cairo_image_surface_get_stride (image);
407 region = cairo_region_create ();
409 for (y = 0; y < extents.height; y++)
411 for (x = 0; x < extents.width; x++)
413 /* Search for a continuous range of "non transparent pixels"*/
415 while (x < extents.width)
417 if (((data[x / 8] >> (x%8)) & 1) == 0)
418 /* This pixel is "transparent"*/
425 /* Add the pixels (x0, y) to (x, y+1) as a new rectangle
433 cairo_region_union_rectangle (region, &rect);
439 cairo_surface_destroy (image);
441 cairo_region_translate (region, extents.x, extents.y);