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 "SkCanvas.h"
10#include "SkPaint.h"
11#include "SkRandom.h"
12
13namespace skiagm {
14
15class EmptyPathGM : public GM {
16public:
17    EmptyPathGM() {}
18
19protected:
20    SkString onShortName() {
21        return SkString("emptypath");
22    }
23
24    SkISize onISize() { return SkISize::Make(600, 280); }
25
26    void drawEmpty(SkCanvas* canvas,
27                    SkColor color,
28                    const SkRect& clip,
29                    SkPaint::Style style,
30                    SkPath::FillType fill) {
31        SkPath path;
32        path.setFillType(fill);
33        SkPaint paint;
34        paint.setColor(color);
35        paint.setStyle(style);
36        canvas->save();
37        canvas->clipRect(clip);
38        canvas->drawPath(path, paint);
39        canvas->restore();
40    }
41
42    virtual void onDraw(SkCanvas* canvas) {
43        struct FillAndName {
44            SkPath::FillType fFill;
45            const char*      fName;
46        };
47        static const FillAndName gFills[] = {
48            {SkPath::kWinding_FillType, "Winding"},
49            {SkPath::kEvenOdd_FillType, "Even / Odd"},
50            {SkPath::kInverseWinding_FillType, "Inverse Winding"},
51            {SkPath::kInverseEvenOdd_FillType, "Inverse Even / Odd"},
52        };
53        struct StyleAndName {
54            SkPaint::Style fStyle;
55            const char*    fName;
56        };
57        static const StyleAndName gStyles[] = {
58            {SkPaint::kFill_Style, "Fill"},
59            {SkPaint::kStroke_Style, "Stroke"},
60            {SkPaint::kStrokeAndFill_Style, "Stroke And Fill"},
61        };
62
63        SkPaint titlePaint;
64        titlePaint.setColor(SK_ColorBLACK);
65        titlePaint.setAntiAlias(true);
66        titlePaint.setLCDRenderText(true);
67        titlePaint.setTextSize(15 * SK_Scalar1);
68        const char title[] = "Empty Paths Drawn Into Rectangle Clips With "
69                             "Indicated Style and Fill";
70        canvas->drawText(title, strlen(title),
71                            20 * SK_Scalar1,
72                            20 * SK_Scalar1,
73                            titlePaint);
74
75        SkLCGRandom rand;
76        SkRect rect = SkRect::MakeWH(100*SK_Scalar1, 30*SK_Scalar1);
77        int i = 0;
78        canvas->save();
79        canvas->translate(10 * SK_Scalar1, 0);
80        canvas->save();
81        for (size_t style = 0; style < SK_ARRAY_COUNT(gStyles); ++style) {
82            for (size_t fill = 0; fill < SK_ARRAY_COUNT(gFills); ++fill) {
83                if (0 == i % 4) {
84                    canvas->restore();
85                    canvas->translate(0, rect.height() + 40 * SK_Scalar1);
86                    canvas->save();
87                } else {
88                    canvas->translate(rect.width() + 40 * SK_Scalar1, 0);
89                }
90                ++i;
91
92
93                SkColor color = rand.nextU();
94                color = 0xff000000| color; // force solid
95                this->drawEmpty(canvas, color, rect,
96                                gStyles[style].fStyle, gFills[fill].fFill);
97
98                SkPaint rectPaint;
99                rectPaint.setColor(SK_ColorBLACK);
100                rectPaint.setStyle(SkPaint::kStroke_Style);
101                rectPaint.setStrokeWidth(-1);
102                rectPaint.setAntiAlias(true);
103                canvas->drawRect(rect, rectPaint);
104
105                SkPaint labelPaint;
106                labelPaint.setColor(color);
107                labelPaint.setAntiAlias(true);
108                labelPaint.setLCDRenderText(true);
109                labelPaint.setTextSize(12 * SK_Scalar1);
110                canvas->drawText(gStyles[style].fName,
111                                 strlen(gStyles[style].fName),
112                                 0, rect.height() + 15 * SK_Scalar1,
113                                 labelPaint);
114                canvas->drawText(gFills[fill].fName,
115                                 strlen(gFills[fill].fName),
116                                 0, rect.height() + 28 * SK_Scalar1,
117                                 labelPaint);
118            }
119        }
120        canvas->restore();
121        canvas->restore();
122    }
123
124private:
125    typedef GM INHERITED;
126};
127
128//////////////////////////////////////////////////////////////////////////////
129
130static GM* MyFactory(void*) { return new EmptyPathGM; }
131static GMRegistry reg(MyFactory);
132
133}
134