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 :
38 : /* representation of length values in computed style data */
39 :
40 : #include "nsStyleCoord.h"
41 : #include "nsString.h"
42 : #include "nsCRT.h"
43 : #include "prlog.h"
44 : #include "nsMathUtils.h"
45 : #include "nsStyleContext.h"
46 :
47 0 : nsStyleCoord::nsStyleCoord(nsStyleUnit aUnit)
48 0 : : mUnit(aUnit)
49 : {
50 0 : NS_ASSERTION(aUnit < eStyleUnit_Percent, "not a valueless unit");
51 0 : if (aUnit >= eStyleUnit_Percent) {
52 0 : mUnit = eStyleUnit_Null;
53 : }
54 0 : mValue.mInt = 0;
55 0 : }
56 :
57 0 : nsStyleCoord::nsStyleCoord(PRInt32 aValue, nsStyleUnit aUnit)
58 0 : : mUnit(aUnit)
59 : {
60 : //if you want to pass in eStyleUnit_Coord, don't. instead, use the
61 : //constructor just above this one... MMP
62 0 : NS_ASSERTION((aUnit == eStyleUnit_Enumerated) ||
63 : (aUnit == eStyleUnit_Integer), "not an int value");
64 0 : if ((aUnit == eStyleUnit_Enumerated) ||
65 : (aUnit == eStyleUnit_Integer)) {
66 0 : mValue.mInt = aValue;
67 : }
68 : else {
69 0 : mUnit = eStyleUnit_Null;
70 0 : mValue.mInt = 0;
71 : }
72 0 : }
73 :
74 0 : nsStyleCoord::nsStyleCoord(float aValue, nsStyleUnit aUnit)
75 0 : : mUnit(aUnit)
76 : {
77 0 : if (aUnit < eStyleUnit_Percent || aUnit >= eStyleUnit_Coord) {
78 0 : NS_NOTREACHED("not a float value");
79 0 : Reset();
80 : } else {
81 0 : mValue.mFloat = aValue;
82 : }
83 0 : }
84 :
85 0 : bool nsStyleCoord::operator==(const nsStyleCoord& aOther) const
86 : {
87 0 : if (mUnit != aOther.mUnit) {
88 0 : return false;
89 : }
90 0 : switch (mUnit) {
91 : case eStyleUnit_Null:
92 : case eStyleUnit_Normal:
93 : case eStyleUnit_Auto:
94 : case eStyleUnit_None:
95 0 : return true;
96 : case eStyleUnit_Percent:
97 : case eStyleUnit_Factor:
98 : case eStyleUnit_Degree:
99 : case eStyleUnit_Grad:
100 : case eStyleUnit_Radian:
101 : case eStyleUnit_Turn:
102 0 : return mValue.mFloat == aOther.mValue.mFloat;
103 : case eStyleUnit_Coord:
104 : case eStyleUnit_Integer:
105 : case eStyleUnit_Enumerated:
106 0 : return mValue.mInt == aOther.mValue.mInt;
107 : case eStyleUnit_Calc:
108 0 : return *this->GetCalcValue() == *aOther.GetCalcValue();
109 : }
110 0 : NS_ABORT_IF_FALSE(false, "unexpected unit");
111 0 : return false;
112 : }
113 :
114 0 : void nsStyleCoord::Reset()
115 : {
116 0 : mUnit = eStyleUnit_Null;
117 0 : mValue.mInt = 0;
118 0 : }
119 :
120 0 : void nsStyleCoord::SetCoordValue(nscoord aValue)
121 : {
122 0 : mUnit = eStyleUnit_Coord;
123 0 : mValue.mInt = aValue;
124 0 : }
125 :
126 0 : void nsStyleCoord::SetIntValue(PRInt32 aValue, nsStyleUnit aUnit)
127 : {
128 0 : NS_ASSERTION((aUnit == eStyleUnit_Enumerated) ||
129 : (aUnit == eStyleUnit_Integer), "not an int value");
130 0 : if ((aUnit == eStyleUnit_Enumerated) ||
131 : (aUnit == eStyleUnit_Integer)) {
132 0 : mUnit = aUnit;
133 0 : mValue.mInt = aValue;
134 : }
135 : else {
136 0 : Reset();
137 : }
138 0 : }
139 :
140 0 : void nsStyleCoord::SetPercentValue(float aValue)
141 : {
142 0 : mUnit = eStyleUnit_Percent;
143 0 : mValue.mFloat = aValue;
144 0 : }
145 :
146 0 : void nsStyleCoord::SetFactorValue(float aValue)
147 : {
148 0 : mUnit = eStyleUnit_Factor;
149 0 : mValue.mFloat = aValue;
150 0 : }
151 :
152 0 : void nsStyleCoord::SetAngleValue(float aValue, nsStyleUnit aUnit)
153 : {
154 0 : if (aUnit == eStyleUnit_Degree ||
155 : aUnit == eStyleUnit_Grad ||
156 : aUnit == eStyleUnit_Radian ||
157 : aUnit == eStyleUnit_Turn) {
158 0 : mUnit = aUnit;
159 0 : mValue.mFloat = aValue;
160 : } else {
161 0 : NS_NOTREACHED("not an angle value");
162 0 : Reset();
163 : }
164 0 : }
165 :
166 0 : void nsStyleCoord::SetCalcValue(Calc* aValue)
167 : {
168 0 : mUnit = eStyleUnit_Calc;
169 0 : mValue.mPointer = aValue;
170 0 : }
171 :
172 0 : void nsStyleCoord::SetNormalValue()
173 : {
174 0 : mUnit = eStyleUnit_Normal;
175 0 : mValue.mInt = 0;
176 0 : }
177 :
178 0 : void nsStyleCoord::SetAutoValue()
179 : {
180 0 : mUnit = eStyleUnit_Auto;
181 0 : mValue.mInt = 0;
182 0 : }
183 :
184 0 : void nsStyleCoord::SetNoneValue()
185 : {
186 0 : mUnit = eStyleUnit_None;
187 0 : mValue.mInt = 0;
188 0 : }
189 :
190 : // accessors that are not inlined
191 :
192 : double
193 0 : nsStyleCoord::GetAngleValueInRadians() const
194 : {
195 0 : double angle = mValue.mFloat;
196 :
197 0 : switch (GetUnit()) {
198 0 : case eStyleUnit_Radian: return angle;
199 0 : case eStyleUnit_Turn: return angle * 2 * M_PI;
200 0 : case eStyleUnit_Degree: return angle * M_PI / 180.0;
201 0 : case eStyleUnit_Grad: return angle * M_PI / 200.0;
202 :
203 : default:
204 0 : NS_NOTREACHED("unrecognized angular unit");
205 0 : return 0.0;
206 : }
207 : }
208 :
209 : // used by nsStyleSides and nsStyleCorners
210 : #define COMPARE_INDEXED_COORD(i) \
211 : PR_BEGIN_MACRO \
212 : if (mUnits[i] != aOther.mUnits[i]) \
213 : return false; \
214 : if ((eStyleUnit_Percent <= mUnits[i]) && \
215 : (mUnits[i] < eStyleUnit_Coord)) { \
216 : if (mValues[i].mFloat != aOther.mValues[i].mFloat) \
217 : return false; \
218 : } \
219 : else { \
220 : if (mValues[i].mInt != aOther.mValues[i].mInt) \
221 : return false; \
222 : } \
223 : PR_END_MACRO
224 :
225 :
226 0 : nsStyleSides::nsStyleSides()
227 : {
228 0 : memset(this, 0x00, sizeof(nsStyleSides));
229 0 : }
230 :
231 0 : bool nsStyleSides::operator==(const nsStyleSides& aOther) const
232 : {
233 0 : NS_FOR_CSS_SIDES(i) {
234 0 : COMPARE_INDEXED_COORD(i);
235 : }
236 0 : return true;
237 : }
238 :
239 0 : void nsStyleSides::Reset()
240 : {
241 0 : memset(this, 0x00, sizeof(nsStyleSides));
242 0 : }
243 :
244 0 : nsStyleCorners::nsStyleCorners()
245 : {
246 0 : memset(this, 0x00, sizeof(nsStyleCorners));
247 0 : }
248 :
249 : bool
250 0 : nsStyleCorners::operator==(const nsStyleCorners& aOther) const
251 : {
252 0 : NS_FOR_CSS_HALF_CORNERS(i) {
253 0 : COMPARE_INDEXED_COORD(i);
254 : }
255 0 : return true;
256 : }
257 :
258 0 : void nsStyleCorners::Reset()
259 : {
260 0 : memset(this, 0x00, sizeof(nsStyleCorners));
261 0 : }
262 :
263 : // Validation of NS_SIDE_IS_VERTICAL and NS_HALF_CORNER_IS_X.
264 : #define CASE(side, result) \
265 : MOZ_STATIC_ASSERT(NS_SIDE_IS_VERTICAL(side) == result, \
266 : "NS_SIDE_IS_VERTICAL is wrong")
267 : CASE(NS_SIDE_TOP, false);
268 : CASE(NS_SIDE_RIGHT, true);
269 : CASE(NS_SIDE_BOTTOM, false);
270 : CASE(NS_SIDE_LEFT, true);
271 : #undef CASE
272 :
273 : #define CASE(corner, result) \
274 : MOZ_STATIC_ASSERT(NS_HALF_CORNER_IS_X(corner) == result, \
275 : "NS_HALF_CORNER_IS_X is wrong")
276 : CASE(NS_CORNER_TOP_LEFT_X, true);
277 : CASE(NS_CORNER_TOP_LEFT_Y, false);
278 : CASE(NS_CORNER_TOP_RIGHT_X, true);
279 : CASE(NS_CORNER_TOP_RIGHT_Y, false);
280 : CASE(NS_CORNER_BOTTOM_RIGHT_X, true);
281 : CASE(NS_CORNER_BOTTOM_RIGHT_Y, false);
282 : CASE(NS_CORNER_BOTTOM_LEFT_X, true);
283 : CASE(NS_CORNER_BOTTOM_LEFT_Y, false);
284 : #undef CASE
285 :
286 : // Validation of NS_HALF_TO_FULL_CORNER.
287 : #define CASE(corner, result) \
288 : MOZ_STATIC_ASSERT(NS_HALF_TO_FULL_CORNER(corner) == result, \
289 : "NS_HALF_TO_FULL_CORNER is wrong")
290 : CASE(NS_CORNER_TOP_LEFT_X, NS_CORNER_TOP_LEFT);
291 : CASE(NS_CORNER_TOP_LEFT_Y, NS_CORNER_TOP_LEFT);
292 : CASE(NS_CORNER_TOP_RIGHT_X, NS_CORNER_TOP_RIGHT);
293 : CASE(NS_CORNER_TOP_RIGHT_Y, NS_CORNER_TOP_RIGHT);
294 : CASE(NS_CORNER_BOTTOM_RIGHT_X, NS_CORNER_BOTTOM_RIGHT);
295 : CASE(NS_CORNER_BOTTOM_RIGHT_Y, NS_CORNER_BOTTOM_RIGHT);
296 : CASE(NS_CORNER_BOTTOM_LEFT_X, NS_CORNER_BOTTOM_LEFT);
297 : CASE(NS_CORNER_BOTTOM_LEFT_Y, NS_CORNER_BOTTOM_LEFT);
298 : #undef CASE
299 :
300 : // Validation of NS_FULL_TO_HALF_CORNER.
301 : #define CASE(corner, vert, result) \
302 : MOZ_STATIC_ASSERT(NS_FULL_TO_HALF_CORNER(corner, vert) == result, \
303 : "NS_FULL_TO_HALF_CORNER is wrong")
304 : CASE(NS_CORNER_TOP_LEFT, false, NS_CORNER_TOP_LEFT_X);
305 : CASE(NS_CORNER_TOP_LEFT, true, NS_CORNER_TOP_LEFT_Y);
306 : CASE(NS_CORNER_TOP_RIGHT, false, NS_CORNER_TOP_RIGHT_X);
307 : CASE(NS_CORNER_TOP_RIGHT, true, NS_CORNER_TOP_RIGHT_Y);
308 : CASE(NS_CORNER_BOTTOM_RIGHT, false, NS_CORNER_BOTTOM_RIGHT_X);
309 : CASE(NS_CORNER_BOTTOM_RIGHT, true, NS_CORNER_BOTTOM_RIGHT_Y);
310 : CASE(NS_CORNER_BOTTOM_LEFT, false, NS_CORNER_BOTTOM_LEFT_X);
311 : CASE(NS_CORNER_BOTTOM_LEFT, true, NS_CORNER_BOTTOM_LEFT_Y);
312 : #undef CASE
313 :
314 : // Validation of NS_SIDE_TO_{FULL,HALF}_CORNER.
315 : #define CASE(side, second, result) \
316 : MOZ_STATIC_ASSERT(NS_SIDE_TO_FULL_CORNER(side, second) == result, \
317 : "NS_SIDE_TO_FULL_CORNER is wrong")
318 : CASE(NS_SIDE_TOP, false, NS_CORNER_TOP_LEFT);
319 : CASE(NS_SIDE_TOP, true, NS_CORNER_TOP_RIGHT);
320 :
321 : CASE(NS_SIDE_RIGHT, false, NS_CORNER_TOP_RIGHT);
322 : CASE(NS_SIDE_RIGHT, true, NS_CORNER_BOTTOM_RIGHT);
323 :
324 : CASE(NS_SIDE_BOTTOM, false, NS_CORNER_BOTTOM_RIGHT);
325 : CASE(NS_SIDE_BOTTOM, true, NS_CORNER_BOTTOM_LEFT);
326 :
327 : CASE(NS_SIDE_LEFT, false, NS_CORNER_BOTTOM_LEFT);
328 : CASE(NS_SIDE_LEFT, true, NS_CORNER_TOP_LEFT);
329 : #undef CASE
330 :
331 : #define CASE(side, second, parallel, result) \
332 : MOZ_STATIC_ASSERT(NS_SIDE_TO_HALF_CORNER(side, second, parallel) == result, \
333 : "NS_SIDE_TO_HALF_CORNER is wrong")
334 : CASE(NS_SIDE_TOP, false, true, NS_CORNER_TOP_LEFT_X);
335 : CASE(NS_SIDE_TOP, false, false, NS_CORNER_TOP_LEFT_Y);
336 : CASE(NS_SIDE_TOP, true, true, NS_CORNER_TOP_RIGHT_X);
337 : CASE(NS_SIDE_TOP, true, false, NS_CORNER_TOP_RIGHT_Y);
338 :
339 : CASE(NS_SIDE_RIGHT, false, false, NS_CORNER_TOP_RIGHT_X);
340 : CASE(NS_SIDE_RIGHT, false, true, NS_CORNER_TOP_RIGHT_Y);
341 : CASE(NS_SIDE_RIGHT, true, false, NS_CORNER_BOTTOM_RIGHT_X);
342 : CASE(NS_SIDE_RIGHT, true, true, NS_CORNER_BOTTOM_RIGHT_Y);
343 :
344 : CASE(NS_SIDE_BOTTOM, false, true, NS_CORNER_BOTTOM_RIGHT_X);
345 : CASE(NS_SIDE_BOTTOM, false, false, NS_CORNER_BOTTOM_RIGHT_Y);
346 : CASE(NS_SIDE_BOTTOM, true, true, NS_CORNER_BOTTOM_LEFT_X);
347 : CASE(NS_SIDE_BOTTOM, true, false, NS_CORNER_BOTTOM_LEFT_Y);
348 :
349 : CASE(NS_SIDE_LEFT, false, false, NS_CORNER_BOTTOM_LEFT_X);
350 : CASE(NS_SIDE_LEFT, false, true, NS_CORNER_BOTTOM_LEFT_Y);
351 : CASE(NS_SIDE_LEFT, true, false, NS_CORNER_TOP_LEFT_X);
352 : CASE(NS_SIDE_LEFT, true, true, NS_CORNER_TOP_LEFT_Y);
353 : #undef CASE
|