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/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"
14 
15 using namespace mozilla;
16 using namespace mozilla::dom;
17 using namespace mozilla::gfx;
18 
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(DOMQuad,mParent,mPoints[0],mPoints[1],mPoints[2],mPoints[3])19 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(DOMQuad, mParent, mPoints[0], mPoints[1],
20                                       mPoints[2], mPoints[3])
21 
22 NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(DOMQuad, AddRef)
23 NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(DOMQuad, Release)
24 
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);
28   }
29 }
30 
DOMQuad(nsISupports * aParent)31 DOMQuad::DOMQuad(nsISupports* aParent) : mParent(aParent) {}
32 
33 DOMQuad::~DOMQuad() = default;
34 
WrapObject(JSContext * aCx,JS::Handle<JSObject * > aGivenProto)35 JSObject* DOMQuad::WrapObject(JSContext* aCx,
36                               JS::Handle<JSObject*> aGivenProto) {
37   return DOMQuad_Binding::Wrap(aCx, this, aGivenProto);
38 }
39 
FromRect(const GlobalObject & aGlobal,const DOMRectInit & aInit)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);
45   obj->mPoints[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);
49   obj->mPoints[3] =
50       new DOMPoint(parent, aInit.mX, aInit.mY + aInit.mHeight, 0, 1);
51   return obj.forget();
52 }
53 
FromQuad(const GlobalObject & aGlobal,const DOMQuadInit & aInit)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);
61   return obj.forget();
62 }
63 
Constructor(const GlobalObject & aGlobal,const DOMPointInit & aP1,const DOMPointInit & aP2,const DOMPointInit & aP3,const DOMPointInit & aP4)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);
74   return obj.forget();
75 }
76 
Constructor(const GlobalObject & aGlobal,const DOMRectReadOnly & aRect)77 already_AddRefed<DOMQuad> DOMQuad::Constructor(const GlobalObject& aGlobal,
78                                                const DOMRectReadOnly& aRect) {
79   CSSPoint points[4];
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);
86   return obj.forget();
87 }
88 
GetHorizontalMinMax(double * aX1,double * aX2) const89 void DOMQuad::GetHorizontalMinMax(double* aX1, double* aX2) const {
90   double x1, x2;
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);
96   }
97   *aX1 = x1;
98   *aX2 = x2;
99 }
100 
GetVerticalMinMax(double * aY1,double * aY2) const101 void DOMQuad::GetVerticalMinMax(double* aY1, double* aY2) const {
102   double y1, y2;
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);
108   }
109   *aY1 = y1;
110   *aY2 = y2;
111 }
112 
GetBounds() const113 already_AddRefed<DOMRectReadOnly> DOMQuad::GetBounds() const {
114   double x1, x2;
115   double y1, y2;
116 
117   GetHorizontalMinMax(&x1, &x2);
118   GetVerticalMinMax(&y1, &y2);
119 
120   RefPtr<DOMRectReadOnly> rval =
121       new DOMRectReadOnly(GetParentObject(), x1, y1, x2 - x1, y2 - y1);
122   return rval.forget();
123 }
124 
125 // https://drafts.fxtf.org/geometry/#structured-serialization
WriteStructuredClone(JSContext * aCx,JSStructuredCloneWriter * aWriter) const126 bool DOMQuad::WriteStructuredClone(JSContext* aCx,
127                                    JSStructuredCloneWriter* aWriter) const {
128   for (const auto& point : mPoints) {
129     if (!point->WriteStructuredClone(aCx, aWriter)) {
130       return false;
131     }
132   }
133   return true;
134 }
135 
136 // static
ReadStructuredClone(JSContext * aCx,nsIGlobalObject * aGlobal,JSStructuredCloneReader * aReader)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);
143     if (!point) {
144       return nullptr;
145     }
146   }
147   return quad.forget();
148 }
149