Debian Codegraph @

include/core/SkPath.h

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 #ifndef SkPath_DEFINED
9 #define SkPath_DEFINED
10
11 #include "include/core/SkMatrix.h"
12 #include "include/core/SkPathIter.h"
13 #include "include/core/SkPathTypes.h"
14 #include "include/core/SkPoint.h"
15 #include "include/core/SkRect.h"
16 #include "include/core/SkRefCnt.h"
17 #include "include/core/SkScalar.h"
18 #include "include/core/SkSpan.h"
19 #include "include/core/SkTypes.h"
20 #include "include/private/base/SkDebug.h"
21 #include "include/private/base/SkTo.h"
22 #include "include/private/base/SkTypeTraits.h"
23
24 #include <atomic>
25 #include <cstddef>
26 #include <cstdint>
27 #include <optional>
28 #include <tuple>
29
30 class SkData;
31 class SkRRect;
32 class SkWStream;
33 enum class SkPathConvexity;
34 enum class SkResolveConvexity;
35 struct SkPathRaw;
36 struct SkPathOvalInfo;
37 struct SkPathRRectInfo;
38
39 // Migrate clients so this is unneeded
40 #define SK_LEGACY_PATH_ACCESSORS
41
42 class SkPathData;
43
44 /** \class SkPath
45 SkPath contain geometry. SkPath may be empty, or contain one or more verbs that
46 outline a figure. SkPath always starts with a move verb to a Cartesian coordinate,
47 and may be followed by additional verbs that add lines or curves.
48 Adding a close verb makes the geometry into a continuous loop, a closed contour.
49 SkPath may contain any number of contours, each beginning with a move verb.
50
51 SkPath contours may contain only a move verb, or may also contain lines,
52 quadratic beziers, conics, and cubic beziers. SkPath contours may be open or
53 closed.
54
55 When used to draw a filled area, SkPath describes whether the fill is inside or
56 outside the geometry. SkPath also describes the winding rule used to fill
57 overlapping contours.
58
59 Internally, SkPath lazily computes metrics likes bounds and convexity. Call
60 SkPath::updateBoundsCache to make SkPath thread safe.
61 */
62 class SK_API SkPath {
63 public:
64 /**
65 * Create a new path with the specified spans.
66 *
67 * The points and weights arrays are read in order, based on the sequence of verbs.
68 *
69 * Move 1 point
70 * Line 1 point
71 * Quad 2 points
72 * Conic 2 points and 1 weight
73 * Cubic 3 points
74 * Close 0 points
75 *
76 * If an illegal sequence of verbs is encountered, or the specified number of points
77 * or weights is not sufficient given the verbs, an empty Path is returned.
78 *
79 * A legal sequence of verbs consists of any number of Contours. A contour always begins
80 * with a Move verb, followed by 0 or more segments: Line, Quad, Conic, Cubic, followed
81 * by an optional Close.
82 */
83 static SkPath Raw(SkSpan<const SkPoint> pts,
84 SkSpan<const SkPathVerb> verbs,
85 SkSpan<const SkScalar> conics,
86 SkPathFillType, bool isVolatile = false);
87
88 static SkPath Rect(const SkRect&, SkPathFillType, SkPathDirection = SkPathDirection::kDefault,
89 unsigned startIndex = 0);
90 static SkPath Rect(const SkRect& r, SkPathDirection direction = SkPathDirection::kDefault,
91 unsigned startIndex = 0) {
92 return Rect(r, SkPathFillType::kDefault, direction, startIndex);
93 }
94 static SkPath Oval(const SkRect&, SkPathDirection = SkPathDirection::kDefault);
95 static SkPath Oval(const SkRect&, SkPathDirection, unsigned startIndex);
96 static SkPath Circle(SkScalar center_x, SkScalar center_y, SkScalar radius,
97 SkPathDirection dir = SkPathDirection::kCW);
98 static SkPath RRect(const SkRRect&, SkPathDirection dir = SkPathDirection::kDefault);
99 static SkPath RRect(const SkRRect&, SkPathDirection, unsigned startIndex);
100 static SkPath RRect(const SkRect& bounds, SkScalar rx, SkScalar ry,
101 SkPathDirection dir = SkPathDirection::kDefault);
102
103 static SkPath Polygon(SkSpan<const SkPoint> pts, bool isClosed,
104 SkPathFillType fillType = SkPathFillType::kDefault,
105 bool isVolatile = false);
106
107 static SkPath Line(SkPoint a, SkPoint b) {
108 return Polygon({{a, b}}, false);
109 }
110
111 // Deprecated: use Raw()
112 static SkPath Make(SkSpan<const SkPoint> pts,
113 SkSpan<const uint8_t> verbs,
114 SkSpan<const SkScalar> conics,
115 SkPathFillType fillType,
116 bool isVolatile = false) {
117 return Raw(pts, {reinterpret_cast<const SkPathVerb*>(verbs.data()), verbs.size()},
118 conics, fillType, isVolatile);
119 }
120
121 /** Constructs an empty SkPath: no verbs, no points, no conic weights.
122
123 @return empty SkPath
124
125 example: https://fiddle.skia.org/c/@Path_empty_constructor
126 */
127 explicit SkPath(SkPathFillType);
128
129 SkPath() : SkPath(SkPathFillType::kDefault) {}
130
131 /** Constructs a copy of an existing path.
132 Copy constructor makes two paths identical by value. Internally, path and
133 the returned result share pointer values. The underlying verb array, SkPoint array
134 and weights are copied when modified.
135
136 Creating a SkPath copy is very efficient and never allocates memory.
137 SkPath are always copied by value from the interface; the underlying shared
138 pointers are not exposed.
139
140 @param path SkPath to copy by value
141 @return copy of SkPath
142
143 example: https://fiddle.skia.org/c/@Path_copy_const_SkPath
144 */
145 SkPath(const SkPath&);
146 SkPath(SkPath&&);
147
148 /** Releases ownership of any shared data and deletes data if SkPath is sole owner.
149
150 example: https://fiddle.skia.org/c/@Path_destructor
151 */
152 ~SkPath();
153
154 /** Returns a copy of this path in the current state. */
155 SkPath snapshot() const {
156 return *this;
157 }
158
159 /** Constructs a copy of an existing path.
160 SkPath assignment makes two paths identical by value. Internally, assignment
161 shares pointer values. The underlying verb array, SkPoint array and weights
162 are copied when modified.
163
164 Copying SkPath by assignment is very efficient and never allocates memory.
165 SkPath are always copied by value from the interface; the underlying shared
166 pointers are not exposed.
167
168 @param path verb array, SkPoint array, weights, and SkPath::FillType to copy
169 @return SkPath copied by value
170
171 example: https://fiddle.skia.org/c/@Path_copy_operator
172 */
173 SkPath& operator=(const SkPath&);
174 SkPath& operator=(SkPath&&);
175
176 /** Compares a and b; returns true if SkPath::FillType, verb array, SkPoint array, and weights
177 are equivalent.
178
179 @param a SkPath to compare
180 @param b SkPath to compare
181 @return true if SkPath pair are equivalent
182 */
183 friend SK_API bool operator==(const SkPath& a, const SkPath& b);
184
185 /** Compares a and b; returns true if SkPath::FillType, verb array, SkPoint array, and weights
186 are not equivalent.
187
188 @param a SkPath to compare
189 @param b SkPath to compare
190 @return true if SkPath pair are not equivalent
191 */
192 friend bool operator!=(const SkPath& a, const SkPath& b) {
193 return !(a == b);
194 }
195
196 // Note: These 3 interpolate() methods no long use any private access/info,
197 // and could trivially be implemented directly by the client.
198
199 /** Returns true if SkPath contain equal verbs and equal weights.
200 If SkPath contain one or more conics, the weights must match.
201
202 conicTo() may add different verbs depending on conic weight, so it is not
203 trivial to interpolate a pair of SkPath containing conics with different
204 conic weight values.
205
206 @param compare SkPath to compare
207 @return true if SkPath verb array and weights are equivalent
208
209 example: https://fiddle.skia.org/c/@Path_isInterpolatable
210 */
211 bool isInterpolatable(const SkPath& compare) const;
212
213 /** Interpolates between SkPath with SkPoint array of equal size.
214 Copy verb array and weights to out, and set out SkPoint array to a weighted
215 average of this SkPoint array and ending SkPoint array, using the formula:
216 (Path Point * weight) + ending Point * (1 - weight).
217
218 weight is most useful when between zero (ending SkPoint array) and
219 one (this Point_Array); will work with values outside of this
220 range.
221
222 interpolate() returns an empty SkPath if SkPoint array is not the same size
223 as ending SkPoint array. Call isInterpolatable() to check SkPath compatibility
224 prior to calling makeInterpolate().
225
226 @param ending SkPoint array averaged with this SkPoint array
227 @param weight contribution of this SkPoint array, and
228 one minus contribution of ending SkPoint array
229 @return SkPath replaced by interpolated averages
230
231 example: https://fiddle.skia.org/c/@Path_interpolate
232 */
233 SkPath makeInterpolate(const SkPath& ending, SkScalar weight) const;
234
235 /** Interpolates between SkPath with SkPoint array of equal size.
236 Copy verb array and weights to out, and set out SkPoint array to a weighted
237 average of this SkPoint array and ending SkPoint array, using the formula:
238 (Path Point * weight) + ending Point * (1 - weight).
239
240 weight is most useful when between zero (ending SkPoint array) and
241 one (this Point_Array); will work with values outside of this
242 range.
243
244 interpolate() returns false and leaves out unchanged if SkPoint array is not
245 the same size as ending SkPoint array. Call isInterpolatable() to check SkPath
246 compatibility prior to calling interpolate().
247
248 @param ending SkPoint array averaged with this SkPoint array
249 @param weight contribution of this SkPoint array, and
250 one minus contribution of ending SkPoint array
251 @param out SkPath replaced by interpolated averages
252 @return true if SkPath contain same number of SkPoint
253
254 example: https://fiddle.skia.org/c/@Path_interpolate
255 */
256 bool interpolate(const SkPath& ending, SkScalar weight, SkPath* out) const;
257
258 /** Returns SkPathFillType, the rule used to fill SkPath.
259
260 @return current SkPathFillType setting
261 */
262 SkPathFillType getFillType() const { return (SkPathFillType)fFillType; }
263
264 /** Creates an SkPath with the same properties and data, and with SkPathFillType
265 set to newFillType.
266 */
267 SkPath makeFillType(SkPathFillType newFillType) const;
268
269 /** Returns if SkPathFillType describes area outside SkPath geometry. The inverse fill area
270 extends indefinitely.
271
272 @return true if FillType is kInverseWinding or kInverseEvenOdd
273 */
274 bool isInverseFillType() const { return SkPathFillType_IsInverse(this->getFillType()); }
275
276 /** Creates an SkPath with the same properties and data, and with SkPathFillType
277 replaced with its inverse. The inverse of SkPathFillType describes the area unmodified
278 by the original FillType.
279 */
280 SkPath makeToggleInverseFillType() const;
281
282 /** Returns true if the path is convex. If necessary, it will first compute the convexity.
283 */
284 bool isConvex() const;
285
286 /** Returns true if this path is recognized as an oval or circle.
287
288 bounds receives bounds of oval.
289
290 bounds is unmodified if oval is not found.
291
292 @param bounds storage for bounding SkRect of oval; may be nullptr
293 @return true if SkPath is recognized as an oval or circle
294
295 example: https://fiddle.skia.org/c/@Path_isOval
296 */
297 bool isOval(SkRect* bounds) const;
298
299 /** Returns true if path is representable as SkRRect.
300 Returns false if path is representable as oval, circle, or SkRect.
301
302 rrect receives bounds of SkRRect.
303
304 rrect is unmodified if SkRRect is not found.
305
306 @param rrect storage for bounding SkRect of SkRRect; may be nullptr
307 @return true if SkPath contains only SkRRect
308
309 example: https://fiddle.skia.org/c/@Path_isRRect
310 */
311 bool isRRect(SkRRect* rrect) const;
312
313 /** Returns if SkPath is empty.
314 Empty SkPath may have FillType but has no SkPoint, SkPath::Verb, or conic weight.
315 SkPath() constructs empty SkPath; reset() and rewind() make SkPath empty.
316
317 @return true if the path contains no SkPath::Verb array
318 */
319 bool isEmpty() const;
320
321 /** Returns if contour is closed.
322 Contour is closed if SkPath SkPath::Verb array was last modified by close(). When stroked,
323 closed contour draws SkPaint::Join instead of SkPaint::Cap at first and last SkPoint.
324
325 @return true if the last contour ends with a kClose_Verb
326
327 example: https://fiddle.skia.org/c/@Path_isLastContourClosed
328 */
329 bool isLastContourClosed() const;
330
331 /** Returns true for finite SkPoint array values between negative SK_ScalarMax and
332 positive SK_ScalarMax. Returns false for any SkPoint array value of
333 SK_ScalarInfinity, SK_ScalarNegativeInfinity, or SK_ScalarNaN.
334
335 @return true if all SkPoint values are finite
336 */
337 bool isFinite() const;
338
339 /** Returns true if the path is volatile; it will not be altered or discarded
340 by the caller after it is drawn. SkPath by default have volatile set false, allowing
341 SkSurface to attach a cache of data which speeds repeated drawing. If true, SkSurface
342 may not speed repeated drawing.
343
344 @return true if caller will alter SkPath after drawing
345 */
346 bool isVolatile() const {
347 return SkToBool(fIsVolatile);
348 }
349
350 /** Return a copy of SkPath with isVolatile indicating whether it will be altered
351 or discarded by the caller after it is drawn. SkPath by default have volatile
352 set false, allowing Skia to attach a cache of data which speeds repeated drawing.
353
354 Mark temporary paths, discarded or modified after use, as volatile
355 to inform Skia that the path need not be cached.
356
357 Mark animating SkPath volatile to improve performance.
358 Mark unchanging SkPath non-volatile to improve repeated rendering.
359
360 raster surface SkPath draws are affected by volatile for some shadows.
361 GPU surface SkPath draws are affected by volatile for some shadows and concave geometries.
362
363 @param isVolatile true if caller will alter SkPath after drawing
364 @return SkPath
365 */
366 SkPath makeIsVolatile(bool isVolatile) const;
367
368 /** Tests if line between SkPoint pair is degenerate.
369 Line with no length or that moves a very short distance is degenerate; it is
370 treated as a point.
371
372 exact changes the equality test. If true, returns true only if p1 equals p2.
373 If false, returns true if p1 equals or nearly equals p2.
374
375 @param p1 line start point
376 @param p2 line end point
377 @param exact if false, allow nearly equals
378 @return true if line is degenerate; its length is effectively zero
379
380 example: https://fiddle.skia.org/c/@Path_IsLineDegenerate
381 */
382 static bool IsLineDegenerate(const SkPoint& p1, const SkPoint& p2, bool exact);
383
384 /** Tests if quad is degenerate.
385 Quad with no length or that moves a very short distance is degenerate; it is
386 treated as a point.
387
388 @param p1 quad start point
389 @param p2 quad control point
390 @param p3 quad end point
391 @param exact if true, returns true only if p1, p2, and p3 are equal;
392 if false, returns true if p1, p2, and p3 are equal or nearly equal
393 @return true if quad is degenerate; its length is effectively zero
394 */
395 static bool IsQuadDegenerate(const SkPoint& p1, const SkPoint& p2,
396 const SkPoint& p3, bool exact);
397
398 /** Tests if cubic is degenerate.
399 Cubic with no length or that moves a very short distance is degenerate; it is
400 treated as a point.
401
402 @param p1 cubic start point
403 @param p2 cubic control point 1
404 @param p3 cubic control point 2
405 @param p4 cubic end point
406 @param exact if true, returns true only if p1, p2, p3, and p4 are equal;
407 if false, returns true if p1, p2, p3, and p4 are equal or nearly equal
408 @return true if cubic is degenerate; its length is effectively zero
409 */
410 static bool IsCubicDegenerate(const SkPoint& p1, const SkPoint& p2,
411 const SkPoint& p3, const SkPoint& p4, bool exact);
412
413 /** Returns true if SkPath contains only one line;
414 SkPath::Verb array has two entries: kMove_Verb, kLine_Verb.
415 If SkPath contains one line and line is not nullptr, line is set to
416 line start point and line end point.
417 Returns false if SkPath is not one line; line is unaltered.
418
419 @param line storage for line. May be nullptr
420 @return true if SkPath contains exactly one line
421
422 example: https://fiddle.skia.org/c/@Path_isLine
423 */
424 bool isLine(SkPoint line[2]) const;
425
426 /*
427 * Return a read-only view into the path's points.
428 */
429 SkSpan<const SkPoint> points() const;
430
431 /*
432 * Return a read-only view into the path's verbs.
433 */
434 SkSpan<const SkPathVerb> verbs() const;
435
436 /*
437 * Return a read-only view into the path's conic-weights.
438 */
439 SkSpan<const float> conicWeights() const;
440
441 int countPoints() const { return SkToInt(this->points().size()); }
442 int countVerbs() const { return SkToInt(this->verbs().size()); }
443
444 /** Return the last point, or {}
445
446 @return The last if the path contains one or more SkPoint, else returns {}
447
448 example: https://fiddle.skia.org/c/@Path_getLastPt
449 */
450 std::optional<SkPoint> getLastPt() const;
451
452 #ifdef SK_LEGACY_PATH_ACCESSORS
453 /** Returns SkPoint at index in SkPoint array. Valid range for index is
454 0 to countPoints() - 1.
455 Returns (0, 0) if index is out of range.
456 DEPRECATED
457 @param index SkPoint array element selector
458 @return SkPoint array value or (0, 0)
459 */
460 SkPoint getPoint(int index) const;
461
462 /** Returns number of points in SkPath.
463 Copies N points from the path into the span, where N = min(#points, span capacity)
464 DEPRECATED
465 @param points span to receive the points. may be empty
466 @return the number of points in the path
467 */
468 size_t getPoints(SkSpan<SkPoint> points) const;
469
470 /** Returns number of points in SkPath.
471 Copies N points from the path into the span, where N = min(#points, span capacity)
472 DEPRECATED
473 @param verbs span to store the verbs. may be empty.
474 @return the number of verbs in the path
475
476 example: https://fiddle.skia.org/c/@Path_getVerbs
477 */
478 size_t getVerbs(SkSpan<uint8_t> verbs) const;
479
480 // DEPRECATED
481 bool getLastPt(SkPoint* lastPt) const {
482 if (auto lp = this->getLastPt()) {
483 if (lastPt) {
484 *lastPt = *lp;
485 }
486 return true;
487 }
488 if (lastPt) {
489 *lastPt = {0, 0};
490 }
491 return false;
492 }
493 #endif
494
495 /** Returns the approximate byte size of the SkPath in memory.
496
497 @return approximate size
498 */
499 size_t approximateBytesUsed() const;
500
501 /** Returns the min/max of the path's 'trimmed' points. The trimmed points are all of the
502 points in the path, with the exception of the path having more than one contour, and the
503 final contour containing only a kMove verb. In that case the trailing kMove point
504 is ignored when computing the bounds.
505
506 If the path has no verbs, or the path contains non-finite values,
507 then {0, 0, 0, 0} is returned. (see isFinite())
508
509 @return bounds of the path's points
510 */
511 const SkRect& getBounds() const;
512
513 /** Updates internal bounds so that subsequent calls to getBounds() are instantaneous.
514 Unaltered copies of SkPath may also access cached bounds through getBounds().
515
516 For now, identical to calling getBounds() and ignoring the returned value.
517
518 Call to prepare SkPath subsequently drawn from multiple threads,
519 to avoid a race condition where each draw separately computes the bounds.
520 */
521 void updateBoundsCache() const {
522 // for now, just calling getBounds() is sufficient
523 this->getBounds();
524 }
525
526 /** Returns minimum and maximum axes values of the lines and curves in SkPath.
527 Returns (0, 0, 0, 0) if SkPath contains no points.
528 Returned bounds width and height may be larger or smaller than area affected
529 when SkPath is drawn.
530
531 Includes SkPoint associated with kMove_Verb that define empty
532 contours.
533
534 Behaves identically to getBounds() when SkPath contains
535 only lines. If SkPath contains curves, computed bounds includes
536 the maximum extent of the quad, conic, or cubic; is slower than getBounds();
537 and unlike getBounds(), does not cache the result.
538
539 @return tight bounds of curves in SkPath
540
541 example: https://fiddle.skia.org/c/@Path_computeTightBounds
542 */
543 SkRect computeTightBounds() const;
544
545 /** Returns true if rect is contained by SkPath.
546 May return false when rect is contained by SkPath.
547
548 For now, only returns true if SkPath has one contour and is convex.
549 rect may share points and edges with SkPath and be contained.
550 Returns true if rect is empty, that is, it has zero width or height; and
551 the SkPoint or line described by rect is contained by SkPath.
552
553 @param rect SkRect, line, or SkPoint checked for containment
554 @return true if rect is contained
555
556 example: https://fiddle.skia.org/c/@Path_conservativelyContainsRect
557 */
558 bool conservativelyContainsRect(const SkRect& rect) const;
559
560 /** Approximates conic with quad array. Conic is constructed from start SkPoint p0,
561 control SkPoint p1, end SkPoint p2, and weight w.
562 Quad array is stored in pts; this storage is supplied by caller.
563 Maximum quad count is 2 to the pow2.
564 Every third point in array shares last SkPoint of previous quad and first SkPoint of
565 next quad. Maximum pts storage size is given by:
566 (1 + 2 * (1 << pow2)) * sizeof(SkPoint).
567
568 Returns quad count used the approximation, which may be smaller
569 than the number requested.
570
571 conic weight determines the amount of influence conic control point has on the curve.
572 w less than one represents an elliptical section. w greater than one represents
573 a hyperbolic section. w equal to one represents a parabolic section.
574
575 Two quad curves are sufficient to approximate an elliptical conic with a sweep
576 of up to 90 degrees; in this case, set pow2 to one.
577
578 @param p0 conic start SkPoint
579 @param p1 conic control SkPoint
580 @param p2 conic end SkPoint
581 @param w conic weight
582 @param pts storage for quad array
583 @param pow2 quad count, as power of two, normally 0 to 5 (1 to 32 quad curves)
584 @return number of quad curves written to pts
585 */
586 static int ConvertConicToQuads(const SkPoint& p0, const SkPoint& p1, const SkPoint& p2,
587 SkScalar w, SkPoint pts[], int pow2);
588
589 /** Returns true if SkPath is equivalent to SkRect when filled.
590 If false: rect, isClosed, and direction are unchanged.
591 If true: rect, isClosed, and direction are written to if not nullptr.
592
593 rect may be smaller than the SkPath bounds. SkPath bounds may include kMove_Verb points
594 that do not alter the area drawn by the returned rect.
595
596 @param rect storage for bounds of SkRect; may be nullptr
597 @param isClosed storage set to true if SkPath is closed; may be nullptr
598 @param direction storage set to SkRect direction; may be nullptr
599 @return true if SkPath contains SkRect
600
601 example: https://fiddle.skia.org/c/@Path_isRect
602 */
603 bool isRect(SkRect* rect, bool* isClosed = nullptr, SkPathDirection* direction = nullptr) const;
604
605 /** \enum SkPath::AddPathMode
606 AddPathMode chooses how addPath() appends. Adding one SkPath to another can extend
607 the last contour or start a new contour.
608 */
609 enum AddPathMode {
610 /** Contours are appended to the destination path as new contours.
611 */
612 kAppend_AddPathMode,
613 /** Extends the last contour of the destination path with the first countour
614 of the source path, connecting them with a line. If the last contour is
615 closed, a new empty contour starting at its start point is extended instead.
616 If the destination path is empty, the result is the source path.
617 The last path of the result is closed only if the last path of the source is.
618 */
619 kExtend_AddPathMode,
620 };
621
622 /** Return a copy of SkPath with verb array, SkPoint array, and weight transformed
623 by matrix. makeTransform may change verbs and increase their number.
624
625 If the resulting path has any non-finite values, returns {}.
626
627 @param matrix SkMatrix to apply to SkPath
628 @return SkPath if finite, or {}
629 */
630 std::optional<SkPath> tryMakeTransform(const SkMatrix& matrix) const;
631
632 std::optional<SkPath> tryMakeOffset(float dx, float dy) const {
633 return this->tryMakeTransform(SkMatrix::Translate(dx, dy));
634 }
635
636 std::optional<SkPath> tryMakeScale(float sx, float sy) const {
637 return this->tryMakeTransform(SkMatrix::Scale(sx, sy));
638 }
639
640 /** Return a copy of SkPath with verb array, SkPoint array, and weight transformed
641 by matrix. makeTransform may change verbs and increase their number.
642
643 If the resulting path has any non-finite values, this will still return a path
644 but that path will return true for isFinite().
645
646 The newer pattern is to call tryMakeTransform(matrix) which will only return a
647 path if the result is finite.
648
649 @param matrix SkMatrix to apply to SkPath
650 @return SkPath
651 */
652 SkPath makeTransform(const SkMatrix& matrix) const;
653
654 /** Returns SkPath with SkPoint array offset by (dx, dy).
655
656 @param dx offset added to SkPoint array x-axis coordinates
657 @param dy offset added to SkPoint array y-axis coordinates
658 */
659 SkPath makeOffset(SkScalar dx, SkScalar dy) const {
660 return this->makeTransform(SkMatrix::Translate(dx, dy));
661 }
662
663 SkPath makeScale(SkScalar sx, SkScalar sy) const {
664 return this->makeTransform(SkMatrix::Scale(sx, sy));
665 }
666
667 /** \enum SkPath::SegmentMask
668 SegmentMask constants correspond to each drawing Verb type in SkPath; for
669 instance, if SkPath only contains lines, only the kLine_SegmentMask bit is set.
670 */
671 enum SegmentMask {
672 kLine_SegmentMask = kLine_SkPathSegmentMask,
673 kQuad_SegmentMask = kQuad_SkPathSegmentMask,
674 kConic_SegmentMask = kConic_SkPathSegmentMask,
675 kCubic_SegmentMask = kCubic_SkPathSegmentMask,
676 };
677
678 /** Returns a mask, where each set bit corresponds to a SegmentMask constant
679 if SkPath contains one or more verbs of that type.
680 Returns zero if SkPath contains no lines, or curves: quads, conics, or cubics.
681
682 getSegmentMasks() returns a cached result; it is very fast.
683
684 @return SegmentMask bits or zero
685 */
686 uint32_t getSegmentMasks() const;
687
688 /** \enum SkPath::Verb
689 Verb instructs SkPath how to interpret one or more SkPoint and optional conic weight;
690 manage contour, and terminate SkPath.
691 */
692 enum Verb {
693 kMove_Verb = static_cast<int>(SkPathVerb::kMove),
694 kLine_Verb = static_cast<int>(SkPathVerb::kLine),
695 kQuad_Verb = static_cast<int>(SkPathVerb::kQuad),
696 kConic_Verb = static_cast<int>(SkPathVerb::kConic),
697 kCubic_Verb = static_cast<int>(SkPathVerb::kCubic),
698 kClose_Verb = static_cast<int>(SkPathVerb::kClose),
699 kDone_Verb = kClose_Verb + 1
700 };
701
702 /** Specifies whether SkPath is volatile; whether it will be altered or discarded
703 by the caller after it is drawn. SkPath by default have volatile set false, allowing
704 Skia to attach a cache of data which speeds repeated drawing.
705
706 Mark temporary paths, discarded or modified after use, as volatile
707 to inform Skia that the path need not be cached.
708
709 Mark animating SkPath volatile to improve performance.
710 Mark unchanging SkPath non-volatile to improve repeated rendering.
711
712 raster surface SkPath draws are affected by volatile for some shadows.
713 GPU surface SkPath draws are affected by volatile for some shadows and concave geometries.
714
715 @param isVolatile true if caller will alter SkPath after drawing
716 @return reference to SkPath
717 */
718 SkPath& setIsVolatile(bool isVolatile) {
719 fIsVolatile = isVolatile;
720 return *this;
721 }
722
723 /** Exchanges the verb array, SkPoint array, weights, and SkPath::FillType with other.
724 Cached state is also exchanged. swap() internally exchanges pointers, so
725 it is lightweight and does not allocate memory.
726
727 swap() usage has largely been replaced by operator=(const SkPath& path).
728 SkPath do not copy their content on assignment until they are written to,
729 making assignment as efficient as swap().
730
731 @param other SkPath exchanged by value
732
733 example: https://fiddle.skia.org/c/@Path_swap
734 */
735 void swap(SkPath& other);
736
737 /** Sets SkPathFillType, the rule used to fill SkPath. While there is no
738 check that ft is legal, values outside of SkPathFillType are not supported.
739 */
740 void setFillType(SkPathFillType ft) {
741 fFillType = ft;
742 }
743
744 /** Replaces SkPathFillType with its inverse. The inverse of SkPathFillType describes the area
745 unmodified by the original SkPathFillType.
746 */
747 void toggleInverseFillType() {
748 fFillType = SkPathFillType_ToggleInverse(fFillType);
749 }
750
751 /** Sets SkPath to its initial state.
752 Removes verb array, SkPoint array, and weights, and sets FillType to kWinding.
753 Internal storage associated with SkPath is released.
754
755 @return reference to SkPath
756
757 example: https://fiddle.skia.org/c/@Path_reset
758 */
759 SkPath& reset();
760
761 #ifdef SK_SUPPORT_UNSPANNED_APIS
762 static SkPath Make(const SkPoint points[], int pointCount,
763 const uint8_t verbs[], int verbCount,
764 const SkScalar conics[], int conicWeightCount,
765 SkPathFillType fillType, bool isVolatile = false) {
766 return Make({points, pointCount},
767 {verbs, verbCount},
768 {conics, conicWeightCount},
769 fillType, isVolatile);
770 }
771 static SkPath Polygon(const SkPoint pts[], int count, bool isClosed,
772 SkPathFillType fillType = SkPathFillType::kWinding,
773 bool isVolatile = false) {
774 return Polygon({pts, count}, isClosed, fillType, isVolatile);
775 }
776 int getPoints(SkPoint points[], int max) const {
777 return (int)this->getPoints({points, max});
778 }
779 int getVerbs(uint8_t verbs[], int max) const {
780 return (int)this->getVerbs({verbs, max});
781 }
782 #endif // SK_SUPPORT_UNSPANNED_APIS
783
784 SkPathIter iter() const;
785
786 struct IterRec {
787 SkPathVerb fVerb;
788 SkSpan<const SkPoint> fPoints;
789 float fConicWeight;
790
791 float conicWeight() const {
792 SkASSERT(fVerb == SkPathVerb::kConic);
793 return fConicWeight;
794 }
795 };
796
797 /** \class SkPath::Iter
798 Iterates through verb array, and associated SkPoint array and conic weight.
799 Provides options to treat open contours as closed, and to ignore
800 degenerate data.
801 */
802 class SK_API Iter {
803 public:
804
805 /** Initializes SkPath::Iter with an empty SkPath. next() on SkPath::Iter returns
806 kDone_Verb.
807 Call setPath to initialize SkPath::Iter at a later time.
808
809 @return SkPath::Iter of empty SkPath
810
811 example: https://fiddle.skia.org/c/@Path_Iter_Iter
812 */
813 Iter();
814
815 /** Sets SkPath::Iter to return elements of verb array, SkPoint array, and conic weight in
816 path. If forceClose is true, SkPath::Iter will add kLine_Verb and kClose_Verb after each
817 open contour. path is not altered.
818
819 @param path SkPath to iterate
820 @param forceClose true if open contours generate kClose_Verb
821 @return SkPath::Iter of path
822
823 example: https://fiddle.skia.org/c/@Path_Iter_const_SkPath
824 */
825 Iter(const SkPath& path, bool forceClose);
826
827 /** Sets SkPath::Iter to return elements of verb array, SkPoint array, and conic weight in
828 path. If forceClose is true, SkPath::Iter will add kLine_Verb and kClose_Verb after each
829 open contour. path is not altered.
830
831 @param path SkPath to iterate
832 @param forceClose true if open contours generate kClose_Verb
833
834 example: https://fiddle.skia.org/c/@Path_Iter_setPath
835 */
836 void setPath(const SkPath& path, bool forceClose);
837
838 /** Returns next SkPath::Verb in verb array, and advances SkPath::Iter.
839 When verb array is exhausted, returns kDone_Verb.
840
841 Zero to four SkPoint are stored in pts, depending on the returned SkPath::Verb.
842
843 @param pts storage for SkPoint data describing returned SkPath::Verb
844 @return next SkPath::Verb from verb array
845
846 example: https://fiddle.skia.org/c/@Path_RawIter_next
847 */
848 Verb next(SkPoint pts[4]);
849
850 std::optional<IterRec> next();
851
852 /** Returns conic weight if next() returned kConic_Verb.
853
854 If next() has not been called, or next() did not return kConic_Verb,
855 result is undefined.
856
857 @return conic weight for conic SkPoint returned by next()
858 */
859 SkScalar conicWeight() const { return *fConicWeights; }
860
861 /** Returns true if last kLine_Verb returned by next() was generated
862 by kClose_Verb. When true, the end point returned by next() is
863 also the start point of contour.
864
865 If next() has not been called, or next() did not return kLine_Verb,
866 result is undefined.
867
868 @return true if last kLine_Verb was generated by kClose_Verb
869 */
870 bool isCloseLine() const { return SkToBool(fCloseLine); }
871
872 /** Returns true if subsequent calls to next() return kClose_Verb before returning
873 kMove_Verb. if true, contour SkPath::Iter is processing may end with kClose_Verb, or
874 SkPath::Iter may have been initialized with force close set to true.
875
876 @return true if contour is closed
877
878 example: https://fiddle.skia.org/c/@Path_Iter_isClosedContour
879 */
880 bool isClosedContour() const;
881
882 private:
883 const SkPoint* fPts;
884 const SkPathVerb* fVerbs;
885 const SkPathVerb* fVerbStop;
886 const SkScalar* fConicWeights;
887 SkPoint fMoveTo;
888 SkPoint fLastPt;
889 std::array<SkPoint, 4> fStorage;
890 bool fForceClose;
891 bool fNeedClose;
892 bool fCloseLine;
893
894 SkPathVerb autoClose(SkPoint pts[2]);
895 };
896
897 private:
898 std::optional<SkPathOvalInfo> getOvalInfo() const;
899 std::optional<SkPathRRectInfo> getRRectInfo() const;
900 std::optional<SkPathRaw> raw(SkResolveConvexity) const;
901
902 /** \class SkPath::RangeIter
903 Iterates through a raw range of path verbs, points, and conics. All values are returned
904 unaltered.
905
906 NOTE: This class will be moved into SkPathPriv once RangeIter is removed.
907 */
908 class RangeIter {
909 public:
910 RangeIter() = default;
911 RangeIter(const SkPathVerb* verbs, const SkPoint* points, const SkScalar* weights)
912 : fVerb(verbs), fPoints(points), fWeights(weights) {
913 SkDEBUGCODE(fInitialPoints = fPoints;)
914 }
915 bool operator!=(const RangeIter& that) const {
916 return fVerb != that.fVerb;
917 }
918 bool operator==(const RangeIter& that) const {
919 return fVerb == that.fVerb;
920 }
921 RangeIter& operator++() {
922 auto verb = *fVerb++;
923 fPoints += pts_advance_after_verb(verb);
924 if (verb == SkPathVerb::kConic) {
925 ++fWeights;
926 }
927 return *this;
928 }
929 RangeIter operator++(int) {
930 RangeIter copy = *this;
931 this->operator++();
932 return copy;
933 }
934 SkPathVerb peekVerb() const {
935 return *fVerb;
936 }
937 std::tuple<SkPathVerb, const SkPoint*, const SkScalar*> operator*() const {
938 SkPathVerb verb = this->peekVerb();
939 // We provide the starting point for beziers by peeking backwards from the current
940 // point, which works fine as long as there is always a kMove before any geometry.
941 // (SkPath::injectMoveToIfNeeded should have guaranteed this to be the case.)
942 int backset = pts_backset_for_verb(verb);
943 SkASSERT(fPoints + backset >= fInitialPoints);
944 return {verb, fPoints + backset, fWeights};
945 }
946 private:
947 constexpr static int pts_advance_after_verb(SkPathVerb verb) {
948 switch (verb) {
949 case SkPathVerb::kMove: return 1;
950 case SkPathVerb::kLine: return 1;
951 case SkPathVerb::kQuad: return 2;
952 case SkPathVerb::kConic: return 2;
953 case SkPathVerb::kCubic: return 3;
954 case SkPathVerb::kClose: return 0;
955 }
956 SkUNREACHABLE;
957 }
958 constexpr static int pts_backset_for_verb(SkPathVerb verb) {
959 switch (verb) {
960 case SkPathVerb::kMove: return 0;
961 case SkPathVerb::kLine: return -1;
962 case SkPathVerb::kQuad: return -1;
963 case SkPathVerb::kConic: return -1;
964 case SkPathVerb::kCubic: return -1;
965 case SkPathVerb::kClose: return -1;
966 }
967 SkUNREACHABLE;
968 }
969 const SkPathVerb* fVerb = nullptr;
970 const SkPoint* fPoints = nullptr;
971 const SkScalar* fWeights = nullptr;
972 SkDEBUGCODE(const SkPoint* fInitialPoints = nullptr;)
973 };
974 public:
975
976 /** \class SkPath::RawIter
977 Use Iter instead. This class will soon be removed and RangeIter will be made private.
978 */
979 class SK_API RawIter {
980 public:
981
982 /** Initializes RawIter with an empty SkPath. next() on RawIter returns kDone_Verb.
983 Call setPath to initialize SkPath::Iter at a later time.
984
985 @return RawIter of empty SkPath
986 */
987 RawIter() {}
988
989 /** Sets RawIter to return elements of verb array, SkPoint array, and conic weight in path.
990
991 @param path SkPath to iterate
992 @return RawIter of path
993 */
994 RawIter(const SkPath& path) {
995 setPath(path);
996 }
997
998 /** Sets SkPath::Iter to return elements of verb array, SkPoint array, and conic weight in
999 path.
1000
1001 @param path SkPath to iterate
1002 */
1003 void setPath(const SkPath&);
1004
1005 /** Returns next SkPath::Verb in verb array, and advances RawIter.
1006 When verb array is exhausted, returns kDone_Verb.
1007 Zero to four SkPoint are stored in pts, depending on the returned SkPath::Verb.
1008
1009 @param pts storage for SkPoint data describing returned SkPath::Verb
1010 @return next SkPath::Verb from verb array
1011 */
1012 Verb next(SkPoint[4]);
1013
1014 std::optional<IterRec> next();
1015
1016 /** Returns next SkPath::Verb, but does not advance RawIter.
1017
1018 @return next SkPath::Verb from verb array
1019 */
1020 Verb peek() const {
1021 return (fIter != fEnd) ? static_cast<Verb>(std::get<0>(*fIter)) : kDone_Verb;
1022 }
1023
1024 /** Returns conic weight if next() returned kConic_Verb.
1025
1026 If next() has not been called, or next() did not return kConic_Verb,
1027 result is undefined.
1028
1029 @return conic weight for conic SkPoint returned by next()
1030 */
1031 SkScalar conicWeight() const {
1032 return fConicWeight;
1033 }
1034
1035 private:
1036 RangeIter fIter;
1037 RangeIter fEnd;
1038 SkScalar fConicWeight = 0;
1039 friend class SkPath;
1040
1041 };
1042
1043 /** Returns true if the point is contained by SkPath, taking into
1044 account FillType.
1045
1046 @param point the point to test
1047 @return true if SkPoint is in SkPath
1048 */
1049 bool contains(SkPoint point) const;
1050
1051 // deprecated
1052 bool contains(SkScalar x, SkScalar y) const {
1053 return this->contains({x, y});
1054 }
1055
1056 /** Writes text representation of SkPath to stream. If stream is nullptr, writes to
1057 standard output. Set dumpAsHex true to generate exact binary representations
1058 of floating point numbers used in SkPoint array and conic weights.
1059
1060 @param stream writable SkWStream receiving SkPath text representation; may be nullptr
1061 @param dumpAsHex true if SkScalar values are written as hexadecimal
1062
1063 example: https://fiddle.skia.org/c/@Path_dump
1064 */
1065 void dump(SkWStream* stream, bool dumpAsHex) const;
1066
1067 void dump() const { this->dump(nullptr, false); }
1068 void dumpHex() const { this->dump(nullptr, true); }
1069
1070 /** Writes SkPath to buffer, returning the number of bytes written.
1071 Pass nullptr to obtain the storage size.
1072
1073 Writes SkPath::FillType, verb array, SkPoint array, conic weight, and
1074 additionally writes computed information like SkPath::Convexity and bounds.
1075
1076 Use only be used in concert with readFromMemory();
1077 the format used for SkPath in memory is not guaranteed.
1078
1079 @param buffer storage for SkPath; may be nullptr
1080 @return size of storage required for SkPath; always a multiple of 4
1081
1082 example: https://fiddle.skia.org/c/@Path_writeToMemory
1083 */
1084 size_t writeToMemory(void* buffer) const;
1085
1086 /** Writes SkPath to buffer, returning the buffer written to, wrapped in SkData.
1087
1088 serialize() writes SkPath::FillType, verb array, SkPoint array, conic weight, and
1089 additionally writes computed information like SkPath::Convexity and bounds.
1090
1091 serialize() should only be used in concert with readFromMemory().
1092 The format used for SkPath in memory is not guaranteed.
1093
1094 @return SkPath data wrapped in SkData buffer
1095
1096 example: https://fiddle.skia.org/c/@Path_serialize
1097 */
1098 sk_sp<SkData> serialize() const;
1099
1100 /** Returns a SkPath from buffer of size length. If the buffer data is inconsistent, or the
1101 length is too small, returns a nullopt.
1102
1103 Reads SkPath::FillType, verb array, SkPoint array, conic weight, and
1104 additionally reads computed information like SkPath::Convexity and bounds.
1105
1106 Used only in concert with writeToMemory();
1107 the format used for SkPath in memory is not guaranteed.
1108
1109 @param buffer storage for SkPath
1110 @param length buffer size in bytes; must be multiple of 4
1111 @param bytesRead if not null, the number of bytes read from buffer will be written here
1112 @return the path read, or nullopt on failure
1113
1114 example: https://fiddle.skia.org/c/@Path_readFromMemory
1115 */
1116 static std::optional<SkPath> ReadFromMemory(const void* buffer, size_t length,
1117 size_t* bytesRead = nullptr);
1118
1119 /** (See skbug.com/40032862)
1120 Returns a non-zero, globally unique value. A different value is returned
1121 if verb array, SkPoint array, or conic weight changes.
1122
1123 Setting SkPath::FillType does not change generation identifier.
1124
1125 Each time the path is modified, a different generation identifier will be returned.
1126 SkPath::FillType does affect generation identifier on Android framework.
1127
1128 @return non-zero, globally unique value
1129
1130 example: https://fiddle.skia.org/c/@Path_getGenerationID
1131 */
1132 uint32_t getGenerationID() const;
1133
1134 /** Returns if SkPath data is consistent. Corrupt SkPath data is detected if
1135 internal values are out of range or internal storage does not match
1136 array dimensions.
1137
1138 @return true if SkPath data is consistent
1139 */
1140 bool isValid() const;
1141
1142 using sk_is_trivially_relocatable = std::true_type;
1143
1144 private:
1145 static SkPath MakeNullCheck(sk_sp<SkPathData>, SkPathFillType, bool isVolatile);
1146 static SkPathData* PeekErrorSingleton();
1147
1148 SkPath(sk_sp<SkPathData>, SkPathFillType, bool isVolatile);
1149
1150 sk_sp<SkPathData> fPathData;
1151 SkPathFillType fFillType;
1152 bool fIsVolatile;
1153
1154 size_t writeToMemoryAsRRect(void* buffer) const;
1155
1156 friend class Iter;
1157 friend class SkPathPriv;
1158 friend class SkPathStroker;
1159
1160 SkPathConvexity computeConvexity() const;
1161
1162 bool isValidImpl() const;
1163 /** Asserts if SkPath data is inconsistent.
1164 Debugging check intended for internal use only.
1165 */
1166 #ifdef SK_DEBUG
1167 void validate() const;
1168 #endif
1169
1170 /** Returns the comvexity type, computing if needed. Never returns kUnknown.
1171 @return path's convexity type (convex or concave)
1172 */
1173 SkPathConvexity getConvexity() const;
1174
1175 SkPathConvexity getConvexityOrUnknown() const;
1176
1177 /** Stores a convexity type for this path. This is what will be returned if
1178 * getConvexityOrUnknown() is called. If you pass kUnknown, then if getContexityType()
1179 * is called, the real convexity will be computed.
1180 */
1181 void setConvexity(SkPathConvexity) const;
1182
1183 friend class SkPathBuilder;
1184 };
1185
1186 #endif