Line data Source code
1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 : *
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 "nsScriptableRegion.h"
8 : #include <stdint.h> // for uint32_t
9 : #include <sys/types.h> // for int32_t
10 : #include "js/RootingAPI.h" // for Rooted
11 : #include "js/Value.h" // for INT_TO_JSVAL, etc
12 : #include "jsapi.h" // for JS_DefineElement, etc
13 : #include "mozilla/Assertions.h" // for MOZ_ASSERT_HELPER2
14 : #include "nsError.h" // for NS_OK, NS_ERROR_FAILURE, etc
15 : #include "nsID.h"
16 : #include "nsRect.h" // for mozilla::gfx::IntRect
17 : #include "nscore.h" // for NS_IMETHODIMP
18 :
19 : class JSObject;
20 : struct JSContext;
21 :
22 0 : nsScriptableRegion::nsScriptableRegion()
23 : {
24 0 : }
25 :
26 0 : NS_IMPL_ISUPPORTS(nsScriptableRegion, nsIScriptableRegion)
27 :
28 0 : NS_IMETHODIMP nsScriptableRegion::Init()
29 : {
30 0 : return NS_OK;
31 : }
32 :
33 0 : NS_IMETHODIMP nsScriptableRegion::SetToRegion(nsIScriptableRegion *aRegion)
34 : {
35 0 : aRegion->GetRegion(&mRegion);
36 0 : return NS_OK;
37 : }
38 :
39 0 : NS_IMETHODIMP nsScriptableRegion::SetToRect(int32_t aX, int32_t aY, int32_t aWidth, int32_t aHeight)
40 : {
41 0 : mRegion = mozilla::gfx::IntRect(aX, aY, aWidth, aHeight);
42 0 : return NS_OK;
43 : }
44 :
45 0 : NS_IMETHODIMP nsScriptableRegion::IntersectRegion(nsIScriptableRegion *aRegion)
46 : {
47 0 : nsIntRegion region;
48 0 : aRegion->GetRegion(®ion);
49 0 : mRegion.And(mRegion, region);
50 0 : return NS_OK;
51 : }
52 :
53 0 : NS_IMETHODIMP nsScriptableRegion::IntersectRect(int32_t aX, int32_t aY, int32_t aWidth, int32_t aHeight)
54 : {
55 0 : mRegion.And(mRegion, mozilla::gfx::IntRect(aX, aY, aWidth, aHeight));
56 0 : return NS_OK;
57 : }
58 :
59 0 : NS_IMETHODIMP nsScriptableRegion::UnionRegion(nsIScriptableRegion *aRegion)
60 : {
61 0 : nsIntRegion region;
62 0 : aRegion->GetRegion(®ion);
63 0 : mRegion.Or(mRegion, region);
64 0 : return NS_OK;
65 : }
66 :
67 0 : NS_IMETHODIMP nsScriptableRegion::UnionRect(int32_t aX, int32_t aY, int32_t aWidth, int32_t aHeight)
68 : {
69 0 : mRegion.Or(mRegion, mozilla::gfx::IntRect(aX, aY, aWidth, aHeight));
70 0 : return NS_OK;
71 : }
72 :
73 0 : NS_IMETHODIMP nsScriptableRegion::SubtractRegion(nsIScriptableRegion *aRegion)
74 : {
75 0 : nsIntRegion region;
76 0 : aRegion->GetRegion(®ion);
77 0 : mRegion.Sub(mRegion, region);
78 0 : return NS_OK;
79 : }
80 :
81 0 : NS_IMETHODIMP nsScriptableRegion::SubtractRect(int32_t aX, int32_t aY, int32_t aWidth, int32_t aHeight)
82 : {
83 0 : mRegion.Sub(mRegion, mozilla::gfx::IntRect(aX, aY, aWidth, aHeight));
84 0 : return NS_OK;
85 : }
86 :
87 0 : NS_IMETHODIMP nsScriptableRegion::IsEmpty(bool *isEmpty)
88 : {
89 0 : *isEmpty = mRegion.IsEmpty();
90 0 : return NS_OK;
91 : }
92 :
93 0 : NS_IMETHODIMP nsScriptableRegion::IsEqualRegion(nsIScriptableRegion *aRegion, bool *isEqual)
94 : {
95 0 : nsIntRegion region;
96 0 : aRegion->GetRegion(®ion);
97 0 : *isEqual = mRegion.IsEqual(region);
98 0 : return NS_OK;
99 : }
100 :
101 0 : NS_IMETHODIMP nsScriptableRegion::GetBoundingBox(int32_t *aX, int32_t *aY, int32_t *aWidth, int32_t *aHeight)
102 : {
103 0 : mozilla::gfx::IntRect boundRect = mRegion.GetBounds();
104 0 : *aX = boundRect.x;
105 0 : *aY = boundRect.y;
106 0 : *aWidth = boundRect.width;
107 0 : *aHeight = boundRect.height;
108 0 : return NS_OK;
109 : }
110 :
111 0 : NS_IMETHODIMP nsScriptableRegion::Offset(int32_t aXOffset, int32_t aYOffset)
112 : {
113 0 : mRegion.MoveBy(aXOffset, aYOffset);
114 0 : return NS_OK;
115 : }
116 :
117 0 : NS_IMETHODIMP nsScriptableRegion::ContainsRect(int32_t aX, int32_t aY, int32_t aWidth, int32_t aHeight, bool *containsRect)
118 : {
119 0 : *containsRect = mRegion.Contains(mozilla::gfx::IntRect(aX, aY, aWidth, aHeight));
120 0 : return NS_OK;
121 : }
122 :
123 :
124 0 : NS_IMETHODIMP nsScriptableRegion::GetRegion(nsIntRegion* outRgn)
125 : {
126 0 : *outRgn = mRegion;
127 0 : return NS_OK;
128 : }
129 :
130 0 : NS_IMETHODIMP nsScriptableRegion::GetRects(JSContext* aCx, JS::MutableHandle<JS::Value> aRects)
131 : {
132 0 : uint32_t numRects = mRegion.GetNumRects();
133 :
134 0 : if (!numRects) {
135 0 : aRects.setNull();
136 0 : return NS_OK;
137 : }
138 :
139 0 : JS::Rooted<JSObject*> destArray(aCx, JS_NewArrayObject(aCx, numRects * 4));
140 0 : if (!destArray) {
141 0 : return NS_ERROR_OUT_OF_MEMORY;
142 : }
143 :
144 0 : aRects.setObject(*destArray);
145 :
146 0 : uint32_t n = 0;
147 0 : for (auto iter = mRegion.RectIter(); !iter.Done(); iter.Next()) {
148 0 : const mozilla::gfx::IntRect& rect = iter.Get();
149 0 : if (!JS_DefineElement(aCx, destArray, n, rect.x, JSPROP_ENUMERATE) ||
150 0 : !JS_DefineElement(aCx, destArray, n + 1, rect.y, JSPROP_ENUMERATE) ||
151 0 : !JS_DefineElement(aCx, destArray, n + 2, rect.width, JSPROP_ENUMERATE) ||
152 0 : !JS_DefineElement(aCx, destArray, n + 3, rect.height, JSPROP_ENUMERATE)) {
153 0 : return NS_ERROR_FAILURE;
154 : }
155 0 : n += 4;
156 : }
157 :
158 0 : return NS_OK;
159 : }
|