device_x, device_y, device_width, device_height);
extents.x, extents.y, extents.width, extents.height);
_cairo_gstate_show_surface (cairo_gstate_t	*gstate,			    cairo_surface_t	*surface,			    int			width,			    int			height){    /* We are dealing with 6 coordinate spaces in this function. this makes     * it ugly.      *     * - "Image" space is the space of the surface we're reading pixels from.     *   it is the surface argument to this function. The surface has a     *   matrix attached to it which maps "user" space (see below) into     *   image space.     *     * - "Device" space is the space of the surface we're ultimately writing     *   pixels to. It is the current surface of the gstate argument to     *   this function.     *      * - "User" space is an arbitrary space defined by the user, defined      *   implicitly by the gstate's CTM. The CTM maps from user space to     *   device space. The CTM inverse (which is also kept at all times)     *   maps from device space to user space.     *     * - "Clip" space is the space of the surface being used to clip pixels     *   during compositing. Space-wise, it is a bounding box (offset+size)     *   within device space. This surface is usually smaller than the device     *   surface (and possibly the image surface too) and logically occupies     *   a bounding box around the "clip path", situated somewhere in device     *   space. The clip path is already painted on the clip surface.     *     * - "Intermediate" space is the subset of the Clip space that the     *   drawing will affect, and we allocate an intermediate surface     *   of this size so that we can paint in it.     *     * - "Pattern" space is another arbitrary space defined in the pattern     *   element of gstate. As pixels are read from image space, they are     *   combined with pixels being read from pattern space and pixels     *   already existing in device space. User coordinates are converted     *   to pattern space, similarly, using a matrix attached to the pattern.     *   (in fact, there is a 7th space in here, which is the space of the     *   surface acting as a source for the pattern)     *     * To composite these spaces, we temporarily change the image surface      * so that it can be read and written in device coordinates; in a sense     * this makes it "spatially compatible" with the clip and device spaces.     *     *     * There is also some confusion about the interaction between a clip and     * a pattern; it is assumed that in this "show surface" operation a pattern     * is to be used as an auxiliary alpha mask. this might be wrong, but it's      * what we're doing now.      *     * so, to follow the operations below, remember that in the compositing     * model, each operation is always of the form ((src IN mask) OP dst).     * that's the basic operation.     *     * so the compositing we are trying to do here, in general, involves 2     * steps, going via a temporary surface:     *     *  - combining clip and pattern pixels together into a mask channel.     *    this will be ((pattern IN clip) SRC temporary). it ignores the     *    pixels already in the temporary, overwriting it with the     *    pattern, clipped to the clip mask.     *     *  - combining temporary and "image" pixels with "device" pixels,     *    with a user-provided porter/duff operator. this will be     *    ((image IN temporary) OP device).     *     * if there is no clip, the degenerate case is just the second step     * with pattern standing in for temporary.     *     */    cairo_status_t status;    cairo_matrix_t user_to_image, image_to_user;    cairo_matrix_t image_to_device, device_to_image;    double device_x, device_y;    double device_width, device_height;    cairo_pattern_t pattern;    cairo_box_t pattern_extents;            cairo_surface_get_matrix (surface, &user_to_image);    cairo_matrix_multiply (&device_to_image, &gstate->ctm_inverse, &user_to_image);    cairo_surface_set_matrix (surface, &device_to_image);    image_to_user = user_to_image;    cairo_matrix_invert (&image_to_user);    cairo_matrix_multiply (&image_to_device, &image_to_user, &gstate->ctm);    _cairo_gstate_current_point (gstate, &device_x, &device_y);    device_width = width;    device_height = height;    _cairo_matrix_transform_bounding_box (&image_to_device,					  &device_x, &device_y,					  &device_width, &device_height);    _cairo_pattern_init (&pattern);    pattern_extents.p1.x = _cairo_fixed_from_double (device_x);    pattern_extents.p1.y = _cairo_fixed_from_double (device_y);    pattern_extents.p2.x = _cairo_fixed_from_double (device_x + device_width);    pattern_extents.p2.y = _cairo_fixed_from_double (device_y + device_height);        if ((gstate->pattern->type != CAIRO_PATTERN_SOLID) ||	(gstate->alpha != 1.0)) {	/* I'm allowing any type of pattern for the mask right now.	   Maybe this is bad. Will allow for some cool effects though. */	_cairo_pattern_init_copy (&pattern, gstate->pattern);	status = _cairo_gstate_create_pattern (gstate, &pattern, &pattern_extents);	if (status)	    return status;    }        if (gstate->clip.surface)    {	cairo_surface_t *intermediate;	cairo_color_t empty_color;	pixman_box16_t *draw_extents;	pixman_region16_t *draw_region;	draw_region = pixman_region_create ();	if (draw_region == NULL)	{	    status = CAIRO_STATUS_NO_MEMORY;	    goto BAIL0;	}		status = _calculate_region_for_intermediate_clip_surface (draw_region,								  &pattern_extents,								  &gstate->clip);	if (status)	    goto BAIL1;	/* Shortcut if empty */	if (!pixman_region_not_empty (draw_region)) {	    status = CAIRO_STATUS_SUCCESS;	    goto BAIL1;	}		draw_extents = pixman_region_extents (draw_region);		_cairo_color_init (&empty_color);	_cairo_color_set_alpha (&empty_color, .0);	intermediate = _cairo_surface_create_similar_solid (gstate->clip.surface,							    CAIRO_FORMAT_A8,							    draw_extents->x2 - draw_extents->x1,							    draw_extents->y2 - draw_extents->y1,							    &empty_color);	/* it is not completely clear what the "right" way to combine the	 pattern and mask surface is. I will use the the clip as a source	 and the pattern as a mask in building up my temporary, because	 this is not *totally* bogus and accomodates the case where	 pattern's source image is NULL reasonably well. feel free to	 correct this if you see a reason. */	status = _cairo_surface_composite (CAIRO_OPERATOR_SRC,					   gstate->clip.surface,					   pattern.source,					   intermediate,					   draw_extents->x1 - gstate->clip.x,					   draw_extents->y1 - gstate->clip.y, 					   0, 0,					   0, 0,					   draw_extents->x2 - draw_extents->x1,					   draw_extents->y2 - draw_extents->y1);	if (status)	    goto BAIL2;	status = _cairo_surface_composite (gstate->operator,					   surface, 					   intermediate,					   gstate->surface,					   draw_extents->x1, draw_extents->y1,					   0, 0,					   draw_extents->x1, draw_extents->y1,					   draw_extents->x2 - draw_extents->x1,					   draw_extents->y2 - draw_extents->y1);	    BAIL2:	cairo_surface_destroy (intermediate);    BAIL1:	pixman_region_destroy (draw_region);    BAIL0:	;    }    else    {		/* XXX: The rendered size is sometimes 1 or 2 pixels short from	   what I expect. Need to fix this. */	status = _cairo_surface_composite (gstate->operator,					   surface, 					   pattern.source, 					   gstate->surface,					   device_x, device_y,					   0, 0,					   device_x, device_y,					   device_width,					   device_height);    }    _cairo_pattern_fini (&pattern);        /* restore the matrix originally in the surface */    cairo_surface_set_matrix (surface, &user_to_image);        if (status)	return status;        return CAIRO_STATUS_SUCCESS;}