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