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" |
Mike Klein | 33d2055 | 2017-03-22 13:47:51 -0400 | [diff] [blame] | 9 | #include "sk_tool_utils.h" |
caryclark | f597c42 | 2015-07-28 10:37:53 -0700 | [diff] [blame] | 10 | #include "Resources.h" |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 11 | #include "SkPath.h" |
Mike Reed | 71f5a0b | 2018-10-25 16:12:39 -0400 | [diff] [blame] | 12 | #include "SkTextUtils.h" |
caryclark | f597c42 | 2015-07-28 10:37:53 -0700 | [diff] [blame] | 13 | #include "SkTypeface.h" |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 14 | |
reed@google.com | 8ea6983 | 2013-06-21 15:11:40 +0000 | [diff] [blame] | 15 | class SkJSCanvas { |
| 16 | public: |
| 17 | SkJSCanvas(SkCanvas* target); |
| 18 | ~SkJSCanvas(); |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 19 | |
reed@google.com | 8ea6983 | 2013-06-21 15:11:40 +0000 | [diff] [blame] | 20 | void save(); |
| 21 | void restore(); |
skia.committer@gmail.com | 020b25b | 2013-06-22 07:00:58 +0000 | [diff] [blame] | 22 | |
reed@google.com | 8ea6983 | 2013-06-21 15:11:40 +0000 | [diff] [blame] | 23 | double lineWidth; |
| 24 | void setLineWidth(double); |
skia.committer@gmail.com | 020b25b | 2013-06-22 07:00:58 +0000 | [diff] [blame] | 25 | |
reed@google.com | 8ea6983 | 2013-06-21 15:11:40 +0000 | [diff] [blame] | 26 | void beginPath(); |
| 27 | void moveTo(double x, double y); |
| 28 | void lineTo(double x, double y); |
| 29 | void closePath(); |
skia.committer@gmail.com | 020b25b | 2013-06-22 07:00:58 +0000 | [diff] [blame] | 30 | |
reed@google.com | 8ea6983 | 2013-06-21 15:11:40 +0000 | [diff] [blame] | 31 | void fill(); |
| 32 | void stroke(); |
skia.committer@gmail.com | 020b25b | 2013-06-22 07:00:58 +0000 | [diff] [blame] | 33 | |
reed@google.com | 8ea6983 | 2013-06-21 15:11:40 +0000 | [diff] [blame] | 34 | void fillText(const char text[], double x, double y); |
| 35 | |
| 36 | private: |
| 37 | SkCanvas* fTarget; |
| 38 | SkPaint fFillPaint; |
| 39 | SkPaint fStrokePaint; |
| 40 | SkPath fPath; |
| 41 | }; |
| 42 | |
| 43 | SkJSCanvas::SkJSCanvas(SkCanvas* target) : fTarget(target) { |
| 44 | fFillPaint.setAntiAlias(true); |
caryclark | 1818acb | 2015-07-24 12:09:25 -0700 | [diff] [blame] | 45 | sk_tool_utils::set_portable_typeface(&fFillPaint); |
reed@google.com | 8ea6983 | 2013-06-21 15:11:40 +0000 | [diff] [blame] | 46 | fStrokePaint.setAntiAlias(true); |
| 47 | fStrokePaint.setStyle(SkPaint::kStroke_Style); |
| 48 | fStrokePaint.setStrokeWidth(SK_Scalar1); |
| 49 | } |
| 50 | |
| 51 | SkJSCanvas::~SkJSCanvas() {} |
| 52 | |
| 53 | void SkJSCanvas::save() { fTarget->save(); } |
| 54 | void SkJSCanvas::restore() { fTarget->restore(); } |
| 55 | |
| 56 | void SkJSCanvas::beginPath() { fPath.reset(); } |
| 57 | void SkJSCanvas::moveTo(double x, double y) { |
| 58 | fPath.moveTo(SkDoubleToScalar(x), SkDoubleToScalar(y)); |
| 59 | } |
| 60 | |
| 61 | void SkJSCanvas::lineTo(double x, double y) { |
| 62 | fPath.lineTo(SkDoubleToScalar(x), SkDoubleToScalar(y)); |
| 63 | } |
| 64 | |
| 65 | void SkJSCanvas::closePath() { fPath.close(); } |
| 66 | |
| 67 | void SkJSCanvas::fill() { |
| 68 | fTarget->drawPath(fPath, fFillPaint); |
| 69 | } |
| 70 | |
| 71 | void SkJSCanvas::stroke() { |
| 72 | fStrokePaint.setStrokeWidth(SkDoubleToScalar(lineWidth)); |
| 73 | fTarget->drawPath(fPath, fStrokePaint); |
| 74 | } |
| 75 | |
| 76 | void SkJSCanvas::fillText(const char text[], double x, double y) { |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 77 | fTarget->drawString(text, SkDoubleToScalar(x), SkDoubleToScalar(y), fFillPaint); |
reed@google.com | 8ea6983 | 2013-06-21 15:11:40 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | /////////////////////////////////////////////////////////////////////////////// |
| 81 | |
| 82 | static void dump(const SkPath& path) { |
| 83 | const SkRect& r = path.getBounds(); |
| 84 | SkDebugf("isEmpty %d, bounds [%g %g %g %g]\n", path.isEmpty(), |
| 85 | r.fLeft, r.fTop, r.fRight, r.fBottom); |
| 86 | } |
| 87 | |
| 88 | static void test_stroke(SkCanvas* canvas) { |
| 89 | if (true) { |
| 90 | SkPath path; |
| 91 | dump(path); |
| 92 | path.reset(); path.moveTo(0, 0); |
| 93 | dump(path); |
| 94 | path.reset(); path.moveTo(100, 100); |
| 95 | dump(path); |
| 96 | path.reset(); path.moveTo(0, 0); path.moveTo(100, 100); |
| 97 | dump(path); |
| 98 | path.reset(); path.moveTo(0, 0); path.lineTo(100, 100); |
| 99 | dump(path); |
| 100 | path.reset(); path.moveTo(0, 0); path.lineTo(100, 100); path.moveTo(200, 200); |
| 101 | dump(path); |
| 102 | } |
skia.committer@gmail.com | 020b25b | 2013-06-22 07:00:58 +0000 | [diff] [blame] | 103 | |
reed@google.com | 8ea6983 | 2013-06-21 15:11:40 +0000 | [diff] [blame] | 104 | #if 0 |
| 105 | // TEST 1 - The rectangle as it's expected to look |
| 106 | var canvas = document.createElement('canvas'); |
| 107 | document.body.appendChild(canvas); |
| 108 | var ctx = canvas.getContext("2d"); |
| 109 | #else |
| 110 | SkJSCanvas ctx(canvas); |
| 111 | #endif |
| 112 | |
| 113 | ctx.save(); |
| 114 | ctx.lineWidth = 2; |
| 115 | ctx.beginPath(); |
| 116 | ctx.moveTo(10, 100); |
| 117 | ctx.lineTo(150, 100); |
| 118 | ctx.lineTo(150, 15); |
| 119 | ctx.lineTo(10, 15); |
| 120 | ctx.closePath(); |
skia.committer@gmail.com | 020b25b | 2013-06-22 07:00:58 +0000 | [diff] [blame] | 121 | |
reed@google.com | 8ea6983 | 2013-06-21 15:11:40 +0000 | [diff] [blame] | 122 | // no extra moveTo here |
| 123 | // ctx.moveTo(175, 125); |
skia.committer@gmail.com | 020b25b | 2013-06-22 07:00:58 +0000 | [diff] [blame] | 124 | |
reed@google.com | 8ea6983 | 2013-06-21 15:11:40 +0000 | [diff] [blame] | 125 | ctx.stroke(); |
| 126 | ctx.restore(); |
skia.committer@gmail.com | 020b25b | 2013-06-22 07:00:58 +0000 | [diff] [blame] | 127 | |
reed@google.com | 8ea6983 | 2013-06-21 15:11:40 +0000 | [diff] [blame] | 128 | ctx.fillText("As Expected", 10, 10); |
skia.committer@gmail.com | 020b25b | 2013-06-22 07:00:58 +0000 | [diff] [blame] | 129 | |
reed@google.com | 8ea6983 | 2013-06-21 15:11:40 +0000 | [diff] [blame] | 130 | #if 0 |
| 131 | // TEST 2 - Includes an extra moveTo call before stroke; the rectangle appears larger |
| 132 | canvas = document.createElement('canvas'); |
| 133 | document.body.appendChild(canvas); |
| 134 | ctx = canvas.getContext("2d"); |
| 135 | #else |
| 136 | canvas->translate(200, 0); |
| 137 | #endif |
| 138 | |
| 139 | ctx.save(); |
| 140 | ctx.lineWidth = 2; |
| 141 | ctx.beginPath(); |
| 142 | ctx.moveTo(10, 100); |
| 143 | ctx.lineTo(150, 100); |
| 144 | ctx.lineTo(150, 15); |
| 145 | ctx.lineTo(10, 15); |
| 146 | ctx.closePath(); |
skia.committer@gmail.com | 020b25b | 2013-06-22 07:00:58 +0000 | [diff] [blame] | 147 | |
reed@google.com | 8ea6983 | 2013-06-21 15:11:40 +0000 | [diff] [blame] | 148 | ctx.moveTo(175, 125); |
skia.committer@gmail.com | 020b25b | 2013-06-22 07:00:58 +0000 | [diff] [blame] | 149 | |
reed@google.com | 8ea6983 | 2013-06-21 15:11:40 +0000 | [diff] [blame] | 150 | ctx.stroke(); |
| 151 | ctx.restore(); |
skia.committer@gmail.com | 020b25b | 2013-06-22 07:00:58 +0000 | [diff] [blame] | 152 | |
reed@google.com | 8ea6983 | 2013-06-21 15:11:40 +0000 | [diff] [blame] | 153 | ctx.fillText("Larger Rectangle", 10, 10); |
skia.committer@gmail.com | 020b25b | 2013-06-22 07:00:58 +0000 | [diff] [blame] | 154 | |
reed@google.com | 8ea6983 | 2013-06-21 15:11:40 +0000 | [diff] [blame] | 155 | #if 0 |
| 156 | // TEST 3 - Identical to test 2 except the line width is 1 |
| 157 | canvas = document.createElement('canvas'); |
| 158 | document.body.appendChild(canvas); |
| 159 | ctx = canvas.getContext("2d"); |
| 160 | #else |
| 161 | canvas->translate(200, 0); |
| 162 | #endif |
| 163 | |
| 164 | ctx.save(); |
| 165 | ctx.lineWidth = 1; |
| 166 | ctx.beginPath(); |
| 167 | ctx.moveTo(10, 100); |
| 168 | ctx.lineTo(150, 100); |
| 169 | ctx.lineTo(150, 15); |
| 170 | ctx.lineTo(10, 15); |
| 171 | ctx.closePath(); |
skia.committer@gmail.com | 020b25b | 2013-06-22 07:00:58 +0000 | [diff] [blame] | 172 | |
reed@google.com | 8ea6983 | 2013-06-21 15:11:40 +0000 | [diff] [blame] | 173 | ctx.moveTo(175, 125); |
skia.committer@gmail.com | 020b25b | 2013-06-22 07:00:58 +0000 | [diff] [blame] | 174 | |
reed@google.com | 8ea6983 | 2013-06-21 15:11:40 +0000 | [diff] [blame] | 175 | ctx.stroke(); |
| 176 | ctx.restore(); |
skia.committer@gmail.com | 020b25b | 2013-06-22 07:00:58 +0000 | [diff] [blame] | 177 | |
reed@google.com | 8ea6983 | 2013-06-21 15:11:40 +0000 | [diff] [blame] | 178 | ctx.fillText("As Expected - line width 1", 10, 10); |
| 179 | } |
| 180 | |
| 181 | class Poly2PolyGM : public skiagm::GM { |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 182 | public: |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 183 | Poly2PolyGM() {} |
| 184 | |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 185 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 186 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 187 | SkString onShortName() override { |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 188 | return SkString("poly2poly"); |
| 189 | } |
| 190 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 191 | SkISize onISize() override { |
reed@google.com | 8ea6983 | 2013-06-21 15:11:40 +0000 | [diff] [blame] | 192 | return SkISize::Make(835, 840); |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 193 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 194 | |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 195 | static void doDraw(SkCanvas* canvas, SkPaint* paint, const int isrc[], |
| 196 | const int idst[], int count) { |
| 197 | SkMatrix matrix; |
| 198 | SkPoint src[4], dst[4]; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 199 | |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 200 | for (int i = 0; i < count; i++) { |
| 201 | src[i].set(SkIntToScalar(isrc[2*i+0]), SkIntToScalar(isrc[2*i+1])); |
| 202 | dst[i].set(SkIntToScalar(idst[2*i+0]), SkIntToScalar(idst[2*i+1])); |
| 203 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 204 | |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 205 | canvas->save(); |
| 206 | matrix.setPolyToPoly(src, dst, count); |
| 207 | canvas->concat(matrix); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 208 | |
Mike Klein | d46dce3 | 2018-08-16 10:17:03 -0400 | [diff] [blame] | 209 | paint->setColor(SK_ColorGRAY); |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 210 | paint->setStyle(SkPaint::kStroke_Style); |
Mike Reed | 3661bc9 | 2017-02-22 13:21:42 -0500 | [diff] [blame] | 211 | const SkScalar D = 64; |
| 212 | canvas->drawRect(SkRect::MakeWH(D, D), *paint); |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 213 | canvas->drawLine(0, 0, D, D, *paint); |
| 214 | canvas->drawLine(0, D, D, 0, *paint); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 215 | |
Mike Reed | b5784ac | 2018-11-12 09:35:15 -0500 | [diff] [blame] | 216 | SkFontMetrics fm; |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 217 | paint->getFontMetrics(&fm); |
| 218 | paint->setColor(SK_ColorRED); |
| 219 | paint->setStyle(SkPaint::kFill_Style); |
| 220 | SkScalar x = D/2; |
bsalomon@google.com | 72e49b8 | 2011-10-27 21:47:03 +0000 | [diff] [blame] | 221 | SkScalar y = D/2 - (fm.fAscent + fm.fDescent)/2; |
caryclark | f597c42 | 2015-07-28 10:37:53 -0700 | [diff] [blame] | 222 | uint16_t glyphID = 3; // X |
Mike Reed | 71f5a0b | 2018-10-25 16:12:39 -0400 | [diff] [blame] | 223 | SkTextUtils::DrawText(canvas, &glyphID, sizeof(glyphID), x, y, *paint, |
Mike Reed | 3a42ec0 | 2018-10-30 12:53:21 -0400 | [diff] [blame] | 224 | SkTextUtils::kCenter_Align); |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 225 | canvas->restore(); |
| 226 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 227 | |
caryclark | f597c42 | 2015-07-28 10:37:53 -0700 | [diff] [blame] | 228 | void onOnceBeforeDraw() override { |
Hal Canary | 53e5e7d | 2017-12-08 14:25:14 -0500 | [diff] [blame] | 229 | fEmFace = MakeResourceAsTypeface("fonts/Em.ttf"); |
caryclark | f597c42 | 2015-07-28 10:37:53 -0700 | [diff] [blame] | 230 | } |
| 231 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 232 | void onDraw(SkCanvas* canvas) override { |
reed@google.com | 8ea6983 | 2013-06-21 15:11:40 +0000 | [diff] [blame] | 233 | if (false) { test_stroke(canvas); return; } |
| 234 | |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 235 | SkPaint paint; |
| 236 | paint.setAntiAlias(true); |
caryclark | f597c42 | 2015-07-28 10:37:53 -0700 | [diff] [blame] | 237 | paint.setTypeface(fEmFace); |
Mike Reed | 97f3cc2 | 2018-12-03 09:45:17 -0500 | [diff] [blame] | 238 | paint.setTextEncoding(kGlyphID_SkTextEncoding); |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 239 | paint.setStrokeWidth(SkIntToScalar(4)); |
| 240 | paint.setTextSize(SkIntToScalar(40)); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 241 | |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 242 | canvas->save(); |
| 243 | canvas->translate(SkIntToScalar(10), SkIntToScalar(10)); |
| 244 | // translate (1 point) |
| 245 | const int src1[] = { 0, 0 }; |
| 246 | const int dst1[] = { 5, 5 }; |
| 247 | doDraw(canvas, &paint, src1, dst1, 1); |
| 248 | canvas->restore(); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 249 | |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 250 | canvas->save(); |
| 251 | canvas->translate(SkIntToScalar(160), SkIntToScalar(10)); |
| 252 | // rotate/uniform-scale (2 points) |
| 253 | const int src2[] = { 32, 32, 64, 32 }; |
| 254 | const int dst2[] = { 32, 32, 64, 48 }; |
| 255 | doDraw(canvas, &paint, src2, dst2, 2); |
| 256 | canvas->restore(); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 257 | |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 258 | canvas->save(); |
| 259 | canvas->translate(SkIntToScalar(10), SkIntToScalar(110)); |
| 260 | // rotate/skew (3 points) |
| 261 | const int src3[] = { 0, 0, 64, 0, 0, 64 }; |
| 262 | const int dst3[] = { 0, 0, 96, 0, 24, 64 }; |
| 263 | doDraw(canvas, &paint, src3, dst3, 3); |
| 264 | canvas->restore(); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 265 | |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 266 | canvas->save(); |
| 267 | canvas->translate(SkIntToScalar(160), SkIntToScalar(110)); |
| 268 | // perspective (4 points) |
| 269 | const int src4[] = { 0, 0, 64, 0, 64, 64, 0, 64 }; |
| 270 | const int dst4[] = { 0, 0, 96, 0, 64, 96, 0, 64 }; |
| 271 | doDraw(canvas, &paint, src4, dst4, 4); |
| 272 | canvas->restore(); |
| 273 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 274 | |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 275 | private: |
reed@google.com | 8ea6983 | 2013-06-21 15:11:40 +0000 | [diff] [blame] | 276 | typedef skiagm::GM INHERITED; |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 277 | sk_sp<SkTypeface> fEmFace; |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 278 | }; |
| 279 | |
| 280 | ////////////////////////////////////////////////////////////////////////////// |
| 281 | |
reed@google.com | 8ea6983 | 2013-06-21 15:11:40 +0000 | [diff] [blame] | 282 | DEF_GM( return new Poly2PolyGM; ) |