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/src/gfxidltypes.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
NSToCoordRound(float aValue
)
93 #if defined(XP_WIN32) && defined(_M_IX86) && !defined(__GNUC__)
94 return NS_lroundup30(aValue
);
96 return nscoord(NS_floorf(aValue
+ 0.5f
));
97 #endif /* XP_WIN32 && _M_IX86 && !__GNUC__ */
100 inline nscoord
NSToCoordRoundWithClamp(float aValue
)
102 #ifndef NS_COORD_IS_FLOAT
103 // Bounds-check before converting out of float, to avoid overflow
104 if (aValue
>= nscoord_MAX
) {
105 NS_WARNING("Overflowed nscoord_MAX in conversion to nscoord");
108 if (aValue
<= nscoord_MIN
) {
109 NS_WARNING("Overflowed nscoord_MIN in conversion to nscoord");
113 return NSToCoordRound(aValue
);
117 * Returns aCoord * aScale, capping the product to nscoord_MAX or nscoord_MIN as
118 * appropriate for the signs of aCoord and aScale. If requireNotNegative is
119 * true, this method will enforce that aScale is not negative; use that
120 * parametrization to get a check of that fact in debug builds.
122 inline nscoord
_nscoordSaturatingMultiply(nscoord aCoord
, float aScale
,
123 PRBool requireNotNegative
) {
124 VERIFY_COORD(aCoord
);
125 if (requireNotNegative
) {
126 NS_ABORT_IF_FALSE(aScale
>= 0.0f
,
127 "negative scaling factors must be handled manually");
129 #ifdef NS_COORD_IS_FLOAT
130 return floorf(aCoord
* aScale
);
132 // This one's only a warning because it may be possible to trigger it with
134 NS_WARN_IF_FALSE((requireNotNegative
136 : (aCoord
> 0) == (aScale
> 0))
137 ? floorf(aCoord
* aScale
) < nscoord_MAX
138 : ceilf(aCoord
* aScale
) > nscoord_MIN
,
139 "nscoord multiplication capped");
141 float product
= aCoord
* aScale
;
142 if (requireNotNegative
? aCoord
> 0 : (aCoord
> 0) == (aScale
> 0))
143 return NSToCoordRoundWithClamp(PR_MIN(nscoord_MAX
, product
));
144 return NSToCoordRoundWithClamp(PR_MAX(nscoord_MIN
, product
));
149 * Returns aCoord * aScale, capping the product to nscoord_MAX or nscoord_MIN as
150 * appropriate for the sign of aCoord. This method requires aScale to not be
151 * negative; use this method when you know that aScale should never be
152 * negative to get a sanity check of that invariant in debug builds.
154 inline nscoord
NSCoordSaturatingNonnegativeMultiply(nscoord aCoord
, float aScale
) {
155 return _nscoordSaturatingMultiply(aCoord
, aScale
, PR_TRUE
);
159 * Returns aCoord * aScale, capping the product to nscoord_MAX or nscoord_MIN as
160 * appropriate for the signs of aCoord and aScale.
162 inline nscoord
NSCoordSaturatingMultiply(nscoord aCoord
, float aScale
) {
163 return _nscoordSaturatingMultiply(aCoord
, aScale
, PR_FALSE
);
166 inline nscoord
NSCoordMultiply(nscoord aCoord
, PRInt32 aScale
) {
167 VERIFY_COORD(aCoord
);
168 return aCoord
* aScale
;
171 inline nscoord
NSCoordDivide(nscoord aCoord
, float aVal
) {
172 VERIFY_COORD(aCoord
);
173 #ifdef NS_COORD_IS_FLOAT
174 return floorf(aCoord
/aVal
);
176 return (PRInt32
)(aCoord
/aVal
);
180 inline nscoord
NSCoordDivide(nscoord aCoord
, PRInt32 aVal
) {
181 VERIFY_COORD(aCoord
);
182 #ifdef NS_COORD_IS_FLOAT
183 return floorf(aCoord
/aVal
);
190 * Returns a + b, capping the sum to nscoord_MAX.
192 * This function assumes that neither argument is nscoord_MIN.
194 * Note: If/when we start using floats for nscoords, this function won't be as
195 * necessary. Normal float addition correctly handles adding with infinity,
196 * assuming we aren't adding nscoord_MIN. (-infinity)
199 NSCoordSaturatingAdd(nscoord a
, nscoord b
)
203 NS_ASSERTION(a
!= nscoord_MIN
&& b
!= nscoord_MIN
,
204 "NSCoordSaturatingAdd got nscoord_MIN as argument");
206 #ifdef NS_COORD_IS_FLOAT
207 // Float math correctly handles a+b, given that neither is -infinity.
210 if (a
== nscoord_MAX
|| b
== nscoord_MAX
) {
211 // infinity + anything = anything + infinity = infinity
215 NS_ASSERTION(a
< nscoord_MAX
&& b
< nscoord_MAX
,
216 "Doing nscoord addition with values > nscoord_MAX");
217 NS_ASSERTION((PRInt64
)a
+ (PRInt64
)b
> (PRInt64
)nscoord_MIN
,
218 "nscoord addition will reach or pass nscoord_MIN");
219 // This one's only a warning because the PR_MIN below means that
220 // we'll handle this case correctly.
221 NS_WARN_IF_FALSE((PRInt64
)a
+ (PRInt64
)b
< (PRInt64
)nscoord_MAX
,
222 "nscoord addition capped to nscoord_MAX");
224 // Cap the result, just in case we're dealing with numbers near nscoord_MAX
225 return PR_MIN(nscoord_MAX
, a
+ b
);
231 * Returns a - b, gracefully handling cases involving nscoord_MAX.
232 * This function assumes that neither argument is nscoord_MIN.
234 * The behavior is as follows:
236 * a) infinity - infinity -> infMinusInfResult
237 * b) N - infinity -> 0 (unexpected -- triggers NOTREACHED)
238 * c) infinity - N -> infinity
239 * d) N1 - N2 -> N1 - N2
241 * Note: For float nscoords, cases (c) and (d) are handled by normal float
242 * math. We still need to explicitly specify the behavior for cases (a)
243 * and (b), though. (Under normal float math, those cases would return NaN
244 * and -infinity, respectively.)
247 NSCoordSaturatingSubtract(nscoord a
, nscoord b
,
248 nscoord infMinusInfResult
)
252 NS_ASSERTION(a
!= nscoord_MIN
&& b
!= nscoord_MIN
,
253 "NSCoordSaturatingSubtract got nscoord_MIN as argument");
255 if (b
== nscoord_MAX
) {
256 if (a
== nscoord_MAX
) {
258 return infMinusInfResult
;
261 NS_NOTREACHED("Attempted to subtract [n - nscoord_MAX]");
265 #ifdef NS_COORD_IS_FLOAT
266 // case (c) and (d) for floats. (float math handles both)
269 if (a
== nscoord_MAX
) {
270 // case (c) for integers
273 // case (d) for integers
274 NS_ASSERTION(a
< nscoord_MAX
&& b
< nscoord_MAX
,
275 "Doing nscoord subtraction with values > nscoord_MAX");
276 NS_ASSERTION((PRInt64
)a
- (PRInt64
)b
> (PRInt64
)nscoord_MIN
,
277 "nscoord subtraction will reach or pass nscoord_MIN");
278 // This one's only a warning because the PR_MIN below means that
279 // we'll handle this case correctly.
280 NS_WARN_IF_FALSE((PRInt64
)a
- (PRInt64
)b
< (PRInt64
)nscoord_MAX
,
281 "nscoord subtraction capped to nscoord_MAX");
283 // Cap the result, in case we're dealing with numbers near nscoord_MAX
284 return PR_MIN(nscoord_MAX
, a
- b
);
289 /** compare against a nscoord "b", which might be unconstrained
290 * "a" must not be unconstrained.
291 * Every number is smaller than a unconstrained one
294 NSCoordLessThan(nscoord a
,nscoord b
)
296 NS_ASSERTION(a
!= nscoord_MAX
,
297 "This coordinate should be constrained");
298 return ((a
< b
) || (b
== nscoord_MAX
));
301 /** compare against a nscoord "b", which might be unconstrained
302 * "a" must not be unconstrained
303 * No number is larger than a unconstrained one.
306 NSCoordGreaterThan(nscoord a
,nscoord b
)
308 NS_ASSERTION(a
!= nscoord_MAX
,
309 "This coordinate should be constrained");
310 return ((a
> b
) && (b
!= nscoord_MAX
));
314 * Convert an nscoord to a PRInt32. This *does not* do rounding because
315 * coords are never fractional. They can be out of range, so this does
316 * clamp out of bounds coord values to PR_INT32_MIN and PR_INT32_MAX.
318 inline PRInt32
NSCoordToInt(nscoord aCoord
) {
319 VERIFY_COORD(aCoord
);
320 #ifdef NS_COORD_IS_FLOAT
321 NS_ASSERTION(!NS_IEEEIsNan(aCoord
), "NaN encountered in int conversion");
322 if (aCoord
< -2147483648.0f
) {
323 // -2147483648 is the smallest 32-bit signed integer that can be
324 // exactly represented as a float
326 } else if (aCoord
> 2147483520.0f
) {
327 // 2147483520 is the largest 32-bit signed integer that can be
328 // exactly represented as an IEEE float
331 return (PRInt32
)aCoord
;
338 inline float NSCoordToFloat(nscoord aCoord
) {
339 VERIFY_COORD(aCoord
);
340 #ifdef NS_COORD_IS_FLOAT
341 NS_ASSERTION(!NS_IEEEIsNan(aCoord
), "NaN encountered in float conversion");
343 return (float)aCoord
;
347 * Coord Rounding Functions
349 inline nscoord
NSToCoordFloor(float aValue
)
351 return nscoord(NS_floorf(aValue
));
354 inline nscoord
NSToCoordFloorClamped(float aValue
)
356 #ifndef NS_COORD_IS_FLOAT
357 // Bounds-check before converting out of float, to avoid overflow
358 if (aValue
>= nscoord_MAX
) {
359 NS_WARNING("Overflowed nscoord_MAX in conversion to nscoord");
362 if (aValue
<= nscoord_MIN
) {
363 NS_WARNING("Overflowed nscoord_MIN in conversion to nscoord");
367 return NSToCoordFloor(aValue
);
370 inline nscoord
NSToCoordCeil(float aValue
)
372 return nscoord(NS_ceilf(aValue
));
375 inline nscoord
NSToCoordCeilClamped(float aValue
)
377 #ifndef NS_COORD_IS_FLOAT
378 // Bounds-check before converting out of float, to avoid overflow
379 if (aValue
>= nscoord_MAX
) {
380 NS_WARNING("Overflowed nscoord_MAX in conversion to nscoord");
383 if (aValue
<= nscoord_MIN
) {
384 NS_WARNING("Overflowed nscoord_MIN in conversion to nscoord");
388 return NSToCoordCeil(aValue
);
392 * Int Rounding Functions
394 inline PRInt32
NSToIntFloor(float aValue
)
396 return PRInt32(NS_floorf(aValue
));
399 inline PRInt32
NSToIntCeil(float aValue
)
401 return PRInt32(NS_ceilf(aValue
));
404 inline PRInt32
NSToIntRound(float aValue
)
406 return NS_lroundf(aValue
);
409 inline PRInt32
NSToIntRoundUp(float aValue
)
411 return PRInt32(NS_floorf(aValue
+ 0.5f
));
414 inline PRInt32
NSToIntRoundUp(double aValue
)
416 return PRInt32(NS_floor(aValue
+ 0.5));
420 * App Unit/Pixel conversions
422 inline nscoord
NSFloatPixelsToAppUnits(float aPixels
, float aAppUnitsPerPixel
)
424 return NSToCoordRoundWithClamp(aPixels
* aAppUnitsPerPixel
);
427 inline nscoord
NSIntPixelsToAppUnits(PRInt32 aPixels
, PRInt32 aAppUnitsPerPixel
)
429 // The cast to nscoord makes sure we don't overflow if we ever change
431 nscoord r
= aPixels
* (nscoord
)aAppUnitsPerPixel
;
436 inline float NSAppUnitsToFloatPixels(nscoord aAppUnits
, float aAppUnitsPerPixel
)
438 return (float(aAppUnits
) / aAppUnitsPerPixel
);
441 inline double NSAppUnitsToDoublePixels(nscoord aAppUnits
, nscoord aAppUnitsPerPixel
)
443 return (double(aAppUnits
) / double(aAppUnitsPerPixel
));
446 inline PRInt32
NSAppUnitsToIntPixels(nscoord aAppUnits
, float aAppUnitsPerPixel
)
448 return NSToIntRound(float(aAppUnits
) / aAppUnitsPerPixel
);
451 inline float NSCoordScale(nscoord aCoord
, PRInt32 aFromAPP
, PRInt32 aToAPP
)
453 return (NSCoordToFloat(aCoord
) * aToAPP
) / aFromAPP
;
457 #define TWIPS_PER_POINT_INT 20
458 #define TWIPS_PER_POINT_FLOAT 20.0f
459 #define POINTS_PER_INCH_INT 72
460 #define POINTS_PER_INCH_FLOAT 72.0f
461 #define CM_PER_INCH_FLOAT 2.54f
462 #define MM_PER_INCH_FLOAT 25.4f
465 * Twips/unit conversions
467 inline float NSUnitsToTwips(float aValue
, float aPointsPerUnit
)
469 return aValue
* aPointsPerUnit
* TWIPS_PER_POINT_FLOAT
;
472 inline float NSTwipsToUnits(float aTwips
, float aUnitsPerPoint
)
474 return (aTwips
* (aUnitsPerPoint
/ TWIPS_PER_POINT_FLOAT
));
477 /// Unit conversion macros
479 #define NS_POINTS_TO_TWIPS(x) NSUnitsToTwips((x), 1.0f)
480 #define NS_INCHES_TO_TWIPS(x) NSUnitsToTwips((x), POINTS_PER_INCH_FLOAT) // 72 points per inch
482 #define NS_MILLIMETERS_TO_TWIPS(x) NSUnitsToTwips((x), (POINTS_PER_INCH_FLOAT * 0.03937f))
483 #define NS_CENTIMETERS_TO_TWIPS(x) NSUnitsToTwips((x), (POINTS_PER_INCH_FLOAT * 0.3937f))
485 #define NS_PICAS_TO_TWIPS(x) NSUnitsToTwips((x), 12.0f) // 12 points per pica
487 #define NS_POINTS_TO_INT_TWIPS(x) NSToIntRound(NS_POINTS_TO_TWIPS(x))
488 #define NS_INCHES_TO_INT_TWIPS(x) NSToIntRound(NS_INCHES_TO_TWIPS(x))
490 #define NS_TWIPS_TO_POINTS(x) NSTwipsToUnits((x), 1.0f)
491 #define NS_TWIPS_TO_INCHES(x) NSTwipsToUnits((x), 1.0f / POINTS_PER_INCH_FLOAT)
493 #define NS_TWIPS_TO_MILLIMETERS(x) NSTwipsToUnits((x), 1.0f / (POINTS_PER_INCH_FLOAT * 0.03937f))
494 #define NS_TWIPS_TO_CENTIMETERS(x) NSTwipsToUnits((x), 1.0f / (POINTS_PER_INCH_FLOAT * 0.3937f))
496 #define NS_TWIPS_TO_PICAS(x) NSTwipsToUnits((x), 1.0f / 12.0f)
499 #endif /* NSCOORD_H */