1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is mozilla.org code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * Netscape Communications Corporation.
19 : * Portions created by the Initial Developer are Copyright (C) 1998
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either of the GNU General Public License Version 2 or later (the "GPL"),
26 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 :
39 : #ifndef NSRECT_H
40 : #define NSRECT_H
41 :
42 : #include <stdio.h>
43 : #include "nsCoord.h"
44 : #include "nsPoint.h"
45 : #include "nsSize.h"
46 : #include "nsMargin.h"
47 : #include "gfxCore.h"
48 : #include "nsTraceRefcnt.h"
49 : #include "mozilla/gfx/BaseRect.h"
50 :
51 : struct nsIntRect;
52 :
53 : struct NS_GFX nsRect :
54 : public mozilla::gfx::BaseRect<nscoord, nsRect, nsPoint, nsSize, nsMargin> {
55 : typedef mozilla::gfx::BaseRect<nscoord, nsRect, nsPoint, nsSize, nsMargin> Super;
56 :
57 : static void VERIFY_COORD(nscoord aValue) { ::VERIFY_COORD(aValue); }
58 :
59 : // Constructors
60 1368 : nsRect() : Super()
61 : {
62 1368 : MOZ_COUNT_CTOR(nsRect);
63 1368 : }
64 1 : nsRect(const nsRect& aRect) : Super(aRect)
65 : {
66 1 : MOZ_COUNT_CTOR(nsRect);
67 1 : }
68 : nsRect(const nsPoint& aOrigin, const nsSize &aSize) : Super(aOrigin, aSize)
69 : {
70 : MOZ_COUNT_CTOR(nsRect);
71 : }
72 0 : nsRect(nscoord aX, nscoord aY, nscoord aWidth, nscoord aHeight) :
73 0 : Super(aX, aY, aWidth, aHeight)
74 : {
75 0 : MOZ_COUNT_CTOR(nsRect);
76 0 : }
77 :
78 : #ifdef NS_BUILD_REFCNT_LOGGING
79 0 : ~nsRect() {
80 0 : MOZ_COUNT_DTOR(nsRect);
81 0 : }
82 : #endif
83 :
84 : // A version of Inflate that caps the values to the nscoord range.
85 : // x & y is capped at the minimum value nscoord_MIN and
86 : // width & height is capped at the maximum value nscoord_MAX.
87 : void SaturatingInflate(const nsMargin& aMargin)
88 : {
89 : #ifdef NS_COORD_IS_FLOAT
90 : Inflate(aMargin);
91 : #else
92 : PRInt64 nx = PRInt64(x) - aMargin.left;
93 : if (nx < nscoord_MIN) {
94 : NS_WARNING("Underflowed nscoord_MIN in conversion to nscoord x");
95 : nx = nscoord_MIN;
96 : }
97 : x = nscoord(nx);
98 :
99 : PRInt64 ny = PRInt64(y) - aMargin.top;
100 : if (ny < nscoord_MIN) {
101 : NS_WARNING("Underflowed nscoord_MIN in conversion to nscoord y");
102 : ny = nscoord_MIN;
103 : }
104 : y = nscoord(ny);
105 :
106 : PRInt64 w = PRInt64(width) + PRInt64(aMargin.left) + aMargin.right;
107 : if (w > nscoord_MAX) {
108 : NS_WARNING("Overflowed nscoord_MAX in conversion to nscoord width");
109 : w = nscoord_MAX;
110 : }
111 : width = nscoord(w);
112 :
113 : PRInt64 h = PRInt64(height) + PRInt64(aMargin.top) + aMargin.bottom;
114 : if (h > nscoord_MAX) {
115 : NS_WARNING("Overflowed nscoord_MAX in conversion to nscoord height");
116 : h = nscoord_MAX;
117 : }
118 : height = nscoord(h);
119 : #endif
120 : }
121 :
122 : // We have saturating versions of all the Union methods. These avoid
123 : // overflowing nscoord values in the 'width' and 'height' fields by
124 : // clamping the width and height values to nscoord_MAX if necessary.
125 :
126 : nsRect SaturatingUnion(const nsRect& aRect) const
127 : {
128 : if (IsEmpty()) {
129 : return aRect;
130 : } else if (aRect.IsEmpty()) {
131 : return *static_cast<const nsRect*>(this);
132 : } else {
133 : return SaturatingUnionEdges(aRect);
134 : }
135 : }
136 :
137 : nsRect SaturatingUnionEdges(const nsRect& aRect) const
138 : {
139 : #ifdef NS_COORD_IS_FLOAT
140 : return UnionEdges(aRect);
141 : #else
142 : nsRect result;
143 : result.x = NS_MIN(aRect.x, x);
144 : result.y = NS_MIN(aRect.y, y);
145 : PRInt64 w = NS_MAX(PRInt64(aRect.x) + aRect.width, PRInt64(x) + width) - result.x;
146 : PRInt64 h = NS_MAX(PRInt64(aRect.y) + aRect.height, PRInt64(y) + height) - result.y;
147 : if (w > nscoord_MAX) {
148 : NS_WARNING("Overflowed nscoord_MAX in conversion to nscoord width");
149 : w = nscoord_MAX;
150 : }
151 : if (h > nscoord_MAX) {
152 : NS_WARNING("Overflowed nscoord_MAX in conversion to nscoord height");
153 : h = nscoord_MAX;
154 : }
155 : result.width = nscoord(w);
156 : result.height = nscoord(h);
157 : return result;
158 : #endif
159 : }
160 :
161 : void SaturatingUnionRect(const nsRect& aRect1, const nsRect& aRect2)
162 : {
163 : *this = aRect1.SaturatingUnion(aRect2);
164 : }
165 : void SaturatingUnionRectEdges(const nsRect& aRect1, const nsRect& aRect2)
166 : {
167 : *this = aRect1.SaturatingUnionEdges(aRect2);
168 : }
169 :
170 : // Converts this rect from aFromAPP, an appunits per pixel ratio, to aToAPP.
171 : // In the RoundOut version we make the rect the smallest rect containing the
172 : // unrounded result. In the RoundIn version we make the rect the largest rect
173 : // contained in the unrounded result.
174 : // Note: this can turn an empty rectangle into a non-empty rectangle
175 : inline nsRect ConvertAppUnitsRoundOut(PRInt32 aFromAPP, PRInt32 aToAPP) const;
176 : inline nsRect ConvertAppUnitsRoundIn(PRInt32 aFromAPP, PRInt32 aToAPP) const;
177 :
178 : inline nsIntRect ScaleToNearestPixels(float aXScale, float aYScale,
179 : nscoord aAppUnitsPerPixel) const;
180 : inline nsIntRect ToNearestPixels(nscoord aAppUnitsPerPixel) const;
181 : // Note: this can turn an empty rectangle into a non-empty rectangle
182 : inline nsIntRect ScaleToOutsidePixels(float aXScale, float aYScale,
183 : nscoord aAppUnitsPerPixel) const;
184 : // Note: this can turn an empty rectangle into a non-empty rectangle
185 : inline nsIntRect ToOutsidePixels(nscoord aAppUnitsPerPixel) const;
186 : inline nsIntRect ScaleToInsidePixels(float aXScale, float aYScale,
187 : nscoord aAppUnitsPerPixel) const;
188 : inline nsIntRect ToInsidePixels(nscoord aAppUnitsPerPixel) const;
189 : };
190 :
191 : struct NS_GFX nsIntRect :
192 : public mozilla::gfx::BaseRect<PRInt32, nsIntRect, nsIntPoint, nsIntSize, nsIntMargin> {
193 : typedef mozilla::gfx::BaseRect<PRInt32, nsIntRect, nsIntPoint, nsIntSize, nsIntMargin> Super;
194 :
195 : // Constructors
196 0 : nsIntRect() : Super()
197 : {
198 0 : }
199 : nsIntRect(const nsIntRect& aRect) : Super(aRect)
200 : {
201 : }
202 : nsIntRect(const nsIntPoint& aOrigin, const nsIntSize &aSize) : Super(aOrigin, aSize)
203 : {
204 : }
205 0 : nsIntRect(PRInt32 aX, PRInt32 aY, PRInt32 aWidth, PRInt32 aHeight) :
206 0 : Super(aX, aY, aWidth, aHeight)
207 : {
208 0 : }
209 :
210 : inline nsRect ToAppUnits(nscoord aAppUnitsPerPixel) const;
211 :
212 : // Returns a special nsIntRect that's used in some places to signify
213 : // "all available space".
214 : static const nsIntRect& GetMaxSizedIntRect() { return kMaxSizedIntRect; }
215 :
216 : // This is here only to keep IPDL-generated code happy. DO NOT USE.
217 : bool operator==(const nsIntRect& aRect) const
218 : {
219 : return IsEqualEdges(aRect);
220 : }
221 :
222 : protected:
223 : static const nsIntRect kMaxSizedIntRect;
224 : };
225 :
226 : /*
227 : * App Unit/Pixel conversions
228 : */
229 :
230 : inline nsRect
231 0 : nsRect::ConvertAppUnitsRoundOut(PRInt32 aFromAPP, PRInt32 aToAPP) const
232 : {
233 0 : if (aFromAPP == aToAPP) {
234 0 : return *this;
235 : }
236 :
237 0 : nsRect rect;
238 0 : nscoord right = NSToCoordCeil(NSCoordScale(XMost(), aFromAPP, aToAPP));
239 0 : nscoord bottom = NSToCoordCeil(NSCoordScale(YMost(), aFromAPP, aToAPP));
240 0 : rect.x = NSToCoordFloor(NSCoordScale(x, aFromAPP, aToAPP));
241 0 : rect.y = NSToCoordFloor(NSCoordScale(y, aFromAPP, aToAPP));
242 0 : rect.width = (right - rect.x);
243 0 : rect.height = (bottom - rect.y);
244 :
245 0 : return rect;
246 : }
247 :
248 : inline nsRect
249 0 : nsRect::ConvertAppUnitsRoundIn(PRInt32 aFromAPP, PRInt32 aToAPP) const
250 : {
251 0 : if (aFromAPP == aToAPP) {
252 0 : return *this;
253 : }
254 :
255 0 : nsRect rect;
256 0 : nscoord right = NSToCoordFloor(NSCoordScale(XMost(), aFromAPP, aToAPP));
257 0 : nscoord bottom = NSToCoordFloor(NSCoordScale(YMost(), aFromAPP, aToAPP));
258 0 : rect.x = NSToCoordCeil(NSCoordScale(x, aFromAPP, aToAPP));
259 0 : rect.y = NSToCoordCeil(NSCoordScale(y, aFromAPP, aToAPP));
260 0 : rect.width = (right - rect.x);
261 0 : rect.height = (bottom - rect.y);
262 :
263 0 : return rect;
264 : }
265 :
266 : // scale the rect but round to preserve centers
267 : inline nsIntRect
268 0 : nsRect::ScaleToNearestPixels(float aXScale, float aYScale,
269 : nscoord aAppUnitsPerPixel) const
270 : {
271 0 : nsIntRect rect;
272 0 : rect.x = NSToIntRoundUp(NSAppUnitsToDoublePixels(x, aAppUnitsPerPixel) * aXScale);
273 0 : rect.y = NSToIntRoundUp(NSAppUnitsToDoublePixels(y, aAppUnitsPerPixel) * aYScale);
274 : rect.width = NSToIntRoundUp(NSAppUnitsToDoublePixels(XMost(),
275 0 : aAppUnitsPerPixel) * aXScale) - rect.x;
276 : rect.height = NSToIntRoundUp(NSAppUnitsToDoublePixels(YMost(),
277 0 : aAppUnitsPerPixel) * aYScale) - rect.y;
278 : return rect;
279 : }
280 :
281 : // scale the rect but round to smallest containing rect
282 : inline nsIntRect
283 0 : nsRect::ScaleToOutsidePixels(float aXScale, float aYScale,
284 : nscoord aAppUnitsPerPixel) const
285 : {
286 0 : nsIntRect rect;
287 0 : rect.x = NSToIntFloor(NSAppUnitsToFloatPixels(x, float(aAppUnitsPerPixel)) * aXScale);
288 0 : rect.y = NSToIntFloor(NSAppUnitsToFloatPixels(y, float(aAppUnitsPerPixel)) * aYScale);
289 : rect.width = NSToIntCeil(NSAppUnitsToFloatPixels(XMost(),
290 0 : float(aAppUnitsPerPixel)) * aXScale) - rect.x;
291 : rect.height = NSToIntCeil(NSAppUnitsToFloatPixels(YMost(),
292 0 : float(aAppUnitsPerPixel)) * aYScale) - rect.y;
293 : return rect;
294 : }
295 :
296 : // scale the rect but round to largest contained rect
297 : inline nsIntRect
298 : nsRect::ScaleToInsidePixels(float aXScale, float aYScale,
299 : nscoord aAppUnitsPerPixel) const
300 : {
301 : nsIntRect rect;
302 : rect.x = NSToIntCeil(NSAppUnitsToFloatPixels(x, float(aAppUnitsPerPixel)) * aXScale);
303 : rect.y = NSToIntCeil(NSAppUnitsToFloatPixels(y, float(aAppUnitsPerPixel)) * aYScale);
304 : rect.width = NSToIntFloor(NSAppUnitsToFloatPixels(XMost(),
305 : float(aAppUnitsPerPixel)) * aXScale) - rect.x;
306 : rect.height = NSToIntFloor(NSAppUnitsToFloatPixels(YMost(),
307 : float(aAppUnitsPerPixel)) * aYScale) - rect.y;
308 : return rect;
309 : }
310 :
311 : inline nsIntRect
312 0 : nsRect::ToNearestPixels(nscoord aAppUnitsPerPixel) const
313 : {
314 0 : return ScaleToNearestPixels(1.0f, 1.0f, aAppUnitsPerPixel);
315 : }
316 :
317 : inline nsIntRect
318 0 : nsRect::ToOutsidePixels(nscoord aAppUnitsPerPixel) const
319 : {
320 0 : return ScaleToOutsidePixels(1.0f, 1.0f, aAppUnitsPerPixel);
321 : }
322 :
323 : inline nsIntRect
324 : nsRect::ToInsidePixels(nscoord aAppUnitsPerPixel) const
325 : {
326 : return ScaleToInsidePixels(1.0f, 1.0f, aAppUnitsPerPixel);
327 : }
328 :
329 : // app units are integer multiples of pixels, so no rounding needed
330 : inline nsRect
331 0 : nsIntRect::ToAppUnits(nscoord aAppUnitsPerPixel) const
332 : {
333 : return nsRect(NSIntPixelsToAppUnits(x, aAppUnitsPerPixel),
334 : NSIntPixelsToAppUnits(y, aAppUnitsPerPixel),
335 : NSIntPixelsToAppUnits(width, aAppUnitsPerPixel),
336 0 : NSIntPixelsToAppUnits(height, aAppUnitsPerPixel));
337 : }
338 :
339 : #ifdef DEBUG
340 : // Diagnostics
341 : extern NS_GFX FILE* operator<<(FILE* out, const nsRect& rect);
342 : #endif // DEBUG
343 :
344 : #endif /* NSRECT_H */
|