Flutter Impeller
path.h
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #pragma once
6 
7 #include <functional>
8 #include <optional>
9 #include <set>
10 #include <tuple>
11 #include <vector>
12 
14 
15 namespace impeller {
16 
17 enum class Cap {
18  kButt,
19  kRound,
20  kSquare,
21 };
22 
23 enum class Join {
24  kMiter,
25  kRound,
26  kBevel,
27 };
28 
29 enum class FillType {
30  kNonZero, // The default winding order.
31  kOdd,
32  kPositive,
33  kNegative,
34  kAbsGeqTwo,
35 };
36 
37 enum class Convexity {
38  kUnknown,
39  kConvex,
40 };
41 
42 //------------------------------------------------------------------------------
43 /// @brief Paths are lightweight objects that describe a collection of
44 /// linear, quadratic, or cubic segments. These segments may be
45 /// broken up by move commands, which are effectively linear
46 /// commands that pick up the pen rather than continuing to draw.
47 ///
48 /// All shapes supported by Impeller are paths either directly or
49 /// via approximation (in the case of circles).
50 ///
51 /// Paths are externally immutable once created, Creating paths must
52 /// be done using a path builder.
53 ///
54 class Path {
55  public:
56  enum class ComponentType {
57  kLinear,
58  kQuadratic,
59  kCubic,
60  kContour,
61  };
62 
63  struct PolylineContour {
64  /// Index that denotes the first point of this contour.
65  size_t start_index;
66  /// Denotes whether the last point of this contour is connected to the first
67  /// point of this contour or not.
68  bool is_closed;
69 
70  /// The direction of the contour's start cap.
72  /// The direction of the contour's end cap.
74  };
75 
76  /// One or more contours represented as a series of points and indices in
77  /// the point vector representing the start of a new contour.
78  struct Polyline {
79  /// Points in the polyline, which may represent multiple contours specified
80  /// by indices in |breaks|.
81  std::vector<Point> points;
82  std::vector<PolylineContour> contours;
83 
84  /// Convenience method to compute the start (inclusive) and end (exclusive)
85  /// point of the given contour index.
86  ///
87  /// The contour_index parameter is clamped to contours.size().
88  std::tuple<size_t, size_t> GetContourPointBounds(
89  size_t contour_index) const;
90  };
91 
92  Path();
93 
94  ~Path();
95 
96  size_t GetComponentCount(std::optional<ComponentType> type = {}) const;
97 
98  FillType GetFillType() const;
99 
100  bool IsConvex() const;
101 
102  template <class T>
103  using Applier = std::function<void(size_t index, const T& component)>;
104  void EnumerateComponents(
105  const Applier<LinearPathComponent>& linear_applier,
106  const Applier<QuadraticPathComponent>& quad_applier,
107  const Applier<CubicPathComponent>& cubic_applier,
108  const Applier<ContourComponent>& contour_applier) const;
109 
110  bool GetLinearComponentAtIndex(size_t index,
111  LinearPathComponent& linear) const;
112 
113  bool GetQuadraticComponentAtIndex(size_t index,
114  QuadraticPathComponent& quadratic) const;
115 
116  bool GetCubicComponentAtIndex(size_t index, CubicPathComponent& cubic) const;
117 
118  bool GetContourComponentAtIndex(size_t index,
119  ContourComponent& contour) const;
120 
121  /// Callers must provide the scale factor for how this path will be
122  /// transformed.
123  ///
124  /// It is suitable to use the max basis length of the matrix used to transform
125  /// the path. If the provided scale is 0, curves will revert to lines.
126  Polyline CreatePolyline(Scalar scale) const;
127 
128  std::optional<Rect> GetBoundingBox() const;
129 
130  std::optional<Rect> GetTransformedBoundingBox(const Matrix& transform) const;
131 
132  std::optional<std::pair<Point, Point>> GetMinMaxCoveragePoints() const;
133 
134  private:
135  friend class PathBuilder;
136 
137  void SetConvexity(Convexity value);
138 
139  void SetFillType(FillType fill);
140 
141  void SetBounds(Rect rect);
142 
143  Path& AddLinearComponent(Point p1, Point p2);
144 
145  Path& AddQuadraticComponent(Point p1, Point cp, Point p2);
146 
147  Path& AddCubicComponent(Point p1, Point cp1, Point cp2, Point p2);
148 
149  Path& AddContourComponent(Point destination, bool is_closed = false);
150 
151  /// @brief Called by `PathBuilder` to compute the bounds for certain paths.
152  ///
153  /// `PathBuilder` may set the bounds directly, in case they come from a source
154  /// with already computed bounds, such as an SkPath.
155  void ComputeBounds();
156 
157  void SetContourClosed(bool is_closed);
158 
159  void Shift(Point shift);
160 
161  bool UpdateLinearComponentAtIndex(size_t index,
162  const LinearPathComponent& linear);
163 
164  bool UpdateQuadraticComponentAtIndex(size_t index,
165  const QuadraticPathComponent& quadratic);
166 
167  bool UpdateCubicComponentAtIndex(size_t index, CubicPathComponent& cubic);
168 
169  bool UpdateContourComponentAtIndex(size_t index,
170  const ContourComponent& contour);
171 
172  struct ComponentIndexPair {
174  size_t index = 0;
175 
176  ComponentIndexPair() {}
177 
178  ComponentIndexPair(ComponentType a_type, size_t a_index)
179  : type(a_type), index(a_index) {}
180  };
181 
183  Convexity convexity_ = Convexity::kUnknown;
184  std::vector<ComponentIndexPair> components_;
185  std::vector<LinearPathComponent> linears_;
186  std::vector<QuadraticPathComponent> quads_;
187  std::vector<CubicPathComponent> cubics_;
188  std::vector<ContourComponent> contours_;
189 
190  std::optional<Rect> computed_bounds_;
191 };
192 
193 } // namespace impeller
impeller::Cap::kRound
@ kRound
impeller::Cap::kSquare
@ kSquare
impeller::LinearPathComponent
Definition: path_component.h:27
impeller::Path::GetQuadraticComponentAtIndex
bool GetQuadraticComponentAtIndex(size_t index, QuadraticPathComponent &quadratic) const
Definition: path.cc:172
impeller::Scalar
float Scalar
Definition: scalar.h:15
impeller::Path::PolylineContour
Definition: path.h:63
impeller::FillType::kOdd
@ kOdd
impeller::Path::ComponentType::kLinear
@ kLinear
impeller::Path::PolylineContour::start_index
size_t start_index
Index that denotes the first point of this contour.
Definition: path.h:65
impeller::FillType::kAbsGeqTwo
@ kAbsGeqTwo
impeller::Path::ComponentType::kCubic
@ kCubic
impeller::PathBuilder
Definition: path_builder.h:13
impeller::Path::GetBoundingBox
std::optional< Rect > GetBoundingBox() const
Definition: path.cc:399
impeller::Convexity
Convexity
Definition: path.h:37
impeller::Path::GetMinMaxCoveragePoints
std::optional< std::pair< Point, Point > > GetMinMaxCoveragePoints() const
Definition: path.cc:424
impeller::Path::ComponentType::kQuadratic
@ kQuadratic
impeller::Cap::kButt
@ kButt
impeller::FillType::kPositive
@ kPositive
impeller::Join::kMiter
@ kMiter
impeller::Path::~Path
~Path()
impeller::Path::Polyline
Definition: path.h:78
impeller::Convexity::kUnknown
@ kUnknown
impeller::Path::ComponentType
ComponentType
Definition: path.h:56
impeller::Path::GetCubicComponentAtIndex
bool GetCubicComponentAtIndex(size_t index, CubicPathComponent &cubic) const
Definition: path.cc:187
impeller::Path::EnumerateComponents
void EnumerateComponents(const Applier< LinearPathComponent > &linear_applier, const Applier< QuadraticPathComponent > &quad_applier, const Applier< CubicPathComponent > &cubic_applier, const Applier< ContourComponent > &contour_applier) const
Definition: path.cc:125
impeller::Path::PolylineContour::start_direction
Vector2 start_direction
The direction of the contour's start cap.
Definition: path.h:71
impeller::Path
Paths are lightweight objects that describe a collection of linear, quadratic, or cubic segments....
Definition: path.h:54
impeller::Path::GetTransformedBoundingBox
std::optional< Rect > GetTransformedBoundingBox(const Matrix &transform) const
Definition: path.cc:415
impeller::Path::Polyline::points
std::vector< Point > points
Definition: path.h:81
impeller::Path::Polyline::GetContourPointBounds
std::tuple< size_t, size_t > GetContourPointBounds(size_t contour_index) const
Definition: path.cc:20
impeller::FillType
FillType
Definition: path.h:29
impeller::Join::kRound
@ kRound
impeller::Path::PolylineContour::end_direction
Vector2 end_direction
The direction of the contour's end cap.
Definition: path.h:73
impeller::FillType::kNonZero
@ kNonZero
impeller::Path::Path
Path()
Definition: path.cc:14
impeller::Path::GetContourComponentAtIndex
bool GetContourComponentAtIndex(size_t index, ContourComponent &contour) const
Definition: path.cc:201
impeller::CubicPathComponent
Definition: path_component.h:90
impeller::Join::kBevel
@ kBevel
impeller::Join
Join
Definition: path.h:23
impeller::Path::Applier
std::function< void(size_t index, const T &component)> Applier
Definition: path.h:103
impeller::TPoint< Scalar >
impeller::Path::GetComponentCount
size_t GetComponentCount(std::optional< ComponentType > type={}) const
Definition: path.cc:32
impeller::Path::Polyline::contours
std::vector< PolylineContour > contours
Definition: path.h:82
impeller::Path::IsConvex
bool IsConvex() const
Definition: path.cc:56
impeller::FillType::kNegative
@ kNegative
path_component.h
impeller::ContourComponent
Definition: path_component.h:137
impeller::Convexity::kConvex
@ kConvex
impeller
Definition: aiks_context.cc:10
impeller::QuadraticPathComponent
Definition: path_component.h:50
impeller::Path::GetLinearComponentAtIndex
bool GetLinearComponentAtIndex(size_t index, LinearPathComponent &linear) const
Definition: path.cc:158
impeller::Path::ComponentType::kContour
@ kContour
impeller::TRect
Definition: rect.h:20
impeller::Matrix
A 4x4 matrix using column-major storage.
Definition: matrix.h:36
impeller::Path::PolylineContour::is_closed
bool is_closed
Definition: path.h:68
impeller::Cap
Cap
Definition: path.h:17
impeller::Path::GetFillType
FillType GetFillType() const
Definition: path.cc:52
impeller::Path::CreatePolyline
Polyline CreatePolyline(Scalar scale) const
Definition: path.cc:272