blob: 6da507da34cce1e3dea691815eec1540df4643a5 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
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 */
reed@google.comc2807002011-04-11 13:57:04 +00008#include "gm.h"
9#include "SkPicture.h"
10#include "SkRectShape.h"
11#include "SkGroupShape.h"
12
13typedef SkScalar (*MakePathProc)(SkPath*);
14
15static SkScalar make_frame(SkPath* path) {
bungeman@google.com3c14d0f2011-05-20 14:05:03 +000016 SkRect r = { SkIntToScalar(10), SkIntToScalar(10),
17 SkIntToScalar(630), SkIntToScalar(470) };
18 path->addRoundRect(r, SkIntToScalar(15), SkIntToScalar(15));
reed@google.comc2807002011-04-11 13:57:04 +000019
20 SkPaint paint;
21 paint.setStyle(SkPaint::kStroke_Style);
bungeman@google.com3c14d0f2011-05-20 14:05:03 +000022 paint.setStrokeWidth(SkIntToScalar(5));
reed@google.comc2807002011-04-11 13:57:04 +000023 paint.getFillPath(*path, path);
bungeman@google.com3c14d0f2011-05-20 14:05:03 +000024 return SkIntToScalar(15);
reed@google.comc2807002011-04-11 13:57:04 +000025}
26
27static 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();
bungeman@google.com3c14d0f2011-05-20 14:05:03 +000035 path->offset(SkIntToScalar(10), SkIntToScalar(0));
reed@google.comc2807002011-04-11 13:57:04 +000036 return SkIntToScalar(30);
37}
38
39static SkScalar make_rect(SkPath* path) {
bungeman@google.com3c14d0f2011-05-20 14:05:03 +000040 SkRect r = { SkIntToScalar(10), SkIntToScalar(10),
41 SkIntToScalar(30), SkIntToScalar(30) };
reed@google.comc2807002011-04-11 13:57:04 +000042 path->addRect(r);
bungeman@google.com3c14d0f2011-05-20 14:05:03 +000043 path->offset(SkIntToScalar(10), SkIntToScalar(0));
reed@google.comc2807002011-04-11 13:57:04 +000044 return SkIntToScalar(30);
45}
46
47static SkScalar make_oval(SkPath* path) {
bungeman@google.com3c14d0f2011-05-20 14:05:03 +000048 SkRect r = { SkIntToScalar(10), SkIntToScalar(10),
49 SkIntToScalar(30), SkIntToScalar(30) };
reed@google.comc2807002011-04-11 13:57:04 +000050 path->addOval(r);
bungeman@google.com3c14d0f2011-05-20 14:05:03 +000051 path->offset(SkIntToScalar(10), SkIntToScalar(0));
reed@google.comc2807002011-04-11 13:57:04 +000052 return SkIntToScalar(30);
53}
54
55static 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 }
bungeman@google.com3c14d0f2011-05-20 14:05:03 +000069 path->lineTo(x, y + (2 * dy));
70 path->lineTo(x0, y + (2 * dy));
reed@google.comc2807002011-04-11 13:57:04 +000071 path->close();
72 return SkIntToScalar(30);
73}
74
75static 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
92static SkScalar make_star_5(SkPath* path) { return make_star(path, 5); }
93static SkScalar make_star_13(SkPath* path) { return make_star(path, 13); }
94
vandebo@chromium.org683001c2012-05-09 17:17:51 +000095// We don't expect any output from this path.
96static SkScalar make_line(SkPath* path) {
97 path->moveTo(SkIntToScalar(30), SkIntToScalar(30));
98 path->lineTo(SkIntToScalar(120), SkIntToScalar(40));
99 path->close();
100 path->moveTo(SkIntToScalar(150), SkIntToScalar(30));
101 path->lineTo(SkIntToScalar(150), SkIntToScalar(30));
102 path->lineTo(SkIntToScalar(300), SkIntToScalar(40));
103 path->close();
104 return SkIntToScalar(40);
105}
106
reed@google.comc2807002011-04-11 13:57:04 +0000107static const MakePathProc gProcs[] = {
108 make_frame,
109 make_triangle,
110 make_rect,
111 make_oval,
112 make_sawtooth,
113 make_star_5,
vandebo@chromium.org683001c2012-05-09 17:17:51 +0000114 make_star_13,
115 make_line,
reed@google.comc2807002011-04-11 13:57:04 +0000116};
117
118#define N SK_ARRAY_COUNT(gProcs)
119
120namespace skiagm {
121
122class PathFillGM : public GM {
123 SkPath fPath[N];
124 SkScalar fDY[N];
125public:
bungeman@google.com3c14d0f2011-05-20 14:05:03 +0000126 PathFillGM() {
reed@google.comc2807002011-04-11 13:57:04 +0000127 for (size_t i = 0; i < N; i++) {
128 fDY[i] = gProcs[i](&fPath[i]);
129 }
130 }
131
132protected:
133 virtual SkString onShortName() {
134 return SkString("pathfill");
135 }
136
bungeman@google.com3c14d0f2011-05-20 14:05:03 +0000137 virtual SkISize onISize() {
reed@google.comc2807002011-04-11 13:57:04 +0000138 return make_isize(640, 480);
139 }
140
reed@google.comc2807002011-04-11 13:57:04 +0000141 virtual void onDraw(SkCanvas* canvas) {
reed@google.comc2807002011-04-11 13:57:04 +0000142 SkPaint paint;
143 paint.setAntiAlias(true);
144
145 for (size_t i = 0; i < N; i++) {
146 canvas->drawPath(fPath[i], paint);
bungeman@google.com3c14d0f2011-05-20 14:05:03 +0000147 canvas->translate(SkIntToScalar(0), fDY[i]);
reed@google.comc2807002011-04-11 13:57:04 +0000148 }
149 }
150
151private:
152 typedef GM INHERITED;
153};
154
155///////////////////////////////////////////////////////////////////////////////
156
157static GM* MyFactory(void*) { return new PathFillGM; }
158static GMRegistry reg(MyFactory);
159
160}