Flutter Impeller
path_builder.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 #ifndef FLUTTER_IMPELLER_GEOMETRY_PATH_BUILDER_H_
6 #define FLUTTER_IMPELLER_GEOMETRY_PATH_BUILDER_H_
7 
13 
14 namespace impeller {
15 
16 class PathBuilder {
17  public:
18  /// Used for approximating quarter circle arcs with cubic curves. This is the
19  /// control point distance which results in the smallest possible unit circle
20  /// integration for a right angle arc. It can be used to approximate arcs less
21  /// than 90 degrees to great effect by simply reducing it proportionally to
22  /// the angle. However, accuracy rapidly diminishes if magnified for obtuse
23  /// angle arcs, and so multiple cubic curves should be used when approximating
24  /// arcs greater than 90 degrees.
25  constexpr static const Scalar kArcApproximationMagic = 0.551915024494f;
26 
27  PathBuilder();
28 
30 
32 
34 
35  /// @brief Reserve [point_size] points and [verb_size] verbs in the underlying
36  /// path buffer.
37  void Reserve(size_t point_size, size_t verb_size);
38 
40 
41  PathBuilder& MoveTo(Point point, bool relative = false);
42 
43  PathBuilder& Close();
44 
45  /// @brief Insert a line from the current position to `point`.
46  ///
47  /// If `relative` is true, then `point` is relative to the current location.
48  PathBuilder& LineTo(Point point, bool relative = false);
49 
50  PathBuilder& HorizontalLineTo(Scalar x, bool relative = false);
51 
52  PathBuilder& VerticalLineTo(Scalar y, bool relative = false);
53 
54  /// @brief Insert a quadradic curve from the current position to `point` using
55  /// the control point `controlPoint`.
56  ///
57  /// If `relative` is true the `point` and `controlPoint` are relative to
58  /// current location.
59  PathBuilder& QuadraticCurveTo(Point controlPoint,
60  Point point,
61  bool relative = false);
62 
63  /// @brief Insert a conic curve from the current position to `point` using
64  /// the control point `controlPoint` and the weight `weight`.
65  ///
66  /// If `relative` is true the `point` and `controlPoint` are relative to
67  /// current location.
68  PathBuilder& ConicCurveTo(Point controlPoint,
69  Point point,
70  Scalar weight,
71  bool relative = false);
72 
73  /// @brief Insert a cubic curve from the curren position to `point` using the
74  /// control points `controlPoint1` and `controlPoint2`.
75  ///
76  /// If `relative` is true the `point`, `controlPoint1`, and `controlPoint2`
77  /// are relative to current location.
78  PathBuilder& CubicCurveTo(Point controlPoint1,
79  Point controlPoint2,
80  Point point,
81  bool relative = false);
82 
83  PathBuilder& AddRect(const Rect& rect);
84 
85  PathBuilder& AddCircle(const Point& center, Scalar radius);
86 
87  PathBuilder& AddArc(const Rect& oval_bounds,
88  Radians start,
89  Radians sweep,
90  bool use_center = false);
91 
92  PathBuilder& AddOval(const Rect& rect);
93 
94  /// @brief Move to point `p1`, then insert a line from `p1` to `p2`.
95  PathBuilder& AddLine(const Point& p1, const Point& p2);
96 
97  /// @brief Move to point `p1`, then insert a quadradic curve from `p1` to `p2`
98  /// with the control point `cp`.
100  const Point& cp,
101  const Point& p2);
102 
103  /// @brief Move to point `p1`, then insert a conic curve from `p1` to `p2`
104  /// with the control point `cp` and weight `weight`.
105  PathBuilder& AddConicCurve(const Point& p1,
106  const Point& cp,
107  const Point& p2,
108  Scalar weight);
109 
110  /// @brief Move to point `p1`, then insert a cubic curve from `p1` to `p2`
111  /// with control points `cp1` and `cp2`.
112  PathBuilder& AddCubicCurve(const Point& p1,
113  const Point& cp1,
114  const Point& cp2,
115  const Point& p2);
116 
117  /// @brief Transform the existing path segments and contours by the given
118  /// `offset`.
120 
121  /// @brief Set the bounding box that will be used by `Path.GetBoundingBox` in
122  /// place of performing the computation.
123  ///
124  /// When Impeller recieves Skia Path objects, many of these already
125  /// have computed bounds. This method is used to avoid needlessly
126  /// recomputing these bounds.
127  PathBuilder& SetBounds(Rect bounds);
128 
130 
132 
133  PathBuilder& AddPath(const Path& path);
134 
135  private:
136  Point subpath_start_;
137  Point current_;
138  size_t current_contour_location_ = 0u;
139  size_t contour_count_ = 0u;
140  Path::Data prototype_;
141 
142  PathBuilder& AddRoundedRectTopLeft(Rect rect, RoundingRadii radii);
143 
144  PathBuilder& AddRoundedRectTopRight(Rect rect, RoundingRadii radii);
145 
146  PathBuilder& AddRoundedRectBottomRight(Rect rect, RoundingRadii radii);
147 
148  PathBuilder& AddRoundedRectBottomLeft(Rect rect, RoundingRadii radii);
149 
150  void AddContourComponent(const Point& destination, bool is_closed = false);
151 
152  void SetContourClosed(bool is_closed);
153 
154  void AddLinearComponent(const Point& p1, const Point& p2);
155 
156  void AddLinearComponentIfNeeded(const Point& p1, const Point& p2);
157 
158  void AddQuadraticComponent(const Point& p1, const Point& cp, const Point& p2);
159 
160  void AddConicComponent(const Point& p1,
161  const Point& cp,
162  const Point& p2,
163  Scalar weight);
164 
165  void AddCubicComponent(const Point& p1,
166  const Point& cp1,
167  const Point& cp2,
168  const Point& p2);
169 
170  /// Compute the bounds of the path unless they are already computed or
171  /// set by an external source, such as |SetBounds|. Any call which mutates
172  /// the path data can invalidate the computed/set bounds.
173  void UpdateBounds();
174 
175  std::optional<std::pair<Point, Point>> GetMinMaxCoveragePoints() const;
176 
177  PathBuilder(const PathBuilder&) = delete;
178  PathBuilder& operator=(const PathBuilder&&) = delete;
179 };
180 
181 } // namespace impeller
182 
183 #endif // FLUTTER_IMPELLER_GEOMETRY_PATH_BUILDER_H_
Path TakePath(FillType fill=FillType::kNonZero)
Definition: path_builder.cc:30
PathBuilder & AddArc(const Rect &oval_bounds, Radians start, Radians sweep, bool use_center=false)
PathBuilder & AddRoundRect(RoundRect rect)
PathBuilder & LineTo(Point point, bool relative=false)
Insert a line from the current position to point.
Definition: path_builder.cc:66
PathBuilder & MoveTo(Point point, bool relative=false)
Definition: path_builder.cc:47
PathBuilder & AddRect(const Rect &rect)
PathBuilder & SetBounds(Rect bounds)
Set the bounding box that will be used by Path.GetBoundingBox in place of performing the computation.
void Reserve(size_t point_size, size_t verb_size)
Reserve [point_size] points and [verb_size] verbs in the underlying path buffer.
Definition: path_builder.cc:42
PathBuilder & AddCubicCurve(const Point &p1, const Point &cp1, const Point &cp2, const Point &p2)
Move to point p1, then insert a cubic curve from p1 to p2 with control points cp1 and cp2.
PathBuilder & AddOval(const Rect &rect)
PathBuilder & AddCircle(const Point &center, Scalar radius)
PathBuilder & Close()
Definition: path_builder.cc:54
PathBuilder & AddPath(const Path &path)
PathBuilder & AddConicCurve(const Point &p1, const Point &cp, const Point &p2, Scalar weight)
Move to point p1, then insert a conic curve from p1 to p2 with the control point cp and weight weight...
Path CopyPath(FillType fill=FillType::kNonZero)
Definition: path_builder.cc:21
PathBuilder & VerticalLineTo(Scalar y, bool relative=false)
Definition: path_builder.cc:81
PathBuilder & Shift(Point offset)
Transform the existing path segments and contours by the given offset.
PathBuilder & AddLine(const Point &p1, const Point &p2)
Move to point p1, then insert a line from p1 to p2.
constexpr static const Scalar kArcApproximationMagic
Definition: path_builder.h:25
PathBuilder & CubicCurveTo(Point controlPoint1, Point controlPoint2, Point point, bool relative=false)
Insert a cubic curve from the curren position to point using the control points controlPoint1 and con...
PathBuilder & ConicCurveTo(Point controlPoint, Point point, Scalar weight, bool relative=false)
Insert a conic curve from the current position to point using the control point controlPoint and the ...
Definition: path_builder.cc:99
PathBuilder & AddQuadraticCurve(const Point &p1, const Point &cp, const Point &p2)
Move to point p1, then insert a quadradic curve from p1 to p2 with the control point cp.
PathBuilder & HorizontalLineTo(Scalar x, bool relative=false)
Definition: path_builder.cc:73
PathBuilder & AddRoundSuperellipse(RoundSuperellipse rse)
PathBuilder & SetConvexity(Convexity value)
PathBuilder & QuadraticCurveTo(Point controlPoint, Point point, bool relative=false)
Insert a quadradic curve from the current position to point using the control point controlPoint.
Definition: path_builder.cc:89
Paths are lightweight objects that describe a collection of linear, quadratic, or cubic segments....
Definition: path.h:54
int32_t value
int32_t x
float Scalar
Definition: scalar.h:18
FillType
Definition: path.h:32
Convexity
Definition: path.h:37
SeparatedVector2 offset