summaryrefslogtreecommitdiff
path: root/gdk/gdkcairo.c
blob: ef8ec25627553eefd8994c63e93f0aa9ae8ef9c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/* GDK - The GIMP Drawing Kit
 * Copyright (C) 2005 Red Hat, Inc. 
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "gdkcairo.h"
#include "gdkdrawable.h"
#include "gdkinternals.h"
#include "gdkregion-generic.h"
#include "gdkalias.h"

/**
 * gdk_cairo_create:
 * @drawable: a #GdkDrawable
 * 
 * Creates a Cairo context for drawing to @drawable.
 *
 * <note><para>
 * Note that due to double-buffering, Cairo contexts created 
 * in a GTK+ expose event handler cannot be cached and reused 
 * between different expose events. 
 * </para></note>
 *
 * Return value: A newly created Cairo context. Free with
 *  cairo_destroy() when you are done drawing.
 * 
 * Since: 2.8
 **/
cairo_t *
gdk_cairo_create (GdkDrawable *drawable)
{
  cairo_surface_t *surface;
  cairo_t *cr;
    
  g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);

  surface = _gdk_drawable_ref_cairo_surface (drawable);
  cr = cairo_create (surface);

  if (GDK_DRAWABLE_GET_CLASS (drawable)->set_cairo_clip)
    GDK_DRAWABLE_GET_CLASS (drawable)->set_cairo_clip (drawable, cr);
    
  cairo_surface_destroy (surface);

  return cr;
}

/**
 * gdk_cairo_reset_clip:
 * @cr: a #cairo_t
 * @drawable: a #GdkDrawable
 *
 * Resets the clip region for a Cairo context created by gdk_cairo_create().
 *
 * This resets the clip region to the "empty" state for the given drawable.
 * This is required for non-native windows since a direct call to
 * cairo_reset_clip() would unset the clip region inherited from the
 * drawable (i.e. the window clip region), and thus let you e.g.
 * draw outside your window.
 *
 * This is rarely needed though, since most code just create a new cairo_t
 * using gdk_cairo_create() each time they want to draw something.
 *
 * Since: 2.18
 **/
void
gdk_cairo_reset_clip (cairo_t            *cr,
		      GdkDrawable        *drawable)
{
  cairo_reset_clip (cr);

  if (GDK_DRAWABLE_GET_CLASS (drawable)->set_cairo_clip)
    GDK_DRAWABLE_GET_CLASS (drawable)->set_cairo_clip (drawable, cr);
}

/**
 * gdk_cairo_set_source_color:
 * @cr: a #cairo_t
 * @color: a #GdkColor
 * 
 * Sets the specified #GdkColor as the source color of @cr.
 *
 * Since: 2.8
 **/
void
gdk_cairo_set_source_color (cairo_t        *cr,
			    const GdkColor *color)
{
  g_return_if_fail (cr != NULL);
  g_return_if_fail (color != NULL);
    
  cairo_set_source_rgb (cr,
			color->red / 65535.,
			color->green / 65535.,
			color->blue / 65535.);
}

/**
 * gdk_cairo_rectangle:
 * @cr: a #cairo_t
 * @rectangle: a #GdkRectangle
 * 
 * Adds the given rectangle to the current path of @cr.
 *
 * Since: 2.8
 **/
void
gdk_cairo_rectangle (cairo_t            *cr,
		     const GdkRectangle *rectangle)
{
  g_return_if_fail (cr != NULL);
  g_return_if_fail (rectangle != NULL);

  cairo_rectangle (cr,
		   rectangle->x,     rectangle->y,
		   rectangle->width, rectangle->height);
}

/**
 * gdk_cairo_region:
 * @cr: a #cairo_t
 * @region: a #GdkRegion
 * 
 * Adds the given region to the current path of @cr.
 *
 * Since: 2.8
 **/
void
gdk_cairo_region (cairo_t         *cr,
		  const GdkRegion *region)
{
  GdkRegionBox *boxes;
  gint n_boxes, i;

  g_return_if_fail (cr != NULL);
  g_return_if_fail (region != NULL);

  boxes = region->rects;
  n_boxes = region->numRects;

  for (i = 0; i < n_boxes; i++)
    cairo_rectangle (cr,
		     boxes[i].x1,
		     boxes[i].y1,
		     boxes[i].x2 - boxes[i].x1,
		     boxes[i].y2 - boxes[i].y1);
}

