API  0.9.6
 All Classes Files Functions Variables Macros Groups Pages
CGContext.j
Go to the documentation of this file.
1 /*
2  * CGContext.j
3  * AppKit
4  *
5  * Created by Francisco Tolmasky.
6  * Copyright 2008, 280 North, Inc.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 
24 
28 
32 
38 
71 
82 function CGContextRelease()
83 {
84 }
85 
92 function CGContextRetain(aContext)
93 {
94  return aContext;
95 }
96 
100 // BEGIN CANVAS IF
102 {
111 function CGGStateCreate()
112 {
113  return { alpha:1.0, strokeStyle:"#000", fillStyle:"#ccc", lineWidth:1.0, lineJoin:kCGLineJoinMiter, lineCap:kCGLineCapButt, miterLimit:10.0, globalAlpha:1.0,
114  blendMode:kCGBlendModeNormal,
115  shadowOffset:_CGSizeMakeZero(), shadowBlur:0.0, shadowColor:NULL, CTM:_CGAffineTransformMakeIdentity() };
116 }
117 
123 function CGGStateCreateCopy(aGState)
124 {
125  return { alpha:aGState.alpha, strokeStyle:aGState.strokeStyle, fillStyle:aGState.fillStyle, lineWidth:aGState.lineWidth,
126  lineJoin:aGState.lineJoin, lineCap:aGState.lineCap, miterLimit:aGState.miterLimit, globalAlpha:aGState.globalAlpha,
127  blendMode:aGState.blendMode,
128  shadowOffset:_CGSizeMakeCopy(aGState.shadowOffset), shadowBlur:aGState.shadowBlur, shadowColor:aGState.shadowColor, CTM:_CGAffineTransformMakeCopy(aGState.CTM) };
129 }
130 
136 {
137  return { DOMElement:document.createElement("div"), path:NULL, gState:CGGStateCreate(), gStateStack:[] };
138 }
139 
145 function CGContextSaveGState(aContext)
146 {
147  aContext.gStateStack.push(CGGStateCreateCopy(aContext.gState));
148 }
149 
155 function CGContextRestoreGState(aContext)
156 {
157  aContext.gState = aContext.gStateStack.pop();
158 }
159 
160 function CGContextSetLineCap(aContext, aLineCap)
161 {
162  aContext.gState.lineCap = aLineCap;
163 }
164 
165 function CGContextSetLineJoin(aContext, aLineJoin)
166 {
167  aContext.gState.lineJoin = aLineJoin;
168 }
169 
170 function CGContextSetLineWidth(aContext, aLineWidth)
171 {
172  aContext.gState.lineWidth = aLineWidth;
173 }
174 
175 function CGContextSetMiterLimit(aContext, aMiterLimit)
176 {
177  aContext.gState.miterLimit = aMiterLimit;
178 }
179 
180 function CGContextSetBlendMode(aContext, aBlendMode)
181 {
182  aContext.gState.blendMode = aBlendMode;
183 }
184 
185 function CGContextAddArc(aContext, x, y, radius, startAngle, endAngle, clockwise)
186 {
187  CGPathAddArc(aContext.path, aContext.gState.CTM, x, y, radius, startAngle, endAngle, clockwise);
188 }
189 
200 function CGContextAddArcToPoint(aContext, x1, y1, x2, y2, radius)
201 {
202  CGPathAddArcToPoint(aContext.path, aContext.gState.CTM, x1, y1, x2, y2, radius);
203 }
204 
216 function CGContextAddCurveToPoint(aContext, cp1x, cp1y, cp2x, cp2y, x, y)
217 {
218  CGPathAddCurveToPoint(aContext.path, aContext.gState.CTM, cp1x, cp1y, cp2x, cp2y, x, y);
219 }
220 
228 function CGContextAddLines(aContext, points, count)
229 {
230  CGPathAddLines(aContext.path, aContext.gState.CTM, points, count);
231 }
232 
240 function CGContextAddLineToPoint(aContext, x, y)
241 {
242  CGPathAddLineToPoint(aContext.path, aContext.gState.CTM, x, y);
243 }
244 
251 function CGContextAddPath(aContext, aPath)
252 {
253  if (!aContext || CGPathIsEmpty(aPath))
254  return;
255 
256  if (!aContext.path)
257  aContext.path = CGPathCreateMutable();
258 
259  CGPathAddPath(aContext.path, aContext.gState.CTM, aPath);
260 }
261 
271 function CGContextAddQuadCurveToPoint(aContext, cpx, cpy, x, y)
272 {
273  CGPathAddQuadCurveToPoint(aContext.path, aContext.gState.CTM, cpx, cpy, x, y);
274 }
275 
282 function CGContextAddRect(aContext, aRect)
283 {
284  CGPathAddRect(aContext.path, aContext.gState.CTM, aRect);
285 }
286 
294 function CGContextAddRects(aContext, rects, count)
295 {
296  CGPathAddRects(aContext.path, aContext.gState.CTM, rects, count);
297 }
298 
304 function CGContextBeginPath(aContext)
305 {
306  // This clears any previous path.
307  aContext.path = CGPathCreateMutable();
308 }
309 
315 function CGContextClosePath(aContext)
316 {
317  CGPathCloseSubpath(aContext.path);
318 }
319 
325 function CGContextIsPathEmpty(aContext)
326 {
327  return (!aContext.path || CGPathIsEmpty(aContext.path));
328 }
329 
337 function CGContextMoveToPoint(aContext, x, y)
338 {
339  if (!aContext.path)
340  aContext.path = CGPathCreateMutable();
341 
342  CGPathMoveToPoint(aContext.path, aContext.gState.CTM, x, y);
343 }
344 
351 function CGContextFillRect(aContext, aRect)
352 {
353  CGContextFillRects(aContext, [aRect], 1);
354 }
355 
363 function CGContextFillRects(aContext, rects, count)
364 {
365  if (arguments[2] === undefined)
366  var count = rects.length;
367 
368  CGContextBeginPath(aContext);
369  CGContextAddRects(aContext, rects, count);
370  CGContextClosePath(aContext);
371 
372  CGContextDrawPath(aContext, kCGPathFill);
373 }
374 
381 function CGContextStrokeRect(aContext, aRect)
382 {
383  CGContextBeginPath(aContext);
384  CGContextAddRect(aContext, aRect);
385  CGContextClosePath(aContext);
386 
387  CGContextDrawPath(aContext, kCGPathStroke);
388 }
389 
397 function CGContextStrokeRectWithWidth(aContext, aRect, aWidth)
398 {
399  CGContextSaveGState(aContext);
400 
401  CGContextSetLineWidth(aContext, aWidth);
402  CGContextStrokeRect(aContext, aRect);
403 
404  CGContextRestoreGState(aContext);
405 }
406 
413 function CGContextConcatCTM(aContext, aTransform)
414 {
415  var CTM = aContext.gState.CTM;
416 
417  _CGAffineTransformConcatTo(CTM, aTransform, CTM);
418 }
419 
425 function CGContextGetCTM(aContext)
426 {
427  return aContext.gState.CTM;
428 }
429 
437 function CGContextRotateCTM(aContext, anAngle)
438 {
439  var gState = aContext.gState;
440 
441  gState.CTM = CGAffineTransformRotate(gState.CTM, anAngle);
442 }
443 
451 function CGContextScaleCTM(aContext, sx, sy)
452 {
453  var gState = aContext.gState;
454 
455  gState.CTM = _CGAffineTransformScale(gState.CTM, sx, sy);
456 }
457 
465 function CGContextTranslateCTM(aContext, tx, ty)
466 {
467  var gState = aContext.gState;
468 
469  gState.CTM = _CGAffineTransformTranslate(gState.CTM, tx, ty);
470 }
471 
480 function CGContextSetShadow(aContext, aSize, aBlur)
481 {
482  var gState = aContext.gState;
483 
484  gState.shadowOffset = _CGSizeMakeCopy(aSize);
485  gState.shadowBlur = aBlur;
486  gState.shadowColor = [CPColor shadowColor];
487 }
488 
497 function CGContextSetShadowWithColor(aContext, aSize, aBlur, aColor)
498 {
499  var gState = aContext.gState;
500 
501  gState.shadowOffset = _CGSizeMakeCopy(aSize);
502  gState.shadowBlur = aBlur;
503  gState.shadowColor = aColor;
504 }
505 
512 function CGContextSetAlpha(aContext, anAlpha)
513 {
514  aContext.gState.alpha = MAX(MIN(anAlpha, 1.0), 0.0);
515 }
516 
520 } // END CANVAS IF
525 // GOOD.
531 function CGContextEOFillPath(aContext)
532 {
533  CGContextDrawPath(aContext, kCGPathEOFill);
534 }
535 
541 function CGContextFillPath(aContext)
542 {
543  CGContextDrawPath(aContext, kCGPathFill);
544  CGContextClosePath(aContext);
545 }
546 
547 var KAPPA = 4.0 * ((SQRT2 - 1.0) / 3.0);
548 
555 function CGContextAddEllipseInRect(aContext, aRect)
556 {
557  CGContextBeginPath(aContext);
558  CGContextAddPath(aContext, CGPathWithEllipseInRect(aRect));
559  CGContextClosePath(aContext);
560 }
561 
568 function CGContextFillEllipseInRect(aContext, aRect)
569 {
570  CGContextBeginPath(aContext);
571  CGContextAddEllipseInRect(aContext, aRect);
572  CGContextClosePath(aContext);
573  CGContextFillPath(aContext);
574 }
575 
582 function CGContextStrokeEllipseInRect(aContext, aRect)
583 {
584  CGContextBeginPath(aContext);
585  CGContextAddEllipseInRect(aContext, aRect);
586  CGContextClosePath(aContext);
587  CGContextStrokePath(aContext);
588 }
589 
595 function CGContextStrokePath(aContext)
596 {
597  CGContextDrawPath(aContext, kCGPathStroke);
598  CGContextClosePath(aContext);
599 }
600 
611 function CGContextStrokeLineSegments(aContext, points, count)
612 {
613  var i = 0;
614 
615  if (count === NULL)
616  var count = points.length;
617 
618  CGContextBeginPath(aContext);
619 
620  for (; i < count; i += 2)
621  {
622  CGContextMoveToPoint(aContext, points[i].x, points[i].y);
623  CGContextAddLineToPoint(aContext, points[i + 1].x, points[i + 1].y);
624  }
625 
626  CGContextStrokePath(aContext);
627 }
628 
629 
630 //FIXME: THIS IS WRONG!!!
631 
639 function CGContextSetFillColor(aContext, aColor)
640 {
641  if (aColor)
642  aContext.gState.fillStyle = [aColor cssString];
643 }
644 
651 function CGContextSetStrokeColor(aContext, aColor)
652 {
653  if (aColor)
654  aContext.gState.strokeStyle = [aColor cssString];
655 }
656 
668 function CGContextFillRoundedRectangleInRect(aContext, aRect, aRadius, ne, se, sw, nw)
669 {
670  CGContextBeginPath(aContext);
671  CGContextAddPath(aContext, CGPathWithRoundedRectangleInRect(aRect, aRadius, aRadius, ne, se, sw, nw));
672  CGContextClosePath(aContext);
673  CGContextFillPath(aContext);
674 }
675 
687 function CGContextStrokeRoundedRectangleInRect(aContext, aRect, aRadius, ne, se, sw, nw)
688 {
689  CGContextBeginPath(aContext);
690  CGContextAddPath(aContext, CGPathWithRoundedRectangleInRect(aRect, aRadius, aRadius, ne, se, sw, nw));
691  CGContextClosePath(aContext);
692  CGContextStrokePath(aContext);
693 }
694 
703 {
704 #include "CGContextCanvas.j"
705 }
707 {
708 #include "CGContextVML.j"
709 }