1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 * ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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
15 * The Original Code is Novell code.
17 * The Initial Developer of the Original Code is Novell Corporation.
18 * Portions created by the Initial Developer are Copyright (C) 2006
19 * the Initial Developer. All Rights Reserved.
22 * Robert O'Callahan (rocallahan@novell.com)
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * 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.
36 * ***** END LICENSE BLOCK ***** */
40 #include "nsMathUtils.h"
43 gfxRect::Intersect(const gfxRect
& aRect
) const
45 gfxRect
result(0,0,0,0);
47 gfxFloat x
= PR_MAX(aRect
.X(), X());
48 gfxFloat xmost
= PR_MIN(aRect
.XMost(), XMost());
52 gfxFloat y
= PR_MAX(aRect
.Y(), Y());
53 gfxFloat ymost
= PR_MIN(aRect
.YMost(), YMost());
57 result
= gfxRect(x
, y
, xmost
- x
, ymost
- y
);
62 gfxRect::Union(const gfxRect
& aRect
) const
69 gfxFloat x
= PR_MIN(aRect
.X(), X());
70 gfxFloat xmost
= PR_MAX(aRect
.XMost(), XMost());
71 gfxFloat y
= PR_MIN(aRect
.Y(), Y());
72 gfxFloat ymost
= PR_MAX(aRect
.YMost(), YMost());
73 return gfxRect(x
, y
, xmost
- x
, ymost
- y
);
77 gfxRect::Contains(const gfxRect
& aRect
) const
79 return aRect
.X() >= X() && aRect
.XMost() <= XMost() &&
80 aRect
.Y() >= Y() && aRect
.YMost() <= YMost();
84 gfxRect::Contains(const gfxPoint
& aPoint
) const
86 return aPoint
.x
>= X() && aPoint
.x
<= XMost() &&
87 aPoint
.y
>= Y() && aPoint
.y
<= YMost();
93 // Note that don't use NS_round here. See the comment for this method in gfxRect.h
94 gfxFloat x0
= NS_floor(X() + 0.5);
95 gfxFloat y0
= NS_floor(Y() + 0.5);
96 gfxFloat x1
= NS_floor(XMost() + 0.5);
97 gfxFloat y1
= NS_floor(YMost() + 0.5);
102 size
.width
= x1
- x0
;
103 size
.height
= y1
- y0
;
109 gfxFloat x0
= NS_ceil(X());
110 gfxFloat y0
= NS_ceil(Y());
111 gfxFloat x1
= NS_floor(XMost());
112 gfxFloat y1
= NS_floor(YMost());
117 size
.width
= x1
- x0
;
118 size
.height
= y1
- y0
;
124 gfxFloat x0
= NS_floor(X());
125 gfxFloat y0
= NS_floor(Y());
126 gfxFloat x1
= NS_ceil(XMost());
127 gfxFloat y1
= NS_ceil(YMost());
132 size
.width
= x1
- x0
;
133 size
.height
= y1
- y0
;
136 /* Clamp r to CAIRO_COORD_MIN .. CAIRO_COORD_MAX
137 * these are to be device coordinates.
139 * Cairo is currently using 24.8 fixed point,
140 * so -2^24 .. 2^24-1 is our valid
143 #define CAIRO_COORD_MAX (16777215.0)
144 #define CAIRO_COORD_MIN (-16777216.0)
149 // if either x or y is way out of bounds;
150 // note that we don't handle negative w/h here
151 if (pos
.x
> CAIRO_COORD_MAX
) {
152 pos
.x
= CAIRO_COORD_MAX
;
156 if (pos
.y
> CAIRO_COORD_MAX
) {
157 pos
.y
= CAIRO_COORD_MAX
;
161 if (pos
.x
< CAIRO_COORD_MIN
) {
162 size
.width
+= pos
.x
- CAIRO_COORD_MIN
;
163 if (size
.width
< 0.0)
165 pos
.x
= CAIRO_COORD_MIN
;
168 if (pos
.y
< CAIRO_COORD_MIN
) {
169 size
.height
+= pos
.y
- CAIRO_COORD_MIN
;
170 if (size
.height
< 0.0)
172 pos
.y
= CAIRO_COORD_MIN
;
175 if (pos
.x
+ size
.width
> CAIRO_COORD_MAX
) {
176 size
.width
= CAIRO_COORD_MAX
- pos
.x
;
179 if (pos
.y
+ size
.height
> CAIRO_COORD_MAX
) {
180 size
.height
= CAIRO_COORD_MAX
- pos
.y
;