content/base/src/DOMRect.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     2 /* This Source Code Form is subject to the terms of the Mozilla Public
     3  * License, v. 2.0. If a copy of the MPL was not distributed with this
     4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     6 #include "mozilla/dom/DOMRect.h"
     8 #include "nsPresContext.h"
     9 #include "mozilla/dom/DOMRectListBinding.h"
    10 #include "mozilla/dom/DOMRectBinding.h"
    12 using namespace mozilla;
    13 using namespace mozilla::dom;
    15 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_1(DOMRectReadOnly, mParent)
    16 NS_IMPL_CYCLE_COLLECTING_ADDREF(DOMRectReadOnly)
    17 NS_IMPL_CYCLE_COLLECTING_RELEASE(DOMRectReadOnly)
    18 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DOMRectReadOnly)
    19   NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
    20   NS_INTERFACE_MAP_ENTRY(nsISupports)
    21 NS_INTERFACE_MAP_END
    23 JSObject*
    24 DOMRectReadOnly::WrapObject(JSContext* aCx)
    25 {
    26   MOZ_ASSERT(mParent);
    27   return DOMRectReadOnlyBinding::Wrap(aCx, this);
    28 }
    30 // -----------------------------------------------------------------------------
    32 NS_IMPL_ISUPPORTS_INHERITED(DOMRect, DOMRectReadOnly, nsIDOMClientRect)
    34 #define FORWARD_GETTER(_name)                                                   \
    35   NS_IMETHODIMP                                                                 \
    36   DOMRect::Get ## _name(float* aResult)                                         \
    37   {                                                                             \
    38     *aResult = float(_name());                                                  \
    39     return NS_OK;                                                               \
    40   }
    42 FORWARD_GETTER(Left)
    43 FORWARD_GETTER(Top)
    44 FORWARD_GETTER(Right)
    45 FORWARD_GETTER(Bottom)
    46 FORWARD_GETTER(Width)
    47 FORWARD_GETTER(Height)
    49 JSObject*
    50 DOMRect::WrapObject(JSContext* aCx)
    51 {
    52   MOZ_ASSERT(mParent);
    53   return DOMRectBinding::Wrap(aCx, this);
    54 }
    56 already_AddRefed<DOMRect>
    57 DOMRect::Constructor(const GlobalObject& aGlobal, ErrorResult& aRV)
    58 {
    59   nsRefPtr<DOMRect> obj =
    60     new DOMRect(aGlobal.GetAsSupports(), 0.0, 0.0, 0.0, 0.0);
    61   return obj.forget();
    62 }
    64 already_AddRefed<DOMRect>
    65 DOMRect::Constructor(const GlobalObject& aGlobal, double aX, double aY,
    66                      double aWidth, double aHeight, ErrorResult& aRV)
    67 {
    68   nsRefPtr<DOMRect> obj =
    69     new DOMRect(aGlobal.GetAsSupports(), aX, aY, aWidth, aHeight);
    70   return obj.forget();
    71 }
    73 // -----------------------------------------------------------------------------
    75 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_2(DOMRectList, mParent, mArray)
    77 NS_INTERFACE_TABLE_HEAD(DOMRectList)
    78   NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
    79   NS_INTERFACE_TABLE(DOMRectList, nsIDOMClientRectList)
    80   NS_INTERFACE_TABLE_TO_MAP_SEGUE_CYCLE_COLLECTION(DOMRectList)
    81 NS_INTERFACE_MAP_END
    83 NS_IMPL_CYCLE_COLLECTING_ADDREF(DOMRectList)
    84 NS_IMPL_CYCLE_COLLECTING_RELEASE(DOMRectList)
    87 NS_IMETHODIMP
    88 DOMRectList::GetLength(uint32_t* aLength)
    89 {
    90   *aLength = Length();
    91   return NS_OK;
    92 }
    94 NS_IMETHODIMP    
    95 DOMRectList::Item(uint32_t aIndex, nsIDOMClientRect** aReturn)
    96 {
    97   NS_IF_ADDREF(*aReturn = Item(aIndex));
    98   return NS_OK;
    99 }
   101 JSObject*
   102 DOMRectList::WrapObject(JSContext *cx)
   103 {
   104   return mozilla::dom::DOMRectListBinding::Wrap(cx, this);
   105 }
   107 static double
   108 RoundFloat(double aValue)
   109 {
   110   return floor(aValue + 0.5);
   111 }
   113 void
   114 DOMRect::SetLayoutRect(const nsRect& aLayoutRect)
   115 {
   116   double scale = 65536.0;
   117   // Round to the nearest 1/scale units. We choose scale so it can be represented
   118   // exactly by machine floating point.
   119   double scaleInv = 1/scale;
   120   double t2pScaled = scale/nsPresContext::AppUnitsPerCSSPixel();
   121   double x = RoundFloat(aLayoutRect.x*t2pScaled)*scaleInv;
   122   double y = RoundFloat(aLayoutRect.y*t2pScaled)*scaleInv;
   123   SetRect(x, y, RoundFloat(aLayoutRect.XMost()*t2pScaled)*scaleInv - x,
   124           RoundFloat(aLayoutRect.YMost()*t2pScaled)*scaleInv - y);
   125 }

mercurial