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/. */
7 #include "mozilla/dom/DOMQuad.h"
9 #include "mozilla/dom/DOMPoint.h"
10 #include "mozilla/dom/DOMQuadBinding.h"
11 #include "mozilla/dom/DOMRect.h"
12 #include "mozilla/dom/DOMRectBinding.h"
13 #include "mozilla/FloatingPoint.h"
15 using namespace mozilla
;
16 using namespace mozilla::dom
;
17 using namespace mozilla::gfx
;
19 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(DOMQuad
, mParent
, mPoints
[0], mPoints
[1],
20 mPoints
[2], mPoints
[3])
22 NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(DOMQuad
, AddRef
)
23 NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(DOMQuad
, Release
)
25 DOMQuad::DOMQuad(nsISupports
* aParent
, CSSPoint aPoints
[4]) : 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
) : mParent(aParent
) {}
33 DOMQuad::~DOMQuad() = default;
35 JSObject
* DOMQuad::WrapObject(JSContext
* aCx
,
36 JS::Handle
<JSObject
*> aGivenProto
) {
37 return DOMQuad_Binding::Wrap(aCx
, this, aGivenProto
);
40 already_AddRefed
<DOMQuad
> DOMQuad::FromRect(const GlobalObject
& aGlobal
,
41 const DOMRectInit
& aInit
) {
42 nsISupports
* parent
= aGlobal
.GetAsSupports();
43 RefPtr
<DOMQuad
> obj
= new DOMQuad(parent
);
44 obj
->mPoints
[0] = new DOMPoint(parent
, aInit
.mX
, aInit
.mY
, 0, 1);
46 new DOMPoint(parent
, aInit
.mX
+ aInit
.mWidth
, aInit
.mY
, 0, 1);
47 obj
->mPoints
[2] = new DOMPoint(parent
, aInit
.mX
+ aInit
.mWidth
,
48 aInit
.mY
+ aInit
.mHeight
, 0, 1);
50 new DOMPoint(parent
, aInit
.mX
, aInit
.mY
+ aInit
.mHeight
, 0, 1);
54 already_AddRefed
<DOMQuad
> DOMQuad::FromQuad(const GlobalObject
& aGlobal
,
55 const DOMQuadInit
& aInit
) {
56 RefPtr
<DOMQuad
> obj
= new DOMQuad(aGlobal
.GetAsSupports());
57 obj
->mPoints
[0] = DOMPoint::FromPoint(aGlobal
, aInit
.mP1
);
58 obj
->mPoints
[1] = DOMPoint::FromPoint(aGlobal
, aInit
.mP2
);
59 obj
->mPoints
[2] = DOMPoint::FromPoint(aGlobal
, aInit
.mP3
);
60 obj
->mPoints
[3] = DOMPoint::FromPoint(aGlobal
, aInit
.mP4
);
64 already_AddRefed
<DOMQuad
> DOMQuad::Constructor(const GlobalObject
& aGlobal
,
65 const DOMPointInit
& aP1
,
66 const DOMPointInit
& aP2
,
67 const DOMPointInit
& aP3
,
68 const DOMPointInit
& aP4
) {
69 RefPtr
<DOMQuad
> obj
= new DOMQuad(aGlobal
.GetAsSupports());
70 obj
->mPoints
[0] = DOMPoint::FromPoint(aGlobal
, aP1
);
71 obj
->mPoints
[1] = DOMPoint::FromPoint(aGlobal
, aP2
);
72 obj
->mPoints
[2] = DOMPoint::FromPoint(aGlobal
, aP3
);
73 obj
->mPoints
[3] = DOMPoint::FromPoint(aGlobal
, aP4
);
77 already_AddRefed
<DOMQuad
> DOMQuad::Constructor(const GlobalObject
& aGlobal
,
78 const DOMRectReadOnly
& aRect
) {
80 Float x
= aRect
.X(), y
= aRect
.Y(), w
= aRect
.Width(), h
= aRect
.Height();
81 points
[0] = CSSPoint(x
, y
);
82 points
[1] = CSSPoint(x
+ w
, y
);
83 points
[2] = CSSPoint(x
+ w
, y
+ h
);
84 points
[3] = CSSPoint(x
, y
+ h
);
85 RefPtr
<DOMQuad
> obj
= new DOMQuad(aGlobal
.GetAsSupports(), points
);
89 void DOMQuad::GetHorizontalMinMax(double* aX1
, double* aX2
) const {
91 x1
= x2
= Point(0)->X();
92 for (uint32_t i
= 1; i
< 4; ++i
) {
93 double x
= Point(i
)->X();
94 x1
= NaNSafeMin(x1
, x
);
95 x2
= NaNSafeMax(x2
, x
);
101 void DOMQuad::GetVerticalMinMax(double* aY1
, double* aY2
) const {
103 y1
= y2
= Point(0)->Y();
104 for (uint32_t i
= 1; i
< 4; ++i
) {
105 double y
= Point(i
)->Y();
106 y1
= NaNSafeMin(y1
, y
);
107 y2
= NaNSafeMax(y2
, y
);
113 already_AddRefed
<DOMRectReadOnly
> DOMQuad::GetBounds() const {
117 GetHorizontalMinMax(&x1
, &x2
);
118 GetVerticalMinMax(&y1
, &y2
);
120 RefPtr
<DOMRectReadOnly
> rval
=
121 new DOMRectReadOnly(GetParentObject(), x1
, y1
, x2
- x1
, y2
- y1
);
122 return rval
.forget();
125 // https://drafts.fxtf.org/geometry/#structured-serialization
126 bool DOMQuad::WriteStructuredClone(JSContext
* aCx
,
127 JSStructuredCloneWriter
* aWriter
) const {
128 for (const auto& point
: mPoints
) {
129 if (!point
->WriteStructuredClone(aCx
, aWriter
)) {
137 already_AddRefed
<DOMQuad
> DOMQuad::ReadStructuredClone(
138 JSContext
* aCx
, nsIGlobalObject
* aGlobal
,
139 JSStructuredCloneReader
* aReader
) {
140 RefPtr
<DOMQuad
> quad
= new DOMQuad(aGlobal
);
141 for (auto& point
: quad
->mPoints
) {
142 point
= DOMPoint::ReadStructuredClone(aCx
, aGlobal
, aReader
);
147 return quad
.forget();