API 0.9.5
AppKit/CoreGraphics/CGContext.j
Go to the documentation of this file.
00001 /*
00002  * CGContext.j
00003  * AppKit
00004  *
00005  * Created by Francisco Tolmasky.
00006  * Copyright 2008, 280 North, Inc.
00007  *
00008  * This library is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Lesser General Public
00010  * License as published by the Free Software Foundation; either
00011  * version 2.1 of the License, or (at your option) any later version.
00012  *
00013  * This library is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with this library; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00021  */
00022 
00023 
00024 
00025 kCGLineCapButt              = 0;
00026 kCGLineCapRound             = 1;
00027 kCGLineCapSquare            = 2;
00028 
00029 kCGLineJoinMiter            = 0;
00030 kCGLineJoinRound            = 1;
00031 kCGLineJoinBevel            = 2;
00032 
00033 kCGPathFill                 = 0;
00034 kCGPathEOFill               = 1;
00035 kCGPathStroke               = 2;
00036 kCGPathFillStroke           = 3;
00037 kCGPathEOFillStroke         = 4;
00038 
00043 kCGBlendModeNormal          = 0;
00044 kCGBlendModeMultiply        = 1;
00045 kCGBlendModeScreen          = 2;
00046 kCGBlendModeOverlay         = 3;
00047 kCGBlendModeDarken          = 4;
00048 kCGBlendModeLighten         = 5;
00049 kCGBlendModeColorDodge      = 6;
00050 kCGBlendModeColorBurn       = 7;
00051 kCGBlendModeSoftLight       = 8;
00052 kCGBlendModeHardLight       = 9;
00053 kCGBlendModeDifference      = 10;
00054 kCGBlendModeExclusion       = 11;
00055 kCGBlendModeHue             = 12;
00056 kCGBlendModeSaturation      = 13;
00057 kCGBlendModeColor           = 14;
00058 kCGBlendModeLuminosity      = 15;
00059 kCGBlendModeClear           = 16;
00060 kCGBlendModeCopy            = 17;
00061 kCGBlendModeSourceIn        = 18;
00062 kCGBlendModeSourceOut       = 19;
00063 kCGBlendModeSourceAtop      = 20;
00064 kCGBlendModeDestinationOver = 21;
00065 kCGBlendModeDestinationIn   = 22;
00066 kCGBlendModeDestinationOut  = 23;
00067 kCGBlendModeDestinationAtop = 24;
00068 kCGBlendModeXOR             = 25;
00069 kCGBlendModePlusDarker      = 26;
00070 kCGBlendModePlusLighter     = 27;
00071 
00082 function CGContextRelease()
00083 {
00084 }
00085 
00092 function CGContextRetain(aContext)
00093 {
00094     return aContext;
00095 }
00096 
00100 // BEGIN CANVAS IF
00101 if (!CPFeatureIsCompatible(CPHTMLCanvasFeature))
00102 {
00111 function CGGStateCreate()
00112 {
00113     return { alpha:1.0, strokeStyle:"#000", fillStyle:"#ccc", lineWidth:1.0, lineJoin:kCGLineJoinMiter, lineCap:kCGLineCapButt, miterLimit:10.0, globalAlpha:1.0,
00114         blendMode:kCGBlendModeNormal,
00115         shadowOffset:_CGSizeMakeZero(), shadowBlur:0.0, shadowColor:NULL, CTM:_CGAffineTransformMakeIdentity() };
00116 }
00117 
00123 function CGGStateCreateCopy(aGState)
00124 {
00125     return { alpha:aGState.alpha, strokeStyle:aGState.strokeStyle, fillStyle:aGState.fillStyle, lineWidth:aGState.lineWidth,
00126         lineJoin:aGState.lineJoin, lineCap:aGState.lineCap, miterLimit:aGState.miterLimit, globalAlpha:aGState.globalAlpha,
00127         blendMode:aGState.blendMode,
00128         shadowOffset:_CGSizeMakeCopy(aGState.shadowOffset), shadowBlur:aGState.shadowBlur, shadowColor:aGState.shadowColor, CTM:_CGAffineTransformMakeCopy(aGState.CTM) };
00129 }
00130 
00135 function CGBitmapGraphicsContextCreate()
00136 {
00137     return { DOMElement:document.createElement("div"), path:NULL, gState:CGGStateCreate(), gStateStack:[] };
00138 }
00139 
00145 function CGContextSaveGState(aContext)
00146 {
00147     aContext.gStateStack.push(CGGStateCreateCopy(aContext.gState));
00148 }
00149 
00155 function CGContextRestoreGState(aContext)
00156 {
00157     aContext.gState = aContext.gStateStack.pop();
00158 }
00159 
00160 function CGContextSetLineCap(aContext, aLineCap)
00161 {
00162     aContext.gState.lineCap = aLineCap;
00163 }
00164 
00165 function CGContextSetLineJoin(aContext, aLineJoin)
00166 {
00167     aContext.gState.lineJoin = aLineJoin;
00168 }
00169 
00170 function CGContextSetLineWidth(aContext, aLineWidth)
00171 {
00172     aContext.gState.lineWidth = aLineWidth;
00173 }
00174 
00175 function CGContextSetMiterLimit(aContext, aMiterLimit)
00176 {
00177     aContext.gState.miterLimit = aMiterLimit;
00178 }
00179 
00180 function CGContextSetBlendMode(aContext, aBlendMode)
00181 {
00182     aContext.gState.blendMode = aBlendMode;
00183 }
00184 
00185 function CGContextAddArc(aContext, x, y, radius, startAngle, endAngle, clockwise)
00186 {
00187     CGPathAddArc(aContext.path, aContext.gState.CTM, x, y, radius, startAngle, endAngle, clockwise);
00188 }
00189 
00200 function CGContextAddArcToPoint(aContext, x1, y1, x2, y2, radius)
00201 {
00202     CGPathAddArcToPoint(aContext.path, aContext.gState.CTM, x1, y1, x2, y2, radius);
00203 }
00204 
00216 function CGContextAddCurveToPoint(aContext, cp1x, cp1y, cp2x, cp2y, x, y)
00217 {
00218     CGPathAddCurveToPoint(aContext.path, aContext.gState.CTM, cp1x, cp1y, cp2x, cp2y, x, y);
00219 }
00220 
00228 function CGContextAddLines(aContext, points, count)
00229 {
00230     CGPathAddLines(aContext.path, aContext.gState.CTM, points, count);
00231 }
00232 
00240 function CGContextAddLineToPoint(aContext, x, y)
00241 {
00242     CGPathAddLineToPoint(aContext.path, aContext.gState.CTM, x, y);
00243 }
00244 
00251 function CGContextAddPath(aContext, aPath)
00252 {
00253     if (!aContext || CGPathIsEmpty(aPath))
00254         return;
00255 
00256     if (!aContext.path)
00257         aContext.path = CGPathCreateMutable();
00258 
00259     CGPathAddPath(aContext.path, aContext.gState.CTM, aPath);
00260 }
00261 
00271 function CGContextAddQuadCurveToPoint(aContext, cpx, cpy, x, y)
00272 {
00273     CGPathAddQuadCurveToPoint(aContext.path, aContext.gState.CTM, cpx, cpy, x, y);
00274 }
00275 
00282 function CGContextAddRect(aContext, aRect)
00283 {
00284     CGPathAddRect(aContext.path, aContext.gState.CTM, aRect);
00285 }
00286 
00294 function CGContextAddRects(aContext, rects, count)
00295 {
00296     CGPathAddRects(aContext.path, aContext.gState.CTM, rects, count);
00297 }
00298 
00304 function CGContextBeginPath(aContext)
00305 {
00306     // This clears any previous path.
00307     aContext.path = CGPathCreateMutable();
00308 }
00309 
00315 function CGContextClosePath(aContext)
00316 {
00317     CGPathCloseSubpath(aContext.path);
00318 }
00319 
00327 function CGContextMoveToPoint(aContext, x, y)
00328 {
00329     if (!aContext.path)
00330         aContext.path = CGPathCreateMutable();
00331 
00332     CGPathMoveToPoint(aContext.path, aContext.gState.CTM, x, y);
00333 }
00334 
00341 function CGContextFillRect(aContext, aRect)
00342 {
00343     CGContextFillRects(aContext, [aRect], 1);
00344 }
00345 
00353 function CGContextFillRects(aContext, rects, count)
00354 {
00355     if (arguments[2] === undefined)
00356         var count = rects.length;
00357 
00358     CGContextBeginPath(aContext);
00359     CGContextAddRects(aContext, rects, count);
00360     CGContextClosePath(aContext);
00361 
00362     CGContextDrawPath(aContext, kCGPathFill);
00363 }
00364 
00371 function CGContextStrokeRect(aContext, aRect)
00372 {
00373     CGContextBeginPath(aContext);
00374     CGContextAddRect(aContext, aRect);
00375     CGContextClosePath(aContext);
00376 
00377     CGContextDrawPath(aContext, kCGPathStroke);
00378 }
00379 
00387 function CGContextStrokeRectWithWidth(aContext, aRect, aWidth)
00388 {
00389     CGContextSaveGState(aContext);
00390 
00391     CGContextSetLineWidth(aContext, aWidth);
00392     CGContextStrokeRect(aContext, aRect);
00393 
00394     CGContextRestoreGState(aContext);
00395 }
00396 
00403 function CGContextConcatCTM(aContext, aTransform)
00404 {
00405     var CTM = aContext.gState.CTM;
00406 
00407     _CGAffineTransformConcatTo(CTM, aTransform, CTM);
00408 }
00409 
00415 function CGContextGetCTM(aContext)
00416 {
00417     return aContext.gState.CTM;
00418 }
00419 
00427 function CGContextRotateCTM(aContext, anAngle)
00428 {
00429     var gState = aContext.gState;
00430 
00431     gState.CTM = CGAffineTransformRotate(gState.CTM, anAngle);
00432 }
00433 
00441 function CGContextScaleCTM(aContext, sx, sy)
00442 {
00443     var gState = aContext.gState;
00444 
00445     gState.CTM = _CGAffineTransformScale(gState.CTM, sx, sy);
00446 }
00447 
00455 function CGContextTranslateCTM(aContext, tx, ty)
00456 {
00457     var gState = aContext.gState;
00458 
00459     gState.CTM = _CGAffineTransformTranslate(gState.CTM, tx, ty);
00460 }
00461 
00470 function CGContextSetShadow(aContext, aSize, aBlur)
00471 {
00472     var gState = aContext.gState;
00473 
00474     gState.shadowOffset = _CGSizeMakeCopy(aSize);
00475     gState.shadowBlur = aBlur;
00476     gState.shadowColor = [CPColor shadowColor];
00477 }
00478 
00487 function CGContextSetShadowWithColor(aContext, aSize, aBlur, aColor)
00488 {
00489     var gState = aContext.gState;
00490 
00491     gState.shadowOffset = _CGSizeMakeCopy(aSize);
00492     gState.shadowBlur = aBlur;
00493     gState.shadowColor = aColor;
00494 }
00495 
00502 function CGContextSetAlpha(aContext, anAlpha)
00503 {
00504     aContext.gState.alpha = MAX(MIN(anAlpha, 1.0), 0.0);
00505 }
00506 
00510 }   // END CANVAS IF
00515 // GOOD.
00521 function CGContextEOFillPath(aContext)
00522 {
00523     CGContextDrawPath(aContext, kCGPathEOFill);
00524 }
00525 
00531 function CGContextFillPath(aContext)
00532 {
00533     CGContextDrawPath(aContext, kCGPathFill);
00534     CGContextClosePath(aContext);
00535 }
00536 
00537 var KAPPA = 4.0 * ((SQRT2 - 1.0) / 3.0);
00538 
00545 function CGContextAddEllipseInRect(aContext, aRect)
00546 {
00547     CGContextBeginPath(aContext);
00548     CGContextAddPath(aContext, CGPathWithEllipseInRect(aRect));
00549     CGContextClosePath(aContext);
00550 }
00551 
00558 function CGContextFillEllipseInRect(aContext, aRect)
00559 {
00560     CGContextBeginPath(aContext);
00561     CGContextAddEllipseInRect(aContext, aRect);
00562     CGContextClosePath(aContext);
00563     CGContextFillPath(aContext);
00564 }
00565 
00572 function CGContextStrokeEllipseInRect(aContext, aRect)
00573 {
00574     CGContextBeginPath(aContext);
00575     CGContextAddEllipseInRect(aContext, aRect);
00576     CGContextClosePath(aContext);
00577     CGContextStrokePath(aContext);
00578 }
00579 
00585 function CGContextStrokePath(aContext)
00586 {
00587     CGContextDrawPath(aContext, kCGPathStroke);
00588     CGContextClosePath(aContext);
00589 }
00590 
00601 function CGContextStrokeLineSegments(aContext, points, count)
00602 {
00603     var i = 0;
00604 
00605     if (count === NULL)
00606         var count = points.length;
00607 
00608     CGContextBeginPath(aContext);
00609 
00610     for (; i < count; i += 2)
00611     {
00612         CGContextMoveToPoint(aContext, points[i].x, points[i].y);
00613         CGContextAddLineToPoint(aContext, points[i + 1].x, points[i + 1].y);
00614     }
00615 
00616     CGContextStrokePath(aContext);
00617 }
00618 
00619 
00620 //FIXME: THIS IS WRONG!!!
00621 
00629 function CGContextSetFillColor(aContext, aColor)
00630 {
00631     if (aColor)
00632         aContext.gState.fillStyle = [aColor cssString];
00633 }
00634 
00641 function CGContextSetStrokeColor(aContext, aColor)
00642 {
00643     if (aColor)
00644         aContext.gState.strokeStyle = [aColor cssString];
00645 }
00646 
00658 function CGContextFillRoundedRectangleInRect(aContext, aRect, aRadius, ne, se, sw, nw)
00659 {
00660     CGContextBeginPath(aContext);
00661     CGContextAddPath(aContext, CGPathWithRoundedRectangleInRect(aRect, aRadius, aRadius, ne, se, sw, nw));
00662     CGContextClosePath(aContext);
00663     CGContextFillPath(aContext);
00664 }
00665 
00677 function CGContextStrokeRoundedRectangleInRect(aContext, aRect, aRadius, ne, se, sw, nw)
00678 {
00679     CGContextBeginPath(aContext);
00680     CGContextAddPath(aContext, CGPathWithRoundedRectangleInRect(aRect, aRadius, aRadius, ne, se, sw, nw));
00681     CGContextClosePath(aContext);
00682     CGContextStrokePath(aContext);
00683 }
00684 
00692 if (CPFeatureIsCompatible(CPHTMLCanvasFeature))
00693 {
00694 #include "CGContextCanvas.j"
00695 }
00696 else if (CPFeatureIsCompatible(CPVMLFeature))
00697 {
00698 #include "CGContextVML.j"
00699 }
 All Classes Files Functions Variables Defines