| 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
8 #include "impeller/geometry/path.h"
9 #include "impeller/geometry/rect.h"
10 #include "impeller/geometry/round_rect.h"
11 #include "impeller/geometry/round_superellipse.h"
12 #include "impeller/geometry/scalar.h"
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
29 ~PathBuilder();
30
31 Path CopyPath(FillType fill = FillType::kNonZero);
32
33 Path TakePath(FillType fill = FillType::kNonZero);
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
39 PathBuilder& SetConvexity(Convexity value);
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`.
99 PathBuilder& AddQuadraticCurve(const Point& p1,
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`.
119 PathBuilder& Shift(Point 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
129 PathBuilder& AddRoundRect(RoundRect rect);
130
131 PathBuilder& AddRoundSuperellipse(RoundSuperellipse rse);
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_
impeller::PathBuilder
Definition: path_builder.h:16
impeller::PathBuilder::TakePath
Path TakePath(FillType fill=FillType::kNonZero)
Definition: path_builder.cc:30
impeller::PathBuilder::~PathBuilder
~PathBuilder()
impeller::PathBuilder::AddArc
PathBuilder & AddArc(const Rect &oval_bounds, Radians start, Radians sweep, bool use_center=false)
Definition: path_builder.cc:397
impeller::PathBuilder::AddRoundRect
PathBuilder & AddRoundRect(RoundRect rect)
Definition: path_builder.cc:175
impeller::PathBuilder::LineTo
PathBuilder & LineTo(Point point, bool relative=false)
Insert a line from the current position to point.
Definition: path_builder.cc:66
impeller::PathBuilder::MoveTo
PathBuilder & MoveTo(Point point, bool relative=false)
Definition: path_builder.cc:47
impeller::PathBuilder::AddRect
PathBuilder & AddRect(const Rect &rect)
Definition: path_builder.cc:153
impeller::PathBuilder::SetBounds
PathBuilder & SetBounds(Rect bounds)
Set the bounding box that will be used by Path.GetBoundingBox in place of performing the computation.
Definition: path_builder.cc:576
impeller::PathBuilder::Reserve
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
impeller::PathBuilder::AddCubicCurve
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.
Definition: path_builder.cc:144
impeller::PathBuilder::AddOval
PathBuilder & AddOval(const Rect &rect)
Definition: path_builder.cc:455
impeller::PathBuilder::AddCircle
PathBuilder & AddCircle(const Point &center, Scalar radius)
Definition: path_builder.cc:171
impeller::PathBuilder::Close
PathBuilder & Close()
Definition: path_builder.cc:54
impeller::PathBuilder::PathBuilder
PathBuilder()
Definition: path_builder.cc:15
impeller::PathBuilder::AddPath
PathBuilder & AddPath(const Path &path)
Definition: path_builder.cc:509
impeller::PathBuilder::AddConicCurve
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...
Definition: path_builder.cc:135
impeller::PathBuilder::CopyPath
Path CopyPath(FillType fill=FillType::kNonZero)
Definition: path_builder.cc:21
impeller::PathBuilder::VerticalLineTo
PathBuilder & VerticalLineTo(Scalar y, bool relative=false)
Definition: path_builder.cc:81
impeller::PathBuilder::Shift
PathBuilder & Shift(Point offset)
Transform the existing path segments and contours by the given offset.
Definition: path_builder.cc:529
impeller::PathBuilder::AddLine
PathBuilder & AddLine(const Point &p1, const Point &p2)
Move to point p1, then insert a line from p1 to p2.
Definition: path_builder.cc:503
impeller::PathBuilder::kArcApproximationMagic
constexpr static const Scalar kArcApproximationMagic
Definition: path_builder.h:25
impeller::PathBuilder::CubicCurveTo
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...
Definition: path_builder.cc:115
impeller::PathBuilder::ConicCurveTo
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
impeller::PathBuilder::AddQuadraticCurve
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.
Definition: path_builder.cc:127
impeller::PathBuilder::HorizontalLineTo
PathBuilder & HorizontalLineTo(Scalar x, bool relative=false)
Definition: path_builder.cc:73
impeller::PathBuilder::AddRoundSuperellipse
PathBuilder & AddRoundSuperellipse(RoundSuperellipse rse)
Definition: path_builder.cc:246
impeller::PathBuilder::SetConvexity
PathBuilder & SetConvexity(Convexity value)
Definition: path_builder.cc:110
impeller::PathBuilder::QuadraticCurveTo
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
impeller::Path
Paths are lightweight objects that describe a collection of linear, quadratic, or cubic segments....
Definition: path.h:54
value
int32_t value
Definition: dl_golden_unittests.cc:459
x
int32_t x
Definition: dl_golden_unittests.cc:458
path.h
impeller
Definition: allocation.cc:12
impeller::Scalar
float Scalar
Definition: scalar.h:18
impeller::FillType
FillType
Definition: path.h:32
impeller::FillType::kNonZero
@ kNonZero
impeller::Convexity
Convexity
Definition: path.h:37
rect.h
round_rect.h
round_superellipse.h
scalar.h
offset
SeparatedVector2 offset
Definition: stroke_path_geometry.cc:322
impeller::Radians
Definition: scalar.h:43
impeller::RoundRect
Definition: round_rect.h:15
impeller::RoundSuperellipse
Definition: round_superellipse.h:17
impeller::RoundingRadii
Definition: rounding_radii.h:14
impeller::TPoint< Scalar >
impeller::TRect< Scalar >