1 #include "gm.h"
2 #include "SkPicture.h"
3 #include "SkRectShape.h"
4 #include "SkGroupShape.h"
5
6 typedef SkScalar (*MakePathProc)(SkPath*);
7
make_frame(SkPath * path)8 static SkScalar make_frame(SkPath* path) {
9 SkRect r = { SkIntToScalar(10), SkIntToScalar(10),
10 SkIntToScalar(630), SkIntToScalar(470) };
11 path->addRoundRect(r, SkIntToScalar(15), SkIntToScalar(15));
12
13 SkPaint paint;
14 paint.setStyle(SkPaint::kStroke_Style);
15 paint.setStrokeWidth(SkIntToScalar(5));
16 paint.getFillPath(*path, path);
17 return SkIntToScalar(15);
18 }
19
make_triangle(SkPath * path)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();
28 path->offset(SkIntToScalar(10), SkIntToScalar(0));
29 return SkIntToScalar(30);
30 }
31
make_rect(SkPath * path)32 static SkScalar make_rect(SkPath* path) {
33 SkRect r = { SkIntToScalar(10), SkIntToScalar(10),
34 SkIntToScalar(30), SkIntToScalar(30) };
35 path->addRect(r);
36 path->offset(SkIntToScalar(10), SkIntToScalar(0));
37 return SkIntToScalar(30);
38 }
39
make_oval(SkPath * path)40 static SkScalar make_oval(SkPath* path) {
41 SkRect r = { SkIntToScalar(10), SkIntToScalar(10),
42 SkIntToScalar(30), SkIntToScalar(30) };
43 path->addOval(r);
44 path->offset(SkIntToScalar(10), SkIntToScalar(0));
45 return SkIntToScalar(30);
46 }
47
make_sawtooth(SkPath * path)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 }
62 path->lineTo(x, y + (2 * dy));
63 path->lineTo(x0, y + (2 * dy));
64 path->close();
65 return SkIntToScalar(30);
66 }
67
make_star(SkPath * path,int n)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
make_star_5(SkPath * path)85 static SkScalar make_star_5(SkPath* path) { return make_star(path, 5); }
make_star_13(SkPath * path)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:
PathFillGM()106 PathFillGM() {
107 for (size_t i = 0; i < N; i++) {
108 fDY[i] = gProcs[i](&fPath[i]);
109 }
110 }
111
112 protected:
onShortName()113 virtual SkString onShortName() {
114 return SkString("pathfill");
115 }
116
onISize()117 virtual SkISize onISize() {
118 return make_isize(640, 480);
119 }
120
drawBG(SkCanvas * canvas)121 void drawBG(SkCanvas* canvas) {
122 canvas->drawColor(SK_ColorWHITE);
123 }
124
onDraw(SkCanvas * canvas)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);
133 canvas->translate(SkIntToScalar(0), fDY[i]);
134 }
135 }
136
137 private:
138 typedef GM INHERITED;
139 };
140
141 ///////////////////////////////////////////////////////////////////////////////
142
MyFactory(void *)143 static GM* MyFactory(void*) { return new PathFillGM; }
144 static GMRegistry reg(MyFactory);
145
146 }
147