2008-07-01 22:57:50 +00:00
|
|
|
/* GDK - The GIMP Drawing Kit
|
2005-05-11 19:16:19 +00:00
|
|
|
* 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
|
2011-02-09 07:43:07 +00:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
2005-05-11 19:16:19 +00:00
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2012-02-27 13:01:10 +00:00
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
2005-05-11 19:16:19 +00:00
|
|
|
*/
|
|
|
|
|
2010-10-15 02:05:51 +00:00
|
|
|
#include "config.h"
|
2010-08-14 02:40:52 +00:00
|
|
|
|
2010-10-15 02:05:51 +00:00
|
|
|
#include "gdkcairo.h"
|
2010-08-14 02:40:52 +00:00
|
|
|
|
2005-05-11 19:16:19 +00:00
|
|
|
#include "gdkinternals.h"
|
2010-07-09 00:34:45 +00:00
|
|
|
|
2010-10-15 02:05:51 +00:00
|
|
|
#include <math.h>
|
2005-05-11 19:16:19 +00:00
|
|
|
|
2010-10-04 01:04:09 +00:00
|
|
|
/**
|
|
|
|
* SECTION:cairo_interaction
|
2011-02-09 07:43:07 +00:00
|
|
|
* @Short_description: Functions to support using cairo
|
2010-10-04 01:04:09 +00:00
|
|
|
* @Title: Cairo Interaction
|
|
|
|
*
|
2014-02-07 02:07:03 +00:00
|
|
|
* [Cairo](http://cairographics.org) is a graphics
|
2010-10-04 01:04:09 +00:00
|
|
|
* library that supports vector graphics and image compositing that
|
2011-02-09 07:43:07 +00:00
|
|
|
* can be used with GDK. GTK+ does all of its drawing using cairo.
|
2010-10-04 01:04:09 +00:00
|
|
|
*
|
2011-02-09 07:43:07 +00:00
|
|
|
* GDK does not wrap the cairo API, instead it allows to create cairo
|
2010-10-12 14:53:44 +00:00
|
|
|
* contexts which can be used to draw on #GdkWindows. Additional
|
|
|
|
* functions allow use #GdkRectangles with cairo and to use #GdkColors,
|
2011-02-09 07:43:07 +00:00
|
|
|
* #GdkRGBAs, #GdkPixbufs and #GdkWindows as sources for drawing
|
|
|
|
* operations.
|
2010-10-04 01:04:09 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2010-09-08 23:45:54 +00:00
|
|
|
/**
|
|
|
|
* gdk_cairo_get_clip_rectangle:
|
|
|
|
* @cr: a cairo context
|
|
|
|
* @rect: (out) (allow-none): return location for the clip, or %NULL
|
|
|
|
*
|
2011-02-09 07:43:07 +00:00
|
|
|
* This is a convenience function around cairo_clip_extents().
|
|
|
|
* It rounds the clip extents to integer coordinates and returns
|
|
|
|
* a boolean indicating if a clip area exists.
|
2010-09-08 23:45:54 +00:00
|
|
|
*
|
|
|
|
* Returns: %TRUE if a clip rectangle exists, %FALSE if all of @cr is
|
2011-02-09 07:43:07 +00:00
|
|
|
* clipped and all drawing can be skipped
|
|
|
|
*/
|
2010-09-08 23:45:54 +00:00
|
|
|
gboolean
|
|
|
|
gdk_cairo_get_clip_rectangle (cairo_t *cr,
|
|
|
|
GdkRectangle *rect)
|
|
|
|
{
|
|
|
|
double x1, y1, x2, y2;
|
|
|
|
gboolean clip_exists;
|
|
|
|
|
|
|
|
cairo_clip_extents (cr, &x1, &y1, &x2, &y2);
|
|
|
|
|
|
|
|
clip_exists = x1 < x2 && y1 < y2;
|
|
|
|
|
|
|
|
if (rect)
|
|
|
|
{
|
|
|
|
x1 = floor (x1);
|
|
|
|
y1 = floor (y1);
|
|
|
|
x2 = ceil (x2);
|
|
|
|
y2 = ceil (y2);
|
|
|
|
|
|
|
|
rect->x = CLAMP (x1, G_MININT, G_MAXINT);
|
|
|
|
rect->y = CLAMP (y1, G_MININT, G_MAXINT);
|
|
|
|
rect->width = CLAMP (x2 - x1, G_MININT, G_MAXINT);
|
|
|
|
rect->height = CLAMP (y2 - y1, G_MININT, G_MAXINT);
|
|
|
|
}
|
|
|
|
|
|
|
|
return clip_exists;
|
|
|
|
}
|
|
|
|
|
2010-12-03 13:50:05 +00:00
|
|
|
/**
|
|
|
|
* gdk_cairo_set_source_rgba:
|
2011-02-09 07:43:07 +00:00
|
|
|
* @cr: a cairo context
|
2010-12-03 13:50:05 +00:00
|
|
|
* @rgba: a #GdkRGBA
|
|
|
|
*
|
|
|
|
* Sets the specified #GdkRGBA as the source color of @cr.
|
|
|
|
*
|
|
|
|
* Since: 3.0
|
2011-02-09 07:43:07 +00:00
|
|
|
*/
|
2010-10-21 13:23:48 +00:00
|
|
|
void
|
|
|
|
gdk_cairo_set_source_rgba (cairo_t *cr,
|
|
|
|
const GdkRGBA *rgba)
|
|
|
|
{
|
|
|
|
g_return_if_fail (cr != NULL);
|
|
|
|
g_return_if_fail (rgba != NULL);
|
|
|
|
|
|
|
|
cairo_set_source_rgba (cr,
|
|
|
|
rgba->red,
|
|
|
|
rgba->green,
|
|
|
|
rgba->blue,
|
|
|
|
rgba->alpha);
|
|
|
|
}
|
|
|
|
|
2005-05-11 19:16:19 +00:00
|
|
|
/**
|
|
|
|
* gdk_cairo_rectangle:
|
2011-02-09 07:43:07 +00:00
|
|
|
* @cr: a cairo context
|
2005-05-11 19:16:19 +00:00
|
|
|
* @rectangle: a #GdkRectangle
|
2011-02-09 07:43:07 +00:00
|
|
|
*
|
2005-05-11 19:16:19 +00:00
|
|
|
* Adds the given rectangle to the current path of @cr.
|
2005-06-12 05:20:04 +00:00
|
|
|
*
|
|
|
|
* Since: 2.8
|
2011-02-09 07:43:07 +00:00
|
|
|
*/
|
2005-05-11 19:16:19 +00:00
|
|
|
void
|
2008-01-14 14:02:12 +00:00
|
|
|
gdk_cairo_rectangle (cairo_t *cr,
|
2011-02-09 07:43:07 +00:00
|
|
|
const GdkRectangle *rectangle)
|
2005-05-11 19:16:19 +00:00
|
|
|
{
|
|
|
|
g_return_if_fail (cr != NULL);
|
|
|
|
g_return_if_fail (rectangle != NULL);
|
|
|
|
|
|
|
|
cairo_rectangle (cr,
|
2011-02-09 07:43:07 +00:00
|
|
|
rectangle->x, rectangle->y,
|
|
|
|
rectangle->width, rectangle->height);
|
2005-05-11 19:16:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gdk_cairo_region:
|
2011-02-09 07:43:07 +00:00
|
|
|
* @cr: a cairo context
|
2010-06-28 12:54:37 +00:00
|
|
|
* @region: a #cairo_region_t
|
2011-02-09 07:43:07 +00:00
|
|
|
*
|
2005-05-11 19:16:19 +00:00
|
|
|
* Adds the given region to the current path of @cr.
|
2005-06-12 05:20:04 +00:00
|
|
|
*
|
|
|
|
* Since: 2.8
|
2011-02-09 07:43:07 +00:00
|
|
|
*/
|
2005-05-11 19:16:19 +00:00
|
|
|
void
|
2013-09-17 05:13:03 +00:00
|
|
|
gdk_cairo_region (cairo_t *cr,
|
2011-02-09 07:43:07 +00:00
|
|
|
const cairo_region_t *region)
|
2005-05-11 19:16:19 +00:00
|
|
|
{
|
2010-06-28 12:27:55 +00:00
|
|
|
cairo_rectangle_int_t box;
|
2005-05-11 19:16:19 +00:00
|
|
|
gint n_boxes, i;
|
|
|
|
|
|
|
|
g_return_if_fail (cr != NULL);
|
|
|
|
g_return_if_fail (region != NULL);
|
|
|
|
|
2010-06-28 12:27:55 +00:00
|
|
|
n_boxes = cairo_region_num_rectangles (region);
|
2005-05-11 19:16:19 +00:00
|
|
|
|
|
|
|
for (i = 0; i < n_boxes; i++)
|
2010-06-28 12:27:55 +00:00
|
|
|
{
|
|
|
|
cairo_region_get_rectangle (region, i, &box);
|
|
|
|
cairo_rectangle (cr, box.x, box.y, box.width, box.height);
|
|
|
|
}
|
2005-05-11 19:16:19 +00:00
|
|
|
}
|
|
|
|
|
2013-09-24 14:34:24 +00:00
|
|
|
static void
|
|
|
|
gdk_cairo_surface_paint_pixbuf (cairo_surface_t *surface,
|
|
|
|
const GdkPixbuf *pixbuf)
|
2005-05-11 19:16:19 +00:00
|
|
|
{
|
2013-09-24 14:34:24 +00:00
|
|
|
gint width, height;
|
|
|
|
guchar *gdk_pixels, *cairo_pixels;
|
|
|
|
int gdk_rowstride, cairo_stride;
|
|
|
|
int n_channels;
|
2005-05-11 19:16:19 +00:00
|
|
|
int j;
|
|
|
|
|
2015-02-20 23:03:49 +00:00
|
|
|
if (cairo_surface_status (surface) != CAIRO_STATUS_SUCCESS)
|
|
|
|
return;
|
|
|
|
|
2013-09-24 14:34:24 +00:00
|
|
|
/* This function can't just copy any pixbuf to any surface, be
|
|
|
|
* sure to read the invariants here before calling it */
|
2005-05-11 19:16:19 +00:00
|
|
|
|
2013-09-24 14:34:24 +00:00
|
|
|
g_assert (cairo_surface_get_type (surface) == CAIRO_SURFACE_TYPE_IMAGE);
|
|
|
|
g_assert (cairo_image_surface_get_format (surface) == CAIRO_FORMAT_RGB24 ||
|
|
|
|
cairo_image_surface_get_format (surface) == CAIRO_FORMAT_ARGB32);
|
|
|
|
g_assert (cairo_image_surface_get_width (surface) == gdk_pixbuf_get_width (pixbuf));
|
|
|
|
g_assert (cairo_image_surface_get_height (surface) == gdk_pixbuf_get_height (pixbuf));
|
|
|
|
|
|
|
|
cairo_surface_flush (surface);
|
|
|
|
|
|
|
|
width = gdk_pixbuf_get_width (pixbuf);
|
|
|
|
height = gdk_pixbuf_get_height (pixbuf);
|
|
|
|
gdk_pixels = gdk_pixbuf_get_pixels (pixbuf);
|
|
|
|
gdk_rowstride = gdk_pixbuf_get_rowstride (pixbuf);
|
|
|
|
n_channels = gdk_pixbuf_get_n_channels (pixbuf);
|
2013-06-24 10:12:43 +00:00
|
|
|
cairo_stride = cairo_image_surface_get_stride (surface);
|
|
|
|
cairo_pixels = cairo_image_surface_get_data (surface);
|
2005-05-11 19:16:19 +00:00
|
|
|
|
|
|
|
for (j = height; j; j--)
|
|
|
|
{
|
|
|
|
guchar *p = gdk_pixels;
|
|
|
|
guchar *q = cairo_pixels;
|
|
|
|
|
|
|
|
if (n_channels == 3)
|
2011-02-09 07:43:07 +00:00
|
|
|
{
|
|
|
|
guchar *end = p + 3 * width;
|
|
|
|
|
|
|
|
while (p < end)
|
|
|
|
{
|
2005-06-17 14:17:43 +00:00
|
|
|
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
|
2011-02-09 07:43:07 +00:00
|
|
|
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];
|
2005-05-11 19:16:19 +00:00
|
|
|
#endif
|
2011-02-09 07:43:07 +00:00
|
|
|
p += 3;
|
|
|
|
q += 4;
|
|
|
|
}
|
|
|
|
}
|
2005-05-11 19:16:19 +00:00
|
|
|
else
|
2011-02-09 07:43:07 +00:00
|
|
|
{
|
|
|
|
guchar *end = p + 4 * width;
|
|
|
|
guint t1,t2,t3;
|
|
|
|
|
2013-09-05 22:42:45 +00:00
|
|
|
#define MULT(d,c,a,t) G_STMT_START { t = c * a + 0x80; d = ((t >> 8) + t) >> 8; } G_STMT_END
|
2005-05-11 19:16:19 +00:00
|
|
|
|
2011-02-09 07:43:07 +00:00
|
|
|
while (p < end)
|
|
|
|
{
|
2005-05-11 19:16:19 +00:00
|
|
|
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
|
2011-02-09 07:43:07 +00:00
|
|
|
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);
|
2005-05-11 19:16:19 +00:00
|
|
|
#endif
|
2011-02-09 07:43:07 +00:00
|
|
|
|
|
|
|
p += 4;
|
|
|
|
q += 4;
|
|
|
|
}
|
|
|
|
|
2005-05-11 19:16:19 +00:00
|
|
|
#undef MULT
|
2011-02-09 07:43:07 +00:00
|
|
|
}
|
2005-05-11 19:16:19 +00:00
|
|
|
|
|
|
|
gdk_pixels += gdk_rowstride;
|
2008-05-26 04:25:25 +00:00
|
|
|
cairo_pixels += cairo_stride;
|
2005-05-11 19:16:19 +00:00
|
|
|
}
|
|
|
|
|
2013-06-24 10:12:43 +00:00
|
|
|
cairo_surface_mark_dirty (surface);
|
2013-09-24 14:34:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gdk_cairo_surface_create_from_pixbuf:
|
|
|
|
* @pixbuf: a #GdkPixbuf
|
|
|
|
* @scale: the scale of the new surface, or 0 to use same as @window
|
2014-03-12 15:49:14 +00:00
|
|
|
* @for_window: (allow-none): The window this will be drawn to, or %NULL
|
2013-09-24 14:34:24 +00:00
|
|
|
*
|
|
|
|
* Creates an image surface with the same contents as
|
|
|
|
* the pixbuf.
|
|
|
|
*
|
|
|
|
* Returns: a new cairo surface, must be freed with cairo_surface_destroy()
|
|
|
|
*
|
|
|
|
* Since: 3.10
|
|
|
|
*/
|
|
|
|
cairo_surface_t *
|
|
|
|
gdk_cairo_surface_create_from_pixbuf (const GdkPixbuf *pixbuf,
|
|
|
|
int scale,
|
|
|
|
GdkWindow *for_window)
|
|
|
|
{
|
|
|
|
cairo_format_t format;
|
|
|
|
cairo_surface_t *surface;
|
|
|
|
|
2013-12-06 18:21:27 +00:00
|
|
|
g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);
|
2014-10-06 12:19:21 +00:00
|
|
|
g_return_val_if_fail (scale >= 0, NULL);
|
2013-12-06 18:21:27 +00:00
|
|
|
g_return_val_if_fail (for_window == NULL || GDK_IS_WINDOW (for_window), NULL);
|
|
|
|
|
2013-09-24 14:34:24 +00:00
|
|
|
if (gdk_pixbuf_get_n_channels (pixbuf) == 3)
|
|
|
|
format = CAIRO_FORMAT_RGB24;
|
|
|
|
else
|
|
|
|
format = CAIRO_FORMAT_ARGB32;
|
|
|
|
|
|
|
|
surface =
|
|
|
|
gdk_window_create_similar_image_surface (for_window,
|
|
|
|
format,
|
|
|
|
gdk_pixbuf_get_width (pixbuf),
|
|
|
|
gdk_pixbuf_get_height (pixbuf),
|
|
|
|
scale);
|
|
|
|
|
|
|
|
gdk_cairo_surface_paint_pixbuf (surface, pixbuf);
|
|
|
|
|
2013-06-24 10:12:43 +00:00
|
|
|
return surface;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 @cr.
|
|
|
|
*
|
|
|
|
* 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,
|
|
|
|
gdouble pixbuf_x,
|
|
|
|
gdouble pixbuf_y)
|
|
|
|
{
|
2013-09-24 14:37:37 +00:00
|
|
|
cairo_format_t format;
|
2013-06-24 10:12:43 +00:00
|
|
|
cairo_surface_t *surface;
|
|
|
|
|
2013-09-24 14:37:37 +00:00
|
|
|
if (gdk_pixbuf_get_n_channels (pixbuf) == 3)
|
|
|
|
format = CAIRO_FORMAT_RGB24;
|
|
|
|
else
|
|
|
|
format = CAIRO_FORMAT_ARGB32;
|
|
|
|
|
|
|
|
surface = cairo_surface_create_similar_image (cairo_get_target (cr),
|
|
|
|
format,
|
|
|
|
gdk_pixbuf_get_width (pixbuf),
|
|
|
|
gdk_pixbuf_get_height (pixbuf));
|
|
|
|
|
2013-09-24 15:43:44 +00:00
|
|
|
gdk_cairo_surface_paint_pixbuf (surface, pixbuf);
|
|
|
|
|
2005-05-11 19:16:19 +00:00
|
|
|
cairo_set_source_surface (cr, surface, pixbuf_x, pixbuf_y);
|
2005-06-10 19:53:47 +00:00
|
|
|
cairo_surface_destroy (surface);
|
2005-05-11 19:16:19 +00:00
|
|
|
}
|
|
|
|
|
2005-12-30 07:16:35 +00:00
|
|
|
/**
|
2010-08-27 10:52:59 +00:00
|
|
|
* gdk_cairo_set_source_window:
|
2011-02-09 07:43:07 +00:00
|
|
|
* @cr: a cairo context
|
2010-08-27 10:52:59 +00:00
|
|
|
* @window: a #GdkWindow
|
|
|
|
* @x: X coordinate of location to place upper left corner of @window
|
|
|
|
* @y: Y coordinate of location to place upper left corner of @window
|
2010-09-28 00:59:08 +00:00
|
|
|
*
|
2011-02-09 07:43:07 +00:00
|
|
|
* Sets the given window as the source pattern for @cr.
|
|
|
|
*
|
2005-12-30 07:16:35 +00:00
|
|
|
* The pattern has an extend mode of %CAIRO_EXTEND_NONE and is aligned
|
2010-08-27 10:52:59 +00:00
|
|
|
* so that the origin of @window is @x, @y. The window contains all its
|
|
|
|
* subwindows when rendering.
|
2010-09-28 00:59:08 +00:00
|
|
|
*
|
2010-08-27 10:52:59 +00:00
|
|
|
* Note that the contents of @window are undefined outside of the
|
|
|
|
* visible part of @window, so use this function with care.
|
2005-12-30 07:16:35 +00:00
|
|
|
*
|
2010-09-28 00:59:08 +00:00
|
|
|
* Since: 2.24
|
|
|
|
*/
|
2005-12-30 07:16:35 +00:00
|
|
|
void
|
2010-08-27 10:52:59 +00:00
|
|
|
gdk_cairo_set_source_window (cairo_t *cr,
|
2010-09-28 00:59:08 +00:00
|
|
|
GdkWindow *window,
|
2011-02-09 07:43:07 +00:00
|
|
|
gdouble x,
|
|
|
|
gdouble y)
|
2005-12-30 07:16:35 +00:00
|
|
|
{
|
|
|
|
cairo_surface_t *surface;
|
2011-02-09 07:43:07 +00:00
|
|
|
|
2010-08-27 10:52:59 +00:00
|
|
|
g_return_if_fail (cr != NULL);
|
|
|
|
g_return_if_fail (GDK_IS_WINDOW (window));
|
|
|
|
|
2010-11-22 16:23:31 +00:00
|
|
|
surface = _gdk_window_ref_cairo_surface (window);
|
2010-08-27 10:52:59 +00:00
|
|
|
cairo_set_source_surface (cr, surface, x, y);
|
2005-12-30 07:16:35 +00:00
|
|
|
cairo_surface_destroy (surface);
|
|
|
|
}
|
2010-08-14 02:40:52 +00:00
|
|
|
|
2014-08-18 12:17:49 +00:00
|
|
|
/*
|
2010-08-14 02:40:52 +00:00
|
|
|
* _gdk_cairo_surface_extents:
|
|
|
|
* @surface: surface to measure
|
|
|
|
* @extents: (out): rectangle to put the extents
|
|
|
|
*
|
|
|
|
* Measures the area covered by @surface and puts it into @extents.
|
2011-02-09 07:43:07 +00:00
|
|
|
*
|
2010-08-14 02:40:52 +00:00
|
|
|
* Note that this function respects device offsets set on @surface.
|
2011-02-09 07:43:07 +00:00
|
|
|
* If @surface is unbounded, the resulting extents will be empty and
|
2010-08-14 02:40:52 +00:00
|
|
|
* not be a maximal sized rectangle. This is to avoid careless coding.
|
|
|
|
* You must explicitly check the return value of you want to handle
|
|
|
|
* that case.
|
|
|
|
*
|
2011-02-09 07:43:07 +00:00
|
|
|
* Returns: %TRUE if the extents fit in a #GdkRectangle, %FALSE if not
|
|
|
|
*/
|
2010-08-14 02:40:52 +00:00
|
|
|
gboolean
|
|
|
|
_gdk_cairo_surface_extents (cairo_surface_t *surface,
|
2011-02-09 07:43:07 +00:00
|
|
|
GdkRectangle *extents)
|
2010-08-14 02:40:52 +00:00
|
|
|
{
|
|
|
|
double x1, x2, y1, y2;
|
|
|
|
cairo_t *cr;
|
|
|
|
|
|
|
|
g_return_val_if_fail (surface != NULL, FALSE);
|
|
|
|
g_return_val_if_fail (extents != NULL, FALSE);
|
|
|
|
|
|
|
|
cr = cairo_create (surface);
|
|
|
|
cairo_clip_extents (cr, &x1, &y1, &x2, &y2);
|
2010-10-10 15:16:52 +00:00
|
|
|
cairo_destroy (cr);
|
2010-08-14 02:40:52 +00:00
|
|
|
|
|
|
|
x1 = floor (x1);
|
|
|
|
y1 = floor (y1);
|
|
|
|
x2 = ceil (x2);
|
|
|
|
y2 = ceil (y2);
|
|
|
|
x2 -= x1;
|
|
|
|
y2 -= y1;
|
2011-02-09 07:43:07 +00:00
|
|
|
|
2010-08-14 02:40:52 +00:00
|
|
|
if (x1 < G_MININT || x1 > G_MAXINT ||
|
|
|
|
y1 < G_MININT || y1 > G_MAXINT ||
|
|
|
|
x2 > G_MAXINT || y2 > G_MAXINT)
|
|
|
|
{
|
|
|
|
extents->x = extents->y = extents->width = extents->height = 0;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
extents->x = x1;
|
|
|
|
extents->y = y1;
|
|
|
|
extents->width = x2;
|
|
|
|
extents->height = y2;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This function originally from Jean-Edouard Lachand-Robert, and
|
|
|
|
* available at www.codeguru.com. Simplified for our needs, not sure
|
|
|
|
* how much of the original code left any longer. Now handles just
|
|
|
|
* one-bit deep bitmaps (in Window parlance, ie those that GDK calls
|
|
|
|
* bitmaps (and not pixmaps), with zero pixels being transparent.
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* gdk_cairo_region_create_from_surface:
|
2011-02-09 07:43:07 +00:00
|
|
|
* @surface: a cairo surface
|
2010-08-14 02:40:52 +00:00
|
|
|
*
|
2011-02-09 07:43:07 +00:00
|
|
|
* Creates region that describes covers the area where the given
|
|
|
|
* @surface is more than 50% opaque.
|
2010-08-14 02:40:52 +00:00
|
|
|
*
|
2011-02-09 07:43:07 +00:00
|
|
|
* This function takes into account device offsets that might be
|
|
|
|
* set with cairo_surface_set_device_offset().
|
|
|
|
*
|
|
|
|
* Returns: A #cairo_region_t; must be freed with cairo_region_destroy()
|
2010-10-11 00:10:24 +00:00
|
|
|
*/
|
2010-08-14 02:40:52 +00:00
|
|
|
cairo_region_t *
|
|
|
|
gdk_cairo_region_create_from_surface (cairo_surface_t *surface)
|
|
|
|
{
|
|
|
|
cairo_region_t *region;
|
|
|
|
GdkRectangle extents, rect;
|
|
|
|
cairo_surface_t *image;
|
|
|
|
cairo_t *cr;
|
|
|
|
gint x, y, stride;
|
|
|
|
guchar *data;
|
|
|
|
|
|
|
|
_gdk_cairo_surface_extents (surface, &extents);
|
|
|
|
|
|
|
|
if (cairo_surface_get_content (surface) == CAIRO_CONTENT_COLOR)
|
|
|
|
return cairo_region_create_rectangle (&extents);
|
|
|
|
|
|
|
|
if (cairo_surface_get_type (surface) != CAIRO_SURFACE_TYPE_IMAGE ||
|
|
|
|
cairo_image_surface_get_format (surface) != CAIRO_FORMAT_A1)
|
|
|
|
{
|
|
|
|
/* coerce to an A1 image */
|
|
|
|
image = cairo_image_surface_create (CAIRO_FORMAT_A1,
|
|
|
|
extents.width, extents.height);
|
|
|
|
cr = cairo_create (image);
|
2010-09-30 14:24:11 +00:00
|
|
|
cairo_set_source_surface (cr, surface, -extents.x, -extents.y);
|
2010-08-14 02:40:52 +00:00
|
|
|
cairo_paint (cr);
|
|
|
|
cairo_destroy (cr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
image = cairo_surface_reference (surface);
|
|
|
|
|
2014-09-02 15:29:47 +00:00
|
|
|
/* Flush the surface to make sure that the rendering is up to date. */
|
|
|
|
cairo_surface_flush (image);
|
|
|
|
|
2010-08-14 02:40:52 +00:00
|
|
|
data = cairo_image_surface_get_data (image);
|
|
|
|
stride = cairo_image_surface_get_stride (image);
|
|
|
|
|
|
|
|
region = cairo_region_create ();
|
|
|
|
|
|
|
|
for (y = 0; y < extents.height; y++)
|
|
|
|
{
|
|
|
|
for (x = 0; x < extents.width; x++)
|
2011-02-09 07:43:07 +00:00
|
|
|
{
|
|
|
|
/* Search for a continuous range of "non transparent pixels"*/
|
|
|
|
gint x0 = x;
|
|
|
|
while (x < extents.width)
|
|
|
|
{
|
2011-11-19 17:23:27 +00:00
|
|
|
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
|
2011-02-09 07:43:07 +00:00
|
|
|
if (((data[x / 8] >> (x%8)) & 1) == 0)
|
2011-11-19 17:23:27 +00:00
|
|
|
#else
|
|
|
|
if (((data[x / 8] >> (7-(x%8))) & 1) == 0)
|
|
|
|
#endif
|
2011-02-09 07:43:07 +00:00
|
|
|
/* This pixel is "transparent"*/
|
|
|
|
break;
|
|
|
|
x++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (x > x0)
|
|
|
|
{
|
|
|
|
/* Add the pixels (x0, y) to (x, y+1) as a new rectangle
|
|
|
|
* in the region
|
|
|
|
*/
|
2010-08-14 02:40:52 +00:00
|
|
|
rect.x = x0;
|
|
|
|
rect.width = x - x0;
|
|
|
|
rect.y = y;
|
|
|
|
rect.height = 1;
|
|
|
|
|
|
|
|
cairo_region_union_rectangle (region, &rect);
|
2011-02-09 07:43:07 +00:00
|
|
|
}
|
|
|
|
}
|
2010-08-14 02:40:52 +00:00
|
|
|
data += stride;
|
|
|
|
}
|
|
|
|
|
|
|
|
cairo_surface_destroy (image);
|
2011-02-09 07:43:07 +00:00
|
|
|
|
2010-08-14 02:40:52 +00:00
|
|
|
cairo_region_translate (region, extents.x, extents.y);
|
|
|
|
|
|
|
|
return region;
|
|
|
|
}
|
2014-10-09 07:58:36 +00:00
|
|
|
|
|
|
|
cairo_region_t *
|
|
|
|
gdk_cairo_region_from_clip (cairo_t *cr)
|
|
|
|
{
|
|
|
|
cairo_rectangle_list_t *rectangles;
|
|
|
|
cairo_region_t *region;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
rectangles = cairo_copy_clip_rectangle_list (cr);
|
|
|
|
|
|
|
|
if (rectangles->status != CAIRO_STATUS_SUCCESS)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
region = cairo_region_create ();
|
|
|
|
for (i = 0; i < rectangles->num_rectangles; i++)
|
|
|
|
{
|
|
|
|
cairo_rectangle_int_t clip_rect;
|
|
|
|
cairo_rectangle_t *rect;
|
|
|
|
|
|
|
|
rect = &rectangles->rectangles[i];
|
|
|
|
|
|
|
|
/* Here we assume clip rects are ints for direct targets, which
|
|
|
|
is true for cairo */
|
|
|
|
clip_rect.x = (int)rect->x;
|
|
|
|
clip_rect.y = (int)rect->y;
|
|
|
|
clip_rect.width = (int)rect->width;
|
|
|
|
clip_rect.height = (int)rect->height;
|
|
|
|
|
|
|
|
cairo_region_union_rectangle (region, &clip_rect);
|
|
|
|
}
|
|
|
|
|
|
|
|
cairo_rectangle_list_destroy (rectangles);
|
|
|
|
|
|
|
|
return region;
|
|
|
|
}
|