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, see <http://www.gnu.org/licenses/>.
22 #include "gdkinternals.h"
27 * SECTION:cairo_interaction
28 * @Short_description: Functions to support using cairo
29 * @Title: Cairo Interaction
31 * <link href="http://cairographics.org">Cairo</link> is a graphics
32 * library that supports vector graphics and image compositing that
33 * can be used with GDK. GTK+ does all of its drawing using cairo.
35 * GDK does not wrap the cairo API, instead it allows to create cairo
36 * contexts which can be used to draw on #GdkWindows. Additional
37 * functions allow use #GdkRectangles with cairo and to use #GdkColors,
38 * #GdkRGBAs, #GdkPixbufs and #GdkWindows as sources for drawing
44 * gdk_cairo_get_clip_rectangle:
45 * @cr: a cairo context
46 * @rect: (out) (allow-none): return location for the clip, or %NULL
48 * This is a convenience function around cairo_clip_extents().
49 * It rounds the clip extents to integer coordinates and returns
50 * a boolean indicating if a clip area exists.
52 * Returns: %TRUE if a clip rectangle exists, %FALSE if all of @cr is
53 * clipped and all drawing can be skipped
56 gdk_cairo_get_clip_rectangle (cairo_t *cr,
59 double x1, y1, x2, y2;
62 cairo_clip_extents (cr, &x1, &y1, &x2, &y2);
64 clip_exists = x1 < x2 && y1 < y2;
73 rect->x = CLAMP (x1, G_MININT, G_MAXINT);
74 rect->y = CLAMP (y1, G_MININT, G_MAXINT);
75 rect->width = CLAMP (x2 - x1, G_MININT, G_MAXINT);
76 rect->height = CLAMP (y2 - y1, G_MININT, G_MAXINT);
83 * gdk_cairo_set_source_color:
84 * @cr: a cairo context
87 * Sets the specified #GdkColor as the source color of @cr.
91 * Deprecated: 3.4: Use gdk_cairo_set_source_rgba() instead
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:
108 * @cr: a cairo context
111 * Sets the specified #GdkRGBA as the source color of @cr.
116 gdk_cairo_set_source_rgba (cairo_t *cr,
119 g_return_if_fail (cr != NULL);
120 g_return_if_fail (rgba != NULL);
122 cairo_set_source_rgba (cr,
130 * gdk_cairo_rectangle:
131 * @cr: a cairo context
132 * @rectangle: a #GdkRectangle
134 * Adds the given rectangle to the current path of @cr.
139 gdk_cairo_rectangle (cairo_t *cr,
140 const GdkRectangle *rectangle)
142 g_return_if_fail (cr != NULL);
143 g_return_if_fail (rectangle != NULL);
146 rectangle->x, rectangle->y,
147 rectangle->width, rectangle->height);
152 * @cr: a cairo context
153 * @region: a #cairo_region_t
155 * Adds the given region to the current path of @cr.
160 gdk_cairo_region (cairo_t *cr,
161 const cairo_region_t *region)
163 cairo_rectangle_int_t box;
166 g_return_if_fail (cr != NULL);
167 g_return_if_fail (region != NULL);
169 n_boxes = cairo_region_num_rectangles (region);
171 for (i = 0; i < n_boxes; i++)
173 cairo_region_get_rectangle (region, i, &box);
174 cairo_rectangle (cr, box.x, box.y, box.width, box.height);
179 * gdk_cairo_set_source_pixbuf:
180 * @cr: a cairo context
181 * @pixbuf: a #GdkPixbuf
182 * @pixbuf_x: X coordinate of location to place upper left corner of @pixbuf
183 * @pixbuf_y: Y coordinate of location to place upper left corner of @pixbuf
185 * Sets the given pixbuf as the source pattern for @cr.
187 * The pattern has an extend mode of %CAIRO_EXTEND_NONE and is aligned
188 * so that the origin of @pixbuf is @pixbuf_x, @pixbuf_y.
193 gdk_cairo_set_source_pixbuf (cairo_t *cr,
194 const GdkPixbuf *pixbuf,
198 gint width = gdk_pixbuf_get_width (pixbuf);
199 gint height = gdk_pixbuf_get_height (pixbuf);
200 guchar *gdk_pixels = gdk_pixbuf_get_pixels (pixbuf);
201 int gdk_rowstride = gdk_pixbuf_get_rowstride (pixbuf);
202 int n_channels = gdk_pixbuf_get_n_channels (pixbuf);
204 guchar *cairo_pixels;
205 cairo_format_t format;
206 cairo_surface_t *surface;
207 static const cairo_user_data_key_t key;
211 format = CAIRO_FORMAT_RGB24;
213 format = CAIRO_FORMAT_ARGB32;
215 cairo_stride = cairo_format_stride_for_width (format, width);
216 cairo_pixels = g_malloc (height * cairo_stride);
217 surface = cairo_image_surface_create_for_data ((unsigned char *)cairo_pixels,
219 width, height, cairo_stride);
221 cairo_surface_set_user_data (surface, &key,
222 cairo_pixels, (cairo_destroy_func_t)g_free);
224 for (j = height; j; j--)
226 guchar *p = gdk_pixels;
227 guchar *q = cairo_pixels;
231 guchar *end = p + 3 * width;
235 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
250 guchar *end = p + 4 * width;
253 #define MULT(d,c,a,t) G_STMT_START { t = c * a + 0x7f; d = ((t >> 8) + t) >> 8; } G_STMT_END
257 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
258 MULT(q[0], p[2], p[3], t1);
259 MULT(q[1], p[1], p[3], t2);
260 MULT(q[2], p[0], p[3], t3);
264 MULT(q[1], p[0], p[3], t1);
265 MULT(q[2], p[1], p[3], t2);
266 MULT(q[3], p[2], p[3], t3);
276 gdk_pixels += gdk_rowstride;
277 cairo_pixels += cairo_stride;
280 cairo_set_source_surface (cr, surface, pixbuf_x, pixbuf_y);
281 cairo_surface_destroy (surface);
285 * gdk_cairo_set_source_window:
286 * @cr: a cairo context
287 * @window: a #GdkWindow
288 * @x: X coordinate of location to place upper left corner of @window
289 * @y: Y coordinate of location to place upper left corner of @window
291 * Sets the given window as the source pattern for @cr.
293 * The pattern has an extend mode of %CAIRO_EXTEND_NONE and is aligned
294 * so that the origin of @window is @x, @y. The window contains all its
295 * subwindows when rendering.
297 * Note that the contents of @window are undefined outside of the
298 * visible part of @window, so use this function with care.
303 gdk_cairo_set_source_window (cairo_t *cr,
308 cairo_surface_t *surface;
310 g_return_if_fail (cr != NULL);
311 g_return_if_fail (GDK_IS_WINDOW (window));
313 surface = _gdk_window_ref_cairo_surface (window);
314 cairo_set_source_surface (cr, surface, x, y);
315 cairo_surface_destroy (surface);
319 * _gdk_cairo_surface_extents:
320 * @surface: surface to measure
321 * @extents: (out): rectangle to put the extents
323 * Measures the area covered by @surface and puts it into @extents.
325 * Note that this function respects device offsets set on @surface.
326 * If @surface is unbounded, the resulting extents will be empty and
327 * not be a maximal sized rectangle. This is to avoid careless coding.
328 * You must explicitly check the return value of you want to handle
331 * Returns: %TRUE if the extents fit in a #GdkRectangle, %FALSE if not
334 _gdk_cairo_surface_extents (cairo_surface_t *surface,
335 GdkRectangle *extents)
337 double x1, x2, y1, y2;
340 g_return_val_if_fail (surface != NULL, FALSE);
341 g_return_val_if_fail (extents != NULL, FALSE);
343 cr = cairo_create (surface);
344 cairo_clip_extents (cr, &x1, &y1, &x2, &y2);
354 if (x1 < G_MININT || x1 > G_MAXINT ||
355 y1 < G_MININT || y1 > G_MAXINT ||
356 x2 > G_MAXINT || y2 > G_MAXINT)
358 extents->x = extents->y = extents->width = extents->height = 0;
365 extents->height = y2;
370 /* This function originally from Jean-Edouard Lachand-Robert, and
371 * available at www.codeguru.com. Simplified for our needs, not sure
372 * how much of the original code left any longer. Now handles just
373 * one-bit deep bitmaps (in Window parlance, ie those that GDK calls
374 * bitmaps (and not pixmaps), with zero pixels being transparent.
377 * gdk_cairo_region_create_from_surface:
378 * @surface: a cairo surface
380 * Creates region that describes covers the area where the given
381 * @surface is more than 50% opaque.
383 * This function takes into account device offsets that might be
384 * set with cairo_surface_set_device_offset().
386 * Returns: A #cairo_region_t; must be freed with cairo_region_destroy()
389 gdk_cairo_region_create_from_surface (cairo_surface_t *surface)
391 cairo_region_t *region;
392 GdkRectangle extents, rect;
393 cairo_surface_t *image;
398 _gdk_cairo_surface_extents (surface, &extents);
400 if (cairo_surface_get_content (surface) == CAIRO_CONTENT_COLOR)
401 return cairo_region_create_rectangle (&extents);
403 if (cairo_surface_get_type (surface) != CAIRO_SURFACE_TYPE_IMAGE ||
404 cairo_image_surface_get_format (surface) != CAIRO_FORMAT_A1)
406 /* coerce to an A1 image */
407 image = cairo_image_surface_create (CAIRO_FORMAT_A1,
408 extents.width, extents.height);
409 cr = cairo_create (image);
410 cairo_set_source_surface (cr, surface, -extents.x, -extents.y);
415 image = cairo_surface_reference (surface);
417 data = cairo_image_surface_get_data (image);
418 stride = cairo_image_surface_get_stride (image);
420 region = cairo_region_create ();
422 for (y = 0; y < extents.height; y++)
424 for (x = 0; x < extents.width; x++)
426 /* Search for a continuous range of "non transparent pixels"*/
428 while (x < extents.width)
430 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
431 if (((data[x / 8] >> (x%8)) & 1) == 0)
433 if (((data[x / 8] >> (7-(x%8))) & 1) == 0)
435 /* This pixel is "transparent"*/
442 /* Add the pixels (x0, y) to (x, y+1) as a new rectangle
450 cairo_region_union_rectangle (region, &rect);
456 cairo_surface_destroy (image);
458 cairo_region_translate (region, extents.x, extents.y);