3 * Copyright 2011 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
10 typedef SkScalar (*MakePathProc)(SkPath*);
12 static SkScalar make_frame(SkPath* path) {
13 SkRect r = { SkIntToScalar(10), SkIntToScalar(10),
14 SkIntToScalar(630), SkIntToScalar(470) };
15 path->addRoundRect(r, SkIntToScalar(15), SkIntToScalar(15));
18 paint.setStyle(SkPaint::kStroke_Style);
19 paint.setStrokeWidth(SkIntToScalar(5));
20 paint.getFillPath(*path, path);
21 return SkIntToScalar(15);
24 static SkScalar make_triangle(SkPath* path) {
25 static const int gCoord[] = {
28 path->moveTo(SkIntToScalar(gCoord[0]), SkIntToScalar(gCoord[1]));
29 path->lineTo(SkIntToScalar(gCoord[2]), SkIntToScalar(gCoord[3]));
30 path->lineTo(SkIntToScalar(gCoord[4]), SkIntToScalar(gCoord[5]));
32 path->offset(SkIntToScalar(10), SkIntToScalar(0));
33 return SkIntToScalar(30);
36 static SkScalar make_rect(SkPath* path) {
37 SkRect r = { SkIntToScalar(10), SkIntToScalar(10),
38 SkIntToScalar(30), SkIntToScalar(30) };
40 path->offset(SkIntToScalar(10), SkIntToScalar(0));
41 return SkIntToScalar(30);
44 static SkScalar make_oval(SkPath* path) {
45 SkRect r = { SkIntToScalar(10), SkIntToScalar(10),
46 SkIntToScalar(30), SkIntToScalar(30) };
48 path->offset(SkIntToScalar(10), SkIntToScalar(0));
49 return SkIntToScalar(30);
52 static SkScalar make_sawtooth(SkPath* path) {
53 SkScalar x = SkIntToScalar(20);
54 SkScalar y = SkIntToScalar(20);
55 const SkScalar x0 = x;
56 const SkScalar dx = SK_Scalar1 * 5;
57 const SkScalar dy = SK_Scalar1 * 10;
60 for (int i = 0; i < 32; i++) {
62 path->lineTo(x, y - dy);
64 path->lineTo(x, y + dy);
66 path->lineTo(x, y + (2 * dy));
67 path->lineTo(x0, y + (2 * dy));
69 return SkIntToScalar(30);
72 static SkScalar make_star(SkPath* path, int n) {
73 const SkScalar c = SkIntToScalar(45);
74 const SkScalar r = SkIntToScalar(20);
76 SkScalar rad = -SK_ScalarPI / 2;
77 const SkScalar drad = (n >> 1) * SK_ScalarPI * 2 / n;
79 path->moveTo(c, c - r);
80 for (int i = 1; i < n; i++) {
82 SkScalar cosV, sinV = SkScalarSinCos(rad, &cosV);
83 path->lineTo(c + SkScalarMul(cosV, r), c + SkScalarMul(sinV, r));
89 static SkScalar make_star_5(SkPath* path) { return make_star(path, 5); }
90 static SkScalar make_star_13(SkPath* path) { return make_star(path, 13); }
92 // We don't expect any output from this path.
93 static SkScalar make_line(SkPath* path) {
94 path->moveTo(SkIntToScalar(30), SkIntToScalar(30));
95 path->lineTo(SkIntToScalar(120), SkIntToScalar(40));
97 path->moveTo(SkIntToScalar(150), SkIntToScalar(30));
98 path->lineTo(SkIntToScalar(150), SkIntToScalar(30));
99 path->lineTo(SkIntToScalar(300), SkIntToScalar(40));
101 return SkIntToScalar(40);
104 static const MakePathProc gProcs[] = {
115 #define N SK_ARRAY_COUNT(gProcs)
117 class PathFillGM : public skiagm::GM {
121 void onOnceBeforeDraw() override {
122 for (size_t i = 0; i < N; i++) {
123 fDY[i] = gProcs[i](&fPath[i]);
128 SkString onShortName() override {
129 return SkString("pathfill");
132 SkISize onISize() override {
133 return SkISize::Make(640, 480);
136 void onDraw(SkCanvas* canvas) override {
138 paint.setAntiAlias(true);
140 for (size_t i = 0; i < N; i++) {
141 canvas->drawPath(fPath[i], paint);
142 canvas->translate(SkIntToScalar(0), fDY[i]);
147 typedef skiagm::GM INHERITED;
150 // test inverse-fill w/ a clip that completely excludes the geometry
151 class PathInverseFillGM : public skiagm::GM {
155 void onOnceBeforeDraw() override {
156 for (size_t i = 0; i < N; i++) {
157 fDY[i] = gProcs[i](&fPath[i]);
161 SkString onShortName() override {
162 return SkString("pathinvfill");
165 SkISize onISize() override {
166 return SkISize::Make(450, 220);
169 static void show(SkCanvas* canvas, const SkPath& path, const SkPaint& paint,
170 const SkRect* clip, SkScalar top, const SkScalar bottom) {
178 canvas->drawPath(path, paint);
182 void onDraw(SkCanvas* canvas) override {
185 path.addCircle(SkIntToScalar(50), SkIntToScalar(50), SkIntToScalar(40));
186 path.toggleInverseFillType();
188 SkRect clipR = { 0, 0, SkIntToScalar(100), SkIntToScalar(200) };
190 canvas->translate(SkIntToScalar(10), SkIntToScalar(10));
192 for (int doclip = 0; doclip <= 1; ++doclip) {
193 for (int aa = 0; aa <= 1; ++aa) {
195 paint.setAntiAlias(SkToBool(aa));
198 canvas->clipRect(clipR);
200 const SkRect* clipPtr = doclip ? &clipR : NULL;
202 show(canvas, path, paint, clipPtr, clipR.fTop, clipR.centerY());
203 show(canvas, path, paint, clipPtr, clipR.centerY(), clipR.fBottom);
206 canvas->translate(SkIntToScalar(110), 0);
212 typedef skiagm::GM INHERITED;
215 ///////////////////////////////////////////////////////////////////////////////
217 static skiagm::GM* MyFactory(void*) { return new PathFillGM; }
218 static skiagm::GMRegistry reg(MyFactory);
220 static skiagm::GM* F1(void*) { return new PathInverseFillGM; }
221 static skiagm::GMRegistry gR1(F1);