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 * @drawable: a #GdkDrawable
32 * Creates a Cairo context for drawing to @drawable.
35 * Note that due to double-buffering, Cairo contexts created
36 * in a GTK+ expose event handler cannot be cached and reused
37 * between different expose events.
40 * Return value: A newly created Cairo context. Free with
41 * cairo_destroy() when you are done drawing.
46 gdk_cairo_create (GdkDrawable *drawable)
48 cairo_surface_t *surface;
51 g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);
53 surface = _gdk_drawable_ref_cairo_surface (drawable);
54 cr = cairo_create (surface);
56 if (GDK_DRAWABLE_GET_CLASS (drawable)->set_cairo_clip)
57 GDK_DRAWABLE_GET_CLASS (drawable)->set_cairo_clip (drawable, cr);
59 cairo_surface_destroy (surface);
65 * gdk_cairo_reset_clip:
67 * @drawable: a #GdkDrawable
69 * Resets the clip region for a Cairo context created by gdk_cairo_create().
71 * This resets the clip region to the "empty" state for the given drawable.
72 * This is required for non-native windows since a direct call to
73 * cairo_reset_clip() would unset the clip region inherited from the
74 * drawable (i.e. the window clip region), and thus let you e.g.
75 * draw outside your window.
77 * This is rarely needed though, since most code just create a new cairo_t
78 * using gdk_cairo_create() each time they want to draw something.
83 gdk_cairo_reset_clip (cairo_t *cr,
84 GdkDrawable *drawable)
86 cairo_reset_clip (cr);
88 if (GDK_DRAWABLE_GET_CLASS (drawable)->set_cairo_clip)
89 GDK_DRAWABLE_GET_CLASS (drawable)->set_cairo_clip (drawable, cr);
93 * gdk_cairo_get_clip_rectangle:
94 * @cr: a cairo context
95 * @rect: (out) (allow-none): return location for the clip, or %NULL
97 * This is a convenience function around cairo_clip_extents(). It rounds
98 * the clip extents to integer coordinates and returns a boolean
99 * indicating if a clip area exists.
101 * Returns: %TRUE if a clip rectangle exists, %FALSE if all of @cr is
102 * clipped and all drawing can be skipped.
105 gdk_cairo_get_clip_rectangle (cairo_t *cr,
108 double x1, y1, x2, y2;
109 gboolean clip_exists;
111 cairo_clip_extents (cr, &x1, &y1, &x2, &y2);
113 clip_exists = x1 < x2 && y1 < y2;
122 rect->x = CLAMP (x1, G_MININT, G_MAXINT);
123 rect->y = CLAMP (y1, G_MININT, G_MAXINT);
124 rect->width = CLAMP (x2 - x1, G_MININT, G_MAXINT);
125 rect->height = CLAMP (y2 - y1, G_MININT, G_MAXINT);
132 * gdk_cairo_set_source_color:
134 * @color: a #GdkColor
136 * Sets the specified #GdkColor as the source color of @cr.
141 gdk_cairo_set_source_color (cairo_t *cr,
142 const GdkColor *color)
144 g_return_if_fail (cr != NULL);
145 g_return_if_fail (color != NULL);
147 cairo_set_source_rgb (cr,
149 color->green / 65535.,
150 color->blue / 65535.);
154 * gdk_cairo_rectangle:
156 * @rectangle: a #GdkRectangle
158 * Adds the given rectangle to the current path of @cr.
163 gdk_cairo_rectangle (cairo_t *cr,
164 const GdkRectangle *rectangle)
166 g_return_if_fail (cr != NULL);
167 g_return_if_fail (rectangle != NULL);
170 rectangle->x, rectangle->y,
171 rectangle->width, rectangle->height);
177 * @region: a #cairo_region_t
179 * Adds the given region to the current path of @cr.
184 gdk_cairo_region (cairo_t *cr,
185 const cairo_region_t *region)
187 cairo_rectangle_int_t box;
190 g_return_if_fail (cr != NULL);
191 g_return_if_fail (region != NULL);
193 n_boxes = cairo_region_num_rectangles (region);
195 for (i = 0; i < n_boxes; i++)
197 cairo_region_get_rectangle (region, i, &box);
198 cairo_rectangle (cr, box.x, box.y, box.width, box.height);
203 * gdk_cairo_set_source_pixbuf:
204 * @cr: a #Cairo context
205 * @pixbuf: a #GdkPixbuf
206 * @pixbuf_x: X coordinate of location to place upper left corner of @pixbuf
207 * @pixbuf_y: Y coordinate of location to place upper left corner of @pixbuf
209 * Sets the given pixbuf as the source pattern for the Cairo context.
210 * The pattern has an extend mode of %CAIRO_EXTEND_NONE and is aligned
211 * so that the origin of @pixbuf is @pixbuf_x, @pixbuf_y
216 gdk_cairo_set_source_pixbuf (cairo_t *cr,
217 const GdkPixbuf *pixbuf,
221 gint width = gdk_pixbuf_get_width (pixbuf);
222 gint height = gdk_pixbuf_get_height (pixbuf);
223 guchar *gdk_pixels = gdk_pixbuf_get_pixels (pixbuf);
224 int gdk_rowstride = gdk_pixbuf_get_rowstride (pixbuf);
225 int n_channels = gdk_pixbuf_get_n_channels (pixbuf);
227 guchar *cairo_pixels;
228 cairo_format_t format;
229 cairo_surface_t *surface;
230 static const cairo_user_data_key_t key;
234 format = CAIRO_FORMAT_RGB24;
236 format = CAIRO_FORMAT_ARGB32;
238 cairo_stride = cairo_format_stride_for_width (format, width);
239 cairo_pixels = g_malloc (height * cairo_stride);
240 surface = cairo_image_surface_create_for_data ((unsigned char *)cairo_pixels,
242 width, height, cairo_stride);
244 cairo_surface_set_user_data (surface, &key,
245 cairo_pixels, (cairo_destroy_func_t)g_free);
247 for (j = height; j; j--)
249 guchar *p = gdk_pixels;
250 guchar *q = cairo_pixels;
254 guchar *end = p + 3 * width;
258 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
273 guchar *end = p + 4 * width;
276 #define MULT(d,c,a,t) G_STMT_START { t = c * a + 0x7f; d = ((t >> 8) + t) >> 8; } G_STMT_END
280 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
281 MULT(q[0], p[2], p[3], t1);
282 MULT(q[1], p[1], p[3], t2);
283 MULT(q[2], p[0], p[3], t3);
287 MULT(q[1], p[0], p[3], t1);
288 MULT(q[2], p[1], p[3], t2);
289 MULT(q[3], p[2], p[3], t3);
299 gdk_pixels += gdk_rowstride;
300 cairo_pixels += cairo_stride;
303 cairo_set_source_surface (cr, surface, pixbuf_x, pixbuf_y);
304 cairo_surface_destroy (surface);
308 * gdk_cairo_set_source_window:
309 * @cr: a #Cairo context
310 * @window: a #GdkWindow
311 * @x: X coordinate of location to place upper left corner of @window
312 * @y: Y coordinate of location to place upper left corner of @window
314 * Sets the given window as the source pattern for the Cairo context.
315 * The pattern has an extend mode of %CAIRO_EXTEND_NONE and is aligned
316 * so that the origin of @window is @x, @y. The window contains all its
317 * subwindows when rendering.
319 * Note that the contents of @window are undefined outside of the
320 * visible part of @window, so use this function with care.
325 gdk_cairo_set_source_window (cairo_t *cr,
330 cairo_surface_t *surface;
332 g_return_if_fail (cr != NULL);
333 g_return_if_fail (GDK_IS_WINDOW (window));
335 surface = _gdk_drawable_ref_cairo_surface (GDK_DRAWABLE (window));
336 cairo_set_source_surface (cr, surface, x, y);
337 cairo_surface_destroy (surface);
341 * _gdk_cairo_surface_extents:
342 * @surface: surface to measure
343 * @extents: (out): rectangle to put the extents
345 * Measures the area covered by @surface and puts it into @extents.
346 * Note that this function respects device offsets set on @surface.
347 * if @surface is unbounded, the resulting extents will be empty and
348 * not be a maximal sized rectangle. This is to avoid careless coding.
349 * You must explicitly check the return value of you want to handle
352 * Returns: %TRUE if the extents fit in a #GdkRectangle, %FALSE if not.
355 _gdk_cairo_surface_extents (cairo_surface_t *surface,
356 GdkRectangle *extents)
358 double x1, x2, y1, y2;
361 g_return_val_if_fail (surface != NULL, FALSE);
362 g_return_val_if_fail (extents != NULL, FALSE);
364 cr = cairo_create (surface);
365 cairo_clip_extents (cr, &x1, &y1, &x2, &y2);
374 if (x1 < G_MININT || x1 > G_MAXINT ||
375 y1 < G_MININT || y1 > G_MAXINT ||
376 x2 > G_MAXINT || y2 > G_MAXINT)
378 extents->x = extents->y = extents->width = extents->height = 0;
385 extents->height = y2;
390 /* This function originally from Jean-Edouard Lachand-Robert, and
391 * available at www.codeguru.com. Simplified for our needs, not sure
392 * how much of the original code left any longer. Now handles just
393 * one-bit deep bitmaps (in Window parlance, ie those that GDK calls
394 * bitmaps (and not pixmaps), with zero pixels being transparent.
397 * gdk_cairo_region_create_from_surface:
398 * @surface: A surface
400 * Creates region that describes covers the area where the given @surface
401 * is more than 50% opaque. This function takes into account device
402 * offsets that might be set with cairo_surface_set_device_offset().
404 * Returns: A new region
407 gdk_cairo_region_create_from_surface (cairo_surface_t *surface)
409 cairo_region_t *region;
410 GdkRectangle extents, rect;
411 cairo_surface_t *image;
416 _gdk_cairo_surface_extents (surface, &extents);
418 if (cairo_surface_get_content (surface) == CAIRO_CONTENT_COLOR)
419 return cairo_region_create_rectangle (&extents);
421 if (cairo_surface_get_type (surface) != CAIRO_SURFACE_TYPE_IMAGE ||
422 cairo_image_surface_get_format (surface) != CAIRO_FORMAT_A1)
424 /* coerce to an A1 image */
425 image = cairo_image_surface_create (CAIRO_FORMAT_A1,
426 extents.width, extents.height);
427 cr = cairo_create (image);
428 cairo_set_source_surface (cr, surface, extents.x, extents.y);
433 image = cairo_surface_reference (surface);
435 data = cairo_image_surface_get_data (image);
436 stride = cairo_image_surface_get_stride (image);
438 region = cairo_region_create ();
440 for (y = 0; y < extents.height; y++)
442 for (x = 0; x < extents.width; x++)
444 /* Search for a continuous range of "non transparent pixels"*/
446 while (x < extents.width)
448 if (((data[x / 8] >> (x%8)) & 1) == 0)
449 /* This pixel is "transparent"*/
456 /* Add the pixels (x0, y) to (x, y+1) as a new rectangle
464 cairo_region_union_rectangle (region, &rect);
470 cairo_surface_destroy (image);
472 cairo_region_translate (region, extents.x, extents.y);