1 /*
2 * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "skia_path.h"
17
18 #include "include/core/SkMatrix.h"
19 #include "include/pathops/SkPathOps.h"
20 #include "include/utils/SkParsePath.h"
21 #include "include/core/SkString.h"
22 #include "skia_matrix.h"
23
24 #include "src/core/SkReadBuffer.h"
25 #include "src/core/SkWriteBuffer.h"
26
27 #include "draw/path.h"
28 #include "utils/data.h"
29 #include "utils/log.h"
30
31 namespace OHOS {
32 namespace Rosen {
33 namespace Drawing {
34
SkiaPath(const SkiaPath & other)35 SkiaPath::SkiaPath(const SkiaPath& other) noexcept : path_(other.path_) {}
36
operator =(const SkiaPath & other)37 SkiaPath& SkiaPath::operator=(const SkiaPath& other) noexcept
38 {
39 path_ = other.path_;
40 return *this;
41 }
42
Clone()43 PathImpl* SkiaPath::Clone()
44 {
45 return new SkiaPath(*this);
46 }
47
InitWithSVGString(const std::string & str)48 bool SkiaPath::InitWithSVGString(const std::string& str)
49 {
50 return SkParsePath::FromSVGString(str.c_str(), &path_);
51 }
52
ConvertToSVGString() const53 std::string SkiaPath::ConvertToSVGString() const
54 {
55 SkString skString;
56 SkParsePath::ToSVGString(path_, &skString);
57
58 return skString.c_str();
59 }
60
MoveTo(scalar x,scalar y)61 void SkiaPath::MoveTo(scalar x, scalar y)
62 {
63 path_.moveTo(x, y);
64 isChanged_ = true;
65 }
66
LineTo(scalar x,scalar y)67 void SkiaPath::LineTo(scalar x, scalar y)
68 {
69 path_.lineTo(x, y);
70 isChanged_ = true;
71 }
72
ArcTo(scalar pt1X,scalar pt1Y,scalar pt2X,scalar pt2Y,scalar startAngle,scalar sweepAngle)73 void SkiaPath::ArcTo(scalar pt1X, scalar pt1Y, scalar pt2X, scalar pt2Y, scalar startAngle, scalar sweepAngle)
74 {
75 path_.arcTo(SkRect::MakeLTRB(pt1X, pt1Y, pt2X, pt2Y), startAngle, sweepAngle, false);
76 isChanged_ = true;
77 }
78
ArcTo(scalar rx,scalar ry,scalar angle,PathDirection direction,scalar endX,scalar endY)79 void SkiaPath::ArcTo(scalar rx, scalar ry, scalar angle, PathDirection direction, scalar endX, scalar endY)
80 {
81 SkPathDirection pathDir = static_cast<SkPathDirection>(direction);
82 SkPath::ArcSize arcLarge = SkPath::ArcSize::kSmall_ArcSize;
83 path_.arcTo(rx, ry, angle, arcLarge, pathDir, endX, endY);
84 isChanged_ = true;
85 }
86
ArcTo(scalar x1,scalar y1,scalar x2,scalar y2,scalar radius)87 void SkiaPath::ArcTo(scalar x1, scalar y1, scalar x2, scalar y2, scalar radius)
88 {
89 path_.arcTo(x1, y1, x2, y2, radius);
90 isChanged_ = true;
91 }
92
CubicTo(scalar ctrlPt1X,scalar ctrlPt1Y,scalar ctrlPt2X,scalar ctrlPt2Y,scalar endPtX,scalar endPtY)93 void SkiaPath::CubicTo(scalar ctrlPt1X, scalar ctrlPt1Y, scalar ctrlPt2X, scalar ctrlPt2Y, scalar endPtX, scalar endPtY)
94 {
95 path_.cubicTo(ctrlPt1X, ctrlPt1Y, ctrlPt2X, ctrlPt2Y, endPtX, endPtY);
96 isChanged_ = true;
97 }
98
QuadTo(scalar ctrlPtX,scalar ctrlPtY,scalar endPtX,scalar endPtY)99 void SkiaPath::QuadTo(scalar ctrlPtX, scalar ctrlPtY, scalar endPtX, scalar endPtY)
100 {
101 path_.quadTo(ctrlPtX, ctrlPtY, endPtX, endPtY);
102 isChanged_ = true;
103 }
104
ConicTo(scalar ctrlX,scalar ctrlY,scalar endX,scalar endY,scalar weight)105 void SkiaPath::ConicTo(scalar ctrlX, scalar ctrlY, scalar endX, scalar endY, scalar weight)
106 {
107 path_.conicTo(ctrlX, ctrlY, endX, endY, weight);
108 isChanged_ = true;
109 }
110
RMoveTo(scalar dx,scalar dy)111 void SkiaPath::RMoveTo(scalar dx, scalar dy)
112 {
113 path_.rMoveTo(dx, dy);
114 isChanged_ = true;
115 }
116
RLineTo(scalar dx,scalar dy)117 void SkiaPath::RLineTo(scalar dx, scalar dy)
118 {
119 path_.rLineTo(dx, dy);
120 isChanged_ = true;
121 }
122
RArcTo(scalar rx,scalar ry,scalar angle,PathDirection direction,scalar dx,scalar dy)123 void SkiaPath::RArcTo(scalar rx, scalar ry, scalar angle, PathDirection direction, scalar dx, scalar dy)
124 {
125 SkPathDirection pathDir = static_cast<SkPathDirection>(direction);
126 SkPath::ArcSize arcLarge = SkPath::ArcSize::kSmall_ArcSize;
127 path_.arcTo(rx, ry, angle, arcLarge, pathDir, dx, dy);
128 isChanged_ = true;
129 }
130
RCubicTo(scalar dx1,scalar dy1,scalar dx2,scalar dy2,scalar dx3,scalar dy3)131 void SkiaPath::RCubicTo(scalar dx1, scalar dy1, scalar dx2, scalar dy2, scalar dx3, scalar dy3)
132 {
133 path_.rCubicTo(dx1, dy1, dx2, dy2, dx3, dy3);
134 isChanged_ = true;
135 }
136
RConicTo(scalar ctrlPtX,scalar ctrlPtY,scalar endPtX,scalar endPtY,scalar weight)137 void SkiaPath::RConicTo(scalar ctrlPtX, scalar ctrlPtY, scalar endPtX, scalar endPtY, scalar weight)
138 {
139 path_.rConicTo(ctrlPtX, ctrlPtY, endPtX, endPtY, weight);
140 isChanged_ = true;
141 }
142
RQuadTo(scalar dx1,scalar dy1,scalar dx2,scalar dy2)143 void SkiaPath::RQuadTo(scalar dx1, scalar dy1, scalar dx2, scalar dy2)
144 {
145 path_.rQuadTo(dx1, dy1, dx2, dy2);
146 isChanged_ = true;
147 }
148
AddRect(scalar left,scalar top,scalar right,scalar bottom,PathDirection dir)149 void SkiaPath::AddRect(scalar left, scalar top, scalar right, scalar bottom, PathDirection dir)
150 {
151 SkPathDirection pathDir = static_cast<SkPathDirection>(dir);
152 path_.addRect(SkRect::MakeLTRB(left, top, right, bottom), pathDir);
153 isChanged_ = true;
154 }
155
AddRect(const Rect & rect,unsigned start,PathDirection dir)156 void SkiaPath::AddRect(const Rect& rect, unsigned start, PathDirection dir)
157 {
158 SkPathDirection pathDir = static_cast<SkPathDirection>(dir);
159 path_.addRect(SkRect::MakeLTRB(rect.GetLeft(), rect.GetTop(), rect.GetRight(), rect.GetBottom()), pathDir, start);
160 isChanged_ = true;
161 }
162
AddOval(scalar left,scalar top,scalar right,scalar bottom,PathDirection dir)163 void SkiaPath::AddOval(scalar left, scalar top, scalar right, scalar bottom, PathDirection dir)
164 {
165 SkPathDirection pathDir = static_cast<SkPathDirection>(dir);
166 path_.addOval(SkRect::MakeLTRB(left, top, right, bottom), pathDir);
167 isChanged_ = true;
168 }
169
AddOval(scalar left,scalar top,scalar right,scalar bottom,unsigned start,PathDirection dir)170 void SkiaPath::AddOval(scalar left, scalar top, scalar right, scalar bottom, unsigned start, PathDirection dir)
171 {
172 SkPathDirection pathDir = static_cast<SkPathDirection>(dir);
173 path_.addOval(SkRect::MakeLTRB(left, top, right, bottom), pathDir, start);
174 isChanged_ = true;
175 }
176
AddArc(scalar left,scalar top,scalar right,scalar bottom,scalar startAngle,scalar sweepAngle)177 void SkiaPath::AddArc(scalar left, scalar top, scalar right, scalar bottom, scalar startAngle, scalar sweepAngle)
178 {
179 path_.addArc(SkRect::MakeLTRB(left, top, right, bottom), startAngle, sweepAngle);
180 isChanged_ = true;
181 }
182
AddPoly(const std::vector<Point> & points,int count,bool close)183 void SkiaPath::AddPoly(const std::vector<Point>& points, int count, bool close)
184 {
185 std::vector<SkPoint> pt;
186 for (auto i = 0; i < count; ++i) {
187 pt.emplace_back(SkPoint::Make(points[i].GetX(), points[i].GetY()));
188 }
189 path_.addPoly(&pt[0], count, close);
190 isChanged_ = true;
191 }
192
AddCircle(scalar x,scalar y,scalar radius,PathDirection dir)193 void SkiaPath::AddCircle(scalar x, scalar y, scalar radius, PathDirection dir)
194 {
195 SkPathDirection pathDir = static_cast<SkPathDirection>(dir);
196 path_.addCircle(x, y, radius, pathDir);
197 isChanged_ = true;
198 }
199
AddRoundRect(scalar left,scalar top,scalar right,scalar bottom,scalar xRadius,scalar yRadius,PathDirection dir)200 void SkiaPath::AddRoundRect(
201 scalar left, scalar top, scalar right, scalar bottom, scalar xRadius, scalar yRadius, PathDirection dir)
202 {
203 SkPathDirection pathDir = static_cast<SkPathDirection>(dir);
204 path_.addRoundRect(SkRect::MakeLTRB(left, top, right, bottom), xRadius, yRadius, pathDir);
205 isChanged_ = true;
206 }
207
AddRoundRect(const RoundRect & rrect,PathDirection dir)208 void SkiaPath::AddRoundRect(const RoundRect& rrect, PathDirection dir)
209 {
210 SkPathDirection pathDir = static_cast<SkPathDirection>(dir);
211
212 Rect rect = rrect.GetRect();
213 SkRect outer = SkRect::MakeLTRB(rect.GetLeft(), rect.GetTop(), rect.GetRight(), rect.GetBottom());
214
215 SkVector radii[4];
216 Point p;
217 p = rrect.GetCornerRadius(RoundRect::TOP_LEFT_POS);
218 radii[SkRRect::kUpperLeft_Corner] = { p.GetX(), p.GetY() };
219 p = rrect.GetCornerRadius(RoundRect::TOP_RIGHT_POS);
220 radii[SkRRect::kUpperRight_Corner] = { p.GetX(), p.GetY() };
221 p = rrect.GetCornerRadius(RoundRect::BOTTOM_RIGHT_POS);
222 radii[SkRRect::kLowerRight_Corner] = { p.GetX(), p.GetY() };
223 p = rrect.GetCornerRadius(RoundRect::BOTTOM_LEFT_POS);
224 radii[SkRRect::kLowerLeft_Corner] = { p.GetX(), p.GetY() };
225
226 SkRRect skRRect;
227 skRRect.setRectRadii(outer, radii);
228 path_.addRRect(skRRect, pathDir);
229 isChanged_ = true;
230 }
231
AddPath(const Path & src,scalar dx,scalar dy,PathAddMode mode)232 void SkiaPath::AddPath(const Path& src, scalar dx, scalar dy, PathAddMode mode)
233 {
234 auto skPathImpl = src.GetImpl<SkiaPath>();
235 if (skPathImpl != nullptr) {
236 path_.addPath(skPathImpl->GetPath(), dx, dy, static_cast<SkPath::AddPathMode>(mode));
237 isChanged_ = true;
238 }
239 }
240
AddPath(const Path & src,PathAddMode mode)241 void SkiaPath::AddPath(const Path& src, PathAddMode mode)
242 {
243 auto skPathImpl = src.GetImpl<SkiaPath>();
244 if (skPathImpl != nullptr) {
245 path_.addPath(skPathImpl->GetPath(), static_cast<SkPath::AddPathMode>(mode));
246 isChanged_ = true;
247 }
248 }
249
Contains(scalar x,scalar y) const250 bool SkiaPath::Contains(scalar x, scalar y) const
251 {
252 return path_.contains(x, y);
253 }
254
ReverseAddPath(const Path & src)255 void SkiaPath::ReverseAddPath(const Path& src)
256 {
257 path_.reverseAddPath(src.GetImpl<SkiaPath>()->GetPath());
258 isChanged_ = true;
259 }
260
AddPath(const Path & src,const Matrix & matrix,PathAddMode mode)261 void SkiaPath::AddPath(const Path& src, const Matrix& matrix, PathAddMode mode)
262 {
263 auto skPathImpl = src.GetImpl<SkiaPath>();
264 auto skMatrixImpl = matrix.GetImpl<SkiaMatrix>();
265 if (skPathImpl != nullptr && skMatrixImpl != nullptr) {
266 path_.addPath(skPathImpl->GetPath(), skMatrixImpl->ExportSkiaMatrix(), static_cast<SkPath::AddPathMode>(mode));
267 isChanged_ = true;
268 }
269 }
270
GetBounds() const271 Rect SkiaPath::GetBounds() const
272 {
273 SkRect rect = path_.getBounds();
274 return Rect(rect.left(), rect.top(), rect.right(), rect.bottom());
275 }
276
SetFillStyle(PathFillType fillstyle)277 void SkiaPath::SetFillStyle(PathFillType fillstyle)
278 {
279 SkPathFillType ft = static_cast<SkPathFillType>(fillstyle);
280 path_.setFillType(ft);
281 isChanged_ = true;
282 }
283
Interpolate(const Path & ending,scalar weight,Path & out)284 bool SkiaPath::Interpolate(const Path& ending, scalar weight, Path& out)
285 {
286 bool isSuccess = false;
287 auto skPathImpl1 = ending.GetImpl<SkiaPath>();
288 auto skPathImpl2 = out.GetImpl<SkiaPath>();
289 if (skPathImpl1 != nullptr && skPathImpl2 != nullptr) {
290 SkPath interp;
291 isSuccess = path_.interpolate(skPathImpl1->GetPath(), weight, &interp);
292 skPathImpl2->SetPath(interp);
293 isChanged_ = true;
294 }
295 return isSuccess;
296 }
297
InitWithInterpolate(const Path & srcPath,const Path & endingPath,scalar weight)298 bool SkiaPath::InitWithInterpolate(const Path& srcPath, const Path& endingPath, scalar weight)
299 {
300 const SkPath& srcSkPath = srcPath.GetImpl<SkiaPath>()->GetPath();
301 isChanged_ = true;
302 return srcSkPath.interpolate(endingPath.GetImpl<SkiaPath>()->GetPath(), weight, &path_);
303 }
304
Transform(const Matrix & matrix)305 void SkiaPath::Transform(const Matrix& matrix)
306 {
307 auto skMatrixImpl = matrix.GetImpl<SkiaMatrix>();
308 if (skMatrixImpl != nullptr) {
309 path_.transform(skMatrixImpl->ExportSkiaMatrix());
310 isChanged_ = true;
311 }
312 }
313
TransformWithPerspectiveClip(const Matrix & matrix,Path * dst,bool applyPerspectiveClip)314 void SkiaPath::TransformWithPerspectiveClip(const Matrix& matrix, Path* dst, bool applyPerspectiveClip)
315 {
316 auto skMatrixImpl = matrix.GetImpl<SkiaMatrix>();
317 if (skMatrixImpl == nullptr) {
318 LOGE("SkiaPath::TransformWithPerspectiveClip, skMatrixImpl is nullptr!");
319 return;
320 }
321 if (dst == nullptr) {
322 path_.transform(skMatrixImpl->ExportSkiaMatrix(), nullptr,
323 static_cast<SkApplyPerspectiveClip>(applyPerspectiveClip));
324 return;
325 }
326 auto dstPathImpl = dst->GetImpl<SkiaPath>();
327 if (dstPathImpl == nullptr) {
328 LOGE("SkiaPath::TransformWithPerspectiveClip, dstPathImpl is nullptr!");
329 return;
330 }
331 path_.transform(skMatrixImpl->ExportSkiaMatrix(), &dstPathImpl->path_,
332 static_cast<SkApplyPerspectiveClip>(applyPerspectiveClip));
333 isChanged_ = true;
334 }
335
Offset(scalar dx,scalar dy)336 void SkiaPath::Offset(scalar dx, scalar dy)
337 {
338 path_.offset(dx, dy);
339 isChanged_ = true;
340 }
341
Offset(Path * dst,scalar dx,scalar dy)342 void SkiaPath::Offset(Path* dst, scalar dx, scalar dy)
343 {
344 if (dst == nullptr) {
345 path_.offset(dx, dy, nullptr);
346 return;
347 }
348 auto dstPathImpl = dst->GetImpl<SkiaPath>();
349 if (dstPathImpl == nullptr) {
350 LOGE("SkiaPath::Offset, data is invalid!");
351 return;
352 }
353 path_.offset(dx, dy, &dstPathImpl->path_);
354 isChanged_ = true;
355 }
356
OpWith(const Path & path1,const Path & path2,PathOp op)357 bool SkiaPath::OpWith(const Path& path1, const Path& path2, PathOp op)
358 {
359 SkPathOp pathOp = static_cast<SkPathOp>(op);
360 bool isOpSuccess = false;
361
362 auto skPathImpl1 = path1.GetImpl<SkiaPath>();
363 auto skPathImpl2 = path2.GetImpl<SkiaPath>();
364 if (skPathImpl1 != nullptr && skPathImpl2 != nullptr) {
365 isOpSuccess = Op(skPathImpl1->GetPath(), skPathImpl2->GetPath(), pathOp, &path_);
366 }
367
368 isChanged_ = true;
369 if (isOpSuccess) {
370 return true;
371 }
372 return false;
373 }
374
IsValid() const375 bool SkiaPath::IsValid() const
376 {
377 return !path_.isEmpty();
378 }
379
Reset()380 void SkiaPath::Reset()
381 {
382 path_.reset();
383 isChanged_ = true;
384 }
385
Close()386 void SkiaPath::Close()
387 {
388 path_.close();
389 isChanged_ = true;
390 }
391
SetPath(const SkPath & path)392 void SkiaPath::SetPath(const SkPath& path)
393 {
394 path_ = path;
395 isChanged_ = true;
396 }
397
GetPath() const398 const SkPath& SkiaPath::GetPath() const
399 {
400 return path_;
401 }
402
GetMutablePath()403 SkPath& SkiaPath::GetMutablePath()
404 {
405 isChanged_ = true;
406 return path_;
407 }
408
PathMeasureUpdate(bool forceClosed)409 void SkiaPath::PathMeasureUpdate(bool forceClosed)
410 {
411 if (pathMeasure_ == nullptr) {
412 pathMeasure_ = std::make_unique<SkPathMeasure>(path_, forceClosed);
413 isChanged_ = false;
414 forceClosed_ = forceClosed;
415 return;
416 }
417
418 if (isChanged_ || forceClosed != forceClosed_) {
419 pathMeasure_->setPath(&path_, forceClosed);
420 isChanged_ = false;
421 forceClosed_ = forceClosed;
422 }
423 }
424
GetLength(bool forceClosed)425 scalar SkiaPath::GetLength(bool forceClosed)
426 {
427 PathMeasureUpdate(forceClosed);
428 return pathMeasure_->getLength();
429 }
430
GetPositionAndTangent(scalar distance,Point & position,Point & tangent,bool forceClosed)431 bool SkiaPath::GetPositionAndTangent(scalar distance, Point& position, Point& tangent, bool forceClosed)
432 {
433 PathMeasureUpdate(forceClosed);
434 bool ret = false;
435 SkPoint skPosition;
436 SkVector skTangent;
437 ret = pathMeasure_->getPosTan(distance, &skPosition, &skTangent);
438 if (ret) {
439 position.SetX(skPosition.x());
440 position.SetY(skPosition.y());
441 tangent.SetX(skTangent.x());
442 tangent.SetY(skTangent.y());
443 }
444
445 return ret;
446 }
447
IsClosed(bool forceClosed)448 bool SkiaPath::IsClosed(bool forceClosed)
449 {
450 PathMeasureUpdate(forceClosed);
451 return pathMeasure_->isClosed();
452 }
453
GetMatrix(bool forceClosed,float distance,Matrix * matrix,PathMeasureMatrixFlags flag)454 bool SkiaPath::GetMatrix(bool forceClosed, float distance, Matrix* matrix, PathMeasureMatrixFlags flag)
455 {
456 if (matrix == nullptr) {
457 return false;
458 }
459 PathMeasureUpdate(forceClosed);
460 SkPathMeasure::MatrixFlags skFlag = SkPathMeasure::kGetPosAndTan_MatrixFlag;
461 if (flag == PathMeasureMatrixFlags::GET_POSITION_MATRIX) {
462 skFlag = SkPathMeasure::kGetPosition_MatrixFlag;
463 } else if (flag == PathMeasureMatrixFlags::GET_TANGENT_MATRIX) {
464 skFlag = SkPathMeasure::kGetTangent_MatrixFlag;
465 }
466 return pathMeasure_->getMatrix(distance,
467 &matrix->GetImpl<SkiaMatrix>()->ExportMatrix(), skFlag);
468 }
469
Serialize() const470 std::shared_ptr<Data> SkiaPath::Serialize() const
471 {
472 if (path_.isEmpty()) {
473 LOGE("SkiaPath::Serialize, path is empty!");
474 }
475 SkBinaryWriteBuffer writer;
476 writer.writePath(path_);
477 size_t length = writer.bytesWritten();
478 std::shared_ptr<Data> data = std::make_shared<Data>();
479 data->BuildUninitialized(length);
480 writer.writeToMemory(data->WritableData());
481 return data;
482 }
483
Deserialize(std::shared_ptr<Data> data)484 bool SkiaPath::Deserialize(std::shared_ptr<Data> data)
485 {
486 if (data == nullptr) {
487 LOGE("SkiaPath::Deserialize, data is invalid!");
488 return false;
489 }
490
491 SkReadBuffer reader(data->GetData(), data->GetSize());
492 reader.readPath(&path_);
493 return true;
494 }
495
496 } // namespace Drawing
497 } // namespace Rosen
498 } // namespace OHOS
499