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