blob: 87e1cc554e47930ce99b8606b11f6531c3012b8f [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 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000023
tfarinaf5393182014-06-09 23:59:03 -070024 SkISize onISize() { return SkISize::Make(600, 280); }
bsalomon@google.comfa6ac932011-10-05 19:57:55 +000025
bsalomon@google.comfa6ac932011-10-05 19:57:55 +000026 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 }
rmistry@google.comae933ce2012-08-23 18:19:56 +000041
bsalomon@google.comfa6ac932011-10-05 19:57:55 +000042 virtual void onDraw(SkCanvas* canvas) {
bsalomon@google.comfa6ac932011-10-05 19:57:55 +000043 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);
Cary Clark992c7b02014-07-31 08:58:44 -040066 sk_tool_utils::set_portable_typeface(&titlePaint);
bsalomon@google.comfa6ac932011-10-05 19:57:55 +000067 titlePaint.setLCDRenderText(true);
68 titlePaint.setTextSize(15 * SK_Scalar1);
69 const char title[] = "Empty Paths Drawn Into Rectangle Clips With "
70 "Indicated Style and Fill";
71 canvas->drawText(title, strlen(title),
72 20 * SK_Scalar1,
73 20 * SK_Scalar1,
74 titlePaint);
75
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +000076 SkLCGRandom rand;
bsalomon@google.comfa6ac932011-10-05 19:57:55 +000077 SkRect rect = SkRect::MakeWH(100*SK_Scalar1, 30*SK_Scalar1);
78 int i = 0;
79 canvas->save();
80 canvas->translate(10 * SK_Scalar1, 0);
81 canvas->save();
bsalomon@google.comf12449b2011-10-10 14:03:33 +000082 for (size_t style = 0; style < SK_ARRAY_COUNT(gStyles); ++style) {
83 for (size_t fill = 0; fill < SK_ARRAY_COUNT(gFills); ++fill) {
bsalomon@google.comfa6ac932011-10-05 19:57:55 +000084 if (0 == i % 4) {
85 canvas->restore();
86 canvas->translate(0, rect.height() + 40 * SK_Scalar1);
87 canvas->save();
88 } else {
89 canvas->translate(rect.width() + 40 * SK_Scalar1, 0);
90 }
91 ++i;
92
93
94 SkColor color = rand.nextU();
95 color = 0xff000000| color; // force solid
96 this->drawEmpty(canvas, color, rect,
97 gStyles[style].fStyle, gFills[fill].fFill);
98
99 SkPaint rectPaint;
100 rectPaint.setColor(SK_ColorBLACK);
101 rectPaint.setStyle(SkPaint::kStroke_Style);
102 rectPaint.setStrokeWidth(-1);
103 rectPaint.setAntiAlias(true);
104 canvas->drawRect(rect, rectPaint);
105
106 SkPaint labelPaint;
107 labelPaint.setColor(color);
108 labelPaint.setAntiAlias(true);
Cary Clark992c7b02014-07-31 08:58:44 -0400109 sk_tool_utils::set_portable_typeface(&labelPaint);
bsalomon@google.comfa6ac932011-10-05 19:57:55 +0000110 labelPaint.setLCDRenderText(true);
111 labelPaint.setTextSize(12 * SK_Scalar1);
112 canvas->drawText(gStyles[style].fName,
113 strlen(gStyles[style].fName),
114 0, rect.height() + 15 * SK_Scalar1,
115 labelPaint);
116 canvas->drawText(gFills[fill].fName,
117 strlen(gFills[fill].fName),
118 0, rect.height() + 28 * SK_Scalar1,
119 labelPaint);
120 }
121 }
122 canvas->restore();
123 canvas->restore();
124 }
rmistry@google.comae933ce2012-08-23 18:19:56 +0000125
bsalomon@google.comfa6ac932011-10-05 19:57:55 +0000126private:
127 typedef GM INHERITED;
128};
129
130//////////////////////////////////////////////////////////////////////////////
131
132static GM* MyFactory(void*) { return new EmptyPathGM; }
133static GMRegistry reg(MyFactory);
134
135}