1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "mozilla/dom/DOMQuad.h"
8 #include "mozilla/dom/DOMQuadBinding.h"
9 #include "mozilla/dom/DOMPoint.h"
10 #include "mozilla/dom/DOMRect.h"
13 using namespace mozilla
;
14 using namespace mozilla::dom
;
15 using namespace mozilla::gfx
;
17 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(DOMQuad
, mParent
, mBounds
, mPoints
[0],
18 mPoints
[1], mPoints
[2], mPoints
[3])
20 NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(DOMQuad
, AddRef
)
21 NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(DOMQuad
, Release
)
23 DOMQuad::DOMQuad(nsISupports
* aParent
, CSSPoint aPoints
[4])
26 for (uint32_t i
= 0; i
< 4; ++i
) {
27 mPoints
[i
] = new DOMPoint(aParent
, aPoints
[i
].x
, aPoints
[i
].y
);
31 DOMQuad::DOMQuad(nsISupports
* aParent
)
41 DOMQuad::WrapObject(JSContext
* aCx
)
43 return DOMQuadBinding::Wrap(aCx
, this);
46 already_AddRefed
<DOMQuad
>
47 DOMQuad::Constructor(const GlobalObject
& aGlobal
,
48 const DOMPointInit
& aP1
,
49 const DOMPointInit
& aP2
,
50 const DOMPointInit
& aP3
,
51 const DOMPointInit
& aP4
,
54 nsRefPtr
<DOMQuad
> obj
= new DOMQuad(aGlobal
.GetAsSupports());
55 obj
->mPoints
[0] = DOMPoint::Constructor(aGlobal
, aP1
, aRV
);
56 obj
->mPoints
[1] = DOMPoint::Constructor(aGlobal
, aP2
, aRV
);
57 obj
->mPoints
[2] = DOMPoint::Constructor(aGlobal
, aP3
, aRV
);
58 obj
->mPoints
[3] = DOMPoint::Constructor(aGlobal
, aP4
, aRV
);
62 already_AddRefed
<DOMQuad
>
63 DOMQuad::Constructor(const GlobalObject
& aGlobal
, const DOMRectReadOnly
& aRect
,
67 Float x
= aRect
.X(), y
= aRect
.Y(), w
= aRect
.Width(), h
= aRect
.Height();
68 points
[0] = CSSPoint(x
, y
);
69 points
[1] = CSSPoint(x
+ w
, y
);
70 points
[2] = CSSPoint(x
+ w
, y
+ h
);
71 points
[3] = CSSPoint(x
, y
+ h
);
72 nsRefPtr
<DOMQuad
> obj
= new DOMQuad(aGlobal
.GetAsSupports(), points
);
76 class DOMQuad::QuadBounds MOZ_FINAL
: public DOMRectReadOnly
79 explicit QuadBounds(DOMQuad
* aQuad
)
80 : DOMRectReadOnly(aQuad
->GetParentObject())
84 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(QuadBounds
, DOMRectReadOnly
)
85 NS_DECL_ISUPPORTS_INHERITED
87 virtual double X() const MOZ_OVERRIDE
90 GetHorizontalMinMax(&x1
, &x2
);
93 virtual double Y() const MOZ_OVERRIDE
96 GetVerticalMinMax(&y1
, &y2
);
99 virtual double Width() const MOZ_OVERRIDE
102 GetHorizontalMinMax(&x1
, &x2
);
105 virtual double Height() const MOZ_OVERRIDE
108 GetVerticalMinMax(&y1
, &y2
);
112 void GetHorizontalMinMax(double* aX1
, double* aX2
) const
115 x1
= x2
= mQuad
->Point(0)->X();
116 for (uint32_t i
= 1; i
< 4; ++i
) {
117 double x
= mQuad
->Point(i
)->X();
118 x1
= std::min(x1
, x
);
119 x2
= std::max(x2
, x
);
125 void GetVerticalMinMax(double* aY1
, double* aY2
) const
128 y1
= y2
= mQuad
->Point(0)->Y();
129 for (uint32_t i
= 1; i
< 4; ++i
) {
130 double y
= mQuad
->Point(i
)->Y();
131 y1
= std::min(y1
, y
);
132 y2
= std::max(y2
, y
);
139 virtual ~QuadBounds() {}
141 nsRefPtr
<DOMQuad
> mQuad
;
144 NS_IMPL_CYCLE_COLLECTION_INHERITED(DOMQuad::QuadBounds
, DOMRectReadOnly
, mQuad
)
146 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(DOMQuad::QuadBounds
)
147 NS_INTERFACE_MAP_END_INHERITING(DOMRectReadOnly
)
149 NS_IMPL_ADDREF_INHERITED(DOMQuad::QuadBounds
, DOMRectReadOnly
)
150 NS_IMPL_RELEASE_INHERITED(DOMQuad::QuadBounds
, DOMRectReadOnly
)
153 DOMQuad::Bounds() const
156 mBounds
= new QuadBounds(const_cast<DOMQuad
*>(this));