Debian Codegraph @

include/core/SkPathBuilder.h

raw
1 /*
2 * Copyright 2015 Google Inc.
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 SkPathBuilder_DEFINED
9 #define SkPathBuilder_DEFINED
10
11 #include "include/core/SkMatrix.h"
12 #include "include/core/SkPath.h"
13 #include "include/core/SkPathIter.h"
14 #include "include/core/SkPathTypes.h"
15 #include "include/core/SkPoint.h"
16 #include "include/core/SkRect.h"
17 #include "include/core/SkRefCnt.h"
18 #include "include/core/SkScalar.h"
19 #include "include/core/SkSpan.h"
20 #include "include/core/SkTypes.h"
21 #include "include/private/SkPathRef.h"
22 #include "include/private/base/SkTArray.h"
23
24 #include <cstdint>
25 #include <optional>
26 #include <tuple>
27
28 class SkPathData;
29 class SkRRect;
30 struct SkPathRaw;
31 class SkString;
32
33 // todo: add this flag to clients
34 #ifndef SK_SUPPORT_LEGACY_PATHBUILDER_SETLASTPT
35 #define SK_SUPPORT_LEGACY_PATHBUILDER_SETLASTPT
36 #endif
37
38 class SK_API SkPathBuilder {
39 using PointsArray = skia_private::STArray<4, SkPoint>;
40 using VerbsArray = skia_private::STArray<4, SkPathVerb>;
41 using ConicWeightsArray = skia_private::STArray<2, float>;
42 public:
43 /** Constructs an empty SkPathBuilder. By default, SkPathBuilder has no verbs, no SkPoint, and
44 no weights. FillType is set to kWinding.
45
46 @return empty SkPathBuilder
47 */
48 SkPathBuilder();
49
50 SkPathBuilder(const SkPathBuilder&);
51 SkPathBuilder& operator=(const SkPathBuilder&);
52 SkPathBuilder(SkPathBuilder&&);
53 SkPathBuilder& operator=(SkPathBuilder&&);
54 ~SkPathBuilder();
55
56 /** Constructs an empty SkPathBuilder with the given FillType. By default, SkPathBuilder has no
57 verbs, no SkPoint, and no weights.
58
59 @param fillType SkPathFillType to set on the SkPathBuilder.
60 @return empty SkPathBuilder
61 */
62 explicit SkPathBuilder(SkPathFillType fillType);
63
64 /** Constructs an SkPathBuilder that is a copy of an existing SkPath.
65 Copies the FillType and replays all of the verbs from the SkPath into the SkPathBuilder.
66
67 @param path SkPath to copy
68 @return SkPathBuilder
69 */
70 explicit SkPathBuilder(const SkPath& path);
71
72 /** Sets an SkPathBuilder to be a copy of an existing SkPath.
73 Copies the FillType and replays all of the verbs from the SkPath into the SkPathBuilder.
74
75 @param path SkPath to copy
76 @return SkPathBuilder
77 */
78 SkPathBuilder& operator=(const SkPath&);
79
80 bool operator==(const SkPathBuilder&) const;
81 bool operator!=(const SkPathBuilder& o) const { return !(*this == o); }
82
83 /** Returns SkPathFillType, the rule used to fill SkPath.
84
85 @return current SkPathFillType setting
86 */
87 SkPathFillType fillType() const { return fFillType; }
88
89 /** Returns minimum and maximum axes values of SkPoint array.
90 Returns (0, 0, 0, 0) if SkPathBuilder contains no points.
91
92 SkRect returned includes all SkPoint added to SkPathBuilder, including SkPoint associated
93 with kMove_Verb that define empty contours.
94
95 If any of the points are non-finite, returns {}.
96
97 @return bounds of all SkPoint in SkPoint array, or {}.
98 */
99 std::optional<SkRect> computeFiniteBounds() const;
100
101 /** Like computeFiniteBounds() but returns a 'tight' bounds, meaning when there are curve
102 * segments, this computes the X/Y limits of the curve itself, not the curve's control
103 * point(s). For a polygon, this returns the same as computeFiniteBounds().
104 */
105 std::optional<SkRect> computeTightBounds() const;
106
107 // DEPRECATED -- returns "empty" if the bounds are non-finite
108 SkRect computeBounds() const {
109 if (auto bounds = this->computeFiniteBounds()) {
110 return *bounds;
111 }
112 return SkRect::MakeEmpty();
113 }
114
115 /** Returns an SkPath representing the current state of the SkPathBuilder. The builder is
116 unchanged after returning the path.
117
118 @param mx if present, applied to the points after they are copied into the resulting path.
119 @return SkPath representing the current state of the builder.
120 */
121 SkPath snapshot(const SkMatrix* mx = nullptr) const;
122
123 /** Returns an SkPath representing the current state of the SkPathBuilder. The builder is
124 reset to empty after returning the path.
125
126 @param mx if present, applied to the points after they are copied into the resulting path.
127 @return SkPath representing the current state of the builder.
128 */
129 SkPath detach(const SkMatrix* mx = nullptr);
130
131 sk_sp<SkPathData> snapshotData() const;
132 sk_sp<SkPathData> detachData();
133
134 /** Sets SkPathFillType, the rule used to fill SkPath. While there is no
135 check that ft is legal, values outside of SkPathFillType are not supported.
136
137 @param ft SkPathFillType to be used by SKPaths generated from this builder.
138 @return reference to SkPathBuilder
139 */
140 SkPathBuilder& setFillType(SkPathFillType ft) { fFillType = ft; return *this; }
141
142 /** Specifies whether SkPath is volatile; whether it will be altered or discarded
143 by the caller after it is drawn. SkPath by default have volatile set false, allowing
144 Skia to attach a cache of data which speeds repeated drawing.
145
146 Mark temporary paths, discarded or modified after use, as volatile
147 to inform Skia that the path need not be cached.
148
149 Mark animating SkPath volatile to improve performance.
150 Mark unchanging SkPath non-volatile to improve repeated rendering.
151
152 raster surface SkPath draws are affected by volatile for some shadows.
153 GPU surface SkPath draws are affected by volatile for some shadows and concave geometries.
154
155 @param isVolatile true if caller will alter SkPath after drawing
156 @return reference to SkPathBuilder
157 */
158 SkPathBuilder& setIsVolatile(bool isVolatile) { fIsVolatile = isVolatile; return *this; }
159
160 /** Sets SkPathBuilder to its initial state.
161 Removes verb array, SkPoint array, and weights, and sets FillType to kWinding.
162 Internal storage associated with SkPathBuilder is preserved.
163
164 @return reference to SkPathBuilder
165 */
166 SkPathBuilder& reset();
167
168 /** Specifies the beginning of contour. If the previous verb was a "move" verb,
169 * then this just replaces the point value of that move, otherwise it appends a new
170 * "move" verb to the builder using the point.
171 *
172 * Thus, each contour can only have 1 move verb in it (the last one specified).
173 */
174 SkPathBuilder& moveTo(SkPoint point);
175
176 SkPathBuilder& moveTo(SkScalar x, SkScalar y) {
177 return this->moveTo(SkPoint::Make(x, y));
178 }
179
180 /** Adds line from last point to SkPoint p. If SkPathBuilder is empty, or last SkPath::Verb is
181 kClose_Verb, last point is set to (0, 0) before adding line.
182
183 lineTo() first appends kMove_Verb to verb array and (0, 0) to SkPoint array, if needed.
184 lineTo() then appends kLine_Verb to verb array and SkPoint p to SkPoint array.
185
186 @param p end SkPoint of added line
187 @return reference to SkPathBuilder
188 */
189 SkPathBuilder& lineTo(SkPoint pt);
190
191 /** Adds line from last point to (x, y). If SkPathBuilder is empty, or last SkPath::Verb is
192 kClose_Verb, last point is set to (0, 0) before adding line.
193
194 lineTo() appends kMove_Verb to verb array and (0, 0) to SkPoint array, if needed.
195 lineTo() then appends kLine_Verb to verb array and (x, y) to SkPoint array.
196
197 @param x end of added line on x-axis
198 @param y end of added line on y-axis
199 @return reference to SkPathBuilder
200 */
201 SkPathBuilder& lineTo(SkScalar x, SkScalar y) { return this->lineTo(SkPoint::Make(x, y)); }
202
203 /** Adds quad from last point towards SkPoint p1, to SkPoint p2.
204 If SkPathBuilder is empty, or last SkPath::Verb is kClose_Verb, last point is set to (0, 0)
205 before adding quad.
206
207 Appends kMove_Verb to verb array and (0, 0) to SkPoint array, if needed;
208 then appends kQuad_Verb to verb array; and SkPoint p1, p2
209 to SkPoint array.
210
211 @param p1 control SkPoint of added quad
212 @param p2 end SkPoint of added quad
213 @return reference to SkPathBuilder
214 */
215 SkPathBuilder& quadTo(SkPoint pt1, SkPoint pt2);
216
217 /** Adds quad from last point towards (x1, y1), to (x2, y2).
218 If SkPath is empty, or last SkPath::Verb is kClose_Verb, last point is set to (0, 0)
219 before adding quad.
220
221 Appends kMove_Verb to verb array and (0, 0) to SkPoint array, if needed;
222 then appends kQuad_Verb to verb array; and (x1, y1), (x2, y2)
223 to SkPoint array.
224
225 @param x1 control SkPoint of quad on x-axis
226 @param y1 control SkPoint of quad on y-axis
227 @param x2 end SkPoint of quad on x-axis
228 @param y2 end SkPoint of quad on y-axis
229 @return reference to SkPath
230
231 example: https://fiddle.skia.org/c/@Path_quadTo
232 */
233 SkPathBuilder& quadTo(SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2) {
234 return this->quadTo(SkPoint::Make(x1, y1), SkPoint::Make(x2, y2));
235 }
236
237 /** Adds quad from last point towards the first SkPoint in pts, to the second.
238 If SkPathBuilder is empty, or last SkPath::Verb is kClose_Verb, last point is set to (0, 0)
239 before adding quad.
240
241 Appends kMove_Verb to verb array and (0, 0) to SkPoint array, if needed;
242 then appends kQuad_Verb to verb array; and the SkPoints to SkPoint array.
243
244 @param pts control point and endpoint of added quad.
245 @return reference to SkPathBuilder
246 */
247 SkPathBuilder& quadTo(const SkPoint pts[2]) { return this->quadTo(pts[0], pts[1]); }
248
249 /** Adds conic from last point towards pt1, to pt2, weighted by w.
250 If SkPathBuilder is empty, or last SkPath::Verb is kClose_Verb, last point is set to (0, 0)
251 before adding conic.
252
253 Appends kMove_Verb to verb array and (0, 0) to SkPoint array, if needed.
254
255 If w is finite and not one, appends kConic_Verb to verb array;
256 and pt1, pt2 to SkPoint array; and w to conic weights.
257
258 If w is one, appends kQuad_Verb to verb array, and
259 pt1, pt2 to SkPoint array.
260
261 If w is not finite, appends kLine_Verb twice to verb array, and
262 pt1, pt2 to SkPoint array.
263
264 @param pt1 control SkPoint of conic
265 @param pt2 end SkPoint of conic
266 @param w weight of added conic
267 @return reference to SkPathBuilder
268 */
269 SkPathBuilder& conicTo(SkPoint pt1, SkPoint pt2, SkScalar w);
270
271 /** Adds conic from last point towards (x1, y1), to (x2, y2), weighted by w.
272 If SkPathBuilder is empty, or last SkPath::Verb is kClose_Verb, last point is set to (0, 0)
273 before adding conic.
274
275 Appends kMove_Verb to verb array and (0, 0) to SkPoint array, if needed.
276
277 If w is finite and not one, appends kConic_Verb to verb array;
278 and (x1, y1), (x2, y2) to SkPoint array; and w to conic weights.
279
280 If w is one, appends kQuad_Verb to verb array, and
281 (x1, y1), (x2, y2) to SkPoint array.
282
283 If w is not finite, appends kLine_Verb twice to verb array, and
284 (x1, y1), (x2, y2) to SkPoint array.
285
286 @param x1 control SkPoint of conic on x-axis
287 @param y1 control SkPoint of conic on y-axis
288 @param x2 end SkPoint of conic on x-axis
289 @param y2 end SkPoint of conic on y-axis
290 @param w weight of added conic
291 @return reference to SkPathBuilder
292 */
293 SkPathBuilder& conicTo(SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2, SkScalar w) {
294 return this->conicTo(SkPoint::Make(x1, y1), SkPoint::Make(x2, y2), w);
295 }
296
297 /** Adds conic from last point towards SkPoint p1, to SkPoint p2, weighted by w.
298 If SkPathBuilder is empty, or last SkPath::Verb is kClose_Verb, last point is set to (0, 0)
299 before adding conic.
300
301 Appends kMove_Verb to verb array and (0, 0) to SkPoint array, if needed.
302
303 If w is finite and not one, appends kConic_Verb to verb array;
304 and SkPoint p1, p2 to SkPoint array; and w to conic weights.
305
306 If w is one, appends kQuad_Verb to verb array, and SkPoint p1, p2
307 to SkPoint array.
308
309 If w is not finite, appends kLine_Verb twice to verb array, and
310 SkPoint p1, p2 to SkPoint array.
311
312 @param p1 control SkPoint of added conic
313 @param p2 end SkPoint of added conic
314 @param w weight of added conic
315 @return reference to SkPathBuilder
316 */
317 SkPathBuilder& conicTo(const SkPoint pts[2], SkScalar w) {
318 return this->conicTo(pts[0], pts[1], w);
319 }
320
321 /** Adds cubic from last point towards SkPoint p1, then towards SkPoint p2, ending at
322 SkPoint p3. If SkPathBuilder is empty, or last SkPath::Verb is kClose_Verb, last point is
323 set to (0, 0) before adding cubic.
324
325 Appends kMove_Verb to verb array and (0, 0) to SkPoint array, if needed;
326 then appends kCubic_Verb to verb array; and SkPoint p1, p2, p3
327 to SkPoint array.
328
329 @param p1 first control SkPoint of cubic
330 @param p2 second control SkPoint of cubic
331 @param p3 end SkPoint of cubic
332 @return reference to SkPathBuilder
333 */
334 SkPathBuilder& cubicTo(SkPoint pt1, SkPoint pt2, SkPoint pt3);
335
336 /** Adds cubic from last point towards (x1, y1), then towards (x2, y2), ending at
337 (x3, y3). If SkPathBuilder is empty, or last SkPath::Verb is kClose_Verb, last point is set
338 to (0, 0) before adding cubic.
339
340 Appends kMove_Verb to verb array and (0, 0) to SkPoint array, if needed;
341 then appends kCubic_Verb to verb array; and (x1, y1), (x2, y2), (x3, y3)
342 to SkPoint array.
343
344 @param x1 first control SkPoint of cubic on x-axis
345 @param y1 first control SkPoint of cubic on y-axis
346 @param x2 second control SkPoint of cubic on x-axis
347 @param y2 second control SkPoint of cubic on y-axis
348 @param x3 end SkPoint of cubic on x-axis
349 @param y3 end SkPoint of cubic on y-axis
350 @return reference to SkPathBuilder
351 */
352 SkPathBuilder& cubicTo(SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2, SkScalar x3, SkScalar y3) {
353 return this->cubicTo(SkPoint::Make(x1, y1), SkPoint::Make(x2, y2), SkPoint::Make(x3, y3));
354 }
355
356 /** Adds cubic from last point towards the first SkPoint, then towards the second, ending at
357 the third. If SkPathBuilder is empty, or last SkPath::Verb is kClose_Verb, last point is
358 set to (0, 0) before adding cubic.
359
360 Appends kMove_Verb to verb array and (0, 0) to SkPoint array, if needed;
361 then appends kCubic_Verb to verb array; and SkPoint p1, p2, p3
362 to SkPoint array.
363
364 @param pts first and second control SkPoints of cubic, and end SkPoint.
365 @return reference to SkPathBuilder
366 */
367 SkPathBuilder& cubicTo(const SkPoint pts[3]) {
368 return this->cubicTo(pts[0], pts[1], pts[2]);
369 }
370
371 /** Appends kClose_Verb to SkPathBuilder. A closed contour connects the first and last SkPoint
372 with line, forming a continuous loop. Open and closed contour draw the same
373 with SkPaint::kFill_Style. With SkPaint::kStroke_Style, open contour draws
374 SkPaint::Cap at contour start and end; closed contour draws
375 SkPaint::Join at contour start and end.
376
377 close() has no effect if SkPathBuilder is empty or last SkPath SkPath::Verb is kClose_Verb.
378
379 @return reference to SkPathBuilder
380 */
381 SkPathBuilder& close();
382
383 /** Append a series of lineTo(...)
384
385 @param pts span of SkPoint
386 @return reference to SkPathBuilder.
387 */
388 SkPathBuilder& polylineTo(SkSpan<const SkPoint> pts);
389
390 // Relative versions of segments, relative to the previous position.
391
392 /** Adds beginning of contour relative to last point.
393 If SkPathBuilder is empty, starts contour at (dx, dy).
394 Otherwise, start contour at last point offset by (dx, dy).
395 Function name stands for "relative move to".
396
397 @param pt vector offset from last point to contour start
398 @return reference to SkPathBuilder
399
400 example: https://fiddle.skia.org/c/@Path_rMoveTo
401 */
402 SkPathBuilder& rMoveTo(SkVector pt);
403
404 /** Adds beginning of contour relative to last point.
405 If SkPathBuilder is empty, starts contour at (dx, dy).
406 Otherwise, start contour at last point offset by (dx, dy).
407 Function name stands for "relative move to".
408
409 @param dx offset from last point to contour start on x-axis
410 @param dy offset from last point to contour start on y-axis
411 @return reference to SkPathBuilder
412
413 example: https://fiddle.skia.org/c/@Path_rMoveTo
414 */
415 SkPathBuilder& rMoveTo(SkScalar dx, SkScalar dy) { return this->rMoveTo({dx, dy}); }
416
417 /** Adds line from last point to vector given by pt. If SkPathBuilder is empty, or last
418 SkPath::Verb is kClose_Verb, last point is set to (0, 0) before adding line.
419
420 Appends kMove_Verb to verb array and (0, 0) to SkPoint array, if needed;
421 then appends kLine_Verb to verb array and line end to SkPoint array.
422 Line end is last point plus vector given by pt.
423 Function name stands for "relative line to".
424
425 @param pt vector offset from last point to line end
426 @return reference to SkPathBuilder
427 */
428 SkPathBuilder& rLineTo(SkVector pt);
429
430 /** Adds line from last point to vector (dx, dy). If SkPathBuilder is empty, or last
431 SkPath::Verb is kClose_Verb, last point is set to (0, 0) before adding line.
432
433 Appends kMove_Verb to verb array and (0, 0) to SkPoint array, if needed;
434 then appends kLine_Verb to verb array and line end to SkPoint array.
435 Line end is last point plus vector (dx, dy).
436 Function name stands for "relative line to".
437
438 @param dx offset from last point to line end on x-axis
439 @param dy offset from last point to line end on y-axis
440 @return reference to SkPathBuilder
441 */
442 SkPathBuilder& rLineTo(SkScalar dx, SkScalar dy) { return this->rLineTo({dx, dy}); }
443
444 /** Adds quad from last point towards vector pt1, to vector pt2.
445 If SkPathBuilder is empty, or last SkPath::Verb
446 is kClose_Verb, last point is set to (0, 0) before adding quad.
447
448 Appends kMove_Verb to verb array and (0, 0) to SkPoint array,
449 if needed; then appends kQuad_Verb to verb array; and appends quad
450 control and quad end to SkPoint array.
451 Quad control is last point plus vector pt1.
452 Quad end is last point plus vector pt2.
453 Function name stands for "relative quad to".
454
455 @param pt1 offset vector from last point to quad control
456 @param pt2 offset vector from last point to quad end
457 @return reference to SkPathBuilder
458 */
459 SkPathBuilder& rQuadTo(SkVector pt1, SkVector pt2);
460
461 /** Adds quad from last point towards vector (dx1, dy1), to vector (dx2, dy2).
462 If SkPathBuilder is empty, or last SkPath::Verb
463 is kClose_Verb, last point is set to (0, 0) before adding quad.
464
465 Appends kMove_Verb to verb array and (0, 0) to SkPoint array,
466 if needed; then appends kQuad_Verb to verb array; and appends quad
467 control and quad end to SkPoint array.
468 Quad control is last point plus vector (dx1, dy1).
469 Quad end is last point plus vector (dx2, dy2).
470 Function name stands for "relative quad to".
471
472 @param dx1 offset from last point to quad control on x-axis
473 @param dy1 offset from last point to quad control on y-axis
474 @param dx2 offset from last point to quad end on x-axis
475 @param dy2 offset from last point to quad end on y-axis
476 @return reference to SkPathBuilder
477 */
478 SkPathBuilder& rQuadTo(SkScalar dx1, SkScalar dy1, SkScalar dx2, SkScalar dy2) {
479 return this->rQuadTo({dx1, dy1}, {dx2, dy2});
480 }
481
482 /** Adds conic from last point towards vector p1, to vector p2,
483 weighted by w. If SkPathBuilder is empty, or last SkPath::Verb
484 is kClose_Verb, last point is set to (0, 0) before adding conic.
485
486 Appends kMove_Verb to verb array and (0, 0) to SkPoint array,
487 if needed.
488
489 If w is finite and not one, next appends kConic_Verb to verb array,
490 and w is recorded as conic weight; otherwise, if w is one, appends
491 kQuad_Verb to verb array; or if w is not finite, appends kLine_Verb
492 twice to verb array.
493
494 In all cases appends SkPoint control and end to SkPoint array.
495 control is last point plus vector p1.
496 end is last point plus vector p2.
497
498 Function name stands for "relative conic to".
499
500 @param p1 offset vector from last point to conic control
501 @param p2 offset vector from last point to conic end
502 @param w weight of added conic
503 @return reference to SkPathBuilder
504 */
505 SkPathBuilder& rConicTo(SkVector p1, SkVector p2, SkScalar w);
506
507 /** Adds conic from last point towards vector (dx1, dy1), to vector (dx2, dy2),
508 weighted by w. If SkPathBuilder is empty, or last SkPath::Verb
509 is kClose_Verb, last point is set to (0, 0) before adding conic.
510
511 Appends kMove_Verb to verb array and (0, 0) to SkPoint array,
512 if needed.
513
514 If w is finite and not one, next appends kConic_Verb to verb array,
515 and w is recorded as conic weight; otherwise, if w is one, appends
516 kQuad_Verb to verb array; or if w is not finite, appends kLine_Verb
517 twice to verb array.
518
519 In all cases appends SkPoint control and end to SkPoint array.
520 control is last point plus vector (dx1, dy1).
521 end is last point plus vector (dx2, dy2).
522
523 Function name stands for "relative conic to".
524
525 @param dx1 offset from last point to conic control on x-axis
526 @param dy1 offset from last point to conic control on y-axis
527 @param dx2 offset from last point to conic end on x-axis
528 @param dy2 offset from last point to conic end on y-axis
529 @param w weight of added conic
530 @return reference to SkPathBuilder
531 */
532 SkPathBuilder& rConicTo(SkScalar dx1, SkScalar dy1, SkScalar dx2, SkScalar dy2, SkScalar w) {
533 return this->rConicTo({dx1, dy1}, {dx2, dy2}, w);
534 }
535
536 /** Adds cubic from last point towards vector pt1, then towards
537 vector pt2, to vector pt3.
538 If SkPathBuilder is empty, or last SkPath::Verb
539 is kClose_Verb, last point is set to (0, 0) before adding cubic.
540
541 Appends kMove_Verb to verb array and (0, 0) to SkPoint array,
542 if needed; then appends kCubic_Verb to verb array; and appends cubic
543 control and cubic end to SkPoint array.
544 Cubic control is last point plus vector (dx1, dy1).
545 Cubic end is last point plus vector (dx2, dy2).
546 Function name stands for "relative cubic to".
547
548 @param pt1 offset vector from last point to first cubic control
549 @param pt2 offset vector from last point to second cubic control
550 @param pt3 offset vector from last point to cubic end
551 @return reference to SkPathBuilder
552 */
553 SkPathBuilder& rCubicTo(SkVector pt1, SkVector pt2, SkVector pt3);
554
555 /** Adds cubic from last point towards vector (dx1, dy1), then towards
556 vector (dx2, dy2), to vector (dx3, dy3).
557 If SkPathBuilder is empty, or last SkPath::Verb
558 is kClose_Verb, last point is set to (0, 0) before adding cubic.
559
560 Appends kMove_Verb to verb array and (0, 0) to SkPoint array,
561 if needed; then appends kCubic_Verb to verb array; and appends cubic
562 control and cubic end to SkPoint array.
563 Cubic control is last point plus vector (dx1, dy1).
564 Cubic end is last point plus vector (dx2, dy2).
565 Function name stands for "relative cubic to".
566
567 @param dx1 offset from last point to first cubic control on x-axis
568 @param dy1 offset from last point to first cubic control on y-axis
569 @param dx2 offset from last point to second cubic control on x-axis
570 @param dy2 offset from last point to second cubic control on y-axis
571 @param dx3 offset from last point to cubic end on x-axis
572 @param dy3 offset from last point to cubic end on y-axis
573 @return reference to SkPathBuilder
574 */
575 SkPathBuilder& rCubicTo(SkScalar dx1, SkScalar dy1,
576 SkScalar dx2, SkScalar dy2,
577 SkScalar dx3, SkScalar dy3) {
578 return this->rCubicTo({dx1, dy1}, {dx2, dy2}, {dx3, dy3});
579 }
580
581 // Arcs
582
583 enum ArcSize {
584 kSmall_ArcSize, //!< smaller of arc pair
585 kLarge_ArcSize, //!< larger of arc pair
586 };
587
588 /** Appends arc to SkPathBuilder, relative to last SkPath SkPoint. Arc is implemented by one or
589 more conic, weighted to describe part of oval with radii (rx, ry) rotated by
590 xAxisRotate degrees. Arc curves from last SkPathBuilder SkPoint to relative end SkPoint:
591 (dx, dy), choosing one of four possible routes: clockwise or
592 counterclockwise, and smaller or larger. If SkPathBuilder is empty, the start arc SkPoint
593 is (0, 0).
594
595 Arc sweep is always less than 360 degrees. arcTo() appends line to end SkPoint
596 if either radii are zero, or if last SkPath SkPoint equals end SkPoint.
597 arcTo() scales radii (rx, ry) to fit last SkPath SkPoint and end SkPoint if both are
598 greater than zero but too small to describe an arc.
599
600 arcTo() appends up to four conic curves.
601 arcTo() implements the functionality of svg arc, although SVG "sweep-flag" value is
602 opposite the integer value of sweep; SVG "sweep-flag" uses 1 for clockwise, while
603 kCW_Direction cast to int is zero.
604
605 @param r radii on axes before x-axis rotation
606 @param xAxisRotate x-axis rotation in degrees; positive values are clockwise
607 @param largeArc chooses smaller or larger arc
608 @param sweep chooses clockwise or counterclockwise arc
609 @param dxdy offset end of arc from last SkPath point
610 @return reference to SkPath
611 */
612 SkPathBuilder& rArcTo(SkPoint r, SkScalar xAxisRotate, ArcSize largeArc,
613 SkPathDirection sweep, SkVector dxdy);
614
615 /** Appends arc to the builder. Arc added is part of ellipse
616 bounded by oval, from startAngle through sweepAngle. Both startAngle and
617 sweepAngle are measured in degrees, where zero degrees is aligned with the
618 positive x-axis, and positive sweeps extends arc clockwise.
619
620 arcTo() adds line connecting the builder's last point to initial arc point if forceMoveTo
621 is false and the builder is not empty. Otherwise, added contour begins with first point
622 of arc. Angles greater than -360 and less than 360 are treated modulo 360.
623
624 @param oval bounds of ellipse containing arc
625 @param startAngleDeg starting angle of arc in degrees
626 @param sweepAngleDeg sweep, in degrees. Positive is clockwise; treated modulo 360
627 @param forceMoveTo true to start a new contour with arc
628 @return reference to the builder
629 */
630 SkPathBuilder& arcTo(const SkRect& oval, SkScalar startAngleDeg, SkScalar sweepAngleDeg,
631 bool forceMoveTo);
632
633 /** Appends arc to SkPath, after appending line if needed. Arc is implemented by conic
634 weighted to describe part of circle. Arc is contained by tangent from
635 last SkPath point to p1, and tangent from p1 to p2. Arc
636 is part of circle sized to radius, positioned so it touches both tangent lines.
637
638 If last SkPath SkPoint does not start arc, arcTo() appends connecting line to SkPath.
639 The length of vector from p1 to p2 does not affect arc.
640
641 Arc sweep is always less than 180 degrees. If radius is zero, or if
642 tangents are nearly parallel, arcTo() appends line from last SkPath SkPoint to p1.
643
644 arcTo() appends at most one line and one conic.
645 arcTo() implements the functionality of PostScript arct and HTML Canvas arcTo.
646
647 @param p1 SkPoint common to pair of tangents
648 @param p2 end of second tangent
649 @param radius distance from arc to circle center
650 @return reference to SkPath
651 */
652 SkPathBuilder& arcTo(SkPoint p1, SkPoint p2, SkScalar radius);
653
654 /** Appends arc to SkPath. Arc is implemented by one or more conic weighted to describe
655 part of oval with radii (r.fX, r.fY) rotated by xAxisRotate degrees. Arc curves
656 from last SkPath SkPoint to (xy.fX, xy.fY), choosing one of four possible routes:
657 clockwise or counterclockwise,
658 and smaller or larger.
659
660 Arc sweep is always less than 360 degrees. arcTo() appends line to xy if either
661 radii are zero, or if last SkPath SkPoint equals (xy.fX, xy.fY). arcTo() scales radii r to
662 fit last SkPath SkPoint and xy if both are greater than zero but too small to describe
663 an arc.
664
665 arcTo() appends up to four conic curves.
666 arcTo() implements the functionality of SVG arc, although SVG sweep-flag value is
667 opposite the integer value of sweep; SVG sweep-flag uses 1 for clockwise, while
668 kCW_Direction cast to int is zero.
669
670 @param r radii on axes before x-axis rotation
671 @param xAxisRotate x-axis rotation in degrees; positive values are clockwise
672 @param largeArc chooses smaller or larger arc
673 @param sweep chooses clockwise or counterclockwise arc
674 @param xy end of arc
675 @return reference to SkPath
676 */
677 SkPathBuilder& arcTo(SkPoint r, SkScalar xAxisRotate, ArcSize largeArc, SkPathDirection sweep,
678 SkPoint xy);
679
680 /** Appends arc to the builder, as the start of new contour. Arc added is part of ellipse
681 bounded by oval, from startAngle through sweepAngle. Both startAngle and
682 sweepAngle are measured in degrees, where zero degrees is aligned with the
683 positive x-axis, and positive sweeps extends arc clockwise.
684
685 If sweepAngle <= -360, or sweepAngle >= 360; and startAngle modulo 90 is nearly
686 zero, append oval instead of arc. Otherwise, sweepAngle values are treated
687 modulo 360, and arc may or may not draw depending on numeric rounding.
688
689 @param oval bounds of ellipse containing arc
690 @param startAngleDeg starting angle of arc in degrees
691 @param sweepAngleDeg sweep, in degrees. Positive is clockwise; treated modulo 360
692 @return reference to this builder
693 */
694 SkPathBuilder& addArc(const SkRect& oval, SkScalar startAngleDeg, SkScalar sweepAngleDeg);
695
696 SkPathBuilder& addLine(SkPoint a, SkPoint b) {
697 return this->moveTo(a).lineTo(b);
698 }
699
700 /** Adds a new contour to the SkPathBuilder, defined by the rect, and wound in the
701 specified direction. The verbs added to the path will be:
702
703 kMove, kLine, kLine, kLine, kClose
704
705 start specifies which corner to begin the contour:
706 0: upper-left corner
707 1: upper-right corner
708 2: lower-right corner
709 3: lower-left corner
710
711 This start point also acts as the implied beginning of the subsequent,
712 contour, if it does not have an explicit moveTo(). e.g.
713
714 path.addRect(...)
715 // if we don't say moveTo() here, we will use the rect's start point
716 path.lineTo(...)
717
718 @param rect SkRect to add as a closed contour
719 @param dir SkPath::Direction to orient the new contour
720 @param start initial corner of SkRect to add
721 @return reference to SkPathBuilder
722 */
723 SkPathBuilder& addRect(const SkRect&, SkPathDirection, unsigned startIndex);
724
725 /** Adds a new contour to the SkPathBuilder, defined by the rect, and wound in the
726 specified direction. The verbs added to the path will be:
727
728 kMove, kLine, kLine, kLine, kClose
729
730 The contour starts at the upper-left corner of the rect, which also acts as the implied
731 beginning of the subsequent contour, if it does not have an explicit moveTo(). e.g.
732
733 path.addRect(...)
734 // if we don't say moveTo() here, we will use the rect's upper-left corner
735 path.lineTo(...)
736
737 @param rect SkRect to add as a closed contour
738 @param dir SkPath::Direction to orient the new contour
739 @return reference to SkPathBuilder
740 */
741 SkPathBuilder& addRect(const SkRect& rect, SkPathDirection dir = SkPathDirection::kDefault) {
742 return this->addRect(rect, dir, 0);
743 }
744
745 /** Adds oval to SkPathBuilder, appending kMove_Verb, four kConic_Verb, and kClose_Verb.
746 Oval is upright ellipse bounded by SkRect oval with radii equal to half oval width
747 and half oval height. Oval begins at (oval.fRight, oval.centerY()) and continues
748 clockwise if dir is kCW_Direction, counterclockwise if dir is kCCW_Direction.
749
750 @param oval bounds of ellipse added
751 @param dir SkPath::Direction to wind ellipse
752 @return reference to SkPathBuilder
753 */
754 SkPathBuilder& addOval(const SkRect&, SkPathDirection, unsigned startIndex);
755
756 /** Appends SkRRect to SkPathBuilder, creating a new closed contour. If dir is kCW_Direction,
757 SkRRect winds clockwise. If dir is kCCW_Direction, SkRRect winds counterclockwise.
758
759 After appending, SkPathBuilder may be empty, or may contain: SkRect, oval, or SkRRect.
760
761 @param rrect SkRRect to add
762 @param dir SkPath::Direction to wind SkRRect
763 @param start index of initial point of SkRRect
764 @return reference to SkPathBuilder
765 */
766 SkPathBuilder& addRRect(const SkRRect& rrect, SkPathDirection, unsigned start);
767
768 /** Appends SkRRect to SkPathBuilder, creating a new closed contour. If dir is kCW_Direction,
769 SkRRect starts at top-left of the lower-left corner and winds clockwise. If dir is
770 kCCW_Direction, SkRRect starts at the bottom-left of the upper-left corner and winds
771 counterclockwise.
772
773 After appending, SkPathBuilder may be empty, or may contain: SkRect, oval, or SkRRect.
774
775 @param rrect SkRRect to add
776 @param dir SkPath::Direction to wind SkRRect
777 @return reference to SkPathBuilder
778 */
779 SkPathBuilder& addRRect(const SkRRect& rrect, SkPathDirection dir = SkPathDirection::kDefault) {
780 // legacy start indices: 6 (CW) and 7 (CCW)
781 return this->addRRect(rrect, dir, dir == SkPathDirection::kCW ? 6 : 7);
782 }
783
784 /** Adds oval to SkPathBuilder, appending kMove_Verb, four kConic_Verb, and kClose_Verb.
785 Oval is upright ellipse bounded by SkRect oval with radii equal to half oval width
786 and half oval height. Oval begins at start and continues
787 clockwise if dir is kCW_Direction, counterclockwise if dir is kCCW_Direction.
788
789 @param oval bounds of ellipse added
790 @param dir SkPath::Direction to wind ellipse
791 @return reference to SkPath
792
793 example: https://fiddle.skia.org/c/@Path_addOval_2
794 */
795 SkPathBuilder& addOval(const SkRect& oval, SkPathDirection dir = SkPathDirection::kDefault) {
796 // legacy start index: 1
797 return this->addOval(oval, dir, 1);
798 }
799
800 /** Adds circle with center and radius to SkPathBuilder, appending kMove_Verb,
801 four kConic_Verb, and kClose_Verb. Circle begins at: (center.fX + radius, center.fY).
802
803 Has no effect if radius is zero or negative.
804
805 @param center center of circle
806 @param radius distance from center to edge
807 @param dir SkPath::Direction to wind circle
808 @return reference to SkPathBuilder
809 */
810 SkPathBuilder& addCircle(SkPoint center, float radius,
811 SkPathDirection dir = SkPathDirection::kDefault);
812 SkPathBuilder& addCircle(float x, float y, float radius,
813 SkPathDirection dir = SkPathDirection::kDefault) {
814 return this->addCircle({x, y}, radius, dir);
815 }
816
817 /** Adds contour created from line array, adding (pts.size() - 1) line segments.
818 Contour added starts at pts[0], then adds a line for every additional SkPoint
819 in pts array. If close is true, appends kClose_Verb to SkPath, connecting
820 pts[count - 1] and pts[0].
821
822 @param pts array of line sharing end and start SkPoint
823 @param close true to add line connecting contour end and start
824 @return reference to SkPath
825 */
826 SkPathBuilder& addPolygon(SkSpan<const SkPoint> pts, bool close);
827
828 /** Appends src to SkPathBuilder, offset by (dx, dy).
829
830 If mode is kAppend_AddPathMode, src verb array, SkPoint array, and conic weights are
831 added unaltered. If mode is kExtend_AddPathMode, add line before appending
832 verbs, SkPoint, and conic weights.
833
834 @param src SkPath verbs, SkPoint, and conic weights to add
835 @param dx offset added to src SkPoint array x-axis coordinates
836 @param dy offset added to src SkPoint array y-axis coordinates
837 @param mode kAppend_AddPathMode or kExtend_AddPathMode
838 @return reference to SkPathBuilder
839 */
840 SkPathBuilder& addPath(const SkPath& src, SkScalar dx, SkScalar dy,
841 SkPath::AddPathMode mode = SkPath::kAppend_AddPathMode);
842
843 /** Appends src to SkPathBuilder.
844
845 If mode is kAppend_AddPathMode, src verb array, SkPoint array, and conic weights are
846 added unaltered. If mode is kExtend_AddPathMode, add line before appending
847 verbs, SkPoint, and conic weights.
848
849 @param src SkPath verbs, SkPoint, and conic weights to add
850 @param mode kAppend_AddPathMode or kExtend_AddPathMode
851 @return reference to SkPathBuilder
852 */
853 SkPathBuilder& addPath(const SkPath& src,
854 SkPath::AddPathMode mode = SkPath::kAppend_AddPathMode) {
855 return this->addPath(src, SkMatrix::I(), mode);
856 }
857
858 /** Appends src to SkPathBuilder, transformed by matrix. Transformed curves may have different
859 verbs, SkPoint, and conic weights.
860
861 If mode is kAppend_AddPathMode, src verb array, SkPoint array, and conic weights are
862 added unaltered. If mode is kExtend_AddPathMode, add line before appending
863 verbs, SkPoint, and conic weights.
864
865 @param src SkPath verbs, SkPoint, and conic weights to add
866 @param matrix transform applied to src
867 @param mode kAppend_AddPathMode or kExtend_AddPathMode
868 @return reference to SkPathBuilder
869 */
870 SkPathBuilder& addPath(const SkPath& src, const SkMatrix& matrix,
871 SkPath::AddPathMode mode = SkPath::AddPathMode::kAppend_AddPathMode);
872
873 // Performance hint, to reserve extra storage for subsequent calls to lineTo, quadTo, etc.
874
875 /** Grows SkPathBuilder verb array and SkPoint array to contain additional space.
876 May improve performance and use less memory by
877 reducing the number and size of allocations when creating SkPathBuilder.
878
879 @param extraPtCount number of additional SkPoint to allocate
880 @param extraVerbCount number of additional verbs
881 @param extraConicCount number of additional conic weights
882 */
883 void incReserve(int extraPtCount, int extraVerbCount, int extraConicCount);
884
885 /** Grows SkPathBuilder verb array and SkPoint array to contain additional space.
886 May improve performance and use less memory by
887 reducing the number and size of allocations when creating SkPathBuilder.
888
889 @param extraPtCount number of additional SkPoints and verbs to allocate
890 */
891 void incReserve(int extraPtCount) {
892 this->incReserve(extraPtCount, extraPtCount, 0);
893 }
894
895 /** Offsets SkPoint array by (dx, dy).
896
897 @param dx offset added to SkPoint array x-axis coordinates
898 @param dy offset added to SkPoint array y-axis coordinates
899 */
900 SkPathBuilder& offset(SkScalar dx, SkScalar dy);
901
902 /** Transforms verb array, SkPoint array, and weight by matrix.
903 transform may change verbs and increase their number.
904
905 @param matrix SkMatrix to apply to SkPath
906 @param pc whether to apply perspective clipping
907 */
908 SkPathBuilder& transform(const SkMatrix& matrix);
909
910 /*
911 * Returns true if the builder is empty, or all of its points are finite.
912 */
913 bool isFinite() const;
914
915 /** Replaces SkPathFillType with its inverse. The inverse of SkPathFillType describes the area
916 unmodified by the original SkPathFillType.
917 */
918 SkPathBuilder& toggleInverseFillType() {
919 fFillType = SkPathFillType_ToggleInverse(fFillType);
920 return *this;
921 }
922
923 /** Returns if SkPath is empty.
924 Empty SkPathBuilder may have FillType but has no SkPoint, SkPath::Verb, or conic weight.
925 SkPathBuilder() constructs empty SkPathBuilder; reset() and rewind() make SkPath empty.
926
927 @return true if the path contains no SkPath::Verb array
928 */
929 bool isEmpty() const { return fVerbs.empty(); }
930
931 /** Returns last point on SkPathBuilder. Returns nullopt if SkPoint array is empty.
932
933 @return last SkPoint if SkPoint array contains one or more SkPoint, otherwise nullopt
934
935 example: https://fiddle.skia.org/c/@Path_getLastPt
936 */
937 std::optional<SkPoint> getLastPt() const;
938
939 /** Change the point at the specified index (see countPoints()).
940 * If index is out of range, the call does nothing.
941 *
942 * @param index which point to replace
943 * @param p the new point value
944 */
945 void setPoint(size_t index, SkPoint p);
946
947 /** Change the last point in the builder.
948 * If the builder is empty, the call does nothing.
949 *
950 * @param p the new point value
951 */
952 void setLastPoint(SkPoint p) {
953 this->setPoint(this->points().size() - 1, p);
954 }
955
956 #ifdef SK_SUPPORT_LEGACY_PATHBUILDER_SETLASTPT
957 /** DEPRECATED: use setLastPoint() or setPoint()
958 Sets the last point on the path. If SkPoint array is empty, this behaves like moveTo().
959
960 @param pt the new value for the last point in the path
961 */
962 void setLastPt(SkPoint pt);
963 void setLastPt(float x, float y) { this->setLastPt({x, y}); }
964 #endif
965
966 /** Returns the number of points in SkPathBuilder.
967 SkPoint count is initially zero.
968
969 @return SkPathBuilder SkPoint array length
970 */
971 int countPoints() const { return fPts.size(); }
972
973 /** Returns if SkPathFillType describes area outside SkPath geometry. The inverse fill area
974 extends indefinitely.
975
976 @return true if FillType is kInverseWinding or kInverseEvenOdd
977 */
978 bool isInverseFillType() const { return SkPathFillType_IsInverse(fFillType); }
979
980 #ifdef SK_SUPPORT_UNSPANNED_APIS
981 SkPathBuilder& addPolygon(const SkPoint pts[], int count, bool close) {
982 return this->addPolygon({pts, count}, close);
983 }
984 SkPathBuilder& polylineTo(const SkPoint pts[], int count) {
985 return this->polylineTo({pts, count});
986 }
987 #endif
988
989 SkSpan<const SkPoint> points() const {
990 return fPts;
991 }
992 SkSpan<const SkPathVerb> verbs() const {
993 return fVerbs;
994 }
995 SkSpan<const float> conicWeights() const {
996 return fConicWeights;
997 }
998
999 SkPathBuilder& addRaw(const SkPathRaw&);
1000
1001 SkPathIter iter() const;
1002
1003 enum class DumpFormat {
1004 kDecimal,
1005 kHex,
1006 };
1007 SkString dumpToString(DumpFormat = DumpFormat::kDecimal) const;
1008 void dump(DumpFormat) const;
1009 // can't use default argument easily in debugger, so we name this
1010 // helper explicitly.
1011 void dump() const { this->dump(DumpFormat::kDecimal); }
1012
1013 bool contains(SkPoint) const;
1014
1015 private:
1016 PointsArray fPts;
1017 VerbsArray fVerbs;
1018 ConicWeightsArray fConicWeights;
1019
1020 SkPathFillType fFillType;
1021 bool fIsVolatile;
1022 SkPathConvexity fConvexity;
1023
1024 unsigned fSegmentMask;
1025 int fLastMoveIndex; // only needed until SkPath is immutable
1026
1027 SkPathIsAType fType = SkPathIsAType::kGeneral;
1028 SkPathIsAData fIsA {};
1029
1030 // called right before we add a (non-move) verb
1031 void ensureMove() {
1032 fType = SkPathIsAType::kGeneral;
1033 if (fVerbs.empty()) {
1034 this->moveTo({0, 0});
1035 } else if (fVerbs.back() == SkPathVerb::kClose) {
1036 this->moveTo(fPts[fLastMoveIndex]);
1037 }
1038 }
1039
1040 bool isZeroLengthSincePoint(int startPtIndex) const;
1041
1042 SkPathBuilder& privateReverseAddPath(const SkPath&);
1043 SkPathBuilder& privateReversePathTo(const SkPath&);
1044
1045 std::tuple<SkPoint*, SkScalar*> growForVerbsInPath(const SkPath& path);
1046
1047 friend class SkPathPriv;
1048 friend class SkStroke;
1049 friend class SkPathStroker;
1050 };
1051
1052 #endif
1053