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
00044
00045
00046
00047
00048 kCGBlendModeNormal = 0;
00049
00050
00051
00052
00053 kCGBlendModeMultiply = 1;
00054
00055
00056
00057
00058 kCGBlendModeScreen = 2;
00059
00060
00061
00062
00063 kCGBlendModeOverlay = 3;
00064
00065
00066
00067
00068 kCGBlendModeDarken = 4;
00069
00070
00071
00072
00073 kCGBlendModeLighten = 5;
00074
00075
00076
00077
00078 kCGBlendModeColorDodge = 6;
00079
00080
00081
00082
00083 kCGBlendModeColorBurn = 7;
00084
00085
00086
00087
00088 kCGBlendModeSoftLight = 8;
00089
00090
00091
00092
00093 kCGBlendModeHardLight = 9;
00094
00095
00096
00097
00098 kCGBlendModeDifference = 10;
00099
00100
00101
00102
00103 kCGBlendModeExclusion = 11;
00104
00105
00106
00107
00108 kCGBlendModeHue = 12;
00109
00110
00111
00112
00113 kCGBlendModeSaturation = 13;
00114
00115
00116
00117
00118 kCGBlendModeColor = 14;
00119
00120
00121
00122
00123 kCGBlendModeLuminosity = 15;
00124
00125
00126
00127
00128 kCGBlendModeClear = 16;
00129
00130
00131
00132
00133 kCGBlendModeCopy = 17;
00134
00135
00136
00137
00138 kCGBlendModeSourceIn = 18;
00139
00140
00141
00142
00143 kCGBlendModeSourceOut = 19;
00144
00145
00146
00147
00148 kCGBlendModeSourceAtop = 20;
00149
00150
00151
00152
00153 kCGBlendModeDestinationOver = 21;
00154
00155
00156
00157
00158 kCGBlendModeDestinationIn = 22;
00159
00160
00161
00162
00163 kCGBlendModeDestinationOut = 23;
00164
00165
00166
00167
00168 kCGBlendModeDestinationAtop = 24;
00169
00170
00171
00172
00173 kCGBlendModeXOR = 25;
00174
00175
00176
00177
00178 kCGBlendModePlusDarker = 26;
00179
00180
00181
00182
00183 kCGBlendModePlusLighter = 27;
00184
00190 function CGContextRelease()
00191 {
00192 }
00193
00200 function CGContextRetain(aContext)
00201 {
00202 return aContext;
00203 }
00204
00205
00206 if (!CPFeatureIsCompatible(CPHTMLCanvasFeature))
00207 {
00208
00209 function CGGStateCreate()
00210 {
00211 return { alpha:1.0, strokeStyle:"#000", fillStyle:"#ccc", lineWidth:1.0, lineJoin:kCGLineJoinMiter, lineCap:kCGLineCapButt, miterLimit:10.0, globalAlpha:1.0,
00212 blendMode:kCGBlendModeNormal,
00213 shadowOffset:_CGSizeMakeZero(), shadowBlur:0.0, shadowColor:NULL, CTM:_CGAffineTransformMakeIdentity() };
00214 }
00215
00216 function CGGStateCreateCopy(aGState)
00217 {
00218 return { alpha:aGState.alpha, strokeStyle:aGState.strokeStyle, fillStyle:aGState.fillStyle, lineWidth:aGState.lineWidth,
00219 lineJoin:aGState.lineJoin, lineCap:aGState.lineCap, miterLimit:aGState.miterLimit, globalAlpha:aGState.globalAlpha,
00220 blendMode:aGState.blendMode,
00221 shadowOffset:aGState.shadowOffset, shadowBlur:aGState.shadowBlur, shadowColor:aGState.shadowColor, CTM:_CGAffineTransformMakeCopy(aGState.CTM) };
00222 }
00223
00224 function CGBitmapGraphicsContextCreate()
00225 {
00226 return { DOMElement:document.createElement("div"), path:NULL, gState:CGGStateCreate(), gStateStack:[] };
00227 }
00228
00229 function CGContextSaveGState(aContext)
00230 {
00231 aContext.gStateStack.push(CGGStateCreateCopy(aContext.gState));
00232 }
00233
00234 function CGContextRestoreGState(aContext)
00235 {
00236 aContext.gState = aContext.gStateStack.pop();
00237 }
00238
00239 function CGContextSetLineCap(aContext, aLineCap)
00240 {
00241 aContext.gState.lineCap = aLineCap;
00242 }
00243
00244 function CGContextSetLineJoin(aContext, aLineJoin)
00245 {
00246 aContext.gState.lineJoin = aLineJoin;
00247 }
00248
00249 function CGContextSetLineWidth(aContext, aLineWidth)
00250 {
00251 aContext.gState.lineWidth = aLineWidth;
00252 }
00253
00254 function CGContextSetMiterLimit(aContext, aMiterLimit)
00255 {
00256 aContext.gState.miterLimit = aMiterLimit;
00257 }
00258
00259 function CGContextSetBlendMode(aContext, aBlendMode)
00260 {
00261 aContext.gState.blendMode = aBlendMode;
00262 }
00263
00264 function CGContextAddArc(aContext, x, y, radius, startAngle, endAngle, clockwise)
00265 {
00266 CGPathAddArc(aContext.path, aContext.gState.CTM, x, y, radius, startAngle, endAngle, clockwise);
00267 }
00268
00269 function CGContextAddArcToPoint(aContext, x1, y1, x2, y2, radius)
00270 {
00271 CGPathAddArcToPoint(aContext.path, aContext.gState.CTM, x1, y1, x2, y2, radius);
00272 }
00273
00274 function CGContextAddCurveToPoint(aContext, cp1x, cp1y, cp2x, cp2y, x, y)
00275 {
00276 CGPathAddCurveToPoint(aContext.path, aContext.gState.CTM, cp1x, cp1y, cp2x, cp2y, x, y);
00277 }
00278
00279 function CGContextAddLines(aContext, points, count)
00280 {
00281 CGPathAddLines(aContext.path, aContext.gState.CTM, points, count);
00282 }
00283
00284 function CGContextAddLineToPoint(aContext, x, y)
00285 {
00286 CGPathAddLineToPoint(aContext.path, aContext.gState.CTM, x, y);
00287 }
00288
00289 function CGContextAddPath(aContext, aPath)
00290 {
00291 if (!aContext || CGPathIsEmpty(aPath))
00292 return;
00293
00294 if (!aContext.path)
00295 aContext.path = CGPathCreateMutable();
00296
00297 CGPathAddPath(aContext.path, aContext.gState.CTM, aPath);
00298 }
00299
00300 function CGContextAddQuadCurveToPoint(aContext, cpx, cpy, x, y)
00301 {
00302 CGPathAddQuadCurveToPoint(aContext.path, aContext.gState.CTM, cpx, cpy, x, y);
00303 }
00304
00305 function CGContextAddRect(aContext, aRect)
00306 {
00307 CGPathAddRect(aContext.path, aContext.gState.CTM, aRect);
00308 }
00309
00310 function CGContextAddRects(aContext, rects, count)
00311 {
00312 CGPathAddRects(aContext.path, aContext.gState.CTM, rects, count);
00313 }
00314
00315 function CGContextBeginPath(aContext)
00316 {
00317
00318 aContext.path = CGPathCreateMutable();
00319 }
00320
00321 function CGContextClosePath(aContext)
00322 {
00323 CGPathCloseSubpath(aContext.path);
00324 }
00325
00326 function CGContextMoveToPoint(aContext, x, y)
00327 {
00328 if (!aContext.path)
00329 aContext.path = CGPathCreateMutable();
00330
00331 CGPathMoveToPoint(aContext.path, aContext.gState.CTM, x, y);
00332 }
00333
00334 function CGContextFillRect(aContext, aRect)
00335 {
00336 CGContextFillRects(aContext, [aRect], 1);
00337 }
00338
00339 function CGContextFillRects(aContext, rects, count)
00340 {
00341 if (arguments["count"] == NULL)
00342 var count = rects.length;
00343
00344 CGContextBeginPath(aContext);
00345 CGContextAddRects(aContext, rects, count);
00346 CGContextClosePath(aContext);
00347
00348 CGContextDrawPath(aContext, kCGPathFill);
00349 }
00350
00351 function CGContextStrokeRect(aContext, aRect)
00352 {
00353 CGContextBeginPath(aContext);
00354 CGContextAddRect(aContext, aRect);
00355 CGContextClosePath(aContext);
00356
00357 CGContextDrawPath(aContext, kCGPathStroke);
00358 }
00359
00360 function CGContextStrokeRectWithWidth(aContext, aRect, aWidth)
00361 {
00362 CGContextSaveGState(aContext);
00363
00364 CGContextSetLineWidth(aContext, aWidth);
00365 CGContextStrokeRect(aContext, aRect);
00366
00367 CGContextRestoreGState(aContext);
00368 }
00369
00370 function CGContextConcatCTM(aContext, aTransform)
00371 {
00372 var CTM = aContext.gState.CTM;
00373
00374 _CGAffineTransformConcatTo(CTM, aTransform, CTM);
00375 }
00376
00377 function CGContextGetCTM(aContext)
00378 {
00379 return aContext.gState.CTM;
00380 }
00381
00382 function CGContextRotateCTM(aContext, anAngle)
00383 {
00384 var gState = aContext.gState;
00385
00386 gState.CTM = CGAffineTransformRotate(gState.CTM, anAngle);
00387 }
00388
00389 function CGContextScaleCTM(aContext, sx, sy)
00390 {
00391 var gState = aContext.gState;
00392
00393 gState.CTM = _CGAffineTransformScale(gState.CTM, sx, sy);
00394 }
00395
00396 function CGContextTranslateCTM(aContext, tx, ty)
00397 {
00398 var gState = aContext.gState;
00399
00400 gState.CTM = _CGAffineTransformTranslate(gState.CTM, tx, ty);
00401 }
00402
00403 function CGContextSetShadow(aContext, aSize, aBlur)
00404 {
00405 var gState = aContext.gState;
00406
00407 gState.shadowOffset = _CGSizeMakeCopy(aSize);
00408 gState.shadowBlur = aBlur;
00409 gState.shadowColor = [CPColor shadowColor];
00410 }
00411
00412 function CGContextSetShadowWithColor(aContext, aSize, aBlur, aColor)
00413 {
00414 var gState = aContext.gState;
00415
00416 gState.shadowOffset = _CGSizeMakeCopy(aSize);
00417 gState.shadowBlur = aBlur;
00418 gState.shadowColor = aColor;
00419 }
00420
00421 function CGContextSetAlpha(aContext, anAlpha)
00422 {
00423 aContext.gState.alpha = MAX(MIN(anAlpha, 1.0), 0.0);
00424 }
00425
00426 }
00427
00428
00435 function CGContextEOFillPath(aContext)
00436 {
00437 CGContextDrawPath(aContext, kCGPathEOFill);
00438 }
00439
00446 function CGContextFillPath(aContext)
00447 {
00448 CGContextDrawPath(aContext, kCGPathFill);
00449 }
00450
00451 var KAPPA = 4.0 * ((SQRT2 - 1.0) / 3.0);
00452
00460 function CGContextAddEllipseInRect(aContext, aRect)
00461 {
00462 CGContextBeginPath(aContext);
00463
00464 if (_CGRectGetWidth(aRect) == _CGRectGetHeight(aRect))
00465 CGContextAddArc(aContext, _CGRectGetMidX(aRect), _CGRectGetMidY(aRect), _CGRectGetWidth(aRect) / 2.0, 0.0, 2 * PI, YES);
00466 else
00467 {
00468 var axis = _CGSizeMake(_CGRectGetWidth(aRect) / 2.0, _CGRectGetHeight(aRect) / 2.0),
00469 center = _CGPointMake(_CGRectGetMinX(aRect) + axis.width, _CGRectGetMinY(aRect) + axis.height);
00470
00471 CGContextMoveToPoint(aContext, center.x, center.y - axis.height);
00472
00473 CGContextAddCurveToPoint(aContext, center.x + (KAPPA * axis.width), center.y - axis.height, center.x + axis.width, center.y - (KAPPA * axis.height), center.x + axis.width, center.y);
00474 CGContextAddCurveToPoint(aContext, center.x + axis.width, center.y + (KAPPA * axis.height), center.x + (KAPPA * axis.width), center.y + axis.height, center.x, center.y + axis.height);
00475 CGContextAddCurveToPoint(aContext, center.x - (KAPPA * axis.width), center.y + axis.height, center.x - axis.width, center.y + (KAPPA * axis.height), center.x - axis.width, center.y);
00476 CGContextAddCurveToPoint(aContext, center.x - axis.width, center.y - (KAPPA * axis.height), center.x - (KAPPA * axis.width), center.y - axis.height, center.x, center.y - axis.height);
00477 }
00478
00479 CGContextClosePath(aContext);
00480 }
00481
00489 function CGContextFillEllipseInRect(aContext, aRect)
00490 {
00491 CGContextAddEllipseInRect(aContext, aRect);
00492 CGContextFillPath(aContext);
00493 }
00494
00502 function CGContextStrokeEllipseInRect(aContext, aRect)
00503 {
00504 CGContextAddEllipseInRect(aContext, aRect);
00505 CGContextStrokePath(aContext);
00506 }
00507
00514 function CGContextStrokePath(aContext)
00515 {
00516 CGContextDrawPath(aContext, kCGPathStroke);
00517 }
00518
00530 function CGContextStrokeLineSegments(aContext, points, count)
00531 {
00532 var i = 0;
00533
00534 if (arguments["count"] == NULL)
00535 var count = points.length;
00536
00537 CGContextBeginPath(aContext);
00538
00539 for (; i < count; i += 2)
00540 {
00541 CGContextMoveToPoint(aContext, points[i].x, points[i].y);
00542 CGContextAddLineToPoint(aContext, points[i + 1].x, points[i + 1].y);
00543 }
00544
00545 CGContextStrokePath(aContext);
00546 }
00547
00548
00549
00550
00559 function CGContextSetFillColor(aContext, aColor)
00560 {
00561 if (aColor)
00562 aContext.gState.fillStyle = [aColor cssString];
00563 }
00564
00572 function CGContextSetStrokeColor(aContext, aColor)
00573 {
00574 if (aColor)
00575 aContext.gState.strokeStyle = [aColor cssString];
00576 }
00577
00590 function CGContextFillRoundedRectangleInRect(aContext, aRect, aRadius, ne, se, sw, nw)
00591 {
00592 var xMin = _CGRectGetMinX(aRect),
00593 xMax = _CGRectGetMaxX(aRect),
00594 yMin = _CGRectGetMinY(aRect),
00595 yMax = _CGRectGetMaxY(aRect);
00596
00597 CGContextBeginPath(aContext);
00598 CGContextMoveToPoint(aContext, xMin + aRadius, yMin);
00599
00600 if (ne)
00601 {
00602 CGContextAddLineToPoint(aContext, xMax - aRadius, yMin);
00603 CGContextAddCurveToPoint(aContext, xMax - aRadius, yMin, xMax, yMin, xMax, yMin + aRadius);
00604 }
00605 else
00606 CGContextAddLineToPoint(aContext, xMax, yMin);
00607
00608 if (se)
00609 {
00610 CGContextAddLineToPoint(aContext, xMax, yMax - aRadius);
00611 CGContextAddCurveToPoint(aContext, xMax, yMax - aRadius, xMax, yMax, xMax - aRadius, yMax);
00612 }
00613 else
00614 CGContextAddLineToPoint(aContext, xMax, yMax);
00615
00616 if (sw)
00617 {
00618 CGContextAddLineToPoint(aContext, xMin + aRadius, yMax);
00619 CGContextAddCurveToPoint(aContext, xMin + aRadius, yMax, xMin, yMax, xMin, yMax - aRadius);
00620 }
00621 else
00622 CGContextAddLineToPoint(aContext, xMin, yMax);
00623
00624 if (nw)
00625 {
00626 CGContextAddLineToPoint(aContext, xMin, yMin + aRadius);
00627 CGContextAddCurveToPoint(aContext, xMin, yMin + aRadius, xMin, yMin, xMin + aRadius, yMin);
00628 } else
00629 CGContextAddLineToPoint(aContext, xMin, yMin);
00630
00631 CGContextClosePath(aContext);
00632
00633 CGContextFillPath(aContext);
00634 }
00635
00636 if (CPFeatureIsCompatible(CPHTMLCanvasFeature))
00637 {
00638 #include "CGContextCanvas.j"
00639 }
00640 else if (CPFeatureIsCompatible(CPVMLFeature))
00641 {
00642 #include "CGContextVML.j"
00643 }