Implement addPath() method for Path object

Intent to implement on blink-dev:
https://groups.google.com/a/chromium.org/forum/#!searchin/blink-dev/addPath/blink-dev/YY-FmD8Efhc/y4Ag3pGF-toJ

BUG=159839

Review URL: https://codereview.chromium.org/170503002

git-svn-id: svn://svn.chromium.org/blink/trunk@168561 bbb929c8-8fbe-4397-9dbb-9b2b20218538
parent f5059adb
Test addPath() method.
On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
Test addPath() with transform as identity matrix.
PASS imgdata[4] is 255
PASS imgdata[5] is 255
PASS imgdata[6] is 0
PASS imgdata[7] is 255
Test addPath() with transform as translate(100, -100).
PASS imgdata[4] is 255
PASS imgdata[5] is 255
PASS imgdata[6] is 0
PASS imgdata[7] is 255
Test addPath() with non-invertible transform.
PASS imgdata[4] is not 255
PASS imgdata[5] is not 255
PASS imgdata[6] is 0
PASS imgdata[7] is not 255
Test addPath() with transform as null or invalid type.
PASS imgdata[4] is 255
PASS imgdata[5] is 0
PASS imgdata[6] is 0
PASS imgdata[7] is 255
Test addPath() with path as null and invalid type
PASS p7.addPath(null, m) threw exception TypeMismatchError: Failed to execute 'addPath' on 'Path': The 1st argument provided is either null, or an invalid Path object..
PASS p7.addPath([], m) threw exception TypeMismatchError: Failed to execute 'addPath' on 'Path': The 1st argument provided is either null, or an invalid Path object..
PASS p7.addPath({}, m) threw exception TypeMismatchError: Failed to execute 'addPath' on 'Path': The 1st argument provided is either null, or an invalid Path object..
PASS successfullyParsed is true
TEST COMPLETE
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
<head>
<script src="../../resources/js-test.js"></script>
</head>
<body>
<script src="script-tests/canvas-path-addpath.js"></script>
</body>
</html>
description("Test addPath() method.");
var ctx = document.createElement('canvas').getContext('2d');
debug("Test addPath() with transform as identity matrix.")
ctx.beginPath();
var p1 = new Path();
p1.rect(0,0,100,100);
var p2 = new Path();
p2.rect(0,100,100,100);
var m = ctx.currentTransform;
p1.addPath(p2, m);
ctx.fillStyle = 'yellow';
ctx.currentPath = p1;
ctx.fill();
var imageData = ctx.getImageData(0, 100, 100, 100);
var imgdata = imageData.data;
shouldBe("imgdata[4]", "255");
shouldBe("imgdata[5]", "255");
shouldBe("imgdata[6]", "0");
shouldBe("imgdata[7]", "255");
debug("");
debug("Test addPath() with transform as translate(100, -100).")
ctx.beginPath();
var p3 = new Path();
p3.rect(0,0,100,100);
var p4 = new Path();
p4.rect(0,100,100,100);
m.a = 1; m.b = 0;
m.c = 0; m.d = 1;
m.e = 100; m.f = -100;
p3.addPath(p4, m);
ctx.fillStyle = 'yellow';
ctx.currentPath = p3;
ctx.fill();
imageData = ctx.getImageData(100, 0, 100, 100);
imgdata = imageData.data;
shouldBe("imgdata[4]", "255");
shouldBe("imgdata[5]", "255");
shouldBe("imgdata[6]", "0");
shouldBe("imgdata[7]", "255");
debug("");
debug("Test addPath() with non-invertible transform.")
ctx.beginPath();
var p5 = new Path();
p5.rect(0,0,100,100);
var p6 = new Path();
p6.rect(100,100,100,100);
m.a = 0; m.b = 0;
m.c = 0; m.d = 0;
m.e = 0; m.f = 0;
p5.addPath(p6, m);
ctx.fillStyle = 'yellow';
ctx.currentPath = p5;
ctx.fill();
imageData = ctx.getImageData(100, 100, 100, 100);
imgdata = imageData.data;
shouldNotBe("imgdata[4]", "255");
shouldNotBe("imgdata[5]", "255");
shouldBe("imgdata[6]", "0");
shouldNotBe("imgdata[7]", "255");
debug("");
debug("Test addPath() with transform as null or invalid type.")
ctx.beginPath();
var p7 = new Path();
p7.rect(0,0,100,100);
var p8 = new Path();
p8.rect(100,100,100,100);
p7.addPath(p8, null);
p7.addPath(p8, []);
p7.addPath(p8, {});
ctx.fillStyle = 'red';
ctx.currentPath = p7;
ctx.fill();
imageData = ctx.getImageData(100, 100, 100, 100);
imgdata = imageData.data;
shouldBe("imgdata[4]", "255");
shouldBe("imgdata[5]", "0");
shouldBe("imgdata[6]", "0");
shouldBe("imgdata[7]", "255");
debug("");
debug("Test addPath() with path as null and invalid type");
var p9 = new Path();
p9.rect(0,0,100,100);
shouldThrow("p7.addPath(null, m)");
shouldThrow("p7.addPath([], m)");
shouldThrow("p7.addPath({}, m)");
debug("");
......@@ -54,6 +54,7 @@ public:
protected:
CanvasPathMethods() { }
CanvasPathMethods(const Path& path) : m_path(path) { }
Path m_path;
};
}
......
......@@ -30,7 +30,9 @@
#include "bindings/v8/ScriptWrappable.h"
#include "core/html/canvas/CanvasPathMethods.h"
#include "core/svg/SVGMatrixTearOff.h"
#include "core/svg/SVGPathUtilities.h"
#include "platform/transforms/AffineTransform.h"
#include "wtf/PassRefPtr.h"
#include "wtf/RefCounted.h"
......@@ -48,6 +50,16 @@ public:
const Path& path() const { return m_path; }
virtual ~DOMPath() { }
void addPath(DOMPath* path, SVGMatrixTearOff* transform, ExceptionState& exceptionState)
{
if (!path) {
exceptionState.throwDOMException(TypeMismatchError, ExceptionMessages::argumentNullOrIncorrectType(1, "Path"));
return;
}
Path src = path->path();
m_path.addPath(src, transform ? transform->value() : AffineTransform(1, 0, 0, 1, 0, 0));
}
private:
DOMPath() : CanvasPathMethods()
{
......@@ -55,17 +67,15 @@ private:
}
DOMPath(const Path& path)
: CanvasPathMethods()
: CanvasPathMethods(path)
{
ScriptWrappable::init(this);
m_path = path;
}
DOMPath(DOMPath* path)
: CanvasPathMethods()
: CanvasPathMethods(path->path())
{
ScriptWrappable::init(this);
m_path = path->path();
}
DOMPath(const String& pathData)
......
......@@ -34,6 +34,8 @@
ImplementedAs=DOMPath
] interface Path {
[RaisesException] void addPath(Path? path, SVGMatrix? transform);
// FIXME: These methods should be shared with CanvasRenderingContext2D in the CanvasPathMethods interface.
void closePath();
void moveTo([Default=Undefined] optional float x,
......
......@@ -472,6 +472,11 @@ void Path::addBeziersForRoundedRect(const FloatRect& rect, const FloatSize& topL
closeSubpath();
}
void Path::addPath(const Path& src, const AffineTransform& transform)
{
m_path.addPath(src.skPath(), affineTransformToSkMatrix(transform));
}
void Path::translate(const FloatSize& size)
{
m_path.offset(WebCoreFloatToSkScalar(size.width()), WebCoreFloatToSkScalar(size.height()));
......
......@@ -131,6 +131,8 @@ public:
void addRoundedRect(const FloatRect&, const FloatSize& topLeftRadius, const FloatSize& topRightRadius, const FloatSize& bottomLeftRadius, const FloatSize& bottomRightRadius);
void addRoundedRect(const RoundedRect&);
void addPath(const Path&, const AffineTransform&);
void translate(const FloatSize&);
const SkPath& skPath() const { return m_path; }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment