blob: 713847f5149dd7c223a5bc356198138dd9d2988a [file] [log] [blame]
reed@google.comc2807002011-04-11 13:57:04 +00001#include "gm.h"
2#include "SkPicture.h"
3#include "SkRectShape.h"
4#include "SkGroupShape.h"
5
6typedef SkScalar (*MakePathProc)(SkPath*);
7
8static SkScalar make_frame(SkPath* path) {
bungeman@google.com3c14d0f2011-05-20 14:05:03 +00009 SkRect r = { SkIntToScalar(10), SkIntToScalar(10),
10 SkIntToScalar(630), SkIntToScalar(470) };
11 path->addRoundRect(r, SkIntToScalar(15), SkIntToScalar(15));
reed@google.comc2807002011-04-11 13:57:04 +000012
13 SkPaint paint;
14 paint.setStyle(SkPaint::kStroke_Style);
bungeman@google.com3c14d0f2011-05-20 14:05:03 +000015 paint.setStrokeWidth(SkIntToScalar(5));
reed@google.comc2807002011-04-11 13:57:04 +000016 paint.getFillPath(*path, path);
bungeman@google.com3c14d0f2011-05-20 14:05:03 +000017 return SkIntToScalar(15);
reed@google.comc2807002011-04-11 13:57:04 +000018}
19
20static 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.com3c14d0f2011-05-20 14:05:03 +000028 path->offset(SkIntToScalar(10), SkIntToScalar(0));
reed@google.comc2807002011-04-11 13:57:04 +000029 return SkIntToScalar(30);
30}
31
32static SkScalar make_rect(SkPath* path) {
bungeman@google.com3c14d0f2011-05-20 14:05:03 +000033 SkRect r = { SkIntToScalar(10), SkIntToScalar(10),
34 SkIntToScalar(30), SkIntToScalar(30) };
reed@google.comc2807002011-04-11 13:57:04 +000035 path->addRect(r);
bungeman@google.com3c14d0f2011-05-20 14:05:03 +000036 path->offset(SkIntToScalar(10), SkIntToScalar(0));
reed@google.comc2807002011-04-11 13:57:04 +000037 return SkIntToScalar(30);
38}
39
40static SkScalar make_oval(SkPath* path) {
bungeman@google.com3c14d0f2011-05-20 14:05:03 +000041 SkRect r = { SkIntToScalar(10), SkIntToScalar(10),
42 SkIntToScalar(30), SkIntToScalar(30) };
reed@google.comc2807002011-04-11 13:57:04 +000043 path->addOval(r);
bungeman@google.com3c14d0f2011-05-20 14:05:03 +000044 path->offset(SkIntToScalar(10), SkIntToScalar(0));
reed@google.comc2807002011-04-11 13:57:04 +000045 return SkIntToScalar(30);
46}
47
48static 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.com3c14d0f2011-05-20 14:05:03 +000062 path->lineTo(x, y + (2 * dy));
63 path->lineTo(x0, y + (2 * dy));
reed@google.comc2807002011-04-11 13:57:04 +000064 path->close();
65 return SkIntToScalar(30);
66}
67
68static 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
85static SkScalar make_star_5(SkPath* path) { return make_star(path, 5); }
86static SkScalar make_star_13(SkPath* path) { return make_star(path, 13); }
87
88static 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
100namespace skiagm {
101
102class PathFillGM : public GM {
103 SkPath fPath[N];
104 SkScalar fDY[N];
105public:
bungeman@google.com3c14d0f2011-05-20 14:05:03 +0000106 PathFillGM() {
reed@google.comc2807002011-04-11 13:57:04 +0000107 for (size_t i = 0; i < N; i++) {
108 fDY[i] = gProcs[i](&fPath[i]);
109 }
110 }
111
112protected:
113 virtual SkString onShortName() {
114 return SkString("pathfill");
115 }
116
bungeman@google.com3c14d0f2011-05-20 14:05:03 +0000117 virtual SkISize onISize() {
reed@google.comc2807002011-04-11 13:57:04 +0000118 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.com3c14d0f2011-05-20 14:05:03 +0000133 canvas->translate(SkIntToScalar(0), fDY[i]);
reed@google.comc2807002011-04-11 13:57:04 +0000134 }
135 }
136
137private:
138 typedef GM INHERITED;
139};
140
141///////////////////////////////////////////////////////////////////////////////
142
143static GM* MyFactory(void*) { return new PathFillGM; }
144static GMRegistry reg(MyFactory);
145
146}