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/SkMatrix.h"
12 #include "include/core/SkPath.h"
14 #include "include/private/SkTDArray.h"
15 
17 public:
21  SkPathBuilder(const SkPathBuilder&) = default;
23 
26 
27  SkPathFillType fillType() const { return fFillType; }
29 
30  SkPath snapshot() const; // the builder is unchanged after returning this path
31  SkPath detach(); // the builder is reset to empty after returning this path
32 
33  SkPathBuilder& setFillType(SkPathFillType ft) { fFillType = ft; return *this; }
34  SkPathBuilder& setIsVolatile(bool isVolatile) { fIsVolatile = isVolatile; return *this; }
35 
37 
39  SkPathBuilder& moveTo(SkScalar x, SkScalar y) { return this->moveTo(SkPoint::Make(x, y)); }
40 
42  SkPathBuilder& lineTo(SkScalar x, SkScalar y) { return this->lineTo(SkPoint::Make(x, y)); }
43 
46  return this->quadTo(SkPoint::Make(x1, y1), SkPoint::Make(x2, y2));
47  }
48  SkPathBuilder& quadTo(const SkPoint pts[2]) { return this->quadTo(pts[0], pts[1]); }
49 
52  return this->conicTo(SkPoint::Make(x1, y1), SkPoint::Make(x2, y2), w);
53  }
54  SkPathBuilder& conicTo(const SkPoint pts[2], SkScalar w) {
55  return this->conicTo(pts[0], pts[1], w);
56  }
57 
60  return this->cubicTo(SkPoint::Make(x1, y1), SkPoint::Make(x2, y2), SkPoint::Make(x3, y3));
61  }
62  SkPathBuilder& cubicTo(const SkPoint pts[3]) {
63  return this->cubicTo(pts[0], pts[1], pts[2]);
64  }
65 
67 
68  // Append a series of lineTo(...)
69  SkPathBuilder& polylineTo(const SkPoint pts[], int count);
70  SkPathBuilder& polylineTo(const std::initializer_list<SkPoint>& list) {
71  return this->polylineTo(list.begin(), SkToInt(list.size()));
72  }
73 
74  // Relative versions of segments, relative to the previous position.
75 
77  SkPathBuilder& rLineTo(SkScalar x, SkScalar y) { return this->rLineTo({x, y}); }
80  return this->rQuadTo({x1, y1}, {x2, y2});
81  }
84  return this->rConicTo({x1, y1}, {x2, y2}, w);
85  }
88  return this->rCubicTo({x1, y1}, {x2, y2}, {x3, y3});
89  }
90 
91  // Arcs
92 
108  SkPathBuilder& arcTo(const SkRect& oval, SkScalar startAngleDeg, SkScalar sweepAngleDeg,
109  bool forceMoveTo);
110 
131 
132  enum ArcSize {
135  };
136 
160  SkPathBuilder& arcTo(SkPoint r, SkScalar xAxisRotate, ArcSize largeArc, SkPathDirection sweep,
161  SkPoint xy);
162 
177  SkPathBuilder& addArc(const SkRect& oval, SkScalar startAngleDeg, SkScalar sweepAngleDeg);
178 
179  // Add a new contour
180 
181  SkPathBuilder& addRect(const SkRect&, SkPathDirection, unsigned startIndex);
182  SkPathBuilder& addOval(const SkRect&, SkPathDirection, unsigned startIndex);
183  SkPathBuilder& addRRect(const SkRRect&, SkPathDirection, unsigned startIndex);
184 
186  return this->addRect(rect, dir, 0);
187  }
189  // legacy start index: 1
190  return this->addOval(rect, dir, 1);
191  }
193  // legacy start indices: 6 (CW) and 7 (CCW)
194  return this->addRRect(rrect, dir, dir == SkPathDirection::kCW ? 6 : 7);
195  }
196 
197  SkPathBuilder& addCircle(SkScalar center_x, SkScalar center_y, SkScalar radius,
199 
200  SkPathBuilder& addPolygon(const SkPoint pts[], int count, bool isClosed);
201  SkPathBuilder& addPolygon(const std::initializer_list<SkPoint>& list, bool isClosed) {
202  return this->addPolygon(list.begin(), SkToInt(list.size()), isClosed);
203  }
204 
206 
207  // Performance hint, to reserve extra storage for subsequent calls to lineTo, quadTo, etc.
208 
209  void incReserve(int extraPtCount, int extraVerbCount);
210  void incReserve(int extraPtCount) {
211  this->incReserve(extraPtCount, extraPtCount);
212  }
213 
215 
217  fFillType = (SkPathFillType)((unsigned)fFillType ^ 2);
218  return *this;
219  }
220 
221 private:
222  SkTDArray<SkPoint> fPts;
223  SkTDArray<uint8_t> fVerbs;
224  SkTDArray<SkScalar> fConicWeights;
225 
226  SkPathFillType fFillType;
227  bool fIsVolatile;
228 
229  unsigned fSegmentMask;
230  SkPoint fLastMovePoint;
231  bool fNeedsMoveVerb;
232 
233  enum IsA {
234  kIsA_JustMoves, // we only have 0 or more moves
235  kIsA_MoreThanMoves, // we have verbs other than just move
236  kIsA_Oval, // we are 0 or more moves followed by an oval
237  kIsA_RRect, // we are 0 or more moves followed by a rrect
238  };
239  IsA fIsA = kIsA_JustMoves;
240  int fIsAStart = -1; // tracks direction iff fIsA is not unknown
241  bool fIsACCW = false; // tracks direction iff fIsA is not unknown
242 
243  // for testing
244  SkPathConvexity fOverrideConvexity = SkPathConvexity::kUnknown;
245 
246  int countVerbs() const { return fVerbs.count(); }
247 
248  // called right before we add a (non-move) verb
249  void ensureMove() {
250  fIsA = kIsA_MoreThanMoves;
251  if (fNeedsMoveVerb) {
252  this->moveTo(fLastMovePoint);
253  }
254  }
255 
256  SkPath make(sk_sp<SkPathRef>) const;
257 
258  SkPathBuilder& privateReverseAddPath(const SkPath&);
259 
260  // For testing
261  void privateSetConvexity(SkPathConvexity c) { fOverrideConvexity = c; }
262 
263  friend class SkPathPriv;
264 };
265 
266 #endif
267 
SkPathBuilder::rConicTo
SkPathBuilder & rConicTo(SkPoint p1, SkPoint p2, SkScalar w)
SkPathBuilder::addPolygon
SkPathBuilder & addPolygon(const std::initializer_list< SkPoint > &list, bool isClosed)
Definition: SkPathBuilder.h:201
SkPathBuilder::moveTo
SkPathBuilder & moveTo(SkPoint pt)
SkPathBuilder::addPath
SkPathBuilder & addPath(const SkPath &)
SkPathBuilder::cubicTo
SkPathBuilder & cubicTo(SkPoint pt1, SkPoint pt2, SkPoint pt3)
SkPathBuilder::SkPathBuilder
SkPathBuilder(const SkPathBuilder &)=default
SkPathBuilder::addOval
SkPathBuilder & addOval(const SkRect &, SkPathDirection, unsigned startIndex)
SkPathBuilder::conicTo
SkPathBuilder & conicTo(SkPoint pt1, SkPoint pt2, SkScalar w)
SkPathBuilder::SkPathBuilder
SkPathBuilder(SkPathFillType)
SkPathBuilder::lineTo
SkPathBuilder & lineTo(SkPoint pt)
SkPath
Definition: SkPath.h:44
SkPathBuilder::snapshot
SkPath snapshot() const
SkPathBuilder::addArc
SkPathBuilder & addArc(const SkRect &oval, SkScalar startAngleDeg, SkScalar sweepAngleDeg)
Appends arc to the builder, as the start of new contour.
SkPoint::Make
static constexpr SkPoint Make(SkScalar x, SkScalar y)
Sets fX to x, fY to y.
Definition: SkPoint.h:170
SkPathBuilder::rCubicTo
SkPathBuilder & rCubicTo(SkPoint pt1, SkPoint pt2, SkPoint pt3)
SkPathBuilder::incReserve
void incReserve(int extraPtCount)
Definition: SkPathBuilder.h:210
SkPathBuilder::addRect
SkPathBuilder & addRect(const SkRect &rect, SkPathDirection dir=SkPathDirection::kCW)
Definition: SkPathBuilder.h:185
SkPathDirection
SkPathDirection
Definition: SkPathTypes.h:36
SkPathBuilder::lineTo
SkPathBuilder & lineTo(SkScalar x, SkScalar y)
Definition: SkPathBuilder.h:42
SkPathBuilder::computeBounds
SkRect computeBounds() const
SkPathBuilder::cubicTo
SkPathBuilder & cubicTo(SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2, SkScalar x3, SkScalar y3)
Definition: SkPathBuilder.h:59
SkPathBuilder::polylineTo
SkPathBuilder & polylineTo(const std::initializer_list< SkPoint > &list)
Definition: SkPathBuilder.h:70
SkPathBuilder::moveTo
SkPathBuilder & moveTo(SkScalar x, SkScalar y)
Definition: SkPathBuilder.h:39
SkPathTypes.h
SkPathBuilder::arcTo
SkPathBuilder & arcTo(const SkRect &oval, SkScalar startAngleDeg, SkScalar sweepAngleDeg, bool forceMoveTo)
Appends arc to the builder.
SkPathFillType
SkPathFillType
Definition: SkPathTypes.h:13
SkPathBuilder::quadTo
SkPathBuilder & quadTo(const SkPoint pts[2])
Definition: SkPathBuilder.h:48
SkRRect
Definition: SkRRect.h:31
SkPathBuilder::addCircle
SkPathBuilder & addCircle(SkScalar center_x, SkScalar center_y, SkScalar radius, SkPathDirection dir=SkPathDirection::kCW)
SkPathBuilder::rCubicTo
SkPathBuilder & rCubicTo(SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2, SkScalar x3, SkScalar y3)
Definition: SkPathBuilder.h:87
SkPathBuilder::conicTo
SkPathBuilder & conicTo(SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2, SkScalar w)
Definition: SkPathBuilder.h:51
SkPathBuilder::addRRect
SkPathBuilder & addRRect(const SkRRect &rrect, SkPathDirection dir=SkPathDirection::kCW)
Definition: SkPathBuilder.h:192
sk_sp< SkPathRef >
SkPathBuilder::polylineTo
SkPathBuilder & polylineTo(const SkPoint pts[], int count)
SkPoint
Definition: SkPoint.h:160
SkPathBuilder::rLineTo
SkPathBuilder & rLineTo(SkScalar x, SkScalar y)
Definition: SkPathBuilder.h:77
SkPathBuilder::addRRect
SkPathBuilder & addRRect(const SkRRect &, SkPathDirection, unsigned startIndex)
SK_API
#define SK_API
Definition: SkTypes.h:181
SkPathBuilder::offset
SkPathBuilder & offset(SkScalar dx, SkScalar dy)
SkPathBuilder::SkPathBuilder
SkPathBuilder(const SkPath &)
SkPathBuilder::rLineTo
SkPathBuilder & rLineTo(SkPoint pt)
SkPathBuilder::conicTo
SkPathBuilder & conicTo(const SkPoint pts[2], SkScalar w)
Definition: SkPathBuilder.h:54
SkPathBuilder::detach
SkPath detach()
SkPathBuilder::incReserve
void incReserve(int extraPtCount, int extraVerbCount)
SkPathBuilder::quadTo
SkPathBuilder & quadTo(SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2)
Definition: SkPathBuilder.h:45
SkPathBuilder::toggleInverseFillType
SkPathBuilder & toggleInverseFillType()
Definition: SkPathBuilder.h:216
SkPathBuilder::operator=
SkPathBuilder & operator=(const SkPathBuilder &)=default
SkPathBuilder::rConicTo
SkPathBuilder & rConicTo(SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2, SkScalar w)
Definition: SkPathBuilder.h:83
SkPathBuilder::setIsVolatile
SkPathBuilder & setIsVolatile(bool isVolatile)
Definition: SkPathBuilder.h:34
SkPathBuilder::arcTo
SkPathBuilder & arcTo(SkPoint r, SkScalar xAxisRotate, ArcSize largeArc, SkPathDirection sweep, SkPoint xy)
Appends arc to SkPath.
SkPathBuilder::fillType
SkPathFillType fillType() const
Definition: SkPathBuilder.h:27
SkScalar
float SkScalar
Definition: SkScalar.h:16
SkPathBuilder::~SkPathBuilder
~SkPathBuilder()
SkPathBuilder::setFillType
SkPathBuilder & setFillType(SkPathFillType ft)
Definition: SkPathBuilder.h:33
SkPath.h
SkPathBuilder::reset
SkPathBuilder & reset()
SkPathBuilder::close
SkPathBuilder & close()
SkMatrix.h
SkPathBuilder::addPolygon
SkPathBuilder & addPolygon(const SkPoint pts[], int count, bool isClosed)
SkPathDirection::kCW
@ kCW
clockwise direction for adding closed contours
SkPathBuilder::addOval
SkPathBuilder & addOval(const SkRect &rect, SkPathDirection dir=SkPathDirection::kCW)
Definition: SkPathBuilder.h:188
SkPathBuilder::addRect
SkPathBuilder & addRect(const SkRect &, SkPathDirection, unsigned startIndex)
SkPathBuilder::arcTo
SkPathBuilder & arcTo(SkPoint p1, SkPoint p2, SkScalar radius)
Appends arc to SkPath, after appending line if needed.
SkPathBuilder::cubicTo
SkPathBuilder & cubicTo(const SkPoint pts[3])
Definition: SkPathBuilder.h:62
SkRect
Definition: SkRect.h:572
SkPathBuilder::kLarge_ArcSize
@ kLarge_ArcSize
larger of arc pair
Definition: SkPathBuilder.h:134
SkPathBuilder::rQuadTo
SkPathBuilder & rQuadTo(SkScalar x1, SkScalar y1, SkScalar x2, SkScalar y2)
Definition: SkPathBuilder.h:79
SkPathBuilder::operator=
SkPathBuilder & operator=(const SkPath &)
SkPathBuilder::quadTo
SkPathBuilder & quadTo(SkPoint pt1, SkPoint pt2)
SkPathBuilder
Definition: SkPathBuilder.h:16
SkPathBuilder::kSmall_ArcSize
@ kSmall_ArcSize
smaller of arc pair
Definition: SkPathBuilder.h:133
SkPathBuilder::rQuadTo
SkPathBuilder & rQuadTo(SkPoint pt1, SkPoint pt2)
SkPathBuilder::SkPathBuilder
SkPathBuilder()
SkPathBuilder::ArcSize
ArcSize
Definition: SkPathBuilder.h:132