1/* GDK - The GIMP Drawing Kit
2 * Copyright (C) 2005 Red Hat, Inc.
3 *
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.
8 *
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.
13 *
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/>.
16 */
17
18#include "config.h"
19
20#include "gdkcairo.h"
21
22#include "gdkinternals.h"
23
24#include <math.h>
25
26/**
27 * SECTION:cairo_interaction
28 * @Short_description: Functions to support using cairo
29 * @Title: Cairo Interaction
30 *
31 * [Cairo](http://cairographics.org) 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.
34 *
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
39 * operations.
40 */
41
42
43/**
44 * gdk_cairo_get_clip_rectangle:
45 * @cr: a cairo context
46 * @rect: (out) (allow-none): return location for the clip, or %NULL
47 *
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.
51 *
52 * Returns: %TRUE if a clip rectangle exists, %FALSE if all of @cr is
53 * clipped and all drawing can be skipped
54 */
55gboolean
56gdk_cairo_get_clip_rectangle (cairo_t *cr,
57 GdkRectangle *rect)
58{
59 double x1, y1, x2, y2;
60 gboolean clip_exists;
61
62 cairo_clip_extents (cr, &x1, &y1, &x2, &y2);
63
64 clip_exists = x1 < x2 && y1 < y2;
65
66 if (rect)
67 {
68 x1 = floor (x1);
69 y1 = floor (y1);
70 x2 = ceil (x2);
71 y2 = ceil (y2);
72
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);
77 }
78
79 return clip_exists;
80}
81
82/**
83 * gdk_cairo_set_source_color:
84 * @cr: a cairo context
85 * @color: a #GdkColor
86 *
87 * Sets the specified #GdkColor as the source color of @cr.
88 *
89 * Since: 2.8
90 *
91 * Deprecated: 3.4: Use gdk_cairo_set_source_rgba() instead
92 */
93void
94gdk_cairo_set_source_color (cairo_t *cr,
95 const GdkColor *color)
96{
97 g_return_if_fail (cr != NULL);
98 g_return_if_fail (color != NULL);
99
100 cairo_set_source_rgb (cr,
101 color->red / 65535.,
102 color->green / 65535.,
103 color->blue / 65535.);
104}
105
106/**
107 * gdk_cairo_set_source_rgba:
108 * @cr: a cairo context
109 * @rgba: a #GdkRGBA
110 *
111 * Sets the specified #GdkRGBA as the source color of @cr.
112 *
113 * Since: 3.0
114 */
115void
116gdk_cairo_set_source_rgba (cairo_t *cr,
117 const GdkRGBA *rgba)
118{
119 g_return_if_fail (cr != NULL);
120 g_return_if_fail (rgba != NULL);
121
122 cairo_set_source_rgba (cr,
123 rgba->red,
124 rgba->green,
125 rgba->blue,
126 rgba->alpha);
127}
128
129/**
130 * gdk_cairo_rectangle:
131 * @cr: a cairo context
132 * @rectangle: a #GdkRectangle
133 *
134 * Adds the given rectangle to the current path of @cr.
135 *
136 * Since: 2.8
137 */
138void
139gdk_cairo_rectangle (cairo_t *cr,
140 const GdkRectangle *rectangle)
141{
142 g_return_if_fail (cr != NULL);
143 g_return_if_fail (rectangle != NULL);
144
145 cairo_rectangle (cr,
146 rectangle->x, rectangle->y,
147 rectangle->width, rectangle->height);
148}
149
150/**
151 * gdk_cairo_region:
152 * @cr: a cairo context
153 * @region: a #cairo_region_t
154 *
155 * Adds the given region to the current path of @cr.
156 *
157 * Since: 2.8
158 */
159void
160gdk_cairo_region (cairo_t *cr,
161 const cairo_region_t *region)
162{
163 cairo_rectangle_int_t box;
164 gint n_boxes, i;
165
166 g_return_if_fail (cr != NULL);
167 g_return_if_fail (region != NULL);
168
169 n_boxes = cairo_region_num_rectangles (region);
170
171 for (i = 0; i < n_boxes; i++)
172 {
173 cairo_region_get_rectangle (region, i, &box);
174 cairo_rectangle (cr, box.x, box.y, box.width, box.height);
175 }
176}
177
178static void
179gdk_cairo_surface_paint_pixbuf (cairo_surface_t *surface,
180 const GdkPixbuf *pixbuf)
181{
182 gint width, height;
183 guchar *gdk_pixels, *cairo_pixels;
184 int gdk_rowstride, cairo_stride;
185 int n_channels;
186 int j;
187
188 if (cairo_surface_status (surface) != CAIRO_STATUS_SUCCESS)
189 return;
190
191 /* This function can't just copy any pixbuf to any surface, be
192 * sure to read the invariants here before calling it */
193
194 g_assert (cairo_surface_get_type (surface) == CAIRO_SURFACE_TYPE_IMAGE);
195 g_assert (cairo_image_surface_get_format (surface) == CAIRO_FORMAT_RGB24 ||
196 cairo_image_surface_get_format (surface) == CAIRO_FORMAT_ARGB32);
197 g_assert (cairo_image_surface_get_width (surface) == gdk_pixbuf_get_width (pixbuf));
198 g_assert (cairo_image_surface_get_height (surface) == gdk_pixbuf_get_height (pixbuf));
199
200 cairo_surface_flush (surface);
201
202 width = gdk_pixbuf_get_width (pixbuf);
203 height = gdk_pixbuf_get_height (pixbuf);
204 gdk_pixels = gdk_pixbuf_get_pixels (pixbuf);
205 gdk_rowstride = gdk_pixbuf_get_rowstride (pixbuf);
206 n_channels = gdk_pixbuf_get_n_channels (pixbuf);
207 cairo_stride = cairo_image_surface_get_stride (surface);
208 cairo_pixels = cairo_image_surface_get_data (surface);
209
210 for (j = height; j; j--)
211 {
212 guchar *p = gdk_pixels;
213 guchar *q = cairo_pixels;
214
215 if (n_channels == 3)
216 {
217 guchar *end = p + 3 * width;
218
219 while (p < end)
220 {
221#if G_BYTE_ORDER == G_LITTLE_ENDIAN
222 q[0] = p[2];
223 q[1] = p[1];
224 q[2] = p[0];
225#else
226 q[1] = p[0];
227 q[2] = p[1];
228 q[3] = p[2];
229#endif
230 p += 3;
231 q += 4;
232 }
233 }
234 else
235 {
236 guchar *end = p + 4 * width;
237 guint t1,t2,t3;
238
239#define MULT(d,c,a,t) G_STMT_START { t = c * a + 0x80; d = ((t >> 8) + t) >> 8; } G_STMT_END
240
241 while (p < end)
242 {
243#if G_BYTE_ORDER == G_LITTLE_ENDIAN
244 MULT(q[0], p[2], p[3], t1);
245 MULT(q[1], p[1], p[3], t2);
246 MULT(q[2], p[0], p[3], t3);
247 q[3] = p[3];
248#else
249 q[0] = p[3];
250 MULT(q[1], p[0], p[3], t1);
251 MULT(q[2], p[1], p[3], t2);
252 MULT(q[3], p[2], p[3], t3);
253#endif
254
255 p += 4;
256 q += 4;
257 }
258
259#undef MULT
260 }
261
262 gdk_pixels += gdk_rowstride;
263 cairo_pixels += cairo_stride;
264 }
265
266 cairo_surface_mark_dirty (surface);
267}
268
269/**
270 * gdk_cairo_surface_create_from_pixbuf:
271 * @pixbuf: a #GdkPixbuf
272 * @scale: the scale of the new surface, or 0 to use same as @window
273 * @for_window: (allow-none): The window this will be drawn to, or %NULL
274 *
275 * Creates an image surface with the same contents as
276 * the pixbuf.
277 *
278 * Returns: a new cairo surface, must be freed with cairo_surface_destroy()
279 *
280 * Since: 3.10
281 */
282cairo_surface_t *
283gdk_cairo_surface_create_from_pixbuf (const GdkPixbuf *pixbuf,
284 int scale,
285 GdkWindow *for_window)
286{
287 cairo_format_t format;
288 cairo_surface_t *surface;
289
290 g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
291 g_return_val_if_fail (scale >= 0, NULL);
292 g_return_val_if_fail (for_window == NULL || GDK_IS_WINDOW (for_window), NULL);
293
294 if (gdk_pixbuf_get_n_channels (pixbuf) == 3)
295 format = CAIRO_FORMAT_RGB24;
296 else
297 format = CAIRO_FORMAT_ARGB32;
298
299 surface =
300 gdk_window_create_similar_image_surface (for_window,
301 format,
302 gdk_pixbuf_get_width (pixbuf),
303 gdk_pixbuf_get_height (pixbuf),
304 scale);
305
306 gdk_cairo_surface_paint_pixbuf (surface, pixbuf);
307
308 return surface;
309}
310
311/**
312 * gdk_cairo_set_source_pixbuf:
313 * @cr: a cairo context
314 * @pixbuf: a #GdkPixbuf
315 * @pixbuf_x: X coordinate of location to place upper left corner of @pixbuf
316 * @pixbuf_y: Y coordinate of location to place upper left corner of @pixbuf
317 *
318 * Sets the given pixbuf as the source pattern for @cr.
319 *
320 * The pattern has an extend mode of %CAIRO_EXTEND_NONE and is aligned
321 * so that the origin of @pixbuf is @pixbuf_x, @pixbuf_y.
322 *
323 * Since: 2.8
324 */
325void
326gdk_cairo_set_source_pixbuf (cairo_t *cr,
327 const GdkPixbuf *pixbuf,
328 gdouble pixbuf_x,
329 gdouble pixbuf_y)
330{
331 cairo_format_t format;
332 cairo_surface_t *surface;
333
334 if (gdk_pixbuf_get_n_channels (pixbuf) == 3)
335 format = CAIRO_FORMAT_RGB24;
336 else
337 format = CAIRO_FORMAT_ARGB32;
338
339 surface = cairo_surface_create_similar_image (cairo_get_target (cr),
340 format,
341 gdk_pixbuf_get_width (pixbuf),
342 gdk_pixbuf_get_height (pixbuf));
343
344 gdk_cairo_surface_paint_pixbuf (surface, pixbuf);
345
346 cairo_set_source_surface (cr, surface, pixbuf_x, pixbuf_y);
347 cairo_surface_destroy (surface);
348}
349
350/**
351 * gdk_cairo_set_source_window:
352 * @cr: a cairo context
353 * @window: a #GdkWindow
354 * @x: X coordinate of location to place upper left corner of @window
355 * @y: Y coordinate of location to place upper left corner of @window
356 *
357 * Sets the given window as the source pattern for @cr.
358 *
359 * The pattern has an extend mode of %CAIRO_EXTEND_NONE and is aligned
360 * so that the origin of @window is @x, @y. The window contains all its
361 * subwindows when rendering.
362 *
363 * Note that the contents of @window are undefined outside of the
364 * visible part of @window, so use this function with care.
365 *
366 * Since: 2.24
367 */
368void
369gdk_cairo_set_source_window (cairo_t *cr,
370 GdkWindow *window,
371 gdouble x,
372 gdouble y)
373{
374 cairo_surface_t *surface;
375
376 g_return_if_fail (cr != NULL);
377 g_return_if_fail (GDK_IS_WINDOW (window));
378
379 surface = _gdk_window_ref_cairo_surface (window);
380 cairo_set_source_surface (cr, surface, x, y);
381 cairo_surface_destroy (surface);
382}
383
384/*
385 * _gdk_cairo_surface_extents:
386 * @surface: surface to measure
387 * @extents: (out): rectangle to put the extents
388 *
389 * Measures the area covered by @surface and puts it into @extents.
390 *
391 * Note that this function respects device offsets set on @surface.
392 * If @surface is unbounded, the resulting extents will be empty and
393 * not be a maximal sized rectangle. This is to avoid careless coding.
394 * You must explicitly check the return value of you want to handle
395 * that case.
396 *
397 * Returns: %TRUE if the extents fit in a #GdkRectangle, %FALSE if not
398 */
399gboolean
400_gdk_cairo_surface_extents (cairo_surface_t *surface,
401 GdkRectangle *extents)
402{
403 double x1, x2, y1, y2;
404 cairo_t *cr;
405
406 g_return_val_if_fail (surface != NULL, FALSE);
407 g_return_val_if_fail (extents != NULL, FALSE);
408
409 cr = cairo_create (surface);
410 cairo_clip_extents (cr, &x1, &y1, &x2, &y2);
411 cairo_destroy (cr);
412
413 x1 = floor (x1);
414 y1 = floor (y1);
415 x2 = ceil (x2);
416 y2 = ceil (y2);
417 x2 -= x1;
418 y2 -= y1;
419
420 if (x1 < G_MININT || x1 > G_MAXINT ||
421 y1 < G_MININT || y1 > G_MAXINT ||
422 x2 > G_MAXINT || y2 > G_MAXINT)
423 {
424 extents->x = extents->y = extents->width = extents->height = 0;
425 return FALSE;
426 }
427
428 extents->x = x1;
429 extents->y = y1;
430 extents->width = x2;
431 extents->height = y2;
432
433 return TRUE;
434}
435
436/* This function originally from Jean-Edouard Lachand-Robert, and
437 * available at www.codeguru.com. Simplified for our needs, not sure
438 * how much of the original code left any longer. Now handles just
439 * one-bit deep bitmaps (in Window parlance, ie those that GDK calls
440 * bitmaps (and not pixmaps), with zero pixels being transparent.
441 */
442/**
443 * gdk_cairo_region_create_from_surface:
444 * @surface: a cairo surface
445 *
446 * Creates region that describes covers the area where the given
447 * @surface is more than 50% opaque.
448 *
449 * This function takes into account device offsets that might be
450 * set with cairo_surface_set_device_offset().
451 *
452 * Returns: A #cairo_region_t; must be freed with cairo_region_destroy()
453 */
454cairo_region_t *
455gdk_cairo_region_create_from_surface (cairo_surface_t *surface)
456{
457 cairo_region_t *region;
458 GdkRectangle extents, rect;
459 cairo_surface_t *image;
460 cairo_t *cr;
461 gint x, y, stride;
462 guchar *data;
463
464 _gdk_cairo_surface_extents (surface, &extents);
465
466 if (cairo_surface_get_content (surface) == CAIRO_CONTENT_COLOR)
467 return cairo_region_create_rectangle (&extents);
468
469 if (cairo_surface_get_type (surface) != CAIRO_SURFACE_TYPE_IMAGE ||
470 cairo_image_surface_get_format (surface) != CAIRO_FORMAT_A1)
471 {
472 /* coerce to an A1 image */
473 image = cairo_image_surface_create (CAIRO_FORMAT_A1,
474 extents.width, extents.height);
475 cr = cairo_create (image);
476 cairo_set_source_surface (cr, surface, -extents.x, -extents.y);
477 cairo_paint (cr);
478 cairo_destroy (cr);
479 }
480 else
481 image = cairo_surface_reference (surface);
482
483 /* Flush the surface to make sure that the rendering is up to date. */
484 cairo_surface_flush (image);
485
486 data = cairo_image_surface_get_data (image);
487 stride = cairo_image_surface_get_stride (image);
488
489 region = cairo_region_create ();
490
491 for (y = 0; y < extents.height; y++)
492 {
493 for (x = 0; x < extents.width; x++)
494 {
495 /* Search for a continuous range of "non transparent pixels"*/
496 gint x0 = x;
497 while (x < extents.width)
498 {
499#if G_BYTE_ORDER == G_LITTLE_ENDIAN
500 if (((data[x / 8] >> (x%8)) & 1) == 0)
501#else
502 if (((data[x / 8] >> (7-(x%8))) & 1) == 0)
503#endif
504 /* This pixel is "transparent"*/
505 break;
506 x++;
507 }
508
509 if (x > x0)
510 {
511 /* Add the pixels (x0, y) to (x, y+1) as a new rectangle
512 * in the region
513 */
514 rect.x = x0;
515 rect.width = x - x0;
516 rect.y = y;
517 rect.height = 1;
518
519 cairo_region_union_rectangle (region, &rect);
520 }
521 }
522 data += stride;
523 }
524
525 cairo_surface_destroy (image);
526
527 cairo_region_translate (region, extents.x, extents.y);
528
529 return region;
530}
531
532cairo_region_t *
533gdk_cairo_region_from_clip (cairo_t *cr)
534{
535 cairo_rectangle_list_t *rectangles;
536 cairo_region_t *region;
537 int i;
538
539 rectangles = cairo_copy_clip_rectangle_list (cr);
540
541 if (rectangles->status != CAIRO_STATUS_SUCCESS)
542 return NULL;
543
544 region = cairo_region_create ();
545 for (i = 0; i < rectangles->num_rectangles; i++)
546 {
547 cairo_rectangle_int_t clip_rect;
548 cairo_rectangle_t *rect;
549
550 rect = &rectangles->rectangles[i];
551
552 /* Here we assume clip rects are ints for direct targets, which
553 is true for cairo */
554 clip_rect.x = (int)rect->x;
555 clip_rect.y = (int)rect->y;
556 clip_rect.width = (int)rect->width;
557 clip_rect.height = (int)rect->height;
558
559 cairo_region_union_rectangle (region, &clip_rect);
560 }
561
562 cairo_rectangle_list_destroy (rectangles);
563
564 return region;
565}
566