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 
11 
12 namespace impeller {
13 
14 class PathBuilder {
15  public:
16  /// Used for approximating quarter circle arcs with cubic curves. This is the
17  /// control point distance which results in the smallest possible unit circle
18  /// integration for a right angle arc. It can be used to approximate arcs less
19  /// than 90 degrees to great effect by simply reducing it proportionally to
20  /// the angle. However, accuracy rapidly diminishes if magnified for obtuse
21  /// angle arcs, and so multiple cubic curves should be used when approximating
22  /// arcs greater than 90 degrees.
23  constexpr static const Scalar kArcApproximationMagic = 0.551915024494f;
24 
25  PathBuilder();
26 
27  ~PathBuilder();
28 
30 
32 
33  /// @brief Reserve [point_size] points and [verb_size] verbs in the underlying
34  /// path buffer.
35  void Reserve(size_t point_size, size_t verb_size);
36 
37  const Path& GetCurrentPath() const;
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 cubic curve from the curren position to `point` using the
64  /// control points `controlPoint1` and `controlPoint2`.
65  ///
66  /// If `relative` is true the `point`, `controlPoint1`, and `controlPoint2`
67  /// are relative to current location.
68  PathBuilder& CubicCurveTo(Point controlPoint1,
69  Point controlPoint2,
70  Point point,
71  bool relative = false);
72 
73  PathBuilder& AddRect(Rect rect);
74 
75  PathBuilder& AddCircle(const Point& center, Scalar radius);
76 
77  PathBuilder& AddArc(const Rect& oval_bounds,
78  Radians start,
79  Radians sweep,
80  bool use_center = false);
81 
82  PathBuilder& AddOval(const Rect& rect);
83 
84  /// @brief Move to point `p1`, then insert a line from `p1` to `p2`.
85  PathBuilder& AddLine(const Point& p1, const Point& p2);
86 
87  /// @brief Move to point `p1`, then insert a quadradic curve from `p1` to `p2`
88  /// with the control point `cp`.
90 
91  /// @brief Move to point `p1`, then insert a cubic curve from `p1` to `p2`
92  /// with control points `cp1` and `cp2`.
93  PathBuilder& AddCubicCurve(Point p1, Point cp1, Point cp2, Point p2);
94 
95  /// @brief Transform the existing path segments and contours by the given
96  /// `offset`.
97  PathBuilder& Shift(Point offset);
98 
99  /// @brief Set the bounding box that will be used by `Path.GetBoundingBox` in
100  /// place of performing the computation.
101  ///
102  /// When Impeller recieves Skia Path objects, many of these already
103  /// have computed bounds. This method is used to avoid needlessly
104  /// recomputing these bounds.
105  PathBuilder& SetBounds(Rect bounds);
106 
107  struct RoundingRadii {
112 
113  RoundingRadii() = default;
114 
115  RoundingRadii(Scalar p_top_left,
116  Scalar p_bottom_left,
117  Scalar p_top_right,
118  Scalar p_bottom_right)
119  : top_left(p_top_left, p_top_left),
120  bottom_left(p_bottom_left, p_bottom_left),
121  top_right(p_top_right, p_top_right),
122  bottom_right(p_bottom_right, p_bottom_right) {}
123 
124  explicit RoundingRadii(Scalar radius)
125  : top_left(radius, radius),
126  bottom_left(radius, radius),
127  top_right(radius, radius),
128  bottom_right(radius, radius) {}
129 
130  explicit RoundingRadii(Point radii)
131  : top_left(radii),
132  bottom_left(radii),
133  top_right(radii),
134  bottom_right(radii) {}
135 
136  explicit RoundingRadii(Size radii)
137  : top_left(radii),
138  bottom_left(radii),
139  top_right(radii),
140  bottom_right(radii) {}
141 
142  bool AreAllZero() const {
143  return top_left.IsZero() && //
144  bottom_left.IsZero() && //
145  top_right.IsZero() && //
147  }
148  };
149 
150  PathBuilder& AddRoundedRect(Rect rect, RoundingRadii radii);
151 
152  PathBuilder& AddRoundedRect(Rect rect, Size radii);
153 
154  PathBuilder& AddRoundedRect(Rect rect, Scalar radius);
155 
156  PathBuilder& AddPath(const Path& path);
157 
158  private:
159  Point subpath_start_;
160  Point current_;
161  Path prototype_;
162  Convexity convexity_;
163  bool did_compute_bounds_ = false;
164 
165  PathBuilder& AddRoundedRectTopLeft(Rect rect, RoundingRadii radii);
166 
167  PathBuilder& AddRoundedRectTopRight(Rect rect, RoundingRadii radii);
168 
169  PathBuilder& AddRoundedRectBottomRight(Rect rect, RoundingRadii radii);
170 
171  PathBuilder& AddRoundedRectBottomLeft(Rect rect, RoundingRadii radii);
172 
173  PathBuilder(const PathBuilder&) = delete;
174  PathBuilder& operator=(const PathBuilder&&) = delete;
175 };
176 
177 } // namespace impeller
178 
179 #endif // FLUTTER_IMPELLER_GEOMETRY_PATH_BUILDER_H_
path.h
impeller::PathBuilder::AddQuadraticCurve
PathBuilder & AddQuadraticCurve(Point p1, Point cp, Point p2)
Move to point p1, then insert a quadradic curve from p1 to p2 with the control point cp.
Definition: path_builder.cc:101
impeller::Scalar
float Scalar
Definition: scalar.h:18
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:402
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:89
impeller::PathBuilder::AddPath
PathBuilder & AddPath(const Path &path)
Definition: path_builder.cc:380
impeller::PathBuilder
Definition: path_builder.h:14
impeller::Convexity
Convexity
Definition: path.h:38
impeller::PathBuilder::RoundingRadii::AreAllZero
bool AreAllZero() const
Definition: path_builder.h:142
impeller::PathBuilder::AddRoundedRect
PathBuilder & AddRoundedRect(Rect rect, RoundingRadii radii)
Definition: path_builder.cc:149
impeller::PathBuilder::HorizontalLineTo
PathBuilder & HorizontalLineTo(Scalar x, bool relative=false)
Definition: path_builder.cc:58
impeller::PathBuilder::~PathBuilder
~PathBuilder()
impeller::PathBuilder::SetConvexity
PathBuilder & SetConvexity(Convexity value)
Definition: path_builder.cc:84
impeller::PathBuilder::kArcApproximationMagic
constexpr static const Scalar kArcApproximationMagic
Definition: path_builder.h:23
impeller::PathBuilder::AddRect
PathBuilder & AddRect(Rect rect)
Definition: path_builder.cc:116
impeller::PathBuilder::RoundingRadii::bottom_right
Point bottom_right
Definition: path_builder.h:111
impeller::TPoint::IsZero
constexpr bool IsZero() const
Definition: point.h:229
impeller::PathBuilder::RoundingRadii
Definition: path_builder.h:107
impeller::TSize< Scalar >
impeller::PathBuilder::RoundingRadii::RoundingRadii
RoundingRadii()=default
impeller::PathBuilder::RoundingRadii::RoundingRadii
RoundingRadii(Scalar p_top_left, Scalar p_bottom_left, Scalar p_top_right, Scalar p_bottom_right)
Definition: path_builder.h:115
impeller::PathBuilder::Shift
PathBuilder & Shift(Point offset)
Transform the existing path segments and contours by the given offset.
Definition: path_builder.cc:397
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:74
impeller::Path
Paths are lightweight objects that describe a collection of linear, quadratic, or cubic segments....
Definition: path.h:55
impeller::PathBuilder::LineTo
PathBuilder & LineTo(Point point, bool relative=false)
Insert a line from the current position to point.
Definition: path_builder.cc:51
impeller::PathBuilder::AddCubicCurve
PathBuilder & AddCubicCurve(Point p1, Point cp1, Point cp2, 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:107
impeller::PathBuilder::RoundingRadii::top_left
Point top_left
Definition: path_builder.h:108
impeller::Radians
Definition: scalar.h:38
impeller::FillType
FillType
Definition: path.h:30
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:370
impeller::PathBuilder::TakePath
Path TakePath(FillType fill=FillType::kNonZero)
Definition: path_builder.cc:21
impeller::FillType::kNonZero
@ kNonZero
impeller::PathBuilder::PathBuilder
PathBuilder()
scalar.h
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:32
impeller::PathBuilder::GetCurrentPath
const Path & GetCurrentPath() const
Definition: path_builder.cc:376
impeller::PathBuilder::RoundingRadii::RoundingRadii
RoundingRadii(Size radii)
Definition: path_builder.h:136
impeller::PathBuilder::Close
PathBuilder & Close()
Definition: path_builder.cc:44
impeller::PathBuilder::CopyPath
Path CopyPath(FillType fill=FillType::kNonZero) const
Definition: path_builder.cc:15
impeller::PathBuilder::RoundingRadii::top_right
Point top_right
Definition: path_builder.h:110
rect.h
impeller::TPoint< Scalar >
impeller::PathBuilder::MoveTo
PathBuilder & MoveTo(Point point, bool relative=false)
Definition: path_builder.cc:37
impeller::PathBuilder::RoundingRadii::RoundingRadii
RoundingRadii(Scalar radius)
Definition: path_builder.h:124
impeller::PathBuilder::RoundingRadii::bottom_left
Point bottom_left
Definition: path_builder.h:109
impeller::PathBuilder::VerticalLineTo
PathBuilder & VerticalLineTo(Scalar y, bool relative=false)
Definition: path_builder.cc:66
impeller::PathBuilder::AddCircle
PathBuilder & AddCircle(const Point &center, Scalar radius)
Definition: path_builder.cc:134
impeller::PathBuilder::AddOval
PathBuilder & AddOval(const Rect &rect)
Definition: path_builder.cc:322
impeller
Definition: aiks_context.cc:10
impeller::PathBuilder::RoundingRadii::RoundingRadii
RoundingRadii(Point radii)
Definition: path_builder.h:130
impeller::TRect< Scalar >
impeller::PathBuilder::AddArc
PathBuilder & AddArc(const Rect &oval_bounds, Radians start, Radians sweep, bool use_center=false)
Definition: path_builder.cc:264