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/DOMRect.h"
10 #include "js/StructuredClone.h"
11 #include "mozilla/AppUnits.h"
12 #include "mozilla/Casting.h"
13 #include "mozilla/MacroForEach.h"
14 #include "mozilla/dom/BindingDeclarations.h"
15 #include "mozilla/dom/DOMRectBinding.h"
16 #include "mozilla/dom/DOMRectListBinding.h"
17 #include "nsIGlobalObject.h"
20 using namespace mozilla
;
21 using namespace mozilla::dom
;
23 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(DOMRectReadOnly
, mParent
)
24 NS_IMPL_CYCLE_COLLECTING_ADDREF(DOMRectReadOnly
)
25 NS_IMPL_CYCLE_COLLECTING_RELEASE(DOMRectReadOnly
)
26 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DOMRectReadOnly
)
27 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
28 NS_INTERFACE_MAP_ENTRY(nsISupports
)
31 JSObject
* DOMRectReadOnly::WrapObject(JSContext
* aCx
,
32 JS::Handle
<JSObject
*> aGivenProto
) {
34 return DOMRectReadOnly_Binding::Wrap(aCx
, this, aGivenProto
);
37 already_AddRefed
<DOMRectReadOnly
> DOMRectReadOnly::FromRect(
38 const GlobalObject
& aGlobal
, const DOMRectInit
& aInit
) {
39 RefPtr
<DOMRectReadOnly
> obj
= new DOMRectReadOnly(
40 aGlobal
.GetAsSupports(), aInit
.mX
, aInit
.mY
, aInit
.mWidth
, aInit
.mHeight
);
44 already_AddRefed
<DOMRectReadOnly
> DOMRectReadOnly::Constructor(
45 const GlobalObject
& aGlobal
, double aX
, double aY
, double aWidth
,
47 RefPtr
<DOMRectReadOnly
> obj
=
48 new DOMRectReadOnly(aGlobal
.GetAsSupports(), aX
, aY
, aWidth
, aHeight
);
52 // https://drafts.fxtf.org/geometry/#structured-serialization
53 bool DOMRectReadOnly::WriteStructuredClone(
54 JSContext
* aCx
, JSStructuredCloneWriter
* aWriter
) const {
55 #define WriteDouble(d) \
56 JS_WriteUint32Pair(aWriter, (BitwiseCast<uint64_t>(d) >> 32) & 0xffffffff, \
57 BitwiseCast<uint64_t>(d) & 0xffffffff)
59 return WriteDouble(mX
) && WriteDouble(mY
) && WriteDouble(mWidth
) &&
66 already_AddRefed
<DOMRectReadOnly
> DOMRectReadOnly::ReadStructuredClone(
67 JSContext
* aCx
, nsIGlobalObject
* aGlobal
,
68 JSStructuredCloneReader
* aReader
) {
69 RefPtr
<DOMRectReadOnly
> retval
= new DOMRectReadOnly(aGlobal
);
70 if (!retval
->ReadStructuredClone(aReader
)) {
73 return retval
.forget();
76 bool DOMRectReadOnly::ReadStructuredClone(JSStructuredCloneReader
* aReader
) {
80 #define ReadDouble(d) \
81 if (!JS_ReadUint32Pair(aReader, &high, &low)) { \
84 (*(d) = BitwiseCast<double>(static_cast<uint64_t>(high) << 32 | low))
96 // -----------------------------------------------------------------------------
98 JSObject
* DOMRect::WrapObject(JSContext
* aCx
,
99 JS::Handle
<JSObject
*> aGivenProto
) {
101 return DOMRect_Binding::Wrap(aCx
, this, aGivenProto
);
104 already_AddRefed
<DOMRect
> DOMRect::FromRect(const GlobalObject
& aGlobal
,
105 const DOMRectInit
& aInit
) {
106 RefPtr
<DOMRect
> obj
= new DOMRect(aGlobal
.GetAsSupports(), aInit
.mX
, aInit
.mY
,
107 aInit
.mWidth
, aInit
.mHeight
);
111 already_AddRefed
<DOMRect
> DOMRect::Constructor(const GlobalObject
& aGlobal
,
112 double aX
, double aY
,
113 double aWidth
, double aHeight
) {
114 RefPtr
<DOMRect
> obj
=
115 new DOMRect(aGlobal
.GetAsSupports(), aX
, aY
, aWidth
, aHeight
);
120 already_AddRefed
<DOMRect
> DOMRect::ReadStructuredClone(
121 JSContext
* aCx
, nsIGlobalObject
* aGlobal
,
122 JSStructuredCloneReader
* aReader
) {
123 RefPtr
<DOMRect
> retval
= new DOMRect(aGlobal
);
124 if (!retval
->ReadStructuredClone(aReader
)) {
127 return retval
.forget();
130 // -----------------------------------------------------------------------------
132 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(DOMRectList
, mParent
, mArray
)
134 NS_INTERFACE_TABLE_HEAD(DOMRectList
)
135 NS_WRAPPERCACHE_INTERFACE_TABLE_ENTRY
136 NS_INTERFACE_TABLE0(DOMRectList
)
137 NS_INTERFACE_TABLE_TO_MAP_SEGUE_CYCLE_COLLECTION(DOMRectList
)
140 NS_IMPL_CYCLE_COLLECTING_ADDREF(DOMRectList
)
141 NS_IMPL_CYCLE_COLLECTING_RELEASE(DOMRectList
)
143 JSObject
* DOMRectList::WrapObject(JSContext
* cx
,
144 JS::Handle
<JSObject
*> aGivenProto
) {
145 return mozilla::dom::DOMRectList_Binding::Wrap(cx
, this, aGivenProto
);
148 static double RoundFloat(double aValue
) { return floor(aValue
+ 0.5); }
150 void DOMRect::SetLayoutRect(const nsRect
& aLayoutRect
) {
151 double scale
= 65536.0;
152 // Round to the nearest 1/scale units. We choose scale so it can be
153 // represented exactly by machine floating point.
154 double scaleInv
= 1 / scale
;
155 double t2pScaled
= scale
/ AppUnitsPerCSSPixel();
156 double x
= RoundFloat(aLayoutRect
.x
* t2pScaled
) * scaleInv
;
157 double y
= RoundFloat(aLayoutRect
.y
* t2pScaled
) * scaleInv
;
158 SetRect(x
, y
, RoundFloat(aLayoutRect
.XMost() * t2pScaled
) * scaleInv
- x
,
159 RoundFloat(aLayoutRect
.YMost() * t2pScaled
) * scaleInv
- y
);