Debian Codegraph @

include/core/SkPathBuilder.h

raw ◀ ▶
1 Debian patch touch this file
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, positive, 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 zero, this is the same as lineTo(pt2)
262
263 If w is not finite, appends kLine_Verb twice to verb array, and
264 pt1, pt2 to SkPoint array.
265
266 @param pt1 control SkPoint of conic
267 @param pt2 end SkPoint of conic
268 @param w weight of added conic
269 @return reference to SkPathBuilder
270 */
271 SkPathBuilder& conicTo(SkPoint pt1, SkPoint pt2, SkScalar w);
272
273 /** Adds conic from last point towards (x1, y1), to (x2, y2), weighted by w.
274 If SkPathBuilder is empty, or last SkPath::Verb is kClose_Verb, last point is set to (0, 0)
275 before adding conic.
276
277 Appends kMove_Verb to verb array and (0, 0) to SkPoint array, if needed.
278
279 If w is finite and not one, appends kConic_Verb to verb array;
280 and (x1, y1), (x2, y2) to SkPoint array; and w to conic weights.
281
282 If w is one, appends kQuad_Verb to verb array, and
283 (x1, y1), (x2, y2) to SkPoint array.
284
285 If w is not finite, appends kLine_Verb twice to verb array, and
286 (x1, y1), (x2, y2) to SkPoint array.
287
288 @param x1 control SkPoint of conic on x-axis
289 @param y1 control SkPoint of conic on y-axis
290 @param x2 end SkPoint of conic on x-axis
291 @param y2 end SkPoint of conic on y-axis
292 @param w weight of added conic
293 @return reference to SkPathBuilder
294 */
295 SkPathBuilder& conicTo(SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2, SkScalar w) {
296 return this->conicTo(SkPoint::Make(x1, y1), SkPoint::Make(x2, y2), w);
297 }
298
299 /** Adds conic from last point towards SkPoint p1, to SkPoint p2, weighted by w.
300 If SkPathBuilder is empty, or last SkPath::Verb is kClose_Verb, last point is set to (0, 0)
301 before adding conic.
302
303 Appends kMove_Verb to verb array and (0, 0) to SkPoint array, if needed.
304
305 If w is finite and not one, appends kConic_Verb to verb array;
306 and SkPoint p1, p2 to SkPoint array; and w to conic weights.
307
308 If w is one, appends kQuad_Verb to verb array, and SkPoint p1, p2
309 to SkPoint array.
310
311 If w is not finite, appends kLine_Verb twice to verb array, and
312 SkPoint p1, p2 to SkPoint array.
313
314 @param p1 control SkPoint of added conic
315 @param p2 end SkPoint of added conic
316 @param w weight of added conic
317 @return reference to SkPathBuilder
318 */
319 SkPathBuilder& conicTo(const SkPoint pts[2], SkScalar w) {
320 return this->conicTo(pts[0], pts[1], w);
321 }
322
323 /** Adds cubic from last point towards SkPoint p1, then towards SkPoint p2, ending at
324 SkPoint p3. If SkPathBuilder is empty, or last SkPath::Verb is kClose_Verb, last point is
325 set to (0, 0) before adding cubic.
326
327 Appends kMove_Verb to verb array and (0, 0) to SkPoint array, if needed;
328 then appends kCubic_Verb to verb array; and SkPoint p1, p2, p3
329 to SkPoint array.
330
331 @param p1 first control SkPoint of cubic
332 @param p2 second control SkPoint of cubic
333 @param p3 end SkPoint of cubic
334 @return reference to SkPathBuilder
335 */
336 SkPathBuilder& cubicTo(SkPoint pt1, SkPoint pt2, SkPoint pt3);
337
338 /** Adds cubic from last point towards (x1, y1), then towards (x2, y2), ending at
339 (x3, y3). If SkPathBuilder is empty, or last SkPath::Verb is kClose_Verb, last point is set
340 to (0, 0) before adding cubic.
341
342 Appends kMove_Verb to verb array and (0, 0) to SkPoint array, if needed;
343 then appends kCubic_Verb to verb array; and (x1, y1), (x2, y2), (x3, y3)
344 to SkPoint array.
345
346 @param x1 first control SkPoint of cubic on x-axis
347 @param y1 first control SkPoint of cubic on y-axis
348 @param x2 second control SkPoint of cubic on x-axis
349 @param y2 second control SkPoint of cubic on y-axis
350 @param x3 end SkPoint of cubic on x-axis
351 @param y3 end SkPoint of cubic on y-axis
352 @return reference to SkPathBuilder
353 */
354 SkPathBuilder& cubicTo(SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2, SkScalar x3, SkScalar y3) {
355 return this->cubicTo(SkPoint::Make(x1, y1), SkPoint::Make(x2, y2), SkPoint::Make(x3, y3));
356 }
357
358 /** Adds cubic from last point towards the first SkPoint, then towards the second, ending at
359 the third. If SkPathBuilder is empty, or last SkPath::Verb is kClose_Verb, last point is
360 set to (0, 0) before adding cubic.
361
362 Appends kMove_Verb to verb array and (0, 0) to SkPoint array, if needed;
363 then appends kCubic_Verb to verb array; and SkPoint p1, p2, p3
364 to SkPoint array.
365
366 @param pts first and second control SkPoints of cubic, and end SkPoint.
367 @return reference to SkPathBuilder
368 */
369 SkPathBuilder& cubicTo(const SkPoint pts[3]) {
370 return this->cubicTo(pts[0], pts[1], pts[2]);
371 }
372
373 /** Appends kClose_Verb to SkPathBuilder. A closed contour connects the first and last SkPoint
374 with line, forming a continuous loop. Open and closed contour draw the same
375 with SkPaint::kFill_Style. With SkPaint::kStroke_Style, open contour draws
376 SkPaint::Cap at contour start and end; closed contour draws
377 SkPaint::Join at contour start and end.
378
379 close() has no effect if SkPathBuilder is empty or last SkPath SkPath::Verb is kClose_Verb.
380
381 @return reference to SkPathBuilder
382 */
383 SkPathBuilder& close();
384
385 /** Append a series of lineTo(...)
386
387 @param pts span of SkPoint
388 @return reference to SkPathBuilder.
389 */
390 SkPathBuilder& polylineTo(SkSpan<const SkPoint> pts);
391
392 // Relative versions of segments, relative to the previous position.
393
394 /** Adds beginning of contour relative to last point.
395 If SkPathBuilder is empty, starts contour at (dx, dy).
396 Otherwise, start contour at last point offset by (dx, dy).
397 Function name stands for "relative move to".
398
399 @param pt vector offset from last point to contour start
400 @return reference to SkPathBuilder
401
402 example: https://fiddle.skia.org/c/@Path_rMoveTo
403 */
404 SkPathBuilder& rMoveTo(SkVector pt);
405
406 /** Adds beginning of contour relative to last point.
407 If SkPathBuilder is empty, starts contour at (dx, dy).
408 Otherwise, start contour at last point offset by (dx, dy).
409 Function name stands for "relative move to".
410
411 @param dx offset from last point to contour start on x-axis
412 @param dy offset from last point to contour start on y-axis
413 @return reference to SkPathBuilder
414
415 example: https://fiddle.skia.org/c/@Path_rMoveTo
416 */
417 SkPathBuilder& rMoveTo(SkScalar dx, SkScalar dy) { return this->rMoveTo({dx, dy}); }
418
419 /** Adds line from last point to vector given by pt. If SkPathBuilder is empty, or last
420 SkPath::Verb is kClose_Verb, last point is set to (0, 0) before adding line.
421
422 Appends kMove_Verb to verb array and (0, 0) to SkPoint array, if needed;
423 then appends kLine_Verb to verb array and line end to SkPoint array.
424 Line end is last point plus vector given by pt.
425 Function name stands for "relative line to".
426
427 @param pt vector offset from last point to line end
428 @return reference to SkPathBuilder
429 */
430 SkPathBuilder& rLineTo(SkVector pt);
431
432 /** Adds line from last point to vector (dx, dy). If SkPathBuilder is empty, or last
433 SkPath::Verb is kClose_Verb, last point is set to (0, 0) before adding line.
434
435 Appends kMove_Verb to verb array and (0, 0) to SkPoint array, if needed;
436 then appends kLine_Verb to verb array and line end to SkPoint array.
437 Line end is last point plus vector (dx, dy).
438 Function name stands for "relative line to".
439
440 @param dx offset from last point to line end on x-axis
441 @param dy offset from last point to line end on y-axis
442 @return reference to SkPathBuilder
443 */
444 SkPathBuilder& rLineTo(SkScalar dx, SkScalar dy) { return this->rLineTo({dx, dy}); }
445
446 /** Adds quad from last point towards vector pt1, to vector pt2.
447 If SkPathBuilder is empty, or last SkPath::Verb
448 is kClose_Verb, last point is set to (0, 0) before adding quad.
449
450 Appends kMove_Verb to verb array and (0, 0) to SkPoint array,
451 if needed; then appends kQuad_Verb to verb array; and appends quad
452 control and quad end to SkPoint array.
453 Quad control is last point plus vector pt1.
454 Quad end is last point plus vector pt2.
455 Function name stands for "relative quad to".
456
457 @param pt1 offset vector from last point to quad control
458 @param pt2 offset vector from last point to quad end
459 @return reference to SkPathBuilder
460 */
461 SkPathBuilder& rQuadTo(SkVector pt1, SkVector pt2);
462
463 /** Adds quad from last point towards vector (dx1, dy1), to vector (dx2, dy2).
464 If SkPathBuilder is empty, or last SkPath::Verb
465 is kClose_Verb, last point is set to (0, 0) before adding quad.
466
467 Appends kMove_Verb to verb array and (0, 0) to SkPoint array,
468 if needed; then appends kQuad_Verb to verb array; and appends quad
469 control and quad end to SkPoint array.
470 Quad control is last point plus vector (dx1, dy1).
471 Quad end is last point plus vector (dx2, dy2).
472 Function name stands for "relative quad to".
473
474 @param dx1 offset from last point to quad control on x-axis
475 @param dy1 offset from last point to quad control on y-axis
476 @param dx2 offset from last point to quad end on x-axis
477 @param dy2 offset from last point to quad end on y-axis
478 @return reference to SkPathBuilder
479 */
480 SkPathBuilder& rQuadTo(SkScalar dx1, SkScalar dy1, SkScalar dx2, SkScalar dy2) {
481 return this->rQuadTo({dx1, dy1}, {dx2, dy2});
482 }
483
484 /** Adds conic from last point towards vector p1, to vector p2,
485 weighted by w. If SkPathBuilder is empty, or last SkPath::Verb
486 is kClose_Verb, last point is set to (0, 0) before adding conic.
487
488 Appends kMove_Verb to verb array and (0, 0) to SkPoint array,
489 if needed.
490
491 If w is finite and not one, next appends kConic_Verb to verb array,
492 and w is recorded as conic weight; otherwise, if w is one, appends
493 kQuad_Verb to verb array; or if w is not finite, appends kLine_Verb
494 twice to verb array.
495
496 In all cases appends SkPoint control and end to SkPoint array.
497 control is last point plus vector p1.
498 end is last point plus vector p2.
499
500 Function name stands for "relative conic to".
501
502 @param p1 offset vector from last point to conic control
503 @param p2 offset vector from last point to conic end
504 @param w weight of added conic
505 @return reference to SkPathBuilder
506 */
507 SkPathBuilder& rConicTo(SkVector p1, SkVector p2, SkScalar w);
508
509 /** Adds conic from last point towards vector (dx1, dy1), to vector (dx2, dy2),
510 weighted by w. If SkPathBuilder is empty, or last SkPath::Verb
511 is kClose_Verb, last point is set to (0, 0) before adding conic.
512
513 Appends kMove_Verb to verb array and (0, 0) to SkPoint array,
514 if needed.
515
516 If w is finite and not one, next appends kConic_Verb to verb array,
517 and w is recorded as conic weight; otherwise, if w is one, appends
518 kQuad_Verb to verb array; or if w is not finite, appends kLine_Verb
519 twice to verb array.
520
521 In all cases appends SkPoint control and end to SkPoint array.
522 control is last point plus vector (dx1, dy1).
523 end is last point plus vector (dx2, dy2).
524
525 Function name stands for "relative conic to".
526
527 @param dx1 offset from last point to conic control on x-axis
528 @param dy1 offset from last point to conic control on y-axis
529 @param dx2 offset from last point to conic end on x-axis
530 @param dy2 offset from last point to conic end on y-axis
531 @param w weight of added conic
532 @return reference to SkPathBuilder
533 */
534 SkPathBuilder& rConicTo(SkScalar dx1, SkScalar dy1, SkScalar dx2, SkScalar dy2, SkScalar w) {
535 return this->rConicTo({dx1, dy1}, {dx2, dy2}, w);
536 }
537
538 /** Adds cubic from last point towards vector pt1, then towards
539 vector pt2, to vector pt3.
540 If SkPathBuilder is empty, or last SkPath::Verb
541 is kClose_Verb, last point is set to (0, 0) before adding cubic.
542
543 Appends kMove_Verb to verb array and (0, 0) to SkPoint array,
544 if needed; then appends kCubic_Verb to verb array; and appends cubic
545 control and cubic end to SkPoint array.
546 Cubic control is last point plus vector (dx1, dy1).
547 Cubic end is last point plus vector (dx2, dy2).
548 Function name stands for "relative cubic to".
549
550 @param pt1 offset vector from last point to first cubic control
551 @param pt2 offset vector from last point to second cubic control
552 @param pt3 offset vector from last point to cubic end
553 @return reference to SkPathBuilder
554 */
555 SkPathBuilder& rCubicTo(SkVector pt1, SkVector pt2, SkVector pt3);
556
557 /** Adds cubic from last point towards vector (dx1, dy1), then towards
558 vector (dx2, dy2), to vector (dx3, dy3).
559 If SkPathBuilder is empty, or last SkPath::Verb
560 is kClose_Verb, last point is set to (0, 0) before adding cubic.
561
562 Appends kMove_Verb to verb array and (0, 0) to SkPoint array,
563 if needed; then appends kCubic_Verb to verb array; and appends cubic
564 control and cubic end to SkPoint array.
565 Cubic control is last point plus vector (dx1, dy1).
566 Cubic end is last point plus vector (dx2, dy2).
567 Function name stands for "relative cubic to".
568
569 @param dx1 offset from last point to first cubic control on x-axis
570 @param dy1 offset from last point to first cubic control on y-axis
571 @param dx2 offset from last point to second cubic control on x-axis
572 @param dy2 offset from last point to second cubic control on y-axis
573 @param dx3 offset from last point to cubic end on x-axis
574 @param dy3 offset from last point to cubic end on y-axis
575 @return reference to SkPathBuilder
576 */
577 SkPathBuilder& rCubicTo(SkScalar dx1, SkScalar dy1,
578 SkScalar dx2, SkScalar dy2,
579 SkScalar dx3, SkScalar dy3) {
580 return this->rCubicTo({dx1, dy1}, {dx2, dy2}, {dx3, dy3});
581 }
582
583 // Arcs
584
585 enum ArcSize {
586 kSmall_ArcSize, //!< smaller of arc pair
587 kLarge_ArcSize, //!< larger of arc pair
588 };
589
590 /** Appends arc to SkPathBuilder, relative to last SkPath SkPoint. Arc is implemented by one or
591 more conic, weighted to describe part of oval with radii (rx, ry) rotated by
592 xAxisRotate degrees. Arc curves from last SkPathBuilder SkPoint to relative end SkPoint:
593 (dx, dy), choosing one of four possible routes: clockwise or
594 counterclockwise, and smaller or larger. If SkPathBuilder is empty, the start arc SkPoint
595 is (0, 0).
596
597 Arc sweep is always less than 360 degrees. arcTo() appends line to end SkPoint
598 if either radii are zero, or if last SkPath SkPoint equals end SkPoint.
599 arcTo() scales radii (rx, ry) to fit last SkPath SkPoint and end SkPoint if both are
600 greater than zero but too small to describe an arc.
601
602 arcTo() appends up to four conic curves.
603 arcTo() implements the functionality of svg arc, although SVG "sweep-flag" value is
604 opposite the integer value of sweep; SVG "sweep-flag" uses 1 for clockwise, while
605 kCW_Direction cast to int is zero.
606
607 @param r radii on axes before x-axis rotation
608 @param xAxisRotate x-axis rotation in degrees; positive values are clockwise
609 @param largeArc chooses smaller or larger arc
610 @param sweep chooses clockwise or counterclockwise arc
611 @param dxdy offset end of arc from last SkPath point
612 @return reference to SkPath
613 */
614 SkPathBuilder& rArcTo(SkPoint r, SkScalar xAxisRotate, ArcSize largeArc,
615 SkPathDirection sweep, SkVector dxdy);
616
617 /** Appends arc to the builder. Arc added is part of ellipse
618 bounded by oval, from startAngle through sweepAngle. Both startAngle and
619 sweepAngle are measured in degrees, where zero degrees is aligned with the
620 positive x-axis, and positive sweeps extends arc clockwise.
621
622 arcTo() adds line connecting the builder's last point to initial arc point if forceMoveTo
623 is false and the builder is not empty. Otherwise, added contour begins with first point
624 of arc. Angles greater than -360 and less than 360 are treated modulo 360.
625
626 @param oval bounds of ellipse containing arc
627 @param startAngleDeg starting angle of arc in degrees
628 @param sweepAngleDeg sweep, in degrees. Positive is clockwise; treated modulo 360
629 @param forceMoveTo true to start a new contour with arc
630 @return reference to the builder
631 */
632 SkPathBuilder& arcTo(const SkRect& oval, SkScalar startAngleDeg, SkScalar sweepAngleDeg,
633 bool forceMoveTo);
634
635 /** Appends arc to SkPath, after appending line if needed. Arc is implemented by conic
636 weighted to describe part of circle. Arc is contained by tangent from
637 last SkPath point to p1, and tangent from p1 to p2. Arc
638 is part of circle sized to radius, positioned so it touches both tangent lines.
639
640 If last SkPath SkPoint does not start arc, arcTo() appends connecting line to SkPath.
641 The length of vector from p1 to p2 does not affect arc.
642
643 Arc sweep is always less than 180 degrees. If radius is zero, or if
644 tangents are nearly parallel, arcTo() appends line from last SkPath SkPoint to p1.
645
646 arcTo() appends at most one line and one conic.
647 arcTo() implements the functionality of PostScript arct and HTML Canvas arcTo.
648
649 @param p1 SkPoint common to pair of tangents
650 @param p2 end of second tangent
651 @param radius distance from arc to circle center
652 @return reference to SkPath
653 */
654 SkPathBuilder& arcTo(SkPoint p1, SkPoint p2, SkScalar radius);
655
656 /** Appends arc to SkPath. Arc is implemented by one or more conic weighted to describe
657 part of oval with radii (r.fX, r.fY) rotated by xAxisRotate degrees. Arc curves
658 from last SkPath SkPoint to (xy.fX, xy.fY), choosing one of four possible routes:
659 clockwise or counterclockwise,
660 and smaller or larger.
661
662 Arc sweep is always less than 360 degrees. arcTo() appends line to xy if either
663 radii are zero, or if last SkPath SkPoint equals (xy.fX, xy.fY). arcTo() scales radii r to
664 fit last SkPath SkPoint and xy if both are greater than zero but too small to describe
665 an arc.
666
667 arcTo() appends up to four conic curves.
668 arcTo() implements the functionality of SVG arc, although SVG sweep-flag value is
669 opposite the integer value of sweep; SVG sweep-flag uses 1 for clockwise, while
670 kCW_Direction cast to int is zero.
671
672 @param r radii on axes before x-axis rotation
673 @param xAxisRotate x-axis rotation in degrees; positive values are clockwise
674 @param largeArc chooses smaller or larger arc
675 @param sweep chooses clockwise or counterclockwise arc
676 @param xy end of arc
677 @return reference to SkPath
678 */
679 SkPathBuilder& arcTo(SkPoint r, SkScalar xAxisRotate, ArcSize largeArc, SkPathDirection sweep,
680 SkPoint xy);
681
682 /** Appends arc to the builder, as the start of new contour. Arc added is part of ellipse
683 bounded by oval, from startAngle through sweepAngle. Both startAngle and
684 sweepAngle are measured in degrees, where zero degrees is aligned with the
685 positive x-axis, and positive sweeps extends arc clockwise.
686
687 If sweepAngle <= -360, or sweepAngle >= 360; and startAngle modulo 90 is nearly
688 zero, append oval instead of arc. Otherwise, sweepAngle values are treated
689 modulo 360, and arc may or may not draw depending on numeric rounding.
690
691 @param oval bounds of ellipse containing arc
692 @param startAngleDeg starting angle of arc in degrees
693 @param sweepAngleDeg sweep, in degrees. Positive is clockwise; treated modulo 360
694 @return reference to this builder
695 */
696 SkPathBuilder& addArc(const SkRect& oval, SkScalar startAngleDeg, SkScalar sweepAngleDeg);
697
698 SkPathBuilder& addLine(SkPoint a, SkPoint b) {
699 return this->moveTo(a).lineTo(b);
700 }
701
702 /** Adds a new contour to the SkPathBuilder, defined by the rect, and wound in the
703 specified direction. The verbs added to the path will be:
704
705 kMove, kLine, kLine, kLine, kClose
706
707 start specifies which corner to begin the contour:
708 0: upper-left corner
709 1: upper-right corner
710 2: lower-right corner
711 3: lower-left corner
712
713 This start point also acts as the implied beginning of the subsequent,
714 contour, if it does not have an explicit moveTo(). e.g.
715
716 path.addRect(...)
717 // if we don't say moveTo() here, we will use the rect's start point
718 path.lineTo(...)
719
720 @param rect SkRect to add as a closed contour
721 @param dir SkPath::Direction to orient the new contour
722 @param start initial corner of SkRect to add
723 @return reference to SkPathBuilder
724 */
725 SkPathBuilder& addRect(const SkRect&, SkPathDirection, unsigned startIndex);
726
727 /** Adds a new contour to the SkPathBuilder, defined by the rect, and wound in the
728 specified direction. The verbs added to the path will be:
729
730 kMove, kLine, kLine, kLine, kClose
731
732 The contour starts at the upper-left corner of the rect, which also acts as the implied
733 beginning of the subsequent contour, if it does not have an explicit moveTo(). e.g.
734
735 path.addRect(...)
736 // if we don't say moveTo() here, we will use the rect's upper-left corner
737 path.lineTo(...)
738
739 @param rect SkRect to add as a closed contour
740 @param dir SkPath::Direction to orient the new contour
741 @return reference to SkPathBuilder
742 */
743 SkPathBuilder& addRect(const SkRect& rect, SkPathDirection dir = SkPathDirection::kDefault) {
744 return this->addRect(rect, dir, 0);
745 }
746
747 /** Adds oval to SkPathBuilder, appending kMove_Verb, four kConic_Verb, and kClose_Verb.
748 Oval is upright ellipse bounded by SkRect oval with radii equal to half oval width
749 and half oval height. Oval begins at (oval.fRight, oval.centerY()) and continues
750 clockwise if dir is kCW_Direction, counterclockwise if dir is kCCW_Direction.
751
752 @param oval bounds of ellipse added
753 @param dir SkPath::Direction to wind ellipse
754 @return reference to SkPathBuilder
755 */
756 SkPathBuilder& addOval(const SkRect&, SkPathDirection, unsigned startIndex);
757
758 /** Appends SkRRect to SkPathBuilder, creating a new closed contour. If dir is kCW_Direction,
759 SkRRect winds clockwise. If dir is kCCW_Direction, SkRRect winds counterclockwise.
760
761 After appending, SkPathBuilder may be empty, or may contain: SkRect, oval, or SkRRect.
762
763 @param rrect SkRRect to add
764 @param dir SkPath::Direction to wind SkRRect
765 @param start index of initial point of SkRRect
766 @return reference to SkPathBuilder
767 */
768 SkPathBuilder& addRRect(const SkRRect& rrect, SkPathDirection, unsigned start);
769
770 /** Appends SkRRect to SkPathBuilder, creating a new closed contour. If dir is kCW_Direction,
771 SkRRect starts at top-left of the lower-left corner and winds clockwise. If dir is
772 kCCW_Direction, SkRRect starts at the bottom-left of the upper-left corner and winds
773 counterclockwise.
774
775 After appending, SkPathBuilder may be empty, or may contain: SkRect, oval, or SkRRect.
776
777 @param rrect SkRRect to add
778 @param dir SkPath::Direction to wind SkRRect
779 @return reference to SkPathBuilder
780 */
781 SkPathBuilder& addRRect(const SkRRect& rrect, SkPathDirection dir = SkPathDirection::kDefault) {
782 // legacy start indices: 6 (CW) and 7 (CCW)
783 return this->addRRect(rrect, dir, dir == SkPathDirection::kCW ? 6 : 7);
784 }
785
786 /** Adds oval to SkPathBuilder, appending kMove_Verb, four kConic_Verb, and kClose_Verb.
787 Oval is upright ellipse bounded by SkRect oval with radii equal to half oval width
788 and half oval height. Oval begins at start and continues
789 clockwise if dir is kCW_Direction, counterclockwise if dir is kCCW_Direction.
790
791 @param oval bounds of ellipse added
792 @param dir SkPath::Direction to wind ellipse
793 @return reference to SkPath
794
795 example: https://fiddle.skia.org/c/@Path_addOval_2
796 */
797 SkPathBuilder& addOval(const SkRect& oval, SkPathDirection dir = SkPathDirection::kDefault) {
798 // legacy start index: 1
799 return this->addOval(oval, dir, 1);
800 }
801
802 /** Adds circle with center and radius to SkPathBuilder, appending kMove_Verb,
803 four kConic_Verb, and kClose_Verb. Circle begins at: (center.fX + radius, center.fY).
804
805 Has no effect if radius is zero or negative.
806
807 @param center center of circle
808 @param radius distance from center to edge
809 @param dir SkPath::Direction to wind circle
810 @return reference to SkPathBuilder
811 */
812 SkPathBuilder& addCircle(SkPoint center, float radius,
813 SkPathDirection dir = SkPathDirection::kDefault);
814 SkPathBuilder& addCircle(float x, float y, float radius,
815 SkPathDirection dir = SkPathDirection::kDefault) {
816 return this->addCircle({x, y}, radius, dir);
817 }
818
819 /** Adds contour created from line array, adding (pts.size() - 1) line segments.
820 Contour added starts at pts[0], then adds a line for every additional SkPoint
821 in pts array. If close is true, appends kClose_Verb to SkPath, connecting
822 pts[count - 1] and pts[0].
823
824 @param pts array of line sharing end and start SkPoint
825 @param close true to add line connecting contour end and start
826 @return reference to SkPath
827 */
828 SkPathBuilder& addPolygon(SkSpan<const SkPoint> pts, bool close);
829
830 /** Appends src to SkPathBuilder, offset by (dx, dy).
831
832 If mode is kAppend_AddPathMode, src verb array, SkPoint array, and conic weights are
833 added unaltered. If mode is kExtend_AddPathMode, add line before appending
834 verbs, SkPoint, and conic weights.
835
836 @param src SkPath verbs, SkPoint, and conic weights to add
837 @param dx offset added to src SkPoint array x-axis coordinates
838 @param dy offset added to src SkPoint array y-axis coordinates
839 @param mode kAppend_AddPathMode or kExtend_AddPathMode
840 @return reference to SkPathBuilder
841 */
842 SkPathBuilder& addPath(const SkPath& src, SkScalar dx, SkScalar dy,
843 SkPath::AddPathMode mode = SkPath::kAppend_AddPathMode);
844
845 /** Appends src to SkPathBuilder.
846
847 If mode is kAppend_AddPathMode, src verb array, SkPoint array, and conic weights are
848 added unaltered. If mode is kExtend_AddPathMode, add line before appending
849 verbs, SkPoint, and conic weights.
850
851 @param src SkPath verbs, SkPoint, and conic weights to add
852 @param mode kAppend_AddPathMode or kExtend_AddPathMode
853 @return reference to SkPathBuilder
854 */
855 SkPathBuilder& addPath(const SkPath& src,
856 SkPath::AddPathMode mode = SkPath::kAppend_AddPathMode) {
857 return this->addPath(src, SkMatrix::I(), mode);
858 }
859
860 /** Appends src to SkPathBuilder, transformed by matrix. Transformed curves may have different
861 verbs, SkPoint, and conic weights.
862
863 If mode is kAppend_AddPathMode, src verb array, SkPoint array, and conic weights are
864 added unaltered. If mode is kExtend_AddPathMode, add line before appending
865 verbs, SkPoint, and conic weights.
866
867 @param src SkPath verbs, SkPoint, and conic weights to add
868 @param matrix transform applied to src
869 @param mode kAppend_AddPathMode or kExtend_AddPathMode
870 @return reference to SkPathBuilder
871 */
872 SkPathBuilder& addPath(const SkPath& src, const SkMatrix& matrix,
873 SkPath::AddPathMode mode = SkPath::AddPathMode::kAppend_AddPathMode);
874
875 // Performance hint, to reserve extra storage for subsequent calls to lineTo, quadTo, etc.
876
877 /** Grows SkPathBuilder verb array and SkPoint array to contain additional space.
878 May improve performance and use less memory by
879 reducing the number and size of allocations when creating SkPathBuilder.
880
881 @param extraPtCount number of additional SkPoint to allocate
882 @param extraVerbCount number of additional verbs
883 @param extraConicCount number of additional conic weights
884 */
885 void incReserve(int extraPtCount, int extraVerbCount, int extraConicCount);
886
887 /** Grows SkPathBuilder verb array and SkPoint array to contain additional space.
888 May improve performance and use less memory by
889 reducing the number and size of allocations when creating SkPathBuilder.
890
891 @param extraPtCount number of additional SkPoints and verbs to allocate
892 */
893 void incReserve(int extraPtCount) {
894 this->incReserve(extraPtCount, extraPtCount, 0);
895 }
896
897 /** Offsets SkPoint array by (dx, dy).
898
899 @param dx offset added to SkPoint array x-axis coordinates
900 @param dy offset added to SkPoint array y-axis coordinates
901 */
902 SkPathBuilder& offset(SkScalar dx, SkScalar dy);
903
904 /** Transforms verb array, SkPoint array, and weight by matrix.
905 transform may change verbs and increase their number.
906
907 @param matrix SkMatrix to apply to SkPath
908 @param pc whether to apply perspective clipping
909 */
910 SkPathBuilder& transform(const SkMatrix& matrix);
911
912 /*
913 * Returns true if the builder is empty, or all of its points are finite.
914 */
915 bool isFinite() const;
916
917 /** Replaces SkPathFillType with its inverse. The inverse of SkPathFillType describes the area
918 unmodified by the original SkPathFillType.
919 */
920 SkPathBuilder& toggleInverseFillType() {
921 fFillType = SkPathFillType_ToggleInverse(fFillType);
922 return *this;
923 }
924
925 /** Returns if SkPath is empty.
926 Empty SkPathBuilder may have FillType but has no SkPoint, SkPath::Verb, or conic weight.
927 SkPathBuilder() constructs empty SkPathBuilder; reset() and rewind() make SkPath empty.
928
929 @return true if the path contains no SkPath::Verb array
930 */
931 bool isEmpty() const { return fVerbs.empty(); }
932
933 /** Returns last point on SkPathBuilder. Returns nullopt if SkPoint array is empty.
934
935 @return last SkPoint if SkPoint array contains one or more SkPoint, otherwise nullopt
936
937 example: https://fiddle.skia.org/c/@Path_getLastPt
938 */
939 std::optional<SkPoint> getLastPt() const;
940
941 /** Change the point at the specified index (see countPoints()).
942 * If index is out of range, the call does nothing.
943 *
944 * @param index which point to replace
945 * @param p the new point value
946 */
947 void setPoint(size_t index, SkPoint p);
948
949 /** Change the last point in the builder.
950 * If the builder is empty, the call does nothing.
951 *
952 * @param p the new point value
953 */
954 void setLastPoint(SkPoint p) {
955 this->setPoint(this->points().size() - 1, p);
956 }
957
958 #ifdef SK_SUPPORT_LEGACY_PATHBUILDER_SETLASTPT
959 /** DEPRECATED: use setLastPoint() or setPoint()
960 Sets the last point on the path. If SkPoint array is empty, this behaves like moveTo().
961
962 @param pt the new value for the last point in the path
963 */
964 void setLastPt(SkPoint pt);
965 void setLastPt(float x, float y) { this->setLastPt({x, y}); }
966 #endif
967
968 /** Returns the number of points in SkPathBuilder.
969 SkPoint count is initially zero.
970
971 @return SkPathBuilder SkPoint array length
972 */
973 int countPoints() const { return fPts.size(); }
974
975 /** Returns if SkPathFillType describes area outside SkPath geometry. The inverse fill area
976 extends indefinitely.
977
978 @return true if FillType is kInverseWinding or kInverseEvenOdd
979 */
980 bool isInverseFillType() const { return SkPathFillType_IsInverse(fFillType); }
981
982 #ifdef SK_SUPPORT_UNSPANNED_APIS
983 SkPathBuilder& addPolygon(const SkPoint pts[], int count, bool close) {
984 return this->addPolygon({pts, count}, close);
985 }
986 SkPathBuilder& polylineTo(const SkPoint pts[], int count) {
987 return this->polylineTo({pts, count});
988 }
989 #endif
990
991 SkSpan<const SkPoint> points() const {
992 return fPts;
993 }
994 SkSpan<const SkPathVerb> verbs() const {
995 return fVerbs;
996 }
997 SkSpan<const float> conicWeights() const {
998 return fConicWeights;
999 }
1000
1001 SkPathBuilder& addRaw(const SkPathRaw&);
1002
1003 SkPathIter iter() const;
1004
1005 enum class DumpFormat {
1006 kDecimal,
1007 kHex,
1008 };
1009 SkString dumpToString(DumpFormat = DumpFormat::kDecimal) const;
1010 void dump(DumpFormat) const;
1011 // can't use default argument easily in debugger, so we name this
1012 // helper explicitly.
1013 void dump() const { this->dump(DumpFormat::kDecimal); }
1014
1015 bool contains(SkPoint) const;
1016
1017 private:
1018 PointsArray fPts;
1019 VerbsArray fVerbs;
1020 ConicWeightsArray fConicWeights;
1021
1022 SkPathFillType fFillType;
1023 bool fIsVolatile;
1024 SkPathConvexity fConvexity;
1025
1026 unsigned fSegmentMask;
1027 int fLastMoveIndex; // only needed until SkPath is immutable
1028
1029 SkPathIsAType fType = SkPathIsAType::kGeneral;
1030 SkPathIsAData fIsA {};
1031
1032 // called right before we add a (non-move) verb
1033 void ensureMove() {
1034 fType = SkPathIsAType::kGeneral;
1035 if (fVerbs.empty()) {
1036 this->moveTo({0, 0});
1037 } else if (fVerbs.back() == SkPathVerb::kClose) {
1038 this->moveTo(fPts[fLastMoveIndex]);
1039 }
1040 }
1041
1042 bool isZeroLengthSincePoint(int startPtIndex) const;
1043
1044 SkPathBuilder& privateReverseAddPath(const SkPath&);
1045 SkPathBuilder& privateReversePathTo(const SkPath&);
1046
1047 std::tuple<SkPoint*, SkScalar*> growForVerbsInPath(const SkPath& path);
1048
1049 friend class SkPathPriv;
1050 friend class SkStroke;
1051 friend class SkPathStroker;
1052 };
1053
1054 #endif
1055