DRAW_TEST_F(CGContextFlush, FillFlush, WhiteBackgroundTest<>) { CGContextRef context = GetDrawingContext(); CGRect bounds = GetDrawingBounds(); _CGContextPushBeginDraw(context); CGContextSetRGBFillColor(context, 1, 0, 0, 1); CGContextFillRect(context, bounds); // Flush the red fill rect CGContextFlush(context); CGContextSetRGBFillColor(context, 0, 0, 1, 1); CGContextFillRect(context, CGRectMake(0, 0, 300, 300)); // We should still have red & blue rectangle should not show up. unsigned char* dataPtr = static_cast<unsigned char*>(CGBitmapContextGetData(context)); ASSERT_NE(dataPtr, nullptr); // Validate only the red fill rect is executed. EXPECT_EQ(dataPtr[0], 0x00); EXPECT_EQ(dataPtr[1], 0x00); EXPECT_EQ(dataPtr[2], 0xff); EXPECT_EQ(dataPtr[3], 0xff); CGContextFlush(context); // We should now see the blue fill rect EXPECT_EQ(dataPtr[0], 0xff); EXPECT_EQ(dataPtr[1], 0x00); EXPECT_EQ(dataPtr[2], 0x00); EXPECT_EQ(dataPtr[3], 0xff); // Add some extra drawings CGContextClearRect(context, CGRectMake(100, 100, 200, 300)); CGPoint center = _CGRectGetCenter(bounds); CGMutablePathRef concentricCirclesPath = CGPathCreateMutable(); CGPathAddEllipseInRect(concentricCirclesPath, nullptr, _CGRectCenteredOnPoint({ 50, 50 }, center)); CGPathAddEllipseInRect(concentricCirclesPath, nullptr, _CGRectCenteredOnPoint({ 100, 100 }, center)); CGPathAddEllipseInRect(concentricCirclesPath, nullptr, _CGRectCenteredOnPoint({ 150, 150 }, center)); CGPathAddEllipseInRect(concentricCirclesPath, nullptr, _CGRectCenteredOnPoint({ 200, 200 }, center)); CGContextSetRGBFillColor(context, 1.0, 0.0, 0.0, 0.5); CGContextSetRGBStrokeColor(context, 1.0, 0.0, 0.0, 1.0); CGContextAddPath(context, concentricCirclesPath); CGContextDrawPath(context, kCGPathFillStroke); CGPathRelease(concentricCirclesPath); _CGContextPopEndDraw(context); }
int drawrun(CGContextRef context, struct BMAP_Run run, const void *buf) { int i; int x; int offset; x = run.startx; offset = 0; while (x < run.endx) { int len; if (sizeof(struct BMAP_Run) + (offset + 2)/2 > run.datalen) { return -1; } len = readnibble(buf, offset++); if (len >= 0 && len <= 7) { /* this is a sequence of different tiles */ len += 1; if (sizeof(struct BMAP_Run) + (offset + len + 1)/2 > run.datalen) { return -1; } for (i = 0; i < len; i++) { /* draw terrain */ setterraincolor(context, readnibble(buf, offset++)); CGContextFillRect(context, CGRectMake(x++, run.y, 1, 1)); } } else if (len >= 8 && len <= 15) { /* this is a sequence of like tiles */ len -= 6; if (sizeof(struct BMAP_Run) + (offset + 2)/2 > run.datalen) { return -1; } /* draw terrain */ setterraincolor(context, readnibble(buf, offset++)); CGContextFillRect(context, CGRectMake(x, run.y, len, 1)); x += len; } else { return -1; } } if (sizeof(struct BMAP_Run) + (offset + 1)/2 != run.datalen) { return -1; } return 0; }
OSStatus GenerateThumbnailForURL(void *thisInterface, QLThumbnailRequestRef thumbnail, CFURLRef url, CFStringRef contentTypeUTI, CFDictionaryRef options, CGSize maxSize) { // load the actual file CFDataRef fileData = nil; CFDictionaryRef propertyDictionary = nil; SInt32 errorCode; if (CFURLCreateDataAndPropertiesFromResource(NULL, url, &fileData, &propertyDictionary, nil, &errorCode)) { CFIndex dataLength = CFDataGetLength(fileData); UInt8 *bytes = (UInt8 *)CFDataGetBytePtr(fileData); long pages = dataLength / ONE_LCD_FRAME_BYTESIZE; CGSize lcdSize = CGSizeMake(LCD_FRAME_PIXEL_WIDTH, LCD_FRAME_PIXEL_HEIGHT * pages); CGContextRef cgContext = QLThumbnailRequestCreateContext(thumbnail, lcdSize, true, NULL); if(cgContext) { CGColorRef lcdForegroundColor = CGColorCreateGenericRGB(0.467, 0.522, 0.047, 1.000); CGColorRef lcdBackgroundColor = CGColorCreateGenericRGB(0.227, 0.192, 0.000, 1.000); CGContextSetFillColorWithColor(cgContext, lcdBackgroundColor); CGContextFillRect(cgContext, CGRectMake(0,0,lcdSize.width,lcdSize.height)); CGPoint origin = CGPointMake(0,0); while (pages-- > 0) { CGContextSetFillColorWithColor(cgContext, lcdForegroundColor); // CGContextFillRect(cgContext, CGRectMake(origin.x,origin.y,10,10)); for (int y=0;y<9;y++) { for (int x=0; x < LCD_FRAME_PIXEL_WIDTH; x++) { UInt8 byte = bytes[y*LCD_FRAME_PIXEL_WIDTH + (LCD_FRAME_PIXEL_WIDTH - x - 1)]; if (byte > 0) { UInt8 byteMask = 1; for (int littleY = 0; littleY < 8; littleY++) { if ((byte & (byteMask << littleY))) { CGContextFillRect(cgContext, CGRectMake(origin.x + x,origin.y + y*8 + littleY,1,1)); } } } } } origin.y += 68; bytes += ONE_LCD_FRAME_BYTESIZE; } // When we are done with our drawing code QLPreviewRequestFlushContext() is called to flush the context QLThumbnailRequestFlushContext(thumbnail, cgContext); } CFRelease(cgContext); CFRelease(fileData); CFRelease(propertyDictionary); } return noErr; }
static void Quartz_Rect(double x0, double y0, double x1, double y1, R_GE_gcontext *gc, NewDevDesc *dd) { QuartzDesc *xd = (QuartzDesc*)dd->deviceSpecific; CGRect rect; CGPoint origin; CGSize size; origin.x = x0; origin.y = y0; size.width = x1-x0; size.height = y1-y0; rect.size = size; rect.origin = origin; CGContextSaveGState( GetContext(xd) ); Quartz_SetLineProperties(gc, dd); Quartz_SetFill( gc->fill, gc->gamma, dd); CGContextFillRect( GetContext(xd), rect); Quartz_SetStroke( gc->col, gc->gamma, dd); CGContextStrokeRect( GetContext(xd), rect); CGContextRestoreGState( GetContext(xd) ); }
DRAW_TEST_F(CGImageDrawing, DrawAContextIntoAnImage, UIKitMimicTest<>) { // This test will create a bitmap context, draw some entity into the context, then create a image out of the bitmap context. // Thereafter it will draw the image into the Canvas context static woc::unique_cf<CGColorSpaceRef> rgbColorSpace(CGColorSpaceCreateDeviceRGB()); // Create a bitmap context to draw the Image into woc::unique_cf<CGContextRef> contextImage(CGBitmapContextCreate( nullptr, 10, 10, 8, 4 * 10 /* bytesPerRow = bytesPerPixel*width*/, rgbColorSpace.get(), kCGImageAlphaPremultipliedFirst)); ASSERT_NE(contextImage, nullptr); CGContextSetRGBFillColor(contextImage.get(), 1.0, 0.0, 0.0, 1.0); CGContextFillRect(contextImage.get(), { 0, 0, 10, 10 }); // Create the image out of the bitmap context woc::unique_cf<CGImageRef> image(CGBitmapContextCreateImage(contextImage.get())); ASSERT_NE(image, nullptr); CGContextRef context = GetDrawingContext(); CGRect bounds = GetDrawingBounds(); CGAffineTransform flip = CGAffineTransformMakeScale(1, -1); CGAffineTransform shift = CGAffineTransformTranslate(flip, 0, bounds.size.height * -1); CGContextConcatCTM(context, shift); // draw the image CGContextDrawImage(context, bounds, image.get()); }
DragImageRef createDragImageFromImage(Image* img, ImageOrientationDescription) { HWndDC dc(0); auto workingDC = adoptGDIObject(::CreateCompatibleDC(dc)); if (!workingDC) return 0; CGContextRef drawContext = 0; auto hbmp = allocImage(workingDC.get(), img->size(), &drawContext); if (!hbmp || !drawContext) return 0; CGImageRef srcImage = img->getCGImageRef(); CGRect rect; rect.size = img->size(); rect.origin.x = 0; rect.origin.y = -rect.size.height; static const CGFloat white [] = {1.0, 1.0, 1.0, 1.0}; CGContextScaleCTM(drawContext, 1, -1); CGContextSetFillColor(drawContext, white); CGContextFillRect(drawContext, rect); if (srcImage) { CGContextSetBlendMode(drawContext, kCGBlendModeNormal); CGContextDrawImage(drawContext, rect, srcImage); } CGContextRelease(drawContext); return hbmp.leak(); }
void PlatformCALayer::drawRepaintIndicator(CGContextRef context, PlatformCALayer* platformCALayer, int repaintCount, CGColorRef customBackgroundColor) { char text[16]; // that's a lot of repaints snprintf(text, sizeof(text), "%d", repaintCount); CGRect indicatorBox = platformCALayer->bounds(); CGContextSaveGState(context); indicatorBox.size.width = 12 + 10 * strlen(text); indicatorBox.size.height = 27; CGContextSetAlpha(context, 0.5f); CGContextBeginTransparencyLayerWithRect(context, indicatorBox, 0); if (customBackgroundColor) CGContextSetFillColorWithColor(context, customBackgroundColor); else CGContextSetRGBFillColor(context, 0, 0.5f, 0.25f, 1); CGContextFillRect(context, indicatorBox); if (platformCALayer->acceleratesDrawing()) CGContextSetRGBFillColor(context, 1, 0, 0, 1); else CGContextSetRGBFillColor(context, 1, 1, 1, 1); platformCALayer->drawTextAtPoint(context, indicatorBox.origin.x + 5, indicatorBox.origin.y + 22, CGSizeMake(1, -1), 22, text, strlen(text)); CGContextEndTransparencyLayer(context); CGContextRestoreGState(context); }
static void paintRepaintRectOverlay(CGContextRef context, WKImageRef image, WKArrayRef repaintRects) { WKSize imageSize = WKImageGetSize(image); CGContextSaveGState(context); // Using a transparency layer is easier than futzing with clipping. CGContextBeginTransparencyLayer(context, 0); // Flip the context. CGContextScaleCTM(context, 1, -1); CGContextTranslateCTM(context, 0, -imageSize.height); CGContextSetRGBFillColor(context, 0, 0, 0, static_cast<CGFloat>(0.66)); CGContextFillRect(context, CGRectMake(0, 0, imageSize.width, imageSize.height)); // Clear the repaint rects. size_t count = WKArrayGetSize(repaintRects); for (size_t i = 0; i < count; ++i) { WKRect rect = WKRectGetValue(static_cast<WKRectRef>(WKArrayGetItemAtIndex(repaintRects, i))); CGRect cgRect = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height); CGContextClearRect(context, cgRect); } CGContextEndTransparencyLayer(context); CGContextRestoreGState(context); }
// Draws a filled rectangle with a stroked border. void GraphicsContext::drawRect(const IntRect& rect) { if (paintingDisabled()) return; CGContextRef context = platformContext(); if (fillColor().alpha()) CGContextFillRect(context, rect); if (strokeStyle() != NoStroke && strokeColor().alpha()) { // We do a fill of four rects to simulate the stroke of a border. Color oldFillColor = fillColor(); if (oldFillColor != strokeColor()) setCGFillColor(context, strokeColor()); CGRect rects[4] = { FloatRect(rect.x(), rect.y(), rect.width(), 1), FloatRect(rect.x(), rect.bottom() - 1, rect.width(), 1), FloatRect(rect.x(), rect.y() + 1, 1, rect.height() - 2), FloatRect(rect.right() - 1, rect.y() + 1, 1, rect.height() - 2) }; CGContextFillRects(context, rects, 4); if (oldFillColor != strokeColor()) setCGFillColor(context, oldFillColor); } }
// Draws a filled rectangle with a stroked border. void GraphicsContext::drawRect(const IntRect& rect) { // FIXME: this function does not handle patterns and gradients // like drawPath does, it probably should. if (paintingDisabled()) return; CGContextRef context = platformContext(); CGContextFillRect(context, rect); if (strokeStyle() != NoStroke) { // We do a fill of four rects to simulate the stroke of a border. Color oldFillColor = fillColor(); if (oldFillColor != strokeColor()) setCGFillColor(context, strokeColor(), strokeColorSpace()); CGRect rects[4] = { FloatRect(rect.x(), rect.y(), rect.width(), 1), FloatRect(rect.x(), rect.bottom() - 1, rect.width(), 1), FloatRect(rect.x(), rect.y() + 1, 1, rect.height() - 2), FloatRect(rect.right() - 1, rect.y() + 1, 1, rect.height() - 2) }; CGContextFillRects(context, rects, 4); if (oldFillColor != strokeColor()) setCGFillColor(context, oldFillColor, fillColorSpace()); } }
void PlatformCALayer::drawRepaintIndicator(CGContextRef context, PlatformCALayer* platformCALayer, int repaintCount, CGColorRef customBackgroundColor) { char text[16]; // that's a lot of repaints snprintf(text, sizeof(text), "%d", repaintCount); CGRect indicatorBox = platformCALayer->bounds(); indicatorBox.size.width = 12 + 10 * strlen(text); indicatorBox.size.height = 27; CGContextSaveGState(context); CGContextSetAlpha(context, 0.5f); CGContextBeginTransparencyLayerWithRect(context, indicatorBox, 0); if (customBackgroundColor) CGContextSetFillColorWithColor(context, customBackgroundColor); else CGContextSetRGBFillColor(context, 0, 0.5f, 0.25f, 1); CGContextFillRect(context, indicatorBox); if (platformCALayer->acceleratesDrawing()) CGContextSetRGBFillColor(context, 1, 0, 0, 1); else CGContextSetRGBFillColor(context, 1, 1, 1, 1); #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" CGContextSetTextMatrix(context, CGAffineTransformMakeScale(1, -1)); CGContextSelectFont(context, "Helvetica", 22, kCGEncodingMacRoman); CGContextShowTextAtPoint(context, indicatorBox.origin.x + 5, indicatorBox.origin.y + 22, text, strlen(text)); #pragma clang diagnostic pop CGContextEndTransparencyLayer(context); CGContextRestoreGState(context); }
bool MCTileCacheCoreGraphicsCompositor_CompositeRect(void *p_context, int32_t p_x, int32_t p_y, uint32_t p_color) { MCTileCacheCoreGraphicsCompositorContext *self; self = (MCTileCacheCoreGraphicsCompositorContext *)p_context; // IM-2013-08-23: [[ RefactorGraphics ]] Use MCGPixelUnpackNative to fix color swap issues uint8_t r, g, b, a; MCGPixelUnpackNative(p_color, r, g, b, a); CGFloat t_red, t_green, t_blue, t_alpha; t_red = r / 255.0; t_green = g / 255.0; t_blue = b / 255.0; t_alpha = a / 255.0; if (t_alpha != 1.0) { t_red /= t_alpha; t_green /= t_alpha; t_blue /= t_alpha; } CGContextSetRGBFillColor(self -> cgcontext, t_red, t_green, t_blue, t_alpha); CGContextFillRect(self -> cgcontext, CGRectMake(p_x, self -> viewport_height - (p_y + self -> tile_size), self -> tile_size, self -> tile_size)); return true; }
FX_BOOL CFX_QuartzDeviceDriver::FillRect(const FX_RECT* rect, FX_ARGB fillArgb, int alphaFlag , void* iccTransform , int blend_type ) { CGBlendMode mode = GetCGBlendMode(blend_type); if (mode != kCGBlendModeNormal) { CGContextSetBlendMode(_context, mode); } CGRect rect_fx = CGRectMake(rect->left, rect->top, rect->Width(), rect->Height()); CGRect rect_usr = CGRectApplyAffineTransform(rect_fx, _foxitDevice2User); FX_INT32 a, r, g, b; ArgbDecode(fillArgb, a, r, g, b); CGContextSetRGBFillColor(_context, r / 255.f, g / 255.f, b / 255.f, a / 255.f); CGContextFillRect(_context, rect_usr); if (mode != kCGBlendModeNormal) { CGContextSetBlendMode(_context, kCGBlendModeNormal); } return TRUE; }
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 doPixelAlignedFillAndStroke(CGContextRef context) { CGPoint p1 = CGPointMake(16.7, 17.8); CGPoint p2 = CGPointMake(116.7, 17.8); CGRect r = CGRectMake(16.7, 20.8, 100.6, 100.6); CGSize s; CGContextSetLineWidth(context, 2); CGContextSetRGBFillColor(context, 1., 0., 0., 1.); CGContextSetRGBStrokeColor(context, 1., 0., 0., 1.); // Unaligned drawing. CGContextBeginPath(context); CGContextMoveToPoint(context, p1.x, p1.y); CGContextAddLineToPoint(context, p2.x, p2.y); CGContextStrokePath(context); CGContextFillRect(context, r); // Translate to the right before drawing along // aligned coordinates. CGContextTranslateCTM(context, 106, 0); // Aligned drawing. // Compute the length of the line in user space. s = CGSizeMake(p2.x - p1.x, p2.y - p1.y); CGContextBeginPath(context); // Align the starting point to a device // pixel boundary. p1 = alignPointToUserSpace(context, p1); // Establish the starting point of the line. CGContextMoveToPoint(context, p1.x, p1.y); // Compute the line length as an integer // number of device pixels. s = alignSizeToUserSpace(context, s); CGContextAddLineToPoint(context, p1.x + s.width, p1.y + s.height); CGContextStrokePath(context); // Compute a rect that is aligned to device // space with a width that is an integer // number of device pixels. r = alignRectToUserSpace(context, r); CGContextFillRect(context, r); }
CGContextRef createRGBBitmapContext(size_t width, size_t height, Boolean wantDisplayColorSpace, Boolean needsTransparentBitmap) { /* This routine allocates data for a pixel array that contains width*height pixels where each pixel is 4 bytes. The format is 8-bit ARGB or XRGB, depending on whether needsTransparentBitmap is true. In order to get the recommended pixel alignment, the bytesPerRow is rounded up to the nearest multiple of BEST_BYTE_ALIGNMENT bytes. */ CGContextRef context; size_t bytesPerRow; unsigned char *rasterData; // Minimum bytes per row is 4 bytes per sample * number of samples. bytesPerRow = width*4; // Round to nearest multiple of BEST_BYTE_ALIGNMENT. bytesPerRow = COMPUTE_BEST_BYTES_PER_ROW(bytesPerRow); // Allocate the data for the raster. The total amount of data is bytesPerRow // times the number of rows. The function 'calloc' is used so that the // memory is initialized to 0. rasterData = calloc(1, bytesPerRow * height); if(rasterData == NULL){ fprintf(stderr, "Couldn't allocate the needed amount of memory!\n"); return NULL; } // The wantDisplayColorSpace argument passed to the function determines // whether or not to use the display color space or the generic calibrated // RGB color space. The needsTransparentBitmap argument determines whether // create a context that records alpha or not. context = CGBitmapContextCreate(rasterData, width, height, 8, bytesPerRow, (wantDisplayColorSpace ? getTheDisplayColorSpace(): getTheCalibratedRGBColorSpace()) , (needsTransparentBitmap ? kCGImageAlphaPremultipliedFirst : kCGImageAlphaNoneSkipFirst)); if(context == NULL){ // If the context couldn't be created, release the raster memory. free(rasterData); fprintf(stderr, "Couldn't create the context!\n"); return NULL; } // Either clear the rect or paint with opaque white, depending on // the needs of the caller. if(needsTransparentBitmap){ // Clear the context bits so they are transparent. CGContextClearRect(context, CGRectMake(0, 0, width, height)); }else{ // Since the drawing destination is opaque, first paint // the context bits to white. CGContextSaveGState(context); CGContextSetFillColorWithColor(context, getRGBOpaqueWhiteColor()); CGContextFillRect(context, CGRectMake(0, 0, width, height)); CGContextRestoreGState(context); } return context; }
static void Quartz_NewPage(R_GE_gcontext *gc, NewDevDesc *dd) { QuartzDesc *xd = (QuartzDesc*)dd->deviceSpecific; CGPoint origin = {0.0, 0.0}; CGSize size; CGRect area; size.width = xd->windowWidth; size.height = xd->windowHeight; area.origin = origin; area.size = size; Quartz_Clip(0,size.width, 0, size.height, dd); /* * Paul to Stefano: * Not sure what is intended here: looks like you are * making sure that on a "new page" operation you clear * the window -- filling the window with a "missing" * colour wouldn't do the job so you use "white". * We no longer deal with NA as a colour internally so * I have changed this as follows: * (i) if gc->fill is not opaque, then fill with white * (to clear the window) * (ii) fill with gc->fill * (to produce the specified "background" which may or * may not be transparent) */ if (!R_OPAQUE(gc->fill)) { unsigned int tempcol = gc->fill; gc->fill = R_RGB(255, 255, 255); Quartz_SetFill(gc->fill, gc->gamma, dd); CGContextFillRect( GetContext(xd), area); gc->fill = tempcol; } Quartz_SetFill(gc->fill, gc->gamma, dd); CGContextFillRect( GetContext(xd), area); CGContextFlush( GetContext(xd) ); /* we need to flash it just now */ }
void CanvasRenderingContext2D::fillRect(float x, float y, float width, float height, ExceptionCode& ec) { ec = 0; if (!(width >= 0 && height >= 0)) { ec = INDEX_SIZE_ERR; return; } GraphicsContext* c = drawingContext(); if (!c) return; // FIXME: Do this through platform-independent GraphicsContext API. #if PLATFORM(CG) CGRect rect = CGRectMake(x, y, width, height); willDraw(rect); if (state().m_fillStyle->gradient()) { // Shading works on the entire clip region, so convert the rect to a clip. c->save(); CGContextClipToRect(c->platformContext(), rect); CGContextDrawShading(c->platformContext(), state().m_fillStyle->gradient()->platformShading()); c->restore(); } else { if (state().m_fillStyle->pattern()) applyFillPattern(); CGContextFillRect(c->platformContext(), rect); } #elif PLATFORM(QT) QRectF rect(x, y, width, height); willDraw(rect); QPainter* p = static_cast<QPainter*>(c->platformContext()); if (state().m_fillStyle->gradient()) { p->fillRect(rect, QBrush(*(state().m_fillStyle->gradient()->platformShading()))); } else { if (state().m_fillStyle->pattern()) applyFillPattern(); p->fillRect(rect, p->brush()); } #elif PLATFORM(CAIRO) FloatRect rect(x, y, width, height); willDraw(rect); cairo_t* cr = c->platformContext(); cairo_save(cr); if (state().m_fillStyle->gradient()) { cairo_set_source(cr, state().m_fillStyle->gradient()->platformShading()); } else { if (state().m_fillStyle->pattern()) applyFillPattern(); } cairo_rectangle(cr, x, y, width, height); cairo_fill(cr); cairo_restore(cr); #endif }
void drawWithGlobalAlpha(CGContextRef context) { int i; CGRect rect = CGRectMake(40., 210., 100., 100.); float color[4] = { 1.0, 0.0, 0.0, 1.0 }; // opaque red // Set the fill color space to that returned by getTheCalibratedRGBColorSpace. CGContextSetFillColorSpace(context, getTheCalibratedRGBColorSpace()); CGContextSetFillColor(context, color); for(i = 0; i < 2 ; i++){ CGContextSaveGState(context); // Paint the leftmost rect on this row with 100% opaque red. CGContextFillRect(context, rect); CGContextTranslateCTM(context, rect.size.width + 70., 0.); // Set the alpha value of this rgba color to 0.5. color[3] = 0.5; // Use the new color as the fill color in the graphics state. CGContextSetFillColor(context, color); // Paint the center rect on this row with 50% opaque red. CGContextFillRect(context, rect); CGContextTranslateCTM(context, rect.size.width + 70., 0.); // Set the alpha value of this rgba color to 0.25. color[3] = 0.25; // Use the new color as the fill color in the graphics state. CGContextSetFillColor(context, color); // Paint the rightmost rect on this row with 25% opaque red. CGContextFillRect(context, rect); CGContextRestoreGState(context); // After restoring the graphics state, the fill color is set to // that prior to calling CGContextSaveGState, that is, opaque // red. The coordinate system is also restored. // Now set the context global alpha value to 50% opaque. CGContextSetAlpha(context, 0.5); // Translate down for a second row of rectangles. CGContextTranslateCTM(context, 0., -(rect.size.height + 70.)); // Reset the alpha value of the color array to fully opaque. color[3] = 1.0; } }
DRAW_TEST_F(CGContextFlush, FillFlushMultipleDrawingCounters, WhiteBackgroundTest<>) { CGContextRef context = GetDrawingContext(); CGRect bounds = GetDrawingBounds(); static int sDrawCount = 5; for (int i = 0; i < sDrawCount; ++i) { _CGContextPushBeginDraw(context); } CGContextSetRGBFillColor(context, 1, 0, 0, 1); CGContextFillRect(context, bounds); for (int i = 0; i < sDrawCount; ++i) { // Multiple flushes should work. CGContextFlush(context); } // Add some extra drawings CGContextClearRect(context, bounds); // We should still have red & clear should not of been executed. unsigned char* dataPtr = static_cast<unsigned char*>(CGBitmapContextGetData(context)); ASSERT_NE(dataPtr, nullptr); // Validate only the red fill rect is executed. EXPECT_EQ(dataPtr[0], 0x00); EXPECT_EQ(dataPtr[1], 0x00); EXPECT_EQ(dataPtr[2], 0xff); EXPECT_EQ(dataPtr[3], 0xff); for (int i = 0; i < 3; ++i) { // Multiple flushes should work. CGContextFlush(context); } // validate clear EXPECT_EQ(dataPtr[0], 0x00); EXPECT_EQ(dataPtr[1], 0x00); EXPECT_EQ(dataPtr[2], 0x00); EXPECT_EQ(dataPtr[3], 0x00); CGContextSetRGBStrokeColor(context, 0, 1, 0, 1); CGContextStrokeRect(context, CGRectMake(100, 100, 200, 300)); // Still should be clear. EXPECT_EQ(dataPtr[0], 0x00); EXPECT_EQ(dataPtr[1], 0x00); EXPECT_EQ(dataPtr[2], 0x00); EXPECT_EQ(dataPtr[3], 0x00); for (int i = 0; i < sDrawCount; ++i) { _CGContextPopEndDraw(context); } }
void drawRect(const SkRect& r, SkColor c) override { CGContextRef cg = (CGContextRef)fCanvas->accessTopRasterHandle(); CGColorRef color = CGColorCreateGenericRGB(SkColorGetR(c)/255.f, SkColorGetG(c)/255.f, SkColorGetB(c)/255.f, SkColorGetA(c)/255.f); CGContextSetFillColorWithColor(cg, color); CGContextFillRect(cg, CGRectMake(r.x(), r.y(), r.width(), r.height())); }
/*---------------------------------------------------------------------- * +++ ToolbarBackground element -- toolbar style for frames. * * This is very similar to the normal background element, but uses a * different ThemeBrush in order to get the lighter pinstripe effect * used in toolbars. We use SetThemeBackground() rather than * ApplyThemeBackground() in order to get the right style. * * <URL: http://developer.apple.com/documentation/Carbon/Reference/ * Appearance_Manager/appearance_manager/constant_7.html#/ * /apple_ref/doc/uid/TP30000243/C005321> * */ static void ToolbarBackgroundElementDraw( void *clientData, void *elementRecord, Tk_Window tkwin, Drawable d, Ttk_Box b, Ttk_State state) { ThemeBrush brush = kThemeBrushToolbarBackground; CGRect bounds = BoxToRect(d, Ttk_WinBox(tkwin)); BEGIN_DRAWING(d) ChkErr(HIThemeSetFill, brush, NULL, dc.context, HIOrientation); //QDSetPatternOrigin(PatternOrigin(tkwin, d)); CGContextFillRect(dc.context, bounds); END_DRAWING }
void PlatformCALayer::drawRepaintIndicator(CGContextRef context, PlatformCALayer* platformCALayer, int repaintCount, CGColorRef customBackgroundColor) { char text[16]; // that's a lot of repaints snprintf(text, sizeof(text), "%d", repaintCount); FloatRect indicatorBox = platformCALayer->bounds();\ indicatorBox.setLocation( { 1, 1 } ); indicatorBox.setSize(FloatSize(12 + 10 * strlen(text), 27)); CGContextStateSaver stateSaver(context); CGContextSetAlpha(context, 0.5f); CGContextBeginTransparencyLayerWithRect(context, indicatorBox, 0); if (customBackgroundColor) CGContextSetFillColorWithColor(context, customBackgroundColor); else CGContextSetRGBFillColor(context, 0, 0.5f, 0.25f, 1); if (platformCALayer->isOpaque()) CGContextFillRect(context, indicatorBox); else { Path boundsPath; boundsPath.moveTo(indicatorBox.maxXMinYCorner()); boundsPath.addLineTo(indicatorBox.maxXMaxYCorner()); boundsPath.addLineTo(indicatorBox.minXMaxYCorner()); const float cornerChunk = 8; boundsPath.addLineTo(FloatPoint(indicatorBox.x(), indicatorBox.y() + cornerChunk)); boundsPath.addLineTo(FloatPoint(indicatorBox.x() + cornerChunk, indicatorBox.y())); boundsPath.closeSubpath(); CGContextAddPath(context, boundsPath.platformPath()); CGContextFillPath(context); } if (platformCALayer->owner()->isUsingDisplayListDrawing(platformCALayer)) { CGContextSetRGBStrokeColor(context, 0, 0, 0, 0.65); CGContextSetLineWidth(context, 2); CGContextStrokeRect(context, indicatorBox); } if (platformCALayer->acceleratesDrawing()) CGContextSetRGBFillColor(context, 1, 0, 0, 1); else CGContextSetRGBFillColor(context, 1, 1, 1, 1); platformCALayer->drawTextAtPoint(context, indicatorBox.x() + 5, indicatorBox.y() + 22, CGSizeMake(1, -1), 22, text, strlen(text)); CGContextEndTransparencyLayer(context); }
void CanvasRenderingContext2D::fillRect(float x, float y, float width, float height) { if (!validateRectForCanvas(x, y, width, height)) return; GraphicsContext* c = drawingContext(); if (!c) return; FloatRect rect(x, y, width, height); willDraw(rect); // FIXME: Do this through platform-independent GraphicsContext API. #if PLATFORM(CG) if (state().m_fillStyle->canvasGradient()) { // Shading works on the entire clip region, so convert the rect to a clip. c->save(); CGContextClipToRect(c->platformContext(), rect); CGContextDrawShading(c->platformContext(), state().m_fillStyle->canvasGradient()->gradient().platformGradient()); c->restore(); } else { if (state().m_fillStyle->pattern()) applyFillPattern(); CGContextFillRect(c->platformContext(), rect); } #elif PLATFORM(QT) QPainter* p = static_cast<QPainter*>(c->platformContext()); if (state().m_fillStyle->canvasGradient()) { p->fillRect(rect, QBrush(*(state().m_fillStyle->canvasGradient()->gradient().platformGradient()))); } else { if (state().m_fillStyle->pattern()) applyFillPattern(); p->fillRect(rect, p->brush()); } #elif PLATFORM(CAIRO) && !PLATFORM(BAL) cairo_t* cr = c->platformContext(); cairo_save(cr); if (state().m_fillStyle->canvasGradient()) { cairo_set_source(cr, state().m_fillStyle->canvasGradient()->gradient().platformGradient()); } else { if (state().m_fillStyle->pattern()) applyFillPattern(); } cairo_rectangle(cr, x, y, width, height); cairo_fill(cr); cairo_restore(cr); #elif PLATFORM(BAL) //FIXME notImplemented(); #endif }
void GraphicsContext::fillRect(const FloatRect& rect, const Color& color) { if (paintingDisabled()) return; if (color.alpha()) { CGContextRef context = platformContext(); Color oldFillColor = fillColor(); if (oldFillColor != color) setCGFillColor(context, color); CGContextFillRect(context, rect); if (oldFillColor != color) setCGFillColor(context, oldFillColor); } }
void GraphicsContext::drawLineForText(const IntPoint& point, int width, bool printing) { if (paintingDisabled()) return; if (width <= 0) return; float x = point.x(); float y = point.y(); float lineLength = width; // Use a minimum thickness of 0.5 in user space. // See http://bugs.webkit.org/show_bug.cgi?id=4255 for details of why 0.5 is the right minimum thickness to use. float thickness = max(strokeThickness(), 0.5f); bool restoreAntialiasMode = false; if (!printing) { // On screen, use a minimum thickness of 1.0 in user space (later rounded to an integral number in device space). float adjustedThickness = max(thickness, 1.0f); // FIXME: This should be done a better way. // We try to round all parameters to integer boundaries in device space. If rounding pixels in device space // makes our thickness more than double, then there must be a shrinking-scale factor and rounding to pixels // in device space will make the underlines too thick. CGRect lineRect = roundToDevicePixels(FloatRect(x, y, lineLength, adjustedThickness)); if (lineRect.size.height < thickness * 2.0) { x = lineRect.origin.x; y = lineRect.origin.y; lineLength = lineRect.size.width; thickness = lineRect.size.height; if (shouldAntialias()) { CGContextSetShouldAntialias(platformContext(), false); restoreAntialiasMode = true; } } } if (fillColor() != strokeColor()) setCGFillColor(platformContext(), strokeColor(), strokeColorSpace()); CGContextFillRect(platformContext(), CGRectMake(x, y, lineLength, thickness)); if (fillColor() != strokeColor()) setCGFillColor(platformContext(), fillColor(), fillColorSpace()); if (restoreAntialiasMode) CGContextSetShouldAntialias(platformContext(), true); }
CGImageRef CreatePDFPageImage(CGPDFPageRef page, CGFloat scale, bool transparentBackground) { CGSize pageSize = PDFPageGetSize(page, kCGPDFCropBox); size_t width = scale * floorf(pageSize.width); size_t height = scale * floorf(pageSize.height); size_t bytesPerLine = width * 4; uint64_t size = (uint64_t)height * (uint64_t)bytesPerLine; if ((size == 0) || (size > SIZE_MAX)) return NULL; void *bitmapData = malloc(size); if (!bitmapData) return NULL; #if TARGET_OS_IPHONE CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); #else CGColorSpaceRef colorSpace = CGColorSpaceCreateWithName(&kCGColorSpaceSRGB ? kCGColorSpaceSRGB : kCGColorSpaceGenericRGB); #endif CGContextRef context = CGBitmapContextCreate(bitmapData, width, height, 8, bytesPerLine, colorSpace, kCGImageAlphaPremultipliedFirst); if (transparentBackground) { CGContextClearRect(context, CGRectMake(0, 0, width, height)); } else { CGContextSetRGBFillColor(context, 1, 1, 1, 1); // white CGContextFillRect(context, CGRectMake(0, 0, width, height)); } // CGPDFPageGetDrawingTransform unfortunately does not upscale, see http://lists.apple.com/archives/quartz-dev/2005/Mar/msg00112.html CGAffineTransform drawingTransform = PDFPageGetDrawingTransform(page, kCGPDFCropBox, scale); CGContextConcatCTM(context, drawingTransform); CGContextDrawPDFPage(context, page); CGImageRef pdfImage = CGBitmapContextCreateImage(context); CGContextRelease(context); CGColorSpaceRelease(colorSpace); free(bitmapData); return pdfImage; }
void GraphicsContext::fillRect(const FloatRect& rect, const Color& color, ColorSpace colorSpace) { if (paintingDisabled()) return; CGContextRef context = platformContext(); Color oldFillColor = fillColor(); ColorSpace oldColorSpace = fillColorSpace(); if (oldFillColor != color || oldColorSpace != colorSpace) setCGFillColor(context, color, colorSpace); CGContextFillRect(context, rect); if (oldFillColor != color || oldColorSpace != colorSpace) setCGFillColor(context, oldFillColor, oldColorSpace); }
bool GiCanvasIos::rawRect(const GiContext* ctx, float x, float y, float w, float h) { bool ret = false; if (m_draw->setPen(ctx)) { CGContextStrokeRect(m_draw->getContext(), CGRectMake(x, y, w, h)); ret = true; } if (m_draw->setBrush(ctx)) { CGContextFillRect(m_draw->getContext(), CGRectMake(x, y, w, h)); ret = true; } return ret; }