epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
reed@google.com | 8ea6983 | 2013-06-21 15:11:40 +0000 | [diff] [blame] | 7 | |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 8 | #include "gm.h" |
caryclark | f597c42 | 2015-07-28 10:37:53 -0700 | [diff] [blame] | 9 | #include "Resources.h" |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 10 | #include "SkPath.h" |
caryclark | f597c42 | 2015-07-28 10:37:53 -0700 | [diff] [blame] | 11 | #include "SkTypeface.h" |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 12 | |
reed@google.com | 8ea6983 | 2013-06-21 15:11:40 +0000 | [diff] [blame] | 13 | class SkJSCanvas { |
| 14 | public: |
| 15 | SkJSCanvas(SkCanvas* target); |
| 16 | ~SkJSCanvas(); |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 17 | |
reed@google.com | 8ea6983 | 2013-06-21 15:11:40 +0000 | [diff] [blame] | 18 | void save(); |
| 19 | void restore(); |
skia.committer@gmail.com | 020b25b | 2013-06-22 07:00:58 +0000 | [diff] [blame] | 20 | |
reed@google.com | 8ea6983 | 2013-06-21 15:11:40 +0000 | [diff] [blame] | 21 | double lineWidth; |
| 22 | void setLineWidth(double); |
skia.committer@gmail.com | 020b25b | 2013-06-22 07:00:58 +0000 | [diff] [blame] | 23 | |
reed@google.com | 8ea6983 | 2013-06-21 15:11:40 +0000 | [diff] [blame] | 24 | void beginPath(); |
| 25 | void moveTo(double x, double y); |
| 26 | void lineTo(double x, double y); |
| 27 | void closePath(); |
skia.committer@gmail.com | 020b25b | 2013-06-22 07:00:58 +0000 | [diff] [blame] | 28 | |
reed@google.com | 8ea6983 | 2013-06-21 15:11:40 +0000 | [diff] [blame] | 29 | void fill(); |
| 30 | void stroke(); |
skia.committer@gmail.com | 020b25b | 2013-06-22 07:00:58 +0000 | [diff] [blame] | 31 | |
reed@google.com | 8ea6983 | 2013-06-21 15:11:40 +0000 | [diff] [blame] | 32 | void fillText(const char text[], double x, double y); |
| 33 | |
| 34 | private: |
| 35 | SkCanvas* fTarget; |
| 36 | SkPaint fFillPaint; |
| 37 | SkPaint fStrokePaint; |
| 38 | SkPath fPath; |
| 39 | }; |
| 40 | |
| 41 | SkJSCanvas::SkJSCanvas(SkCanvas* target) : fTarget(target) { |
| 42 | fFillPaint.setAntiAlias(true); |
caryclark | 1818acb | 2015-07-24 12:09:25 -0700 | [diff] [blame] | 43 | sk_tool_utils::set_portable_typeface(&fFillPaint); |
reed@google.com | 8ea6983 | 2013-06-21 15:11:40 +0000 | [diff] [blame] | 44 | fStrokePaint.setAntiAlias(true); |
| 45 | fStrokePaint.setStyle(SkPaint::kStroke_Style); |
| 46 | fStrokePaint.setStrokeWidth(SK_Scalar1); |
| 47 | } |
| 48 | |
| 49 | SkJSCanvas::~SkJSCanvas() {} |
| 50 | |
| 51 | void SkJSCanvas::save() { fTarget->save(); } |
| 52 | void SkJSCanvas::restore() { fTarget->restore(); } |
| 53 | |
| 54 | void SkJSCanvas::beginPath() { fPath.reset(); } |
| 55 | void SkJSCanvas::moveTo(double x, double y) { |
| 56 | fPath.moveTo(SkDoubleToScalar(x), SkDoubleToScalar(y)); |
| 57 | } |
| 58 | |
| 59 | void SkJSCanvas::lineTo(double x, double y) { |
| 60 | fPath.lineTo(SkDoubleToScalar(x), SkDoubleToScalar(y)); |
| 61 | } |
| 62 | |
| 63 | void SkJSCanvas::closePath() { fPath.close(); } |
| 64 | |
| 65 | void SkJSCanvas::fill() { |
| 66 | fTarget->drawPath(fPath, fFillPaint); |
| 67 | } |
| 68 | |
| 69 | void SkJSCanvas::stroke() { |
| 70 | fStrokePaint.setStrokeWidth(SkDoubleToScalar(lineWidth)); |
| 71 | fTarget->drawPath(fPath, fStrokePaint); |
| 72 | } |
| 73 | |
| 74 | void SkJSCanvas::fillText(const char text[], double x, double y) { |
| 75 | fTarget->drawText(text, strlen(text), |
| 76 | SkDoubleToScalar(x), SkDoubleToScalar(y), fFillPaint); |
| 77 | } |
| 78 | |
| 79 | /////////////////////////////////////////////////////////////////////////////// |
| 80 | |
| 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); |
| 85 | } |
| 86 | |
| 87 | static void test_stroke(SkCanvas* canvas) { |
| 88 | if (true) { |
| 89 | SkPath path; |
| 90 | dump(path); |
| 91 | path.reset(); path.moveTo(0, 0); |
| 92 | dump(path); |
| 93 | path.reset(); path.moveTo(100, 100); |
| 94 | dump(path); |
| 95 | path.reset(); path.moveTo(0, 0); path.moveTo(100, 100); |
| 96 | dump(path); |
| 97 | path.reset(); path.moveTo(0, 0); path.lineTo(100, 100); |
| 98 | dump(path); |
| 99 | path.reset(); path.moveTo(0, 0); path.lineTo(100, 100); path.moveTo(200, 200); |
| 100 | dump(path); |
| 101 | } |
skia.committer@gmail.com | 020b25b | 2013-06-22 07:00:58 +0000 | [diff] [blame] | 102 | |
reed@google.com | 8ea6983 | 2013-06-21 15:11:40 +0000 | [diff] [blame] | 103 | #if 0 |
| 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"); |
| 108 | #else |
| 109 | SkJSCanvas ctx(canvas); |
| 110 | #endif |
| 111 | |
| 112 | ctx.save(); |
| 113 | ctx.lineWidth = 2; |
| 114 | ctx.beginPath(); |
| 115 | ctx.moveTo(10, 100); |
| 116 | ctx.lineTo(150, 100); |
| 117 | ctx.lineTo(150, 15); |
| 118 | ctx.lineTo(10, 15); |
| 119 | ctx.closePath(); |
skia.committer@gmail.com | 020b25b | 2013-06-22 07:00:58 +0000 | [diff] [blame] | 120 | |
reed@google.com | 8ea6983 | 2013-06-21 15:11:40 +0000 | [diff] [blame] | 121 | // no extra moveTo here |
| 122 | // ctx.moveTo(175, 125); |
skia.committer@gmail.com | 020b25b | 2013-06-22 07:00:58 +0000 | [diff] [blame] | 123 | |
reed@google.com | 8ea6983 | 2013-06-21 15:11:40 +0000 | [diff] [blame] | 124 | ctx.stroke(); |
| 125 | ctx.restore(); |
skia.committer@gmail.com | 020b25b | 2013-06-22 07:00:58 +0000 | [diff] [blame] | 126 | |
reed@google.com | 8ea6983 | 2013-06-21 15:11:40 +0000 | [diff] [blame] | 127 | ctx.fillText("As Expected", 10, 10); |
skia.committer@gmail.com | 020b25b | 2013-06-22 07:00:58 +0000 | [diff] [blame] | 128 | |
reed@google.com | 8ea6983 | 2013-06-21 15:11:40 +0000 | [diff] [blame] | 129 | #if 0 |
| 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"); |
| 134 | #else |
| 135 | canvas->translate(200, 0); |
| 136 | #endif |
| 137 | |
| 138 | ctx.save(); |
| 139 | ctx.lineWidth = 2; |
| 140 | ctx.beginPath(); |
| 141 | ctx.moveTo(10, 100); |
| 142 | ctx.lineTo(150, 100); |
| 143 | ctx.lineTo(150, 15); |
| 144 | ctx.lineTo(10, 15); |
| 145 | ctx.closePath(); |
skia.committer@gmail.com | 020b25b | 2013-06-22 07:00:58 +0000 | [diff] [blame] | 146 | |
reed@google.com | 8ea6983 | 2013-06-21 15:11:40 +0000 | [diff] [blame] | 147 | ctx.moveTo(175, 125); |
skia.committer@gmail.com | 020b25b | 2013-06-22 07:00:58 +0000 | [diff] [blame] | 148 | |
reed@google.com | 8ea6983 | 2013-06-21 15:11:40 +0000 | [diff] [blame] | 149 | ctx.stroke(); |
| 150 | ctx.restore(); |
skia.committer@gmail.com | 020b25b | 2013-06-22 07:00:58 +0000 | [diff] [blame] | 151 | |
reed@google.com | 8ea6983 | 2013-06-21 15:11:40 +0000 | [diff] [blame] | 152 | ctx.fillText("Larger Rectangle", 10, 10); |
skia.committer@gmail.com | 020b25b | 2013-06-22 07:00:58 +0000 | [diff] [blame] | 153 | |
reed@google.com | 8ea6983 | 2013-06-21 15:11:40 +0000 | [diff] [blame] | 154 | #if 0 |
| 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"); |
| 159 | #else |
| 160 | canvas->translate(200, 0); |
| 161 | #endif |
| 162 | |
| 163 | ctx.save(); |
| 164 | ctx.lineWidth = 1; |
| 165 | ctx.beginPath(); |
| 166 | ctx.moveTo(10, 100); |
| 167 | ctx.lineTo(150, 100); |
| 168 | ctx.lineTo(150, 15); |
| 169 | ctx.lineTo(10, 15); |
| 170 | ctx.closePath(); |
skia.committer@gmail.com | 020b25b | 2013-06-22 07:00:58 +0000 | [diff] [blame] | 171 | |
reed@google.com | 8ea6983 | 2013-06-21 15:11:40 +0000 | [diff] [blame] | 172 | ctx.moveTo(175, 125); |
skia.committer@gmail.com | 020b25b | 2013-06-22 07:00:58 +0000 | [diff] [blame] | 173 | |
reed@google.com | 8ea6983 | 2013-06-21 15:11:40 +0000 | [diff] [blame] | 174 | ctx.stroke(); |
| 175 | ctx.restore(); |
skia.committer@gmail.com | 020b25b | 2013-06-22 07:00:58 +0000 | [diff] [blame] | 176 | |
reed@google.com | 8ea6983 | 2013-06-21 15:11:40 +0000 | [diff] [blame] | 177 | ctx.fillText("As Expected - line width 1", 10, 10); |
| 178 | } |
| 179 | |
| 180 | class Poly2PolyGM : public skiagm::GM { |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 181 | public: |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 182 | Poly2PolyGM() {} |
| 183 | |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 184 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 185 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 186 | SkString onShortName() override { |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 187 | return SkString("poly2poly"); |
| 188 | } |
| 189 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 190 | SkISize onISize() override { |
reed@google.com | 8ea6983 | 2013-06-21 15:11:40 +0000 | [diff] [blame] | 191 | return SkISize::Make(835, 840); |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 192 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 193 | |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 194 | static void doDraw(SkCanvas* canvas, SkPaint* paint, const int isrc[], |
| 195 | const int idst[], int count) { |
| 196 | SkMatrix matrix; |
| 197 | SkPoint src[4], dst[4]; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 198 | |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 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])); |
| 202 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 203 | |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 204 | canvas->save(); |
| 205 | matrix.setPolyToPoly(src, dst, count); |
| 206 | canvas->concat(matrix); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 207 | |
caryclark | 1e545b6 | 2015-07-13 08:19:58 -0700 | [diff] [blame] | 208 | paint->setColor(sk_tool_utils::color_to_565(SK_ColorGRAY)); |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 209 | paint->setStyle(SkPaint::kStroke_Style); |
| 210 | const SkScalar D = SkIntToScalar(64); |
| 211 | canvas->drawRectCoords(0, 0, D, D, *paint); |
| 212 | canvas->drawLine(0, 0, D, D, *paint); |
| 213 | canvas->drawLine(0, D, D, 0, *paint); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 214 | |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 215 | SkPaint::FontMetrics fm; |
| 216 | paint->getFontMetrics(&fm); |
| 217 | paint->setColor(SK_ColorRED); |
| 218 | paint->setStyle(SkPaint::kFill_Style); |
| 219 | SkScalar x = D/2; |
bsalomon@google.com | 72e49b8 | 2011-10-27 21:47:03 +0000 | [diff] [blame] | 220 | SkScalar y = D/2 - (fm.fAscent + fm.fDescent)/2; |
caryclark | f597c42 | 2015-07-28 10:37:53 -0700 | [diff] [blame] | 221 | uint16_t glyphID = 3; // X |
| 222 | canvas->drawText((void*) &glyphID, sizeof(glyphID), x, y, *paint); |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 223 | canvas->restore(); |
| 224 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 225 | |
caryclark | f597c42 | 2015-07-28 10:37:53 -0700 | [diff] [blame] | 226 | void onOnceBeforeDraw() override { |
| 227 | fEmFace.reset(GetResourceAsTypeface("/fonts/Em.ttf")); |
| 228 | } |
| 229 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 230 | void onDraw(SkCanvas* canvas) override { |
reed@google.com | 8ea6983 | 2013-06-21 15:11:40 +0000 | [diff] [blame] | 231 | if (false) { test_stroke(canvas); return; } |
| 232 | |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 233 | SkPaint paint; |
| 234 | paint.setAntiAlias(true); |
caryclark | f597c42 | 2015-07-28 10:37:53 -0700 | [diff] [blame] | 235 | paint.setTypeface(fEmFace); |
| 236 | paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 237 | paint.setStrokeWidth(SkIntToScalar(4)); |
| 238 | paint.setTextSize(SkIntToScalar(40)); |
| 239 | paint.setTextAlign(SkPaint::kCenter_Align); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 240 | |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 241 | canvas->save(); |
| 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); |
| 247 | canvas->restore(); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 248 | |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 249 | canvas->save(); |
| 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); |
| 255 | canvas->restore(); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 256 | |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 257 | canvas->save(); |
| 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); |
| 263 | canvas->restore(); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 264 | |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 265 | canvas->save(); |
| 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); |
| 271 | canvas->restore(); |
| 272 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 273 | |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 274 | private: |
reed@google.com | 8ea6983 | 2013-06-21 15:11:40 +0000 | [diff] [blame] | 275 | typedef skiagm::GM INHERITED; |
caryclark | f597c42 | 2015-07-28 10:37:53 -0700 | [diff] [blame] | 276 | SkAutoTUnref<SkTypeface> fEmFace; |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 277 | }; |
| 278 | |
| 279 | ////////////////////////////////////////////////////////////////////////////// |
| 280 | |
reed@google.com | 8ea6983 | 2013-06-21 15:11:40 +0000 | [diff] [blame] | 281 | DEF_GM( return new Poly2PolyGM; ) |