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.
21 #include "gdkdrawable.h"
22 #include "gdkinternals.h"
23 #include "gdkregion-generic.h"
28 * @drawable: a #GdkDrawable
30 * Creates a Cairo context for drawing to @drawable.
33 * Note that due to double-buffering, Cairo contexts created
34 * in a GTK+ expose event handler cannot be cached and reused
35 * between different expose events.
38 * Return value: A newly created Cairo context. Free with
39 * cairo_destroy() when you are done drawing.
44 gdk_cairo_create (GdkDrawable *drawable)
46 cairo_surface_t *surface;
49 g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);
51 surface = _gdk_drawable_ref_cairo_surface (drawable);
52 cr = cairo_create (surface);
54 if (GDK_DRAWABLE_GET_CLASS (drawable)->set_cairo_clip)
55 GDK_DRAWABLE_GET_CLASS (drawable)->set_cairo_clip (drawable, cr);
57 cairo_surface_destroy (surface);
63 * gdk_cairo_set_source_color:
67 * Sets the specified #GdkColor as the source color of @cr.
72 gdk_cairo_set_source_color (cairo_t *cr,
73 const GdkColor *color)
75 g_return_if_fail (cr != NULL);
76 g_return_if_fail (color != NULL);
78 cairo_set_source_rgb (cr,
80 color->green / 65535.,
81 color->blue / 65535.);
85 * gdk_cairo_rectangle:
87 * @rectangle: a #GdkRectangle
89 * Adds the given rectangle to the current path of @cr.
94 gdk_cairo_rectangle (cairo_t *cr,
95 const GdkRectangle *rectangle)
97 g_return_if_fail (cr != NULL);
98 g_return_if_fail (rectangle != NULL);
101 rectangle->x, rectangle->y,
102 rectangle->width, rectangle->height);
108 * @region: a #GdkRegion
110 * Adds the given region to the current path of @cr.
115 gdk_cairo_region (cairo_t *cr,
116 const GdkRegion *region)
121 g_return_if_fail (cr != NULL);
122 g_return_if_fail (region != NULL);
124 boxes = region->rects;
125 n_boxes = region->numRects;
127 for (i = 0; i < n_boxes; i++)
131 boxes[i].x2 - boxes[i].x1,
132 boxes[i].y2 - boxes[i].y1);
136 * gdk_cairo_set_source_pixbuf:
137 * @cr: a #Cairo context
138 * @pixbuf: a #GdkPixbuf
139 * @pixbuf_x: X coordinate of location to place upper left corner of @pixbuf
140 * @pixbuf_y: Y coordinate of location to place upper left corner of @pixbuf
142 * Sets the given pixbuf as the source pattern for the Cairo context.
143 * The pattern has an extend mode of %CAIRO_EXTEND_NONE and is aligned
144 * so that the origin of @pixbuf is @pixbuf_x, @pixbuf_y
149 gdk_cairo_set_source_pixbuf (cairo_t *cr,
150 const GdkPixbuf *pixbuf,
154 gint width = gdk_pixbuf_get_width (pixbuf);
155 gint height = gdk_pixbuf_get_height (pixbuf);
156 guchar *gdk_pixels = gdk_pixbuf_get_pixels (pixbuf);
157 int gdk_rowstride = gdk_pixbuf_get_rowstride (pixbuf);
158 int n_channels = gdk_pixbuf_get_n_channels (pixbuf);
160 guchar *cairo_pixels;
161 cairo_format_t format;
162 cairo_surface_t *surface;
163 static const cairo_user_data_key_t key;
167 format = CAIRO_FORMAT_RGB24;
169 format = CAIRO_FORMAT_ARGB32;
171 cairo_stride = cairo_format_stride_for_width (format, width);
172 cairo_pixels = g_malloc (height * cairo_stride);
173 surface = cairo_image_surface_create_for_data ((unsigned char *)cairo_pixels,
175 width, height, cairo_stride);
177 cairo_surface_set_user_data (surface, &key,
178 cairo_pixels, (cairo_destroy_func_t)g_free);
180 for (j = height; j; j--)
182 guchar *p = gdk_pixels;
183 guchar *q = cairo_pixels;
187 guchar *end = p + 3 * width;
191 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
206 guchar *end = p + 4 * width;
209 #define MULT(d,c,a,t) G_STMT_START { t = c * a + 0x7f; d = ((t >> 8) + t) >> 8; } G_STMT_END
213 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
214 MULT(q[0], p[2], p[3], t1);
215 MULT(q[1], p[1], p[3], t2);
216 MULT(q[2], p[0], p[3], t3);
220 MULT(q[1], p[0], p[3], t1);
221 MULT(q[2], p[1], p[3], t2);
222 MULT(q[3], p[2], p[3], t3);
232 gdk_pixels += gdk_rowstride;
233 cairo_pixels += cairo_stride;
236 cairo_set_source_surface (cr, surface, pixbuf_x, pixbuf_y);
237 cairo_surface_destroy (surface);
241 * gdk_cairo_set_source_pixmap:
242 * @cr: a #Cairo context
243 * @pixmap: a #GdkPixmap
244 * @pixmap_x: X coordinate of location to place upper left corner of @pixmap
245 * @pixmap_y: Y coordinate of location to place upper left corner of @pixmap
247 * Sets the given pixmap as the source pattern for the Cairo context.
248 * The pattern has an extend mode of %CAIRO_EXTEND_NONE and is aligned
249 * so that the origin of @pixmap is @pixmap_x, @pixmap_y
254 gdk_cairo_set_source_pixmap (cairo_t *cr,
259 cairo_surface_t *surface;
261 surface = _gdk_drawable_ref_cairo_surface (GDK_DRAWABLE (pixmap));
262 cairo_set_source_surface (cr, surface, pixmap_x, pixmap_y);
263 cairo_surface_destroy (surface);
267 #define __GDK_CAIRO_C__
268 #include "gdkaliasdef.c"