michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef NSCOORD_H michael@0: #define NSCOORD_H michael@0: michael@0: #include "nsAlgorithm.h" michael@0: #include "nscore.h" michael@0: #include "nsMathUtils.h" michael@0: #include michael@0: #include michael@0: michael@0: #include "nsDebug.h" michael@0: #include michael@0: michael@0: /* michael@0: * Basic type used for the geometry classes. michael@0: * michael@0: * Normally all coordinates are maintained in an app unit coordinate michael@0: * space. An app unit is 1/60th of a CSS device pixel, which is, in turn michael@0: * an integer number of device pixels, such at the CSS DPI is as close to michael@0: * 96dpi as possible. michael@0: */ michael@0: michael@0: // This controls whether we're using integers or floats for coordinates. We michael@0: // want to eventually use floats. michael@0: //#define NS_COORD_IS_FLOAT michael@0: michael@0: inline float NS_IEEEPositiveInfinity() { michael@0: union { uint32_t mPRUint32; float mFloat; } pun; michael@0: pun.mPRUint32 = 0x7F800000; michael@0: return pun.mFloat; michael@0: } michael@0: inline bool NS_IEEEIsNan(float aF) { michael@0: union { uint32_t mBits; float mFloat; } pun; michael@0: pun.mFloat = aF; michael@0: return (pun.mBits & 0x7F800000) == 0x7F800000 && michael@0: (pun.mBits & 0x007FFFFF) != 0; michael@0: } michael@0: michael@0: #ifdef NS_COORD_IS_FLOAT michael@0: typedef float nscoord; michael@0: #define nscoord_MAX NS_IEEEPositiveInfinity() michael@0: #else michael@0: typedef int32_t nscoord; michael@0: #define nscoord_MAX nscoord(1 << 30) michael@0: #endif michael@0: michael@0: #define nscoord_MIN (-nscoord_MAX) michael@0: michael@0: inline void VERIFY_COORD(nscoord aCoord) { michael@0: #ifdef NS_COORD_IS_FLOAT michael@0: NS_ASSERTION(floorf(aCoord) == aCoord, michael@0: "Coords cannot have fractions"); michael@0: #endif michael@0: } michael@0: michael@0: inline nscoord NSToCoordRound(float aValue) michael@0: { michael@0: #if defined(XP_WIN32) && defined(_M_IX86) && !defined(__GNUC__) michael@0: return NS_lroundup30(aValue); michael@0: #else michael@0: return nscoord(floorf(aValue + 0.5f)); michael@0: #endif /* XP_WIN32 && _M_IX86 && !__GNUC__ */ michael@0: } michael@0: michael@0: inline nscoord NSToCoordRound(double aValue) michael@0: { michael@0: #if defined(XP_WIN32) && defined(_M_IX86) && !defined(__GNUC__) michael@0: return NS_lroundup30((float)aValue); michael@0: #else michael@0: return nscoord(floor(aValue + 0.5f)); michael@0: #endif /* XP_WIN32 && _M_IX86 && !__GNUC__ */ michael@0: } michael@0: michael@0: inline nscoord NSToCoordRoundWithClamp(float aValue) michael@0: { michael@0: #ifndef NS_COORD_IS_FLOAT michael@0: // Bounds-check before converting out of float, to avoid overflow michael@0: if (aValue >= nscoord_MAX) { michael@0: return nscoord_MAX; michael@0: } michael@0: if (aValue <= nscoord_MIN) { michael@0: return nscoord_MIN; michael@0: } michael@0: #endif michael@0: return NSToCoordRound(aValue); michael@0: } michael@0: michael@0: /** michael@0: * Returns aCoord * aScale, capping the product to nscoord_MAX or nscoord_MIN as michael@0: * appropriate for the signs of aCoord and aScale. If requireNotNegative is michael@0: * true, this method will enforce that aScale is not negative; use that michael@0: * parametrization to get a check of that fact in debug builds. michael@0: */ michael@0: inline nscoord _nscoordSaturatingMultiply(nscoord aCoord, float aScale, michael@0: bool requireNotNegative) { michael@0: VERIFY_COORD(aCoord); michael@0: if (requireNotNegative) { michael@0: NS_ABORT_IF_FALSE(aScale >= 0.0f, michael@0: "negative scaling factors must be handled manually"); michael@0: } michael@0: #ifdef NS_COORD_IS_FLOAT michael@0: return floorf(aCoord * aScale); michael@0: #else michael@0: float product = aCoord * aScale; michael@0: if (requireNotNegative ? aCoord > 0 : (aCoord > 0) == (aScale > 0)) michael@0: return NSToCoordRoundWithClamp(std::min(nscoord_MAX, product)); michael@0: return NSToCoordRoundWithClamp(std::max(nscoord_MIN, product)); michael@0: #endif michael@0: } michael@0: michael@0: /** michael@0: * Returns aCoord * aScale, capping the product to nscoord_MAX or nscoord_MIN as michael@0: * appropriate for the sign of aCoord. This method requires aScale to not be michael@0: * negative; use this method when you know that aScale should never be michael@0: * negative to get a sanity check of that invariant in debug builds. michael@0: */ michael@0: inline nscoord NSCoordSaturatingNonnegativeMultiply(nscoord aCoord, float aScale) { michael@0: return _nscoordSaturatingMultiply(aCoord, aScale, true); michael@0: } michael@0: michael@0: /** michael@0: * Returns aCoord * aScale, capping the product to nscoord_MAX or nscoord_MIN as michael@0: * appropriate for the signs of aCoord and aScale. michael@0: */ michael@0: inline nscoord NSCoordSaturatingMultiply(nscoord aCoord, float aScale) { michael@0: return _nscoordSaturatingMultiply(aCoord, aScale, false); michael@0: } michael@0: michael@0: /** michael@0: * Returns a + b, capping the sum to nscoord_MAX. michael@0: * michael@0: * This function assumes that neither argument is nscoord_MIN. michael@0: * michael@0: * Note: If/when we start using floats for nscoords, this function won't be as michael@0: * necessary. Normal float addition correctly handles adding with infinity, michael@0: * assuming we aren't adding nscoord_MIN. (-infinity) michael@0: */ michael@0: inline nscoord michael@0: NSCoordSaturatingAdd(nscoord a, nscoord b) michael@0: { michael@0: VERIFY_COORD(a); michael@0: VERIFY_COORD(b); michael@0: michael@0: #ifdef NS_COORD_IS_FLOAT michael@0: // Float math correctly handles a+b, given that neither is -infinity. michael@0: return a + b; michael@0: #else michael@0: if (a == nscoord_MAX || b == nscoord_MAX) { michael@0: // infinity + anything = anything + infinity = infinity michael@0: return nscoord_MAX; michael@0: } else { michael@0: // a + b = a + b michael@0: // Cap the result, just in case we're dealing with numbers near nscoord_MAX michael@0: return std::min(nscoord_MAX, a + b); michael@0: } michael@0: #endif michael@0: } michael@0: michael@0: /** michael@0: * Returns a - b, gracefully handling cases involving nscoord_MAX. michael@0: * This function assumes that neither argument is nscoord_MIN. michael@0: * michael@0: * The behavior is as follows: michael@0: * michael@0: * a) infinity - infinity -> infMinusInfResult michael@0: * b) N - infinity -> 0 (unexpected -- triggers NOTREACHED) michael@0: * c) infinity - N -> infinity michael@0: * d) N1 - N2 -> N1 - N2 michael@0: * michael@0: * Note: For float nscoords, cases (c) and (d) are handled by normal float michael@0: * math. We still need to explicitly specify the behavior for cases (a) michael@0: * and (b), though. (Under normal float math, those cases would return NaN michael@0: * and -infinity, respectively.) michael@0: */ michael@0: inline nscoord michael@0: NSCoordSaturatingSubtract(nscoord a, nscoord b, michael@0: nscoord infMinusInfResult) michael@0: { michael@0: VERIFY_COORD(a); michael@0: VERIFY_COORD(b); michael@0: michael@0: if (b == nscoord_MAX) { michael@0: if (a == nscoord_MAX) { michael@0: // case (a) michael@0: return infMinusInfResult; michael@0: } else { michael@0: // case (b) michael@0: NS_NOTREACHED("Attempted to subtract [n - nscoord_MAX]"); michael@0: return 0; michael@0: } michael@0: } else { michael@0: #ifdef NS_COORD_IS_FLOAT michael@0: // case (c) and (d) for floats. (float math handles both) michael@0: return a - b; michael@0: #else michael@0: if (a == nscoord_MAX) { michael@0: // case (c) for integers michael@0: return nscoord_MAX; michael@0: } else { michael@0: // case (d) for integers michael@0: // Cap the result, in case we're dealing with numbers near nscoord_MAX michael@0: return std::min(nscoord_MAX, a - b); michael@0: } michael@0: } michael@0: #endif michael@0: } michael@0: michael@0: inline float NSCoordToFloat(nscoord aCoord) { michael@0: VERIFY_COORD(aCoord); michael@0: #ifdef NS_COORD_IS_FLOAT michael@0: NS_ASSERTION(!NS_IEEEIsNan(aCoord), "NaN encountered in float conversion"); michael@0: #endif michael@0: return (float)aCoord; michael@0: } michael@0: michael@0: /* michael@0: * Coord Rounding Functions michael@0: */ michael@0: inline nscoord NSToCoordFloor(float aValue) michael@0: { michael@0: return nscoord(floorf(aValue)); michael@0: } michael@0: michael@0: inline nscoord NSToCoordFloor(double aValue) michael@0: { michael@0: return nscoord(floor(aValue)); michael@0: } michael@0: michael@0: inline nscoord NSToCoordFloorClamped(float aValue) michael@0: { michael@0: #ifndef NS_COORD_IS_FLOAT michael@0: // Bounds-check before converting out of float, to avoid overflow michael@0: if (aValue >= nscoord_MAX) { michael@0: return nscoord_MAX; michael@0: } michael@0: if (aValue <= nscoord_MIN) { michael@0: return nscoord_MIN; michael@0: } michael@0: #endif michael@0: return NSToCoordFloor(aValue); michael@0: } michael@0: michael@0: inline nscoord NSToCoordCeil(float aValue) michael@0: { michael@0: return nscoord(ceilf(aValue)); michael@0: } michael@0: michael@0: inline nscoord NSToCoordCeil(double aValue) michael@0: { michael@0: return nscoord(ceil(aValue)); michael@0: } michael@0: michael@0: inline nscoord NSToCoordCeilClamped(double aValue) michael@0: { michael@0: #ifndef NS_COORD_IS_FLOAT michael@0: // Bounds-check before converting out of double, to avoid overflow michael@0: if (aValue >= nscoord_MAX) { michael@0: return nscoord_MAX; michael@0: } michael@0: if (aValue <= nscoord_MIN) { michael@0: return nscoord_MIN; michael@0: } michael@0: #endif michael@0: return NSToCoordCeil(aValue); michael@0: } michael@0: michael@0: /* michael@0: * Int Rounding Functions michael@0: */ michael@0: inline int32_t NSToIntFloor(float aValue) michael@0: { michael@0: return int32_t(floorf(aValue)); michael@0: } michael@0: michael@0: inline int32_t NSToIntCeil(float aValue) michael@0: { michael@0: return int32_t(ceilf(aValue)); michael@0: } michael@0: michael@0: inline int32_t NSToIntRound(float aValue) michael@0: { michael@0: return NS_lroundf(aValue); michael@0: } michael@0: michael@0: inline int32_t NSToIntRound(double aValue) michael@0: { michael@0: return NS_lround(aValue); michael@0: } michael@0: michael@0: inline int32_t NSToIntRoundUp(double aValue) michael@0: { michael@0: return int32_t(floor(aValue + 0.5)); michael@0: } michael@0: michael@0: /* michael@0: * App Unit/Pixel conversions michael@0: */ michael@0: inline nscoord NSFloatPixelsToAppUnits(float aPixels, float aAppUnitsPerPixel) michael@0: { michael@0: return NSToCoordRoundWithClamp(aPixels * aAppUnitsPerPixel); michael@0: } michael@0: michael@0: inline nscoord NSIntPixelsToAppUnits(int32_t aPixels, int32_t aAppUnitsPerPixel) michael@0: { michael@0: // The cast to nscoord makes sure we don't overflow if we ever change michael@0: // nscoord to float michael@0: nscoord r = aPixels * (nscoord)aAppUnitsPerPixel; michael@0: VERIFY_COORD(r); michael@0: return r; michael@0: } michael@0: michael@0: inline float NSAppUnitsToFloatPixels(nscoord aAppUnits, float aAppUnitsPerPixel) michael@0: { michael@0: return (float(aAppUnits) / aAppUnitsPerPixel); michael@0: } michael@0: michael@0: inline double NSAppUnitsToDoublePixels(nscoord aAppUnits, double aAppUnitsPerPixel) michael@0: { michael@0: return (double(aAppUnits) / aAppUnitsPerPixel); michael@0: } michael@0: michael@0: inline int32_t NSAppUnitsToIntPixels(nscoord aAppUnits, float aAppUnitsPerPixel) michael@0: { michael@0: return NSToIntRound(float(aAppUnits) / aAppUnitsPerPixel); michael@0: } michael@0: michael@0: inline float NSCoordScale(nscoord aCoord, int32_t aFromAPP, int32_t aToAPP) michael@0: { michael@0: return (NSCoordToFloat(aCoord) * aToAPP) / aFromAPP; michael@0: } michael@0: michael@0: /// handy constants michael@0: #define TWIPS_PER_POINT_INT 20 michael@0: #define TWIPS_PER_POINT_FLOAT 20.0f michael@0: #define POINTS_PER_INCH_INT 72 michael@0: #define POINTS_PER_INCH_FLOAT 72.0f michael@0: #define CM_PER_INCH_FLOAT 2.54f michael@0: #define MM_PER_INCH_FLOAT 25.4f michael@0: michael@0: /* michael@0: * Twips/unit conversions michael@0: */ michael@0: inline float NSUnitsToTwips(float aValue, float aPointsPerUnit) michael@0: { michael@0: return aValue * aPointsPerUnit * TWIPS_PER_POINT_FLOAT; michael@0: } michael@0: michael@0: inline float NSTwipsToUnits(float aTwips, float aUnitsPerPoint) michael@0: { michael@0: return (aTwips * (aUnitsPerPoint / TWIPS_PER_POINT_FLOAT)); michael@0: } michael@0: michael@0: /// Unit conversion macros michael@0: //@{ michael@0: #define NS_POINTS_TO_TWIPS(x) NSUnitsToTwips((x), 1.0f) michael@0: #define NS_INCHES_TO_TWIPS(x) NSUnitsToTwips((x), POINTS_PER_INCH_FLOAT) // 72 points per inch michael@0: michael@0: #define NS_MILLIMETERS_TO_TWIPS(x) NSUnitsToTwips((x), (POINTS_PER_INCH_FLOAT * 0.03937f)) michael@0: michael@0: #define NS_POINTS_TO_INT_TWIPS(x) NSToIntRound(NS_POINTS_TO_TWIPS(x)) michael@0: #define NS_INCHES_TO_INT_TWIPS(x) NSToIntRound(NS_INCHES_TO_TWIPS(x)) michael@0: michael@0: #define NS_TWIPS_TO_INCHES(x) NSTwipsToUnits((x), 1.0f / POINTS_PER_INCH_FLOAT) michael@0: michael@0: #define NS_TWIPS_TO_MILLIMETERS(x) NSTwipsToUnits((x), 1.0f / (POINTS_PER_INCH_FLOAT * 0.03937f)) michael@0: //@} michael@0: michael@0: #endif /* NSCOORD_H */