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
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 mozilla.org code.
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.
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.
36 * ***** END LICENSE BLOCK ***** */
42 #include "nsMathUtils.h"
49 * Basic type used for the geometry classes.
51 * Normally all coordinates are maintained in an app unit coordinate
52 * space. An app unit is 1/60th of a CSS device pixel, which is, in turn
53 * an integer number of device pixels, such at the CSS DPI is as close to
57 // This controls whether we're using integers or floats for coordinates. We
58 // want to eventually use floats. If you change this, you need to manually
59 // change the definition of nscoord in gfx/idl/gfxtypes.idl.
60 //#define NS_COORD_IS_FLOAT
62 inline float NS_IEEEPositiveInfinity() {
63 union { PRUint32 mPRUint32
; float mFloat
; } pun
;
64 pun
.mPRUint32
= 0x7F800000;
67 inline PRBool
NS_IEEEIsNan(float aF
) {
68 union { PRUint32 mBits
; float mFloat
; } pun
;
70 return (pun
.mBits
& 0x7F800000) == 0x7F800000 &&
71 (pun
.mBits
& 0x007FFFFF) != 0;
74 #ifdef NS_COORD_IS_FLOAT
75 typedef float nscoord
;
76 #define nscoord_MAX NS_IEEEPositiveInfinity()
78 typedef PRInt32 nscoord
;
79 #define nscoord_MAX nscoord(1 << 30)
82 #define nscoord_MIN (-nscoord_MAX)
84 inline void VERIFY_COORD(nscoord aCoord
) {
85 #ifdef NS_COORD_IS_FLOAT
86 NS_ASSERTION(floorf(aCoord
) == aCoord
,
87 "Coords cannot have fractions");
91 inline nscoord
NSCoordMultiply(nscoord aCoord
, float aVal
) {
93 #ifdef NS_COORD_IS_FLOAT
94 return floorf(aCoord
*aVal
);
96 return (PRInt32
)(aCoord
*aVal
);
100 inline nscoord
NSCoordMultiply(nscoord aCoord
, PRInt32 aVal
) {
101 VERIFY_COORD(aCoord
);
105 inline nscoord
NSCoordDivide(nscoord aCoord
, float aVal
) {
106 VERIFY_COORD(aCoord
);
107 #ifdef NS_COORD_IS_FLOAT
108 return floorf(aCoord
/aVal
);
110 return (PRInt32
)(aCoord
/aVal
);
114 inline nscoord
NSCoordDivide(nscoord aCoord
, PRInt32 aVal
) {
115 VERIFY_COORD(aCoord
);
116 #ifdef NS_COORD_IS_FLOAT
117 return floorf(aCoord
/aVal
);
124 * Returns a + b, capping the sum to nscoord_MAX.
126 * This function assumes that neither argument is nscoord_MIN.
128 * Note: If/when we start using floats for nscoords, this function won't be as
129 * necessary. Normal float addition correctly handles adding with infinity,
130 * assuming we aren't adding nscoord_MIN. (-infinity)
133 NSCoordSaturatingAdd(nscoord a
, nscoord b
)
137 NS_ASSERTION(a
!= nscoord_MIN
&& b
!= nscoord_MIN
,
138 "NSCoordSaturatingAdd got nscoord_MIN as argument");
140 #ifdef NS_COORD_IS_FLOAT
141 // Float math correctly handles a+b, given that neither is -infinity.
144 if (a
== nscoord_MAX
|| b
== nscoord_MAX
) {
145 // infinity + anything = anything + infinity = infinity
149 NS_ASSERTION(a
< nscoord_MAX
&& b
< nscoord_MAX
,
150 "Doing nscoord addition with values > nscoord_MAX");
151 NS_ASSERTION((PRInt64
)a
+ (PRInt64
)b
> (PRInt64
)nscoord_MIN
,
152 "nscoord addition will reach or pass nscoord_MIN");
153 // This one's only a warning because the PR_MIN below means that
154 // we'll handle this case correctly.
155 NS_WARN_IF_FALSE((PRInt64
)a
+ (PRInt64
)b
< (PRInt64
)nscoord_MAX
,
156 "nscoord addition capped to nscoord_MAX");
158 // Cap the result, just in case we're dealing with numbers near nscoord_MAX
159 return PR_MIN(nscoord_MAX
, a
+ b
);
165 * Returns a - b, gracefully handling cases involving nscoord_MAX.
166 * This function assumes that neither argument is nscoord_MIN.
168 * The behavior is as follows:
170 * a) infinity - infinity -> infMinusInfResult
171 * b) N - infinity -> 0 (unexpected -- triggers NOTREACHED)
172 * c) infinity - N -> infinity
173 * d) N1 - N2 -> N1 - N2
175 * Note: For float nscoords, cases (c) and (d) are handled by normal float
176 * math. We still need to explicitly specify the behavior for cases (a)
177 * and (b), though. (Under normal float math, those cases would return NaN
178 * and -infinity, respectively.)
181 NSCoordSaturatingSubtract(nscoord a
, nscoord b
,
182 nscoord infMinusInfResult
)
186 NS_ASSERTION(a
!= nscoord_MIN
&& b
!= nscoord_MIN
,
187 "NSCoordSaturatingSubtract got nscoord_MIN as argument");
189 if (b
== nscoord_MAX
) {
190 if (a
== nscoord_MAX
) {
192 return infMinusInfResult
;
195 NS_NOTREACHED("Attempted to subtract [n - nscoord_MAX]");
199 #ifdef NS_COORD_IS_FLOAT
200 // case (c) and (d) for floats. (float math handles both)
203 if (a
== nscoord_MAX
) {
204 // case (c) for integers
207 // case (d) for integers
208 NS_ASSERTION(a
< nscoord_MAX
&& b
< nscoord_MAX
,
209 "Doing nscoord subtraction with values > nscoord_MAX");
210 NS_ASSERTION((PRInt64
)a
- (PRInt64
)b
> (PRInt64
)nscoord_MIN
,
211 "nscoord subtraction will reach or pass nscoord_MIN");
212 // This one's only a warning because the PR_MIN below means that
213 // we'll handle this case correctly.
214 NS_WARN_IF_FALSE((PRInt64
)a
- (PRInt64
)b
< (PRInt64
)nscoord_MAX
,
215 "nscoord subtraction capped to nscoord_MAX");
217 // Cap the result, in case we're dealing with numbers near nscoord_MAX
218 return PR_MIN(nscoord_MAX
, a
- b
);
223 /** compare against a nscoord "b", which might be unconstrained
224 * "a" must not be unconstrained.
225 * Every number is smaller than a unconstrained one
228 NSCoordLessThan(nscoord a
,nscoord b
)
230 NS_ASSERTION(a
!= nscoord_MAX
,
231 "This coordinate should be constrained");
232 return ((a
< b
) || (b
== nscoord_MAX
));
235 /** compare against a nscoord "b", which might be unconstrained
236 * "a" must not be unconstrained
237 * No number is larger than a unconstrained one.
240 NSCoordGreaterThan(nscoord a
,nscoord b
)
242 NS_ASSERTION(a
!= nscoord_MAX
,
243 "This coordinate should be constrained");
244 return ((a
> b
) && (b
!= nscoord_MAX
));
248 * Convert an nscoord to a PRInt32. This *does not* do rounding because
249 * coords are never fractional. They can be out of range, so this does
250 * clamp out of bounds coord values to PR_INT32_MIN and PR_INT32_MAX.
252 inline PRInt32
NSCoordToInt(nscoord aCoord
) {
253 VERIFY_COORD(aCoord
);
254 #ifdef NS_COORD_IS_FLOAT
255 NS_ASSERTION(!NS_IEEEIsNan(aCoord
), "NaN encountered in int conversion");
256 if (aCoord
< -2147483648.0f
) {
257 // -2147483648 is the smallest 32-bit signed integer that can be
258 // exactly represented as a float
260 } else if (aCoord
> 2147483520.0f
) {
261 // 2147483520 is the largest 32-bit signed integer that can be
262 // exactly represented as an IEEE float
265 return (PRInt32
)aCoord
;
272 inline float NSCoordToFloat(nscoord aCoord
) {
273 VERIFY_COORD(aCoord
);
274 #ifdef NS_COORD_IS_FLOAT
275 NS_ASSERTION(!NS_IEEEIsNan(aCoord
), "NaN encountered in float conversion");
277 return (float)aCoord
;
281 * Coord Rounding Functions
283 inline nscoord
NSToCoordFloor(float aValue
)
285 return nscoord(NS_floorf(aValue
));
288 inline nscoord
NSToCoordCeil(float aValue
)
290 return nscoord(NS_ceilf(aValue
));
293 inline nscoord
NSToCoordRound(float aValue
)
295 #if defined(XP_WIN32) && defined(_M_IX86) && !defined(__GNUC__)
296 return NS_lroundup30(aValue
);
298 return nscoord(NS_floorf(aValue
+ 0.5f
));
299 #endif /* XP_WIN32 && _M_IX86 && !__GNUC__ */
303 * Int Rounding Functions
305 inline PRInt32
NSToIntFloor(float aValue
)
307 return PRInt32(NS_floorf(aValue
));
310 inline PRInt32
NSToIntCeil(float aValue
)
312 return PRInt32(NS_ceilf(aValue
));
315 inline PRInt32
NSToIntRound(float aValue
)
317 return NS_lroundf(aValue
);
321 * App Unit/Pixel conversions
323 inline nscoord
NSFloatPixelsToAppUnits(float aPixels
, PRInt32 aAppUnitsPerPixel
)
325 float product
= aPixels
* aAppUnitsPerPixel
;
328 #ifdef NS_COORD_IS_FLOAT
329 // No need to bounds-check if converting float to float
330 result
= NSToCoordRound(product
);
332 // Bounds-check before converting out of float, to avoid overflow
333 if (product
>= nscoord_MAX
) {
334 NS_WARNING("Overflowed nscoord_MAX in conversion to nscoord");
335 result
= nscoord_MAX
;
336 } else if (product
<= nscoord_MIN
) {
337 NS_WARNING("Overflowed nscoord_MIN in conversion to nscoord");
338 result
= nscoord_MIN
;
340 result
= NSToCoordRound(product
);
344 VERIFY_COORD(result
);
348 inline nscoord
NSIntPixelsToAppUnits(PRInt32 aPixels
, PRInt32 aAppUnitsPerPixel
)
350 // The cast to nscoord makes sure we don't overflow if we ever change
352 nscoord r
= aPixels
* (nscoord
)aAppUnitsPerPixel
;
357 inline float NSAppUnitsToFloatPixels(nscoord aAppUnits
, PRInt32 aAppUnitsPerPixel
)
359 return (float(aAppUnits
) / aAppUnitsPerPixel
);
362 inline PRInt32
NSAppUnitsToIntPixels(nscoord aAppUnits
, PRInt32 aAppUnitsPerPixel
)
364 return NSToIntRound(float(aAppUnits
) / aAppUnitsPerPixel
);
368 #define TWIPS_PER_POINT_INT 20
369 #define TWIPS_PER_POINT_FLOAT 20.0f
370 #define POINTS_PER_INCH_INT 72
371 #define POINTS_PER_INCH_FLOAT 72.0f
374 * Twips/unit conversions
376 inline nscoord
NSUnitsToTwips(float aValue
, float aPointsPerUnit
)
378 return NSToCoordRound(aValue
* aPointsPerUnit
* TWIPS_PER_POINT_FLOAT
);
381 inline float NSTwipsToUnits(nscoord aTwips
, float aUnitsPerPoint
)
383 return (aTwips
* (aUnitsPerPoint
/ TWIPS_PER_POINT_FLOAT
));
387 /// Unit conversion macros
389 #define NS_POINTS_TO_TWIPS(x) NSUnitsToTwips((x), 1.0f)
390 #define NS_INCHES_TO_TWIPS(x) NSUnitsToTwips((x), POINTS_PER_INCH_FLOAT) // 72 points per inch
391 #define NS_FEET_TO_TWIPS(x) NSUnitsToTwips((x), (POINTS_PER_INCH_FLOAT * 12.0f)) // 12 inches per foot
392 #define NS_MILES_TO_TWIPS(x) NSUnitsToTwips((x), (POINTS_PER_INCH_FLOAT * 12.0f * 5280.0f)) // 5280 feet per mile
394 #define NS_MILLIMETERS_TO_TWIPS(x) NSUnitsToTwips((x), (POINTS_PER_INCH_FLOAT * 0.03937f))
395 #define NS_CENTIMETERS_TO_TWIPS(x) NSUnitsToTwips((x), (POINTS_PER_INCH_FLOAT * 0.3937f))
396 #define NS_METERS_TO_TWIPS(x) NSUnitsToTwips((x), (POINTS_PER_INCH_FLOAT * 39.37f))
397 #define NS_KILOMETERS_TO_TWIPS(x) NSUnitsToTwips((x), (POINTS_PER_INCH_FLOAT * 39370.0f))
399 #define NS_PICAS_TO_TWIPS(x) NSUnitsToTwips((x), 12.0f) // 12 points per pica
400 #define NS_DIDOTS_TO_TWIPS(x) NSUnitsToTwips((x), (16.0f / 15.0f)) // 15 didots per 16 points
401 #define NS_CICEROS_TO_TWIPS(x) NSUnitsToTwips((x), (12.0f * (16.0f / 15.0f))) // 12 didots per cicero
403 #define NS_TWIPS_TO_POINTS(x) NSTwipsToUnits((x), 1.0f)
404 #define NS_TWIPS_TO_INCHES(x) NSTwipsToUnits((x), 1.0f / POINTS_PER_INCH_FLOAT)
405 #define NS_TWIPS_TO_FEET(x) NSTwipsToUnits((x), 1.0f / (POINTS_PER_INCH_FLOAT * 12.0f))
406 #define NS_TWIPS_TO_MILES(x) NSTwipsToUnits((x), 1.0f / (POINTS_PER_INCH_FLOAT * 12.0f * 5280.0f))
408 #define NS_TWIPS_TO_MILLIMETERS(x) NSTwipsToUnits((x), 1.0f / (POINTS_PER_INCH_FLOAT * 0.03937f))
409 #define NS_TWIPS_TO_CENTIMETERS(x) NSTwipsToUnits((x), 1.0f / (POINTS_PER_INCH_FLOAT * 0.3937f))
410 #define NS_TWIPS_TO_METERS(x) NSTwipsToUnits((x), 1.0f / (POINTS_PER_INCH_FLOAT * 39.37f))
411 #define NS_TWIPS_TO_KILOMETERS(x) NSTwipsToUnits((x), 1.0f / (POINTS_PER_INCH_FLOAT * 39370.0f))
413 #define NS_TWIPS_TO_PICAS(x) NSTwipsToUnits((x), 1.0f / 12.0f)
414 #define NS_TWIPS_TO_DIDOTS(x) NSTwipsToUnits((x), 1.0f / (16.0f / 15.0f))
415 #define NS_TWIPS_TO_CICEROS(x) NSTwipsToUnits((x), 1.0f / (12.0f * (16.0f / 15.0f)))
418 #endif /* NSCOORD_H */