Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
emptypath.cpp
Go to the documentation of this file.
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"
11#include "include/core/SkFont.h"
15#include "include/core/SkRect.h"
17#include "include/core/SkSize.h"
21#include "src/base/SkRandom.h"
22#include "tools/ToolUtils.h"
24
25namespace skiagm {
26
27class EmptyPathGM : public GM {
28 SkString getName() const override { return SkString("emptypath"); }
29
30 SkISize getISize() override { return {600, 280}; }
31
32 void drawEmpty(SkCanvas* canvas,
34 const SkRect& clip,
35 SkPaint::Style style,
36 SkPathFillType fill) {
37 SkPath path;
38 path.setFillType(fill);
40 paint.setColor(color);
41 paint.setStyle(style);
42 canvas->save();
43 canvas->clipRect(clip);
44 canvas->drawPath(path, paint);
45 canvas->restore();
46 }
47
48 void onDraw(SkCanvas* canvas) override {
49 struct FillAndName {
50 SkPathFillType fFill;
51 const char* fName;
52 };
53 constexpr FillAndName gFills[] = {
54 {SkPathFillType::kWinding, "Winding"},
55 {SkPathFillType::kEvenOdd, "Even / Odd"},
56 {SkPathFillType::kInverseWinding, "Inverse Winding"},
57 {SkPathFillType::kInverseEvenOdd, "Inverse Even / Odd"},
58 };
59 struct StyleAndName {
61 const char* fName;
62 };
63 constexpr StyleAndName gStyles[] = {
64 {SkPaint::kFill_Style, "Fill"},
65 {SkPaint::kStroke_Style, "Stroke"},
66 {SkPaint::kStrokeAndFill_Style, "Stroke And Fill"},
67 };
68
70 const char title[] = "Empty Paths Drawn Into Rectangle Clips With "
71 "Indicated Style and Fill";
72 canvas->drawString(title, 20.0f, 20.0f, font, SkPaint());
73
74 SkRandom rand;
76 int i = 0;
77 canvas->save();
78 canvas->translate(10 * SK_Scalar1, 0);
79 canvas->save();
80 for (size_t style = 0; style < std::size(gStyles); ++style) {
81 for (size_t fill = 0; fill < std::size(gFills); ++fill) {
82 if (0 == i % 4) {
83 canvas->restore();
84 canvas->translate(0, rect.height() + 40 * SK_Scalar1);
85 canvas->save();
86 } else {
87 canvas->translate(rect.width() + 40 * SK_Scalar1, 0);
88 }
89 ++i;
90
91
92 SkColor color = rand.nextU();
93 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);
101 rectPaint.setStrokeWidth(-1);
102 rectPaint.setAntiAlias(true);
103 canvas->drawRect(rect, rectPaint);
104
105 SkPaint labelPaint;
106 labelPaint.setColor(color);
108 canvas->drawString(gStyles[style].fName, 0, rect.height() + 15.0f,
109 labelFont, labelPaint);
110 canvas->drawString(gFills[fill].fName, 0, rect.height() + 28.0f,
111 labelFont, labelPaint);
112 }
113 }
114 canvas->restore();
115 canvas->restore();
116 }
117};
118DEF_GM( return new EmptyPathGM; )
119
120//////////////////////////////////////////////////////////////////////////////
121
122static constexpr int kPtsCount = 3;
123static constexpr SkPoint kPts[kPtsCount] = {
124 {40, 40},
125 {80, 40},
126 {120, 40},
127};
128
130 SkPathBuilder builder;
131 for (SkPoint p : kPts) {
132 builder.moveTo(p);
133 }
134 return builder.detach();
135}
136
138 SkPathBuilder builder;
139 for (SkPoint p : kPts) {
140 builder.moveTo(p).close();
141 }
142 return builder.detach();
143}
144
146 SkPathBuilder builder;
147 for (SkPoint p : kPts) {
148 builder.moveTo(p).lineTo(p);
149 }
150 return builder.detach();
151}
152
154 return SkPathBuilder().moveTo(kPts[0])
155 .moveTo(kPts[1]).close()
156 .moveTo(kPts[2]).lineTo(kPts[2])
157 .detach();
158}
159
160class EmptyStrokeGM : public GM {
161 SkString getName() const override { return SkString("emptystroke"); }
162
163 SkISize getISize() override { return {200, 240}; }
164
165 void onDraw(SkCanvas* canvas) override {
166 static constexpr SkPath (*kProcs[])() = {
167 make_path_move, // expect red red red
168 make_path_move_close, // expect black black black
169 make_path_move_line, // expect black black black
170 make_path_move_mix, // expect red black black,
171 };
172
173 SkPaint strokePaint;
174 strokePaint.setStyle(SkPaint::kStroke_Style);
175 strokePaint.setStrokeWidth(21);
177
178 SkPaint dotPaint;
179 dotPaint.setColor(SK_ColorRED);
180 strokePaint.setStyle(SkPaint::kStroke_Style);
181 dotPaint.setStrokeWidth(7);
182
183 for (auto proc : kProcs) {
185 canvas->drawPath(proc(), strokePaint);
186 canvas->translate(0, 40);
187 }
188 }
189};
190DEF_GM( return new EmptyStrokeGM; )
191
192} // namespace skiagm
SkStrokeRec::Style fStyle
const char * fName
SkColor4f color
uint32_t SkColor
Definition SkColor.h:37
constexpr SkColor SK_ColorRED
Definition SkColor.h:126
constexpr SkColor SK_ColorBLACK
Definition SkColor.h:103
SkPathFillType
Definition SkPathTypes.h:11
static SkPath clip(const SkPath &path, const SkHalfPlane &plane)
Definition SkPath.cpp:3824
#define SK_Scalar1
Definition SkScalar.h:18
void drawRect(const SkRect &rect, const SkPaint &paint)
void clipRect(const SkRect &rect, SkClipOp op, bool doAntiAlias)
void drawPoints(PointMode mode, size_t count, const SkPoint pts[], const SkPaint &paint)
void restore()
Definition SkCanvas.cpp:465
void translate(SkScalar dx, SkScalar dy)
int save()
Definition SkCanvas.cpp:451
void drawPath(const SkPath &path, const SkPaint &paint)
void drawString(const char str[], SkScalar x, SkScalar y, const SkFont &font, const SkPaint &paint)
Definition SkCanvas.h:1803
@ kPoints_PointMode
draw each point separately
Definition SkCanvas.h:1241
@ kSquare_Cap
adds square
Definition SkPaint.h:336
void setStyle(Style style)
Definition SkPaint.cpp:105
void setColor(SkColor color)
Definition SkPaint.cpp:119
void setAntiAlias(bool aa)
Definition SkPaint.h:170
void setStrokeCap(Cap cap)
Definition SkPaint.cpp:179
@ kStroke_Style
set to stroke geometry
Definition SkPaint.h:194
@ kFill_Style
set to fill geometry
Definition SkPaint.h:193
@ kStrokeAndFill_Style
sets to stroke and fill geometry
Definition SkPaint.h:195
void setStrokeWidth(SkScalar width)
Definition SkPaint.cpp:159
SkPathBuilder & close()
SkPathBuilder & lineTo(SkPoint pt)
SkPathBuilder & moveTo(SkPoint pt)
uint32_t nextU()
Definition SkRandom.h:42
SkISize getISize() override
Definition emptypath.cpp:30
SkString getName() const override
Definition emptypath.cpp:28
void onDraw(SkCanvas *canvas) override
Definition emptypath.cpp:48
void onDraw(SkCanvas *canvas) override
SkISize getISize() override
SkString getName() const override
const Paint & paint
#define DEF_GM(CODE)
Definition gm.h:40
sk_sp< SkTypeface > DefaultPortableTypeface()
SkColor color_to_565(SkColor color)
static SkPath make_path_move()
static constexpr SkPoint kPts[kPtsCount]
const char * fName
static SkPath make_path_move_close()
static SkPath make_path_move_mix()
static SkPath make_path_move_line()
static constexpr int kPtsCount
static constexpr SkRect MakeWH(float w, float h)
Definition SkRect.h:609
static constexpr SkFontStyle gStyles[]
Definition typeface.cpp:89