Blame gdk/gdkcairo.c

Packit 98cdb6
/* GDK - The GIMP Drawing Kit
Packit 98cdb6
 * Copyright (C) 2005 Red Hat, Inc. 
Packit 98cdb6
 *
Packit 98cdb6
 * This library is free software; you can redistribute it and/or
Packit 98cdb6
 * modify it under the terms of the GNU Lesser General Public
Packit 98cdb6
 * License as published by the Free Software Foundation; either
Packit 98cdb6
 * version 2 of the License, or (at your option) any later version.
Packit 98cdb6
 *
Packit 98cdb6
 * This library is distributed in the hope that it will be useful,
Packit 98cdb6
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 98cdb6
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
Packit 98cdb6
 * Lesser General Public License for more details.
Packit 98cdb6
 *
Packit 98cdb6
 * You should have received a copy of the GNU Lesser General Public
Packit 98cdb6
 * License along with this library; if not, write to the
Packit 98cdb6
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Packit 98cdb6
 * Boston, MA 02111-1307, USA.
Packit 98cdb6
 */
Packit 98cdb6
Packit 98cdb6
#include "gdkcairo.h"
Packit 98cdb6
#include "gdkdrawable.h"
Packit 98cdb6
#include "gdkinternals.h"
Packit 98cdb6
#include "gdkregion-generic.h"
Packit 98cdb6
#include "gdkalias.h"
Packit 98cdb6
Packit 98cdb6
static void
Packit 98cdb6
gdk_ensure_surface_flush (gpointer surface)
Packit 98cdb6
{
Packit 98cdb6
  cairo_surface_flush (surface);
Packit 98cdb6
  cairo_surface_destroy (surface);
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
/**
Packit 98cdb6
 * gdk_cairo_create:
Packit 98cdb6
 * @drawable: a #GdkDrawable
Packit 98cdb6
 * 
Packit 98cdb6
 * Creates a Cairo context for drawing to @drawable.
Packit 98cdb6
 *
Packit 98cdb6
 * <note><para>
Packit 98cdb6
 * Note that due to double-buffering, Cairo contexts created 
Packit 98cdb6
 * in a GTK+ expose event handler cannot be cached and reused 
Packit 98cdb6
 * between different expose events. 
Packit 98cdb6
 * </para></note>
Packit 98cdb6
 *
Packit 98cdb6
 * Return value: A newly created Cairo context. Free with
Packit 98cdb6
 *  cairo_destroy() when you are done drawing.
Packit 98cdb6
 * 
Packit 98cdb6
 * Since: 2.8
Packit 98cdb6
 **/
Packit 98cdb6
cairo_t *
Packit 98cdb6
gdk_cairo_create (GdkDrawable *drawable)
Packit 98cdb6
{
Packit 98cdb6
  static const cairo_user_data_key_t key;
Packit 98cdb6
  cairo_surface_t *surface;
Packit 98cdb6
  cairo_t *cr;
Packit 98cdb6
    
Packit 98cdb6
  g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);
Packit 98cdb6
Packit 98cdb6
  surface = _gdk_drawable_ref_cairo_surface (drawable);
Packit 98cdb6
  cr = cairo_create (surface);
Packit 98cdb6
Packit 98cdb6
  if (GDK_DRAWABLE_GET_CLASS (drawable)->set_cairo_clip)
Packit 98cdb6
    GDK_DRAWABLE_GET_CLASS (drawable)->set_cairo_clip (drawable, cr);
Packit 98cdb6
    
Packit 98cdb6
  /* Ugly workaround for GTK not ensuring to flush surfaces before
Packit 98cdb6
   * directly accessing the drawable backed by the surface. Not visible
Packit 98cdb6
   * on X11 (where flushing is a no-op). For details, see
Packit 98cdb6
   * https://bugzilla.gnome.org/show_bug.cgi?id=628291
Packit 98cdb6
   */
Packit 98cdb6
  cairo_set_user_data (cr, &key, surface, gdk_ensure_surface_flush);
Packit 98cdb6
Packit 98cdb6
  return cr;
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
/**
Packit 98cdb6
 * gdk_cairo_reset_clip:
Packit 98cdb6
 * @cr: a #cairo_t
Packit 98cdb6
 * @drawable: a #GdkDrawable
Packit 98cdb6
 *
Packit 98cdb6
 * Resets the clip region for a Cairo context created by gdk_cairo_create().
Packit 98cdb6
 *
Packit 98cdb6
 * This resets the clip region to the "empty" state for the given drawable.
Packit 98cdb6
 * This is required for non-native windows since a direct call to
Packit 98cdb6
 * cairo_reset_clip() would unset the clip region inherited from the
Packit 98cdb6
 * drawable (i.e. the window clip region), and thus let you e.g.
Packit 98cdb6
 * draw outside your window.
Packit 98cdb6
 *
Packit 98cdb6
 * This is rarely needed though, since most code just create a new cairo_t
Packit 98cdb6
 * using gdk_cairo_create() each time they want to draw something.
Packit 98cdb6
 *
Packit 98cdb6
 * Since: 2.18
Packit 98cdb6
 **/
Packit 98cdb6
void
Packit 98cdb6
gdk_cairo_reset_clip (cairo_t            *cr,
Packit 98cdb6
		      GdkDrawable        *drawable)
Packit 98cdb6
{
Packit 98cdb6
  cairo_reset_clip (cr);
Packit 98cdb6
Packit 98cdb6
  if (GDK_DRAWABLE_GET_CLASS (drawable)->set_cairo_clip)
Packit 98cdb6
    GDK_DRAWABLE_GET_CLASS (drawable)->set_cairo_clip (drawable, cr);
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
/**
Packit 98cdb6
 * gdk_cairo_set_source_color:
Packit 98cdb6
 * @cr: a #cairo_t
Packit 98cdb6
 * @color: a #GdkColor
Packit 98cdb6
 * 
Packit 98cdb6
 * Sets the specified #GdkColor as the source color of @cr.
Packit 98cdb6
 *
Packit 98cdb6
 * Since: 2.8
Packit 98cdb6
 **/
Packit 98cdb6
void
Packit 98cdb6
gdk_cairo_set_source_color (cairo_t        *cr,
Packit 98cdb6
			    const GdkColor *color)
Packit 98cdb6
{
Packit 98cdb6
  g_return_if_fail (cr != NULL);
Packit 98cdb6
  g_return_if_fail (color != NULL);
Packit 98cdb6
    
Packit 98cdb6
  cairo_set_source_rgb (cr,
Packit 98cdb6
			color->red / 65535.,
Packit 98cdb6
			color->green / 65535.,
Packit 98cdb6
			color->blue / 65535.);
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
/**
Packit 98cdb6
 * gdk_cairo_rectangle:
Packit 98cdb6
 * @cr: a #cairo_t
Packit 98cdb6
 * @rectangle: a #GdkRectangle
Packit 98cdb6
 * 
Packit 98cdb6
 * Adds the given rectangle to the current path of @cr.
Packit 98cdb6
 *
Packit 98cdb6
 * Since: 2.8
Packit 98cdb6
 **/
Packit 98cdb6
void
Packit 98cdb6
gdk_cairo_rectangle (cairo_t            *cr,
Packit 98cdb6
		     const GdkRectangle *rectangle)
Packit 98cdb6
{
Packit 98cdb6
  g_return_if_fail (cr != NULL);
Packit 98cdb6
  g_return_if_fail (rectangle != NULL);
Packit 98cdb6
Packit 98cdb6
  cairo_rectangle (cr,
Packit 98cdb6
		   rectangle->x,     rectangle->y,
Packit 98cdb6
		   rectangle->width, rectangle->height);
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
/**
Packit 98cdb6
 * gdk_cairo_region:
Packit 98cdb6
 * @cr: a #cairo_t
Packit 98cdb6
 * @region: a #GdkRegion
Packit 98cdb6
 * 
Packit 98cdb6
 * Adds the given region to the current path of @cr.
Packit 98cdb6
 *
Packit 98cdb6
 * Since: 2.8
Packit 98cdb6
 **/
Packit 98cdb6
void
Packit 98cdb6
gdk_cairo_region (cairo_t         *cr,
Packit 98cdb6
		  const GdkRegion *region)
Packit 98cdb6
{
Packit 98cdb6
  GdkRegionBox *boxes;
Packit 98cdb6
  gint n_boxes, i;
Packit 98cdb6
Packit 98cdb6
  g_return_if_fail (cr != NULL);
Packit 98cdb6
  g_return_if_fail (region != NULL);
Packit 98cdb6
Packit 98cdb6
  boxes = region->rects;
Packit 98cdb6
  n_boxes = region->numRects;
Packit 98cdb6
Packit 98cdb6
  for (i = 0; i < n_boxes; i++)
Packit 98cdb6
    cairo_rectangle (cr,
Packit 98cdb6
		     boxes[i].x1,
Packit 98cdb6
		     boxes[i].y1,
Packit 98cdb6
		     boxes[i].x2 - boxes[i].x1,
Packit 98cdb6
		     boxes[i].y2 - boxes[i].y1);
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
/**
Packit 98cdb6
 * gdk_cairo_set_source_pixbuf:
Packit 98cdb6
 * @cr: a #Cairo context
Packit 98cdb6
 * @pixbuf: a #GdkPixbuf
Packit 98cdb6
 * @pixbuf_x: X coordinate of location to place upper left corner of @pixbuf
Packit 98cdb6
 * @pixbuf_y: Y coordinate of location to place upper left corner of @pixbuf
Packit 98cdb6
 * 
Packit 98cdb6
 * Sets the given pixbuf as the source pattern for the Cairo context.
Packit 98cdb6
 * The pattern has an extend mode of %CAIRO_EXTEND_NONE and is aligned
Packit 98cdb6
 * so that the origin of @pixbuf is @pixbuf_x, @pixbuf_y
Packit 98cdb6
 *
Packit 98cdb6
 * Since: 2.8
Packit 98cdb6
 **/
Packit 98cdb6
void
Packit 98cdb6
gdk_cairo_set_source_pixbuf (cairo_t         *cr,
Packit 98cdb6
			     const GdkPixbuf *pixbuf,
Packit 98cdb6
			     double           pixbuf_x,
Packit 98cdb6
			     double           pixbuf_y)
Packit 98cdb6
{
Packit 98cdb6
  gint width = gdk_pixbuf_get_width (pixbuf);
Packit 98cdb6
  gint height = gdk_pixbuf_get_height (pixbuf);
Packit 98cdb6
  guchar *gdk_pixels = gdk_pixbuf_get_pixels (pixbuf);
Packit 98cdb6
  int gdk_rowstride = gdk_pixbuf_get_rowstride (pixbuf);
Packit 98cdb6
  int n_channels = gdk_pixbuf_get_n_channels (pixbuf);
Packit 98cdb6
  int cairo_stride;
Packit 98cdb6
  guchar *cairo_pixels;
Packit 98cdb6
  cairo_format_t format;
Packit 98cdb6
  cairo_surface_t *surface;
Packit 98cdb6
  static const cairo_user_data_key_t key;
Packit 98cdb6
  cairo_status_t status;
Packit 98cdb6
  int j;
Packit 98cdb6
Packit 98cdb6
  if (n_channels == 3)
Packit 98cdb6
    format = CAIRO_FORMAT_RGB24;
Packit 98cdb6
  else
Packit 98cdb6
    format = CAIRO_FORMAT_ARGB32;
Packit 98cdb6
Packit 98cdb6
  cairo_stride = cairo_format_stride_for_width (format, width);
Packit 98cdb6
  cairo_pixels = g_malloc_n (height, cairo_stride);
Packit 98cdb6
  surface = cairo_image_surface_create_for_data ((unsigned char *)cairo_pixels,
Packit 98cdb6
                                                 format,
Packit 98cdb6
                                                 width, height, cairo_stride);
Packit 98cdb6
Packit 98cdb6
  status = cairo_surface_set_user_data (surface, &key,
Packit 98cdb6
                                        cairo_pixels, (cairo_destroy_func_t)g_free);
Packit 98cdb6
  if (status != CAIRO_STATUS_SUCCESS)
Packit 98cdb6
    {
Packit 98cdb6
      g_free (cairo_pixels);
Packit 98cdb6
      goto out;
Packit 98cdb6
    }
Packit 98cdb6
Packit 98cdb6
  for (j = height; j; j--)
Packit 98cdb6
    {
Packit 98cdb6
      guchar *p = gdk_pixels;
Packit 98cdb6
      guchar *q = cairo_pixels;
Packit 98cdb6
Packit 98cdb6
      if (n_channels == 3)
Packit 98cdb6
	{
Packit 98cdb6
	  guchar *end = p + 3 * width;
Packit 98cdb6
	  
Packit 98cdb6
	  while (p < end)
Packit 98cdb6
	    {
Packit 98cdb6
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
Packit 98cdb6
	      q[0] = p[2];
Packit 98cdb6
	      q[1] = p[1];
Packit 98cdb6
	      q[2] = p[0];
Packit 98cdb6
#else	  
Packit 98cdb6
	      q[1] = p[0];
Packit 98cdb6
	      q[2] = p[1];
Packit 98cdb6
	      q[3] = p[2];
Packit 98cdb6
#endif
Packit 98cdb6
	      p += 3;
Packit 98cdb6
	      q += 4;
Packit 98cdb6
	    }
Packit 98cdb6
	}
Packit 98cdb6
      else
Packit 98cdb6
	{
Packit 98cdb6
	  guchar *end = p + 4 * width;
Packit 98cdb6
	  guint t1,t2,t3;
Packit 98cdb6
	    
Packit 98cdb6
#define MULT(d,c,a,t) G_STMT_START { t = c * a + 0x80; d = ((t >> 8) + t) >> 8; } G_STMT_END
Packit 98cdb6
Packit 98cdb6
	  while (p < end)
Packit 98cdb6
	    {
Packit 98cdb6
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
Packit 98cdb6
	      MULT(q[0], p[2], p[3], t1);
Packit 98cdb6
	      MULT(q[1], p[1], p[3], t2);
Packit 98cdb6
	      MULT(q[2], p[0], p[3], t3);
Packit 98cdb6
	      q[3] = p[3];
Packit 98cdb6
#else	  
Packit 98cdb6
	      q[0] = p[3];
Packit 98cdb6
	      MULT(q[1], p[0], p[3], t1);
Packit 98cdb6
	      MULT(q[2], p[1], p[3], t2);
Packit 98cdb6
	      MULT(q[3], p[2], p[3], t3);
Packit 98cdb6
#endif
Packit 98cdb6
	      
Packit 98cdb6
	      p += 4;
Packit 98cdb6
	      q += 4;
Packit 98cdb6
	    }
Packit 98cdb6
	  
Packit 98cdb6
#undef MULT
Packit 98cdb6
	}
Packit 98cdb6
Packit 98cdb6
      gdk_pixels += gdk_rowstride;
Packit 98cdb6
      cairo_pixels += cairo_stride;
Packit 98cdb6
    }
Packit 98cdb6
Packit 98cdb6
out:
Packit 98cdb6
  cairo_set_source_surface (cr, surface, pixbuf_x, pixbuf_y);
Packit 98cdb6
  cairo_surface_destroy (surface);
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
/**
Packit 98cdb6
 * gdk_cairo_set_source_pixmap:
Packit 98cdb6
 * @cr: a #Cairo context
Packit 98cdb6
 * @pixmap: a #GdkPixmap
Packit 98cdb6
 * @pixmap_x: X coordinate of location to place upper left corner of @pixmap
Packit 98cdb6
 * @pixmap_y: Y coordinate of location to place upper left corner of @pixmap
Packit 98cdb6
 * 
Packit 98cdb6
 * Sets the given pixmap as the source pattern for the Cairo context.
Packit 98cdb6
 * The pattern has an extend mode of %CAIRO_EXTEND_NONE and is aligned
Packit 98cdb6
 * so that the origin of @pixmap is @pixmap_x, @pixmap_y
Packit 98cdb6
 *
Packit 98cdb6
 * Since: 2.10
Packit 98cdb6
 *
Packit 98cdb6
 * Deprecated: 2.24: This function is being removed in GTK+ 3 (together
Packit 98cdb6
 *     with #GdkPixmap). Instead, use gdk_cairo_set_source_window() where
Packit 98cdb6
 *     appropriate.
Packit 98cdb6
 **/
Packit 98cdb6
void
Packit 98cdb6
gdk_cairo_set_source_pixmap (cairo_t   *cr,
Packit 98cdb6
			     GdkPixmap *pixmap,
Packit 98cdb6
			     double     pixmap_x,
Packit 98cdb6
			     double     pixmap_y)
Packit 98cdb6
{
Packit 98cdb6
  cairo_surface_t *surface;
Packit 98cdb6
  
Packit 98cdb6
  surface = _gdk_drawable_ref_cairo_surface (GDK_DRAWABLE (pixmap));
Packit 98cdb6
  cairo_set_source_surface (cr, surface, pixmap_x, pixmap_y);
Packit 98cdb6
  cairo_surface_destroy (surface);
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
/**
Packit 98cdb6
 * gdk_cairo_set_source_window:
Packit 98cdb6
 * @cr: a #Cairo context
Packit 98cdb6
 * @window: a #GdkWindow
Packit 98cdb6
 * @x: X coordinate of location to place upper left corner of @window
Packit 98cdb6
 * @y: Y coordinate of location to place upper left corner of @window
Packit 98cdb6
 *
Packit 98cdb6
 * Sets the given window as the source pattern for the Cairo context.
Packit 98cdb6
 * The pattern has an extend mode of %CAIRO_EXTEND_NONE and is aligned
Packit 98cdb6
 * so that the origin of @window is @x, @y. The window contains all its
Packit 98cdb6
 * subwindows when rendering.
Packit 98cdb6
 *
Packit 98cdb6
 * Note that the contents of @window are undefined outside of the
Packit 98cdb6
 * visible part of @window, so use this function with care.
Packit 98cdb6
 *
Packit 98cdb6
 * Since: 2.24
Packit 98cdb6
 **/
Packit 98cdb6
void
Packit 98cdb6
gdk_cairo_set_source_window (cairo_t   *cr,
Packit 98cdb6
                             GdkWindow *window,
Packit 98cdb6
                             double     x,
Packit 98cdb6
                             double     y)
Packit 98cdb6
{
Packit 98cdb6
  cairo_surface_t *surface;
Packit 98cdb6
Packit 98cdb6
  g_return_if_fail (cr != NULL);
Packit 98cdb6
  g_return_if_fail (GDK_IS_WINDOW (window));
Packit 98cdb6
Packit 98cdb6
  surface = _gdk_drawable_ref_cairo_surface (GDK_DRAWABLE (window));
Packit 98cdb6
  cairo_set_source_surface (cr, surface, x, y);
Packit 98cdb6
  cairo_surface_destroy (surface);
Packit 98cdb6
}
Packit 98cdb6
Packit 98cdb6
Packit 98cdb6
#define __GDK_CAIRO_C__
Packit 98cdb6
#include "gdkaliasdef.c"