Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SkStrokeRec.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2012 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
9
11#include "src/core/SkStroke.h"
12
13#include <algorithm>
14
15// must be < 0, since ==0 means hairline, and >0 means normal stroke
16#define kStrokeRec_FillStyleWidth (-SK_Scalar1)
17
19 fResScale = 1;
21 fMiterLimit = SkPaintDefaults_MiterLimit;
24 fStrokeAndFill = false;
25}
26
28 this->init(paint, paint.getStyle(), resScale);
29}
30
32 this->init(paint, styleOverride, resScale);
33}
34
35void SkStrokeRec::init(const SkPaint& paint, SkPaint::Style style, SkScalar resScale) {
36 fResScale = resScale;
37
38 switch (style) {
41 fStrokeAndFill = false;
42 break;
44 fWidth = paint.getStrokeWidth();
45 fStrokeAndFill = false;
46 break;
48 if (0 == paint.getStrokeWidth()) {
49 // hairline+fill == fill
51 fStrokeAndFill = false;
52 } else {
53 fWidth = paint.getStrokeWidth();
54 fStrokeAndFill = true;
55 }
56 break;
57 default:
58 SkDEBUGFAIL("unknown paint style");
59 // fall back on just fill
61 fStrokeAndFill = false;
62 break;
63 }
64
65 // copy these from the paint, regardless of our "style"
66 fMiterLimit = paint.getStrokeMiter();
67 fCap = paint.getStrokeCap();
68 fJoin = paint.getStrokeJoin();
69}
70
72 if (fWidth < 0) {
73 return kFill_Style;
74 } else if (0 == fWidth) {
75 return kHairline_Style;
76 } else {
77 return fStrokeAndFill ? kStrokeAndFill_Style : kStroke_Style;
78 }
79}
80
83 fStrokeAndFill = false;
84}
85
87 fWidth = 0;
88 fStrokeAndFill = false;
89}
90
91void SkStrokeRec::setStrokeStyle(SkScalar width, bool strokeAndFill) {
92 if (strokeAndFill && (0 == width)) {
93 // hairline+fill == fill
94 this->setFillStyle();
95 } else {
96 fWidth = width;
97 fStrokeAndFill = strokeAndFill;
98 }
99}
100
101#ifdef SK_DEBUG
102 // enables tweaking these values at runtime from Viewer
103 bool gDebugStrokerErrorSet = false;
104 SkScalar gDebugStrokerError;
105#endif
106
107bool SkStrokeRec::applyToPath(SkPath* dst, const SkPath& src) const {
108 if (fWidth <= 0) { // hairline or fill
109 return false;
110 }
111
112 SkStroke stroker;
113 stroker.setCap((SkPaint::Cap)fCap);
114 stroker.setJoin((SkPaint::Join)fJoin);
115 stroker.setMiterLimit(fMiterLimit);
116 stroker.setWidth(fWidth);
117 stroker.setDoFill(fStrokeAndFill);
118#ifdef SK_DEBUG
119 stroker.setResScale(gDebugStrokerErrorSet ? gDebugStrokerError : fResScale);
120#else
121 stroker.setResScale(fResScale);
122#endif
123 stroker.strokePath(src, dst);
124 return true;
125}
126
128 if (fWidth < 0) { // fill
129 paint->setStyle(SkPaint::kFill_Style);
130 return;
131 }
132
133 paint->setStyle(fStrokeAndFill ? SkPaint::kStrokeAndFill_Style : SkPaint::kStroke_Style);
134 paint->setStrokeWidth(fWidth);
135 paint->setStrokeMiter(fMiterLimit);
136 paint->setStrokeCap((SkPaint::Cap)fCap);
137 paint->setStrokeJoin((SkPaint::Join)fJoin);
138}
139
141 return GetInflationRadius((SkPaint::Join)fJoin, fMiterLimit, (SkPaint::Cap)fCap, fWidth);
142}
143
145 SkScalar width = SkPaint::kFill_Style == style ? -SK_Scalar1 : paint.getStrokeWidth();
146 return GetInflationRadius(paint.getStrokeJoin(), paint.getStrokeMiter(), paint.getStrokeCap(),
147 width);
148
149}
150
153 if (strokeWidth < 0) { // fill
154 return 0;
155 } else if (0 == strokeWidth) {
156 // FIXME: We need a "matrixScale" parameter here in order to properly handle hairlines.
157 // Their with is determined in device space, unlike other strokes.
158 // http://skbug.com/8157
159 return SK_Scalar1;
160 }
161
162 // since we're stroked, outset the rect by the radius (and join type, caps)
163 SkScalar multiplier = SK_Scalar1;
164 if (SkPaint::kMiter_Join == join) {
165 multiplier = std::max(multiplier, miterLimit);
166 }
167 if (SkPaint::kSquare_Cap == cap) {
168 multiplier = std::max(multiplier, SK_ScalarSqrt2);
169 }
170 return strokeWidth/2 * multiplier;
171}
172
static const int strokeWidth
Definition BlurTest.cpp:60
#define SkPaintDefaults_MiterLimit
#define SkDEBUGFAIL(message)
Definition SkAssert.h:118
#define SK_Scalar1
Definition SkScalar.h:18
#define SK_ScalarSqrt2
Definition SkScalar.h:20
#define kStrokeRec_FillStyleWidth
@ kDefault_Cap
equivalent to kButt_Cap
Definition SkPaint.h:338
@ kSquare_Cap
adds square
Definition SkPaint.h:336
@ 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
@ kDefault_Join
equivalent to kMiter_Join
Definition SkPaint.h:363
@ kMiter_Join
extends to miter limit
Definition SkPaint.h:359
Style getStyle() const
@ kStrokeAndFill_Style
Definition SkStrokeRec.h:36
void setHairlineStyle()
void setStrokeStyle(SkScalar width, bool strokeAndFill=false)
void setFillStyle()
static SkScalar GetInflationRadius(const SkPaint &, SkPaint::Style)
SkScalar getInflationRadius() const
SkStrokeRec(InitStyle style)
bool applyToPath(SkPath *dst, const SkPath &src) const
void applyToPaint(SkPaint *paint) const
void strokePath(const SkPath &path, SkPath *) const
void setWidth(SkScalar)
void setCap(SkPaint::Cap)
void setDoFill(bool doFill)
Definition SkStroke.h:51
void setResScale(SkScalar rs)
Definition SkStroke.h:62
void setJoin(SkPaint::Join)
void setMiterLimit(SkScalar)
const Paint & paint
float SkScalar
Definition extension.cpp:12
struct MyStruct s
int32_t width