Back to index

lightning-sunbird  0.9+nobinonly
nsCoord.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #ifndef NSCOORD_H
00039 #define NSCOORD_H
00040 
00041 #include <math.h>
00042 
00043 #include "nsDebug.h"
00044 
00045 /*
00046  * Basic type used for the geometry classes.
00047  *
00048  * Normally all coordinates are maintained in the twips coordinate
00049  * space. A twip is 1/20th of a point, and there are 72 points per
00050  * inch. However, nscoords do appear in pixel space and other
00051  * coordinate spaces.
00052  *
00053  * Twips are used because they are a device-independent unit of measure. See
00054  * header file nsUnitConversion.h for many useful macros to convert between
00055  * different units of measure.
00056  */
00057 
00058 // This controls whether we're using integers or floats for coordinates. We
00059 // want to eventually use floats. If you change this, you need to manually
00060 // change the definition of nscoord in gfx/idl/gfxtypes.idl.
00061 //#define NS_COORD_IS_FLOAT
00062 
00063 inline float NS_IEEEPositiveInfinity() {
00064   float f;
00065   *(PRUint32*)&f = 0x7F800000;
00066   return f;
00067 }
00068 inline PRBool NS_IEEEIsNan(float aF) {
00069   PRUint32 bits = *(PRUint32*)&aF;
00070   return (bits & 0x7F800000) == 0x7F800000 &&
00071     (bits & 0x007FFFFF) != 0;
00072 }
00073 
00074 #ifdef NS_COORD_IS_FLOAT
00075 typedef float nscoord;
00076 #define nscoord_MAX NS_IEEEPositiveInfinity()
00077 #else
00078 typedef PRInt32 nscoord;
00079 #define nscoord_MAX nscoord(1 << 30)
00080 #endif
00081 
00082 #define nscoord_MIN (-nscoord_MAX)
00083 
00084 inline void VERIFY_COORD(nscoord aCoord) {
00085 #ifdef NS_COORD_IS_FLOAT
00086   NS_ASSERTION(floorf(aCoord) == aCoord,
00087                "Coords cannot have fractions");
00088 #endif
00089 }
00090 
00091 inline nscoord NSCoordMultiply(nscoord aCoord, float aVal) {
00092   VERIFY_COORD(aCoord);
00093 #ifdef NS_COORD_IS_FLOAT
00094   return floorf(aCoord*aVal);
00095 #else
00096   return (PRInt32)(aCoord*aVal);
00097 #endif
00098 }
00099 
00100 inline nscoord NSCoordMultiply(nscoord aCoord, PRInt32 aVal) {
00101   VERIFY_COORD(aCoord);
00102   return aCoord*aVal;
00103 }
00104 
00105 inline nscoord NSCoordDivide(nscoord aCoord, float aVal) {
00106   VERIFY_COORD(aCoord);
00107 #ifdef NS_COORD_IS_FLOAT
00108   return floorf(aCoord/aVal);
00109 #else
00110   return (PRInt32)(aCoord/aVal);
00111 #endif
00112 }
00113 
00114 inline nscoord NSCoordDivide(nscoord aCoord, PRInt32 aVal) {
00115   VERIFY_COORD(aCoord);
00116 #ifdef NS_COORD_IS_FLOAT
00117   return floorf(aCoord/aVal);
00118 #else
00119   return aCoord/aVal;
00120 #endif
00121 }
00122 
00128 inline PRInt32 NSCoordToInt(nscoord aCoord) {
00129   VERIFY_COORD(aCoord);
00130 #ifdef NS_COORD_IS_FLOAT
00131   NS_ASSERTION(!NS_IEEEIsNan(aCoord), "NaN encountered in int conversion");
00132   if (aCoord < -2147483648.0f) {
00133     // -2147483648 is the smallest 32-bit signed integer that can be
00134     // exactly represented as a float
00135     return PR_INT32_MIN;
00136   } else if (aCoord > 2147483520.0f) {
00137     // 2147483520 is the largest 32-bit signed integer that can be
00138     // exactly represented as an IEEE float
00139     return PR_INT32_MAX;
00140   } else {
00141     return (PRInt32)aCoord;
00142   }
00143 #else
00144   return aCoord;
00145 #endif
00146 }
00147 
00148 inline float NSCoordToFloat(nscoord aCoord) {
00149   VERIFY_COORD(aCoord);
00150 #ifdef NS_COORD_IS_FLOAT
00151   NS_ASSERTION(!NS_IEEEIsNan(aCoord), "NaN encountered in float conversion");
00152 #endif
00153   return (float)aCoord;
00154 }
00155 
00156 #endif /* NSCOORD_H */