Fixed a bunch of bugs in CGContextCanvas and CGPath.

- With canvas, we have to track ourselves whether the context has a path or not.
- All shapes except rects may not be added to a path with no context.
- CGPath was not setting the start and current point correctly in some cases.
- CGContextAddPath was not moving to the path's start point at the beginning.

Sorry, I am not going to spend the time to port these fixes to VML!
This commit is contained in:
Aparajita Fishman committed 2013-02-21 21:44:16 -05:00
1 parent 7d37427c96
commit 7ab84ca607
3 files changed
+212 -90

No files matched your search

+1
View File
@@ -68,6 +68,7 @@ var CPGraphicsContextCurrent = nil,
+ (void)restoreGraphicsState
{
var lastContext = [CPGraphicsContextThreadStack lastObject];
if (lastContext)
{
[lastContext restoreGraphicsState];
+150 -71
View File
@@ -43,6 +43,14 @@ var CANVAS_LINECAP_TABLE = [ "butt", "round", "square" ],
#define _CGContextFillRectCanvas(aContext, aRect) aContext.fillRect(_CGRectGetMinX(aRect), _CGRectGetMinY(aRect), _CGRectGetWidth(aRect), _CGRectGetHeight(aRect))
#define _CGContextClipCanvas(aContext) aContext.clip()
// In Cocoa, all primitives excepts rects cannot be added to the context's path
// until a move to point has been done, because an empty path has no current point.
var checkHasPath = function(aContext, methodName)
{
if (!aContext.hasPath)
CPLog.error(methodName + ": no current point");
}
function CGContextSaveGState(aContext)
{
aContext.save();
@@ -80,6 +88,9 @@ function CGContextSetBlendMode(aContext, aBlendMode)
function CGContextAddArc(aContext, x, y, radius, startAngle, endAngle, clockwise)
{
if (!checkHasPath(aContext, "CGContextAddArc"))
return;
// Despite the documentation saying otherwise, the last parameter is anti-clockwise not clockwise.
// http://developer.mozilla.org/en/docs/Canvas_tutorial:Drawing_shapes#Arcs
_CGContextAddArcCanvas(aContext, x, y, radius, startAngle, endAngle, !clockwise);
@@ -87,16 +98,25 @@ function CGContextAddArc(aContext, x, y, radius, startAngle, endAngle, clockwise
function CGContextAddArcToPoint(aContext, x1, y1, x2, y2, radius)
{
if (!checkHasPath(aContext, "CGContextAddArcToPoint"))
return;
_CGContextAddArcToPointCanvas(aContext, x1, y1, x2, y2, radius);
}
function CGContextAddCurveToPoint(aContext, cp1x, cp1y, cp2x, cp2y, x, y)
{
if (!checkHasPath(aContext, "CGContextAddCurveToPoint"))
return;
_CGContextAddCurveToPointCanvas(aContext, cp1x, cp1y, cp2x, cp2y, x, y);
}
function CGContextAddLines(aContext, points, count)
{
if (!checkHasPath(aContext, "CGContextAddLines"))
return;
// implementation mirrors that of CGPathAddLines()
if (count === null || count === undefined)
count = points.length;
@@ -112,6 +132,9 @@ function CGContextAddLines(aContext, points, count)
function CGContextAddLineToPoint(aContext, x, y)
{
if (!checkHasPath(aContext, "CGContextAddLineToPoint"))
return;
_CGContextAddLineToPointCanvas(aContext, x, y);
}
@@ -120,6 +143,13 @@ function CGContextAddPath(aContext, aPath)
if (!aContext || CGPathIsEmpty(aPath))
return;
// If the context does not have a path, explicitly begin one
if (!aContext.hasPath)
_CGContextBeginPathCanvas(aContext);
// We must implicitly move to the start of the path
_CGContextMoveToPointCanvas(aContext, aPath.start.x, aPath.start.y);
var elements = aPath.elements,
i = 0,
count = aPath.count;
@@ -131,31 +161,50 @@ function CGContextAddPath(aContext, aPath)
switch (type)
{
case kCGPathElementMoveToPoint: _CGContextMoveToPointCanvas(aContext, element.x, element.y);
break;
case kCGPathElementAddLineToPoint: _CGContextAddLineToPointCanvas(aContext, element.x, element.y);
break;
case kCGPathElementAddQuadCurveToPoint: _CGContextAddQuadCurveToPointCanvas(aContext, element.cpx, element.cpy, element.x, element.y);
break;
case kCGPathElementAddCurveToPoint: _CGContextAddCurveToPointCanvas(aContext, element.cp1x, element.cp1y, element.cp2x, element.cp2y, element.x, element.y);
break;
case kCGPathElementCloseSubpath: _CGContextClosePathCanvas(aContext);
break;
case kCGPathElementAddArc: _CGContextAddArcCanvas(aContext, element.x, element.y, element.radius, element.startAngle, element.endAngle, element.clockwise);
break;
case kCGPathElementAddArcToPoint: _CGContextAddArcToPointCanvas(aContext, element.p1x, element.p1y, element.p2x, element.p2y, element.radius);
break;
case kCGPathElementMoveToPoint:
_CGContextMoveToPointCanvas(aContext, element.x, element.y);
break;
case kCGPathElementAddLineToPoint:
_CGContextAddLineToPointCanvas(aContext, element.x, element.y);
break;
case kCGPathElementAddQuadCurveToPoint:
_CGContextAddQuadCurveToPointCanvas(aContext, element.cpx, element.cpy, element.x, element.y);
break;
case kCGPathElementAddCurveToPoint:
_CGContextAddCurveToPointCanvas(aContext, element.cp1x, element.cp1y, element.cp2x, element.cp2y, element.x, element.y);
break;
case kCGPathElementCloseSubpath:
_CGContextClosePathCanvas(aContext);
break;
case kCGPathElementAddArc:
_CGContextAddArcCanvas(aContext, element.x, element.y, element.radius, element.startAngle, element.endAngle, element.clockwise);
break;
case kCGPathElementAddArcToPoint:
_CGContextAddArcToPointCanvas(aContext, element.p1x, element.p1y, element.p2x, element.p2y, element.radius);
break;
}
}
aContext.hasPath = YES;
}
function CGContextAddRect(aContext, aRect)
{
_CGContextAddRectCanvas(aContext, aRect);
aContext.hasPath = YES;
}
function CGContextAddQuadCurveToPoint(aContext, cpx, cpy, x, y)
{
if (!checkHasPath(aContext, "CGContextAddQuadCurveToPoint"))
return;
_CGContextAddQuadCurveToPointCanvas(aContext, cpx, cpy, x, y);
}
@@ -169,11 +218,14 @@ function CGContextAddRects(aContext, rects, count)
var rect = rects[i];
_CGContextAddRectCanvas(aContext, rect);
}
aContext.hasPath = YES;
}
function CGContextBeginPath(aContext)
{
_CGContextBeginPathCanvas(aContext);
aContext.hasPath = NO;
}
function CGContextClosePath(aContext)
@@ -181,30 +233,42 @@ function CGContextClosePath(aContext)
_CGContextClosePathCanvas(aContext);
}
function CGContextIsPathEmpty(aContext)
{
return !aContext.hasPath;
}
function CGContextMoveToPoint(aContext, x, y)
{
_CGContextMoveToPointCanvas(aContext, x, y);
aContext.hasPath = YES;
}
function CGContextClearRect(aContext, aRect)
{
aContext.clearRect(_CGRectGetMinX(aRect), _CGRectGetMinY(aRect), _CGRectGetWidth(aRect), _CGRectGetHeight(aRect));
aContext.hasPath = NO;
}
function CGContextDrawPath(aContext, aMode)
{
if (aMode == kCGPathFill || aMode == kCGPathFillStroke)
aContext.fill();
else if (aMode == kCGPathEOFill || aMode == kCGPathEOFillStroke)
alert("not implemented!!!");
if (!aContext.hasPath)
return;
if (aMode == kCGPathStroke || aMode == kCGPathFillStroke || aMode == kCGPathEOFillStroke)
if (aMode === kCGPathFill || aMode === kCGPathFillStroke)
aContext.fill();
else if (aMode === kCGPathStroke || aMode === kCGPathFillStroke || aMode === kCGPathEOFillStroke)
aContext.stroke();
else if (aMode === kCGPathEOFill || aMode === kCGPathEOFillStroke)
CPLog.warn("Unimplemented fill mode in CGContextDrawPath: %d", aMode);
aContext.hasPath = NO;
}
function CGContextFillRect(aContext, aRect)
{
_CGContextFillRectCanvas(aContext, aRect);
aContext.hasPath = NO;
}
function CGContextFillRects(aContext, rects, count)
@@ -217,16 +281,20 @@ function CGContextFillRects(aContext, rects, count)
var rect = rects[i];
_CGContextFillRectCanvas(aContext, rect);
}
aContext.hasPath = NO;
}
function CGContextStrokeRect(aContext, aRect)
{
aContext.strokeRect(_CGRectGetMinX(aRect), _CGRectGetMinY(aRect), _CGRectGetWidth(aRect), _CGRectGetHeight(aRect));
aContext.hasPath = NO;
}
function CGContextClip(aContext)
{
_CGContextClipCanvas(aContext);
aContext.hasPath = NO;
}
function CGContextClipToRect(aContext, aRect)
@@ -236,6 +304,7 @@ function CGContextClipToRect(aContext, aRect)
_CGContextClosePathCanvas(aContext);
_CGContextClipCanvas(aContext);
aContext.hasPath = NO;
}
function CGContextClipToRects(aContext, rects, count)
@@ -246,6 +315,7 @@ function CGContextClipToRects(aContext, rects, count)
_CGContextBeginPathCanvas(aContext);
CGContextAddRects(aContext, rects, count);
_CGContextClipCanvas(aContext);
aContext.hasPath = NO;
}
function CGContextSetAlpha(aContext, anAlpha)
@@ -311,59 +381,63 @@ function CGContextTranslateCTM(aContext, tx, ty)
aContext.translate(tx, ty);
}
#define scale_rotate(a, b, c, d) \
var sign = (a * d < 0.0 || b * c > 0.0) ? -1.0 : 1.0, \
a2 = (ATAN2(b, d) + ATAN2(-sign * c, sign * a)) / 2.0, \
cos = COS(a2),\
sin = SIN(a2);\
\
if (cos == 0)\
{\
sx = -c / sin;\
sy = b / sin;\
}\
else if (sin == 0)\
{\
sx = a / cos;\
sy = d / cos;\
}\
else\
{\
abs_cos = ABS(cos);\
abs_sin = ABS(sin);\
\
sx = (abs_cos * a / cos + abs_sin * -c / sin) / (abs_cos + abs_sin);\
sy = (abs_cos * d / cos + abs_sin * b / sin) / (abs_cos + abs_sin);\
}\
var scale_rotate = function(a, b, c, d)
{
var sign = (a * d < 0.0 || b * c > 0.0) ? -1.0 : 1.0,
a2 = (ATAN2(b, d) + ATAN2(-sign * c, sign * a)) / 2.0,
cos = COS(a2),
sin = SIN(a2);
#define rotate_scale(a, b, c, d) \
var sign = (a * d < 0.0 || b * c > 0.0) ? -1.0 : 1.0;\
a1 = (ATAN2(sign * b, sign * a) + ATAN2(-c, d)) / 2.0,\
cos = COS(a1),\
sin = SIN(a1);\
\
if (cos == 0)\
{\
sx = b / sin;\
sy = -c / sin;\
}\
else if (sin == 0)\
{\
sx = a / cos;\
sy = d / cos;\
}\
else\
{\
abs_cos = ABS(cos);\
abs_sin = ABS(sin);\
\
sx = (abs_cos * a / cos + abs_sin * b / sin) / (abs_cos + abs_sin);\
sy = (abs_cos * d / cos + abs_sin * -c / sin) / (abs_cos + abs_sin);\
}\
if (cos === 0)
{
sx = -c / sin;
sy = b / sin;
}
else if (sin === 0)
{
sx = a / cos;
sy = d / cos;
}
else
{
abs_cos = ABS(cos);
abs_sin = ABS(sin);
sx = (abs_cos * a / cos + abs_sin * -c / sin) / (abs_cos + abs_sin);
sy = (abs_cos * d / cos + abs_sin * b / sin) / (abs_cos + abs_sin);
}
};
var rotate_scale = function(a, b, c, d)
{
var sign = (a * d < 0.0 || b * c > 0.0) ? -1.0 : 1.0;
a1 = (ATAN2(sign * b, sign * a) + ATAN2(-c, d)) / 2.0,
cos = COS(a1),
sin = SIN(a1);
if (cos === 0)
{
sx = b / sin;
sy = -c / sin;
}
else if (sin === 0)
{
sx = a / cos;
sy = d / cos;
}
else
{
abs_cos = ABS(cos);
abs_sin = ABS(sin);
sx = (abs_cos * a / cos + abs_sin * b / sin) / (abs_cos + abs_sin);
sy = (abs_cos * d / cos + abs_sin * -c / sin) / (abs_cos + abs_sin);
}
};
function eigen(anAffineTransform)
{
alert("IMPLEMENT ME!");
CPLog.warn("Unimplemented function: eigen");
}
@@ -393,20 +467,20 @@ CGContextConcatCTM = function(aContext, anAffineTransform)
a2 = 0.0;
// Detect the simple case of just scaling.
if (b == 0.0 && c == 0.0)
if (b === 0.0 && c === 0.0)
{
sx = a;
sy = d;
}
// a scale followed by a rotate
else if (a * b == -c * d)
else if (a * b === -c * d)
{
scale_rotate(a, b, c, d)
}
// rotate, then scale.
else if (a * c == -b * d)
else if (a * c === -b * d)
{
rotate_scale(a, b, c, d)
}
@@ -450,6 +524,7 @@ CGContextConcatCTM = function(aContext, anAffineTransform)
function CGContextDrawImage(aContext, aRect, anImage)
{
aContext.drawImage(anImage._image, _CGRectGetMinX(aRect), _CGRectGetMinY(aRect), _CGRectGetWidth(aRect), _CGRectGetHeight(aRect));
aContext.hasPath = NO;
}
function to_string(aColor)
@@ -469,6 +544,7 @@ function CGContextDrawLinearGradient(aContext, aGradient, aStartPoint, anEndPoin
aContext.fillStyle = linearGradient;
aContext.fill();
aContext.hasPath = NO;
}
function CGBitmapGraphicsContextCreate()
@@ -478,5 +554,8 @@ function CGBitmapGraphicsContextCreate()
context.DOMElement = DOMElement;
// canvas gives us no way to query whether the path is empty or not, so we have to track it ourselves
context.hasPath = NO;
return context;
}
+61 -19
View File
@@ -43,7 +43,7 @@ kCGPathElementAddArcToPoint = 6;
*/
function CGPathCreateMutable()
{
return { count:0, start:NULL, current:NULL, elements:[] };
return { count:0, start:_CGPointMake(0, 0), current:_CGPointMake(0, 0), elements:[] };
}
/*!
@@ -101,7 +101,7 @@ function CGPathAddArc(aPath, aTransform, x, y, aRadius, aStartAngle, anEndAngle,
// Angles that equal "modulo" 2 pi return as equal after transforming them,
// so we have to make sure to make them different again if they were different
// to start out with. It's the difference between no circle and a full circle.
if (anEndAngle == aStartAngle && oldEndAngle != oldStartAngle)
if (anEndAngle === aStartAngle && oldEndAngle != oldStartAngle)
if (oldStartAngle > oldEndAngle)
anEndAngle = anEndAngle - PI2;
else
@@ -112,7 +112,30 @@ function CGPathAddArc(aPath, aTransform, x, y, aRadius, aStartAngle, anEndAngle,
aRadius = SQRT(aRadius.width * aRadius.width + aRadius.height * aRadius.height);
}
aPath.current = _CGPointMake(x + aRadius * COS(anEndAngle), y + aRadius * SIN(anEndAngle));
/*
From the Cocoa docs:
If the specified path already contains a subpath, Quartz implicitly adds a line connecting the subpath’s current point to the beginning of the arc. If the path is empty, Quartz creates a new subpath with a starting point set to the starting point of the arc.
The ending point of the arc becomes the new current point of the path.
*/
var arcEndX = x + aRadius * COS(anEndAngle),
arcEndY = y + aRadius * SIN(anEndAngle);
if (aPath.count)
{
if (aPath.current.x !== arcEndX || aPath.current.y !== arcEndY)
CGPathAddLineToPoint(aPath, aTransform, arcEndX, arcEndY);
}
else
{
var arcStartX = x + aRadius * COS(aStartAngle),
arcStartY = y + aRadius * SIN(aStartAngle);
aPath.start = _CGPointMake(arcStartX, arcStartY);
}
aPath.current = _CGPointMake(arcEndX, arcEndY);
aPath.elements[aPath.count++] = { type:kCGPathElementAddArc, x:x, y:y, radius:aRadius, startAngle:aStartAngle, endAngle:anEndAngle, clockwise:isClockwise };
}
@@ -127,6 +150,21 @@ function CGPathAddArcToPoint(aPath, aTransform, x1, y1, x2, y2, aRadius)
p2 = _CGPointApplyAffineTransform(p2, aTransform);
}
/*
From the Cocoa docs:
If the current point and the first tangent point of the arc (the starting point) are not equal, Quartz appends a straight line segment from the current point to the first tangent point.
The ending point of the arc becomes the new current point of the path.
*/
if (aPath.count)
{
if (aPath.current.x !== p1.x || aPath.current.y !== p1.y)
CGPathAddLineToPoint(aPath, aTransform, p1.x, p1.y);
}
else
aPath.start = p1;
aPath.current = p2;
aPath.elements[aPath.count++] = { type:kCGPathElementAddArcToPoint, p1x:p1.x, p1y:p1.y, p2x:p2.x, p2y:p2.y, radius:aRadius };
}
@@ -144,8 +182,8 @@ function CGPathAddCurveToPoint(aPath, aTransform, cp1x, cp1y, cp2x, cp2y, x, y)
end = _CGPointApplyAffineTransform(end, aTransform);
}
aPath.current = end;
aPath.elements[aPath.count++] = { type:kCGPathElementAddCurveToPoint, cp1x:cp1.x, cp1y:cp1.y, cp2x:cp2.x, cp2y:cp2.y, x:end.x, y:end.y };
aPath.current = end;
aPath.elements[aPath.count++] = { type:kCGPathElementAddCurveToPoint, cp1x:cp1.x, cp1y:cp1.y, cp2x:cp2.x, cp2y:cp2.y, x:end.x, y:end.y };
}
function CGPathAddLines(aPath, aTransform, points, count)
@@ -264,21 +302,23 @@ function CGPathAddRects(aPath, aTransform, rects, count)
function CGPathMoveToPoint(aPath, aTransform, x, y)
{
var point = _CGPointMake(x, y),
count = aPath.count;
var point = _CGPointMake(x, y);
if (aTransform != NULL)
point = _CGPointApplyAffineTransform(point, aTransform);
aPath.start = point;
aPath.current = point;
aPath.start = aPath.current = point;
var previous = aPath.elements[count - 1];
if (count != 0 && previous.type == kCGPathElementMoveToPoint)
// If the previous op was a move, just update that point
if (aPath.count)
{
previous.x = point.x;
previous.y = point.y;
var previous = aPath.elements[count - 1];
if (previous.type === kCGPathElementMoveToPoint)
{
previous.x = point.x;
previous.y = point.y;
}
}
else
aPath.elements[aPath.count++] = { type:kCGPathElementMoveToPoint, x:point.x, y:point.y };
@@ -290,7 +330,7 @@ function CGPathWithEllipseInRect(aRect)
{
var path = CGPathCreateMutable();
if (_CGRectGetWidth(aRect) == _CGRectGetHeight(aRect))
if (_CGRectGetWidth(aRect) === _CGRectGetHeight(aRect))
CGPathAddArc(path, nil, _CGRectGetMidX(aRect), _CGRectGetMidY(aRect), _CGRectGetWidth(aRect) / 2.0, 0.0, 2 * PI, YES);
else
{
@@ -362,15 +402,17 @@ function CGPathCloseSubpath(aPath)
var count = aPath.count;
// Don't bother closing this subpath if there aren't any current elements, or the last element already closed the subpath.
if (count == 0 || aPath.elements[count - 1].type == kCGPathElementCloseSubpath)
if (count === 0 || aPath.elements[count - 1].type === kCGPathElementCloseSubpath)
return;
// After closing, the current point is the previous path's starting point
aPath.current = _CGPointCreateCopy(aPath.start);
aPath.elements[aPath.count++] = { type:kCGPathElementCloseSubpath, points:[aPath.start] };
}
function CGPathEqualToPath(aPath, anotherPath)
{
if (aPath == anotherPath)
if (aPath === anotherPath)
return YES;
if (aPath.count != anotherPath.count || !_CGPointEqualToPoint(aPath.start, anotherPath.start) || !_CGPointEqualToPoint(aPath.current, anotherPath.current))
@@ -387,7 +429,7 @@ function CGPathEqualToPath(aPath, anotherPath)
if (element.type != anotherElement.type)
return NO;
if ((element.type == kCGPathElementAddArc || element.type == kCGPathElementAddArcToPoint) &&
if ((element.type === kCGPathElementAddArc || element.type === kCGPathElementAddArcToPoint) &&
element.radius != anotherElement.radius)
return NO;
@@ -408,7 +450,7 @@ function CGPathGetCurrentPoint(aPath)
function CGPathIsEmpty(aPath)
{
return !aPath || aPath.count == 0;
return !aPath || aPath.count === 0;
}
/*!