예제 #1
0
void GraphicsContext::fillRect(const FloatRect& rect)
{
    if (paintingDisabled())
        return;

    CGContextRef context = platformContext();

    if (m_state.fillGradient) {
        CGContextSaveGState(context);
        CGContextConcatCTM(context, m_state.fillGradient->gradientSpaceTransform());
        if (hasShadow()) {
            CGLayerRef layer = CGLayerCreateWithContext(context, CGSizeMake(rect.width(), rect.height()), 0);
            CGContextRef layerContext = CGLayerGetContext(layer);
            m_state.fillGradient->paint(layerContext);
            CGContextDrawLayerAtPoint(context, CGPointMake(rect.left(), rect.top()), layer);
            CGLayerRelease(layer);
        } else {
            CGContextClipToRect(context, rect);
            m_state.fillGradient->paint(this);
        }
        CGContextRestoreGState(context);
        return;
    }

    if (m_state.fillPattern)
        applyFillPattern();
    CGContextFillRect(context, rect);
}
static CGLayerRef createCGLayerForDrawing(CGContextRef c)
{
    CGRect rect = { 0, 0, 50, 50 };
    CGSize layerSize;
    CGLayerRef layer;

    // Make the layer the size of the rectangle that
    // this code draws into the layer.
    layerSize.width = rect.size.width;
    layerSize.height = rect.size.height;

    // Create the layer to draw into.
    layer = CGLayerCreateWithContext(c, layerSize, NULL);
    if(layer == NULL)
	return NULL;
	
    // Get the context corresponding to the layer. Note
    // that this is a 'Get' function so the code must
    // not release the context.
    CGContextRef layerContext = CGLayerGetContext(layer);
    if(layerContext == NULL){
		CGLayerRelease(layer);
		return NULL;
    }
    
    // Set the fill color to opaque black.
    CGContextSetFillColorWithColor(layerContext, getRGBOpaqueBlackColor());
    
    // Draw the content into the layer.
    CGContextFillRect(layerContext, rect);
    
    // Now the layer has the contents needed.
    return layer;
}
void doSimpleCGLayer(CGContextRef context)
{
    int i,j;
    CGSize s;
    // Create the layer.
    CGLayerRef layer = createCGLayerForDrawing(context);
    if(layer == NULL){
		fprintf(stderr, "Couldn't create layer!\n");
		return;
    }
    
    // Get the size of the layer created.
    s = CGLayerGetSize(layer);
    
    // Clip to a rect that corresponds to
    // a grid of 8x8 layer objects.
    CGContextClipToRect(context, CGRectMake(0, 0, 8*s.width, 8*s.height));
    
    // Paint 8 rows of layer objects.
    for(j = 0 ; j < 8 ; j++){
		CGContextSaveGState(context);
			// Paint 4 columns of layer objects, moving
			// across the drawing canvas by skipping a
			// square on the grid each time across.
			for(i = 0 ; i < 4 ; i++){
				// Draw the layer at the current origin.
				CGContextDrawLayerAtPoint(context, 
					CGPointZero, 
					layer);
				// Translate across two layer widths.
				CGContextTranslateCTM(context, 2*s.width, 0);
			}
		CGContextRestoreGState(context);
		// Translate to the left one layer width on
		// even loop counts and to the right one
		// layer width on odd loop counts. Each
		// time through the outer loop, translate up
		// one layer height.
		CGContextTranslateCTM(context, 
			(j % 2) ? s.width: -s.width, 
			s.height);
    }
    // Release the layer when done drawing with it.
    CGLayerRelease(layer);
}
void TilePDFWithCGLayer(CGContextRef context, CFURLRef url)
{
    // Again this should really be computed based on
    // the area intended to be tiled.
    float fillwidth = 612., fillheight = 792.;
    CGSize s;
    float tileX, tileY, tileOffsetX, tileOffsetY;
    float w, h;
    CGLayerRef layer = createLayerWithImageForContext(context, url);
    if(layer == NULL){
	    fprintf(stderr, "Couldn't create the layer!\n");
	    return;
    }
    
    // Compute the tile size and offset.
    s = CGLayerGetSize(layer);
    tileX = s.width;
    tileY = s.height;

#if DOSCALING
    // Space the tiles by the tile width and height
    // plus an extra 2 units in each dimension.
    tileOffsetX = 2. + tileX;
    tileOffsetY = 2. + tileY;
#else
	// Add 6 units to the offset in each direction
	// if there is no scaling of the source PDF document.
    tileOffsetX = 6. + tileX;
    tileOffsetY = 6. + tileY;
#endif

    // Now draw the contents of the layer to the context.
    // The layer is drawn at its true size (the size of
    // the tile) with its origin located at the corner
    // of each tile.
    for(h = 0; h < fillheight ; h += tileOffsetY)
		for(w = 0; w < fillwidth ; w += tileOffsetX){
			CGContextDrawLayerAtPoint(context, CGPointMake(w, h), layer);
		}
    
    // Release the layer when done drawing with it.
    CGLayerRelease(layer);
}
static CGLayerRef createLayerWithImageForContext(CGContextRef c, CFURLRef url)
{
    CGSize layerSize;
    CGLayerRef layer;
    CGPDFDocumentRef pdfDoc = getThePDFDoc(url, &layerSize.width, &layerSize.height);
    if(pdfDoc == NULL){
		return NULL;
    }

#if DOSCALING
    // Make the layer 1/3 the size of the PDF document.
    layerSize.width /= 3;
    layerSize.height /= 3;
#endif

    // Create the layer to draw into.
    layer = CGLayerCreateWithContext(c, layerSize, NULL);
    if(layer == NULL)
		return NULL;
	
    // Get the context corresponding to the layer. Note
    // that this is a 'Get' function so the code must
    // not release the context.
    CGContextRef layerContext = CGLayerGetContext(layer);
    if(layerContext == NULL){
		CGLayerRelease(layer);
		return NULL;
    }
    
    // Draw the PDF document into the layer.
    CGContextDrawPDFDocument(layerContext,
		CGRectMake(0, 0, layerSize.width, layerSize.height), pdfDoc, 1);
    
    // Now the layer has the contents needed.
    return layer;
}