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