1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3  * You can obtain one at http://mozilla.org/MPL/2.0/. */
4 
5 #ifndef CanvasPath_h
6 #define CanvasPath_h
7 
8 #include "mozilla/Attributes.h"
9 #include "mozilla/RefPtr.h"
10 #include "nsWrapperCache.h"
11 #include "mozilla/gfx/2D.h"
12 #include "mozilla/dom/BindingDeclarations.h"
13 #include "mozilla/ErrorResult.h"
14 
15 namespace mozilla {
16 namespace dom {
17 
18 enum class CanvasWindingRule : uint8_t;
19 struct DOMMatrix2DInit;
20 
21 class CanvasPath final : public nsWrapperCache {
22  public:
NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(CanvasPath)23   NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(CanvasPath)
24   NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_NATIVE_CLASS(CanvasPath)
25 
26   nsCOMPtr<nsISupports> GetParentObject() { return mParent; }
27 
28   JSObject* WrapObject(JSContext* aCx,
29                        JS::Handle<JSObject*> aGivenProto) override;
30 
31   static already_AddRefed<CanvasPath> Constructor(const GlobalObject& aGlobal);
32   static already_AddRefed<CanvasPath> Constructor(const GlobalObject& aGlobal,
33                                                   CanvasPath& aCanvasPath);
34   static already_AddRefed<CanvasPath> Constructor(const GlobalObject& aGlobal,
35                                                   const nsAString& aPathString);
36 
37   void ClosePath();
38   void MoveTo(double x, double y);
39   void LineTo(double x, double y);
40   void QuadraticCurveTo(double cpx, double cpy, double x, double y);
41   void BezierCurveTo(double cp1x, double cp1y, double cp2x, double cp2y,
42                      double x, double y);
43   void ArcTo(double x1, double y1, double x2, double y2, double radius,
44              ErrorResult& error);
45   void Rect(double x, double y, double w, double h);
46   void Arc(double x, double y, double radius, double startAngle,
47            double endAngle, bool anticlockwise, ErrorResult& error);
48   void Ellipse(double x, double y, double radiusX, double radiusY,
49                double rotation, double startAngle, double endAngle,
50                bool anticlockwise, ErrorResult& error);
51 
52   void LineTo(const gfx::Point& aPoint);
53   void BezierTo(const gfx::Point& aCP1, const gfx::Point& aCP2,
54                 const gfx::Point& aCP3);
55 
56   already_AddRefed<gfx::Path> GetPath(const CanvasWindingRule& aWinding,
57                                       const gfx::DrawTarget* aTarget) const;
58 
59   explicit CanvasPath(nsISupports* aParent);
60   // already_AddRefed arg because the return value from Path::CopyToBuilder()
61   // is passed directly and we can't drop the only ref to have a raw pointer.
62   CanvasPath(nsISupports* aParent,
63              already_AddRefed<gfx::PathBuilder> aPathBuilder);
64 
65   void AddPath(CanvasPath& aCanvasPath, const DOMMatrix2DInit& aInit,
66                ErrorResult& aError);
67 
68  private:
69   virtual ~CanvasPath() = default;
70 
71   nsCOMPtr<nsISupports> mParent;
ToFloat(double aValue)72   static gfx::Float ToFloat(double aValue) { return gfx::Float(aValue); }
73 
74   mutable RefPtr<gfx::Path> mPath;
75   mutable RefPtr<gfx::PathBuilder> mPathBuilder;
76 
77   void EnsurePathBuilder() const;
78 };
79 
80 }  // namespace dom
81 }  // namespace mozilla
82 
83 #endif /* CanvasPath_h */
84