1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/src/nsScriptableRegion.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,161 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- 1.5 + * 1.6 + * This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#include "nsScriptableRegion.h" 1.11 +#include <stdint.h> // for uint32_t 1.12 +#include <sys/types.h> // for int32_t 1.13 +#include "js/RootingAPI.h" // for Rooted 1.14 +#include "js/Value.h" // for INT_TO_JSVAL, etc 1.15 +#include "jsapi.h" // for JS_DefineElement, etc 1.16 +#include "mozilla/Assertions.h" // for MOZ_ASSERT_HELPER2 1.17 +#include "nsError.h" // for NS_OK, NS_ERROR_FAILURE, etc 1.18 +#include "nsID.h" 1.19 +#include "nsRect.h" // for nsIntRect 1.20 +#include "nscore.h" // for NS_IMETHODIMP 1.21 + 1.22 +class JSObject; 1.23 +struct JSContext; 1.24 + 1.25 +nsScriptableRegion::nsScriptableRegion() 1.26 +{ 1.27 +} 1.28 + 1.29 +NS_IMPL_ISUPPORTS(nsScriptableRegion, nsIScriptableRegion) 1.30 + 1.31 +NS_IMETHODIMP nsScriptableRegion::Init() 1.32 +{ 1.33 + return NS_OK; 1.34 +} 1.35 + 1.36 +NS_IMETHODIMP nsScriptableRegion::SetToRegion(nsIScriptableRegion *aRegion) 1.37 +{ 1.38 + aRegion->GetRegion(&mRegion); 1.39 + return NS_OK; 1.40 +} 1.41 + 1.42 +NS_IMETHODIMP nsScriptableRegion::SetToRect(int32_t aX, int32_t aY, int32_t aWidth, int32_t aHeight) 1.43 +{ 1.44 + mRegion = nsIntRect(aX, aY, aWidth, aHeight); 1.45 + return NS_OK; 1.46 +} 1.47 + 1.48 +NS_IMETHODIMP nsScriptableRegion::IntersectRegion(nsIScriptableRegion *aRegion) 1.49 +{ 1.50 + nsIntRegion region; 1.51 + aRegion->GetRegion(®ion); 1.52 + mRegion.And(mRegion, region); 1.53 + return NS_OK; 1.54 +} 1.55 + 1.56 +NS_IMETHODIMP nsScriptableRegion::IntersectRect(int32_t aX, int32_t aY, int32_t aWidth, int32_t aHeight) 1.57 +{ 1.58 + mRegion.And(mRegion, nsIntRect(aX, aY, aWidth, aHeight)); 1.59 + return NS_OK; 1.60 +} 1.61 + 1.62 +NS_IMETHODIMP nsScriptableRegion::UnionRegion(nsIScriptableRegion *aRegion) 1.63 +{ 1.64 + nsIntRegion region; 1.65 + aRegion->GetRegion(®ion); 1.66 + mRegion.Or(mRegion, region); 1.67 + return NS_OK; 1.68 +} 1.69 + 1.70 +NS_IMETHODIMP nsScriptableRegion::UnionRect(int32_t aX, int32_t aY, int32_t aWidth, int32_t aHeight) 1.71 +{ 1.72 + mRegion.Or(mRegion, nsIntRect(aX, aY, aWidth, aHeight)); 1.73 + return NS_OK; 1.74 +} 1.75 + 1.76 +NS_IMETHODIMP nsScriptableRegion::SubtractRegion(nsIScriptableRegion *aRegion) 1.77 +{ 1.78 + nsIntRegion region; 1.79 + aRegion->GetRegion(®ion); 1.80 + mRegion.Sub(mRegion, region); 1.81 + return NS_OK; 1.82 +} 1.83 + 1.84 +NS_IMETHODIMP nsScriptableRegion::SubtractRect(int32_t aX, int32_t aY, int32_t aWidth, int32_t aHeight) 1.85 +{ 1.86 + mRegion.Sub(mRegion, nsIntRect(aX, aY, aWidth, aHeight)); 1.87 + return NS_OK; 1.88 +} 1.89 + 1.90 +NS_IMETHODIMP nsScriptableRegion::IsEmpty(bool *isEmpty) 1.91 +{ 1.92 + *isEmpty = mRegion.IsEmpty(); 1.93 + return NS_OK; 1.94 +} 1.95 + 1.96 +NS_IMETHODIMP nsScriptableRegion::IsEqualRegion(nsIScriptableRegion *aRegion, bool *isEqual) 1.97 +{ 1.98 + nsIntRegion region; 1.99 + aRegion->GetRegion(®ion); 1.100 + *isEqual = mRegion.IsEqual(region); 1.101 + return NS_OK; 1.102 +} 1.103 + 1.104 +NS_IMETHODIMP nsScriptableRegion::GetBoundingBox(int32_t *aX, int32_t *aY, int32_t *aWidth, int32_t *aHeight) 1.105 +{ 1.106 + nsIntRect boundRect = mRegion.GetBounds(); 1.107 + *aX = boundRect.x; 1.108 + *aY = boundRect.y; 1.109 + *aWidth = boundRect.width; 1.110 + *aHeight = boundRect.height; 1.111 + return NS_OK; 1.112 +} 1.113 + 1.114 +NS_IMETHODIMP nsScriptableRegion::Offset(int32_t aXOffset, int32_t aYOffset) 1.115 +{ 1.116 + mRegion.MoveBy(aXOffset, aYOffset); 1.117 + return NS_OK; 1.118 +} 1.119 + 1.120 +NS_IMETHODIMP nsScriptableRegion::ContainsRect(int32_t aX, int32_t aY, int32_t aWidth, int32_t aHeight, bool *containsRect) 1.121 +{ 1.122 + *containsRect = mRegion.Contains(nsIntRect(aX, aY, aWidth, aHeight)); 1.123 + return NS_OK; 1.124 +} 1.125 + 1.126 + 1.127 +NS_IMETHODIMP nsScriptableRegion::GetRegion(nsIntRegion* outRgn) 1.128 +{ 1.129 + *outRgn = mRegion; 1.130 + return NS_OK; 1.131 +} 1.132 + 1.133 +NS_IMETHODIMP nsScriptableRegion::GetRects(JSContext* aCx, JS::MutableHandle<JS::Value> aRects) 1.134 +{ 1.135 + uint32_t numRects = mRegion.GetNumRects(); 1.136 + 1.137 + if (!numRects) { 1.138 + aRects.setNull(); 1.139 + return NS_OK; 1.140 + } 1.141 + 1.142 + JS::Rooted<JSObject*> destArray(aCx, JS_NewArrayObject(aCx, numRects * 4)); 1.143 + if (!destArray) { 1.144 + return NS_ERROR_OUT_OF_MEMORY; 1.145 + } 1.146 + 1.147 + aRects.setObject(*destArray); 1.148 + 1.149 + uint32_t n = 0; 1.150 + nsIntRegionRectIterator iter(mRegion); 1.151 + const nsIntRect *rect; 1.152 + 1.153 + while ((rect = iter.Next())) { 1.154 + if (!JS_DefineElement(aCx, destArray, n, INT_TO_JSVAL(rect->x), nullptr, nullptr, JSPROP_ENUMERATE) || 1.155 + !JS_DefineElement(aCx, destArray, n + 1, INT_TO_JSVAL(rect->y), nullptr, nullptr, JSPROP_ENUMERATE) || 1.156 + !JS_DefineElement(aCx, destArray, n + 2, INT_TO_JSVAL(rect->width), nullptr, nullptr, JSPROP_ENUMERATE) || 1.157 + !JS_DefineElement(aCx, destArray, n + 3, INT_TO_JSVAL(rect->height), nullptr, nullptr, JSPROP_ENUMERATE)) { 1.158 + return NS_ERROR_FAILURE; 1.159 + } 1.160 + n += 4; 1.161 + } 1.162 + 1.163 + return NS_OK; 1.164 +}