Skia
2D Graphics Library
SkPathBuilder.h
Go to the documentation of 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/SkPath.h"
13 #include "include/core/SkPoint.h"
14 #include "include/core/SkRect.h"
15 #include "include/core/SkRefCnt.h"
16 #include "include/core/SkScalar.h"
17 #include "include/core/SkTypes.h"
18 #include "include/private/SkPathRef.h"
19 #include "include/private/base/SkTo.h"
20 
21 #include <initializer_list>
22 
23 class SkRRect;
24 
25 class SK_API SkPathBuilder {
26 public:
30  SkPathBuilder(const SkPathBuilder&) = default;
32 
35 
36  SkPathFillType fillType() const { return fFillType; }
38 
39  SkPath snapshot() const; // the builder is unchanged after returning this path
40  SkPath detach(); // the builder is reset to empty after returning this path
41 
42  SkPathBuilder& setFillType(SkPathFillType ft) { fFillType = ft; return *this; }
43  SkPathBuilder& setIsVolatile(bool isVolatile) { fIsVolatile = isVolatile; return *this; }
44 
46 
47  SkPathBuilder& moveTo(SkPoint pt);
48  SkPathBuilder& moveTo(SkScalar x, SkScalar y) { return this->moveTo(SkPoint::Make(x, y)); }
49 
50  SkPathBuilder& lineTo(SkPoint pt);
51  SkPathBuilder& lineTo(SkScalar x, SkScalar y) { return this->lineTo(SkPoint::Make(x, y)); }
52 
53  SkPathBuilder& quadTo(SkPoint pt1, SkPoint pt2);
55  return this->quadTo(SkPoint::Make(x1, y1), SkPoint::Make(x2, y2));
56  }
57  SkPathBuilder& quadTo(const SkPoint pts[2]) { return this->quadTo(pts[0], pts[1]); }
58 
59  SkPathBuilder& conicTo(SkPoint pt1, SkPoint pt2, SkScalar w);
61  return this->conicTo(SkPoint::Make(x1, y1), SkPoint::Make(x2, y2), w);
62  }
63  SkPathBuilder& conicTo(const SkPoint pts[2], SkScalar w) {
64  return this->conicTo(pts[0], pts[1], w);
65  }
66 
67  SkPathBuilder& cubicTo(SkPoint pt1, SkPoint pt2, SkPoint pt3);
69  return this->cubicTo(SkPoint::Make(x1, y1), SkPoint::Make(x2, y2), SkPoint::Make(x3, y3));
70  }
71  SkPathBuilder& cubicTo(const SkPoint pts[3]) {
72  return this->cubicTo(pts[0], pts[1], pts[2]);
73  }
74 
76 
77  // Append a series of lineTo(...)
78  SkPathBuilder& polylineTo(const SkPoint pts[], int count);
79  SkPathBuilder& polylineTo(const std::initializer_list<SkPoint>& list) {
80  return this->polylineTo(list.begin(), SkToInt(list.size()));
81  }
82 
83  // Relative versions of segments, relative to the previous position.
84 
85  SkPathBuilder& rLineTo(SkPoint pt);
86  SkPathBuilder& rLineTo(SkScalar x, SkScalar y) { return this->rLineTo({x, y}); }
87  SkPathBuilder& rQuadTo(SkPoint pt1, SkPoint pt2);
89  return this->rQuadTo({x1, y1}, {x2, y2});
90  }
91  SkPathBuilder& rConicTo(SkPoint p1, SkPoint p2, SkScalar w);
93  return this->rConicTo({x1, y1}, {x2, y2}, w);
94  }
95  SkPathBuilder& rCubicTo(SkPoint pt1, SkPoint pt2, SkPoint pt3);
97  return this->rCubicTo({x1, y1}, {x2, y2}, {x3, y3});
98  }
99 
100  // Arcs
101 
117  SkPathBuilder& arcTo(const SkRect& oval, SkScalar startAngleDeg, SkScalar sweepAngleDeg,
118  bool forceMoveTo);
119 
139  SkPathBuilder& arcTo(SkPoint p1, SkPoint p2, SkScalar radius);
140 
141  enum ArcSize {
144  };
145 
169  SkPathBuilder& arcTo(SkPoint r, SkScalar xAxisRotate, ArcSize largeArc, SkPathDirection sweep,
170  SkPoint xy);
171 
186  SkPathBuilder& addArc(const SkRect& oval, SkScalar startAngleDeg, SkScalar sweepAngleDeg);
187 
188  // Add a new contour
189 
190  SkPathBuilder& addRect(const SkRect&, SkPathDirection, unsigned startIndex);
191  SkPathBuilder& addOval(const SkRect&, SkPathDirection, unsigned startIndex);
192  SkPathBuilder& addRRect(const SkRRect&, SkPathDirection, unsigned startIndex);
193 
195  return this->addRect(rect, dir, 0);
196  }
198  // legacy start index: 1
199  return this->addOval(rect, dir, 1);
200  }
202  // legacy start indices: 6 (CW) and 7 (CCW)
203  return this->addRRect(rrect, dir, dir == SkPathDirection::kCW ? 6 : 7);
204  }
205 
206  SkPathBuilder& addCircle(SkScalar center_x, SkScalar center_y, SkScalar radius,
208 
209  SkPathBuilder& addPolygon(const SkPoint pts[], int count, bool isClosed);
210  SkPathBuilder& addPolygon(const std::initializer_list<SkPoint>& list, bool isClosed) {
211  return this->addPolygon(list.begin(), SkToInt(list.size()), isClosed);
212  }
213 
215 
216  // Performance hint, to reserve extra storage for subsequent calls to lineTo, quadTo, etc.
217 
218  void incReserve(int extraPtCount, int extraVerbCount);
219  void incReserve(int extraPtCount) {
220  this->incReserve(extraPtCount, extraPtCount);
221  }
222 
224 
226  fFillType = (SkPathFillType)((unsigned)fFillType ^ 2);
227  return *this;
228  }
229 
230 private:
231  SkPathRef::PointsArray fPts;
232  SkPathRef::VerbsArray fVerbs;
233  SkPathRef::ConicWeightsArray fConicWeights;
234 
235  SkPathFillType fFillType;
236  bool fIsVolatile;
237 
238  unsigned fSegmentMask;
239  SkPoint fLastMovePoint;
240  int fLastMoveIndex; // only needed until SkPath is immutable
241  bool fNeedsMoveVerb;
242 
243  enum IsA {
244  kIsA_JustMoves, // we only have 0 or more moves
245  kIsA_MoreThanMoves, // we have verbs other than just move
246  kIsA_Oval, // we are 0 or more moves followed by an oval
247  kIsA_RRect, // we are 0 or more moves followed by a rrect
248  };
249  IsA fIsA = kIsA_JustMoves;
250  int fIsAStart = -1; // tracks direction iff fIsA is not unknown
251  bool fIsACCW = false; // tracks direction iff fIsA is not unknown
252 
253  int countVerbs() const { return fVerbs.size(); }
254 
255  // called right before we add a (non-move) verb
256  void ensureMove() {
257  fIsA = kIsA_MoreThanMoves;
258  if (fNeedsMoveVerb) {
259  this->moveTo(fLastMovePoint);
260  }
261  }
262 
263  SkPath make(sk_sp<SkPathRef>) const;
264 
265  SkPathBuilder& privateReverseAddPath(const SkPath&);
266 
267  friend class SkPathPriv;
268 };
269 
270 #endif
271 
SkPathDirection
Definition: SkPathTypes.h:34
@ kCW
clockwise direction for adding closed contours
SkPathFillType
Definition: SkPathTypes.h:11
float SkScalar
Definition: SkScalar.h:14
Definition: SkPathBuilder.h:25
SkPathBuilder & cubicTo(SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2, SkScalar x3, SkScalar y3)
Definition: SkPathBuilder.h:68
SkPathBuilder & arcTo(const SkRect &oval, SkScalar startAngleDeg, SkScalar sweepAngleDeg, bool forceMoveTo)
Appends arc to the builder.
SkPathBuilder & conicTo(const SkPoint pts[2], SkScalar w)
Definition: SkPathBuilder.h:63
SkRect computeBounds() const
SkPathBuilder & operator=(const SkPathBuilder &)=default
SkPathBuilder & rCubicTo(SkPoint pt1, SkPoint pt2, SkPoint pt3)
SkPathBuilder & addCircle(SkScalar center_x, SkScalar center_y, SkScalar radius, SkPathDirection dir=SkPathDirection::kCW)
SkPathBuilder & arcTo(SkPoint p1, SkPoint p2, SkScalar radius)
Appends arc to SkPath, after appending line if needed.
SkPathBuilder & arcTo(SkPoint r, SkScalar xAxisRotate, ArcSize largeArc, SkPathDirection sweep, SkPoint xy)
Appends arc to SkPath.
SkPathBuilder & addPolygon(const std::initializer_list< SkPoint > &list, bool isClosed)
Definition: SkPathBuilder.h:210
SkPathBuilder(const SkPathBuilder &)=default
SkPathBuilder & addRRect(const SkRRect &rrect, SkPathDirection dir=SkPathDirection::kCW)
Definition: SkPathBuilder.h:201
SkPathBuilder & rConicTo(SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2, SkScalar w)
Definition: SkPathBuilder.h:92
SkPathBuilder & quadTo(SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2)
Definition: SkPathBuilder.h:54
SkPathBuilder & polylineTo(const std::initializer_list< SkPoint > &list)
Definition: SkPathBuilder.h:79
void incReserve(int extraPtCount)
Definition: SkPathBuilder.h:219
SkPathBuilder & quadTo(const SkPoint pts[2])
Definition: SkPathBuilder.h:57
SkPathBuilder & operator=(const SkPath &)
SkPathBuilder & rQuadTo(SkPoint pt1, SkPoint pt2)
SkPathBuilder & rCubicTo(SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2, SkScalar x3, SkScalar y3)
Definition: SkPathBuilder.h:96
SkPathBuilder & offset(SkScalar dx, SkScalar dy)
SkPathBuilder & addRect(const SkRect &rect, SkPathDirection dir=SkPathDirection::kCW)
Definition: SkPathBuilder.h:194
SkPathBuilder & lineTo(SkScalar x, SkScalar y)
Definition: SkPathBuilder.h:51
SkPathBuilder & rConicTo(SkPoint p1, SkPoint p2, SkScalar w)
SkPathFillType fillType() const
Definition: SkPathBuilder.h:36
SkPathBuilder & polylineTo(const SkPoint pts[], int count)
SkPathBuilder & conicTo(SkPoint pt1, SkPoint pt2, SkScalar w)
SkPathBuilder & rQuadTo(SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2)
Definition: SkPathBuilder.h:88
SkPathBuilder & addPolygon(const SkPoint pts[], int count, bool isClosed)
void incReserve(int extraPtCount, int extraVerbCount)
SkPathBuilder & conicTo(SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2, SkScalar w)
Definition: SkPathBuilder.h:60
SkPathBuilder & toggleInverseFillType()
Definition: SkPathBuilder.h:225
SkPathBuilder & addRect(const SkRect &, SkPathDirection, unsigned startIndex)
SkPathBuilder & addOval(const SkRect &rect, SkPathDirection dir=SkPathDirection::kCW)
Definition: SkPathBuilder.h:197
SkPathBuilder & addArc(const SkRect &oval, SkScalar startAngleDeg, SkScalar sweepAngleDeg)
Appends arc to the builder, as the start of new contour.
SkPathBuilder & setFillType(SkPathFillType ft)
Definition: SkPathBuilder.h:42
SkPathBuilder & setIsVolatile(bool isVolatile)
Definition: SkPathBuilder.h:43
SkPath detach()
SkPathBuilder & quadTo(SkPoint pt1, SkPoint pt2)
SkPathBuilder & close()
SkPathBuilder & cubicTo(const SkPoint pts[3])
Definition: SkPathBuilder.h:71
SkPathBuilder & rLineTo(SkScalar x, SkScalar y)
Definition: SkPathBuilder.h:86
SkPathBuilder & lineTo(SkPoint pt)
SkPathBuilder(SkPathFillType)
SkPathBuilder & reset()
SkPathBuilder & addRRect(const SkRRect &, SkPathDirection, unsigned startIndex)
SkPathBuilder & cubicTo(SkPoint pt1, SkPoint pt2, SkPoint pt3)
SkPathBuilder & rLineTo(SkPoint pt)
SkPathBuilder & addOval(const SkRect &, SkPathDirection, unsigned startIndex)
SkPathBuilder & moveTo(SkPoint pt)
SkPathBuilder(const SkPath &)
SkPathBuilder & addPath(const SkPath &)
ArcSize
Definition: SkPathBuilder.h:141
@ kSmall_ArcSize
smaller of arc pair
Definition: SkPathBuilder.h:142
@ kLarge_ArcSize
larger of arc pair
Definition: SkPathBuilder.h:143
SkPathBuilder & moveTo(SkScalar x, SkScalar y)
Definition: SkPathBuilder.h:48
SkPath snapshot() const
SkPath contain geometry.
Definition: SkPath.h:58
SkRRect describes a rounded rectangle with a bounds and a pair of radii for each corner.
Definition: SkRRect.h:35
sk_sp< SkDrawLooper > SK_API Make(SkColor4f color, SkColorSpace *cs, SkScalar sigma, SkScalar dx, SkScalar dy)
SkRect holds four float coordinates describing the upper and lower bounds of a rectangle.
Definition: SkRect.h:582