Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
linepaths.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"
13#include "include/core/SkPath.h"
14#include "include/core/SkRect.h"
18#include "src/base/SkRandom.h"
19#include "tools/ToolUtils.h"
21
22static void drawPath(SkPath& path,SkCanvas* canvas,SkColor color,
23 const SkRect& clip,SkPaint::Cap cap, SkPaint::Join join,
26 path.setFillType(fill);
28 paint.setStrokeCap(cap);
29 paint.setStrokeWidth(strokeWidth);
30 paint.setStrokeJoin(join);
31 paint.setColor(color);
32 paint.setStyle(style);
33 canvas->save();
34 canvas->clipRect(clip);
35 canvas->drawPath(path, paint);
36 canvas->restore();
37}
38
39static void draw(SkCanvas* canvas, bool doClose) {
40 struct FillAndName {
41 SkPathFillType fFill;
42 const char* fName;
43 };
44 constexpr FillAndName gFills[] = {
45 {SkPathFillType::kWinding, "Winding"},
46 {SkPathFillType::kEvenOdd, "Even / Odd"},
47 {SkPathFillType::kInverseWinding, "Inverse Winding"},
48 {SkPathFillType::kInverseEvenOdd, "Inverse Even / Odd"},
49 };
50 struct StyleAndName {
52 const char* fName;
53 };
54 constexpr StyleAndName gStyles[] = {
55 {SkPaint::kFill_Style, "Fill"},
56 {SkPaint::kStroke_Style, "Stroke"},
57 {SkPaint::kStrokeAndFill_Style, "Stroke And Fill"},
58 };
59 struct CapAndName {
60 SkPaint::Cap fCap;
62 const char* fName;
63 };
64 constexpr CapAndName gCaps[] = {
68 };
69 struct PathAndName {
71 const char* fName;
72 };
73 PathAndName path;
74 path.fPath.moveTo(25*SK_Scalar1, 15*SK_Scalar1);
75 path.fPath.lineTo(75*SK_Scalar1, 15*SK_Scalar1);
76 if (doClose) {
77 path.fPath.close();
78 path.fName = "moveTo-line-close";
79 } else {
80 path.fName = "moveTo-line";
81 }
82
83 SkPaint titlePaint;
84 titlePaint.setColor(SK_ColorBLACK);
85 titlePaint.setAntiAlias(true);
86
88
89 const char titleNoClose[] = "Line Drawn Into Rectangle Clips With "
90 "Indicated Style, Fill and Linecaps, with stroke width 10";
91 const char titleClose[] = "Line Closed Drawn Into Rectangle Clips With "
92 "Indicated Style, Fill and Linecaps, with stroke width 10";
93 const char* title = doClose ? titleClose : titleNoClose;
94 canvas->drawString(title, 20.0f, 20.0f, font, titlePaint);
95
96 SkRandom rand;
98 canvas->save();
99 canvas->translate(10 * SK_Scalar1, 30 * SK_Scalar1);
100 canvas->save();
101 for (size_t cap = 0; cap < std::size(gCaps); ++cap) {
102 if (0 < cap) {
103 canvas->translate((rect.width() + 40 * SK_Scalar1) * std::size(gStyles), 0);
104 }
105 canvas->save();
106 for (size_t fill = 0; fill < std::size(gFills); ++fill) {
107 if (0 < fill) {
108 canvas->translate(0, rect.height() + 40 * SK_Scalar1);
109 }
110 canvas->save();
111 for (size_t style = 0; style < std::size(gStyles); ++style) {
112 if (0 < style) {
113 canvas->translate(rect.width() + 40 * SK_Scalar1, 0);
114 }
115
117 drawPath(path.fPath, canvas, color, rect,
118 gCaps[cap].fCap, gCaps[cap].fJoin, gStyles[style].fStyle,
119 gFills[fill].fFill, SK_Scalar1*10);
120
121 SkPaint rectPaint;
122 rectPaint.setColor(SK_ColorBLACK);
124 rectPaint.setStrokeWidth(-1);
125 rectPaint.setAntiAlias(true);
126 canvas->drawRect(rect, rectPaint);
127
128 SkPaint labelPaint;
129 labelPaint.setColor(color);
130 font.setSize(10);
131 canvas->drawString(gStyles[style].fName, 0, rect.height() + 12.0f,
132 font, labelPaint);
133 canvas->drawString(gFills[fill].fName, 0, rect.height() + 24.0f,
134 font, labelPaint);
135 canvas->drawString(gCaps[cap].fName, 0, rect.height() + 36.0f,
136 font, labelPaint);
137 }
138 canvas->restore();
139 }
140 canvas->restore();
141 }
142 canvas->restore();
143 canvas->restore();
144}
145DEF_SIMPLE_GM(linepath, canvas, 1240, 390) {
146 draw(canvas, false);
147}
148DEF_SIMPLE_GM(lineclosepath, canvas, 1240, 390) {
149 draw(canvas, true);
150}
SkPath fPath
SkPaint::Join fJoin
SkStrokeRec::Style fStyle
static const int strokeWidth
Definition BlurTest.cpp:60
const char * fName
SkColor4f color
uint32_t SkColor
Definition SkColor.h:37
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 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
@ kRound_Cap
adds circle
Definition SkPaint.h:335
@ kButt_Cap
no stroke extension
Definition SkPaint.h:334
@ 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
@ 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
@ kRound_Join
adds circle
Definition SkPaint.h:360
@ kBevel_Join
connects outside edges
Definition SkPaint.h:361
void setStrokeWidth(SkScalar width)
Definition SkPaint.cpp:159
const Paint & paint
float SkScalar
Definition extension.cpp:12
#define DEF_SIMPLE_GM(NAME, CANVAS, W, H)
Definition gm.h:50
static void draw(SkCanvas *canvas, bool doClose)
Definition linepaths.cpp:39
static void drawPath(SkPath &path, SkCanvas *canvas, SkColor color, const SkRect &clip, SkPaint::Cap cap, SkPaint::Join join, SkPaint::Style style, SkPathFillType fill, SkScalar strokeWidth)
Definition linepaths.cpp:22
sk_sp< SkTypeface > DefaultPortableTypeface()
SkColor color_to_565(SkColor color)
static constexpr SkRect MakeWH(float w, float h)
Definition SkRect.h:609
static constexpr SkFontStyle gStyles[]
Definition typeface.cpp:89