VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/xpcom/ds/nsUnitConversion.h@ 99553

Last change on this file since 99553 was 1, checked in by vboxsync, 55 years ago

import

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.9 KB
Line 
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
4 *
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/
9 *
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
13 * License.
14 *
15 * The Original Code is mozilla.org code.
16 *
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.
21 *
22 * Contributor(s):
23 *
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.
35 *
36 * ***** END LICENSE BLOCK ***** */
37#ifndef nsUnitConversion_h__
38#define nsUnitConversion_h__
39
40#include "nscore.h"
41#include "nsCoord.h"
42#include <math.h>
43#include <float.h>
44
45#ifndef FLT_EPSILON
46// Not an ANSI compiler... oh, well. Use an IEEE value.
47#define FLT_EPSILON 1.19209290e-7f
48#endif
49/// handy constants
50#define TWIPS_PER_POINT_INT 20
51#define TWIPS_PER_POINT_FLOAT 20.0f
52#define CEIL_CONST_FLOAT (1.0f - 0.5f*FLT_EPSILON)
53#define ROUND_EXCLUSIVE_CONST_FLOAT (0.5f*CEIL_CONST_FLOAT)
54#define ROUND_CONST_FLOAT 0.5f
55
56
57/*
58 * Coord Rounding Functions
59 */
60inline nscoord NSToCoordFloor(float aValue)
61{
62 return ((0.0f <= aValue) ? nscoord(aValue) : nscoord(aValue - CEIL_CONST_FLOAT));
63}
64
65inline nscoord NSToCoordCeil(float aValue)
66{
67 return ((0.0f <= aValue) ? nscoord(aValue + CEIL_CONST_FLOAT) : nscoord(aValue));
68}
69
70inline nscoord NSToCoordRound(float aValue)
71{
72 return ((0.0f <= aValue) ? nscoord(aValue + ROUND_CONST_FLOAT) : nscoord(aValue - ROUND_CONST_FLOAT));
73}
74
75inline nscoord NSToCoordRoundExclusive(float aValue)
76{
77 return ((0.0f <= aValue) ? nscoord(aValue + ROUND_EXCLUSIVE_CONST_FLOAT) :
78 nscoord(aValue - ROUND_EXCLUSIVE_CONST_FLOAT));
79}
80
81
82/*
83 * Int Rounding Functions
84 */
85inline PRInt32 NSToIntFloor(float aValue)
86{
87 return ((0.0f <= aValue) ? PRInt32(aValue) : PRInt32(aValue - CEIL_CONST_FLOAT));
88}
89
90inline PRInt32 NSToIntCeil(float aValue)
91{
92 return ((0.0f <= aValue) ? PRInt32(aValue + CEIL_CONST_FLOAT) : PRInt32(aValue));
93}
94
95inline PRInt32 NSToIntRound(float aValue)
96{
97 return ((0.0f <= aValue) ? PRInt32(aValue + ROUND_CONST_FLOAT) : PRInt32(aValue - ROUND_CONST_FLOAT));
98}
99
100inline PRInt32 NSToIntRoundExclusive(float aValue)
101{
102 return ((0.0f <= aValue) ? PRInt32(aValue + ROUND_EXCLUSIVE_CONST_FLOAT) :
103 PRInt32(aValue - ROUND_EXCLUSIVE_CONST_FLOAT));
104}
105
106
107/*
108 * Twips/Points conversions
109 */
110inline nscoord NSFloatPointsToTwips(float aPoints)
111{
112 return NSToCoordRound(aPoints * TWIPS_PER_POINT_FLOAT);
113}
114
115inline nscoord NSIntPointsToTwips(PRInt32 aPoints)
116{
117 return nscoord(aPoints * TWIPS_PER_POINT_INT);
118}
119
120inline PRInt32 NSTwipsToIntPoints(nscoord aTwips)
121{
122 return NSToIntRound(aTwips / TWIPS_PER_POINT_FLOAT);
123}
124
125inline PRInt32 NSTwipsToFloorIntPoints(nscoord aTwips)
126{
127 return NSToIntFloor(aTwips / TWIPS_PER_POINT_FLOAT);
128}
129
130inline PRInt32 NSTwipsToCeilIntPoints(nscoord aTwips)
131{
132 return NSToIntCeil(aTwips / TWIPS_PER_POINT_FLOAT);
133}
134
135inline float NSTwipsToFloatPoints(nscoord aTwips)
136{
137 return (float(aTwips) / TWIPS_PER_POINT_FLOAT);
138}
139
140/*
141 * Twips/Pixel conversions
142 */
143inline nscoord NSFloatPixelsToTwips(float aPixels, float aTwipsPerPixel)
144{
145 return NSToCoordRound(aPixels * aTwipsPerPixel);
146}
147
148inline nscoord NSIntPixelsToTwips(PRInt32 aPixels, float aTwipsPerPixel)
149{
150 return NSToCoordRound(float(aPixels) * aTwipsPerPixel);
151}
152
153inline float NSTwipsToFloatPixels(nscoord aTwips, float aPixelsPerTwip)
154{
155 return (float(aTwips) * aPixelsPerTwip);
156}
157
158inline PRInt32 NSTwipsToIntPixels(nscoord aTwips, float aPixelsPerTwip)
159{
160 return NSToIntRound(float(aTwips) * aPixelsPerTwip);
161}
162
163/*
164 * Twips/unit conversions
165 */
166inline nscoord NSUnitsToTwips(float aValue, float aPointsPerUnit)
167{
168 return NSToCoordRound(aValue * aPointsPerUnit * TWIPS_PER_POINT_FLOAT);
169}
170
171inline float NSTwipsToUnits(nscoord aTwips, float aUnitsPerPoint)
172{
173 return (aTwips * (aUnitsPerPoint / TWIPS_PER_POINT_FLOAT));
174}
175
176
177/// Unit conversion macros
178//@{
179#define NS_INCHES_TO_TWIPS(x) NSUnitsToTwips((x), 72.0f) // 72 points per inch
180#define NS_FEET_TO_TWIPS(x) NSUnitsToTwips((x), (72.0f * 12.0f)) // 12 inches per foot
181#define NS_MILES_TO_TWIPS(x) NSUnitsToTwips((x), (72.0f * 12.0f * 5280.0f)) // 5280 feet per mile
182
183#define NS_MILLIMETERS_TO_TWIPS(x) NSUnitsToTwips((x), (72.0f * 0.03937f))
184#define NS_CENTIMETERS_TO_TWIPS(x) NSUnitsToTwips((x), (72.0f * 0.3937f))
185#define NS_METERS_TO_TWIPS(x) NSUnitsToTwips((x), (72.0f * 39.37f))
186#define NS_KILOMETERS_TO_TWIPS(x) NSUnitsToTwips((x), (72.0f * 39370.0f))
187
188#define NS_PICAS_TO_TWIPS(x) NSUnitsToTwips((x), 12.0f) // 12 points per pica
189#define NS_DIDOTS_TO_TWIPS(x) NSUnitsToTwips((x), (16.0f / 15.0f)) // 15 didots per 16 points
190#define NS_CICEROS_TO_TWIPS(x) NSUnitsToTwips((x), (12.0f * (16.0f / 15.0f))) // 12 didots per cicero
191
192
193#define NS_TWIPS_TO_INCHES(x) NSTwipsToUnits((x), 1.0f / 72.0f)
194#define NS_TWIPS_TO_FEET(x) NSTwipsToUnits((x), 1.0f / (72.0f * 12.0f))
195#define NS_TWIPS_TO_MILES(x) NSTwipsToUnits((x), 1.0f / (72.0f * 12.0f * 5280.0f))
196
197#define NS_TWIPS_TO_MILLIMETERS(x) NSTwipsToUnits((x), 1.0f / (72.0f * 0.03937f))
198#define NS_TWIPS_TO_CENTIMETERS(x) NSTwipsToUnits((x), 1.0f / (72.0f * 0.3937f))
199#define NS_TWIPS_TO_METERS(x) NSTwipsToUnits((x), 1.0f / (72.0f * 39.37f))
200#define NS_TWIPS_TO_KILOMETERS(x) NSTwipsToUnits((x), 1.0f / (72.0f * 39370.0f))
201
202#define NS_TWIPS_TO_PICAS(x) NSTwipsToUnits((x), 1.0f / 12.0f)
203#define NS_TWIPS_TO_DIDOTS(x) NSTwipsToUnits((x), 1.0f / (16.0f / 15.0f))
204#define NS_TWIPS_TO_CICEROS(x) NSTwipsToUnits((x), 1.0f / (12.0f * (16.0f / 15.0f)))
205//@}
206
207#endif
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette