JNIEXPORT void JNICALL Java_gnu_java_awt_peer_gtk_GdkGraphics2D_cairoClip 
   (JNIEnv *env, jobject obj)
{
  struct graphics2d *gr = NULL;

  gdk_threads_enter();
  if (peer_is_disposed(env, obj)) { gdk_threads_leave(); return; }

  gr = (struct graphics2d *) NSA_GET_G2D_PTR (env, obj);
  g_assert (gr != NULL);
  if (gr->debug) printf ("cairo_clip\n");
  begin_drawing_operation (gr);
  cairo_init_clip (gr->cr);
  cairo_clip (gr->cr);
  end_drawing_operation (gr);
  gdk_threads_leave();
}
JNIEXPORT void JNICALL Java_gnu_java_awt_peer_gtk_GdkGraphics2D_cairoShowGlyphs
   (JNIEnv *env, jobject obj, jintArray java_codes, jfloatArray java_posns)
{
  struct graphics2d *gr = NULL;
  cairo_glyph_t *glyphs = NULL;
  jfloat *native_posns = NULL;
  jint *native_codes = NULL;
  jint i;
  jint ncodes, nposns;

  gr = (struct graphics2d *) NSA_GET_G2D_PTR (env, obj);
  g_assert (gr != NULL);

  native_codes = (*env)->GetIntArrayElements (env, java_codes, NULL);  
  native_posns = (*env)->GetFloatArrayElements (env, java_posns, NULL);  
  g_assert (native_codes != NULL);
  g_assert (native_posns != NULL);

  ncodes = (*env)->GetArrayLength (env, java_codes);
  nposns = (*env)->GetArrayLength (env, java_posns);
  g_assert (2 * ncodes == nposns);

  if (gr->debug) printf ("cairo_show_glyphs (%d glyphs)\n", ncodes);

  glyphs = malloc (sizeof(cairo_glyph_t) * ncodes);
  g_assert (glyphs);

  for (i = 0; i < ncodes; ++i)
    {
      glyphs[i].index = native_codes[i];
      glyphs[i].x = (double) native_posns[2*i];
      glyphs[i].y = (double) native_posns[2*i + 1];
      if (gr->debug) printf ("cairo_show_glyphs (glyph %d (code %d) : %f,%f)\n", 
			     i, glyphs[i].index, glyphs[i].x, glyphs[i].y);
    }

  (*env)->ReleaseIntArrayElements (env, java_codes, native_codes, 0);
  (*env)->ReleaseFloatArrayElements (env, java_posns, native_posns, 0);

  begin_drawing_operation (gr);
  cairo_show_glyphs (gr->cr, glyphs, ncodes);
  end_drawing_operation (gr);

  free(glyphs);
}
JNIEXPORT void JNICALL Java_gnu_java_awt_peer_gtk_GdkGraphics2D_setTexturePixels 
  (JNIEnv *env, jobject obj, jintArray jarr, jint w, jint h, jint stride)
{
  struct graphics2d *gr = NULL;
  jint *jpixels = NULL;

  gdk_threads_enter();
  if (peer_is_disposed(env, obj)) { gdk_threads_leave(); return; }
  gr = (struct graphics2d *) NSA_GET_G2D_PTR (env, obj);
  g_assert (gr != NULL);

  if (gr->debug) printf ("setTexturePixels (%d pixels, %dx%d, stride: %d)\n",
			 (*env)->GetArrayLength (env, jarr), w, h, stride);

  if (gr->pattern)
    cairo_pattern_destroy (gr->pattern);

  if (gr->pattern_surface)
    cairo_surface_destroy (gr->pattern_surface);

  if (gr->pattern_pixels)
    free (gr->pattern_pixels);

  gr->pattern = NULL;
  gr->pattern_surface = NULL;
  gr->pattern_pixels = NULL;

  gr->pattern_pixels = (char *) malloc (h * stride * 4);
  g_assert (gr->pattern_pixels != NULL);

  jpixels = (*env)->GetIntArrayElements (env, jarr, NULL);
  g_assert (jpixels != NULL);
  memcpy (gr->pattern_pixels, jpixels, h * stride * 4);
  (*env)->ReleaseIntArrayElements (env, jarr, jpixels, 0);

  gr->pattern_surface = cairo_surface_create_for_image (gr->pattern_pixels, 
							CAIRO_FORMAT_ARGB32, 
							w, h, stride * 4);
  g_assert (gr->pattern_surface != NULL);
  cairo_surface_set_repeat (gr->pattern_surface, 1);
  gr->pattern = cairo_pattern_create_for_surface (gr->pattern_surface);
  g_assert (gr->pattern != NULL);
  cairo_set_pattern (gr->cr, gr->pattern);
  gdk_threads_leave();
}
JNIEXPORT void JNICALL Java_gnu_java_awt_peer_gtk_GdkGraphics2D_cairoSetDash 
   (JNIEnv *env, jobject obj, jdoubleArray dashes, jint ndash, jdouble offset)
{
  struct graphics2d *gr = NULL;
  jdouble *dasharr = NULL;

  gdk_threads_enter();
  if (peer_is_disposed(env, obj)) { gdk_threads_leave(); return; }

  gr = (struct graphics2d *) NSA_GET_G2D_PTR (env, obj);
  g_assert (gr != NULL);
  if (gr->debug) printf ("cairo_set_dash\n");
  dasharr = (*env)->GetDoubleArrayElements (env, dashes, NULL);  
  g_assert (dasharr != NULL);
  cairo_set_dash (gr->cr, dasharr, ndash, offset);
  (*env)->ReleaseDoubleArrayElements (env, dashes, dasharr, 0);
  gdk_threads_leave();
}
JNIEXPORT void JNICALL Java_gnu_java_awt_peer_gtk_GdkGraphics2D_cairoSetRGBColor 
   (JNIEnv *env, jobject obj, jdouble r, jdouble g, jdouble b)
{
  struct graphics2d *gr = NULL;
  gr = (struct graphics2d *) NSA_GET_G2D_PTR (env, obj);
  g_assert (gr != NULL);

  /* this is a very weird fact: GDK Pixbufs and RENDER drawables consider
     colors in opposite pixel order. I have no idea why.  thus when you
     draw to a PixBuf, you must exchange the R and B components of your
     color. */

  if (gr->debug) printf ("cairo_set_rgb_color (%f, %f, %f)\n", r, g, b);

  if (gr->drawbuf)
    cairo_set_rgb_color (gr->cr, b, g, r);
  else
    cairo_set_rgb_color (gr->cr, r, g, b);
}
JNIEXPORT void JNICALL Java_gnu_java_awt_peer_gtk_GdkGraphics2D_cairoSetLineJoin 
   (JNIEnv *env, jobject obj, jint join)
{
  struct graphics2d *gr = NULL;
  gr = (struct graphics2d *) NSA_GET_G2D_PTR (env, obj);
  g_assert (gr != NULL);
  if (gr->debug) printf ("cairo_set_line_join %d\n", join);
  switch ((enum java_awt_basic_stroke_join_rule) join)
    {
    case java_awt_basic_stroke_JOIN_MITER:
      cairo_set_line_join (gr->cr, CAIRO_LINE_JOIN_MITER);
      break;

    case java_awt_basic_stroke_JOIN_ROUND:
      cairo_set_line_join (gr->cr, CAIRO_LINE_JOIN_ROUND);
      break;

    case java_awt_basic_stroke_JOIN_BEVEL:
      cairo_set_line_join (gr->cr, CAIRO_LINE_JOIN_BEVEL);
      break;
    }
}
JNIEXPORT void JNICALL Java_gnu_java_awt_peer_gtk_GdkGraphics2D_cairoSetLineCap 
   (JNIEnv *env, jobject obj, jint cap)
{
  struct graphics2d *gr = NULL;
  gr = (struct graphics2d *) NSA_GET_G2D_PTR (env, obj);
  g_assert (gr != NULL);
  if (gr->debug) printf ("cairo_set_line_cap %d\n", cap);
  switch ((enum java_awt_basic_stroke_cap_rule) cap)
    {
    case java_awt_basic_stroke_CAP_BUTT: 
      cairo_set_line_cap (gr->cr, CAIRO_LINE_CAP_BUTT);
      break;

    case java_awt_basic_stroke_CAP_ROUND: 
      cairo_set_line_cap (gr->cr, CAIRO_LINE_CAP_ROUND);
      break;

    case java_awt_basic_stroke_CAP_SQUARE: 
      cairo_set_line_cap (gr->cr, CAIRO_LINE_CAP_SQUARE);
      break;
    }
}
JNIEXPORT void JNICALL Java_gnu_java_awt_peer_gtk_GdkGraphics2D_cairoSetFillRule 
   (JNIEnv *env, jobject obj, jint rule)
{
  struct graphics2d *gr = NULL;

  gdk_threads_enter();
  if (peer_is_disposed(env, obj)) { gdk_threads_leave(); return; }

  gr = (struct graphics2d *) NSA_GET_G2D_PTR (env, obj);
  if (gr->debug) printf ("cairo_set_fill_rule %d\n", rule);
  g_assert (gr != NULL);
  switch ((enum java_awt_geom_path_iterator_winding_rule) rule)
    {
    case java_awt_geom_path_iterator_WIND_NON_ZERO:
      cairo_set_fill_rule (gr->cr, CAIRO_FILL_RULE_WINDING);
      break;
    case java_awt_geom_path_iterator_WIND_EVEN_ODD:
      cairo_set_fill_rule (gr->cr, CAIRO_FILL_RULE_EVEN_ODD);
      break;
    }  
  gdk_threads_leave();
}
JNIEXPORT void JNICALL Java_gnu_java_awt_peer_gtk_GdkGraphics2D_setGradient 
  (JNIEnv *env, jobject obj, 
   jdouble x1, jdouble y1, 
   jdouble x2, jdouble y2,
   jint r1, jint g1, jint b1, jint a1,
   jint r2, jint g2, jint b2, jint a2,
   jboolean cyclic)
{
  struct graphics2d *gr = NULL;
  cairo_surface_t *surf = NULL;
  cairo_matrix_t *mat = NULL;
  gr = (struct graphics2d *) NSA_GET_G2D_PTR (env, obj);
  g_assert (gr != NULL);

  if (gr->debug) printf ("setGradient (%f,%f) -> (%f,%f); (%d,%d,%d,%d) -> (%d,%d,%d,%d)\n",
			 x1, y1, 
			 x2, y2, 
			 r1, g1, b1, a1, 
			 r2, g2, b2, a2);
  
  cairo_save (gr->cr);
  
  if (cyclic)
    surf = cairo_surface_create_similar (gr->surface, CAIRO_FORMAT_ARGB32, 3, 2);
  else
    surf = cairo_surface_create_similar (gr->surface, CAIRO_FORMAT_ARGB32, 2, 2);      
  g_assert (surf != NULL);

  cairo_set_target_surface (gr->cr, surf);
  
  cairo_identity_matrix (gr->cr);

  cairo_set_rgb_color (gr->cr, r1 / 255.0, g1 / 255.0, b1 / 255.0);
  cairo_set_alpha (gr->cr, a1 / 255.0);
  cairo_rectangle (gr->cr, 0, 0, 1, 2);
  cairo_fill (gr->cr);
    
  cairo_set_rgb_color (gr->cr, r2 / 255.0, g2 / 255.0, b2 / 255.0);
  cairo_set_alpha (gr->cr, a2 / 255.0);
  cairo_rectangle (gr->cr, 1, 0, 1, 2);
  cairo_fill (gr->cr);

  if (cyclic)
    {
      cairo_set_rgb_color (gr->cr, r1 / 255.0, g1 / 255.0, b1 / 255.0);
      cairo_set_alpha (gr->cr, a1 / 255.0);
      cairo_rectangle (gr->cr, 2, 0, 1, 2);
      cairo_fill (gr->cr);
    }

  mat = cairo_matrix_create ();
  g_assert (mat != NULL);

  /* 
     consider the vector [x2 - x1, y2 - y1] = [p,q]

     this is a line in space starting at an 'origin' x1, y1.

     it can also be thought of as a "transformed" unit vector in either the
     x or y directions. we have just *drawn* our gradient as a unit vector
     (well, a 2-3x unit vector) in the x dimension. so what we want to know
     is which transformation turns our existing unit vector into [p,q].

     which means solving for M in 
 
     [p,q] = M[1,0]

     [p,q] = |a b| [1,0]
             |c d|      

     [p,q] = [a,c], with b = d = 0.

     what does this mean? it means that our gradient is 1-dimensional; as
     you move through the x axis of our 2 or 3 pixel gradient from logical
     x positions 0 to 1, the transformation of your x coordinate under the
     matrix M causes you to accumulate both x and y values in fill
     space. the y value of a gradient coordinate is ignored, since the
     gradient is one dimensional. which is correct.

     unfortunately we want the opposite transformation, it seems, because of
     the way cairo is going to use this transformation. I'm a bit confused by
     that, but it seems to work right, so we take reciprocals of values and
     negate offsets. oh well.
     
   */

  double a = (x2 - x1 == 0.) ? 0. : ((cyclic ? 3.0 : 2.0) / (x2 - x1));
  double c = (y2 - y1 == 0.) ? 0. : (1. / (y2 - y1));
  double dx = (x1 == 0.) ? 0. : 1. / x1;
  double dy = (y1 == 0.) ? 0. : 1. / y1;

  cairo_matrix_set_affine (mat,
			   a, 0.,
			   c, 0.,
			   dx, dy);

  cairo_surface_set_matrix (surf, mat);
  cairo_matrix_destroy (mat);
  cairo_surface_set_filter (surf, CAIRO_FILTER_BILINEAR);

  /* FIXME: repeating gradients (not to mention hold gradients) don't seem to work. */
  /*   cairo_surface_set_repeat (surf, cyclic ? 1 : 0); */

  if (gr->pattern)
    cairo_surface_destroy (gr->pattern);

  if (gr->pattern_pixels)
    {
      free (gr->pattern_pixels);
      gr->pattern_pixels = NULL;
    }

  gr->pattern = surf;  

  cairo_restore (gr->cr);    
  cairo_set_pattern (gr->cr, gr->pattern);

}
JNIEXPORT void JNICALL Java_gnu_java_awt_peer_gtk_GdkGraphics2D_cairoSetOperator 
   (JNIEnv *env, jobject obj, jint op)
{
  struct graphics2d *gr = NULL;

  gdk_threads_enter();
  if (peer_is_disposed(env, obj)) { gdk_threads_leave(); return; }

  gr = (struct graphics2d *) NSA_GET_G2D_PTR (env, obj);
  g_assert (gr != NULL);
  if (gr->debug) printf ("cairo_set_operator %d\n", op);
  switch ((enum java_awt_alpha_composite_rule) op)
    {
    case java_awt_alpha_composite_CLEAR: 
      cairo_set_operator (gr->cr, CAIRO_OPERATOR_CLEAR);
      break;
      
    case java_awt_alpha_composite_SRC: 
      cairo_set_operator (gr->cr, CAIRO_OPERATOR_SRC);
      break;
      
    case java_awt_alpha_composite_SRC_OVER: 
      cairo_set_operator (gr->cr, CAIRO_OPERATOR_OVER);
      break;

    case java_awt_alpha_composite_DST_OVER: 
      cairo_set_operator (gr->cr, CAIRO_OPERATOR_OVER_REVERSE);
      break;

    case java_awt_alpha_composite_SRC_IN: 
      cairo_set_operator (gr->cr, CAIRO_OPERATOR_IN);
      break;

    case java_awt_alpha_composite_DST_IN: 
      cairo_set_operator (gr->cr, CAIRO_OPERATOR_IN_REVERSE);
      break;

    case java_awt_alpha_composite_SRC_OUT: 
      cairo_set_operator (gr->cr, CAIRO_OPERATOR_OUT);
      break;

    case java_awt_alpha_composite_DST_OUT: 
      cairo_set_operator (gr->cr, CAIRO_OPERATOR_OUT_REVERSE);
      break;

    case java_awt_alpha_composite_DST: 
      cairo_set_operator (gr->cr, CAIRO_OPERATOR_DST);
      break;

    case java_awt_alpha_composite_SRC_ATOP: 
      cairo_set_operator (gr->cr, CAIRO_OPERATOR_ATOP);
      break;

    case java_awt_alpha_composite_DST_ATOP: 
      cairo_set_operator (gr->cr, CAIRO_OPERATOR_ATOP_REVERSE);
      break;

    case java_awt_alpha_composite_XOR: 
      cairo_set_operator (gr->cr, CAIRO_OPERATOR_XOR);
      break;
    }
  gdk_threads_leave();
}