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.
47 * @drawable: a #GdkDrawable
49 * Creates a Cairo context for drawing to @drawable.
52 * Note that due to double-buffering, Cairo contexts created
53 * in a GTK+ expose event handler cannot be cached and reused
54 * between different expose events.
57 * Return value: A newly created Cairo context. Free with
58 * cairo_destroy() when you are done drawing.
63 gdk_cairo_create (GdkDrawable *drawable)
65 cairo_surface_t *surface;
68 g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);
70 surface = _gdk_drawable_ref_cairo_surface (drawable);
71 cr = cairo_create (surface);
73 if (GDK_DRAWABLE_GET_CLASS (drawable)->set_cairo_clip)
74 GDK_DRAWABLE_GET_CLASS (drawable)->set_cairo_clip (drawable, cr);
76 cairo_surface_destroy (surface);
82 * gdk_cairo_reset_clip:
84 * @drawable: a #GdkDrawable
86 * Resets the clip region for a Cairo context created by gdk_cairo_create().
88 * This resets the clip region to the "empty" state for the given drawable.
89 * This is required for non-native windows since a direct call to
90 * cairo_reset_clip() would unset the clip region inherited from the
91 * drawable (i.e. the window clip region), and thus let you e.g.
92 * draw outside your window.
94 * This is rarely needed though, since most code just create a new cairo_t
95 * using gdk_cairo_create() each time they want to draw something.
100 gdk_cairo_reset_clip (cairo_t *cr,
101 GdkDrawable *drawable)
103 cairo_reset_clip (cr);
105 if (GDK_DRAWABLE_GET_CLASS (drawable)->set_cairo_clip)
106 GDK_DRAWABLE_GET_CLASS (drawable)->set_cairo_clip (drawable, cr);
110 * gdk_cairo_get_clip_rectangle:
111 * @cr: a cairo context
112 * @rect: (out) (allow-none): return location for the clip, or %NULL
114 * This is a convenience function around cairo_clip_extents(). It rounds
115 * the clip extents to integer coordinates and returns a boolean
116 * indicating if a clip area exists.
118 * Returns: %TRUE if a clip rectangle exists, %FALSE if all of @cr is
119 * clipped and all drawing can be skipped.
122 gdk_cairo_get_clip_rectangle (cairo_t *cr,
125 double x1, y1, x2, y2;
126 gboolean clip_exists;
128 cairo_clip_extents (cr, &x1, &y1, &x2, &y2);
130 clip_exists = x1 < x2 && y1 < y2;
139 rect->x = CLAMP (x1, G_MININT, G_MAXINT);
140 rect->y = CLAMP (y1, G_MININT, G_MAXINT);
141 rect->width = CLAMP (x2 - x1, G_MININT, G_MAXINT);
142 rect->height = CLAMP (y2 - y1, G_MININT, G_MAXINT);
149 * gdk_cairo_set_source_color:
151 * @color: a #GdkColor
153 * Sets the specified #GdkColor as the source color of @cr.
158 gdk_cairo_set_source_color (cairo_t *cr,
159 const GdkColor *color)
161 g_return_if_fail (cr != NULL);
162 g_return_if_fail (color != NULL);
164 cairo_set_source_rgb (cr,
166 color->green / 65535.,
167 color->blue / 65535.);
171 * gdk_cairo_rectangle:
173 * @rectangle: a #GdkRectangle
175 * Adds the given rectangle to the current path of @cr.
180 gdk_cairo_rectangle (cairo_t *cr,
181 const GdkRectangle *rectangle)
183 g_return_if_fail (cr != NULL);
184 g_return_if_fail (rectangle != NULL);
187 rectangle->x, rectangle->y,
188 rectangle->width, rectangle->height);
194 * @region: a #cairo_region_t
196 * Adds the given region to the current path of @cr.
201 gdk_cairo_region (cairo_t *cr,
202 const cairo_region_t *region)
204 cairo_rectangle_int_t box;
207 g_return_if_fail (cr != NULL);
208 g_return_if_fail (region != NULL);
210 n_boxes = cairo_region_num_rectangles (region);
212 for (i = 0; i < n_boxes; i++)
214 cairo_region_get_rectangle (region, i, &box);
215 cairo_rectangle (cr, box.x, box.y, box.width, box.height);
220 * gdk_cairo_set_source_pixbuf:
221 * @cr: a #Cairo context
222 * @pixbuf: a #GdkPixbuf
223 * @pixbuf_x: X coordinate of location to place upper left corner of @pixbuf
224 * @pixbuf_y: Y coordinate of location to place upper left corner of @pixbuf
226 * Sets the given pixbuf as the source pattern for the Cairo context.
227 * The pattern has an extend mode of %CAIRO_EXTEND_NONE and is aligned
228 * so that the origin of @pixbuf is @pixbuf_x, @pixbuf_y
233 gdk_cairo_set_source_pixbuf (cairo_t *cr,
234 const GdkPixbuf *pixbuf,
238 gint width = gdk_pixbuf_get_width (pixbuf);
239 gint height = gdk_pixbuf_get_height (pixbuf);
240 guchar *gdk_pixels = gdk_pixbuf_get_pixels (pixbuf);
241 int gdk_rowstride = gdk_pixbuf_get_rowstride (pixbuf);
242 int n_channels = gdk_pixbuf_get_n_channels (pixbuf);
244 guchar *cairo_pixels;
245 cairo_format_t format;
246 cairo_surface_t *surface;
247 static const cairo_user_data_key_t key;
251 format = CAIRO_FORMAT_RGB24;
253 format = CAIRO_FORMAT_ARGB32;
255 cairo_stride = cairo_format_stride_for_width (format, width);
256 cairo_pixels = g_malloc (height * cairo_stride);
257 surface = cairo_image_surface_create_for_data ((unsigned char *)cairo_pixels,
259 width, height, cairo_stride);
261 cairo_surface_set_user_data (surface, &key,
262 cairo_pixels, (cairo_destroy_func_t)g_free);
264 for (j = height; j; j--)
266 guchar *p = gdk_pixels;
267 guchar *q = cairo_pixels;
271 guchar *end = p + 3 * width;
275 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
290 guchar *end = p + 4 * width;
293 #define MULT(d,c,a,t) G_STMT_START { t = c * a + 0x7f; d = ((t >> 8) + t) >> 8; } G_STMT_END
297 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
298 MULT(q[0], p[2], p[3], t1);
299 MULT(q[1], p[1], p[3], t2);
300 MULT(q[2], p[0], p[3], t3);
304 MULT(q[1], p[0], p[3], t1);
305 MULT(q[2], p[1], p[3], t2);
306 MULT(q[3], p[2], p[3], t3);
316 gdk_pixels += gdk_rowstride;
317 cairo_pixels += cairo_stride;
320 cairo_set_source_surface (cr, surface, pixbuf_x, pixbuf_y);
321 cairo_surface_destroy (surface);
325 * gdk_cairo_set_source_window:
326 * @cr: a #Cairo context
327 * @window: a #GdkWindow
328 * @x: X coordinate of location to place upper left corner of @window
329 * @y: Y coordinate of location to place upper left corner of @window
331 * Sets the given window as the source pattern for the Cairo context.
332 * The pattern has an extend mode of %CAIRO_EXTEND_NONE and is aligned
333 * so that the origin of @window is @x, @y. The window contains all its
334 * subwindows when rendering.
336 * Note that the contents of @window are undefined outside of the
337 * visible part of @window, so use this function with care.
342 gdk_cairo_set_source_window (cairo_t *cr,
347 cairo_surface_t *surface;
349 g_return_if_fail (cr != NULL);
350 g_return_if_fail (GDK_IS_WINDOW (window));
352 surface = _gdk_drawable_ref_cairo_surface (GDK_DRAWABLE (window));
353 cairo_set_source_surface (cr, surface, x, y);
354 cairo_surface_destroy (surface);
358 * _gdk_cairo_surface_extents:
359 * @surface: surface to measure
360 * @extents: (out): rectangle to put the extents
362 * Measures the area covered by @surface and puts it into @extents.
363 * Note that this function respects device offsets set on @surface.
364 * if @surface is unbounded, the resulting extents will be empty and
365 * not be a maximal sized rectangle. This is to avoid careless coding.
366 * You must explicitly check the return value of you want to handle
369 * Returns: %TRUE if the extents fit in a #GdkRectangle, %FALSE if not.
372 _gdk_cairo_surface_extents (cairo_surface_t *surface,
373 GdkRectangle *extents)
375 double x1, x2, y1, y2;
378 g_return_val_if_fail (surface != NULL, FALSE);
379 g_return_val_if_fail (extents != NULL, FALSE);
381 cr = cairo_create (surface);
382 cairo_clip_extents (cr, &x1, &y1, &x2, &y2);
392 if (x1 < G_MININT || x1 > G_MAXINT ||
393 y1 < G_MININT || y1 > G_MAXINT ||
394 x2 > G_MAXINT || y2 > G_MAXINT)
396 extents->x = extents->y = extents->width = extents->height = 0;
403 extents->height = y2;
408 /* This function originally from Jean-Edouard Lachand-Robert, and
409 * available at www.codeguru.com. Simplified for our needs, not sure
410 * how much of the original code left any longer. Now handles just
411 * one-bit deep bitmaps (in Window parlance, ie those that GDK calls
412 * bitmaps (and not pixmaps), with zero pixels being transparent.
415 * gdk_cairo_region_create_from_surface:
416 * @surface: A surface
418 * Creates region that describes covers the area where the given @surface
419 * is more than 50% opaque. This function takes into account device
420 * offsets that might be set with cairo_surface_set_device_offset().
422 * Returns: A #cairo_region_t. This must be freed with cairo_region_destroy()
426 gdk_cairo_region_create_from_surface (cairo_surface_t *surface)
428 cairo_region_t *region;
429 GdkRectangle extents, rect;
430 cairo_surface_t *image;
435 _gdk_cairo_surface_extents (surface, &extents);
437 if (cairo_surface_get_content (surface) == CAIRO_CONTENT_COLOR)
438 return cairo_region_create_rectangle (&extents);
440 if (cairo_surface_get_type (surface) != CAIRO_SURFACE_TYPE_IMAGE ||
441 cairo_image_surface_get_format (surface) != CAIRO_FORMAT_A1)
443 /* coerce to an A1 image */
444 image = cairo_image_surface_create (CAIRO_FORMAT_A1,
445 extents.width, extents.height);
446 cr = cairo_create (image);
447 cairo_set_source_surface (cr, surface, -extents.x, -extents.y);
452 image = cairo_surface_reference (surface);
454 data = cairo_image_surface_get_data (image);
455 stride = cairo_image_surface_get_stride (image);
457 region = cairo_region_create ();
459 for (y = 0; y < extents.height; y++)
461 for (x = 0; x < extents.width; x++)
463 /* Search for a continuous range of "non transparent pixels"*/
465 while (x < extents.width)
467 if (((data[x / 8] >> (x%8)) & 1) == 0)
468 /* This pixel is "transparent"*/
475 /* Add the pixels (x0, y) to (x, y+1) as a new rectangle
483 cairo_region_union_rectangle (region, &rect);
489 cairo_surface_destroy (image);
491 cairo_region_translate (region, extents.x, extents.y);