26 "lighter",
"source-over",
"source-over",
"source-over",
"source-over",
27 "source-over",
"source-over",
"source-over",
"source-over",
"source-over",
28 "source-over",
"source-over",
29 "copy",
"source-in",
"source-out",
"source-atop",
30 "destination-over",
"destination-in",
"destination-out",
"destination-atop",
31 "xor",
"source-over",
"source-over" ];
33 #define _CGContextAddArcCanvas(aContext, x, y, radius, startAngle, endAngle, anticlockwise) aContext.arc(x, y, radius, startAngle, endAngle, anticlockwise)
34 #define _CGContextAddArcToPointCanvas(aContext, x1, y1, x2, y2, radius) aContext.arcTo(x1, y1, x2, y2, radius)
35 #define _CGContextAddCurveToPointCanvas(aContext, cp1x, cp1y, cp2x, cp2y, x, y) aContext.bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y)
36 #define _CGContextAddQuadCurveToPointCanvas(aContext, cpx, cpy, x, y) aContext.quadraticCurveTo(cpx, cpy, x, y)
37 #define _CGContextAddLineToPointCanvas(aContext, x, y) aContext.lineTo(x, y)
38 #define _CGContextClosePathCanvas(aContext) aContext.closePath()
39 #define _CGContextMoveToPointCanvas(aContext, x, y) aContext.moveTo(x, y)
41 #define _CGContextAddRectCanvas(aContext, aRect) aContext.rect(CGRectGetMinX(aRect), CGRectGetMinY(aRect), CGRectGetWidth(aRect), CGRectGetHeight(aRect))
42 #define _CGContextBeginPathCanvas(aContext) aContext.beginPath()
43 #define _CGContextFillRectCanvas(aContext, aRect) aContext.fillRect(CGRectGetMinX(aRect), CGRectGetMinY(aRect), CGRectGetWidth(aRect), CGRectGetHeight(aRect))
44 #define _CGContextClipCanvas(aContext) aContext.clip()
48 var
hasPath =
function(aContext, methodName)
50 if (!aContext.hasPath)
51 CPLog.error(methodName +
": no current point");
53 return aContext.hasPath;
73 if (aContext.setLineDash)
75 aContext.setLineDash(someDashes);
76 aContext.lineDashOffset = aPhase;
78 else if (typeof aContext[
'webkitLineDash'] !==
'undefined')
80 aContext.webkitLineDash = someDashes;
81 aContext.webkitLineDashOffset = aPhase;
83 else if (typeof aContext[
'mozDash'] !==
'undefined')
85 aContext.mozDash = someDashes;
86 aContext.mozDashOffset = aPhase;
90 CPLog.warn(
"CGContextSetLineDash not implemented in this environment.")
101 aContext.lineWidth = aLineWidth;
106 aContext.miterLimit = aMiterLimit;
114 function CGContextAddArc(aContext, x, y, radius, startAngle, endAngle, clockwise)
118 _CGContextAddArcCanvas(aContext, x, y, radius, startAngle, endAngle, !clockwise);
121 aContext.hasPath = YES;
126 if (!
hasPath(aContext,
"CGContextAddArcToPoint"))
129 _CGContextAddArcToPointCanvas(aContext, x1, y1, x2, y2, radius);
134 if (!
hasPath(aContext,
"CGContextAddCurveToPoint"))
137 _CGContextAddCurveToPointCanvas(aContext, cp1x, cp1y, cp2x, cp2y, x, y);
143 if (count === null || count === undefined)
144 count = points.length;
149 _CGContextMoveToPointCanvas(aContext, points[0].x, points[0].y);
151 for (var i = 1; i < count; ++i)
152 _CGContextAddLineToPointCanvas(aContext, points[i].x, points[i].y);
154 aContext.hasPath = YES;
159 if (!
hasPath(aContext,
"CGContextAddLineToPoint"))
162 _CGContextAddLineToPointCanvas(aContext, x, y);
171 if (!aContext.hasPath)
172 _CGContextBeginPathCanvas(aContext);
175 _CGContextMoveToPointCanvas(aContext, aPath.start.x, aPath.start.y);
177 var elements = aPath.elements,
181 for (; i < count; ++i)
183 var element = elements[i],
189 _CGContextMoveToPointCanvas(aContext, element.x, element.y);
193 _CGContextAddLineToPointCanvas(aContext, element.x, element.y);
197 _CGContextAddQuadCurveToPointCanvas(aContext, element.cpx, element.cpy, element.x, element.y);
201 _CGContextAddCurveToPointCanvas(aContext, element.cp1x, element.cp1y, element.cp2x, element.cp2y, element.x, element.y);
205 _CGContextClosePathCanvas(aContext);
209 _CGContextAddArcCanvas(aContext, element.x, element.y, element.radius, element.startAngle, element.endAngle, element.clockwise);
213 _CGContextAddArcToPointCanvas(aContext, element.p1x, element.p1y, element.p2x, element.p2y, element.radius);
218 aContext.hasPath = YES;
223 _CGContextAddRectCanvas(aContext, aRect);
224 aContext.hasPath = YES;
229 if (!
hasPath(aContext,
"CGContextAddQuadCurveToPoint"))
232 _CGContextAddQuadCurveToPointCanvas(aContext, cpx, cpy, x, y);
237 if (count === null || count === undefined)
238 count = rects.length;
240 for (var i = 0; i < count; ++i)
243 _CGContextAddRectCanvas(aContext, rect);
246 aContext.hasPath = YES;
251 _CGContextBeginPathCanvas(aContext);
252 aContext.hasPath = NO;
257 _CGContextClosePathCanvas(aContext);
262 return !aContext.hasPath;
267 _CGContextMoveToPointCanvas(aContext, x, y);
268 aContext.hasPath = YES;
273 aContext.clearRect(CGRectGetMinX(aRect), CGRectGetMinY(aRect), CGRectGetWidth(aRect), CGRectGetHeight(aRect));
274 aContext.hasPath = NO;
279 if (!aContext.hasPath)
287 CPLog.warn(
"Unimplemented fill mode in CGContextDrawPath: %d", aMode);
289 aContext.hasPath = NO;
294 _CGContextFillRectCanvas(aContext, aRect);
295 aContext.hasPath = NO;
300 if (count === null || count === undefined)
301 count = rects.length;
303 for (var i = 0; i < count; ++i)
306 _CGContextFillRectCanvas(aContext, rect);
309 aContext.hasPath = NO;
314 aContext.strokeRect(CGRectGetMinX(aRect), CGRectGetMinY(aRect), CGRectGetWidth(aRect), CGRectGetHeight(aRect));
315 aContext.hasPath = NO;
318 function CGContextClip(aContext)
320 _CGContextClipCanvas(aContext);
321 aContext.hasPath = NO;
324 function CGContextClipToRect(aContext, aRect)
326 _CGContextBeginPathCanvas(aContext);
327 _CGContextAddRectCanvas(aContext, aRect);
328 _CGContextClosePathCanvas(aContext);
330 _CGContextClipCanvas(aContext);
331 aContext.hasPath = NO;
334 function CGContextClipToRects(aContext, rects, count)
336 if (count === null || count === undefined)
337 count = rects.length;
339 _CGContextBeginPathCanvas(aContext);
341 _CGContextClipCanvas(aContext);
342 aContext.hasPath = NO;
347 aContext.globalAlpha = anAlpha;
352 var patternImage = [aColor patternImage];
354 if ([patternImage isSingleImage])
356 var pattern = aContext.createPattern([patternImage image],
"repeat");
358 aContext.fillStyle = pattern;
361 aContext.fillStyle = [aColor cssString];
370 function CGContextCreatePatternContext(aContext, aSize)
372 var pattern = document.createElement(
"canvas");
374 pattern.width = aSize.width;
375 pattern.height = aSize.height;
377 return pattern.getContext(
"2d");
384 function CGContextSetFillPattern(aContext, aPatternContext)
386 var pattern = aContext.createPattern(aPatternContext.canvas,
"repeat");
387 aContext.fillStyle = pattern;
394 function CGContextSetStrokePattern(aContext, aPatternContext)
396 var pattern = aContext.createPattern(aPatternContext.canvas,
"repeat");
397 aContext.strokeStyle = pattern;
402 var patternImage = [aColor patternImage];
404 if ([patternImage isSingleImage])
406 var pattern = aContext.createPattern([patternImage image],
"repeat");
408 aContext.strokeStyle = pattern;
411 aContext.strokeStyle = [aColor cssString];
416 aContext.shadowOffsetX = aSize.width;
417 aContext.shadowOffsetY = aSize.height;
418 aContext.shadowBlur = aBlur;
423 aContext.shadowOffsetX = aSize.width;
424 aContext.shadowOffsetY = aSize.height;
425 aContext.shadowBlur = aBlur;
426 aContext.shadowColor = [aColor cssString];
431 aContext.rotate(anAngle);
436 aContext.scale(sx, sy);
441 aContext.translate(tx, ty);
444 var scale_rotate =
function(a, b, c, d)
446 var sign = (a * d < 0.0 || b * c > 0.0) ? -1.0 : 1.0,
447 a2 = (ATAN2(b, d) + ATAN2(-sign * c, sign * a)) / 2.0,
466 sx = (abs_cos * a / cos + abs_sin * -c / sin) / (abs_cos + abs_sin);
467 sy = (abs_cos * d / cos + abs_sin * b / sin) / (abs_cos + abs_sin);
473 var sign = (a * d < 0.0 || b * c > 0.0) ? -1.0 : 1.0,
474 a1 = (ATAN2(sign * b, sign * a) + ATAN2(-c, d)) / 2.0,
493 sx = (abs_cos * a / cos + abs_sin * b / sin) / (abs_cos + abs_sin);
494 sy = (abs_cos * d / cos + abs_sin * -c / sin) / (abs_cos + abs_sin);
500 CPLog.warn(
"Unimplemented function: eigen");
509 aContext.transform(anAffineTransform.a, anAffineTransform.b, anAffineTransform.c, anAffineTransform.d, anAffineTransform.tx, anAffineTransform.ty);
518 var a = anAffineTransform.a,
519 b = anAffineTransform.b,
520 c = anAffineTransform.c,
521 d = anAffineTransform.d,
522 tx = anAffineTransform.tx,
523 ty = anAffineTransform.ty,
530 if (b === 0.0 && c === 0.0)
537 else if (a * b === -c * d)
539 scale_rotate(a, b, c, d)
543 else if (a * c === -b * d)
560 scale_rotate(a, b, c, d);
572 if (tx != 0 || ty != 0)
576 if (sx != 1.0 || sy != 1.0)
586 aContext.drawImage(anImage._image, CGRectGetMinX(aRect), CGRectGetMinY(aRect), CGRectGetWidth(aRect), CGRectGetHeight(aRect));
587 aContext.hasPath = NO;
592 return "rgba(" + ROUND(aColor.components[0] * 255) +
", " + ROUND(aColor.components[1] * 255) +
", " + ROUND(255 * aColor.components[2]) +
", " + aColor.components[3] +
")";
597 var colors = aGradient.colors,
598 count = colors.length,
599 linearGradient = aContext.createLinearGradient(aStartPoint.x, aStartPoint.y, anEndPoint.x, anEndPoint.y);
602 linearGradient.addColorStop(aGradient.locations[count],
to_string(colors[count]));
604 aContext.fillStyle = linearGradient;
606 aContext.hasPath = NO;
611 var colors = aGradient.colors,
612 count = colors.length,
613 linearGradient = aContext.createRadialGradient(aStartCenter.x, aStartCenter.y, aStartRadius, anEndCenter.x, anEndCenter.y, anEndRadius);
616 linearGradient.addColorStop(aGradient.locations[count],
to_string(colors[count]));
618 aContext.fillStyle = linearGradient;
620 aContext.hasPath = NO;
625 var DOMElement = document.createElement(
"canvas"),
626 context = DOMElement.getContext(
"2d");
628 context.DOMElement = DOMElement;
631 context.hasPath = NO;