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