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 "gdkinternals.h"
29 * SECTION:cairo_interaction
30 * @Short_description: Functions to support using cairo
31 * @Title: Cairo Interaction
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.
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
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().
51 * It rounds the clip extents to integer coordinates and returns
52 * a boolean 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:
86 * @cr: a cairo context
89 * Sets the specified #GdkColor as the source color of @cr.
93 * Deprecated: 3.4: Use gdk_cairo_set_source_rgba() instead
96 gdk_cairo_set_source_color (cairo_t *cr,
97 const GdkColor *color)
99 g_return_if_fail (cr != NULL);
100 g_return_if_fail (color != NULL);
102 cairo_set_source_rgb (cr,
104 color->green / 65535.,
105 color->blue / 65535.);
109 * gdk_cairo_set_source_rgba:
110 * @cr: a cairo context
113 * Sets the specified #GdkRGBA as the source color of @cr.
118 gdk_cairo_set_source_rgba (cairo_t *cr,
121 g_return_if_fail (cr != NULL);
122 g_return_if_fail (rgba != NULL);
124 cairo_set_source_rgba (cr,
132 * gdk_cairo_rectangle:
133 * @cr: a cairo context
134 * @rectangle: a #GdkRectangle
136 * Adds the given rectangle to the current path of @cr.
141 gdk_cairo_rectangle (cairo_t *cr,
142 const GdkRectangle *rectangle)
144 g_return_if_fail (cr != NULL);
145 g_return_if_fail (rectangle != NULL);
148 rectangle->x, rectangle->y,
149 rectangle->width, rectangle->height);
154 * @cr: a cairo context
155 * @region: a #cairo_region_t
157 * Adds the given region to the current path of @cr.
162 gdk_cairo_region (cairo_t *cr,
163 const cairo_region_t *region)
165 cairo_rectangle_int_t box;
168 g_return_if_fail (cr != NULL);
169 g_return_if_fail (region != NULL);
171 n_boxes = cairo_region_num_rectangles (region);
173 for (i = 0; i < n_boxes; i++)
175 cairo_region_get_rectangle (region, i, &box);
176 cairo_rectangle (cr, box.x, box.y, box.width, box.height);
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
187 * Sets the given pixbuf as the source pattern for @cr.
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.
195 gdk_cairo_set_source_pixbuf (cairo_t *cr,
196 const GdkPixbuf *pixbuf,
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);
206 guchar *cairo_pixels;
207 cairo_format_t format;
208 cairo_surface_t *surface;
209 static const cairo_user_data_key_t key;
213 format = CAIRO_FORMAT_RGB24;
215 format = CAIRO_FORMAT_ARGB32;
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,
221 width, height, cairo_stride);
223 cairo_surface_set_user_data (surface, &key,
224 cairo_pixels, (cairo_destroy_func_t)g_free);
226 for (j = height; j; j--)
228 guchar *p = gdk_pixels;
229 guchar *q = cairo_pixels;
233 guchar *end = p + 3 * width;
237 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
252 guchar *end = p + 4 * width;
255 #define MULT(d,c,a,t) G_STMT_START { t = c * a + 0x7f; d = ((t >> 8) + t) >> 8; } G_STMT_END
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);
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);
278 gdk_pixels += gdk_rowstride;
279 cairo_pixels += cairo_stride;
282 cairo_set_source_surface (cr, surface, pixbuf_x, pixbuf_y);
283 cairo_surface_destroy (surface);
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
293 * Sets the given window as the source pattern for @cr.
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.
299 * Note that the contents of @window are undefined outside of the
300 * visible part of @window, so use this function with care.
305 gdk_cairo_set_source_window (cairo_t *cr,
310 cairo_surface_t *surface;
312 g_return_if_fail (cr != NULL);
313 g_return_if_fail (GDK_IS_WINDOW (window));
315 surface = _gdk_window_ref_cairo_surface (window);
316 cairo_set_source_surface (cr, surface, x, y);
317 cairo_surface_destroy (surface);
321 * _gdk_cairo_surface_extents:
322 * @surface: surface to measure
323 * @extents: (out): rectangle to put the extents
325 * Measures the area covered by @surface and puts it into @extents.
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
333 * Returns: %TRUE if the extents fit in a #GdkRectangle, %FALSE if not
336 _gdk_cairo_surface_extents (cairo_surface_t *surface,
337 GdkRectangle *extents)
339 double x1, x2, y1, y2;
342 g_return_val_if_fail (surface != NULL, FALSE);
343 g_return_val_if_fail (extents != NULL, FALSE);
345 cr = cairo_create (surface);
346 cairo_clip_extents (cr, &x1, &y1, &x2, &y2);
356 if (x1 < G_MININT || x1 > G_MAXINT ||
357 y1 < G_MININT || y1 > G_MAXINT ||
358 x2 > G_MAXINT || y2 > G_MAXINT)
360 extents->x = extents->y = extents->width = extents->height = 0;
367 extents->height = y2;
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.
379 * gdk_cairo_region_create_from_surface:
380 * @surface: a cairo surface
382 * Creates region that describes covers the area where the given
383 * @surface is more than 50% opaque.
385 * This function takes into account device offsets that might be
386 * set with cairo_surface_set_device_offset().
388 * Returns: A #cairo_region_t; must be freed with cairo_region_destroy()
391 gdk_cairo_region_create_from_surface (cairo_surface_t *surface)
393 cairo_region_t *region;
394 GdkRectangle extents, rect;
395 cairo_surface_t *image;
400 _gdk_cairo_surface_extents (surface, &extents);
402 if (cairo_surface_get_content (surface) == CAIRO_CONTENT_COLOR)
403 return cairo_region_create_rectangle (&extents);
405 if (cairo_surface_get_type (surface) != CAIRO_SURFACE_TYPE_IMAGE ||
406 cairo_image_surface_get_format (surface) != CAIRO_FORMAT_A1)
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);
417 image = cairo_surface_reference (surface);
419 data = cairo_image_surface_get_data (image);
420 stride = cairo_image_surface_get_stride (image);
422 region = cairo_region_create ();
424 for (y = 0; y < extents.height; y++)
426 for (x = 0; x < extents.width; x++)
428 /* Search for a continuous range of "non transparent pixels"*/
430 while (x < extents.width)
432 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
433 if (((data[x / 8] >> (x%8)) & 1) == 0)
435 if (((data[x / 8] >> (7-(x%8))) & 1) == 0)
437 /* This pixel is "transparent"*/
444 /* Add the pixels (x0, y) to (x, y+1) as a new rectangle
452 cairo_region_union_rectangle (region, &rect);
458 cairo_surface_destroy (image);
460 cairo_region_translate (region, extents.x, extents.y);