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