[cairo-commit] libsvg-cairo/src svg_cairo.c,1.24,1.25

Carl Worth commit at pdx.freedesktop.org
Wed Apr 28 21:16:46 PDT 2004


Committed by: cworth

Update of /cvs/cairo/libsvg-cairo/src
In directory pdx:/tmp/cvs-serv12804/src

Modified Files:
	svg_cairo.c 
Log Message:

        * src/svg_cairo.c (_svg_cairo_set_gradient):
        (_svg_cairo_set_pattern): Update to use new cairo_pattern API.
        (_svg_cairo_set_paint_and_opacity): Make alpha apply to all paint
        types, (not just solid colors). Thanks to David Reveman.


Index: svg_cairo.c
===================================================================
RCS file: /cvs/cairo/libsvg-cairo/src/svg_cairo.c,v
retrieving revision 1.24
retrieving revision 1.25
diff -C2 -d -r1.24 -r1.25
*** a/svg_cairo.c	16 Dec 2003 15:24:11 -0000	1.24
--- b/svg_cairo.c	29 Apr 2004 04:16:44 -0000	1.25
***************
*** 539,615 ****
  _svg_cairo_set_gradient (svg_cairo_t *svg_cairo, svg_gradient_t *gradient)
  {
      switch (gradient->type) {
!       case SVG_GRADIENT_LINEAR:
!       {
! 	  int i;
! 	  double x1, y1, x2, y2;
! 	  double scale;
! 	  cairo_surface_t *pattern;
! 	  cairo_matrix_t *matrix;
! 	  svg_gradient_stop_t *stop;
! 
! 	  cairo_save (svg_cairo->cr);
! 	  cairo_identity_matrix (svg_cairo->cr);
! 	  cairo_new_path (svg_cairo->cr);
! 
! 	  pattern = cairo_surface_create_similar (cairo_current_target_surface (svg_cairo->cr),
! 						  CAIRO_FORMAT_ARGB32,
! 						  gradient->num_stops,
! 						  1);
! 	  cairo_set_target_surface (svg_cairo->cr, pattern);
! 	  
! 	  /* XXX: This is a major hack right now... I'm ignoring stop->offset. */
! 	  for (i=0; i < gradient->num_stops; i++) {
! 	      stop = &gradient->stops[i];
! 	      cairo_rectangle (svg_cairo->cr, i, 0, 1, 1);
! 	      cairo_set_rgb_color (svg_cairo->cr,
! 				   svg_color_get_red (&stop->color) / 255.0,
! 				   svg_color_get_green (&stop->color) / 255.0,
! 				   svg_color_get_blue (&stop->color) / 255.0);
! 	      cairo_set_alpha (svg_cairo->cr, stop->opacity);
! 	      cairo_fill (svg_cairo->cr);
! 	  }
! 	  
! 	  cairo_restore (svg_cairo->cr);
! 
! 	  /* XXX: Not yet handling gradients with percentage units in
!              terms of the current object (gradientUnits="objectBoundingBox").  
! 
! 	     To support those, we will need to be able to compute the
! 	     bounds of the current object.
! 	  */
! 	  _svg_cairo_length_to_pixel (svg_cairo, &gradient->u.linear.x1, &x1);
! 	  _svg_cairo_length_to_pixel (svg_cairo, &gradient->u.linear.y1, &y1);
! 	  _svg_cairo_length_to_pixel (svg_cairo, &gradient->u.linear.x2, &x2);
! 	  _svg_cairo_length_to_pixel (svg_cairo, &gradient->u.linear.y2, &y2);
! 
! 	  scale = sqrt ((x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1));
! 	  matrix = cairo_matrix_create ();
! 	  /* XXX: The horizontal scale factor here must be sufficient
!              for covering the object completely. So we'll need some
!              estimate on the bounds of the object. For now, I'll just
!              throw in an arbitrary hard-coded scale factor. */
! 	  cairo_matrix_scale (matrix,
! 			      (gradient->num_stops - 1) / scale,
! 			      1.0/65536.0);
! 	  cairo_matrix_rotate (matrix,
! 			       -atan2 (y2-y1, x2-x1));
! 	  cairo_matrix_translate (matrix,
! 				  -x1, -y1);
! 	  cairo_surface_set_matrix (pattern, matrix);
! 	  cairo_surface_set_filter (pattern, CAIRO_FILTER_BILINEAR);
! 	  cairo_matrix_destroy (matrix);
! 
! 	  cairo_move_to (svg_cairo->cr, 0, 0);
! 	  cairo_set_pattern (svg_cairo->cr, pattern);
! 	  
! 	  cairo_surface_destroy (pattern);
!       }
!       break;
!       case SVG_GRADIENT_RADIAL:
! 	/* XXX: NYI */
  	break;
      }
! 
      return SVG_STATUS_SUCCESS;
  }
