2 * Copyright 2011 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
9 #include "sk_tool_utils.h"
10 #include "Resources.h"
12 #include "SkTypeface.h"
16 SkJSCanvas(SkCanvas* target);
23 void setLineWidth(double);
26 void moveTo(double x, double y);
27 void lineTo(double x, double y);
33 void fillText(const char text[], double x, double y);
42 SkJSCanvas::SkJSCanvas(SkCanvas* target) : fTarget(target) {
43 fFillPaint.setAntiAlias(true);
44 sk_tool_utils::set_portable_typeface(&fFillPaint);
45 fStrokePaint.setAntiAlias(true);
46 fStrokePaint.setStyle(SkPaint::kStroke_Style);
47 fStrokePaint.setStrokeWidth(SK_Scalar1);
50 SkJSCanvas::~SkJSCanvas() {}
52 void SkJSCanvas::save() { fTarget->save(); }
53 void SkJSCanvas::restore() { fTarget->restore(); }
55 void SkJSCanvas::beginPath() { fPath.reset(); }
56 void SkJSCanvas::moveTo(double x, double y) {
57 fPath.moveTo(SkDoubleToScalar(x), SkDoubleToScalar(y));
60 void SkJSCanvas::lineTo(double x, double y) {
61 fPath.lineTo(SkDoubleToScalar(x), SkDoubleToScalar(y));
64 void SkJSCanvas::closePath() { fPath.close(); }
66 void SkJSCanvas::fill() {
67 fTarget->drawPath(fPath, fFillPaint);
70 void SkJSCanvas::stroke() {
71 fStrokePaint.setStrokeWidth(SkDoubleToScalar(lineWidth));
72 fTarget->drawPath(fPath, fStrokePaint);
75 void SkJSCanvas::fillText(const char text[], double x, double y) {
76 fTarget->drawString(text, SkDoubleToScalar(x), SkDoubleToScalar(y), fFillPaint);
79 ///////////////////////////////////////////////////////////////////////////////
81 static void dump(const SkPath& path) {
82 const SkRect& r = path.getBounds();
83 SkDebugf("isEmpty %d, bounds [%g %g %g %g]\n", path.isEmpty(),
84 r.fLeft, r.fTop, r.fRight, r.fBottom);
87 static void test_stroke(SkCanvas* canvas) {
91 path.reset(); path.moveTo(0, 0);
93 path.reset(); path.moveTo(100, 100);
95 path.reset(); path.moveTo(0, 0); path.moveTo(100, 100);
97 path.reset(); path.moveTo(0, 0); path.lineTo(100, 100);
99 path.reset(); path.moveTo(0, 0); path.lineTo(100, 100); path.moveTo(200, 200);
104 // TEST 1 - The rectangle as it's expected to look
105 var canvas = document.createElement('canvas');
106 document.body.appendChild(canvas);
107 var ctx = canvas.getContext("2d");
109 SkJSCanvas ctx(canvas);
116 ctx.lineTo(150, 100);
121 // no extra moveTo here
122 // ctx.moveTo(175, 125);
127 ctx.fillText("As Expected", 10, 10);
130 // TEST 2 - Includes an extra moveTo call before stroke; the rectangle appears larger
131 canvas = document.createElement('canvas');
132 document.body.appendChild(canvas);
133 ctx = canvas.getContext("2d");
135 canvas->translate(200, 0);
142 ctx.lineTo(150, 100);
147 ctx.moveTo(175, 125);
152 ctx.fillText("Larger Rectangle", 10, 10);
155 // TEST 3 - Identical to test 2 except the line width is 1
156 canvas = document.createElement('canvas');
157 document.body.appendChild(canvas);
158 ctx = canvas.getContext("2d");
160 canvas->translate(200, 0);
167 ctx.lineTo(150, 100);
172 ctx.moveTo(175, 125);
177 ctx.fillText("As Expected - line width 1", 10, 10);
180 class Poly2PolyGM : public skiagm::GM {
186 SkString onShortName() override {
187 return SkString("poly2poly");
190 SkISize onISize() override {
191 return SkISize::Make(835, 840);
194 static void doDraw(SkCanvas* canvas, SkPaint* paint, const int isrc[],
195 const int idst[], int count) {
197 SkPoint src[4], dst[4];
199 for (int i = 0; i < count; i++) {
200 src[i].set(SkIntToScalar(isrc[2*i+0]), SkIntToScalar(isrc[2*i+1]));
201 dst[i].set(SkIntToScalar(idst[2*i+0]), SkIntToScalar(idst[2*i+1]));
205 matrix.setPolyToPoly(src, dst, count);
206 canvas->concat(matrix);
208 paint->setColor(sk_tool_utils::color_to_565(SK_ColorGRAY));
209 paint->setStyle(SkPaint::kStroke_Style);
210 const SkScalar D = 64;
211 canvas->drawRect(SkRect::MakeWH(D, D), *paint);
212 canvas->drawLine(0, 0, D, D, *paint);
213 canvas->drawLine(0, D, D, 0, *paint);
215 SkPaint::FontMetrics fm;
216 paint->getFontMetrics(&fm);
217 paint->setColor(SK_ColorRED);
218 paint->setStyle(SkPaint::kFill_Style);
220 SkScalar y = D/2 - (fm.fAscent + fm.fDescent)/2;
221 uint16_t glyphID = 3; // X
222 canvas->drawText((void*) &glyphID, sizeof(glyphID), x, y, *paint);
226 void onOnceBeforeDraw() override {
227 fEmFace = MakeResourceAsTypeface("/fonts/Em.ttf");
230 void onDraw(SkCanvas* canvas) override {
231 if (false) { test_stroke(canvas); return; }
234 paint.setAntiAlias(true);
235 paint.setTypeface(fEmFace);
236 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
237 paint.setStrokeWidth(SkIntToScalar(4));
238 paint.setTextSize(SkIntToScalar(40));
239 paint.setTextAlign(SkPaint::kCenter_Align);
242 canvas->translate(SkIntToScalar(10), SkIntToScalar(10));
243 // translate (1 point)
244 const int src1[] = { 0, 0 };
245 const int dst1[] = { 5, 5 };
246 doDraw(canvas, &paint, src1, dst1, 1);
250 canvas->translate(SkIntToScalar(160), SkIntToScalar(10));
251 // rotate/uniform-scale (2 points)
252 const int src2[] = { 32, 32, 64, 32 };
253 const int dst2[] = { 32, 32, 64, 48 };
254 doDraw(canvas, &paint, src2, dst2, 2);
258 canvas->translate(SkIntToScalar(10), SkIntToScalar(110));
259 // rotate/skew (3 points)
260 const int src3[] = { 0, 0, 64, 0, 0, 64 };
261 const int dst3[] = { 0, 0, 96, 0, 24, 64 };
262 doDraw(canvas, &paint, src3, dst3, 3);
266 canvas->translate(SkIntToScalar(160), SkIntToScalar(110));
267 // perspective (4 points)
268 const int src4[] = { 0, 0, 64, 0, 64, 64, 0, 64 };
269 const int dst4[] = { 0, 0, 96, 0, 64, 96, 0, 64 };
270 doDraw(canvas, &paint, src4, dst4, 4);
275 typedef skiagm::GM INHERITED;
276 sk_sp<SkTypeface> fEmFace;
279 //////////////////////////////////////////////////////////////////////////////
281 DEF_GM( return new Poly2PolyGM; )