1/*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "gm.h"
9#include "SkCanvas.h"
10#include "SkPath.h"
11
12static SkPath generate_square(SkScalar cx, SkScalar cy, SkScalar w) {
13    SkRect rect = SkRect::MakeXYWH(cx - w / 2, cy - w / 2, w, w);
14    SkPath path;
15    path.addRect(rect);
16    return path;
17}
18
19static SkPath generate_rect_line(SkScalar cx, SkScalar cy, SkScalar l) {
20    SkRect rect = SkRect::MakeXYWH(cx - l / 2, cy, l, 0);
21    SkPath path;
22    path.addRect(rect);
23    return path;
24}
25
26static SkPath generate_circle(SkScalar cx, SkScalar cy, SkScalar d) {
27    SkPath path;
28    path.addCircle(cx, cy, d/2, SkPath::kCW_Direction);
29    return path;
30}
31
32static SkPath generate_line(SkScalar cx, SkScalar cy, SkScalar l) {
33    SkPath path;
34    path.moveTo(cx - l / 2, cy);
35    path.lineTo(cx + l / 2, cy);
36    return path;
37}
38
39namespace {
40SkPaint::Style styles[] = {
41        SkPaint::kStroke_Style,
42        SkPaint::kStrokeAndFill_Style,
43        SkPaint::kFill_Style
44};
45SkScalar pathSizes[] = {
46        40,
47        10,
48        0
49};
50SkScalar strokeWidths[] = {
51        10,
52        0
53};
54SkPath ((*paths[])(SkScalar, SkScalar, SkScalar)) = {
55        generate_square,
56        generate_rect_line,
57        generate_circle,
58        generate_line
59};
60
61const SkScalar slideWidth = 90, slideHeight = 90;
62const SkScalar slideBoundary = 5;
63
64}  // namespace
65
66DEF_SIMPLE_GM(inverse_paths, canvas, 800, 900) {
67        SkScalar cx = slideWidth / 2 + slideBoundary;
68        SkScalar cy = slideHeight / 2 + slideBoundary;
69        SkScalar dx = slideWidth + 2 * slideBoundary;
70        SkScalar dy = slideHeight + 2 * slideBoundary;
71
72        SkRect clipRect = SkRect::MakeLTRB(slideBoundary, slideBoundary,
73                                           slideBoundary + slideWidth,
74                                           slideBoundary + slideHeight);
75        SkPaint clipPaint;
76        clipPaint.setStyle(SkPaint::kStroke_Style);
77        clipPaint.setStrokeWidth(SkIntToScalar(2));
78
79        SkPaint outlinePaint;
80        outlinePaint.setColor(0x40000000);
81        outlinePaint.setStyle(SkPaint::kStroke_Style);
82        outlinePaint.setStrokeWidth(SkIntToScalar(0));
83
84        for (size_t styleIndex = 0; styleIndex < SK_ARRAY_COUNT(styles);
85                styleIndex++) {
86            for (size_t sizeIndex = 0; sizeIndex < SK_ARRAY_COUNT(pathSizes);
87                    sizeIndex++) {
88                SkScalar size = pathSizes[sizeIndex];
89
90                canvas->save();
91
92                for (size_t widthIndex = 0;
93                        widthIndex < SK_ARRAY_COUNT(strokeWidths);
94                        widthIndex++) {
95                    SkPaint paint;
96                    paint.setColor(0xff007000);
97                    paint.setStrokeWidth(strokeWidths[widthIndex]);
98                    paint.setStyle(styles[styleIndex]);
99
100                    for (size_t pathIndex = 0;
101                            pathIndex < SK_ARRAY_COUNT(paths);
102                            pathIndex++) {
103                        canvas->drawRect(clipRect, clipPaint);
104
105                        canvas->save();
106                        canvas->clipRect(clipRect);
107
108                        SkPath path = paths[pathIndex](cx, cy, size);
109                        path.setFillType(SkPath::kInverseWinding_FillType);
110                        canvas->drawPath(path, paint);
111
112                        path.setFillType(SkPath::kWinding_FillType);
113                        canvas->drawPath(path, outlinePaint);
114
115                        canvas->restore();
116                        canvas->translate(dx, 0);
117                    }
118                }
119
120                canvas->restore();
121                canvas->translate(0, dy);
122            }
123        }
124}
125