osxcontext.cpp
来自「这是VCF框架的代码」· C++ 代码 · 共 2,398 行 · 第 1/5 页
CPP
2,398 行
}void OSXContext::rectangle(const double & x1, const double & y1, const double & x2, const double & y2){ endLastPrimitive(); CGRect rect; rect.origin.x = x1 + origin_.x_; rect.origin.y = y1 + origin_.y_; rect.size.width = x2 - x1; rect.size.height = y2 - y1; //CGContextBeginPath( contextID_ ); CGContextAddRect( contextID_, rect ); //CGContextClosePath( contextID_ ); if ( GraphicsContext::doStroke == currentDrawingOperation_ ) { //CGContextStrokePath ( contextID_ ); } else if ( GraphicsContext::doFill == currentDrawingOperation_ ) { //CGContextFillPath ( contextID_ ); }}void OSXContext::roundRect(const double & x1, const double & y1, const double & x2, const double & y2, const double & xc, const double & yc){}void OSXContext::ellipse(const double & x1, const double & y1, const double & x2, const double & y2 ){ endLastPrimitive(); float a, b; CGPoint center; center.x = x1 + (x2-x1)/2.0; center.y = y1 + (y2-y1)/2.0; a = (x2-x1)/2.0; b = (y2-y1)/2.0; CGContextBeginPath ( contextID_ ); CGContextSaveGState(contextID_); CGContextTranslateCTM(contextID_, center.x, center.y); CGContextScaleCTM(contextID_, a, b); CGContextMoveToPoint(contextID_, 1, 0); CGContextAddArc(contextID_, 0, 0, 1, Math::degreesToRadians(0), Math::degreesToRadians(360), 0); CGContextClosePath(contextID_); CGContextRestoreGState(contextID_); if ( GraphicsContext::doStroke == currentDrawingOperation_ ) { CGContextStrokePath(contextID_); } else if ( GraphicsContext::doFill == currentDrawingOperation_ ) { CGContextFillPath ( contextID_ ); }}void OSXContext::arc(const double & x1, const double & y1, const double & x2, const double & y2, const double & x3, const double & y3, const double & x4, const double & y4){ //CGContextAddArc}void OSXContext::polyline(const std::vector<Point>& pts ){ endLastPrimitive(); std::vector<Point>::const_iterator it = pts.begin(); CGPoint* cgPts = new CGPoint[pts.size()]; int i = 0; while ( it != pts.end() ) { const Point& pt = *it; cgPts[i].x = pt.x_ + origin_.x_; cgPts[i].y = pt.y_ + origin_.y_; it ++; i++; } CGContextAddLines( contextID_, cgPts, pts.size() ); if ( GraphicsContext::doStroke == currentDrawingOperation_ ) { //CGContextStrokePath ( contextID_ ); } else if ( GraphicsContext::doFill == currentDrawingOperation_ ) { //CGContextFillPath ( contextID_ ); } delete [] cgPts;}void OSXContext::curve(const double & x1, const double & y1, const double & x2, const double & y2, const double & x3, const double & y3, const double & x4, const double & y4){ endLastPrimitive(); //CGContextBeginPath( contextID_ ); CGContextMoveToPoint( contextID_, x1 + origin_.x_, y1 + origin_.y_ ); CGContextAddCurveToPoint( contextID_, x2 + origin_.x_, y2 + origin_.y_, x3 + origin_.x_, y3 + origin_.y_, x4 + origin_.x_, y4 + origin_.y_ ); //CGContextClosePath( contextID_ ); if ( GraphicsContext::doStroke == currentDrawingOperation_ ) { //CGContextStrokePath ( contextID_ ); } else if ( GraphicsContext::doFill == currentDrawingOperation_ ) { //CGContextFillPath ( contextID_ ); }}void OSXContext::lineTo(const double & x, const double & y){ //CGContextAddLineToPoint( contextID_, x + origin_.x_, y + origin_.y_ ); //CGContextStrokePath ( contextID_ ); finishLastPrimitive(x,y); CGPoint pt = CGContextGetPathCurrentPoint ( contextID_ ); double dx = static_cast<double>(pt.x)-(x+.5); double dy = static_cast<double>(pt.y)-(y+.5); double len = sqrt(dx*dx + dy*dy); if ( len < 1.0 ){ return; } double ndx = dx/len; double ndy = dy/len; lastPrimitiveV1_.x_ = ndx; lastPrimitiveV1_.y_ = ndy; lastPrimitive_ = OSXContext::lpLine; lastPrimitiveP1_.x_ = x; lastPrimitiveP1_.y_ = y; CGContextAddLineToPoint( contextID_, x + origin_.x_ + ndx + .5, y + origin_.y_ +ndy + .5); }void OSXContext::moveTo(const double & x, const double & y){ //CGContextMoveToPoint( contextID_, x + origin_.x_, y + origin_.y_ ); endLastPrimitive(); lastPrimitive_ = OSXContext::lpMove; lastPrimitiveP1_.x_ = x; lastPrimitiveP1_.y_ = y; CGContextMoveToPoint( contextID_, x + origin_.x_ + .5, y + origin_.y_ + .5);}void OSXContext::setOrigin( const double& x, const double& y ){ if ( lastPrimitive_ != OSXContext::lpNone ) { lastPrimitiveP1_.x_ += origin_.x_ - x; lastPrimitiveP1_.y_ += origin_.y_ - y; } origin_.x_ = x; origin_.y_ = y;}VCF::Point OSXContext::getOrigin(){ return origin_;}void OSXContext::copyContext( const Rect& sourceRect, const Rect& destRect, ContextPeer* sourceContext ){ ::Rect srcRect; srcRect.left = (int)sourceRect.left_; srcRect.right = (int)sourceRect.right_; srcRect.top = (int)sourceRect.top_; srcRect.bottom = (int)sourceRect.bottom_; ::Rect dstRect; dstRect.left = (int)(destRect.left_ + origin_.x_); dstRect.right = (int)(destRect.right_ + origin_.x_); dstRect.top = (int)(destRect.top_ + origin_.y_); dstRect.bottom = (int)(destRect.bottom_ + origin_.y_); GrafPtr srcPort = (GrafPtr)sourceContext->getContextID(); GrafPtr destPort = grafPort_; CopyBits( GetPortBitMapForCopyBits (srcPort), GetPortBitMapForCopyBits (destPort), &srcRect, &dstRect, srcCopy, 0 );}bool OSXContext::isMemoryContext(){ return (NULL != inMemoryImage_) ? true : false;}bool OSXContext::prepareForDrawing( long drawingOperation ){ currentDrawingOperation_ = drawingOperation; checkHandle(); Color* currentColor = context_->getColor(); GrafPtr oldPort; GetPort( &oldPort ); if ( oldPort != grafPort_ ) { //SetPort( grafPort_ ); } if ( xorModeOn_ ) { PenMode( srcXor ); } else { PenMode( srcCopy ); } if ( oldPort != grafPort_ ) { //SetPort( oldPort ); } CGContextSetShouldAntialias(contextID_, antialiasingOn_); float colorComponents[4] = {currentColor->getRed(), currentColor->getGreen(), currentColor->getBlue(), 1.0}; switch ( drawingOperation ) { case GraphicsContext::doStroke : { CGContextSetLineWidth( contextID_, context_->getStrokeWidth() ); CGContextSetRGBStrokeColor( contextID_, colorComponents[0], colorComponents[1], colorComponents[2], colorComponents[3] ); CGContextBeginPath ( contextID_ ); } break; case GraphicsContext::doFill : { CGContextSetRGBFillColor( contextID_, colorComponents[0], colorComponents[1], colorComponents[2], colorComponents[3] ); CGContextBeginPath ( contextID_ ); } break; case GraphicsContext::doText : { //save state CGContextSaveGState( contextID_ ); //change the scaling back to normal otherwise we will have vertically //flipped glyphs - and no one wants that!! CGAffineTransform xfrm = CGAffineTransformMakeScale( 1, -1 ); CGContextConcatCTM( contextID_, xfrm ); //xfrm = CGAffineTransformMakeTranslation( 0, -(ownerRect_.top_) ); //CGContextConcatCTM( contextID_, xfrm ); Font* ctxFont = context_->getCurrentFont(); OSXFont* fontImpl = (OSXFont*)ctxFont->getFontPeer(); Color* fontColor = ctxFont->getColor(); fontImpl->setColor( fontColor ); colorComponents[0] = fontColor->getRed(); colorComponents[1] = fontColor->getGreen(); colorComponents[2] = fontColor->getBlue(); CGContextSetRGBFillColor( contextID_, colorComponents[0], colorComponents[1], colorComponents[2], colorComponents[3] ); CGContextSetRGBStrokeColor( contextID_, colorComponents[0], colorComponents[1], colorComponents[2], colorComponents[3] ); } break; case GraphicsContext::doImage : { } break; } return true;}void OSXContext::finishedDrawing( long drawingOperation ){ endLastPrimitive(); switch ( drawingOperation ) { case GraphicsContext::doStroke : { //CGContextClosePath( contextID_ ); CGContextStrokePath ( contextID_ ); } break; case GraphicsContext::doFill : { //CGContextClosePath( contextID_ ); CGContextFillPath ( contextID_ ); } break; case GraphicsContext::doText : { CGContextRestoreGState( contextID_ ); } break; case GraphicsContext::doImage : { } break; } currentDrawingOperation_ = -1; releaseHandle();}void OSXContext::setAntiAliasingOn( bool antiAliasingOn ){ endLastPrimitive(); antialiasingOn_ = antiAliasingOn;}void OSXContext::endLastPrimitive(){ if (!isAntiAliasingOn()) return; if ( lastPrimitive_ == OSXContext::lpLine ) { CGContextAddLineToPoint( contextID_, origin_.x_ + lastPrimitiveP1_.x_ + .5 + lastPrimitiveV1_.x_/2.0, origin_.y_ + lastPrimitiveP1_.y_ + .5 + lastPrimitiveV1_.y_/2.0); } lastPrimitive_ = OSXContext::lpNone;}void OSXContext::finishLastPrimitive(const double & x, const double & y){ if ( lastPrimitive_ == OSXContext::lpNone ){ return; } if ( lastPrimitive_ == OSXContext::lpMove ) { if (!isAntiAliasingOn()) return; CGPoint pt=CGContextGetPathCurrentPoint ( contextID_ ); double dx=x-lastPrimitiveP1_.x_; double dy=y-lastPrimitiveP1_.y_; double len=sqrt(dx*dx+dy*dy); if (len<1.0) return; double ndx=dx/len; double ndy=dy/len; CGContextMoveToPoint(contextID_, origin_.x_ + lastPrimitiveP1_.x_ + .5 - ndx/2.0, origin_.y_ + lastPrimitiveP1_.y_ + .5 - ndy/2.0); } else if ( lastPrimitive_ == OSXContext::lpLine ){ CGContextAddLineToPoint(contextID_, origin_.x_ + lastPrimitiveP1_.x_ + .5, origin_.y_ + lastPrimitiveP1_.y_ + .5); }}void OSXContext::drawImage( const double& x, const double& y, Rect* imageBounds, Image* image ){ if ( (imageBounds->getWidth() > image->getWidth()) || (imageBounds->getHeight() > image->getHeight()) ) { throw BasicException( MAKE_ERROR_MSG("Invalid image bounds requested"), __LINE__); } OSXImage* osXimage = (OSXImage*)(image); ulong32 imgBoundsWidth = (ulong32)imageBounds->getWidth(); ulong32 imgBoundsHeight = (ulong32)imageBounds->getHeight(); if ( (imgBoundsWidth == image->getWidth()) && (imgBoundsHeight == image->getHeight()) ) { CGImageRef imgRef = osXimage->getCGImage(); CGRect imgBounds; imgBounds.origin.x = x + origin_.x_; imgBounds.origin.y = y + origin_.y_; imgBounds.size.width = imgBoundsWidth; imgBounds.size.height = imgBoundsHeight; CGContextDrawImage( contextID_, imgBounds, imgRef ); } else { //create a smaller portion of the image int componentCount = image->getType(); int bitsPerPix = image->getChannelSize() * componentCount; int bitsPerComponent = image->getChannelSize(); int rowStride = ((imgBoundsWidth * bitsPerComponent * componentCount) + 7)/ 8; CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); SysPixelType* data = new SysPixelType[imgBoundsWidth*imgBoundsHeight]; ulong32 imgSize = imgBoundsWidth * imgBoundsHeight * componentCount; CGDataProviderRef provider = CGDataProviderCreateWithData( NULL, (char*)data, imgSize, NULL ); CGImageRef imgRef = CGImageCreate( imgBoundsWidth, imgBoundsHeight, image->getChannelSize(), bitsPerPix, rowStride, colorSpace, kCGImageAlphaNone, provider, NULL, FALSE, kCGRenderingIntentDefault ); //copy over bits SysPixelType* imageBuf = image->getImageBits()->pixels_; SysPixelType* tmpBmpBuf = data; ulong32 incr = (ulong32)((imageBounds->top_ * image->getWidth()) + imageBounds->left_); imageBuf += incr; ulong32 imgWidth = image->getWidth(); int s = (int)imageBounds->top_; int e = (int)imageBounds->bottom_; for (int y1=s;y1<e;y1++) { memcpy( tmpBmpBuf, imageBuf, imgBoundsWidth*componentCount ); tmpBmpBuf += imgBoundsWidth; imageBuf += imgWidth; } CGRect imgBounds; imgBounds.origin.x = x + origin_.x_; imgBounds.origin.y = y + origin_.y_; imgBounds.size.width = imgBoundsWidth; imgBounds.size.height = imgBoundsHeight; CGContextDrawImage( contextID_, imgBounds, imgRef ); CGColorSpaceRelease(colorSpace); CGDataProviderRelease(provider); CGImageRelease( imgRef ); delete [] data; }}void OSXContext::checkHandle(){}void OSXContext::releaseHandle(){}bool OSXContext::isXORModeOn(){ return xorModeOn_;}void OSXContext::setXORModeOn( const bool& XORModeOn ){ endLastPrimitive(); xorModeOn_ = XORModeOn;}void OSXContext::setTextAlignment( const bool& alignTobaseline ){}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?