//----------------------------------------------------------------------------- void CGDrawContext::lineTo (const CPoint& point) { CGContextRef context = beginCGContext (true, currentState.drawMode.integralMode ()); if (context) { applyLineStyle (context); if ((((int32_t)currentState.frameWidth) % 2)) CGContextTranslateCTM (context, 0.5f, -0.5f); CGContextBeginPath (context); if (currentState.drawMode.integralMode ()) { CGContextMoveToPoint (context, round (currentState.penLoc.h), round (currentState.penLoc.v)); CGContextAddLineToPoint (context, round (point.h), round (point.v)); } else { CGContextMoveToPoint (context, currentState.penLoc.h, currentState.penLoc.v); CGContextAddLineToPoint (context, point.h, point.v); } CGContextDrawPath (context, kCGPathStroke); releaseCGContext (context); } currentState.penLoc = point; }
void structGraphicsScreen :: v_fillArea (long numberOfPoints, double *xyDC) { #if cairo if (our d_cairoGraphicsContext == NULL) return; // cairo_new_path (our 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]); cairo_close_path (our d_cairoGraphicsContext); cairo_fill (our d_cairoGraphicsContext); #elif win MY_BRUSH BeginPath (our d_gdiGraphicsContext); MoveToEx (our d_gdiGraphicsContext, xyDC [0], xyDC [1], NULL); for (long i = 1; i < numberOfPoints; i ++) LineTo (our d_gdiGraphicsContext, xyDC [i + i], xyDC [i + i + 1]); EndPath (our d_gdiGraphicsContext); FillPath (our d_gdiGraphicsContext); DEFAULT #elif mac GraphicsQuartz_initDraw (this); quartzPrepareFill (this); CGContextBeginPath (our d_macGraphicsContext); CGContextMoveToPoint (our d_macGraphicsContext, xyDC [0], xyDC [1]); for (long i = 1; i < numberOfPoints; i ++) { CGContextAddLineToPoint (our d_macGraphicsContext, xyDC [i + i], xyDC [i + i + 1]); } CGContextFillPath (our d_macGraphicsContext); GraphicsQuartz_exitDraw (this); #endif }
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); } } }
FX_BOOL CFX_QuartzDeviceDriver::DrawCosmeticLine(FX_FLOAT x1, FX_FLOAT y1, FX_FLOAT x2, FX_FLOAT y2, FX_DWORD argb, int alphaFlag , void* iccTransform , int blend_type ) { CGBlendMode mode = GetCGBlendMode(blend_type); if (mode != kCGBlendModeNormal) { CGContextSetBlendMode(_context, mode); } CGPoint pt = CGPointApplyAffineTransform(CGPointMake(x1, y1), _foxitDevice2User); x1 = pt.x; y1 = pt.y; pt = CGPointApplyAffineTransform(CGPointMake(x2, y2), _foxitDevice2User); x2 = pt.x; y2 = pt.y; FX_INT32 a, r, g, b; ArgbDecode(argb, a, r, g, b); CGContextSetRGBStrokeColor(_context, r / 255.f, g / 255.f, b / 255.f, a / 255.f); CGContextMoveToPoint(_context, x1, y1); CGContextAddLineToPoint(_context, x2, y2); CGContextStrokePath(_context); if (mode != kCGBlendModeNormal) { CGContextSetBlendMode(_context, kCGBlendModeNormal); } return TRUE; }
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); }
//----------------------------------------------------------------------------- void CGDrawContext::drawLine (const LinePair& line) { CGContextRef context = beginCGContext (true, getDrawMode ().integralMode ()); if (context) { applyLineStyle (context); CGContextBeginPath (context); CGPoint first = CGPointFromCPoint (line.first); CGPoint second = CGPointFromCPoint (line.second); if (getDrawMode ().integralMode ()) { first = pixelAlligned (first); second = pixelAlligned (second); int32_t frameWidth = static_cast<int32_t> (currentState.frameWidth); if (frameWidth % 2) CGContextTranslateCTM (context, 0.5, 0.5); } CGContextMoveToPoint (context, first.x, first.y); CGContextAddLineToPoint (context, second.x, second.y); CGContextDrawPath (context, kCGPathStroke); releaseCGContext (context); } }
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); }
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_polyline(GVJ_t *job, pointf *A, int n) { /* convert polyline 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); /* draw the ellipse */ quartzgen_path(job, FALSE); }
bool GiCanvasIos::rawLine(const GiContext* ctx, float x1, float y1, float x2, float y2) { bool ret = m_draw->setPen(ctx); if (ret) { CGContextMoveToPoint(m_draw->getContext(), x1, y1); CGContextAddLineToPoint(m_draw->getContext(), x2, y2); CGContextStrokePath(m_draw->getContext()); } return ret; }
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); }
static void drawNeedle(CGContextRef gc, int w, int h, float angle) { float dx, dy, cx, cy, radius, needle; cx = CENTERX * w; cy = CENTERY * h; dx = -cos(angle); dy = -sin(angle); radius = 0.5 * (w > h ? w : h); needle = radius * 0.85; CGContextMoveToPoint(gc, cx + needle*dx - 0.5*dy, cy + needle*dy + 0.5*dx); CGContextAddLineToPoint(gc, cx + needle*dx + 0.5*dy, cy + needle*dy - 0.5*dx); CGContextAddLineToPoint(gc, cx - NEEDLE_THICKNESS*dx + 0.5*NEEDLE_THICKNESS*dy, cy - NEEDLE_THICKNESS*dy - 0.5*NEEDLE_THICKNESS*dx); CGContextAddArc(gc, cx - NEEDLE_THICKNESS*dx, cy - NEEDLE_THICKNESS*dy, 0.5*NEEDLE_THICKNESS, angle - 0.5*M_PI, angle + 0.5*M_PI, false); CGContextAddLineToPoint(gc, cx - NEEDLE_THICKNESS*dx - 0.5*NEEDLE_THICKNESS*dy, cy - NEEDLE_THICKNESS*dy + 0.5*NEEDLE_THICKNESS*dx); CGContextFillPath(gc); }
bool GiCanvasIos::rawLines(const GiContext* ctx, const Point2d* pxs, int count) { bool ret = m_draw->setPen(ctx) && count > 1; 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); } CGContextStrokePath(m_draw->getContext()); } return ret; }
//----------------------------------------------------------------------------------- // Called for each CGPathElement when scanning the CGPath in CGPathApply() static void MyCGPathApplier(void* info, const CGPathElement* element) { CGContextRef ctx = (CGContextRef)info; switch (element->type) { case kCGPathElementMoveToPoint: CGContextMoveToPoint(ctx, element->points[0].x, element->points[0].y); break; case kCGPathElementAddLineToPoint: CGContextAddLineToPoint(ctx, element->points[0].x, element->points[0].y); break; default: // we know our path only contains line segments break; } }
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 MacVegaPrinterListener::DrawLine(const OpPoint& from, const OpPoint& to, UINT32 width) { if(width != 1) { CGContextSetLineWidth(m_ctx, width); } CGContextBeginPath(m_ctx); CGContextMoveToPoint(m_ctx, from.x, m_winHeight - from.y); CGContextAddLineToPoint(m_ctx, to.x, m_winHeight - to.y); CGContextStrokePath(m_ctx); if(width != 1) { CGContextSetLineWidth(m_ctx, 1); } }
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; }
//----------------------------------------------------------------------------- void CGDrawContext::drawPolygon (const CPoint* pPoints, int32_t numberOfPoints, 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); CGContextBeginPath (context); CGContextMoveToPoint (context, pPoints[0].h, pPoints[0].v); for (int32_t i = 1; i < numberOfPoints; i++) CGContextAddLineToPoint (context, pPoints[i].h, pPoints[i].v); CGContextDrawPath (context, m); releaseCGContext (context); } }
//----------------------------------------------------------------------------- void CGDrawContext::drawArc (const CRect &rect, const float _startAngle, const float _endAngle, const CDrawStyle drawStyle) // in degree { 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); CGContextBeginPath (context); addOvalToPath (context, CPoint (rect.left + rect.width () / 2, rect.top + rect.height () / 2), rect.width () / 2, rect.height () / 2, -_startAngle, -_endAngle); if (drawStyle == kDrawFilled || kDrawFilledAndStroked) CGContextAddLineToPoint (context, rect.left + rect.width () / 2, rect.top + rect.height () / 2); CGContextDrawPath (context, m); releaseCGContext (context); } }
//----------------------------------------------------------------------------- void CGDrawContext::drawPolygon (const PointList& polygonPointList, const CDrawStyle drawStyle) { if (polygonPointList.size () == 0) return; CGContextRef context = beginCGContext (true, getDrawMode ().integralMode ()); if (context) { CGPathDrawingMode m; switch (drawStyle) { case kDrawFilled : m = kCGPathFill; break; case kDrawFilledAndStroked : m = kCGPathFillStroke; break; default : m = kCGPathStroke; break; } applyLineStyle (context); CGContextBeginPath (context); CGPoint p = CGPointFromCPoint(polygonPointList[0]); if (getDrawMode ().integralMode ()) p = pixelAlligned (p); CGContextMoveToPoint (context, p.x, p.y); for (uint32_t i = 1; i < polygonPointList.size (); i++) { p = CGPointFromCPoint (polygonPointList[i]); if (getDrawMode ().integralMode ()) p = pixelAlligned (p); CGContextAddLineToPoint (context, p.x, p.y); } CGContextDrawPath (context, m); releaseCGContext (context); } }
// Handles a line drawing operation for cubic (Type 1 / Postscript) outlines // OSStatus MyCubicLineToProc(const Float32Point *pt, void *callBackDataPtr) { // Adjust the point according to the glyph origin float x = ((MyCurveCallbackData *)callBackDataPtr)->origin.x + pt->x; float y = ((MyCurveCallbackData *)callBackDataPtr)->origin.y + pt->y; // Beginning of degenerate filter if ( ! (gFilterDegenerates && (x == ((MyCurveCallbackData *)callBackDataPtr)->current.x) && (y == ((MyCurveCallbackData *)callBackDataPtr)->current.y)) ) { // Draw a line to the point y = ((MyCurveCallbackData *)callBackDataPtr)->windowHeight - y; CGContextAddLineToPoint(((MyCurveCallbackData *)callBackDataPtr)->context, x, y); } // End of degenerate filter // Keep track of the current pen position (used to filter degenerate cases) ((MyCurveCallbackData *)callBackDataPtr)->current.x = x; ((MyCurveCallbackData *)callBackDataPtr)->current.y = y; // Update counters and return if (gOutputNumSegments) segmentCount++; if (gOutputNumOps) lineToCount++; return noErr; }
/***************************************************** * * 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
// This is only used to draw borders. void GraphicsContext::drawLine(const IntPoint& point1, const IntPoint& point2) { if (paintingDisabled()) return; if (strokeStyle() == NoStroke) return; float width = strokeThickness(); FloatPoint p1 = point1; FloatPoint p2 = point2; bool isVerticalLine = (p1.x() == p2.x()); // For odd widths, we add in 0.5 to the appropriate x/y so that the float arithmetic // works out. For example, with a border width of 3, KHTML will pass us (y1+y2)/2, e.g., // (50+53)/2 = 103/2 = 51 when we want 51.5. It is always true that an even width gave // us a perfect position, but an odd width gave us a position that is off by exactly 0.5. if (strokeStyle() == DottedStroke || strokeStyle() == DashedStroke) { if (isVerticalLine) { p1.move(0, width); p2.move(0, -width); } else { p1.move(width, 0); p2.move(-width, 0); } } if (((int)width) % 2) { if (isVerticalLine) { // We're a vertical line. Adjust our x. p1.move(0.5f, 0.0f); p2.move(0.5f, 0.0f); } else { // We're a horizontal line. Adjust our y. p1.move(0.0f, 0.5f); p2.move(0.0f, 0.5f); } } int patWidth = 0; switch (strokeStyle()) { case NoStroke: case SolidStroke: break; case DottedStroke: patWidth = (int)width; break; case DashedStroke: patWidth = 3 * (int)width; break; } CGContextRef context = platformContext(); if (shouldAntialias()) CGContextSetShouldAntialias(context, false); if (patWidth) { CGContextSaveGState(context); // Do a rect fill of our endpoints. This ensures we always have the // appearance of being a border. We then draw the actual dotted/dashed line. setCGFillColor(context, strokeColor(), strokeColorSpace()); // The save/restore make it safe to mutate the fill color here without setting it back to the old color. if (isVerticalLine) { CGContextFillRect(context, FloatRect(p1.x() - width / 2, p1.y() - width, width, width)); CGContextFillRect(context, FloatRect(p2.x() - width / 2, p2.y(), width, width)); } else { CGContextFillRect(context, FloatRect(p1.x() - width, p1.y() - width / 2, width, width)); CGContextFillRect(context, FloatRect(p2.x(), p2.y() - width / 2, width, width)); } // Example: 80 pixels with a width of 30 pixels. // Remainder is 20. The maximum pixels of line we could paint // will be 50 pixels. int distance = (isVerticalLine ? (point2.y() - point1.y()) : (point2.x() - point1.x())) - 2*(int)width; int remainder = distance % patWidth; int coverage = distance - remainder; int numSegments = coverage / patWidth; float patternOffset = 0.0f; // Special case 1px dotted borders for speed. if (patWidth == 1) patternOffset = 1.0f; else { bool evenNumberOfSegments = !(numSegments % 2); if (remainder) evenNumberOfSegments = !evenNumberOfSegments; if (evenNumberOfSegments) { if (remainder) { patternOffset += patWidth - remainder; patternOffset += remainder / 2; } else patternOffset = patWidth / 2; } else { if (remainder) patternOffset = (patWidth - remainder)/2; } } const CGFloat dottedLine[2] = { patWidth, patWidth }; CGContextSetLineDash(context, patternOffset, dottedLine, 2); } CGContextBeginPath(context); CGContextMoveToPoint(context, p1.x(), p1.y()); CGContextAddLineToPoint(context, p2.x(), p2.y()); CGContextStrokePath(context); if (patWidth) CGContextRestoreGState(context); if (shouldAntialias()) CGContextSetShouldAntialias(context, true); }
void QuartzWindow::draw_line(int x1, int y1, int x2, int y2) { CGContextBeginPath(myContext); CGContextMoveToPoint(myContext, x1, y1); CGContextAddLineToPoint(myContext, x2, y2); CGContextStrokePath(myContext); }
static void MusicBoxDrawIndicator(HIViewRef view, CGContextRef mboxctx) { if (!showIndicator) return; // Bar const double length[] = { 1.0, 1.0 }; CGContextSetLineWidth(mboxctx, mbxBarWidth); CGContextSetLineDash(mboxctx, 0, length, 2); CGContextSetLineJoin(mboxctx, kCGLineJoinMiter); CGContextBeginPath(mboxctx); double x = mbxOffsetX + mbxMarginX + mbxBarWidth / 2.0; for (int h = 0; h < 8; h++) { // Inactive CGContextSetRGBStrokeColor(mboxctx, (196.0 / 256.0), (200.0 / 256.0), (176.0 / 256.0), 1.0); CGContextMoveToPoint (mboxctx, x, mbxOffsetY + mbxMarginY); CGContextAddLineToPoint(mboxctx, x, mbxOffsetY + mbxMarginY + mbxBarHeight); CGContextMoveToPoint (mboxctx, x + mbxRightBarX, mbxOffsetY + mbxMarginY); CGContextAddLineToPoint(mboxctx, x + mbxRightBarX, mbxOffsetY + mbxMarginY + mbxBarHeight); CGContextStrokePath(mboxctx); // Max Channel *ch = &SoundData.channels[h]; ch->envx = ch->xenvx >> 4; ch-> left_vol_level = (ch->xenvx * ch->volume_left ) >> 11; ch->right_vol_level = (ch->xenvx * ch->volume_right) >> 11; short vl = ch-> left_vol_level; short vr = ch->right_vol_level; long long currentTime; if (vl <= 0) vl = 0; else if (vl > 64) vl = 64; else vl = (short) (yyscale * sqrt((double) vl)) & (~0 << 1); if (vr <= 0) vr = 0; else if (vr > 64) vr = 64; else vr = (short) (yyscale * sqrt((double) vr)) & (~0 << 1); if (vl < prevLVol[h]) vl = ((prevLVol[h] + vl) >> 1); if (vr < prevRVol[h]) vr = ((prevRVol[h] + vr) >> 1); Microseconds((UnsignedWide *) ¤tTime); // left if ((vl >= prevLMax[h]) && (vl > prevLVol[h])) { barTimeL[h] = currentTime; prevLMax[h] = vl; } else if ((prevLMax[h] > 0) && (barTimeL[h] + 1000000 > currentTime)) { CGContextSetRGBStrokeColor(mboxctx, (22.0 / 256.0), (156.0 / 256.0), (20.0 / 256.0), (double) (barTimeL[h] + 1000000 - currentTime) / 1000000.0); CGContextMoveToPoint (mboxctx, x, mbxOffsetY + mbxMarginY + (double) (prevLMax[h] - 2)); CGContextAddLineToPoint(mboxctx, x, mbxOffsetY + mbxMarginY + (double) (prevLMax[h] )); CGContextStrokePath(mboxctx); } else prevLMax[h] = 0; prevLVol[h] = vl; // right if ((vr >= prevRMax[h]) && (vr > prevRVol[h])) { barTimeR[h] = currentTime; prevRMax[h] = vr; } else if ((prevRMax[h] > 0) && (barTimeR[h] + 1000000 > currentTime)) { CGContextSetRGBStrokeColor(mboxctx, (22.0 / 256.0), (156.0 / 256.0), (20.0 / 256.0), (double) (barTimeR[h] + 1000000 - currentTime) / 1000000.0); CGContextMoveToPoint (mboxctx, x + mbxRightBarX, mbxOffsetY + mbxMarginY + (double) (prevRMax[h] - 2)); CGContextAddLineToPoint(mboxctx, x + mbxRightBarX, mbxOffsetY + mbxMarginY + (double) (prevRMax[h] )); CGContextStrokePath(mboxctx); } else prevRMax[h] = 0; prevRVol[h] = vr; // Active CGContextSetRGBStrokeColor(mboxctx, (22.0 / 256.0), (22.0 / 256.0), (20.0 / 256.0), 1.0); CGContextMoveToPoint (mboxctx, x, mbxOffsetY + mbxMarginY); CGContextAddLineToPoint(mboxctx, x, mbxOffsetY + mbxMarginY + (double) vl); CGContextStrokePath(mboxctx); CGContextMoveToPoint (mboxctx, x + mbxRightBarX, mbxOffsetY + mbxMarginY); CGContextAddLineToPoint(mboxctx, x + mbxRightBarX, mbxOffsetY + mbxMarginY + (double) vr); CGContextStrokePath(mboxctx); x += (mbxBarWidth + mbxBarSpace); } }
void LineTool( WindowRef window ) { OSStatus err; Point endPt; MouseTrackingResult trackingResult; Point beginPt; Rect windowRect; WindowRef overlayWindow; CGRect cgRect; CGContextRef cgContext; Boolean isStillDown = true; SetThemeCursor( kThemeCrossCursor ); SetPortWindowPort( window ); GetWindowPortBounds( window, &windowRect ); LocalToGlobalRect( &windowRect ); (void) CreateNewWindow( kOverlayWindowClass, kWindowHideOnSuspendAttribute | kWindowIgnoreClicksAttribute, &windowRect, &overlayWindow ); SetPortWindowPort( overlayWindow ); SetWindowGroup( overlayWindow, GetWindowGroup(window) ); // This assures we draw into the same layer as the window ShowWindow( overlayWindow ); GetMouse( &beginPt ); cgRect = CGRectMake( 0, 0, windowRect.right - windowRect.left+1, windowRect.bottom - windowRect.top+1 ); CreateCGContextForPort( GetWindowPort(overlayWindow), &cgContext ); CGContextSetLineWidth( cgContext, 3 ); // Line is 3 pixels wide CGContextSetRGBStrokeColor( cgContext, 1.0, .45, .3, .4 ); // Make it orange with alpha = 0.4 SyncCGContextOriginWithPort( cgContext, GetWindowPort(overlayWindow) ); CGContextTranslateCTM( cgContext, 0, windowRect.bottom - windowRect.top ); // Flip & rotate the context to use QD coordinates CGContextScaleCTM( cgContext, 1.0, -1.0 ); do { err = TrackMouseLocation( GetWindowPort(window), &endPt, &trackingResult ); switch ( trackingResult ) { case kMouseTrackingMouseDragged: CGContextClearRect( cgContext, cgRect ); // "Erase" the window #if ( 1 ) CGContextMoveToPoint( cgContext, beginPt.h, beginPt.v ); // Draw the line CGContextAddLineToPoint( cgContext, endPt.h, endPt.v ); CGContextStrokePath( cgContext ); #else MoveTo( beginPt.h, beginPt.v ); // We could use QuickDraw and draw opaque lines LineTo( endPt.h, endPt.v ); #endif CGContextFlush( cgContext ); // Flush our drawing to the screen break; case kMouseTrackingMouseDown: break; case kMouseTrackingMouseUp: case kMouseTrackingUserCancelled: isStillDown = false; break; } } while( isStillDown == true ); CGContextRelease( cgContext ); DisposeWindow( overlayWindow ); SetThemeCursor( kThemeArrowCursor ); return; }
void RSTileRendererDrawTile( CGContextRef c, CGRect bounds, uint8_t *data, uint32_t dataSize, uint32_t zoom, RSTileRendererGraphicsSpace graphicsSpace) { CGFloat width = (CGFloat)bounds.size.width; CGFloat height = (CGFloat)bounds.size.height; const CGFloat backgroundColor[4] = {0.5, 0.5, 0.5, 1}; const CGFloat fillColors[8][4] = { {0, 0, 0, 0}, {(210 / 255.0), (173 / 255.0), (104 / 255.0), 1}, // Building = 1, {(66 / 255.0), (66 / 255.0), (255 / 255.0), 1}, // HighwayMotorway = 2, {(255 / 255.0), (66 / 255.0), (66 / 255.0), 1}, // HighwayPrimary = 3, {(255 / 255.0), (155 / 255.0), (66 / 255.0), 1}, // HighwaySecondary = 4, {(255 / 255.0), (255 / 255.0), (66 / 255.0), 1}, // HighwayTertiary = 5, {(255 / 255.0), (255 / 255.0), (255 / 255.0), 1}, // HighwayResidential = 6, {(255 / 255.0), (255 / 255.0), (255 / 255.0), 1} // HighwayService = 7, }; const CGFloat strokeColors[8][4] = { {0, 0, 0, 0}, {(140 / 255.0), (93 / 255.0), (33 / 255.0), 1}, // Building = 1, {(44 / 255.0), (44 / 255.0), (200 / 255.0), 1}, // HighwayMotorway = 2, {(200 / 255.0), (44 / 255.0), (44 / 255.0), 1}, // HighwayPrimary = 3, {(200 / 255.0), (100 / 255.0), (44 / 255.0), 1}, // HighwaySecondary = 4, {(200 / 255.0), (200 / 255.0), (44 / 255.0), 1}, // HighwayTertiary = 5, {(200 / 255.0), (200 / 255.0), (200 / 255.0), 1}, // HighwayResidential = 6, {(200 / 255.0), (200 / 255.0), (200 / 255.0), 1} // HighwayService = 7, }; CGContextSetFillColor(c, backgroundColor); CGContextFillRect(c, bounds); uint32_t pos = 0; uint32_t resolutionBits = 8; double intToFloat = width / (1 << resolutionBits); int32_t ix, iy; double x, y, firstX, firstY; while (pos < dataSize) { ix = 0; iy = 0; RSTiledataThingType thingType = (RSTiledataThingType)RSVarintRead(data, &pos); uint32_t numNodes = RSVarintRead(data, &pos); for (uint32_t i = 0; i < numNodes; i++) { ix += RSVarintSignedRead(data, &pos); iy += RSVarintSignedRead(data, &pos); x = intToFloat * ix; y = intToFloat * iy; if (graphicsSpace == RSTileRendererGraphicsSpaceQuadrantOne) { y = height - y; } if (i == 0) { firstX = x; firstY = y; CGContextMoveToPoint(c, x, y); } else { CGContextAddLineToPoint(c, x, y); } } CGContextSetFillColor(c, fillColors[thingType]); CGContextSetStrokeColor(c, strokeColors[thingType]); switch (thingType) { case RSTiledataBuilding: CGContextSetLineWidth(c, 4); break; case RSTiledataHighwayMotorway: case RSTiledataHighwayPrimary: case RSTiledataHighwaySecondary: case RSTiledataHighwayTertiary: case RSTiledataHighwayResidential: case RSTiledataHighwayService: CGContextSetLineWidth(c, 12); break; } if (RSTiledataThingTypeIsClosedWay(thingType)) { CGContextAddLineToPoint(c, firstX, firstY); CGContextDrawPath(c, kCGPathFillStroke); } else { CGContextStrokePath(c); } } }
static void drawMarks(CGContextRef gc, int width, int height, int max) { float angle, c, s, tick, radius, needle; float r0, r1, r2, r3, cx, cy; int i, start, end, redline, section; cx = CENTERX * width; cy = CENTERY * height; redline = TICKS * SUBTICKS * 0.8; radius = 0.5 * (width > height ? width : height); needle = radius * 0.85; for (section = 0; section < 2; section++) { start = section ? redline + 1 : 0; end = section ? TICKS * SUBTICKS : redline; if (section) CGContextSetRGBStrokeColor(gc, 1.0, 0.1, 0.1, 1.0); else CGContextSetRGBStrokeColor(gc, 0.9, 0.9, 1.0, 1.0); // inner tick ring r0 = 0.97 * needle; r1 = 1.04 * needle; r2 = 1.00 * needle; r3 = 1.01 * needle; for (i = start; i <= end ; i++) { tick = i / (float)(SUBTICKS * TICKS); angle = (2.0 / 3.0) * (2 * M_PI) * tick - M_PI / 6; c = cos(angle); s = sin(angle); if (i % SUBTICKS != 0) { CGContextMoveToPoint(gc, cx - r0*c, cy + r0*s); CGContextAddLineToPoint(gc, cx - r1*c, cy + r1*s); } else { CGContextMoveToPoint(gc, cx - r2*c, cy + r2*s); CGContextAddLineToPoint(gc, cx - r3*c, cy + r3*s); } } CGContextSetLineWidth(gc, 2.0); CGContextStrokePath(gc); // outer tick ring start = ((float)start / SUBTICKS) + section; end = ((float)end / SUBTICKS); r0 = 1.05 * needle; r1 = 1.14 * needle; for (i = start; i <= end ; i++) { tick = i / (float)(TICKS); angle = (2.0 / 3.0) * (2 * M_PI) * tick - M_PI / 6; c = cos(angle); s = sin(angle); CGContextMoveToPoint(gc, cx - r0*c, cy + r0*s); CGContextAddLineToPoint(gc, cx - r1*c, cy + r1*s); } CGContextSetLineWidth(gc, 3.0); CGContextStrokePath(gc); } CGContextSelectFont(gc, "Arial Bold Italic", 20.0, kCGEncodingMacRoman); r0 = 0.82 * needle; for (i = 0 ; i <= max ; i += (max / TICKS)) { char text[20]; float dx, dy; sprintf(text, "%d", i); // hardcoded text centering for this font size { if (i > 199) dx = -18; else if (i > 99) dx = -17; else if (i > 0) dx = -14; else dx = -12; dy = -6.0; } angle = (2.0 / 3.0) * (2 * M_PI) * i / max - M_PI / 6; c = cos(angle); s = sin(angle); CGContextShowTextAtPoint(gc, cx - r0*c + dx, cy + r0*s + dy, text, strlen(text)); } }
void draw__line(xy__Float x1, xy__Float y1, xy__Float x2, xy__Float y2) { CGContextMoveToPoint (ctx, x1, y1); CGContextAddLineToPoint(ctx, x2, y2); CGContextStrokePath (ctx); }
bool GiCanvasIos::rawLineTo(float x, float y) { CGContextAddLineToPoint(m_draw->getContext(), x, y); return true; }