Debian Codegraph @

src/effects/SkCornerPathEffect.cpp

raw
1 /*
2 * Copyright 2006 The Android Open Source Project
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 "include/effects/SkCornerPathEffect.h"
9
10 #include "include/core/SkFlattenable.h"
11 #include "include/core/SkPath.h"
12 #include "include/core/SkPathBuilder.h"
13 #include "include/core/SkPathEffect.h"
14 #include "include/core/SkPoint.h"
15 #include "include/core/SkRefCnt.h"
16 #include "include/core/SkScalar.h"
17 #include "include/core/SkTypes.h"
18 #include "include/private/base/SkFloatingPoint.h"
19 #include "src/core/SkPathEffectBase.h"
20 #include "src/core/SkReadBuffer.h"
21 #include "src/core/SkWriteBuffer.h"
22
23 class SkMatrix;
24 class SkStrokeRec;
25 struct SkRect;
26
27 static bool ComputeStep(const SkPoint& a, const SkPoint& b, SkScalar radius,
28 SkPoint* step) {
29 SkScalar dist = SkPoint::Distance(a, b);
30
31 *step = b - a;
32 if (dist <= radius * 2) {
33 *step *= SK_ScalarHalf;
34 return false;
35 } else {
36 *step *= radius / dist;
37 return true;
38 }
39 }
40
41 class SkCornerPathEffectImpl : public SkPathEffectBase {
42 public:
43 explicit SkCornerPathEffectImpl(SkScalar radius) : fRadius(radius) {
44 SkASSERT(radius > 0);
45 }
46
47 bool onFilterPath(SkPathBuilder* dst, const SkPath& src, SkStrokeRec*, const SkRect*,
48 const SkMatrix&) const override {
49 if (fRadius <= 0) {
50 return false;
51 }
52
53 // just need a value that won't match when we compare it initially
54 SkPathVerb prevVerb = static_cast<SkPathVerb>(0xFF);
55
56 bool closed;
57 SkPoint moveTo, lastCorner;
58 SkVector firstStep, step;
59 bool prevIsValid = true;
60
61 // to avoid warnings
62 step.set(0, 0);
63 moveTo.set(0, 0);
64 firstStep.set(0, 0);
65 lastCorner.set(0, 0);
66
67 SkPath::Iter iter(src, false);
68 while (auto rec = iter.next()) {
69 SkSpan<const SkPoint> pts = rec->fPoints;
70 switch (rec->fVerb) {
71 case SkPathVerb::kMove:
72 // close out the previous (open) contour
73 if (SkPathVerb::kLine == prevVerb) {
74 dst->lineTo(lastCorner);
75 }
76 closed = iter.isClosedContour();
77 if (closed) {
78 moveTo = pts[0];
79 prevIsValid = false;
80 } else {
81 dst->moveTo(pts[0]);
82 prevIsValid = true;
83 }
84 break;
85 case SkPathVerb::kLine: {
86 bool drawSegment = ComputeStep(pts[0], pts[1], fRadius, &step);
87 // prev corner
88 if (!prevIsValid) {
89 dst->moveTo(moveTo + step);
90 prevIsValid = true;
91 } else {
92 dst->quadTo(pts[0].fX, pts[0].fY, pts[0].fX + step.fX,
93 pts[0].fY + step.fY);
94 }
95 if (drawSegment) {
96 dst->lineTo(pts[1].fX - step.fX, pts[1].fY - step.fY);
97 }
98 lastCorner = pts[1];
99 prevIsValid = true;
100 break;
101 }
102 case SkPathVerb::kQuad:
103 // TBD - just replicate the curve for now
104 if (!prevIsValid) {
105 dst->moveTo(pts[0]);
106 prevIsValid = true;
107 }
108 dst->quadTo(pts[1], pts[2]);
109 lastCorner = pts[2];
110 firstStep.set(0, 0);
111 break;
112 case SkPathVerb::kConic:
113 // TBD - just replicate the curve for now
114 if (!prevIsValid) {
115 dst->moveTo(pts[0]);
116 prevIsValid = true;
117 }
118 dst->conicTo(pts[1], pts[2], rec->conicWeight());
119 lastCorner = pts[2];
120 firstStep.set(0, 0);
121 break;
122 case SkPathVerb::kCubic:
123 if (!prevIsValid) {
124 dst->moveTo(pts[0]);
125 prevIsValid = true;
126 }
127 // TBD - just replicate the curve for now
128 dst->cubicTo(pts[1], pts[2], pts[3]);
129 lastCorner = pts[3];
130 firstStep.set(0, 0);
131 break;
132 case SkPathVerb::kClose:
133 if (firstStep.fX || firstStep.fY) {
134 dst->quadTo(lastCorner.fX, lastCorner.fY,
135 lastCorner.fX + firstStep.fX,
136 lastCorner.fY + firstStep.fY);
137 }
138 dst->close();
139 prevIsValid = false;
140 break;
141 }
142 if (SkPathVerb::kMove == prevVerb) {
143 firstStep = step;
144 }
145 prevVerb = rec->fVerb;
146 }
147 if (prevIsValid) {
148 dst->lineTo(lastCorner);
149 }
150 return true;
151 }
152
153 bool computeFastBounds(SkRect*) const override {
154 // Rounding sharp corners within a path produces a new path that is still contained within
155 // the original's bounds, so leave 'bounds' unmodified.
156 return true;
157 }
158
159 static sk_sp<SkFlattenable> CreateProc(SkReadBuffer& buffer) {
160 return SkCornerPathEffect::Make(buffer.readScalar());
161 }
162
163 void flatten(SkWriteBuffer& buffer) const override {
164 buffer.writeScalar(fRadius);
165 }
166
167 Factory getFactory() const override { return CreateProc; }
168 const char* getTypeName() const override { return "SkCornerPathEffect"; }
169
170 private:
171 const SkScalar fRadius;
172
173 using INHERITED = SkPathEffectBase;
174 };
175
176 //////////////////////////////////////////////////////////////////////////////////////////////////
177
178 sk_sp<SkPathEffect> SkCornerPathEffect::Make(SkScalar radius) {
179 return SkIsFinite(radius) && (radius > 0) ?
180 sk_sp<SkPathEffect>(new SkCornerPathEffectImpl(radius)) : nullptr;
181 }
182
183 void SkCornerPathEffect::RegisterFlattenables() {
184 SkFlattenable::Register("SkCornerPathEffect", SkCornerPathEffectImpl::CreateProc);
185 }