void drawRoundedRect(CGContextRef context, CGRect rrect) { // Drawing with a white stroke color CGContextSetRGBStrokeColor(context, 0.3, 0.3, 0.3, 1.0); CGContextSetRGBFillColor(context, 0.3, 0.3, 0.3, 1.0); // Add Rect to the current path, then stroke it // CGContextAddRect(context, CGRectMake(10.0, 190.0, 290.0, 73.0)); CGContextSetLineWidth(context, 1); CGFloat radius = 10.0; CGFloat minx = CGRectGetMinX(rrect), midx = CGRectGetMidX(rrect), maxx = CGRectGetMaxX(rrect); CGFloat miny = CGRectGetMinY(rrect), midy = CGRectGetMidY(rrect), maxy = CGRectGetMaxY(rrect); // Next, we will go around the rectangle in the order given by the figure below. // minx midx maxx // miny 2 3 4 // midy 1 9 5 // maxy 8 7 6 // Which gives us a coincident start and end point, which is incidental to this technique, but still doesn't // form a closed path, so we still need to close the path to connect the ends correctly. // Thus we start by moving to point 1, then adding arcs through each pair of points that follows. // You could use a similar tecgnique to create any shape with rounded corners. // Start at 1 CGContextMoveToPoint(context, minx, midy); // Add an arc through 2 to 3 CGContextAddArcToPoint(context, minx, miny, midx, miny, radius); // Add an arc through 4 to 5 CGContextAddArcToPoint(context, maxx, miny, maxx, midy, radius); // Add an arc through 6 to 7 CGContextAddArcToPoint(context, maxx, maxy, midx, maxy, radius); // Add an arc through 8 to 9 CGContextAddArcToPoint(context, minx, maxy, minx, midy, radius); // Close the path CGContextClosePath(context); // Fill & stroke the path CGContextDrawPath(context, kCGPathFillStroke); CGContextSetRGBStrokeColor(context, 0.0, 0.0, 0.0, 0.0); CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 1.0); CGContextMoveToPoint(context, minx, midy); // Add an arc through 2 to 3 CGContextAddArcToPoint(context, minx, miny, midx, miny, radius); // Add an arc through 4 to 5 CGContextAddArcToPoint(context, maxx, miny, maxx, midy, radius); // Add an arc through 6 to 7 CGContextAddArcToPoint(context, maxx, maxy, midx, maxy, radius); // Add an arc through 8 to 9 CGContextAddArcToPoint(context, minx, maxy, minx, midy, radius); // Close the path CGContextClosePath(context); // Fill & stroke the path CGContextDrawPath(context, kCGPathFillStroke); }
// This method is only used to draw the little circles used in lists. void GraphicsContext::drawEllipse(const IntRect& rect) { // FIXME: CG added CGContextAddEllipseinRect in Tiger, so we should be able to quite easily draw an ellipse. // This code can only handle circles, not ellipses. But khtml only // uses it for circles. ASSERT(rect.width() == rect.height()); if (paintingDisabled()) return; CGContextRef context = platformContext(); CGContextBeginPath(context); float r = (float)rect.width() / 2; CGContextAddArc(context, rect.x() + r, rect.y() + r, r, 0, 2*M_PI, true); CGContextClosePath(context); if (fillColor().alpha()) { if (strokeStyle() != NoStroke) // stroke and fill CGContextDrawPath(context, kCGPathFillStroke); else CGContextFillPath(context); } else if (strokeStyle() != NoStroke) CGContextStrokePath(context); }
void GraphicsContext::drawConvexPolygon(size_t npoints, const FloatPoint* points, bool shouldAntialias) { if (paintingDisabled()) return; if (npoints <= 1) return; CGContextRef context = platformContext(); CGContextSaveGState(context); CGContextSetShouldAntialias(context, shouldAntialias); CGContextBeginPath(context); CGContextMoveToPoint(context, points[0].x(), points[0].y()); for (size_t i = 1; i < npoints; i++) CGContextAddLineToPoint(context, points[i].x(), points[i].y()); CGContextClosePath(context); if (fillColor().alpha()) CGContextEOFillPath(context); if (strokeStyle() != NoStroke) CGContextStrokePath(context); CGContextRestoreGState(context); }
static void AddRoundedRectToPath(CGContextRef context, CGRect rect, float ovalWidth, float ovalHeight) { float fw, fh; if (ovalWidth == 0 || ovalHeight == 0) { CGContextAddRect(context, rect); return; } CGContextSaveGState(context); CGContextTranslateCTM (context, CGRectGetMinX(rect), CGRectGetMinY(rect)); CGContextScaleCTM (context, ovalWidth, ovalHeight); fw = CGRectGetWidth (rect) / ovalWidth; fh = CGRectGetHeight (rect) / ovalHeight; CGContextMoveToPoint(context, fw, fh / 2); CGContextAddArcToPoint(context, fw, fh, fw / 2, fh, 1); CGContextAddArcToPoint(context, 0, fh, 0, fh / 2, 1); CGContextAddArcToPoint(context, 0, 0, fw / 2, 0, 1); CGContextAddArcToPoint(context, fw, 0, fw, fh / 2, 1); CGContextClosePath(context); CGContextRestoreGState(context); }
void CFX_QuartzDeviceDriver::setPathToContext(const CFX_PathData* pathData) { FX_INT32 count = pathData->GetPointCount(); FX_PATHPOINT* points = pathData->GetPoints(); CGContextBeginPath(_context); for (FX_INT32 i = 0; i < count; i ++) { switch (points[i].m_Flag & FXPT_TYPE) { case FXPT_MOVETO: CGContextMoveToPoint(_context, points[i].m_PointX, points[i].m_PointY); break; case FXPT_LINETO: CGContextAddLineToPoint(_context, points[i].m_PointX, points[i].m_PointY); break; case FXPT_BEZIERTO: { CGContextAddCurveToPoint(_context, points[i].m_PointX, points[i].m_PointY, points[i + 1].m_PointX, points[i + 1].m_PointY, points[i + 2].m_PointX, points[i + 2].m_PointY); i += 2; } } if (points[i].m_Flag & FXPT_CLOSEFIGURE) { CGContextClosePath(_context); } } }
void drawRoundedRect(CGContextRef context, int x, int y){ struct CGRect cgRect; struct CGPoint cgPoint; cgRect.size.width = 640; cgRect.size.height = y+30; cgPoint.x = 0; cgPoint.y = 5; cgRect.origin = cgPoint; //printf("Drawing %f, %f, %f, %f", cgPoint.x, cgPoint.y, cgRect.size.width, cgRect.size.height); CGContextBeginPath(context); float ovalWidth = 10; float ovalHeight = 10; float fw, fh; // If the width or height of the corner oval is zero, then it reduces to a right angle, // so instead of a rounded rectangle we have an ordinary one. if (ovalWidth == 0 || ovalHeight == 0) { CGContextAddRect(context, cgRect); return; } // Save the context's state so that the translate and scale can be undone with a call // to CGContextRestoreGState. CGContextSaveGState(context); // Translate the origin of the contex to the lower left corner of the rectangle. CGContextTranslateCTM(context, CGRectGetMinX(cgRect), CGRectGetMinY(cgRect)); //Normalize the scale of the context so that the width and height of the arcs are 1.0 CGContextScaleCTM(context, ovalWidth, ovalHeight); // Calculate the width and height of the rectangle in the new coordinate system. fw = CGRectGetWidth(cgRect) / ovalWidth; fh = CGRectGetHeight(cgRect) / ovalHeight; // CGContextAddArcToPoint adds an arc of a circle to the context's path (creating the rounded // corners). It also adds a line from the path's last point to the begining of the arc, making // the sides of the rectangle. CGContextMoveToPoint(context, fw, fh/2); // Start at lower right corner CGContextAddArcToPoint(context, fw, fh, fw/2, fh, 1); // Top right corner CGContextAddArcToPoint(context, 0, fh, 0, fh/2, 1); // Top left corner CGContextAddArcToPoint(context, 0, 0, fw/2, 0, 1); // Lower left corner CGContextAddArcToPoint(context, fw, 0, fw, fh/2, 1); // Back to lower right // Close the path CGContextClosePath(context); CGContextSetRGBFillColor (context, 1, 1, 1, 0.7); CGContextFillPath(context); CGContextRestoreGState(context); }
static void addRoundedRectToPath(CGContextRef context, CGRect rect, float ovalWidth, float ovalHeight) { float fw, fh; // If either ovalWidth or ovalHeight is 0, draw a regular rectangle. if (ovalWidth == 0 || ovalHeight == 0) { CGContextAddRect(context, rect); }else{ CGContextSaveGState(context); // Translate to lower-left corner of rectangle. CGContextTranslateCTM(context, CGRectGetMinX(rect), CGRectGetMinY(rect)); // Scale by the oval width and height so that // each rounded corner is 0.5 units in radius. CGContextScaleCTM(context, ovalWidth, ovalHeight); // Unscale the rectangle width by the amount of the X scaling. fw = CGRectGetWidth(rect) / ovalWidth; // Unscale the rectangle height by the amount of the Y scaling. fh = CGRectGetHeight(rect) / ovalHeight; // Start at the right edge of the rect, at the midpoint in Y. CGContextMoveToPoint(context, fw, fh/2); // Segment 1 CGContextAddArcToPoint(context, fw, fh, fw/2, fh, 0.5); // Segment 2 CGContextAddArcToPoint(context, 0, fh, 0, fh/2, 0.5); // Segment 3 CGContextAddArcToPoint(context, 0, 0, fw/2, 0, 0.5); // Segment 4 CGContextAddArcToPoint(context, fw, 0, fw, fh/2, 0.5); // Closing the path adds the last segment. CGContextClosePath(context); CGContextRestoreGState(context); } }
//----------------------------------------------------------------------------- void CGDrawContext::drawEllipse (const CRect &rect, const CDrawStyle drawStyle) { CGContextRef context = beginCGContext (true, currentState.drawMode.integralMode ()); if (context) { CGPathDrawingMode m; switch (drawStyle) { case kDrawFilled : m = kCGPathFill; break; case kDrawFilledAndStroked : m = kCGPathFillStroke; break; default : m = kCGPathStroke; break; } applyLineStyle (context); if (rect.width () != rect.height ()) { CGContextSaveGState (context); CGContextBeginPath (context); CGRect cgRect = CGRectMake (rect.left, rect.top, rect.width (), rect.height ()); CGPoint center = CGPointMake (CGRectGetMidX (cgRect), CGRectGetMidY (cgRect)); CGFloat a = CGRectGetWidth (cgRect) / 2.; CGFloat b = CGRectGetHeight (cgRect) / 2.; CGContextTranslateCTM (context, center.x, center.y); CGContextScaleCTM (context, a, b); CGContextMoveToPoint (context, 1, 0); CGContextAddArc (context, 0, 0, 1, radians (0), radians (360), 0); CGContextClosePath (context); CGContextRestoreGState (context); CGContextDrawPath (context, m); } else { CGFloat radius = rect.width () * 0.5; CGContextBeginPath (context); CGContextAddArc (context, rect.left + radius, rect.top + radius, radius, radians (0), radians (360), 0); CGContextClosePath (context); CGContextDrawPath (context, m); } releaseCGContext (context); } }
void JBGFillTriangle(CGContextRef ctx, CGPoint point_a, CGPoint point_b, CGPoint point_c, CGColorRef color) { CGContextBeginPath(ctx); CGPoint points[] = {point_a, point_b, point_c}; CGContextAddLines(ctx, points, sizeof(points) / sizeof(points[0])); CGContextClosePath(ctx); CGContextSetFillColorWithColor(ctx, color); CGContextFillPath(ctx); }
void MacDock::overlay(const QString& text) { if (text.isEmpty()) { overlayed = false; RestoreApplicationDockTileImage(); return; } // Create the context CGContextRef context = BeginCGContextForApplicationDockTile(); if (!overlayed) { overlayed = true; // Add some subtle drop down shadow // FIXME: Disabled because 10.2 doesn't support it //CGSize s = { 2.0, -4.0 }; //CGContextSetShadow(context,s,5.0); } // Draw a circle CGContextBeginPath(context); CGContextAddArc(context, 95.0, 95.0, 25.0, 0.0, 2 * M_PI, true); CGContextClosePath(context); CGContextSetRGBFillColor(context, 1, 0.0, 0.0, 1); CGContextFillPath(context); // Set the clipping path to the same circle CGContextBeginPath(context); CGContextAddArc(context, 95.0, 95.0, 25.0, 0.0, 2 * M_PI, true); CGContextClip(context); // Remove drop shadow // FIXME: Disabled because 10.2 doesn't support it //CGSize s = { 0.0, -0.0 }; //CGContextSetShadowWithColor(context, s, 0, NULL); // Select the appropriate font CGContextSelectFont(context,DOCK_FONT_NAME, DOCK_FONT_SIZE, kCGEncodingMacRoman); CGContextSetRGBFillColor(context, 1, 1, 1, 1); // Draw the text invisible CGPoint begin = CGContextGetTextPosition(context); CGContextSetTextDrawingMode(context, kCGTextInvisible); CGContextShowTextAtPoint(context, begin.x, begin.y, text.toStdString().c_str(), text.length()); CGPoint end = CGContextGetTextPosition(context); // Draw the text CGContextSetTextDrawingMode(context, kCGTextFill); CGContextShowTextAtPoint(context, 95 - (end.x - begin.x)/2, 95 - 8, text.toStdString().c_str(), text.length()); // Cleanup CGContextFlush(context); EndCGContextForApplicationDockTile(context); }
static void gdk_quartz_draw_polygon (GdkDrawable *drawable, GdkGC *gc, gboolean filled, GdkPoint *points, gint npoints) { CGContextRef context = gdk_quartz_drawable_get_context (drawable, FALSE); int i; if (!context) return; if (!_gdk_quartz_gc_update_cg_context (gc, drawable, context, filled ? GDK_QUARTZ_CONTEXT_FILL : GDK_QUARTZ_CONTEXT_STROKE)) { gdk_quartz_drawable_release_context (drawable, context); return; } if (filled) { CGContextMoveToPoint (context, points[0].x, points[0].y); for (i = 1; i < npoints; i++) CGContextAddLineToPoint (context, points[i].x, points[i].y); CGContextClosePath (context); CGContextFillPath (context); } else { CGContextMoveToPoint (context, points[0].x + 0.5, points[0].y + 0.5); for (i = 1; i < npoints; i++) CGContextAddLineToPoint (context, points[i].x + 0.5, points[i].y + 0.5); CGContextClosePath (context); CGContextStrokePath (context); } gdk_quartz_drawable_release_context (drawable, context); }
static void quartzgen_polygon(GVJ_t *job, pointf *A, int n, int filled) { /* convert polygon into the current path */ CGContextRef context = (CGContextRef)job->context; CGContextMoveToPoint(context, A[0].x, A[0].y); int i; for (i = 1; i < n; ++i) CGContextAddLineToPoint(context, A[i].x, A[i].y); CGContextClosePath(context); /* draw the ellipse */ quartzgen_path(job, filled); }
void doEgg(CGContextRef context) { CGPoint p0 = {0., 0.}, p1 = {0., 200.}; CGPoint c1 = {140., 5.}, c2 = {80., 198.}; CGContextTranslateCTM(context, 100., 5.); CGContextBeginPath(context); CGContextMoveToPoint(context, p0.x, p0.y); // Create the BŽzier path segment for the right side of the egg. CGContextAddCurveToPoint(context, c1.x, c1.y, c2.x, c2.y, p1.x, p1.y); // Create the BŽzier path segment for the left side of the egg. CGContextAddCurveToPoint(context, -c2.x, c2.y, -c1.x, c1.y, p0.x, p0.y); CGContextClosePath(context); CGContextSetLineWidth(context, 2); CGContextDrawPath(context, kCGPathStroke); }
void addRoundedRectToPath(CGContextRef context, CGRect rect, CGFloat radius) { CGFloat minX = CGRectGetMinX(rect); CGFloat minY = CGRectGetMinY(rect); CGFloat maxX = CGRectGetMaxX(rect); CGFloat maxY = CGRectGetMaxY(rect); CGFloat midX = CGRectGetMidX(rect); CGFloat midY = CGRectGetMidY(rect); CGContextBeginPath(context); CGContextMoveToPoint(context, maxX, midY); CGContextAddArcToPoint(context, maxX, maxY, midX, maxY, radius); CGContextAddArcToPoint(context, minX, maxY, minX, midY, radius); CGContextAddArcToPoint(context, minX, minY, midX, minY, radius); CGContextAddArcToPoint(context, maxX, minY, maxX, midY, radius); CGContextClosePath(context); }
bool GiCanvasIos::rawPath(const GiContext* ctx, int count, const Point2d* pxs, const UInt8* types) { CGContextBeginPath(m_draw->getContext()); for (int i = 0; i < count; i++) { switch (types[i] & ~kGiCloseFigure) { case kGiMoveTo: CGContextMoveToPoint(m_draw->getContext(), pxs[i].x, pxs[i].y); break; case kGiLineTo: CGContextAddLineToPoint(m_draw->getContext(), pxs[i].x, pxs[i].y); break; case kGiBeziersTo: if (i + 2 >= count) return false; CGContextAddCurveToPoint(m_draw->getContext(), pxs[i+0].x, pxs[i+0].y, pxs[i+1].x, pxs[i+1].y, pxs[i+2].x, pxs[i+2].y); i += 2; break; default: return false; } if (types[i] & kGiCloseFigure) CGContextClosePath(m_draw->getContext()); } bool usepen = m_draw->setPen(ctx); bool usebrush = m_draw->setBrush(ctx); bool ret = count > 1 && (usepen || usebrush); if (ret) { CGContextDrawPath(m_draw->getContext(), usepen && usebrush ? kCGPathFillStroke : usepen ? kCGPathStroke : kCGPathFill); } return ret; }
void paintArc(CGContextRef context, CGRect r, int startAngle, int arcAngle) { float start, end; // Signal the start of a path CGContextBeginPath(context); // Set the start of the path to the arcs focal point CGContextMoveToPoint(context, r.origin.x + r.size.width/2, r.origin.y + r.size.height/2); // Add to the path the arc of the oval that fits inside the rectangle. pathForArc(context,r,startAngle,arcAngle); // Complete the path closing the arc at the focal point CGContextClosePath(context); // Fill the path CGContextFillPath(context); }
bool GiCanvasIos::rawPolygon(const GiContext* ctx, const Point2d* pxs, int count) { bool usepen = m_draw->setPen(ctx); bool usebrush = m_draw->setBrush(ctx); bool ret = count > 1 && (usepen || usebrush); if (ret) { CGContextMoveToPoint(m_draw->getContext(), pxs[0].x, pxs[0].y); for (int i = 1; i < count; i++) { CGContextAddLineToPoint(m_draw->getContext(), pxs[i].x, pxs[i].y); } CGContextClosePath(m_draw->getContext()); CGContextDrawPath(m_draw->getContext(), usepen && usebrush ? kCGPathFillStroke : usepen ? kCGPathStroke : kCGPathFill); } return ret; }
static void addRoundedRectToPath(CGContextRef context, CGRect rect, float ovalWidth, float ovalHeight) { float fw, fh; // If the width or height of the corner oval is zero, then it reduces to a right angle, // so instead of a rounded rectangle we have an ordinary one. if (ovalWidth == 0 || ovalHeight == 0) { CGContextAddRect(context, rect); return; } // Save the context's state so that the translate and scale can be undone with a call // to CGContextRestoreGState. CGContextSaveGState(context); // Translate the origin of the contex to the lower left corner of the rectangle. CGContextTranslateCTM(context, CGRectGetMinX(rect), CGRectGetMinY(rect)); //Normalize the scale of the context so that the width and height of the arcs are 1.0 CGContextScaleCTM(context, ovalWidth, ovalHeight); // Calculate the width and height of the rectangle in the new coordinate system. fw = CGRectGetWidth(rect) / ovalWidth; fh = CGRectGetHeight(rect) / ovalHeight; // CGContextAddArcToPoint adds an arc of a circle to the context's path (creating the rounded // corners). It also adds a line from the path's last point to the begining of the arc, making // the sides of the rectangle. CGContextMoveToPoint(context, fw, fh/2); // Start at lower right corner CGContextAddArcToPoint(context, fw, fh, fw/2, fh, 1); // Top right corner CGContextAddArcToPoint(context, 0, fh, 0, fh/2, 1); // Top left corner CGContextAddArcToPoint(context, 0, 0, fw/2, 0, 1); // Lower left corner CGContextAddArcToPoint(context, fw, 0, fw, fh/2, 1); // Back to lower right // Close the path CGContextClosePath(context); // Restore the context's state. This removes the translation and scaling // but leaves the path, since the path is not part of the graphics state. CGContextRestoreGState(context); }
static void createEllipsePath(CGContextRef context, CGPoint center, CGSize ellipseSize) { CGContextSaveGState(context); // Translate the coordinate origin to the center point. CGContextTranslateCTM(context, center.x, center.y); // Scale the coordinate system to half the width and height // of the ellipse. CGContextScaleCTM(context, ellipseSize.width/2, ellipseSize.height/2); CGContextBeginPath(context); // Add a circular arc to the path, centered at the origin and // with a radius of 1.0. This radius, together with the // scaling above for the width and height, produces an ellipse // of the correct size. CGContextAddArc(context, 0.0, 0.0, 1.0, 0.0, DEGREES_TO_RADIANS(360.0), 0.0); // Close the path so that this path is suitable for stroking, // should that be desired. CGContextClosePath(context); CGContextRestoreGState(context); }
/***************************************************** * * Internal_HICustomViewHandler(inHandlerCallRef, inEvent, inUserData) * * Purpose: Event handler that implements our HICustomView custom view * * Inputs: inHandlerCallRef - reference to the current handler call chain * inEvent - the event * inUserData - app-specified data you passed in the call to InstallEventHandler * * Returns: OSStatus - error code (0 == no error) */ static pascal OSStatus Internal_HICustomViewHandler(EventHandlerCallRef inHandlerCallRef, EventRef inEvent, void * inUserData) { OSStatus status = eventNotHandledErr; HICustomViewData * myData = (HICustomViewData *)inUserData; switch (GetEventClass(inEvent)) { case kEventClassHIObject: switch (GetEventKind(inEvent)) { case kEventHIObjectConstruct: { // allocate some instance data myData = (HICustomViewData *) calloc(1, sizeof(HICustomViewData)); require_action(myData != NULL, ConstructExit, status = memFullErr); // get our superclass instance HIViewRef epView; status = GetEventParameter(inEvent, kEventParamHIObjectInstance, typeHIObjectRef, NULL, sizeof(epView), NULL, &epView); require_noerr(status, ConstructExit); // remember our superclass in our instance data myData->view = epView; // store our instance data into the event status = SetEventParameter(inEvent, kEventParamHIObjectInstance, typeVoidPtr, sizeof(myData), &myData); require_noerr(status, ConstructExit); ConstructExit: break; } #pragma mark * kEventHIObjectInitialize case kEventHIObjectInitialize: { // always begin kEventHIObjectInitialize by calling through to the previous handler status = CallNextEventHandler(inHandlerCallRef, inEvent); require_noerr(status, InitializeExit); // if that succeeded, do our own initialization // in this sample code, there is nothing to do InitializeExit: break; } case kEventHIObjectDestruct: { // freeing our storage if (myData != NULL) free(myData); status = noErr; break; } default: break; } break; case kEventClassControl: switch (GetEventKind(inEvent)) { #pragma mark * kEventControlDraw case kEventControlDraw: { CGContextRef context; status = GetEventParameter(inEvent, kEventParamCGContextRef, typeCGContextRef, NULL, sizeof(context), NULL, &context); require_noerr(status, ControlDrawExit); HIRect bounds, viewBounds; HIViewGetBounds(myData->view, &viewBounds); // setting our colors according to state: IsControlEnabled, IsControlActive, IsControlHilited if (!IsControlEnabled(myData->view)) { CGContextSetRGBFillColor(context, 0.3, 0.3, 0.3, 0.8); CGContextSetRGBStrokeColor(context, 0.5, 0.5, 0.5, 0.8); } else if (!IsControlActive(myData->view)) { CGContextSetRGBFillColor(context, 0.7, 0.7, 0.7, 0.8); CGContextSetRGBStrokeColor(context, 0.8, 0.8, 0.8, 0.8); } else if (!IsControlHilited(myData->view)) { CGContextSetRGBFillColor(context, 1, 0, 0, 0.8); CGContextSetRGBStrokeColor(context, 0, 0, 1, 0.8); } else { CGContextSetRGBFillColor(context, 0.7, 0, 0, 0.8); CGContextSetRGBStrokeColor(context, 0, 0, 0.7, 0.8); } // using a line thickness of 3 CGContextSetLineWidth(context, 3); bounds = CGRectInset(viewBounds, 3, 3); float minDim = (bounds.size.height < bounds.size.width) ? bounds.size.height / 2 : bounds.size.width / 2; float cx = bounds.origin.x + minDim, cy = bounds.origin.y + minDim; UInt32 i, n = GetControl32BitValue(myData->view); // having some fun with geometric shapes based on the value of the custom view CGContextBeginPath(context); switch (n) { case 0: CGContextAddArc(context, cx, cy, minDim, 0, 2 * pi, true); break; case 1: CGContextAddEllipseInRect(context, CGRectInset(bounds, bounds.size.width * 0.4, 0)); break; default: { float deltangle = pi / n, angle = 0, r = minDim / 2; CGContextMoveToPoint(context, cx + minDim, cy); for (i = 0; i < n; i++) { angle += deltangle; CGContextAddLineToPoint(context, cx + r * cos(angle), cy + r * sin(angle)); angle += deltangle; CGContextAddLineToPoint(context, cx + minDim * cos(angle), cy + minDim * sin(angle)); } CGContextAddLineToPoint(context, cx + minDim, cy); } } CGContextClosePath(context); CGContextDrawPath(context, kCGPathFillStroke); status = noErr; ControlDrawExit: break; } #pragma mark * kEventControl___Changed case kEventControlValueFieldChanged: case kEventControlHiliteChanged: { // just asking for a refresh HIViewSetNeedsDisplay(myData->view, true); break; } default: break; } break; default: break; } return status; } // Internal_HICustomViewHandler
static void gdk_quartz_draw_arc (GdkDrawable *drawable, GdkGC *gc, gboolean filled, gint x, gint y, gint width, gint height, gint angle1, gint angle2) { CGContextRef context = gdk_quartz_drawable_get_context (drawable, FALSE); float start_angle, end_angle; gboolean clockwise = FALSE; if (!context) return; if (!_gdk_quartz_gc_update_cg_context (gc, drawable, context, filled ? GDK_QUARTZ_CONTEXT_FILL : GDK_QUARTZ_CONTEXT_STROKE)) { gdk_quartz_drawable_release_context (drawable, context); return; } start_angle = angle1 * 2.0 * G_PI / 360.0 / 64.0; end_angle = start_angle + angle2 * 2.0 * G_PI / 360.0 / 64.0; /* angle2 is relative to angle1 and can be negative, which switches * the drawing direction */ if (angle2 < 0) clockwise = TRUE; /* below, flip the coordinate system back to its original y-diretion * so the angles passed to CGContextAddArc() are interpreted as * expected * * FIXME: the implementation below works only for perfect circles * (width == height). Any other aspect ratio either scales the * line width unevenly or scales away the path entirely for very * small line widths (esp. for line_width == 0, which is a hair * line on X11 but must be approximated with the thinnest possible * line on quartz). */ if (filled) { CGContextTranslateCTM (context, x + width / 2.0, y + height / 2.0); CGContextScaleCTM (context, 1.0, - (double)height / (double)width); CGContextMoveToPoint (context, 0, 0); CGContextAddArc (context, 0, 0, width / 2.0, start_angle, end_angle, clockwise); CGContextClosePath (context); CGContextFillPath (context); } else { CGContextTranslateCTM (context, x + width / 2.0 + 0.5, y + height / 2.0 + 0.5); CGContextScaleCTM (context, 1.0, - (double)height / (double)width); CGContextAddArc (context, 0, 0, width / 2.0, start_angle, end_angle, clockwise); CGContextStrokePath (context); } gdk_quartz_drawable_release_context (drawable, context); }
bool GiCanvasIos::rawClosePath() { CGContextClosePath(m_draw->getContext()); return true; }
void structGraphicsScreen :: v_polyline (long numberOfPoints, double *xyDC, bool close) { #if cairo if (duringXor) { #if ALLOW_GDK_DRAWING gdkPrepareLine (this); for (long i = 0; i < numberOfPoints - 1; i ++) { gdk_draw_line (our d_window, our d_gdkGraphicsContext, xyDC [i + i], xyDC [i + i + 1], xyDC [i + i + 2], xyDC [i + i + 3]); } gdkRevertLine (this); gdk_flush (); #endif } else { if (our d_cairoGraphicsContext == NULL) return; cairoPrepareLine (this); // cairo_new_path (d_cairoGraphicsContext); // move_to() automatically creates a new path cairo_move_to (our d_cairoGraphicsContext, xyDC [0], xyDC [1]); for (long i = 1; i < numberOfPoints; i ++) { cairo_line_to (our d_cairoGraphicsContext, xyDC [i + i], xyDC [i + i + 1]); } if (close) cairo_close_path (our d_cairoGraphicsContext); cairo_stroke (our d_cairoGraphicsContext); cairoRevertLine (this); } #elif win winPrepareLine (this); POINT *points = Melder_malloc (POINT, numberOfPoints + close); if (points) { for (long i = 0; i < numberOfPoints; i ++) { points [i]. x = *xyDC, xyDC ++; points [i]. y = *xyDC, xyDC ++; } if (close) points [numberOfPoints] = points [0]; Polyline (our d_gdiGraphicsContext, points, numberOfPoints + close); if (our d_fatNonSolid) { for (long i = 0; i < numberOfPoints; i ++) points [i]. x -= 1; if (close) points [numberOfPoints] = points [0]; Polyline (our d_gdiGraphicsContext, points, numberOfPoints + close); for (long i = 0; i < numberOfPoints; i ++) { points [i]. x += 1; points [i]. y -= 1; } if (close) points [numberOfPoints] = points [0]; Polyline (our d_gdiGraphicsContext, points, numberOfPoints + close); } } DEFAULT #elif mac GraphicsQuartz_initDraw (this); quartzPrepareLine (this); CGContextBeginPath (our d_macGraphicsContext); CGContextMoveToPoint (our d_macGraphicsContext, xyDC [0], xyDC [1]); // starts a new subpath for (long i = 1; i < numberOfPoints; i ++) { CGContextAddLineToPoint (our d_macGraphicsContext, xyDC [i + i], xyDC [i + i + 1]); } if (close) CGContextClosePath (our d_macGraphicsContext); // closes only the subpath CGContextStrokePath (our d_macGraphicsContext); quartzRevertLine (this); GraphicsQuartz_exitDraw (this); #endif }