00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "CGGeometry.h"
00024 #include "CGAffineTransform.h"
00025
00026 @import "CGGeometry.j"
00027 @import "CGAffineTransform.j"
00028 @import "CGPath.j"
00029
00030 kCGLineCapButt = 0;
00031 kCGLineCapRound = 1;
00032 kCGLineCapSquare = 2;
00033
00034 kCGLineJoinMiter = 0;
00035 kCGLineJoinRound = 1;
00036 kCGLineJoinBevel = 2;
00037
00038 kCGPathFill = 0;
00039 kCGPathEOFill = 1;
00040 kCGPathStroke = 2;
00041 kCGPathFillStroke = 3;
00042 kCGPathEOFillStroke = 4;
00043
00048 kCGBlendModeNormal = 0;
00049 kCGBlendModeMultiply = 1;
00050 kCGBlendModeScreen = 2;
00051 kCGBlendModeOverlay = 3;
00052 kCGBlendModeDarken = 4;
00053 kCGBlendModeLighten = 5;
00054 kCGBlendModeColorDodge = 6;
00055 kCGBlendModeColorBurn = 7;
00056 kCGBlendModeSoftLight = 8;
00057 kCGBlendModeHardLight = 9;
00058 kCGBlendModeDifference = 10;
00059 kCGBlendModeExclusion = 11;
00060 kCGBlendModeHue = 12;
00061 kCGBlendModeSaturation = 13;
00062 kCGBlendModeColor = 14;
00063 kCGBlendModeLuminosity = 15;
00064 kCGBlendModeClear = 16;
00065 kCGBlendModeCopy = 17;
00066 kCGBlendModeSourceIn = 18;
00067 kCGBlendModeSourceOut = 19;
00068 kCGBlendModeSourceAtop = 20;
00069 kCGBlendModeDestinationOver = 21;
00070 kCGBlendModeDestinationIn = 22;
00071 kCGBlendModeDestinationOut = 23;
00072 kCGBlendModeDestinationAtop = 24;
00073 kCGBlendModeXOR = 25;
00074 kCGBlendModePlusDarker = 26;
00075 kCGBlendModePlusLighter = 27;
00076
00087 function CGContextRelease()
00088 {
00089 }
00090
00097 function CGContextRetain(aContext)
00098 {
00099 return aContext;
00100 }
00101
00105
00106 if (!CPFeatureIsCompatible(CPHTMLCanvasFeature))
00107 {
00116 function CGGStateCreate()
00117 {
00118 return { alpha:1.0, strokeStyle:"#000", fillStyle:"#ccc", lineWidth:1.0, lineJoin:kCGLineJoinMiter, lineCap:kCGLineCapButt, miterLimit:10.0, globalAlpha:1.0,
00119 blendMode:kCGBlendModeNormal,
00120 shadowOffset:_CGSizeMakeZero(), shadowBlur:0.0, shadowColor:NULL, CTM:_CGAffineTransformMakeIdentity() };
00121 }
00122
00128 function CGGStateCreateCopy(aGState)
00129 {
00130 return { alpha:aGState.alpha, strokeStyle:aGState.strokeStyle, fillStyle:aGState.fillStyle, lineWidth:aGState.lineWidth,
00131 lineJoin:aGState.lineJoin, lineCap:aGState.lineCap, miterLimit:aGState.miterLimit, globalAlpha:aGState.globalAlpha,
00132 blendMode:aGState.blendMode,
00133 shadowOffset:aGState.shadowOffset, shadowBlur:aGState.shadowBlur, shadowColor:aGState.shadowColor, CTM:_CGAffineTransformMakeCopy(aGState.CTM) };
00134 }
00135
00140 function CGBitmapGraphicsContextCreate()
00141 {
00142 return { DOMElement:document.createElement("div"), path:NULL, gState:CGGStateCreate(), gStateStack:[] };
00143 }
00144
00150 function CGContextSaveGState(aContext)
00151 {
00152 aContext.gStateStack.push(CGGStateCreateCopy(aContext.gState));
00153 }
00154
00160 function CGContextRestoreGState(aContext)
00161 {
00162 aContext.gState = aContext.gStateStack.pop();
00163 }
00164
00165 function CGContextSetLineCap(aContext, aLineCap)
00166 {
00167 aContext.gState.lineCap = aLineCap;
00168 }
00169
00170 function CGContextSetLineJoin(aContext, aLineJoin)
00171 {
00172 aContext.gState.lineJoin = aLineJoin;
00173 }
00174
00175 function CGContextSetLineWidth(aContext, aLineWidth)
00176 {
00177 aContext.gState.lineWidth = aLineWidth;
00178 }
00179
00180 function CGContextSetMiterLimit(aContext, aMiterLimit)
00181 {
00182 aContext.gState.miterLimit = aMiterLimit;
00183 }
00184
00185 function CGContextSetBlendMode(aContext, aBlendMode)
00186 {
00187 aContext.gState.blendMode = aBlendMode;
00188 }
00189
00190 function CGContextAddArc(aContext, x, y, radius, startAngle, endAngle, clockwise)
00191 {
00192 CGPathAddArc(aContext.path, aContext.gState.CTM, x, y, radius, startAngle, endAngle, clockwise);
00193 }
00194
00205 function CGContextAddArcToPoint(aContext, x1, y1, x2, y2, radius)
00206 {
00207 CGPathAddArcToPoint(aContext.path, aContext.gState.CTM, x1, y1, x2, y2, radius);
00208 }
00209
00221 function CGContextAddCurveToPoint(aContext, cp1x, cp1y, cp2x, cp2y, x, y)
00222 {
00223 CGPathAddCurveToPoint(aContext.path, aContext.gState.CTM, cp1x, cp1y, cp2x, cp2y, x, y);
00224 }
00225
00233 function CGContextAddLines(aContext, points, count)
00234 {
00235 CGPathAddLines(aContext.path, aContext.gState.CTM, points, count);
00236 }
00237
00245 function CGContextAddLineToPoint(aContext, x, y)
00246 {
00247 CGPathAddLineToPoint(aContext.path, aContext.gState.CTM, x, y);
00248 }
00249
00256 function CGContextAddPath(aContext, aPath)
00257 {
00258 if (!aContext || CGPathIsEmpty(aPath))
00259 return;
00260
00261 if (!aContext.path)
00262 aContext.path = CGPathCreateMutable();
00263
00264 CGPathAddPath(aContext.path, aContext.gState.CTM, aPath);
00265 }
00266
00276 function CGContextAddQuadCurveToPoint(aContext, cpx, cpy, x, y)
00277 {
00278 CGPathAddQuadCurveToPoint(aContext.path, aContext.gState.CTM, cpx, cpy, x, y);
00279 }
00280
00287 function CGContextAddRect(aContext, aRect)
00288 {
00289 CGPathAddRect(aContext.path, aContext.gState.CTM, aRect);
00290 }
00291
00299 function CGContextAddRects(aContext, rects, count)
00300 {
00301 CGPathAddRects(aContext.path, aContext.gState.CTM, rects, count);
00302 }
00303
00309 function CGContextBeginPath(aContext)
00310 {
00311
00312 aContext.path = CGPathCreateMutable();
00313 }
00314
00320 function CGContextClosePath(aContext)
00321 {
00322 CGPathCloseSubpath(aContext.path);
00323 }
00324
00332 function CGContextMoveToPoint(aContext, x, y)
00333 {
00334 if (!aContext.path)
00335 aContext.path = CGPathCreateMutable();
00336
00337 CGPathMoveToPoint(aContext.path, aContext.gState.CTM, x, y);
00338 }
00339
00346 function CGContextFillRect(aContext, aRect)
00347 {
00348 CGContextFillRects(aContext, [aRect], 1);
00349 }
00350
00358 function CGContextFillRects(aContext, rects, count)
00359 {
00360 if (arguments[2] === undefined)
00361 var count = rects.length;
00362
00363 CGContextBeginPath(aContext);
00364 CGContextAddRects(aContext, rects, count);
00365 CGContextClosePath(aContext);
00366
00367 CGContextDrawPath(aContext, kCGPathFill);
00368 }
00369
00376 function CGContextStrokeRect(aContext, aRect)
00377 {
00378 CGContextBeginPath(aContext);
00379 CGContextAddRect(aContext, aRect);
00380 CGContextClosePath(aContext);
00381
00382 CGContextDrawPath(aContext, kCGPathStroke);
00383 }
00384
00392 function CGContextStrokeRectWithWidth(aContext, aRect, aWidth)
00393 {
00394 CGContextSaveGState(aContext);
00395
00396 CGContextSetLineWidth(aContext, aWidth);
00397 CGContextStrokeRect(aContext, aRect);
00398
00399 CGContextRestoreGState(aContext);
00400 }
00401
00408 function CGContextConcatCTM(aContext, aTransform)
00409 {
00410 var CTM = aContext.gState.CTM;
00411
00412 _CGAffineTransformConcatTo(CTM, aTransform, CTM);
00413 }
00414
00420 function CGContextGetCTM(aContext)
00421 {
00422 return aContext.gState.CTM;
00423 }
00424
00432 function CGContextRotateCTM(aContext, anAngle)
00433 {
00434 var gState = aContext.gState;
00435
00436 gState.CTM = CGAffineTransformRotate(gState.CTM, anAngle);
00437 }
00438
00446 function CGContextScaleCTM(aContext, sx, sy)
00447 {
00448 var gState = aContext.gState;
00449
00450 gState.CTM = _CGAffineTransformScale(gState.CTM, sx, sy);
00451 }
00452
00460 function CGContextTranslateCTM(aContext, tx, ty)
00461 {
00462 var gState = aContext.gState;
00463
00464 gState.CTM = _CGAffineTransformTranslate(gState.CTM, tx, ty);
00465 }
00466
00475 function CGContextSetShadow(aContext, aSize, aBlur)
00476 {
00477 var gState = aContext.gState;
00478
00479 gState.shadowOffset = _CGSizeMakeCopy(aSize);
00480 gState.shadowBlur = aBlur;
00481 gState.shadowColor = [CPColor shadowColor];
00482 }
00483
00492 function CGContextSetShadowWithColor(aContext, aSize, aBlur, aColor)
00493 {
00494 var gState = aContext.gState;
00495
00496 gState.shadowOffset = _CGSizeMakeCopy(aSize);
00497 gState.shadowBlur = aBlur;
00498 gState.shadowColor = aColor;
00499 }
00500
00507 function CGContextSetAlpha(aContext, anAlpha)
00508 {
00509 aContext.gState.alpha = MAX(MIN(anAlpha, 1.0), 0.0);
00510 }
00511
00515 }
00520
00526 function CGContextEOFillPath(aContext)
00527 {
00528 CGContextDrawPath(aContext, kCGPathEOFill);
00529 }
00530
00536 function CGContextFillPath(aContext)
00537 {
00538 CGContextDrawPath(aContext, kCGPathFill);
00539 }
00540
00541 var KAPPA = 4.0 * ((SQRT2 - 1.0) / 3.0);
00542
00549 function CGContextAddEllipseInRect(aContext, aRect)
00550 {
00551 CGContextBeginPath(aContext);
00552 CGContextAddPath(aContext, CGPathWithEllipseInRect(aRect));
00553 CGContextClosePath(aContext);
00554 }
00555
00562 function CGContextFillEllipseInRect(aContext, aRect)
00563 {
00564 CGContextBeginPath(aContext);
00565 CGContextAddEllipseInRect(aContext, aRect);
00566 CGContextClosePath(aContext);
00567 CGContextFillPath(aContext);
00568 }
00569
00576 function CGContextStrokeEllipseInRect(aContext, aRect)
00577 {
00578 CGContextBeginPath(aContext);
00579 CGContextAddEllipseInRect(aContext, aRect);
00580 CGContextClosePath(aContext);
00581 CGContextStrokePath(aContext);
00582 }
00583
00589 function CGContextStrokePath(aContext)
00590 {
00591 CGContextDrawPath(aContext, kCGPathStroke);
00592 }
00593
00604 function CGContextStrokeLineSegments(aContext, points, count)
00605 {
00606 var i = 0;
00607
00608 if (arguments["count"] == NULL)
00609 var count = points.length;
00610
00611 CGContextBeginPath(aContext);
00612
00613 for (; i < count; i += 2)
00614 {
00615 CGContextMoveToPoint(aContext, points[i].x, points[i].y);
00616 CGContextAddLineToPoint(aContext, points[i + 1].x, points[i + 1].y);
00617 }
00618
00619 CGContextStrokePath(aContext);
00620 }
00621
00622
00623
00624
00632 function CGContextSetFillColor(aContext, aColor)
00633 {
00634 if (aColor)
00635 aContext.gState.fillStyle = [aColor cssString];
00636 }
00637
00644 function CGContextSetStrokeColor(aContext, aColor)
00645 {
00646 if (aColor)
00647 aContext.gState.strokeStyle = [aColor cssString];
00648 }
00649
00661 function CGContextFillRoundedRectangleInRect(aContext, aRect, aRadius, ne, se, sw, nw)
00662 {
00663 CGContextBeginPath(aContext);
00664 CGContextAddPath(aContext, CGPathWithRoundedRectangleInRect(aRect, aRadius, aRadius, ne, se, sw, nw));
00665 CGContextClosePath(aContext);
00666 CGContextFillPath(aContext);
00667 }
00668
00680 function CGContextStrokeRoundedRectangleInRect(aContext, aRect, aRadius, ne, se, sw, nw)
00681 {
00682 CGContextBeginPath(aContext);
00683 CGContextAddPath(aContext, CGPathWithRoundedRectangleInRect(aRect, aRadius, aRadius, ne, se, sw, nw));
00684 CGContextClosePath(aContext);
00685 CGContextStrokePath(aContext);
00686 }
00687
00695 if (CPFeatureIsCompatible(CPHTMLCanvasFeature))
00696 {
00697 #include "CGContextCanvas.j"
00698 }
00699 else if (CPFeatureIsCompatible(CPVMLFeature))
00700 {
00701 #include "CGContextVML.j"
00702 }