Skia
2D Graphics Library
SkContourMeasure.h
Go to the documentation of this file.
1 /*
2  * Copyright 2018 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 SkContourMeasure_DEFINED
9 #define SkContourMeasure_DEFINED
10 
11 #include "include/core/SkPoint.h"
12 #include "include/core/SkRefCnt.h"
13 #include "include/core/SkScalar.h"
14 #include "include/private/base/SkAPI.h"
15 #include "include/private/base/SkTDArray.h"
16 
17 #include <memory>
18 
19 class SkMatrix;
20 class SkPath;
21 
22 class SK_API SkContourMeasure : public SkRefCnt {
23 public:
26  SkScalar length() const { return fLength; }
27 
31  [[nodiscard]] bool getPosTan(SkScalar distance, SkPoint* position, SkVector* tangent) const;
32 
33  enum MatrixFlags {
34  kGetPosition_MatrixFlag = 0x01,
35  kGetTangent_MatrixFlag = 0x02,
36  kGetPosAndTan_MatrixFlag = kGetPosition_MatrixFlag | kGetTangent_MatrixFlag
37  };
38 
44  [[nodiscard]] bool getMatrix(SkScalar distance, SkMatrix* matrix,
45  MatrixFlags flags = kGetPosAndTan_MatrixFlag) const;
46 
53  [[nodiscard]] bool getSegment(SkScalar startD, SkScalar stopD, SkPath* dst,
54  bool startWithMoveTo) const;
55 
58  bool isClosed() const { return fIsClosed; }
59 
60 private:
61  struct Segment {
62  SkScalar fDistance; // total distance up to this point
63  unsigned fPtIndex; // index into the fPts array
64  unsigned fTValue : 30;
65  unsigned fType : 2; // actually the enum SkSegType
66  // See SkPathMeasurePriv.h
67 
68  SkScalar getScalarT() const;
69 
70  static const Segment* Next(const Segment* seg) {
71  unsigned ptIndex = seg->fPtIndex;
72  do {
73  ++seg;
74  } while (seg->fPtIndex == ptIndex);
75  return seg;
76  }
77 
78  };
79 
80  const SkTDArray<Segment> fSegments;
81  const SkTDArray<SkPoint> fPts; // Points used to define the segments
82 
83  const SkScalar fLength;
84  const bool fIsClosed;
85 
86  SkContourMeasure(SkTDArray<Segment>&& segs, SkTDArray<SkPoint>&& pts,
87  SkScalar length, bool isClosed);
88  ~SkContourMeasure() override {}
89 
90  const Segment* distanceToSegment(SkScalar distance, SkScalar* t) const;
91 
92  friend class SkContourMeasureIter;
93 };
94 
95 class SK_API SkContourMeasureIter {
96 public:
106  SkContourMeasureIter(const SkPath& path, bool forceClosed, SkScalar resScale = 1);
108 
111 
117  void reset(const SkPath& path, bool forceClosed, SkScalar resScale = 1);
118 
131 
132 private:
133  class Impl;
134 
135  std::unique_ptr<Impl> fImpl;
136 };
137 
138 #endif
float SkScalar
Definition: SkScalar.h:14
Definition: SkContourMeasure.h:95
sk_sp< SkContourMeasure > next()
Iterates through contours in path, returning a contour-measure object for each contour in the path.
SkContourMeasureIter(SkContourMeasureIter &&)
SkContourMeasureIter(const SkPath &path, bool forceClosed, SkScalar resScale=1)
Initialize the Iter with a path.
void reset(const SkPath &path, bool forceClosed, SkScalar resScale=1)
Reset the Iter with a path.
SkContourMeasureIter & operator=(SkContourMeasureIter &&)
Definition: SkContourMeasure.h:22
bool getMatrix(SkScalar distance, SkMatrix *matrix, MatrixFlags flags=kGetPosAndTan_MatrixFlag) const
Pins distance to 0 <= distance <= getLength(), and then computes the corresponding matrix (by calling...
bool getSegment(SkScalar startD, SkScalar stopD, SkPath *dst, bool startWithMoveTo) const
Given a start and stop distance, return in dst the intervening segment(s).
bool getPosTan(SkScalar distance, SkPoint *position, SkVector *tangent) const
Pins distance to 0 <= distance <= length(), and then computes the corresponding position and tangent.
MatrixFlags
Definition: SkContourMeasure.h:33
SkScalar length() const
Return the length of the contour.
Definition: SkContourMeasure.h:26
bool isClosed() const
Return true if the contour is closed()
Definition: SkContourMeasure.h:58
SkMatrix holds a 3x3 matrix for transforming coordinates.
Definition: SkMatrix.h:53
SkPath contain geometry.
Definition: SkPath.h:58
Definition: SkRefCnt.h:119