• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2011 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/gm.h"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkColor.h"
11 #include "include/core/SkFont.h"
12 #include "include/core/SkPaint.h"
13 #include "include/core/SkPath.h"
14 #include "include/core/SkPoint.h"
15 #include "include/core/SkRect.h"
16 #include "include/core/SkScalar.h"
17 #include "include/core/SkSize.h"
18 #include "include/core/SkString.h"
19 #include "include/core/SkTypeface.h"
20 #include "include/core/SkTypes.h"
21 #include "include/utils/SkRandom.h"
22 #include "tools/ToolUtils.h"
23 
24 namespace skiagm {
25 
26 class EmptyPathGM : public GM {
onShortName()27     SkString onShortName() override { return SkString("emptypath"); }
28 
onISize()29     SkISize onISize() override { return {600, 280}; }
30 
drawEmpty(SkCanvas * canvas,SkColor color,const SkRect & clip,SkPaint::Style style,SkPath::FillType fill)31     void drawEmpty(SkCanvas* canvas,
32                     SkColor color,
33                     const SkRect& clip,
34                     SkPaint::Style style,
35                     SkPath::FillType fill) {
36         SkPath path;
37         path.setFillType(fill);
38         SkPaint paint;
39         paint.setColor(color);
40         paint.setStyle(style);
41         canvas->save();
42         canvas->clipRect(clip);
43         canvas->drawPath(path, paint);
44         canvas->restore();
45     }
46 
onDraw(SkCanvas * canvas)47     void onDraw(SkCanvas* canvas) override {
48         struct FillAndName {
49             SkPath::FillType fFill;
50             const char*      fName;
51         };
52         constexpr 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         constexpr StyleAndName gStyles[] = {
63             {SkPaint::kFill_Style, "Fill"},
64             {SkPaint::kStroke_Style, "Stroke"},
65             {SkPaint::kStrokeAndFill_Style, "Stroke And Fill"},
66         };
67 
68         SkFont     font(ToolUtils::create_portable_typeface(), 15);
69         const char title[] = "Empty Paths Drawn Into Rectangle Clips With "
70                              "Indicated Style and Fill";
71         canvas->drawString(title, 20.0f, 20.0f, font, SkPaint());
72 
73         SkRandom rand;
74         SkRect rect = SkRect::MakeWH(100*SK_Scalar1, 30*SK_Scalar1);
75         int i = 0;
76         canvas->save();
77         canvas->translate(10 * SK_Scalar1, 0);
78         canvas->save();
79         for (size_t style = 0; style < SK_ARRAY_COUNT(gStyles); ++style) {
80             for (size_t fill = 0; fill < SK_ARRAY_COUNT(gFills); ++fill) {
81                 if (0 == i % 4) {
82                     canvas->restore();
83                     canvas->translate(0, rect.height() + 40 * SK_Scalar1);
84                     canvas->save();
85                 } else {
86                     canvas->translate(rect.width() + 40 * SK_Scalar1, 0);
87                 }
88                 ++i;
89 
90 
91                 SkColor color = rand.nextU();
92                 color = 0xff000000 | color; // force solid
93                 color         = ToolUtils::color_to_565(color);
94                 this->drawEmpty(canvas, color, rect,
95                                 gStyles[style].fStyle, gFills[fill].fFill);
96 
97                 SkPaint rectPaint;
98                 rectPaint.setColor(SK_ColorBLACK);
99                 rectPaint.setStyle(SkPaint::kStroke_Style);
100                 rectPaint.setStrokeWidth(-1);
101                 rectPaint.setAntiAlias(true);
102                 canvas->drawRect(rect, rectPaint);
103 
104                 SkPaint labelPaint;
105                 labelPaint.setColor(color);
106                 SkFont labelFont(ToolUtils::create_portable_typeface(), 12);
107                 canvas->drawString(gStyles[style].fName, 0, rect.height() + 15.0f,
108                                    labelFont, labelPaint);
109                 canvas->drawString(gFills[fill].fName, 0, rect.height() + 28.0f,
110                                    labelFont, labelPaint);
111             }
112         }
113         canvas->restore();
114         canvas->restore();
115     }
116 };
117 DEF_GM( return new EmptyPathGM; )
118 
119 //////////////////////////////////////////////////////////////////////////////
120 
121 static constexpr int kPtsCount = 3;
122 static constexpr SkPoint kPts[kPtsCount] = {
123     {40, 40},
124     {80, 40},
125     {120, 40},
126 };
127 
make_path_move(SkPath * path)128 static void make_path_move(SkPath* path) {
129     for (SkPoint p : kPts) {
130         path->moveTo(p);
131     }
132 }
133 
make_path_move_close(SkPath * path)134 static void make_path_move_close(SkPath* path) {
135     for (SkPoint p : kPts) {
136         path->moveTo(p);
137         path->close();
138     }
139 }
140 
make_path_move_line(SkPath * path)141 static void make_path_move_line(SkPath* path) {
142     for (SkPoint p : kPts) {
143         path->moveTo(p);
144         path->lineTo(p);
145     }
146 }
147 
make_path_move_mix(SkPath * path)148 static void make_path_move_mix(SkPath* path) {
149     path->moveTo(kPts[0]);
150     path->moveTo(kPts[1]); path->close();
151     path->moveTo(kPts[2]); path->lineTo(kPts[2]);
152 }
153 
154 class EmptyStrokeGM : public GM {
onShortName()155     SkString onShortName() override { return SkString("emptystroke"); }
156 
onISize()157     SkISize onISize() override { return {200, 240}; }
158 
onDraw(SkCanvas * canvas)159     void onDraw(SkCanvas* canvas) override {
160         static constexpr void (*kProcs[])(SkPath*) = {
161             make_path_move,             // expect red red red
162             make_path_move_close,       // expect black black black
163             make_path_move_line,        // expect black black black
164             make_path_move_mix,         // expect red black black,
165         };
166 
167         SkPaint strokePaint;
168         strokePaint.setStyle(SkPaint::kStroke_Style);
169         strokePaint.setStrokeWidth(21);
170         strokePaint.setStrokeCap(SkPaint::kSquare_Cap);
171 
172         SkPaint dotPaint;
173         dotPaint.setColor(SK_ColorRED);
174         strokePaint.setStyle(SkPaint::kStroke_Style);
175         dotPaint.setStrokeWidth(7);
176 
177         for (auto proc : kProcs) {
178             SkPath path;
179             proc(&path);
180             canvas->drawPoints(SkCanvas::kPoints_PointMode, kPtsCount, kPts, dotPaint);
181             canvas->drawPath(path, strokePaint);
182             canvas->translate(0, 40);
183         }
184     }
185 };
186 DEF_GM( return new EmptyStrokeGM; )
187 
188 }
189