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 #pragma once
6 
10 
11 namespace impeller {
12 
13 class PathBuilder {
14  public:
15  /// Used for approximating quarter circle arcs with cubic curves. This is the
16  /// control point distance which results in the smallest possible unit circle
17  /// integration for a right angle arc. It can be used to approximate arcs less
18  /// than 90 degrees to great effect by simply reducing it proportionally to
19  /// the angle. However, accuracy rapidly diminishes if magnified for obtuse
20  /// angle arcs, and so multiple cubic curves should be used when approximating
21  /// arcs greater than 90 degrees.
22  constexpr static const Scalar kArcApproximationMagic = 0.551915024494f;
23 
24  PathBuilder();
25 
26  ~PathBuilder();
27 
29 
31 
32  const Path& GetCurrentPath() const;
33 
35 
36  PathBuilder& MoveTo(Point point, bool relative = false);
37 
38  PathBuilder& Close();
39 
40  /// @brief Insert a line from the current position to `point`.
41  ///
42  /// If `relative` is true, then `point` is relative to the current location.
43  PathBuilder& LineTo(Point point, bool relative = false);
44 
45  PathBuilder& HorizontalLineTo(Scalar x, bool relative = false);
46 
47  PathBuilder& VerticalLineTo(Scalar y, bool relative = false);
48 
49  /// @brief Insert a quadradic curve from the current position to `point` using
50  /// the control point `controlPoint`.
51  ///
52  /// If `relative` is true the `point` and `controlPoint` are relative to
53  /// current location.
54  PathBuilder& QuadraticCurveTo(Point controlPoint,
55  Point point,
56  bool relative = false);
57 
58  PathBuilder& SmoothQuadraticCurveTo(Point point, bool relative = false);
59 
60  /// @brief Insert a cubic curve from the curren position to `point` using the
61  /// control points `controlPoint1` and `controlPoint2`.
62  ///
63  /// If `relative` is true the `point`, `controlPoint1`, and `controlPoint2`
64  /// are relative to current location.
65  PathBuilder& CubicCurveTo(Point controlPoint1,
66  Point controlPoint2,
67  Point point,
68  bool relative = false);
69 
70  PathBuilder& SmoothCubicCurveTo(Point controlPoint2,
71  Point point,
72  bool relative = false);
73 
74  PathBuilder& AddRect(Rect rect);
75 
76  PathBuilder& AddCircle(const Point& center, Scalar radius);
77 
78  PathBuilder& AddArc(const Rect& oval_bounds,
79  Radians start,
80  Radians sweep,
81  bool use_center = false);
82 
83  PathBuilder& AddOval(const Rect& rect);
84 
85  /// @brief Move to point `p1`, then insert a line from `p1` to `p2`.
86  PathBuilder& AddLine(const Point& p1, const Point& p2);
87 
88  /// @brief Move to point `p1`, then insert a quadradic curve from `p1` to `p2`
89  /// with the control point `cp`.
91 
92  /// @brief Move to point `p1`, then insert a cubic curve from `p1` to `p2`
93  /// with control points `cp1` and `cp2`.
94  PathBuilder& AddCubicCurve(Point p1, Point cp1, Point cp2, Point p2);
95 
96  /// @brief Transform the existing path segments and contours by the given
97  /// `offset`.
98  PathBuilder& Shift(Point offset);
99 
100  /// @brief Set the bounding box that will be used by `Path.GetBoundingBox` in
101  /// place of performing the computation.
102  ///
103  /// When Impeller recieves Skia Path objects, many of these already
104  /// have computed bounds. This method is used to avoid needlessly
105  /// recomputing these bounds.
106  PathBuilder& SetBounds(Rect bounds);
107 
108  struct RoundingRadii {
113 
114  RoundingRadii() = default;
115 
116  RoundingRadii(Scalar p_top_left,
117  Scalar p_bottom_left,
118  Scalar p_top_right,
119  Scalar p_bottom_right)
120  : top_left(p_top_left, p_top_left),
121  bottom_left(p_bottom_left, p_bottom_left),
122  top_right(p_top_right, p_top_right),
123  bottom_right(p_bottom_right, p_bottom_right) {}
124 
125  bool AreAllZero() const {
126  return top_left.IsZero() && //
127  bottom_left.IsZero() && //
128  top_right.IsZero() && //
130  }
131  };
132 
133  PathBuilder& AddRoundedRect(Rect rect, RoundingRadii radii);
134 
135  PathBuilder& AddRoundedRect(Rect rect, Scalar radius);
136 
137  PathBuilder& AddPath(const Path& path);
138 
139  private:
140  Point subpath_start_;
141  Point current_;
142  Path prototype_;
143  Convexity convexity_;
144  bool did_compute_bounds_ = false;
145 
146  PathBuilder& AddRoundedRectTopLeft(Rect rect, RoundingRadii radii);
147 
148  PathBuilder& AddRoundedRectTopRight(Rect rect, RoundingRadii radii);
149 
150  PathBuilder& AddRoundedRectBottomRight(Rect rect, RoundingRadii radii);
151 
152  PathBuilder& AddRoundedRectBottomLeft(Rect rect, RoundingRadii radii);
153 
154  Point ReflectedQuadraticControlPoint1() const;
155 
156  Point ReflectedCubicControlPoint1() const;
157 
158  PathBuilder(const PathBuilder&) = delete;
159  PathBuilder& operator=(const PathBuilder&&) = delete;
160 };
161 
162 } // namespace impeller
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:166
impeller::Scalar
float Scalar
Definition: scalar.h:15
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:465
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:116
impeller::PathBuilder::AddPath
PathBuilder & AddPath(const Path &path)
Definition: path_builder.cc:443
impeller::PathBuilder
Definition: path_builder.h:13
impeller::Convexity
Convexity
Definition: path.h:37
impeller::PathBuilder::RoundingRadii::AreAllZero
bool AreAllZero() const
Definition: path_builder.h:125
impeller::PathBuilder::AddRoundedRect
PathBuilder & AddRoundedRect(Rect rect, RoundingRadii radii)
Definition: path_builder.cc:207
impeller::PathBuilder::HorizontalLineTo
PathBuilder & HorizontalLineTo(Scalar x, bool relative=false)
Definition: path_builder.cc:53
impeller::PathBuilder::~PathBuilder
~PathBuilder()
impeller::PathBuilder::SetConvexity
PathBuilder & SetConvexity(Convexity value)
Definition: path_builder.cc:111
impeller::PathBuilder::kArcApproximationMagic
constexpr static const Scalar kArcApproximationMagic
Definition: path_builder.h:22
impeller::PathBuilder::SmoothQuadraticCurveTo
PathBuilder & SmoothQuadraticCurveTo(Point point, bool relative=false)
Definition: path_builder.cc:101
impeller::PathBuilder::AddRect
PathBuilder & AddRect(Rect rect)
Definition: path_builder.cc:181
impeller::PathBuilder::RoundingRadii::bottom_right
Point bottom_right
Definition: path_builder.h:112
impeller::TPoint::IsZero
constexpr bool IsZero() const
Definition: point.h:227
impeller::PathBuilder::RoundingRadii
Definition: path_builder.h:108
impeller::PathBuilder::SmoothCubicCurveTo
PathBuilder & SmoothCubicCurveTo(Point controlPoint2, Point point, bool relative=false)
Definition: path_builder.cc:151
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:116
impeller::PathBuilder::Shift
PathBuilder & Shift(Point offset)
Transform the existing path segments and contours by the given offset.
Definition: path_builder.cc:460
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:69
impeller::Path
Paths are lightweight objects that describe a collection of linear, quadratic, or cubic segments....
Definition: path.h:54
impeller::PathBuilder::LineTo
PathBuilder & LineTo(Point point, bool relative=false)
Insert a line from the current position to point.
Definition: path_builder.cc:46
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:172
impeller::PathBuilder::RoundingRadii::top_left
Point top_left
Definition: path_builder.h:109
impeller::Radians
Definition: scalar.h:35
impeller::FillType
FillType
Definition: path.h:29
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:433
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::GetCurrentPath
const Path & GetCurrentPath() const
Definition: path_builder.cc:439
impeller::PathBuilder::Close
PathBuilder & Close()
Definition: path_builder.cc:39
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:111
rect.h
impeller::TPoint< Scalar >
impeller::PathBuilder::MoveTo
PathBuilder & MoveTo(Point point, bool relative=false)
Definition: path_builder.cc:32
impeller::PathBuilder::RoundingRadii::bottom_left
Point bottom_left
Definition: path_builder.h:110
impeller::PathBuilder::VerticalLineTo
PathBuilder & VerticalLineTo(Scalar y, bool relative=false)
Definition: path_builder.cc:61
impeller::PathBuilder::AddCircle
PathBuilder & AddCircle(const Point &center, Scalar radius)
Definition: path_builder.cc:198
impeller::PathBuilder::AddOval
PathBuilder & AddOval(const Rect &rect)
Definition: path_builder.cc:385
impeller
Definition: aiks_context.cc:10
impeller::TRect< Scalar >
impeller::PathBuilder::AddArc
PathBuilder & AddArc(const Rect &oval_bounds, Radians start, Radians sweep, bool use_center=false)
Definition: path_builder.cc:325