2011-07-28 14:26:00 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2011 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
2013-06-21 15:11:40 +00:00
|
|
|
|
2011-02-17 14:21:01 +00:00
|
|
|
#include "gm.h"
|
2017-03-22 17:47:51 +00:00
|
|
|
#include "sk_tool_utils.h"
|
2015-07-28 17:37:53 +00:00
|
|
|
#include "Resources.h"
|
2015-08-05 20:57:49 +00:00
|
|
|
#include "SkPath.h"
|
2018-10-25 20:12:39 +00:00
|
|
|
#include "SkTextUtils.h"
|
2015-07-28 17:37:53 +00:00
|
|
|
#include "SkTypeface.h"
|
2011-02-17 14:21:01 +00:00
|
|
|
|
2013-06-21 15:11:40 +00:00
|
|
|
class SkJSCanvas {
|
|
|
|
public:
|
|
|
|
SkJSCanvas(SkCanvas* target);
|
|
|
|
~SkJSCanvas();
|
|
|
|
|
|
|
|
void save();
|
|
|
|
void restore();
|
2013-06-22 07:00:58 +00:00
|
|
|
|
2013-06-21 15:11:40 +00:00
|
|
|
double lineWidth;
|
|
|
|
void setLineWidth(double);
|
2013-06-22 07:00:58 +00:00
|
|
|
|
2013-06-21 15:11:40 +00:00
|
|
|
void beginPath();
|
|
|
|
void moveTo(double x, double y);
|
|
|
|
void lineTo(double x, double y);
|
|
|
|
void closePath();
|
2013-06-22 07:00:58 +00:00
|
|
|
|
2013-06-21 15:11:40 +00:00
|
|
|
void fill();
|
|
|
|
void stroke();
|
2013-06-22 07:00:58 +00:00
|
|
|
|
2013-06-21 15:11:40 +00:00
|
|
|
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);
|
2015-07-24 19:09:25 +00:00
|
|
|
sk_tool_utils::set_portable_typeface(&fFillPaint);
|
2013-06-21 15:11:40 +00:00
|
|
|
fStrokePaint.setAntiAlias(true);
|
|
|
|
fStrokePaint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
fStrokePaint.setStrokeWidth(SK_Scalar1);
|
|
|
|
}
|
2011-02-17 14:21:01 +00:00
|
|
|
|
2013-06-21 15:11:40 +00:00
|
|
|
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) {
|
2017-04-28 19:35:12 +00:00
|
|
|
fTarget->drawString(text, SkDoubleToScalar(x), SkDoubleToScalar(y), fFillPaint);
|
2013-06-21 15:11:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2013-06-22 07:00:58 +00:00
|
|
|
|
2013-06-21 15:11:40 +00:00
|
|
|
#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();
|
2013-06-22 07:00:58 +00:00
|
|
|
|
2013-06-21 15:11:40 +00:00
|
|
|
// no extra moveTo here
|
|
|
|
// ctx.moveTo(175, 125);
|
2013-06-22 07:00:58 +00:00
|
|
|
|
2013-06-21 15:11:40 +00:00
|
|
|
ctx.stroke();
|
|
|
|
ctx.restore();
|
2013-06-22 07:00:58 +00:00
|
|
|
|
2013-06-21 15:11:40 +00:00
|
|
|
ctx.fillText("As Expected", 10, 10);
|
2013-06-22 07:00:58 +00:00
|
|
|
|
2013-06-21 15:11:40 +00:00
|
|
|
#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();
|
2013-06-22 07:00:58 +00:00
|
|
|
|
2013-06-21 15:11:40 +00:00
|
|
|
ctx.moveTo(175, 125);
|
2013-06-22 07:00:58 +00:00
|
|
|
|
2013-06-21 15:11:40 +00:00
|
|
|
ctx.stroke();
|
|
|
|
ctx.restore();
|
2013-06-22 07:00:58 +00:00
|
|
|
|
2013-06-21 15:11:40 +00:00
|
|
|
ctx.fillText("Larger Rectangle", 10, 10);
|
2013-06-22 07:00:58 +00:00
|
|
|
|
2013-06-21 15:11:40 +00:00
|
|
|
#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();
|
2013-06-22 07:00:58 +00:00
|
|
|
|
2013-06-21 15:11:40 +00:00
|
|
|
ctx.moveTo(175, 125);
|
2013-06-22 07:00:58 +00:00
|
|
|
|
2013-06-21 15:11:40 +00:00
|
|
|
ctx.stroke();
|
|
|
|
ctx.restore();
|
2013-06-22 07:00:58 +00:00
|
|
|
|
2013-06-21 15:11:40 +00:00
|
|
|
ctx.fillText("As Expected - line width 1", 10, 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
class Poly2PolyGM : public skiagm::GM {
|
2011-02-17 14:21:01 +00:00
|
|
|
public:
|
2012-08-23 18:14:13 +00:00
|
|
|
Poly2PolyGM() {}
|
|
|
|
|
2011-02-17 14:21:01 +00:00
|
|
|
protected:
|
2014-04-30 13:20:45 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2011-02-17 14:21:01 +00:00
|
|
|
return SkString("poly2poly");
|
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
SkISize onISize() override {
|
2013-06-21 15:11:40 +00:00
|
|
|
return SkISize::Make(835, 840);
|
2011-02-17 14:21:01 +00:00
|
|
|
}
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2011-02-17 14:21:01 +00:00
|
|
|
static void doDraw(SkCanvas* canvas, SkPaint* paint, const int isrc[],
|
|
|
|
const int idst[], int count) {
|
|
|
|
SkMatrix matrix;
|
|
|
|
SkPoint src[4], dst[4];
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2011-02-17 14:21:01 +00:00
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
src[i].set(SkIntToScalar(isrc[2*i+0]), SkIntToScalar(isrc[2*i+1]));
|
|
|
|
dst[i].set(SkIntToScalar(idst[2*i+0]), SkIntToScalar(idst[2*i+1]));
|
|
|
|
}
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2011-02-17 14:21:01 +00:00
|
|
|
canvas->save();
|
|
|
|
matrix.setPolyToPoly(src, dst, count);
|
|
|
|
canvas->concat(matrix);
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2018-08-16 14:17:03 +00:00
|
|
|
paint->setColor(SK_ColorGRAY);
|
2011-02-17 14:21:01 +00:00
|
|
|
paint->setStyle(SkPaint::kStroke_Style);
|
2017-02-22 18:21:42 +00:00
|
|
|
const SkScalar D = 64;
|
|
|
|
canvas->drawRect(SkRect::MakeWH(D, D), *paint);
|
2011-02-17 14:21:01 +00:00
|
|
|
canvas->drawLine(0, 0, D, D, *paint);
|
|
|
|
canvas->drawLine(0, D, D, 0, *paint);
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2011-02-17 14:21:01 +00:00
|
|
|
SkPaint::FontMetrics fm;
|
|
|
|
paint->getFontMetrics(&fm);
|
|
|
|
paint->setColor(SK_ColorRED);
|
|
|
|
paint->setStyle(SkPaint::kFill_Style);
|
|
|
|
SkScalar x = D/2;
|
2011-10-27 21:47:03 +00:00
|
|
|
SkScalar y = D/2 - (fm.fAscent + fm.fDescent)/2;
|
2015-07-28 17:37:53 +00:00
|
|
|
uint16_t glyphID = 3; // X
|
2018-10-25 20:12:39 +00:00
|
|
|
SkTextUtils::DrawText(canvas, &glyphID, sizeof(glyphID), x, y, *paint,
|
|
|
|
SkPaint::kCenter_Align);
|
2011-02-17 14:21:01 +00:00
|
|
|
canvas->restore();
|
|
|
|
}
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2015-07-28 17:37:53 +00:00
|
|
|
void onOnceBeforeDraw() override {
|
2017-12-08 19:25:14 +00:00
|
|
|
fEmFace = MakeResourceAsTypeface("fonts/Em.ttf");
|
2015-07-28 17:37:53 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2013-06-21 15:11:40 +00:00
|
|
|
if (false) { test_stroke(canvas); return; }
|
|
|
|
|
2011-02-17 14:21:01 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
2015-07-28 17:37:53 +00:00
|
|
|
paint.setTypeface(fEmFace);
|
|
|
|
paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
|
2011-02-17 14:21:01 +00:00
|
|
|
paint.setStrokeWidth(SkIntToScalar(4));
|
|
|
|
paint.setTextSize(SkIntToScalar(40));
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2011-02-17 14:21:01 +00:00
|
|
|
canvas->save();
|
|
|
|
canvas->translate(SkIntToScalar(10), SkIntToScalar(10));
|
|
|
|
// translate (1 point)
|
|
|
|
const int src1[] = { 0, 0 };
|
|
|
|
const int dst1[] = { 5, 5 };
|
|
|
|
doDraw(canvas, &paint, src1, dst1, 1);
|
|
|
|
canvas->restore();
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2011-02-17 14:21:01 +00:00
|
|
|
canvas->save();
|
|
|
|
canvas->translate(SkIntToScalar(160), SkIntToScalar(10));
|
|
|
|
// rotate/uniform-scale (2 points)
|
|
|
|
const int src2[] = { 32, 32, 64, 32 };
|
|
|
|
const int dst2[] = { 32, 32, 64, 48 };
|
|
|
|
doDraw(canvas, &paint, src2, dst2, 2);
|
|
|
|
canvas->restore();
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2011-02-17 14:21:01 +00:00
|
|
|
canvas->save();
|
|
|
|
canvas->translate(SkIntToScalar(10), SkIntToScalar(110));
|
|
|
|
// rotate/skew (3 points)
|
|
|
|
const int src3[] = { 0, 0, 64, 0, 0, 64 };
|
|
|
|
const int dst3[] = { 0, 0, 96, 0, 24, 64 };
|
|
|
|
doDraw(canvas, &paint, src3, dst3, 3);
|
|
|
|
canvas->restore();
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2011-02-17 14:21:01 +00:00
|
|
|
canvas->save();
|
|
|
|
canvas->translate(SkIntToScalar(160), SkIntToScalar(110));
|
|
|
|
// perspective (4 points)
|
|
|
|
const int src4[] = { 0, 0, 64, 0, 64, 64, 0, 64 };
|
|
|
|
const int dst4[] = { 0, 0, 96, 0, 64, 96, 0, 64 };
|
|
|
|
doDraw(canvas, &paint, src4, dst4, 4);
|
|
|
|
canvas->restore();
|
|
|
|
}
|
2012-08-23 18:14:13 +00:00
|
|
|
|
2011-02-17 14:21:01 +00:00
|
|
|
private:
|
2013-06-21 15:11:40 +00:00
|
|
|
typedef skiagm::GM INHERITED;
|
2016-05-12 17:09:30 +00:00
|
|
|
sk_sp<SkTypeface> fEmFace;
|
2011-02-17 14:21:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2013-06-21 15:11:40 +00:00
|
|
|
DEF_GM( return new Poly2PolyGM; )
|