--- 539,599 ----
  _svg_cairo_set_gradient (svg_cairo_t *svg_cairo, svg_gradient_t *gradient)
  {
+     svg_gradient_stop_t *stop;
+     cairo_pattern_t *pattern = NULL;
+     int i;
+     
      switch (gradient->type) {
!     case SVG_GRADIENT_LINEAR:
!     {
! 	double x1, y1, x2, y2;
!       
! 	_svg_cairo_length_to_pixel (svg_cairo, &gradient->u.linear.x1, &x1);
! 	_svg_cairo_length_to_pixel (svg_cairo, &gradient->u.linear.y1, &y1);
! 	_svg_cairo_length_to_pixel (svg_cairo, &gradient->u.linear.x2, &x2);
! 	_svg_cairo_length_to_pixel (svg_cairo, &gradient->u.linear.y2, &y2);
! 	
! 	pattern = cairo_pattern_create_linear (x1, y1, x2, y2);
!     }
!     break;
!     case SVG_GRADIENT_RADIAL:
!     {
! 	double cx, cy, r, fx, fy;
!       
! 	_svg_cairo_length_to_pixel (svg_cairo, &gradient->u.radial.cx, &cx);
! 	_svg_cairo_length_to_pixel (svg_cairo, &gradient->u.radial.cy, &cy);
! 	_svg_cairo_length_to_pixel (svg_cairo, &gradient->u.radial.r, &r);
! 	_svg_cairo_length_to_pixel (svg_cairo, &gradient->u.radial.fx, &fx);
! 	_svg_cairo_length_to_pixel (svg_cairo, &gradient->u.radial.fy, &fy);
! 	
! 	pattern = cairo_pattern_create_radial (fx, fx, 0.0,
! 					       cx, cy, r);
!     } break;
!     }
!     
!     for (i = 0; i < gradient->num_stops; i++) {
! 	stop = &gradient->stops[i];
! 	cairo_pattern_add_color_stop (pattern, stop->offset,
! 				      svg_color_get_red (&stop->color) / 255.0,
! 				      svg_color_get_green (&stop->color) / 255.0,
! 				      svg_color_get_blue (&stop->color) / 255.0,
! 				      stop->opacity);
!     }
!     
!     switch (gradient->spread) {
!     case SVG_GRADIENT_SPREAD_REPEAT:
! 	cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REPEAT);
! 	break;
!     case SVG_GRADIENT_SPREAD_REFLECT:
! 	cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REFLECT);
! 	break;
!     default:
! 	cairo_pattern_set_extend (pattern, CAIRO_EXTEND_NONE);
  	break;
      }
!     
!     cairo_pattern_set_filter (pattern, CAIRO_FILTER_BILINEAR);
!     
!     cairo_set_pattern (svg_cairo->cr, pattern);
!     
      return SVG_STATUS_SUCCESS;
  }
***************
*** 620,623 ****
--- 604,608 ----
      svg_pattern_t *pattern = svg_element_pattern (pattern_element);
      cairo_surface_t *pattern_surface;
+     cairo_pattern_t *surface_pattern;
      double x_px, y_px, width_px, height_px;
  
***************
*** 647,654 ****
      cairo_restore (svg_cairo->cr);
  
!     cairo_surface_set_repeat (pattern_surface, 1);
!     cairo_set_pattern (svg_cairo->cr, pattern_surface);
! 
      cairo_surface_destroy (pattern_surface);
  
      return SVG_STATUS_SUCCESS;
--- 632,643 ----
      cairo_restore (svg_cairo->cr);
  
!     surface_pattern = cairo_pattern_create_for_surface (pattern_surface);
      cairo_surface_destroy (pattern_surface);
+     
+     cairo_pattern_set_extend (surface_pattern, CAIRO_EXTEND_REPEAT);
+     
+     cairo_set_pattern (svg_cairo->cr, surface_pattern);
+     
+     cairo_pattern_destroy (surface_pattern);
  
      return SVG_STATUS_SUCCESS;
***************
*** 667,673 ****
  	if (status)
  	    return status;
- 	status = _svg_cairo_set_alpha (svg_cairo, opacity * svg_cairo->state->opacity);
- 	if (status)
- 	    return status;
  	break;
      case SVG_PAINT_TYPE_GRADIENT:
--- 656,659 ----
***************
*** 682,685 ****
--- 668,675 ----
  	break;
      }
+     
+     status = _svg_cairo_set_alpha (svg_cairo, opacity * svg_cairo->state->opacity);
+     if (status)
+         return status;
  
      return SVG_STATUS_SUCCESS;





More information about the cairo-commit mailing list