Bug 458861. Validate TrueType headers before activating downloaded font. r=roc, sr...
[wine-gecko.git] / layout / style / nsStyleCoord.cpp
blob16dd2afe858d77f4362e0782c53010665cb50024
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
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/
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.
15 * The Original Code is mozilla.org code.
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.
22 * Contributor(s):
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.
36 * ***** END LICENSE BLOCK ***** */
38 /* representation of length values in computed style data */
40 #include "nsStyleCoord.h"
41 #include "nsString.h"
42 #include "nsCRT.h"
43 #include "prlog.h"
45 nsStyleCoord::nsStyleCoord(nsStyleUnit aUnit)
46 : mUnit(aUnit)
48 NS_ASSERTION(aUnit < eStyleUnit_Percent, "not a valueless unit");
49 if (aUnit >= eStyleUnit_Percent) {
50 mUnit = eStyleUnit_Null;
52 mValue.mInt = 0;
55 nsStyleCoord::nsStyleCoord(nscoord aValue)
56 : mUnit(eStyleUnit_Coord)
58 mValue.mInt = aValue;
61 nsStyleCoord::nsStyleCoord(PRInt32 aValue, nsStyleUnit aUnit)
62 : mUnit(aUnit)
64 //if you want to pass in eStyleUnit_Coord, don't. instead, use the
65 //constructor just above this one... MMP
66 NS_ASSERTION((aUnit == eStyleUnit_Enumerated) ||
67 (aUnit == eStyleUnit_Integer), "not an int value");
68 if ((aUnit == eStyleUnit_Enumerated) ||
69 (aUnit == eStyleUnit_Integer)) {
70 mValue.mInt = aValue;
72 else {
73 mUnit = eStyleUnit_Null;
74 mValue.mInt = 0;
78 nsStyleCoord::nsStyleCoord(float aValue, nsStyleUnit aUnit)
79 : mUnit(aUnit)
81 NS_ASSERTION((aUnit == eStyleUnit_Percent) ||
82 (aUnit == eStyleUnit_Factor), "not a float value");
83 if ((aUnit == eStyleUnit_Percent) ||
84 (aUnit == eStyleUnit_Factor)) {
85 mValue.mFloat = aValue;
87 else {
88 mUnit = eStyleUnit_Null;
89 mValue.mInt = 0;
93 nsStyleCoord& nsStyleCoord::operator=(const nsStyleCoord& aCopy)
95 mUnit = aCopy.mUnit;
96 if ((eStyleUnit_Percent <= mUnit) && (mUnit < eStyleUnit_Coord)) {
97 mValue.mFloat = aCopy.mValue.mFloat;
99 else {
100 mValue.mInt = aCopy.mValue.mInt;
102 return *this;
105 PRBool nsStyleCoord::operator==(const nsStyleCoord& aOther) const
107 if (mUnit == aOther.mUnit) {
108 if ((eStyleUnit_Percent <= mUnit) && (mUnit < eStyleUnit_Coord)) {
109 return PRBool(mValue.mFloat == aOther.mValue.mFloat);
111 else {
112 return PRBool(mValue.mInt == aOther.mValue.mInt);
115 return PR_FALSE;
118 void nsStyleCoord::Reset(void)
120 mUnit = eStyleUnit_Null;
121 mValue.mInt = 0;
124 void nsStyleCoord::SetCoordValue(nscoord aValue)
126 mUnit = eStyleUnit_Coord;
127 mValue.mInt = aValue;
130 void nsStyleCoord::SetIntValue(PRInt32 aValue, nsStyleUnit aUnit)
132 NS_ASSERTION((aUnit == eStyleUnit_Enumerated) ||
133 (aUnit == eStyleUnit_Integer), "not an int value");
134 if ((aUnit == eStyleUnit_Enumerated) ||
135 (aUnit == eStyleUnit_Integer)) {
136 mUnit = aUnit;
137 mValue.mInt = aValue;
139 else {
140 Reset();
144 void nsStyleCoord::SetPercentValue(float aValue)
146 mUnit = eStyleUnit_Percent;
147 mValue.mFloat = aValue;
150 void nsStyleCoord::SetFactorValue(float aValue)
152 mUnit = eStyleUnit_Factor;
153 mValue.mFloat = aValue;
156 void nsStyleCoord::SetNormalValue(void)
158 mUnit = eStyleUnit_Normal;
159 mValue.mInt = 0;
162 void nsStyleCoord::SetAutoValue(void)
164 mUnit = eStyleUnit_Auto;
165 mValue.mInt = 0;
168 void nsStyleCoord::SetNoneValue(void)
170 mUnit = eStyleUnit_None;
171 mValue.mInt = 0;
174 #ifdef DEBUG
175 void nsStyleCoord::AppendToString(nsString& aBuffer) const
177 if ((eStyleUnit_Percent <= mUnit) && (mUnit < eStyleUnit_Coord)) {
178 aBuffer.AppendFloat(mValue.mFloat);
180 else if ((eStyleUnit_Coord == mUnit) ||
181 (eStyleUnit_Enumerated == mUnit) ||
182 (eStyleUnit_Integer == mUnit)) {
183 aBuffer.AppendInt(mValue.mInt, 10);
184 aBuffer.AppendLiteral("[0x");
185 aBuffer.AppendInt(mValue.mInt, 16);
186 aBuffer.Append(PRUnichar(']'));
189 switch (mUnit) {
190 case eStyleUnit_Null: aBuffer.AppendLiteral("Null"); break;
191 case eStyleUnit_Coord: aBuffer.AppendLiteral("tw"); break;
192 case eStyleUnit_Percent: aBuffer.AppendLiteral("%"); break;
193 case eStyleUnit_Factor: aBuffer.AppendLiteral("f"); break;
194 case eStyleUnit_Normal: aBuffer.AppendLiteral("Normal"); break;
195 case eStyleUnit_Auto: aBuffer.AppendLiteral("Auto"); break;
196 case eStyleUnit_None: aBuffer.AppendLiteral("None"); break;
197 case eStyleUnit_Enumerated: aBuffer.AppendLiteral("enum"); break;
198 case eStyleUnit_Integer: aBuffer.AppendLiteral("int"); break;
200 aBuffer.Append(PRUnichar(' '));
203 void nsStyleCoord::ToString(nsString& aBuffer) const
205 aBuffer.Truncate();
206 AppendToString(aBuffer);
208 #endif
210 // used by nsStyleSides and nsStyleCorners
211 #define COMPARE_INDEXED_COORD(i) \
212 PR_BEGIN_MACRO \
213 if (mUnits[i] != aOther.mUnits[i]) \
214 return PR_FALSE; \
215 if ((eStyleUnit_Percent <= mUnits[i]) && \
216 (mUnits[i] < eStyleUnit_Coord)) { \
217 if (mValues[i].mFloat != aOther.mValues[i].mFloat) \
218 return PR_FALSE; \
220 else { \
221 if (mValues[i].mInt != aOther.mValues[i].mInt) \
222 return PR_FALSE; \
224 PR_END_MACRO
227 nsStyleSides::nsStyleSides(void)
229 memset(this, 0x00, sizeof(nsStyleSides));
232 PRBool nsStyleSides::operator==(const nsStyleSides& aOther) const
234 NS_FOR_CSS_SIDES(i) {
235 COMPARE_INDEXED_COORD(i);
237 return PR_TRUE;
240 void nsStyleSides::Reset(void)
242 memset(this, 0x00, sizeof(nsStyleSides));
245 #ifdef DEBUG
246 void nsStyleSides::AppendToString(nsString& aBuffer) const
248 aBuffer.AppendLiteral("left: ");
249 GetLeft().AppendToString(aBuffer);
251 aBuffer.AppendLiteral("top: ");
252 GetTop().AppendToString(aBuffer);
254 aBuffer.AppendLiteral("right: ");
255 GetRight().AppendToString(aBuffer);
257 aBuffer.AppendLiteral("bottom: ");
258 GetBottom().AppendToString(aBuffer);
261 void nsStyleSides::ToString(nsString& aBuffer) const
263 aBuffer.Truncate();
264 AppendToString(aBuffer);
266 #endif
268 nsStyleCorners::nsStyleCorners()
270 memset(this, 0x00, sizeof(nsStyleCorners));
273 PRBool
274 nsStyleCorners::operator==(const nsStyleCorners& aOther) const
276 NS_FOR_CSS_HALF_CORNERS(i) {
277 COMPARE_INDEXED_COORD(i);
279 return PR_TRUE;
282 void nsStyleCorners::Reset(void)
284 memset(this, 0x00, sizeof(nsStyleCorners));
287 #ifdef DEBUG
288 void nsStyleCorners::AppendToString(nsString& aBuffer) const
290 aBuffer.AppendLiteral("top-left: ");
291 Get(NS_CORNER_TOP_LEFT_X).AppendToString(aBuffer);
292 Get(NS_CORNER_TOP_LEFT_Y).AppendToString(aBuffer);
294 aBuffer.AppendLiteral("top-right: ");
295 Get(NS_CORNER_TOP_RIGHT_X).AppendToString(aBuffer);
296 Get(NS_CORNER_TOP_RIGHT_Y).AppendToString(aBuffer);
298 aBuffer.AppendLiteral("bottom-right: ");
299 Get(NS_CORNER_BOTTOM_RIGHT_X).AppendToString(aBuffer);
300 Get(NS_CORNER_BOTTOM_RIGHT_Y).AppendToString(aBuffer);
302 aBuffer.AppendLiteral("bottom-left: ");
303 Get(NS_CORNER_BOTTOM_LEFT_X).AppendToString(aBuffer);
304 Get(NS_CORNER_BOTTOM_LEFT_Y).AppendToString(aBuffer);
307 void nsStyleCorners::ToString(nsString& aBuffer) const
309 aBuffer.Truncate();
310 AppendToString(aBuffer);
312 #endif
314 // Validation of NS_SIDE_IS_VERTICAL and NS_HALF_CORNER_IS_X.
315 #define CASE(side, result) \
316 PR_STATIC_ASSERT(NS_SIDE_IS_VERTICAL(side) == result)
317 CASE(NS_SIDE_TOP, PR_FALSE);
318 CASE(NS_SIDE_RIGHT, PR_TRUE);
319 CASE(NS_SIDE_BOTTOM, PR_FALSE);
320 CASE(NS_SIDE_LEFT, PR_TRUE);
321 #undef CASE
323 #define CASE(corner, result) \
324 PR_STATIC_ASSERT(NS_HALF_CORNER_IS_X(corner) == result)
325 CASE(NS_CORNER_TOP_LEFT_X, PR_TRUE);
326 CASE(NS_CORNER_TOP_LEFT_Y, PR_FALSE);
327 CASE(NS_CORNER_TOP_RIGHT_X, PR_TRUE);
328 CASE(NS_CORNER_TOP_RIGHT_Y, PR_FALSE);
329 CASE(NS_CORNER_BOTTOM_RIGHT_X, PR_TRUE);
330 CASE(NS_CORNER_BOTTOM_RIGHT_Y, PR_FALSE);
331 CASE(NS_CORNER_BOTTOM_LEFT_X, PR_TRUE);
332 CASE(NS_CORNER_BOTTOM_LEFT_Y, PR_FALSE);
333 #undef CASE
335 // Validation of NS_HALF_TO_FULL_CORNER.
336 #define CASE(corner, result) \
337 PR_STATIC_ASSERT(NS_HALF_TO_FULL_CORNER(corner) == result)
338 CASE(NS_CORNER_TOP_LEFT_X, NS_CORNER_TOP_LEFT);
339 CASE(NS_CORNER_TOP_LEFT_Y, NS_CORNER_TOP_LEFT);
340 CASE(NS_CORNER_TOP_RIGHT_X, NS_CORNER_TOP_RIGHT);
341 CASE(NS_CORNER_TOP_RIGHT_Y, NS_CORNER_TOP_RIGHT);
342 CASE(NS_CORNER_BOTTOM_RIGHT_X, NS_CORNER_BOTTOM_RIGHT);
343 CASE(NS_CORNER_BOTTOM_RIGHT_Y, NS_CORNER_BOTTOM_RIGHT);
344 CASE(NS_CORNER_BOTTOM_LEFT_X, NS_CORNER_BOTTOM_LEFT);
345 CASE(NS_CORNER_BOTTOM_LEFT_Y, NS_CORNER_BOTTOM_LEFT);
346 #undef CASE
348 // Validation of NS_FULL_TO_HALF_CORNER.
349 #define CASE(corner, vert, result) \
350 PR_STATIC_ASSERT(NS_FULL_TO_HALF_CORNER(corner, vert) == result)
351 CASE(NS_CORNER_TOP_LEFT, PR_FALSE, NS_CORNER_TOP_LEFT_X);
352 CASE(NS_CORNER_TOP_LEFT, PR_TRUE, NS_CORNER_TOP_LEFT_Y);
353 CASE(NS_CORNER_TOP_RIGHT, PR_FALSE, NS_CORNER_TOP_RIGHT_X);
354 CASE(NS_CORNER_TOP_RIGHT, PR_TRUE, NS_CORNER_TOP_RIGHT_Y);
355 CASE(NS_CORNER_BOTTOM_RIGHT, PR_FALSE, NS_CORNER_BOTTOM_RIGHT_X);
356 CASE(NS_CORNER_BOTTOM_RIGHT, PR_TRUE, NS_CORNER_BOTTOM_RIGHT_Y);
357 CASE(NS_CORNER_BOTTOM_LEFT, PR_FALSE, NS_CORNER_BOTTOM_LEFT_X);
358 CASE(NS_CORNER_BOTTOM_LEFT, PR_TRUE, NS_CORNER_BOTTOM_LEFT_Y);
359 #undef CASE
361 // Validation of NS_SIDE_TO_{FULL,HALF}_CORNER.
362 #define CASE(side, second, result) \
363 PR_STATIC_ASSERT(NS_SIDE_TO_FULL_CORNER(side, second) == result)
364 CASE(NS_SIDE_TOP, PR_FALSE, NS_CORNER_TOP_LEFT);
365 CASE(NS_SIDE_TOP, PR_TRUE, NS_CORNER_TOP_RIGHT);
367 CASE(NS_SIDE_RIGHT, PR_FALSE, NS_CORNER_TOP_RIGHT);
368 CASE(NS_SIDE_RIGHT, PR_TRUE, NS_CORNER_BOTTOM_RIGHT);
370 CASE(NS_SIDE_BOTTOM, PR_FALSE, NS_CORNER_BOTTOM_RIGHT);
371 CASE(NS_SIDE_BOTTOM, PR_TRUE, NS_CORNER_BOTTOM_LEFT);
373 CASE(NS_SIDE_LEFT, PR_FALSE, NS_CORNER_BOTTOM_LEFT);
374 CASE(NS_SIDE_LEFT, PR_TRUE, NS_CORNER_TOP_LEFT);
375 #undef CASE
377 #define CASE(side, second, parallel, result) \
378 PR_STATIC_ASSERT(NS_SIDE_TO_HALF_CORNER(side, second, parallel) == result)
379 CASE(NS_SIDE_TOP, PR_FALSE, PR_TRUE, NS_CORNER_TOP_LEFT_X);
380 CASE(NS_SIDE_TOP, PR_FALSE, PR_FALSE, NS_CORNER_TOP_LEFT_Y);
381 CASE(NS_SIDE_TOP, PR_TRUE, PR_TRUE, NS_CORNER_TOP_RIGHT_X);
382 CASE(NS_SIDE_TOP, PR_TRUE, PR_FALSE, NS_CORNER_TOP_RIGHT_Y);
384 CASE(NS_SIDE_RIGHT, PR_FALSE, PR_FALSE, NS_CORNER_TOP_RIGHT_X);
385 CASE(NS_SIDE_RIGHT, PR_FALSE, PR_TRUE, NS_CORNER_TOP_RIGHT_Y);
386 CASE(NS_SIDE_RIGHT, PR_TRUE, PR_FALSE, NS_CORNER_BOTTOM_RIGHT_X);
387 CASE(NS_SIDE_RIGHT, PR_TRUE, PR_TRUE, NS_CORNER_BOTTOM_RIGHT_Y);
389 CASE(NS_SIDE_BOTTOM, PR_FALSE, PR_TRUE, NS_CORNER_BOTTOM_RIGHT_X);
390 CASE(NS_SIDE_BOTTOM, PR_FALSE, PR_FALSE, NS_CORNER_BOTTOM_RIGHT_Y);
391 CASE(NS_SIDE_BOTTOM, PR_TRUE, PR_TRUE, NS_CORNER_BOTTOM_LEFT_X);
392 CASE(NS_SIDE_BOTTOM, PR_TRUE, PR_FALSE, NS_CORNER_BOTTOM_LEFT_Y);
394 CASE(NS_SIDE_LEFT, PR_FALSE, PR_FALSE, NS_CORNER_BOTTOM_LEFT_X);
395 CASE(NS_SIDE_LEFT, PR_FALSE, PR_TRUE, NS_CORNER_BOTTOM_LEFT_Y);
396 CASE(NS_SIDE_LEFT, PR_TRUE, PR_FALSE, NS_CORNER_TOP_LEFT_X);
397 CASE(NS_SIDE_LEFT, PR_TRUE, PR_TRUE, NS_CORNER_TOP_LEFT_Y);
398 #undef CASE