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 | */ |
Mike Reed | 9cd2a5c | 2019-01-22 15:17:59 -0500 | [diff] [blame] | 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkCanvas.h" |
| 9 | #include "include/core/SkFontMetrics.h" |
| 10 | #include "include/core/SkGraphics.h" |
| 11 | #include "include/core/SkPath.h" |
| 12 | #include "include/core/SkString.h" |
| 13 | #include "include/core/SkTime.h" |
| 14 | #include "include/utils/SkRandom.h" |
| 15 | #include "include/utils/SkTextUtils.h" |
| 16 | #include "samplecode/Sample.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 17 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 18 | class PolyToPolyView : public Sample { |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 19 | public: |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 20 | PolyToPolyView() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 21 | // tests |
| 22 | { |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 23 | SkPoint src[] = { { 0, 0 }, |
| 24 | { SK_Scalar1, 0 }, |
| 25 | { 0, SK_Scalar1 } }; |
| 26 | SkPoint dst[] = { { 0, 0 }, |
| 27 | { 2*SK_Scalar1, 0 }, |
| 28 | { 0, 2*SK_Scalar1 } }; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 29 | SkMatrix m1, m2; |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 30 | |
djsollen@google.com | efbe8e9 | 2013-02-07 18:58:35 +0000 | [diff] [blame] | 31 | (void) m1.setPolyToPoly(src, dst, 3); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 32 | |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 33 | m2.reset(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 34 | m2.set(SkMatrix::kMScaleX, dst[1].fX - dst[0].fX); |
| 35 | m2.set(SkMatrix::kMSkewX, dst[2].fX - dst[0].fX); |
| 36 | m2.set(SkMatrix::kMTransX, dst[0].fX); |
| 37 | m2.set(SkMatrix::kMSkewY, dst[1].fY - dst[0].fY); |
| 38 | m2.set(SkMatrix::kMScaleY, dst[2].fY - dst[0].fY); |
| 39 | m2.set(SkMatrix::kMTransY, dst[0].fY); |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 40 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 41 | m1.reset(); |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 42 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 43 | const SkScalar src1[] = { |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 44 | 0, 0, 0, 427, 316, 427, 316, 0 |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 45 | }; |
| 46 | const SkScalar dst1[] = { |
commit-bot@chromium.org | 4b413c8 | 2013-11-25 19:44:07 +0000 | [diff] [blame] | 47 | 158, 177.5f, 158, 249.5f, |
| 48 | 158, 604.5f, 158, -177.5f |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 49 | }; |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 50 | |
djsollen@google.com | efbe8e9 | 2013-02-07 18:58:35 +0000 | [diff] [blame] | 51 | (void) m2.setPolyToPoly((const SkPoint*)src1, (SkPoint*)dst1, 4); |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 52 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 53 | { |
| 54 | const SkPoint src[] = { |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 55 | { SkIntToScalar(1), SkIntToScalar(0) }, |
| 56 | { SkIntToScalar(4), SkIntToScalar(7) }, |
| 57 | { SkIntToScalar(10), SkIntToScalar(2) } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 58 | }; |
| 59 | const SkPoint dst[] = { |
senorblanco@chromium.org | 64cc579 | 2011-05-19 19:58:58 +0000 | [diff] [blame] | 60 | { SkIntToScalar(4), SkIntToScalar(2) }, |
| 61 | { SkIntToScalar(45), SkIntToScalar(26) }, |
| 62 | { SkIntToScalar(32), SkIntToScalar(17) } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 63 | }; |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 64 | |
bsalomon@google.com | 820e80a | 2011-10-24 21:09:40 +0000 | [diff] [blame] | 65 | SkMatrix m0; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 66 | m0.setPolyToPoly(src, dst, 3); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | } |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 70 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 71 | protected: |
Brian Salomon | d007281 | 2020-07-21 17:03:56 -0400 | [diff] [blame] | 72 | SkString name() override { return SkString("PolyToPolyView"); } |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 73 | |
Mike Reed | 9191913 | 2019-01-02 12:21:01 -0500 | [diff] [blame] | 74 | static void doDraw(SkCanvas* canvas, SkPaint* paint, const SkFont& font, const int isrc[], |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 75 | const int idst[], int count) { |
| 76 | SkMatrix matrix; |
| 77 | SkPoint src[4], dst[4]; |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 78 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 79 | for (int i = 0; i < count; i++) { |
| 80 | src[i].set(SkIntToScalar(isrc[2*i+0]), SkIntToScalar(isrc[2*i+1])); |
| 81 | dst[i].set(SkIntToScalar(idst[2*i+0]), SkIntToScalar(idst[2*i+1])); |
| 82 | } |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 83 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 84 | canvas->save(); |
| 85 | matrix.setPolyToPoly(src, dst, count); |
| 86 | canvas->concat(matrix); |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 87 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 88 | paint->setColor(SK_ColorGRAY); |
| 89 | paint->setStyle(SkPaint::kStroke_Style); |
| 90 | const SkScalar D = SkIntToScalar(64); |
Mike Reed | 3661bc9 | 2017-02-22 13:21:42 -0500 | [diff] [blame] | 91 | canvas->drawRect(SkRect::MakeWH(D, D), *paint); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 92 | canvas->drawLine(0, 0, D, D, *paint); |
| 93 | canvas->drawLine(0, D, D, 0, *paint); |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 94 | |
Mike Reed | b5784ac | 2018-11-12 09:35:15 -0500 | [diff] [blame] | 95 | SkFontMetrics fm; |
Mike Reed | 9191913 | 2019-01-02 12:21:01 -0500 | [diff] [blame] | 96 | font.getMetrics(&fm); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 97 | paint->setColor(SK_ColorRED); |
| 98 | paint->setStyle(SkPaint::kFill_Style); |
| 99 | SkScalar x = D/2; |
| 100 | float y = D/2 - (fm.fAscent + fm.fDescent)/2; |
| 101 | SkString str; |
| 102 | str.appendS32(count); |
Mike Reed | 9191913 | 2019-01-02 12:21:01 -0500 | [diff] [blame] | 103 | SkTextUtils::DrawString(canvas, str.c_str(), x, y, font, *paint, SkTextUtils::kCenter_Align); |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 104 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 105 | canvas->restore(); |
| 106 | } |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 107 | |
Brian Salomon | d007281 | 2020-07-21 17:03:56 -0400 | [diff] [blame] | 108 | void onDrawContent(SkCanvas* canvas) override { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 109 | SkPaint paint; |
| 110 | paint.setAntiAlias(true); |
| 111 | paint.setStrokeWidth(SkIntToScalar(4)); |
Mike Reed | 9191913 | 2019-01-02 12:21:01 -0500 | [diff] [blame] | 112 | |
| 113 | SkFont font; |
| 114 | font.setSize(40); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 115 | |
| 116 | canvas->save(); |
| 117 | canvas->translate(SkIntToScalar(10), SkIntToScalar(10)); |
| 118 | // translate (1 point) |
| 119 | const int src1[] = { 0, 0 }; |
| 120 | const int dst1[] = { 5, 5 }; |
Mike Reed | 9191913 | 2019-01-02 12:21:01 -0500 | [diff] [blame] | 121 | doDraw(canvas, &paint, font, src1, dst1, 1); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 122 | canvas->restore(); |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 123 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 124 | canvas->save(); |
| 125 | canvas->translate(SkIntToScalar(160), SkIntToScalar(10)); |
| 126 | // rotate/uniform-scale (2 points) |
| 127 | const int src2[] = { 32, 32, 64, 32 }; |
| 128 | const int dst2[] = { 32, 32, 64, 48 }; |
Mike Reed | 9191913 | 2019-01-02 12:21:01 -0500 | [diff] [blame] | 129 | doDraw(canvas, &paint, font, src2, dst2, 2); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 130 | canvas->restore(); |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 131 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 132 | canvas->save(); |
| 133 | canvas->translate(SkIntToScalar(10), SkIntToScalar(110)); |
| 134 | // rotate/skew (3 points) |
| 135 | const int src3[] = { 0, 0, 64, 0, 0, 64 }; |
| 136 | const int dst3[] = { 0, 0, 96, 0, 24, 64 }; |
Mike Reed | 9191913 | 2019-01-02 12:21:01 -0500 | [diff] [blame] | 137 | doDraw(canvas, &paint, font, src3, dst3, 3); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 138 | canvas->restore(); |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 139 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 140 | canvas->save(); |
| 141 | canvas->translate(SkIntToScalar(160), SkIntToScalar(110)); |
| 142 | // perspective (4 points) |
| 143 | const int src4[] = { 0, 0, 64, 0, 64, 64, 0, 64 }; |
| 144 | const int dst4[] = { 0, 0, 96, 0, 64, 96, 0, 64 }; |
Mike Reed | 9191913 | 2019-01-02 12:21:01 -0500 | [diff] [blame] | 145 | doDraw(canvas, &paint, font, src4, dst4, 4); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 146 | canvas->restore(); |
| 147 | } |
reed@google.com | 67981c5 | 2011-02-17 14:21:01 +0000 | [diff] [blame] | 148 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 149 | private: |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 150 | using INHERITED = Sample; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 151 | }; |
| 152 | |
| 153 | ////////////////////////////////////////////////////////////////////////////// |
| 154 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 155 | DEF_SAMPLE( return new PolyToPolyView(); ) |