1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "PathSkia.h"
#include "HelpersSkia.h"
#include "PathHelpers.h"
#include "skia/src/core/SkDraw.h"
#include "skia/src/core/SkGeometry.h"
namespace mozilla::gfx {
PathBuilderSkia::PathBuilderSkia(const Matrix& aTransform, const SkPath& aPath,
FillRule aFillRule)
: mPath(aPath) {
SkMatrix matrix;
GfxMatrixToSkiaMatrix(aTransform, matrix);
mPath.transform(matrix);
SetFillRule(aFillRule);
}
PathBuilderSkia::PathBuilderSkia(FillRule aFillRule) { SetFillRule(aFillRule); }
void PathBuilderSkia::SetFillRule(FillRule aFillRule) {
mFillRule = aFillRule;
if (mFillRule == FillRule::FILL_WINDING) {
mPath.setFillType(SkPath::kWinding_FillType);
} else {
mPath.setFillType(SkPath::kEvenOdd_FillType);
}
}
void PathBuilderSkia::MoveTo(const Point& aPoint) {
mPath.moveTo(SkFloatToScalar(aPoint.x), SkFloatToScalar(aPoint.y));
mCurrentPoint = aPoint;
mBeginPoint = aPoint;
}
void PathBuilderSkia::LineTo(const Point& aPoint) {
if (!mPath.countPoints()) {
MoveTo(aPoint);
} else {
mPath.lineTo(SkFloatToScalar(aPoint.x), SkFloatToScalar(aPoint.y));
}
mCurrentPoint = aPoint;
}
void PathBuilderSkia::BezierTo(const Point& aCP1, const Point& aCP2,
const Point& aCP3) {
if (!mPath.countPoints()) {
MoveTo(aCP1);
}
mPath.cubicTo(SkFloatToScalar(aCP1.x), SkFloatToScalar(aCP1.y),
SkFloatToScalar(aCP2.x), SkFloatToScalar(aCP2.y),
SkFloatToScalar(aCP3.x), SkFloatToScalar(aCP3.y));
mCurrentPoint = aCP3;
}
void PathBuilderSkia::QuadraticBezierTo(const Point& aCP1, const Point& aCP2) {
if (!mPath.countPoints()) {
MoveTo(aCP1);
}
mPath.quadTo(SkFloatToScalar(aCP1.x), SkFloatToScalar(aCP1.y),
SkFloatToScalar(aCP2.x), SkFloatToScalar(aCP2.y));
mCurrentPoint = aCP2;
}
void PathBuilderSkia::Close() {
mPath.close();
mCurrentPoint = mBeginPoint;
}
void PathBuilderSkia::Arc(const Point& aOrigin, float aRadius,
float aStartAngle, float aEndAngle,
bool aAntiClockwise) {
ArcToBezier(this, aOrigin, Size(aRadius, aRadius), aStartAngle, aEndAngle,
aAntiClockwise);
}
already_AddRefed<Path> PathBuilderSkia::Finish() {
RefPtr<Path> path =
MakeAndAddRef<PathSkia>(mPath, mFillRule, mCurrentPoint, mBeginPoint);
mCurrentPoint = Point(0.0, 0.0);
mBeginPoint = Point(0.0, 0.0);
return path.forget();
}
void PathBuilderSkia::AppendPath(const SkPath& aPath) { mPath.addPath(aPath); }
already_AddRefed<PathBuilder> PathSkia::CopyToBuilder(
FillRule aFillRule) const {
return TransformedCopyToBuilder(Matrix(), aFillRule);
}
already_AddRefed<PathBuilder> PathSkia::TransformedCopyToBuilder(
const Matrix& aTransform, FillRule aFillRule) const {
RefPtr<PathBuilderSkia> builder =
MakeAndAddRef<PathBuilderSkia>(aTransform, mPath, aFillRule);
builder->mCurrentPoint = aTransform.TransformPoint(mCurrentPoint);
builder->mBeginPoint = aTransform.TransformPoint(mBeginPoint);
return builder.forget();
}
static bool SkPathContainsPoint(const SkPath& aPath, const Point& aPoint,
const Matrix& aTransform) {
Matrix inverse = aTransform;
if (!inverse.Invert()) {
return false;
}
SkPoint point = PointToSkPoint(inverse.TransformPoint(aPoint));
return aPath.contains(point.fX, point.fY);
}
bool PathSkia::ContainsPoint(const Point& aPoint,
const Matrix& aTransform) const {
if (!mPath.isFinite()) {
return false;
}
return SkPathContainsPoint(mPath, aPoint, aTransform);
}
float ComputeResScaleForStroking(const Matrix& aTransform) {
SkMatrix skiaMatrix;
GfxMatrixToSkiaMatrix(aTransform, skiaMatrix);
return SkDraw::ComputeResScaleForStroking(skiaMatrix);
}
bool PathSkia::StrokeContainsPoint(const StrokeOptions& aStrokeOptions,
const Point& aPoint,
const Matrix& aTransform) const {
if (!mPath.isFinite()) {
return false;
}
SkPaint paint;
if (!StrokeOptionsToPaint(paint, aStrokeOptions)) {
return false;
}
SkPath strokePath;
paint.getFillPath(mPath, &strokePath, nullptr,
ComputeResScaleForStroking(aTransform));
return SkPathContainsPoint(strokePath, aPoint, aTransform);
}
Rect PathSkia::GetBounds(const Matrix& aTransform) const {
if (!mPath.isFinite()) {
return Rect();
}
Rect bounds = SkRectToRect(mPath.computeTightBounds());
return aTransform.TransformBounds(bounds);
}
Rect PathSkia::GetStrokedBounds(const StrokeOptions& aStrokeOptions,
const Matrix& aTransform) const {
if (!mPath.isFinite()) {
return Rect();
}
SkPaint paint;
if (!StrokeOptionsToPaint(paint, aStrokeOptions)) {
return Rect();
}
SkPath result;
paint.getFillPath(mPath, &result);
Rect bounds = SkRectToRect(result.computeTightBounds());
return aTransform.TransformBounds(bounds);
}
Rect PathSkia::GetFastBounds(const Matrix& aTransform,
const StrokeOptions* aStrokeOptions) const {
if (!mPath.isFinite()) {
return Rect();
}
SkRect bounds = mPath.getBounds();
if (aStrokeOptions) {
// If the path is stroked, ensure that the bounds are inflated by any
// relevant options such as line width. Avoid using dash path effects
// for performance and to ensure computeFastStrokeBounds succeeds.
SkPaint paint;
if (!StrokeOptionsToPaint(paint, *aStrokeOptions, false)) {
return Rect();
}
SkRect outBounds = SkRect::MakeEmpty();
bounds = paint.computeFastStrokeBounds(bounds, &outBounds);
}
return aTransform.TransformBounds(SkRectToRect(bounds));
}
int ConvertConicToQuads(const Point& aP0, const Point& aP1, const Point& aP2,
float aWeight, std::vector<Point>& aQuads) {
SkConic conic(PointToSkPoint(aP0), PointToSkPoint(aP1), PointToSkPoint(aP2),
aWeight);
int pow2 = conic.computeQuadPOW2(0.25f);
aQuads.resize(1 + 2 * (1 << pow2));
int numQuads =
conic.chopIntoQuadsPOW2(reinterpret_cast<SkPoint*>(&aQuads[0]), pow2);
if (numQuads < 1 << pow2) {
aQuads.resize(1 + 2 * numQuads);
}
return numQuads;
}
void PathSkia::StreamToSink(PathSink* aSink) const {
SkPath::RawIter iter(mPath);
SkPoint points[4];
SkPath::Verb currentVerb;
while ((currentVerb = iter.next(points)) != SkPath::kDone_Verb) {
switch (currentVerb) {
case SkPath::kMove_Verb:
aSink->MoveTo(SkPointToPoint(points[0]));
break;
case SkPath::kLine_Verb:
aSink->LineTo(SkPointToPoint(points[1]));
break;
case SkPath::kCubic_Verb:
aSink->BezierTo(SkPointToPoint(points[1]), SkPointToPoint(points[2]),
SkPointToPoint(points[3]));
break;
case SkPath::kQuad_Verb:
aSink->QuadraticBezierTo(SkPointToPoint(points[1]),
SkPointToPoint(points[2]));
break;
case SkPath::kConic_Verb: {
std::vector<Point> quads;
int numQuads = ConvertConicToQuads(
SkPointToPoint(points[0]), SkPointToPoint(points[1]),
SkPointToPoint(points[2]), iter.conicWeight(), quads);
for (int i = 0; i < numQuads; i++) {
aSink->QuadraticBezierTo(quads[2 * i + 1], quads[2 * i + 2]);
}
break;
}
case SkPath::kClose_Verb:
aSink->Close();
break;
default:
MOZ_ASSERT(false);
// Unexpected verb found in path!
}
}
}
Maybe<Rect> PathSkia::AsRect() const {
SkRect rect;
if (mPath.isRect(&rect)) {
return Some(SkRectToRect(rect));
}
return Nothing();
}
} // namespace mozilla::gfx
|