1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/content/base/src/DOMQuad.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,159 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#include "mozilla/dom/DOMQuad.h" 1.10 + 1.11 +#include "mozilla/dom/DOMQuadBinding.h" 1.12 +#include "mozilla/dom/DOMPoint.h" 1.13 +#include "mozilla/dom/DOMRect.h" 1.14 +#include <algorithm> 1.15 + 1.16 +using namespace mozilla; 1.17 +using namespace mozilla::dom; 1.18 +using namespace mozilla::gfx; 1.19 + 1.20 +NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_6(DOMQuad, mParent, mBounds, mPoints[0], 1.21 + mPoints[1], mPoints[2], mPoints[3]) 1.22 + 1.23 +NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(DOMQuad, AddRef) 1.24 +NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(DOMQuad, Release) 1.25 + 1.26 +DOMQuad::DOMQuad(nsISupports* aParent, CSSPoint aPoints[4]) 1.27 + : mParent(aParent) 1.28 +{ 1.29 + SetIsDOMBinding(); 1.30 + for (uint32_t i = 0; i < 4; ++i) { 1.31 + mPoints[i] = new DOMPoint(aParent, aPoints[i].x, aPoints[i].y); 1.32 + } 1.33 +} 1.34 + 1.35 +DOMQuad::DOMQuad(nsISupports* aParent) 1.36 + : mParent(aParent) 1.37 +{ 1.38 + SetIsDOMBinding(); 1.39 +} 1.40 + 1.41 +DOMQuad::~DOMQuad() 1.42 +{ 1.43 +} 1.44 + 1.45 +JSObject* 1.46 +DOMQuad::WrapObject(JSContext* aCx) 1.47 +{ 1.48 + return DOMQuadBinding::Wrap(aCx, this); 1.49 +} 1.50 + 1.51 +already_AddRefed<DOMQuad> 1.52 +DOMQuad::Constructor(const GlobalObject& aGlobal, 1.53 + const DOMPointInit& aP1, 1.54 + const DOMPointInit& aP2, 1.55 + const DOMPointInit& aP3, 1.56 + const DOMPointInit& aP4, 1.57 + ErrorResult& aRV) 1.58 +{ 1.59 + nsRefPtr<DOMQuad> obj = new DOMQuad(aGlobal.GetAsSupports()); 1.60 + obj->mPoints[0] = DOMPoint::Constructor(aGlobal, aP1, aRV); 1.61 + obj->mPoints[1] = DOMPoint::Constructor(aGlobal, aP2, aRV); 1.62 + obj->mPoints[2] = DOMPoint::Constructor(aGlobal, aP3, aRV); 1.63 + obj->mPoints[3] = DOMPoint::Constructor(aGlobal, aP4, aRV); 1.64 + return obj.forget(); 1.65 +} 1.66 + 1.67 +already_AddRefed<DOMQuad> 1.68 +DOMQuad::Constructor(const GlobalObject& aGlobal, const DOMRectReadOnly& aRect, 1.69 + ErrorResult& aRV) 1.70 +{ 1.71 + CSSPoint points[4]; 1.72 + Float x = aRect.X(), y = aRect.Y(), w = aRect.Width(), h = aRect.Height(); 1.73 + points[0] = CSSPoint(x, y); 1.74 + points[1] = CSSPoint(x + w, y); 1.75 + points[2] = CSSPoint(x + w, y + h); 1.76 + points[3] = CSSPoint(x, y + h); 1.77 + nsRefPtr<DOMQuad> obj = new DOMQuad(aGlobal.GetAsSupports(), points); 1.78 + return obj.forget(); 1.79 +} 1.80 + 1.81 +class DOMQuad::QuadBounds MOZ_FINAL : public DOMRectReadOnly 1.82 +{ 1.83 +public: 1.84 + QuadBounds(DOMQuad* aQuad) 1.85 + : DOMRectReadOnly(aQuad->GetParentObject()) 1.86 + , mQuad(aQuad) 1.87 + {} 1.88 + 1.89 + NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(QuadBounds, DOMRectReadOnly) 1.90 + NS_DECL_ISUPPORTS_INHERITED 1.91 + 1.92 + virtual double X() const 1.93 + { 1.94 + double x1, x2; 1.95 + GetHorizontalMinMax(&x1, &x2); 1.96 + return x1; 1.97 + } 1.98 + virtual double Y() const 1.99 + { 1.100 + double y1, y2; 1.101 + GetVerticalMinMax(&y1, &y2); 1.102 + return y1; 1.103 + } 1.104 + virtual double Width() const 1.105 + { 1.106 + double x1, x2; 1.107 + GetHorizontalMinMax(&x1, &x2); 1.108 + return x2 - x1; 1.109 + } 1.110 + virtual double Height() const 1.111 + { 1.112 + double y1, y2; 1.113 + GetVerticalMinMax(&y1, &y2); 1.114 + return y2 - y1; 1.115 + } 1.116 + 1.117 + void GetHorizontalMinMax(double* aX1, double* aX2) const 1.118 + { 1.119 + double x1, x2; 1.120 + x1 = x2 = mQuad->Point(0)->X(); 1.121 + for (uint32_t i = 1; i < 4; ++i) { 1.122 + double x = mQuad->Point(i)->X(); 1.123 + x1 = std::min(x1, x); 1.124 + x2 = std::max(x2, x); 1.125 + } 1.126 + *aX1 = x1; 1.127 + *aX2 = x2; 1.128 + } 1.129 + 1.130 + void GetVerticalMinMax(double* aY1, double* aY2) const 1.131 + { 1.132 + double y1, y2; 1.133 + y1 = y2 = mQuad->Point(0)->Y(); 1.134 + for (uint32_t i = 1; i < 4; ++i) { 1.135 + double y = mQuad->Point(i)->Y(); 1.136 + y1 = std::min(y1, y); 1.137 + y2 = std::max(y2, y); 1.138 + } 1.139 + *aY1 = y1; 1.140 + *aY2 = y2; 1.141 + } 1.142 + 1.143 +protected: 1.144 + nsRefPtr<DOMQuad> mQuad; 1.145 +}; 1.146 + 1.147 +NS_IMPL_CYCLE_COLLECTION_INHERITED(DOMQuad::QuadBounds, DOMRectReadOnly, mQuad) 1.148 + 1.149 +NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(DOMQuad::QuadBounds) 1.150 +NS_INTERFACE_MAP_END_INHERITING(DOMRectReadOnly) 1.151 + 1.152 +NS_IMPL_ADDREF_INHERITED(DOMQuad::QuadBounds, DOMRectReadOnly) 1.153 +NS_IMPL_RELEASE_INHERITED(DOMQuad::QuadBounds, DOMRectReadOnly) 1.154 + 1.155 +DOMRectReadOnly* 1.156 +DOMQuad::Bounds() const 1.157 +{ 1.158 + if (!mBounds) { 1.159 + mBounds = new QuadBounds(const_cast<DOMQuad*>(this)); 1.160 + } 1.161 + return mBounds; 1.162 +}