DOMQuad.cpp (5195B)
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/FloatingPoint.h" 10 #include "mozilla/dom/BindingDeclarations.h" 11 #include "mozilla/dom/DOMPoint.h" 12 #include "mozilla/dom/DOMQuadBinding.h" 13 #include "mozilla/dom/DOMRect.h" 14 #include "mozilla/dom/DOMRectBinding.h" 15 #include "mozilla/gfx/BasePoint.h" 16 #include "mozilla/gfx/MatrixFwd.h" 17 #include "nsIGlobalObject.h" 18 19 using namespace mozilla; 20 using namespace mozilla::dom; 21 using namespace mozilla::gfx; 22 23 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(DOMQuad, mParent, mPoints[0], mPoints[1], 24 mPoints[2], mPoints[3]) 25 26 DOMQuad::DOMQuad(nsISupports* aParent, CSSPoint aPoints[4]) : mParent(aParent) { 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) : mParent(aParent) {} 33 34 DOMQuad::~DOMQuad() = default; 35 36 JSObject* DOMQuad::WrapObject(JSContext* aCx, 37 JS::Handle<JSObject*> aGivenProto) { 38 return DOMQuad_Binding::Wrap(aCx, this, aGivenProto); 39 } 40 41 already_AddRefed<DOMQuad> DOMQuad::FromRect(const GlobalObject& aGlobal, 42 const DOMRectInit& aInit) { 43 nsISupports* parent = aGlobal.GetAsSupports(); 44 RefPtr<DOMQuad> obj = new DOMQuad(parent); 45 obj->mPoints[0] = new DOMPoint(parent, aInit.mX, aInit.mY, 0, 1); 46 obj->mPoints[1] = 47 new DOMPoint(parent, aInit.mX + aInit.mWidth, aInit.mY, 0, 1); 48 obj->mPoints[2] = new DOMPoint(parent, aInit.mX + aInit.mWidth, 49 aInit.mY + aInit.mHeight, 0, 1); 50 obj->mPoints[3] = 51 new DOMPoint(parent, aInit.mX, aInit.mY + aInit.mHeight, 0, 1); 52 return obj.forget(); 53 } 54 55 already_AddRefed<DOMQuad> DOMQuad::FromQuad(const GlobalObject& aGlobal, 56 const DOMQuadInit& aInit) { 57 RefPtr<DOMQuad> obj = new DOMQuad(aGlobal.GetAsSupports()); 58 obj->mPoints[0] = DOMPoint::FromPoint(aGlobal, aInit.mP1); 59 obj->mPoints[1] = DOMPoint::FromPoint(aGlobal, aInit.mP2); 60 obj->mPoints[2] = DOMPoint::FromPoint(aGlobal, aInit.mP3); 61 obj->mPoints[3] = DOMPoint::FromPoint(aGlobal, aInit.mP4); 62 return obj.forget(); 63 } 64 65 already_AddRefed<DOMQuad> DOMQuad::Constructor(const GlobalObject& aGlobal, 66 const DOMPointInit& aP1, 67 const DOMPointInit& aP2, 68 const DOMPointInit& aP3, 69 const DOMPointInit& aP4) { 70 RefPtr<DOMQuad> obj = new DOMQuad(aGlobal.GetAsSupports()); 71 obj->mPoints[0] = DOMPoint::FromPoint(aGlobal, aP1); 72 obj->mPoints[1] = DOMPoint::FromPoint(aGlobal, aP2); 73 obj->mPoints[2] = DOMPoint::FromPoint(aGlobal, aP3); 74 obj->mPoints[3] = DOMPoint::FromPoint(aGlobal, aP4); 75 return obj.forget(); 76 } 77 78 already_AddRefed<DOMQuad> DOMQuad::Constructor(const GlobalObject& aGlobal, 79 const DOMRectReadOnly& aRect) { 80 CSSPoint points[4]; 81 Float x = aRect.X(), y = aRect.Y(), w = aRect.Width(), h = aRect.Height(); 82 points[0] = CSSPoint(x, y); 83 points[1] = CSSPoint(x + w, y); 84 points[2] = CSSPoint(x + w, y + h); 85 points[3] = CSSPoint(x, y + h); 86 RefPtr<DOMQuad> obj = new DOMQuad(aGlobal.GetAsSupports(), points); 87 return obj.forget(); 88 } 89 90 void DOMQuad::GetHorizontalMinMax(double* aX1, double* aX2) const { 91 double x1, x2; 92 x1 = x2 = Point(0)->X(); 93 for (uint32_t i = 1; i < 4; ++i) { 94 double x = Point(i)->X(); 95 x1 = NaNSafeMin(x1, x); 96 x2 = NaNSafeMax(x2, x); 97 } 98 *aX1 = x1; 99 *aX2 = x2; 100 } 101 102 void DOMQuad::GetVerticalMinMax(double* aY1, double* aY2) const { 103 double y1, y2; 104 y1 = y2 = Point(0)->Y(); 105 for (uint32_t i = 1; i < 4; ++i) { 106 double y = Point(i)->Y(); 107 y1 = NaNSafeMin(y1, y); 108 y2 = NaNSafeMax(y2, y); 109 } 110 *aY1 = y1; 111 *aY2 = y2; 112 } 113 114 already_AddRefed<DOMRectReadOnly> DOMQuad::GetBounds() const { 115 double x1, x2; 116 double y1, y2; 117 118 GetHorizontalMinMax(&x1, &x2); 119 GetVerticalMinMax(&y1, &y2); 120 121 RefPtr<DOMRectReadOnly> rval = 122 new DOMRectReadOnly(GetParentObject(), x1, y1, x2 - x1, y2 - y1); 123 return rval.forget(); 124 } 125 126 // https://drafts.fxtf.org/geometry/#structured-serialization 127 bool DOMQuad::WriteStructuredClone(JSContext* aCx, 128 JSStructuredCloneWriter* aWriter) const { 129 for (const auto& point : mPoints) { 130 if (!point->WriteStructuredClone(aCx, aWriter)) { 131 return false; 132 } 133 } 134 return true; 135 } 136 137 // static 138 already_AddRefed<DOMQuad> DOMQuad::ReadStructuredClone( 139 JSContext* aCx, nsIGlobalObject* aGlobal, 140 JSStructuredCloneReader* aReader) { 141 RefPtr<DOMQuad> quad = new DOMQuad(aGlobal); 142 for (auto& point : quad->mPoints) { 143 point = DOMPoint::ReadStructuredClone(aCx, aGlobal, aReader); 144 if (!point) { 145 return nullptr; 146 } 147 } 148 return quad.forget(); 149 }