add (disabled) wrapper for javascript canvas

git-svn-id: http://skia.googlecode.com/svn/trunk@9717 2bbb7eff-a529-9590-31e7-b0007b416f81
This commit is contained in:
reed@google.com 2013-06-21 15:11:40 +00:00
parent 09ebe0be26
commit 8ea69838a4

View File

@ -1,25 +1,189 @@
/*
* Copyright 2011 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "gm.h"
namespace skiagm {
class SkJSCanvas {
public:
SkJSCanvas(SkCanvas* target);
~SkJSCanvas();
class Poly2PolyGM : public GM {
void save();
void restore();
double lineWidth;
void setLineWidth(double);
void beginPath();
void moveTo(double x, double y);
void lineTo(double x, double y);
void closePath();
void fill();
void stroke();
void fillText(const char text[], double x, double y);
private:
SkCanvas* fTarget;
SkPaint fFillPaint;
SkPaint fStrokePaint;
SkPath fPath;
};
SkJSCanvas::SkJSCanvas(SkCanvas* target) : fTarget(target) {
fFillPaint.setAntiAlias(true);
fStrokePaint.setAntiAlias(true);
fStrokePaint.setStyle(SkPaint::kStroke_Style);
fStrokePaint.setStrokeWidth(SK_Scalar1);
}
SkJSCanvas::~SkJSCanvas() {}
void SkJSCanvas::save() { fTarget->save(); }
void SkJSCanvas::restore() { fTarget->restore(); }
void SkJSCanvas::beginPath() { fPath.reset(); }
void SkJSCanvas::moveTo(double x, double y) {
fPath.moveTo(SkDoubleToScalar(x), SkDoubleToScalar(y));
}
void SkJSCanvas::lineTo(double x, double y) {
fPath.lineTo(SkDoubleToScalar(x), SkDoubleToScalar(y));
}
void SkJSCanvas::closePath() { fPath.close(); }
void SkJSCanvas::fill() {
fTarget->drawPath(fPath, fFillPaint);
}
void SkJSCanvas::stroke() {
fStrokePaint.setStrokeWidth(SkDoubleToScalar(lineWidth));
fTarget->drawPath(fPath, fStrokePaint);
}
void SkJSCanvas::fillText(const char text[], double x, double y) {
fTarget->drawText(text, strlen(text),
SkDoubleToScalar(x), SkDoubleToScalar(y), fFillPaint);
}
///////////////////////////////////////////////////////////////////////////////
static void dump(const SkPath& path) {
const SkRect& r = path.getBounds();
SkDebugf("isEmpty %d, bounds [%g %g %g %g]\n", path.isEmpty(),
r.fLeft, r.fTop, r.fRight, r.fBottom);
}
static void test_stroke(SkCanvas* canvas) {
if (true) {
SkPath path;
dump(path);
path.reset(); path.moveTo(0, 0);
dump(path);
path.reset(); path.moveTo(100, 100);
dump(path);
path.reset(); path.moveTo(0, 0); path.moveTo(100, 100);
dump(path);
path.reset(); path.moveTo(0, 0); path.lineTo(100, 100);
dump(path);
path.reset(); path.moveTo(0, 0); path.lineTo(100, 100); path.moveTo(200, 200);
dump(path);
}
#if 0
// TEST 1 - The rectangle as it's expected to look
var canvas = document.createElement('canvas');
document.body.appendChild(canvas);
var ctx = canvas.getContext("2d");
#else
SkJSCanvas ctx(canvas);
#endif
ctx.save();
ctx.lineWidth = 2;
ctx.beginPath();
ctx.moveTo(10, 100);
ctx.lineTo(150, 100);
ctx.lineTo(150, 15);
ctx.lineTo(10, 15);
ctx.closePath();
// no extra moveTo here
// ctx.moveTo(175, 125);
ctx.stroke();
ctx.restore();
ctx.fillText("As Expected", 10, 10);
#if 0
// TEST 2 - Includes an extra moveTo call before stroke; the rectangle appears larger
canvas = document.createElement('canvas');
document.body.appendChild(canvas);
ctx = canvas.getContext("2d");
#else
canvas->translate(200, 0);
#endif
ctx.save();
ctx.lineWidth = 2;
ctx.beginPath();
ctx.moveTo(10, 100);
ctx.lineTo(150, 100);
ctx.lineTo(150, 15);
ctx.lineTo(10, 15);
ctx.closePath();
ctx.moveTo(175, 125);
ctx.stroke();
ctx.restore();
ctx.fillText("Larger Rectangle", 10, 10);
#if 0
// TEST 3 - Identical to test 2 except the line width is 1
canvas = document.createElement('canvas');
document.body.appendChild(canvas);
ctx = canvas.getContext("2d");
#else
canvas->translate(200, 0);
#endif
ctx.save();
ctx.lineWidth = 1;
ctx.beginPath();
ctx.moveTo(10, 100);
ctx.lineTo(150, 100);
ctx.lineTo(150, 15);
ctx.lineTo(10, 15);
ctx.closePath();
ctx.moveTo(175, 125);
ctx.stroke();
ctx.restore();
ctx.fillText("As Expected - line width 1", 10, 10);
}
class Poly2PolyGM : public skiagm::GM {
public:
Poly2PolyGM() {}
protected:
virtual SkString onShortName() {
virtual SkString onShortName() SK_OVERRIDE {
return SkString("poly2poly");
}
virtual SkISize onISize() {
return make_isize(835, 840);
virtual SkISize onISize() SK_OVERRIDE {
return SkISize::Make(835, 840);
}
static void doDraw(SkCanvas* canvas, SkPaint* paint, const int isrc[],
@ -56,7 +220,9 @@ protected:
canvas->restore();
}
virtual void onDraw(SkCanvas* canvas) {
virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
if (false) { test_stroke(canvas); return; }
SkPaint paint;
paint.setAntiAlias(true);
paint.setStrokeWidth(SkIntToScalar(4));
@ -97,12 +263,10 @@ protected:
}
private:
typedef GM INHERITED;
typedef skiagm::GM INHERITED;
};
//////////////////////////////////////////////////////////////////////////////
static GM* MyFactory(void*) { return new Poly2PolyGM; }
static GMRegistry reg(MyFactory);
DEF_GM( return new Poly2PolyGM; )
}