|
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/. */ |
|
5 |
|
6 #include "mozilla/dom/DOMQuad.h" |
|
7 |
|
8 #include "mozilla/dom/DOMQuadBinding.h" |
|
9 #include "mozilla/dom/DOMPoint.h" |
|
10 #include "mozilla/dom/DOMRect.h" |
|
11 #include <algorithm> |
|
12 |
|
13 using namespace mozilla; |
|
14 using namespace mozilla::dom; |
|
15 using namespace mozilla::gfx; |
|
16 |
|
17 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_6(DOMQuad, mParent, mBounds, mPoints[0], |
|
18 mPoints[1], mPoints[2], mPoints[3]) |
|
19 |
|
20 NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(DOMQuad, AddRef) |
|
21 NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(DOMQuad, Release) |
|
22 |
|
23 DOMQuad::DOMQuad(nsISupports* aParent, CSSPoint aPoints[4]) |
|
24 : mParent(aParent) |
|
25 { |
|
26 SetIsDOMBinding(); |
|
27 for (uint32_t i = 0; i < 4; ++i) { |
|
28 mPoints[i] = new DOMPoint(aParent, aPoints[i].x, aPoints[i].y); |
|
29 } |
|
30 } |
|
31 |
|
32 DOMQuad::DOMQuad(nsISupports* aParent) |
|
33 : mParent(aParent) |
|
34 { |
|
35 SetIsDOMBinding(); |
|
36 } |
|
37 |
|
38 DOMQuad::~DOMQuad() |
|
39 { |
|
40 } |
|
41 |
|
42 JSObject* |
|
43 DOMQuad::WrapObject(JSContext* aCx) |
|
44 { |
|
45 return DOMQuadBinding::Wrap(aCx, this); |
|
46 } |
|
47 |
|
48 already_AddRefed<DOMQuad> |
|
49 DOMQuad::Constructor(const GlobalObject& aGlobal, |
|
50 const DOMPointInit& aP1, |
|
51 const DOMPointInit& aP2, |
|
52 const DOMPointInit& aP3, |
|
53 const DOMPointInit& aP4, |
|
54 ErrorResult& aRV) |
|
55 { |
|
56 nsRefPtr<DOMQuad> obj = new DOMQuad(aGlobal.GetAsSupports()); |
|
57 obj->mPoints[0] = DOMPoint::Constructor(aGlobal, aP1, aRV); |
|
58 obj->mPoints[1] = DOMPoint::Constructor(aGlobal, aP2, aRV); |
|
59 obj->mPoints[2] = DOMPoint::Constructor(aGlobal, aP3, aRV); |
|
60 obj->mPoints[3] = DOMPoint::Constructor(aGlobal, aP4, aRV); |
|
61 return obj.forget(); |
|
62 } |
|
63 |
|
64 already_AddRefed<DOMQuad> |
|
65 DOMQuad::Constructor(const GlobalObject& aGlobal, const DOMRectReadOnly& aRect, |
|
66 ErrorResult& aRV) |
|
67 { |
|
68 CSSPoint points[4]; |
|
69 Float x = aRect.X(), y = aRect.Y(), w = aRect.Width(), h = aRect.Height(); |
|
70 points[0] = CSSPoint(x, y); |
|
71 points[1] = CSSPoint(x + w, y); |
|
72 points[2] = CSSPoint(x + w, y + h); |
|
73 points[3] = CSSPoint(x, y + h); |
|
74 nsRefPtr<DOMQuad> obj = new DOMQuad(aGlobal.GetAsSupports(), points); |
|
75 return obj.forget(); |
|
76 } |
|
77 |
|
78 class DOMQuad::QuadBounds MOZ_FINAL : public DOMRectReadOnly |
|
79 { |
|
80 public: |
|
81 QuadBounds(DOMQuad* aQuad) |
|
82 : DOMRectReadOnly(aQuad->GetParentObject()) |
|
83 , mQuad(aQuad) |
|
84 {} |
|
85 |
|
86 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(QuadBounds, DOMRectReadOnly) |
|
87 NS_DECL_ISUPPORTS_INHERITED |
|
88 |
|
89 virtual double X() const |
|
90 { |
|
91 double x1, x2; |
|
92 GetHorizontalMinMax(&x1, &x2); |
|
93 return x1; |
|
94 } |
|
95 virtual double Y() const |
|
96 { |
|
97 double y1, y2; |
|
98 GetVerticalMinMax(&y1, &y2); |
|
99 return y1; |
|
100 } |
|
101 virtual double Width() const |
|
102 { |
|
103 double x1, x2; |
|
104 GetHorizontalMinMax(&x1, &x2); |
|
105 return x2 - x1; |
|
106 } |
|
107 virtual double Height() const |
|
108 { |
|
109 double y1, y2; |
|
110 GetVerticalMinMax(&y1, &y2); |
|
111 return y2 - y1; |
|
112 } |
|
113 |
|
114 void GetHorizontalMinMax(double* aX1, double* aX2) const |
|
115 { |
|
116 double x1, x2; |
|
117 x1 = x2 = mQuad->Point(0)->X(); |
|
118 for (uint32_t i = 1; i < 4; ++i) { |
|
119 double x = mQuad->Point(i)->X(); |
|
120 x1 = std::min(x1, x); |
|
121 x2 = std::max(x2, x); |
|
122 } |
|
123 *aX1 = x1; |
|
124 *aX2 = x2; |
|
125 } |
|
126 |
|
127 void GetVerticalMinMax(double* aY1, double* aY2) const |
|
128 { |
|
129 double y1, y2; |
|
130 y1 = y2 = mQuad->Point(0)->Y(); |
|
131 for (uint32_t i = 1; i < 4; ++i) { |
|
132 double y = mQuad->Point(i)->Y(); |
|
133 y1 = std::min(y1, y); |
|
134 y2 = std::max(y2, y); |
|
135 } |
|
136 *aY1 = y1; |
|
137 *aY2 = y2; |
|
138 } |
|
139 |
|
140 protected: |
|
141 nsRefPtr<DOMQuad> mQuad; |
|
142 }; |
|
143 |
|
144 NS_IMPL_CYCLE_COLLECTION_INHERITED(DOMQuad::QuadBounds, DOMRectReadOnly, mQuad) |
|
145 |
|
146 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(DOMQuad::QuadBounds) |
|
147 NS_INTERFACE_MAP_END_INHERITING(DOMRectReadOnly) |
|
148 |
|
149 NS_IMPL_ADDREF_INHERITED(DOMQuad::QuadBounds, DOMRectReadOnly) |
|
150 NS_IMPL_RELEASE_INHERITED(DOMQuad::QuadBounds, DOMRectReadOnly) |
|
151 |
|
152 DOMRectReadOnly* |
|
153 DOMQuad::Bounds() const |
|
154 { |
|
155 if (!mBounds) { |
|
156 mBounds = new QuadBounds(const_cast<DOMQuad*>(this)); |
|
157 } |
|
158 return mBounds; |
|
159 } |