summaryrefslogtreecommitdiff
path: root/dom/base/DOMQuad.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'dom/base/DOMQuad.cpp')
-rw-r--r--dom/base/DOMQuad.cpp171
1 files changed, 98 insertions, 73 deletions
diff --git a/dom/base/DOMQuad.cpp b/dom/base/DOMQuad.cpp
index 9da70c043d..258bfc1bfd 100644
--- a/dom/base/DOMQuad.cpp
+++ b/dom/base/DOMQuad.cpp
@@ -44,6 +44,32 @@ DOMQuad::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
}
already_AddRefed<DOMQuad>
+DOMQuad::FromRect(const GlobalObject& aGlobal, const DOMRectInit& aInit)
+{
+ nsISupports* parent = aGlobal.GetAsSupports();
+ RefPtr<DOMQuad> obj = new DOMQuad(parent);
+ obj->mPoints[0] = new DOMPoint(parent, aInit.mX, aInit.mY, 0, 1);
+ obj->mPoints[1] =
+ new DOMPoint(parent, aInit.mX + aInit.mWidth, aInit.mY, 0, 1);
+ obj->mPoints[2] = new DOMPoint(parent, aInit.mX + aInit.mWidth,
+ aInit.mY + aInit.mHeight, 0, 1);
+ obj->mPoints[3] =
+ new DOMPoint(parent, aInit.mX, aInit.mY + aInit.mHeight, 0, 1);
+ return obj.forget();
+}
+
+already_AddRefed<DOMQuad>
+DOMQuad::FromQuad(const GlobalObject& aGlobal, const DOMQuadInit& aInit)
+{
+ RefPtr<DOMQuad> obj = new DOMQuad(aGlobal.GetAsSupports());
+ obj->mPoints[0] = DOMPoint::FromPoint(aGlobal, aInit.mP1);
+ obj->mPoints[1] = DOMPoint::FromPoint(aGlobal, aInit.mP2);
+ obj->mPoints[2] = DOMPoint::FromPoint(aGlobal, aInit.mP3);
+ obj->mPoints[3] = DOMPoint::FromPoint(aGlobal, aInit.mP4);
+ return obj.forget();
+}
+
+already_AddRefed<DOMQuad>
DOMQuad::Constructor(const GlobalObject& aGlobal,
const DOMPointInit& aP1,
const DOMPointInit& aP2,
@@ -52,10 +78,10 @@ DOMQuad::Constructor(const GlobalObject& aGlobal,
ErrorResult& aRV)
{
RefPtr<DOMQuad> obj = new DOMQuad(aGlobal.GetAsSupports());
- obj->mPoints[0] = DOMPoint::Constructor(aGlobal, aP1, aRV);
- obj->mPoints[1] = DOMPoint::Constructor(aGlobal, aP2, aRV);
- obj->mPoints[2] = DOMPoint::Constructor(aGlobal, aP3, aRV);
- obj->mPoints[3] = DOMPoint::Constructor(aGlobal, aP4, aRV);
+ obj->mPoints[0] = DOMPoint::FromPoint(aGlobal, aP1);
+ obj->mPoints[1] = DOMPoint::FromPoint(aGlobal, aP2);
+ obj->mPoints[2] = DOMPoint::FromPoint(aGlobal, aP3);
+ obj->mPoints[3] = DOMPoint::FromPoint(aGlobal, aP4);
return obj.forget();
}
@@ -73,87 +99,86 @@ DOMQuad::Constructor(const GlobalObject& aGlobal, const DOMRectReadOnly& aRect,
return obj.forget();
}
-class DOMQuad::QuadBounds final : public DOMRectReadOnly
+void
+DOMQuad::GetHorizontalMinMax(double* aX1, double* aX2) const
{
-public:
- explicit QuadBounds(DOMQuad* aQuad)
- : DOMRectReadOnly(aQuad->GetParentObject())
- , mQuad(aQuad)
- {}
-
- NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(QuadBounds, DOMRectReadOnly)
- NS_DECL_ISUPPORTS_INHERITED
-
- virtual double X() const override
- {
- double x1, x2;
- GetHorizontalMinMax(&x1, &x2);
- return x1;
- }
- virtual double Y() const override
- {
- double y1, y2;
- GetVerticalMinMax(&y1, &y2);
- return y1;
- }
- virtual double Width() const override
- {
- double x1, x2;
- GetHorizontalMinMax(&x1, &x2);
- return x2 - x1;
- }
- virtual double Height() const override
- {
- double y1, y2;
- GetVerticalMinMax(&y1, &y2);
- return y2 - y1;
+ double x1, x2;
+ x1 = x2 = Point(0)->X();
+ for (uint32_t i = 1; i < 4; ++i) {
+ double x = Point(i)->X();
+ x1 = std::min(x1, x);
+ x2 = std::max(x2, x);
}
+ *aX1 = x1;
+ *aX2 = x2;
+}
- void GetHorizontalMinMax(double* aX1, double* aX2) const
- {
- double x1, x2;
- x1 = x2 = mQuad->Point(0)->X();
- for (uint32_t i = 1; i < 4; ++i) {
- double x = mQuad->Point(i)->X();
- x1 = std::min(x1, x);
- x2 = std::max(x2, x);
- }
- *aX1 = x1;
- *aX2 = x2;
+void
+DOMQuad::GetVerticalMinMax(double* aY1, double* aY2) const
+{
+ double y1, y2;
+ y1 = y2 = Point(0)->Y();
+ for (uint32_t i = 1; i < 4; ++i) {
+ double y = Point(i)->Y();
+ y1 = std::min(y1, y);
+ y2 = std::max(y2, y);
}
+ *aY1 = y1;
+ *aY2 = y2;
+}
- void GetVerticalMinMax(double* aY1, double* aY2) const
- {
- double y1, y2;
- y1 = y2 = mQuad->Point(0)->Y();
- for (uint32_t i = 1; i < 4; ++i) {
- double y = mQuad->Point(i)->Y();
- y1 = std::min(y1, y);
- y2 = std::max(y2, y);
- }
- *aY1 = y1;
- *aY2 = y2;
+DOMRectReadOnly*
+DOMQuad::Bounds()
+{
+ if (!mBounds) {
+ mBounds = GetBounds();
}
+ return mBounds;
+}
-protected:
- virtual ~QuadBounds() {}
+already_AddRefed<DOMRectReadOnly>
+DOMQuad::GetBounds() const
+{
+ double x1, x2;
+ double y1, y2;
- RefPtr<DOMQuad> mQuad;
-};
+ GetHorizontalMinMax(&x1, &x2);
+ GetVerticalMinMax(&y1, &y2);
-NS_IMPL_CYCLE_COLLECTION_INHERITED(DOMQuad::QuadBounds, DOMRectReadOnly, mQuad)
+ RefPtr<DOMRectReadOnly> rval = new DOMRectReadOnly(GetParentObject(),
+ x1, y1, x2 - x1, y2 - y1);
+ return rval.forget();
+}
-NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(DOMQuad::QuadBounds)
-NS_INTERFACE_MAP_END_INHERITING(DOMRectReadOnly)
+void
+DOMQuad::ToJSON(DOMQuadJSON& aInit)
+{
+ aInit.mP1.Construct(RefPtr<DOMPoint>(P1()).forget());
+ aInit.mP2.Construct(RefPtr<DOMPoint>(P2()).forget());
+ aInit.mP3.Construct(RefPtr<DOMPoint>(P3()).forget());
+ aInit.mP4.Construct(RefPtr<DOMPoint>(P4()).forget());
+}
-NS_IMPL_ADDREF_INHERITED(DOMQuad::QuadBounds, DOMRectReadOnly)
-NS_IMPL_RELEASE_INHERITED(DOMQuad::QuadBounds, DOMRectReadOnly)
+// https://drafts.fxtf.org/geometry/#structured-serialization
+bool
+DOMQuad::WriteStructuredClone(JSStructuredCloneWriter* aWriter) const
+{
+ for (const auto& point : mPoints) {
+ if (!point->WriteStructuredClone(aWriter)) {
+ return false;
+ }
+ }
+ return true;
+}
-DOMRectReadOnly*
-DOMQuad::Bounds() const
+bool
+DOMQuad::ReadStructuredClone(JSStructuredCloneReader* aReader)
{
- if (!mBounds) {
- mBounds = new QuadBounds(const_cast<DOMQuad*>(this));
+ for (auto& point : mPoints) {
+ point = new DOMPoint(mParent);
+ if (!point->ReadStructuredClone(aReader)) {
+ return false;
+ }
}
- return mBounds;
+ return true;
}