reed@google.com | c280700 | 2011-04-11 13:57:04 +0000 | [diff] [blame] | 1 | #include "gm.h" |
| 2 | #include "SkPicture.h" |
| 3 | #include "SkRectShape.h" |
| 4 | #include "SkGroupShape.h" |
| 5 | |
| 6 | typedef SkScalar (*MakePathProc)(SkPath*); |
| 7 | |
| 8 | static SkScalar make_frame(SkPath* path) { |
bungeman@google.com | 3c14d0f | 2011-05-20 14:05:03 +0000 | [diff] [blame] | 9 | SkRect r = { SkIntToScalar(10), SkIntToScalar(10), |
| 10 | SkIntToScalar(630), SkIntToScalar(470) }; |
| 11 | path->addRoundRect(r, SkIntToScalar(15), SkIntToScalar(15)); |
reed@google.com | c280700 | 2011-04-11 13:57:04 +0000 | [diff] [blame] | 12 | |
| 13 | SkPaint paint; |
| 14 | paint.setStyle(SkPaint::kStroke_Style); |
bungeman@google.com | 3c14d0f | 2011-05-20 14:05:03 +0000 | [diff] [blame] | 15 | paint.setStrokeWidth(SkIntToScalar(5)); |
reed@google.com | c280700 | 2011-04-11 13:57:04 +0000 | [diff] [blame] | 16 | paint.getFillPath(*path, path); |
bungeman@google.com | 3c14d0f | 2011-05-20 14:05:03 +0000 | [diff] [blame] | 17 | return SkIntToScalar(15); |
reed@google.com | c280700 | 2011-04-11 13:57:04 +0000 | [diff] [blame] | 18 | } |
| 19 | |
| 20 | static SkScalar make_triangle(SkPath* path) { |
| 21 | static const int gCoord[] = { |
| 22 | 10, 20, 15, 5, 30, 30 |
| 23 | }; |
| 24 | path->moveTo(SkIntToScalar(gCoord[0]), SkIntToScalar(gCoord[1])); |
| 25 | path->lineTo(SkIntToScalar(gCoord[2]), SkIntToScalar(gCoord[3])); |
| 26 | path->lineTo(SkIntToScalar(gCoord[4]), SkIntToScalar(gCoord[5])); |
| 27 | path->close(); |
bungeman@google.com | 3c14d0f | 2011-05-20 14:05:03 +0000 | [diff] [blame] | 28 | path->offset(SkIntToScalar(10), SkIntToScalar(0)); |
reed@google.com | c280700 | 2011-04-11 13:57:04 +0000 | [diff] [blame] | 29 | return SkIntToScalar(30); |
| 30 | } |
| 31 | |
| 32 | static SkScalar make_rect(SkPath* path) { |
bungeman@google.com | 3c14d0f | 2011-05-20 14:05:03 +0000 | [diff] [blame] | 33 | SkRect r = { SkIntToScalar(10), SkIntToScalar(10), |
| 34 | SkIntToScalar(30), SkIntToScalar(30) }; |
reed@google.com | c280700 | 2011-04-11 13:57:04 +0000 | [diff] [blame] | 35 | path->addRect(r); |
bungeman@google.com | 3c14d0f | 2011-05-20 14:05:03 +0000 | [diff] [blame] | 36 | path->offset(SkIntToScalar(10), SkIntToScalar(0)); |
reed@google.com | c280700 | 2011-04-11 13:57:04 +0000 | [diff] [blame] | 37 | return SkIntToScalar(30); |
| 38 | } |
| 39 | |
| 40 | static SkScalar make_oval(SkPath* path) { |
bungeman@google.com | 3c14d0f | 2011-05-20 14:05:03 +0000 | [diff] [blame] | 41 | SkRect r = { SkIntToScalar(10), SkIntToScalar(10), |
| 42 | SkIntToScalar(30), SkIntToScalar(30) }; |
reed@google.com | c280700 | 2011-04-11 13:57:04 +0000 | [diff] [blame] | 43 | path->addOval(r); |
bungeman@google.com | 3c14d0f | 2011-05-20 14:05:03 +0000 | [diff] [blame] | 44 | path->offset(SkIntToScalar(10), SkIntToScalar(0)); |
reed@google.com | c280700 | 2011-04-11 13:57:04 +0000 | [diff] [blame] | 45 | return SkIntToScalar(30); |
| 46 | } |
| 47 | |
| 48 | static SkScalar make_sawtooth(SkPath* path) { |
| 49 | SkScalar x = SkIntToScalar(20); |
| 50 | SkScalar y = SkIntToScalar(20); |
| 51 | const SkScalar x0 = x; |
| 52 | const SkScalar dx = SK_Scalar1 * 5; |
| 53 | const SkScalar dy = SK_Scalar1 * 10; |
| 54 | |
| 55 | path->moveTo(x, y); |
| 56 | for (int i = 0; i < 32; i++) { |
| 57 | x += dx; |
| 58 | path->lineTo(x, y - dy); |
| 59 | x += dx; |
| 60 | path->lineTo(x, y + dy); |
| 61 | } |
bungeman@google.com | 3c14d0f | 2011-05-20 14:05:03 +0000 | [diff] [blame] | 62 | path->lineTo(x, y + (2 * dy)); |
| 63 | path->lineTo(x0, y + (2 * dy)); |
reed@google.com | c280700 | 2011-04-11 13:57:04 +0000 | [diff] [blame] | 64 | path->close(); |
| 65 | return SkIntToScalar(30); |
| 66 | } |
| 67 | |
| 68 | static SkScalar make_star(SkPath* path, int n) { |
| 69 | const SkScalar c = SkIntToScalar(45); |
| 70 | const SkScalar r = SkIntToScalar(20); |
| 71 | |
| 72 | SkScalar rad = -SK_ScalarPI / 2; |
| 73 | const SkScalar drad = (n >> 1) * SK_ScalarPI * 2 / n; |
| 74 | |
| 75 | path->moveTo(c, c - r); |
| 76 | for (int i = 1; i < n; i++) { |
| 77 | rad += drad; |
| 78 | SkScalar cosV, sinV = SkScalarSinCos(rad, &cosV); |
| 79 | path->lineTo(c + SkScalarMul(cosV, r), c + SkScalarMul(sinV, r)); |
| 80 | } |
| 81 | path->close(); |
| 82 | return r * 2 * 6 / 5; |
| 83 | } |
| 84 | |
| 85 | static SkScalar make_star_5(SkPath* path) { return make_star(path, 5); } |
| 86 | static SkScalar make_star_13(SkPath* path) { return make_star(path, 13); } |
| 87 | |
| 88 | static const MakePathProc gProcs[] = { |
| 89 | make_frame, |
| 90 | make_triangle, |
| 91 | make_rect, |
| 92 | make_oval, |
| 93 | make_sawtooth, |
| 94 | make_star_5, |
| 95 | make_star_13 |
| 96 | }; |
| 97 | |
| 98 | #define N SK_ARRAY_COUNT(gProcs) |
| 99 | |
| 100 | namespace skiagm { |
| 101 | |
| 102 | class PathFillGM : public GM { |
| 103 | SkPath fPath[N]; |
| 104 | SkScalar fDY[N]; |
| 105 | public: |
bungeman@google.com | 3c14d0f | 2011-05-20 14:05:03 +0000 | [diff] [blame] | 106 | PathFillGM() { |
reed@google.com | c280700 | 2011-04-11 13:57:04 +0000 | [diff] [blame] | 107 | for (size_t i = 0; i < N; i++) { |
| 108 | fDY[i] = gProcs[i](&fPath[i]); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | protected: |
| 113 | virtual SkString onShortName() { |
| 114 | return SkString("pathfill"); |
| 115 | } |
| 116 | |
bungeman@google.com | 3c14d0f | 2011-05-20 14:05:03 +0000 | [diff] [blame] | 117 | virtual SkISize onISize() { |
reed@google.com | c280700 | 2011-04-11 13:57:04 +0000 | [diff] [blame] | 118 | return make_isize(640, 480); |
| 119 | } |
| 120 | |
| 121 | void drawBG(SkCanvas* canvas) { |
| 122 | canvas->drawColor(SK_ColorWHITE); |
| 123 | } |
| 124 | |
| 125 | virtual void onDraw(SkCanvas* canvas) { |
| 126 | this->drawBG(canvas); |
| 127 | |
| 128 | SkPaint paint; |
| 129 | paint.setAntiAlias(true); |
| 130 | |
| 131 | for (size_t i = 0; i < N; i++) { |
| 132 | canvas->drawPath(fPath[i], paint); |
bungeman@google.com | 3c14d0f | 2011-05-20 14:05:03 +0000 | [diff] [blame] | 133 | canvas->translate(SkIntToScalar(0), fDY[i]); |
reed@google.com | c280700 | 2011-04-11 13:57:04 +0000 | [diff] [blame] | 134 | } |
| 135 | } |
| 136 | |
| 137 | private: |
| 138 | typedef GM INHERITED; |
| 139 | }; |
| 140 | |
| 141 | /////////////////////////////////////////////////////////////////////////////// |
| 142 | |
| 143 | static GM* MyFactory(void*) { return new PathFillGM; } |
| 144 | static GMRegistry reg(MyFactory); |
| 145 | |
| 146 | } |