1 /*
2 * Copyright 2022 Google LLC
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/core/SkPathUtils.h"
9
10 #include "include/core/SkMatrix.h"
11 #include "include/core/SkPaint.h"
12 #include "include/core/SkPath.h"
13 #include "include/core/SkPathEffect.h"
14 #include "include/core/SkStrokeRec.h"
15 #include "src/core/SkMatrixPriv.h"
16
17 namespace skpathutils {
18
FillPathWithPaint(const SkPath & src,const SkPaint & paint,SkPath * dst)19 bool FillPathWithPaint(const SkPath& src, const SkPaint& paint, SkPath* dst) {
20 return skpathutils::FillPathWithPaint(src, paint, dst, nullptr, 1);
21 }
22
FillPathWithPaint(const SkPath & src,const SkPaint & paint,SkPath * dst,const SkRect * cullRect,SkScalar resScale)23 bool FillPathWithPaint(const SkPath& src, const SkPaint& paint, SkPath* dst,
24 const SkRect* cullRect, SkScalar resScale) {
25 return skpathutils::FillPathWithPaint(src, paint, dst, cullRect,
26 SkMatrix::Scale(resScale, resScale));
27 }
28
FillPathWithPaint(const SkPath & src,const SkPaint & paint,SkPath * dst,const SkRect * cullRect,const SkMatrix & ctm)29 bool FillPathWithPaint(const SkPath& src, const SkPaint& paint, SkPath* dst,
30 const SkRect* cullRect, const SkMatrix& ctm) {
31 if (!src.isFinite()) {
32 dst->reset();
33 return false;
34 }
35
36 const SkScalar resScale = SkMatrixPriv::ComputeResScaleForStroking(ctm);
37 SkStrokeRec rec(paint, resScale);
38
39 #if defined(SK_BUILD_FOR_FUZZER)
40 // Prevent lines with small widths from timing out.
41 if (rec.getStyle() == SkStrokeRec::Style::kStroke_Style && rec.getWidth() < 0.001) {
42 return false;
43 }
44 #endif
45
46 const SkPath* srcPtr = &src;
47 SkPath tmpPath;
48
49 SkPathEffect* pe = paint.getPathEffect();
50 if (pe && pe->filterPath(&tmpPath, src, &rec, cullRect, ctm)) {
51 srcPtr = &tmpPath;
52 }
53
54 if (!rec.applyToPath(dst, *srcPtr)) {
55 if (srcPtr == &tmpPath) {
56 // If path's were copy-on-write, this trick would not be needed.
57 // As it is, we want to save making a deep-copy from tmpPath -> dst
58 // since we know we're just going to delete tmpPath when we return,
59 // so the swap saves that copy.
60 dst->swap(tmpPath);
61 } else {
62 *dst = *srcPtr;
63 }
64 }
65
66 if (!dst->isFinite()) {
67 dst->reset();
68 return false;
69 }
70 return !rec.isHairlineStyle();
71 }
72
73 } // namespace skpathutils
74
FillPathWithPaint(const SkPath & src,const SkPaint & paint,SkPath * dst,const SkRect * cullRect,SkScalar resScale)75 SK_API bool FillPathWithPaint(const SkPath &src, const SkPaint &paint, SkPath *dst,
76 const SkRect *cullRect, SkScalar resScale) {
77 return skpathutils::FillPathWithPaint(src, paint, dst, cullRect, resScale);
78 }
79
FillPathWithPaint(const SkPath & src,const SkPaint & paint,SkPath * dst,const SkRect * cullRect,const SkMatrix & ctm)80 SK_API bool FillPathWithPaint(const SkPath &src, const SkPaint &paint, SkPath *dst,
81 const SkRect *cullRect, const SkMatrix &ctm) {
82 return skpathutils::FillPathWithPaint(src, paint, dst, cullRect, ctm);
83 }
84
FillPathWithPaint(const SkPath & src,const SkPaint & paint,SkPath * dst)85 SK_API bool FillPathWithPaint(const SkPath &src, const SkPaint &paint, SkPath *dst) {
86 return skpathutils::FillPathWithPaint(src, paint, dst);
87 }
88