//----------------------------------------------------------------------------- void CGDrawContext::applyLineStyle (CGContextRef context) { switch (currentState.lineStyle.getLineCap ()) { case CLineStyle::kLineCapButt: CGContextSetLineCap (context, kCGLineCapButt); break; case CLineStyle::kLineCapRound: CGContextSetLineCap (context, kCGLineCapRound); break; case CLineStyle::kLineCapSquare: CGContextSetLineCap (context, kCGLineCapSquare); break; } switch (currentState.lineStyle.getLineJoin ()) { case CLineStyle::kLineJoinMiter: CGContextSetLineJoin (context, kCGLineJoinMiter); break; case CLineStyle::kLineJoinRound: CGContextSetLineJoin (context, kCGLineJoinRound); break; case CLineStyle::kLineJoinBevel: CGContextSetLineJoin (context, kCGLineJoinBevel); break; } if (currentState.lineStyle.getDashCount () > 0) { CGFloat* dashLengths = new CGFloat [currentState.lineStyle.getDashCount ()]; for (int32_t i = 0; i < currentState.lineStyle.getDashCount (); i++) { dashLengths[i] = currentState.frameWidth * currentState.lineStyle.getDashLengths ()[i]; } CGContextSetLineDash (context, currentState.lineStyle.getDashPhase (), dashLengths, currentState.lineStyle.getDashCount ()); delete [] dashLengths; } }
void GraphicsContext::setLineCap(LineCap cap) { if (paintingDisabled()) return; switch (cap) { case ButtCap: CGContextSetLineCap(platformContext(), kCGLineCapButt); break; case RoundCap: CGContextSetLineCap(platformContext(), kCGLineCapRound); break; case SquareCap: CGContextSetLineCap(platformContext(), kCGLineCapSquare); break; } }
bool GiCanvasIos::beginPaintBuffered(bool fast, bool autoscale) { if (m_draw->getContext() || !gs() || !m_draw->createBufferBitmap(m_draw->width(), m_draw->height(), autoscale ? m_draw->_scale : 1.f)) { return false; } RECT_2D clipBox = { 0, 0, m_draw->width(), m_draw->height() }; owner()->_beginPaint(clipBox); m_draw->_fast = fast; m_draw->_ctxused[0] = false; m_draw->_ctxused[1] = false; CGContextRef context = m_draw->getContext(); bool antiAlias = !fast && gs()->isAntiAliasMode(); CGContextSetAllowsAntialiasing(context, antiAlias); CGContextSetShouldAntialias(context, antiAlias); CGContextSetFlatness(context, fast ? 20 : 1); CGContextSetLineCap(context, kCGLineCapRound); CGContextSetLineJoin(context, kCGLineJoinRound); owner()->setMaxPenWidth(-1, 0.5f / m_draw->_scale); return true; }
bool GiCanvasIos::beginPaint(CGContextRef context, bool fast, bool buffered) { if (m_draw->getContext() || !context) return false; if (gs()) { CGRect rc = CGContextGetClipBoundingBox(context); RECT_2D clipBox = { rc.origin.x, rc.origin.y, rc.origin.x + rc.size.width, rc.origin.y + rc.size.height }; owner()->_beginPaint(clipBox); } m_draw->_context = context; m_draw->_fast = fast; m_draw->_ctxused[0] = false; m_draw->_ctxused[1] = false; if (buffered && gs()) { m_draw->createBufferBitmap(m_draw->width(), m_draw->height(), m_draw->_scale); context = m_draw->getContext(); } bool antiAlias = !fast && (!gs() || gs()->isAntiAliasMode()); CGContextSetAllowsAntialiasing(context, antiAlias); CGContextSetShouldAntialias(context, antiAlias); CGContextSetFlatness(context, fast ? 20 : 1); CGContextSetLineCap(context, kCGLineCapRound); CGContextSetLineJoin(context, kCGLineJoinRound); if (owner()) // 设置最小线宽为0.5像素,使用屏幕放大倍数以便得到实际像素值 owner()->setMaxPenWidth(-1, 0.5f / m_draw->_scale); return true; }
bool setPen(const GiContext* ctx) { bool changed = !_ctxused[0]; if (ctx && !ctx->isNullLine()) { if (_gictx.getLineColor() != ctx->getLineColor()) { _gictx.setLineColor(ctx->getLineColor()); changed = true; } if (_gictx.getLineWidth() != ctx->getLineWidth()) { _gictx.setLineWidth(ctx->getLineWidth(), ctx->isAutoScale()); changed = true; } if (_gictx.getLineStyle() != ctx->getLineStyle()) { _gictx.setLineStyle(ctx->getLineStyle()); changed = true; } } if (!ctx) ctx = &_gictx; if (!ctx->isNullLine() && changed) { _ctxused[0] = true; GiColor color = ctx->getLineColor(); if (gs()) color = gs()->calcPenColor(color); CGContextSetRGBStrokeColor(getContext(), toFloat(color.r), toFloat(color.g), toFloat(color.b), toFloat(color.a)); float w = ctx->getLineWidth(); w = gs() ? gs()->calcPenWidth(w, ctx->isAutoScale()) : (w < 0 ? -w : 1); CGContextSetLineWidth(getContext(), _fast && w > 1 ? w - 1 : w); // 不是反走样就细一点 int style = ctx->getLineStyle(); CGFloat pattern[6]; if (style >= 0 && style < sizeof(lpats)/sizeof(lpats[0])) { if (lpats[style].arr && !_fast) { // 快速画时不要线型 makeLinePattern(pattern, lpats[style].arr, lpats[style].n, w); CGContextSetLineDash(getContext(), 0, pattern, lpats[style].n); } else { CGContextSetLineDash(getContext(), 0, NULL, 0); } CGContextSetLineCap(getContext(), style > 0 ? kCGLineCapButt : kCGLineCapRound); } } return !ctx->isNullLine(); }
static void Quartz_SetLineEnd(R_GE_lineend lend, NewDevDesc *dd) { QuartzDesc *xd = (QuartzDesc*)dd->deviceSpecific; CGLineCap linecap; switch (lend) { case GE_ROUND_CAP: linecap = kCGLineCapRound; break; case GE_BUTT_CAP: linecap = kCGLineCapButt; break; case GE_SQUARE_CAP: linecap = kCGLineCapSquare; break; default: error(_("Invalid line end")); } CGContextSetLineCap( GetContext(xd), linecap); }
static void initBackground(unsigned w, unsigned h, unsigned max) { CGColorSpaceRef colorspace; CGContextRef gc; unsigned char *data; float cx, cy; float radius, needle; data = (unsigned char *)malloc(w * h * 4); colorspace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB); gc = CGBitmapContextCreate(data, w, h, 8, w * 4, colorspace, kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host); cx = CENTERX * w; cy = CENTERY * h; radius = 0.5 * (w > h ? w : h); needle = radius * 0.85; // background CGContextTranslateCTM(gc, 0.0, h); CGContextScaleCTM(gc, 1.0, -1.0); CGContextClearRect(gc, CGRectMake(0, 0, w, h)); CGContextSetRGBFillColor(gc, 0.0, 0.0, 0.0, 0.7); CGContextAddArc(gc, cx, cy, radius, 0, 2*M_PI, false); CGContextFillPath(gc); CGGradientRef gradient; size_t num_locations = 2; CGFloat locations[2] = { 0.0, 1.0 }; CGFloat components[8] = { 1.0, 1.0, 1.0, 0.5, // Start color 0.0, 0.0, 0.0, 0.0 }; // End color gradient = CGGradientCreateWithColorComponents (colorspace, components, locations, num_locations); // top rim light CGContextSaveGState(gc); CGContextAddArc(gc, cx, cy, radius, 0, 2*M_PI, false); CGContextAddArc(gc, cx, cy, needle*1.05, 0, 2*M_PI, false); CGContextEOClip(gc); CGContextDrawRadialGradient (gc, gradient, CGPointMake(cx, cy*1.00), radius*1.01, CGPointMake(cx, cy*0.96), radius*0.98, 0); // bottom rim light CGContextDrawRadialGradient (gc, gradient, CGPointMake(cx, cy*1.00), radius*1.01, CGPointMake(cx, cy*1.04), radius*0.98, 0); // top bevel CGContextDrawRadialGradient (gc, gradient, CGPointMake(cx, cy*2.2), radius*0.2, CGPointMake(cx, cy*1.0), radius*1.0, 0); // bottom bevel CGContextRestoreGState(gc); CGContextSaveGState(gc); CGContextAddArc(gc, cx, cy, needle*1.05, 0, 2*M_PI, false); CGContextAddArc(gc, cx, cy, needle*0.96, 0, 2*M_PI, false); CGContextEOClip(gc); CGContextDrawRadialGradient (gc, gradient, CGPointMake(cx, cy* -.5), radius*0.2, CGPointMake(cx, cy*1.0), radius*1.0, 0); CGGradientRelease(gradient); CGContextRestoreGState(gc); CGContextSetRGBFillColor(gc, 0.9, 0.9, 1.0, 1.0); CGContextSetRGBStrokeColor(gc, 0.9, 0.9, 1.0, 1.0); CGContextSetLineCap(gc, kCGLineCapRound); // draw several glow passes, with the content offscreen CGContextTranslateCTM(gc, 0, OFFSCREEN - 10); CGContextSetShadowWithColor(gc, CGSizeMake(0, OFFSCREEN), 48.0, CGColorCreateGenericRGB(0.5, 0.5, 1.0, 0.7)); drawMarks(gc, w, h, max); CGContextTranslateCTM(gc, 0, 20); CGContextSetShadowWithColor(gc, CGSizeMake(0, OFFSCREEN), 48.0, CGColorCreateGenericRGB(0.5, 0.5, 1.0, 0.7)); drawMarks(gc, w, h, max); CGContextTranslateCTM(gc, -10, -10); CGContextSetShadowWithColor(gc, CGSizeMake(0, OFFSCREEN), 48.0, CGColorCreateGenericRGB(0.5, 0.5, 1.0, 0.7)); drawMarks(gc, w, h, max); CGContextTranslateCTM(gc, 20, 0); CGContextSetShadowWithColor(gc, CGSizeMake(0, OFFSCREEN), 48.0, CGColorCreateGenericRGB(0.5, 0.5, 1.0, 0.7)); drawMarks(gc, w, h, max); CGContextTranslateCTM(gc, -10, -OFFSCREEN); // draw real content CGContextSetShadowWithColor(gc, CGSizeMake(0, 1), 6.0, CGColorCreateGenericRGB(0.7, 0.7, 1.0, 0.9)); drawMarks(gc, w, h, max); glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA8, w, h, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, data); CGContextRelease(gc); CGColorSpaceRelease(colorspace); free(data); }
void CFX_QuartzDeviceDriver::setStrokeInfo(const CFX_GraphStateData* graphState, FX_ARGB argb, FX_FLOAT lineWidth) { if (NULL == graphState) { return; } CGContextSetLineWidth(_context, lineWidth); CGLineCap cap; switch (graphState->m_LineCap) { case CFX_GraphStateData::LineCapRound: { cap = kCGLineCapRound; break; } case CFX_GraphStateData::LineCapSquare: { cap = kCGLineCapSquare; break; } case CFX_GraphStateData::LineCapButt: default: { cap = kCGLineCapButt; } } CGContextSetLineCap(_context, cap); CGLineJoin join; switch (graphState->m_LineJoin) { case CFX_GraphStateData::LineJoinRound: { join = kCGLineJoinRound; break; } case CFX_GraphStateData::LineJoinBevel: { join = kCGLineJoinBevel; break; } case CFX_GraphStateData::LineJoinMiter: default: { join = kCGLineJoinMiter; } } CGContextSetLineJoin(_context, join); if (graphState->m_DashCount) { #if CGFLOAT_IS_DOUBLE CGFloat* dashArray = new CGFloat[graphState->m_DashCount]; if (!dashArray) { return; } for (int index = 0; index < graphState->m_DashCount; ++index) { dashArray[index] = graphState->m_DashArray[index]; } #else CGFloat* dashArray = (CGFloat*)graphState->m_DashArray; #endif CGContextSetLineDash(_context, graphState->m_DashPhase, dashArray, graphState->m_DashCount); #if CGFLOAT_IS_DOUBLE delete[] dashArray; #endif } 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); }
void CGContextSetLineCap_wrap(CGContext *con, int cap) { CGContextSetLineCap(con, CGLineCap(cap)); }
void MyTimerProc ( EventLoopTimerRef inTimer, void *inUserData ) { WindowRef window = (WindowRef)inUserData; static int i = 0; static int j = 0; static int step = 5; GrafPtr curPort; CGContextRef context; CGrafPtr windowPort = GetWindowPort( window ); GetPort(&curPort); SetPort(windowPort); CreateCGContextForPort( windowPort, &context ); // Do our drawing in here CGContextSetGrayFillColor( context, 0.0, 1.0 ); CGContextFillRect( context, CGRectMake( 0, 0, 800, 600 ) ); CGContextSetGrayFillColor( context, 1.0, 1.0 ); CGContextSetRGBStrokeColor( context, 1.0, 0.0, 0.0, 1.0 ); CGContextSetLineWidth( context, 5.0 ); CGContextSetLineCap( context, kCGLineCapRound ); CGContextSetLineJoin( context, kCGLineJoinRound ); CGContextSetMiterLimit( context, 5 ); // Draw each of the 4 transform demos // // 1. Translate - move the origin, draw the graphic // Place the origin in the llh corner of the upper left quadrant CGContextSaveGState( context ); CGContextTranslateCTM( context, i / 2, i / 2 + 300 ); CGContextRotateCTM( context, -2.0 * 3.1416 / 8 ); drawGraphic( context, 0, 0 ); CGContextRestoreGState( context ); // 2. Rotate - move origin, do rotation, draw the graphic // Put the origin back into the llh corner of the upper right quadrant CGContextSaveGState( context ); if ( j < 1000/2 ) { // Rotate around left circle CGContextTranslateCTM( context, 500, 450 ); CGContextRotateCTM( context, -j * 3.1416 * 2.0 / 500.0 - 3.1416 ); CGContextTranslateCTM( context, -100, -20 ); drawGraphic( context, 0, 0 ); } if ( j >= 1000/2 ) { // Rotate under bottom half and over top right 1/4 CGContextTranslateCTM( context, 700, 450 ); CGContextRotateCTM( context, j * 3.1416 * 2.0 / 500.0 ); CGContextScaleCTM( context, 1, 1 ); CGContextTranslateCTM( context, -100, 20 ); CGContextRotateCTM( context, 3.1416 ); drawGraphic( context, 0, 0 ); } CGContextRestoreGState( context ); // 3. Scale - move origin, set scale, draw the graphic CGContextSaveGState( context ); CGContextScaleCTM( context, i * 400.0 / 500.0 / 40.0, i * 300.0 / 500.0 / 40.0 ); CGContextRotateCTM( context, -3.1416 / 4 ); drawGraphic( context, 0, 0 ); CGContextRestoreGState( context ); // 4. Show the basic graphic CGContextTranslateCTM( context, 600, 150 ); drawGraphic( context, 0, 0 ); CGContextFlush( context ); CGContextRelease( context ); SetPort ( curPort ); i += step; j += abs( step ); if ( i >= 500 ) { step = -step; } if ( i <= 0 ) { step = -step; j = 0; } }