Bumping gaia.json for 8 gaia revision(s) a=gaia-bump
[gecko.git] / dom / base / DOMQuad.cpp
blob9c4581fcee705909f73b1320f5dfb68d9f6b95c2
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"
11 #include <algorithm>
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])
24 : mParent(aParent)
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)
32 : mParent(aParent)
36 DOMQuad::~DOMQuad()
40 JSObject*
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,
52 ErrorResult& aRV)
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);
59 return obj.forget();
62 already_AddRefed<DOMQuad>
63 DOMQuad::Constructor(const GlobalObject& aGlobal, const DOMRectReadOnly& aRect,
64 ErrorResult& aRV)
66 CSSPoint points[4];
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);
73 return obj.forget();
76 class DOMQuad::QuadBounds MOZ_FINAL : public DOMRectReadOnly
78 public:
79 explicit QuadBounds(DOMQuad* aQuad)
80 : DOMRectReadOnly(aQuad->GetParentObject())
81 , mQuad(aQuad)
84 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(QuadBounds, DOMRectReadOnly)
85 NS_DECL_ISUPPORTS_INHERITED
87 virtual double X() const MOZ_OVERRIDE
89 double x1, x2;
90 GetHorizontalMinMax(&x1, &x2);
91 return x1;
93 virtual double Y() const MOZ_OVERRIDE
95 double y1, y2;
96 GetVerticalMinMax(&y1, &y2);
97 return y1;
99 virtual double Width() const MOZ_OVERRIDE
101 double x1, x2;
102 GetHorizontalMinMax(&x1, &x2);
103 return x2 - x1;
105 virtual double Height() const MOZ_OVERRIDE
107 double y1, y2;
108 GetVerticalMinMax(&y1, &y2);
109 return y2 - y1;
112 void GetHorizontalMinMax(double* aX1, double* aX2) const
114 double x1, x2;
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);
121 *aX1 = x1;
122 *aX2 = x2;
125 void GetVerticalMinMax(double* aY1, double* aY2) const
127 double y1, y2;
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);
134 *aY1 = y1;
135 *aY2 = y2;
138 protected:
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)
152 DOMRectReadOnly*
153 DOMQuad::Bounds() const
155 if (!mBounds) {
156 mBounds = new QuadBounds(const_cast<DOMQuad*>(this));
158 return mBounds;