Back to index

lightning-sunbird  0.9+nobinonly
nsUnitConversion.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 #ifndef nsUnitConversion_h__
00038 #define nsUnitConversion_h__
00039 
00040 #include "nscore.h"
00041 #include "nsCoord.h"
00042 #include <math.h>
00043 #include <float.h>
00044 
00045 #ifndef FLT_EPSILON
00046 // Not an ANSI compiler... oh, well.  Use an IEEE value.
00047 #define FLT_EPSILON 1.19209290e-7f
00048 #endif
00049 
00050 #define TWIPS_PER_POINT_INT           20
00051 #define TWIPS_PER_POINT_FLOAT         20.0f
00052 #define ROUND_CONST_FLOAT             0.5f
00053 #define CEIL_CONST_FLOAT              (1.0f - 0.5f*FLT_EPSILON)
00054 
00055 
00056 /*
00057  * Coord Rounding Functions
00058  */
00059 inline nscoord NSToCoordFloor(float aValue)
00060 {
00061 #ifdef NS_COORD_IS_FLOAT
00062   return floorf(aValue);
00063 #else
00064   return ((0.0f <= aValue) ? nscoord(aValue) : nscoord(aValue - CEIL_CONST_FLOAT));
00065 #endif
00066 }
00067 
00068 inline nscoord NSToCoordCeil(float aValue)
00069 {
00070 #ifdef NS_COORD_IS_FLOAT
00071   return ceilf(aValue);
00072 #else
00073   return ((0.0f <= aValue) ? nscoord(aValue + CEIL_CONST_FLOAT) : nscoord(aValue));
00074 #endif
00075 }
00076 
00077 inline nscoord NSToCoordRound(float aValue)
00078 {
00079 #ifdef NS_COORD_IS_FLOAT
00080   return floorf(aValue + ROUND_CONST_FLOAT);
00081 #else
00082   return ((0.0f <= aValue) ? nscoord(aValue + ROUND_CONST_FLOAT) : nscoord(aValue - ROUND_CONST_FLOAT));
00083 #endif
00084 }
00085 
00086 /*
00087  * Int Rounding Functions
00088  */
00089 inline PRInt32 NSToIntFloor(float aValue)
00090 {
00091   return ((0.0f <= aValue) ? PRInt32(aValue) : PRInt32(aValue - CEIL_CONST_FLOAT));
00092 }
00093 
00094 inline PRInt32 NSToIntCeil(float aValue)
00095 {
00096   return ((0.0f <= aValue) ? PRInt32(aValue + CEIL_CONST_FLOAT) : PRInt32(aValue));
00097 }
00098 
00099 inline PRInt32 NSToIntRound(float aValue)
00100 {
00101   return ((0.0f <= aValue) ? PRInt32(aValue + ROUND_CONST_FLOAT) : PRInt32(aValue - ROUND_CONST_FLOAT));
00102 }
00103 
00104 /* 
00105  * Twips/Points conversions
00106  */
00107 inline nscoord NSFloatPointsToTwips(float aPoints)
00108 {
00109   return NSToCoordRound(aPoints * TWIPS_PER_POINT_FLOAT);
00110 }
00111 
00112 inline nscoord NSIntPointsToTwips(PRInt32 aPoints)
00113 {
00114   // If nscoord is a float, do the multiplication as float to avoid
00115   // overflow
00116   return nscoord(aPoints) * TWIPS_PER_POINT_INT;
00117 }
00118 
00119 inline PRInt32 NSTwipsToIntPoints(nscoord aTwips)
00120 {
00121   return NSToIntRound(aTwips / TWIPS_PER_POINT_FLOAT);
00122 }
00123 
00124 inline PRInt32 NSTwipsToFloorIntPoints(nscoord aTwips)
00125 {
00126   return NSToIntFloor(aTwips / TWIPS_PER_POINT_FLOAT);
00127 }
00128 
00129 inline PRInt32 NSTwipsToCeilIntPoints(nscoord aTwips)
00130 {
00131   return NSToIntCeil(aTwips / TWIPS_PER_POINT_FLOAT);
00132 }
00133 
00134 inline float NSTwipsToFloatPoints(nscoord aTwips)
00135 {
00136   return (float(aTwips) / TWIPS_PER_POINT_FLOAT);
00137 }
00138 
00139 /* 
00140  * Twips/Pixel conversions
00141  */
00142 inline nscoord NSFloatPixelsToTwips(float aPixels, float aTwipsPerPixel)
00143 {
00144   nscoord r = NSToCoordRound(aPixels * aTwipsPerPixel);
00145   VERIFY_COORD(r);
00146   return r;
00147 }
00148 
00149 inline nscoord NSIntPixelsToTwips(PRInt32 aPixels, float aTwipsPerPixel)
00150 {
00151 #ifdef NS_COORD_IS_FLOAT
00152   nscoord r = aPixels * aTwipsPerPixel;
00153 #else
00154   nscoord r = NSToCoordRound(float(aPixels) * aTwipsPerPixel);
00155 #endif
00156   VERIFY_COORD(r);
00157   return r;
00158 }
00159 
00160 inline float NSTwipsToFloatPixels(nscoord aTwips, float aPixelsPerTwip)
00161 {
00162   return (float(aTwips) * aPixelsPerTwip);
00163 }
00164 
00165 inline PRInt32 NSTwipsToIntPixels(nscoord aTwips, float aPixelsPerTwip)
00166 {
00167   return NSToIntRound(float(aTwips) * aPixelsPerTwip);
00168 }
00169 
00170 /* 
00171  * Twips/unit conversions
00172  */
00173 inline nscoord NSUnitsToTwips(float aValue, float aPointsPerUnit)
00174 {
00175   return NSToCoordRound(aValue * aPointsPerUnit * TWIPS_PER_POINT_FLOAT);
00176 }
00177 
00178 inline float NSTwipsToUnits(nscoord aTwips, float aUnitsPerPoint)
00179 {
00180   return (aTwips * (aUnitsPerPoint / TWIPS_PER_POINT_FLOAT));
00181 }
00182 
00183 
00185 
00186 #define NS_INCHES_TO_TWIPS(x)         NSUnitsToTwips((x), 72.0f)                      // 72 points per inch
00187 #define NS_FEET_TO_TWIPS(x)           NSUnitsToTwips((x), (72.0f * 12.0f))            // 12 inches per foot
00188 #define NS_MILES_TO_TWIPS(x)          NSUnitsToTwips((x), (72.0f * 12.0f * 5280.0f))  // 5280 feet per mile
00189 
00190 #define NS_MILLIMETERS_TO_TWIPS(x)    NSUnitsToTwips((x), (72.0f * 0.03937f))
00191 #define NS_CENTIMETERS_TO_TWIPS(x)    NSUnitsToTwips((x), (72.0f * 0.3937f))
00192 #define NS_METERS_TO_TWIPS(x)         NSUnitsToTwips((x), (72.0f * 39.37f))
00193 #define NS_KILOMETERS_TO_TWIPS(x)     NSUnitsToTwips((x), (72.0f * 39370.0f))
00194 
00195 #define NS_PICAS_TO_TWIPS(x)          NSUnitsToTwips((x), 12.0f)                      // 12 points per pica
00196 #define NS_DIDOTS_TO_TWIPS(x)         NSUnitsToTwips((x), (16.0f / 15.0f))            // 15 didots per 16 points
00197 #define NS_CICEROS_TO_TWIPS(x)        NSUnitsToTwips((x), (12.0f * (16.0f / 15.0f)))  // 12 didots per cicero
00198 
00199 
00200 #define NS_TWIPS_TO_INCHES(x)         NSTwipsToUnits((x), 1.0f / 72.0f)
00201 #define NS_TWIPS_TO_FEET(x)           NSTwipsToUnits((x), 1.0f / (72.0f * 12.0f))
00202 #define NS_TWIPS_TO_MILES(x)          NSTwipsToUnits((x), 1.0f / (72.0f * 12.0f * 5280.0f))
00203 
00204 #define NS_TWIPS_TO_MILLIMETERS(x)    NSTwipsToUnits((x), 1.0f / (72.0f * 0.03937f))
00205 #define NS_TWIPS_TO_CENTIMETERS(x)    NSTwipsToUnits((x), 1.0f / (72.0f * 0.3937f))
00206 #define NS_TWIPS_TO_METERS(x)         NSTwipsToUnits((x), 1.0f / (72.0f * 39.37f))
00207 #define NS_TWIPS_TO_KILOMETERS(x)     NSTwipsToUnits((x), 1.0f / (72.0f * 39370.0f))
00208 
00209 #define NS_TWIPS_TO_PICAS(x)          NSTwipsToUnits((x), 1.0f / 12.0f)
00210 #define NS_TWIPS_TO_DIDOTS(x)         NSTwipsToUnits((x), 1.0f / (16.0f / 15.0f))
00211 #define NS_TWIPS_TO_CICEROS(x)        NSTwipsToUnits((x), 1.0f / (12.0f * (16.0f / 15.0f)))
00212 
00213 
00214 #endif