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;

  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);

  /* 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);

  gdk_threads_leave();
}
示例#2
0
/* Draw the word cairo at NUM_TEXT different angles */
static void
draw (cairo_t *cr, int width, int height)
{
    cairo_surface_t *target, *stamp;

    target = cairo_current_target_surface (cr);
    cairo_surface_reference (target);

    stamp = cairo_surface_create_similar (target, CAIRO_FORMAT_ARGB32,
                                          WIDTH, HEIGHT);
    cairo_set_target_surface (cr, stamp);
    cairo_new_path (cr);
    cairo_rectangle (cr, WIDTH / 4, HEIGHT / 4, WIDTH / 2, HEIGHT / 2);
    cairo_set_rgb_color (cr, 1, 0, 0);
    cairo_set_alpha (cr, 0.8);
    cairo_fill (cr);

    cairo_rectangle (cr, 0, 0, WIDTH, HEIGHT);
    cairo_set_line_width (cr, 2);
    cairo_set_rgb_color (cr, 0, 0, 0);
    cairo_set_alpha (cr, 1);
    cairo_stroke (cr);

    cairo_set_target_surface (cr, target);

    /* Draw a translucent rectangle for reference where the rotated
     * image should be. */
    cairo_new_path (cr);
    cairo_rectangle (cr, WIDTH, HEIGHT, WIDTH, HEIGHT);
    cairo_set_rgb_color (cr, 1, 1, 0);
    cairo_set_alpha (cr, 0.3);
    cairo_fill (cr);

#if 1 /* Set to 0 to generate reference image */
    cairo_translate (cr, 2 * WIDTH, 2 * HEIGHT);
    cairo_rotate (cr, M_PI);
#else
    cairo_translate (cr, WIDTH, HEIGHT);
#endif

    cairo_set_alpha (cr, 1);
    cairo_show_surface (cr, stamp, WIDTH + 2, HEIGHT + 2);

    cairo_show_page (cr);

    cairo_surface_destroy (stamp);
    cairo_surface_destroy (target);
}
示例#3
0
bool cairoBlitButton(PG_Button* button, SDL_Surface* srf, const PG_Rect& src, const PG_Rect& dst, PG_Button::STATE state) {
	double linewidth = 4;
	cairo_save(cr);
	
	cairo_set_alpha(cr, 0.5);
	cairo_rectangle (cr, button->x+linewidth/2,button->y+linewidth/2,button->w-linewidth/2-1,button->h-linewidth/2-1);
	cairo_set_line_width(cr, 4);
	cairo_set_rgb_color(cr, 0, 0, 0);
	cairo_stroke(cr);
	cairo_restore(cr);
	
	return true;
}
示例#4
0
void CMyWidget::eventBlit(SDL_Surface* srf, const PG_Rect& src, const PG_Rect& dst) {
	//PG_ThemeWidget::eventBlit(srf, src, dst);
	//PG_Rect* clip = GetClipRect();
		
	cairo_save(cr);

	// set clipping rectangle
	/*cairo_rectangle(cr, clip->x, clip->y, clip->w, clip->h);
	cairo_clip(cr);
	cairo_new_path(cr);*/

	/*cairo_pattern_t* pat = cairo_pattern_create_linear (x, y,  w, h);
	cairo_pattern_add_color_stop (pat, 0, 1, 1, 1, 0);
	cairo_pattern_add_color_stop (pat, h, .1, .1, .1, .9);*/

	//cairo_rectangle (cr, x,y,w,h);
	//cairo_set_pattern (cr, pat);
	//cairo_fill (cr);
	//cairo_pattern_destroy(pat);

	// set caps
	cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
	cairo_set_line_join(cr, CAIRO_LINE_JOIN_ROUND);

	// create path
	cairo_set_alpha(cr, 1);
	cairo_move_to(cr, x+linewidth, y+linewidth);
	cairo_line_to(cr, x + w - linewidth, y+linewidth);
	cairo_curve_to(cr, x+w/2, y+h-linewidth, x+w/2, y+h-linewidth, x+linewidth, y+linewidth);
	cairo_close_path(cr);

	// stroke
	cairo_set_alpha(cr, 0.2);
	cairo_set_line_width(cr, linewidth*1.7);
	cairo_set_rgb_color(cr, 0, 0, 0);
	cairo_stroke(cr);
	
	cairo_restore(cr);
}
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);

}