Bug 985257 - Add implementation for Path2D constructor that takes an SVG path string. r=roc

This commit is contained in:
Rik Cabanier 2014-03-21 09:18:58 -04:00
parent 5bd0ff3c56
commit 086c625c06
7 changed files with 63 additions and 1 deletions

View File

@ -94,6 +94,7 @@
#include "nsGlobalWindow.h"
#include "GLContext.h"
#include "GLContextProvider.h"
#include "SVGContentUtils.h"
#undef free // apparently defined by some windows header, clashing with a free()
// method in SkTypes.h
@ -4362,6 +4363,18 @@ CanvasPath::Constructor(const GlobalObject& aGlobal, CanvasPath& aCanvasPath, Er
return path.forget();
}
already_AddRefed<CanvasPath>
CanvasPath::Constructor(const GlobalObject& aGlobal, const nsAString& aPathString, ErrorResult& aRv)
{
RefPtr<gfx::Path> tempPath = SVGContentUtils::GetPath(aPathString);
if (!tempPath) {
return Constructor(aGlobal, aRv);
}
nsRefPtr<CanvasPath> path = new CanvasPath(aGlobal.GetAsSupports(), tempPath->CopyToBuilder());
return path.forget();
}
void
CanvasPath::ClosePath()
{

View File

@ -61,6 +61,9 @@ public:
static already_AddRefed<CanvasPath> Constructor(const GlobalObject& aGlobal,
CanvasPath& aCanvasPath,
ErrorResult& rv);
static already_AddRefed<CanvasPath> Constructor(const GlobalObject& aGlobal,
const nsAString& aPathString,
ErrorResult& rv);
void ClosePath();
void MoveTo(double x, double y);

View File

@ -97,6 +97,7 @@ FINAL_LIBRARY = 'gklayout'
LOCAL_INCLUDES += [
'/content/base/src',
'/content/html/content/src',
'/content/svg/content/src',
'/content/xul/content/src',
'/dom/base',
'/image/src',

View File

@ -315,6 +315,23 @@ function test_isPointInStroke_canvas() {
}
</script>
<p>Canvas test: test_pathconstructor_canvas</p>
<canvas id="c7" class="output" width="200" height="100">+
</canvas>
<script type="text/javascript">
function test_pathconstructor_canvas() {
var c = document.getElementById("c7");
var ctx = c.getContext("2d");
var p = new Path2D("M100,0L200,0L200,100L100,100z");
ctx.fillStyle = 'blue';
ctx.fill(p);
isPixel(ctx, 105, 5, [0, 0, 255, 255], 0);
isPixel(ctx, 5, 5, [0, 0, 0, 0], 0);
}
</script>
<script>
function runTests() {
@ -354,6 +371,12 @@ function runTests() {
throw e;
ok(false, "unexpected exception thrown in: test_isPointInStroke_canvas");
}
try {
test_pathconstructor_canvas();
} catch(e) {
throw e;
ok(false, "unexpected exception thrown in: test_pathconstructor_canvas");
}
SpecialPowers.setBoolPref("canvas.path.enabled", false);
SimpleTest.finish();
}

View File

@ -20,6 +20,7 @@
#include "nsContentUtils.h"
#include "mozilla/gfx/2D.h"
#include "gfx2DGlue.h"
#include "nsSVGPathDataParser.h"
using namespace mozilla;
using namespace mozilla::dom;
@ -584,3 +585,15 @@ SVGContentUtils::CoordToFloat(nsPresContext *aPresContext,
return 0.0f;
}
}
RefPtr<gfx::Path>
SVGContentUtils::GetPath(const nsAString& aPathString)
{
SVGPathData pathData;
nsSVGPathDataParser parser(aPathString, &pathData);
if (!parser.Parse()) {
return NULL;
}
return pathData.BuildPath(FillRule::FILL_WINDING, NS_STYLE_STROKE_LINECAP_BUTT, 1);
}

View File

@ -14,6 +14,7 @@
#include "mozilla/RangedPtr.h"
#include "nsError.h"
#include "nsStringFwd.h"
#include "gfx2DGlue.h"
class nsIContent;
class nsIDocument;
@ -243,6 +244,13 @@ public:
static float CoordToFloat(nsPresContext *aPresContext,
nsSVGElement *aContent,
const nsStyleCoord &aCoord);
/**
* Parse the SVG path string
* Returns a path
* string formatted as an SVG path
*/
static mozilla::RefPtr<mozilla::gfx::Path>
GetPath(const nsAString& aPathString);
};
#endif

View File

@ -316,7 +316,8 @@ interface TextMetrics {
[Pref="canvas.path.enabled",
Constructor,
Constructor(Path2D other)]
Constructor(Path2D other),
Constructor(DOMString pathString)]
interface Path2D
{};
Path2D implements CanvasPathMethods;