/**
 * gdk_cairo_set_source_pixbuf:
 * @cr: a #Cairo context
 * @pixbuf: a #GdkPixbuf
 * @pixbuf_x: X coordinate of location to place upper left corner of @pixbuf
 * @pixbuf_y: Y coordinate of location to place upper left corner of @pixbuf
 * 
 * Sets the given pixbuf as the source pattern for the Cairo context.
 * The pattern has an extend mode of %CAIRO_EXTEND_NONE and is aligned
 * so that the origin of @pixbuf is @pixbuf_x, @pixbuf_y
 *
 * Since: 2.8
 **/
void
gdk_cairo_set_source_pixbuf (cairo_t         *cr,
			     const GdkPixbuf *pixbuf,
			     double           pixbuf_x,
			     double           pixbuf_y)
{
  gint width = gdk_pixbuf_get_width (pixbuf);
  gint height = gdk_pixbuf_get_height (pixbuf);
  guchar *gdk_pixels = gdk_pixbuf_get_pixels (pixbuf);
  int gdk_rowstride = gdk_pixbuf_get_rowstride (pixbuf);
  int n_channels = gdk_pixbuf_get_n_channels (pixbuf);
  int cairo_stride;
  guchar *cairo_pixels;
  cairo_format_t format;
  cairo_surface_t *surface;
  static const cairo_user_data_key_t key;
  int j;

  if (n_channels == 3)
    format = CAIRO_FORMAT_RGB24;
  else
    format = CAIRO_FORMAT_ARGB32;

  cairo_stride = cairo_format_stride_for_width (format, width);
  cairo_pixels = g_malloc (height * cairo_stride);
  surface = cairo_image_surface_create_for_data ((unsigned char *)cairo_pixels,
                                                 format,
                                                 width, height, cairo_stride);

  cairo_surface_set_user_data (surface, &key,
			       cairo_pixels, (cairo_destroy_func_t)g_free);

  for (j = height; j; j--)
    {
      guchar *p = gdk_pixels;
      guchar *q = cairo_pixels;

      if (n_channels == 3)
	{
	  guchar *end = p + 3 * width;
	  
	  while (p < end)
	    {
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
	      q[0] = p[2];
	      q[1] = p[1];
	      q[2] = p[0];
#else	  
	      q[1] = p[0];
	      q[2] = p[1];
	      q[3] = p[2];
#endif
	      p += 3;
	      q += 4;
	    }
	}
      else
	{
	  guchar *end = p + 4 * width;
	  guint t1,t2,t3;
	    
#define MULT(d,c,a,t) G_STMT_START { t = c * a + 0x7f; d = ((t >> 8) + t) >> 8; } G_STMT_END

	  while (p < end)
	    {
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
	      MULT(q[0], p[2], p[3], t1);
	      MULT(q[1], p[1], p[3], t2);
	      MULT(q[2], p[0], p[3], t3);
	      q[3] = p[3];
#else	  
	      q[0] = p[3];
	      MULT(q[1], p[0], p[3], t1);
	      MULT(q[2], p[1], p[3], t2);
	      MULT(q[3], p[2], p[3], t3);
#endif
	      
	      p += 4;
	      q += 4;
	    }
	  
#undef MULT
	}

      gdk_pixels += gdk_rowstride;
      cairo_pixels += cairo_stride;
    }

  cairo_set_source_surface (cr, surface, pixbuf_x, pixbuf_y);
  cairo_surface_destroy (surface);
}

/**
 * gdk_cairo_set_source_pixmap:
 * @cr: a #Cairo context
 * @pixmap: a #GdkPixmap
 * @pixmap_x: X coordinate of location to place upper left corner of @pixmap
 * @pixmap_y: Y coordinate of location to place upper left corner of @pixmap
 * 
 * Sets the given pixmap as the source pattern for the Cairo context.
 * The pattern has an extend mode of %CAIRO_EXTEND_NONE and is aligned
 * so that the origin of @pixmap is @pixmap_x, @pixmap_y
 *
 * Since: 2.10
 **/
void
gdk_cairo_set_source_pixmap (cairo_t   *cr,
			     GdkPixmap *pixmap,
			     double     pixmap_x,
			     double     pixmap_y)
{
  cairo_surface_t *surface;
  
  surface = _gdk_drawable_ref_cairo_surface (GDK_DRAWABLE (pixmap));
  cairo_set_source_surface (cr, surface, pixmap_x, pixmap_y);
  cairo_surface_destroy (surface);
}


#define __GDK_CAIRO_C__
#include "gdkaliasdef.c"