-
Notifications
You must be signed in to change notification settings - Fork 54
/
nsStyleCoord.cpp
333 lines (286 loc) · 9.71 KB
/
nsStyleCoord.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/* representation of length values in computed style data */
#include "mozilla/HashFunctions.h"
#include "mozilla/PodOperations.h"
// nsStyleCoord.h must not be the first header in a unified source file,
// otherwise it may not build with MSVC due to a bug in our STL wrapper.
// See bug 1331102.
#include "nsStyleCoord.h"
using namespace mozilla;
nsStyleCoord::nsStyleCoord(nsStyleUnit aUnit) : mUnit(aUnit) {
NS_ASSERTION(aUnit < eStyleUnit_Percent, "not a valueless unit");
if (aUnit >= eStyleUnit_Percent) {
mUnit = eStyleUnit_Null;
}
mValue.mInt = 0;
}
nsStyleCoord::nsStyleCoord(int32_t aValue, nsStyleUnit aUnit) : mUnit(aUnit) {
// if you want to pass in eStyleUnit_Coord, don't. instead, use the
// constructor just above this one... MMP
NS_ASSERTION(
(aUnit == eStyleUnit_Enumerated) || (aUnit == eStyleUnit_Integer),
"not an int value");
if ((aUnit == eStyleUnit_Enumerated) || (aUnit == eStyleUnit_Integer)) {
mValue.mInt = aValue;
} else {
mUnit = eStyleUnit_Null;
mValue.mInt = 0;
}
}
nsStyleCoord::nsStyleCoord(float aValue, nsStyleUnit aUnit) : mUnit(aUnit) {
if (aUnit < eStyleUnit_Percent || aUnit >= eStyleUnit_Coord) {
MOZ_ASSERT_UNREACHABLE("not a float value");
mUnit = eStyleUnit_Null;
mValue.mInt = 0;
} else {
mValue.mFloat = aValue;
}
}
bool nsStyleCoord::operator==(const nsStyleCoord& aOther) const {
if (mUnit != aOther.mUnit) {
return false;
}
switch (mUnit) {
case eStyleUnit_Null:
case eStyleUnit_Normal:
case eStyleUnit_Auto:
case eStyleUnit_None:
return true;
case eStyleUnit_Percent:
case eStyleUnit_Factor:
case eStyleUnit_Degree:
case eStyleUnit_FlexFraction:
return mValue.mFloat == aOther.mValue.mFloat;
case eStyleUnit_Coord:
case eStyleUnit_Integer:
case eStyleUnit_Enumerated:
return mValue.mInt == aOther.mValue.mInt;
case eStyleUnit_Calc:
return *this->GetCalcValue() == *aOther.GetCalcValue();
}
MOZ_ASSERT(false, "unexpected unit");
return false;
}
void nsStyleCoord::Reset() { Reset(mUnit, mValue); }
void nsStyleCoord::SetCoordValue(nscoord aValue) {
Reset();
mUnit = eStyleUnit_Coord;
mValue.mInt = aValue;
}
void nsStyleCoord::SetIntValue(int32_t aValue, nsStyleUnit aUnit) {
NS_ASSERTION(
(aUnit == eStyleUnit_Enumerated) || (aUnit == eStyleUnit_Integer),
"not an int value");
Reset();
if ((aUnit == eStyleUnit_Enumerated) || (aUnit == eStyleUnit_Integer)) {
mUnit = aUnit;
mValue.mInt = aValue;
}
}
void nsStyleCoord::SetPercentValue(float aValue) {
Reset();
mUnit = eStyleUnit_Percent;
mValue.mFloat = aValue;
}
void nsStyleCoord::SetFactorValue(float aValue) {
Reset();
mUnit = eStyleUnit_Factor;
mValue.mFloat = aValue;
}
void nsStyleCoord::SetFlexFractionValue(float aValue) {
Reset();
mUnit = eStyleUnit_FlexFraction;
mValue.mFloat = aValue;
}
void nsStyleCoord::SetCalcValue(Calc* aValue) {
Reset();
mUnit = eStyleUnit_Calc;
mValue.mPointer = aValue;
aValue->AddRef();
}
void nsStyleCoord::SetNormalValue() {
Reset();
mUnit = eStyleUnit_Normal;
mValue.mInt = 0;
}
void nsStyleCoord::SetAutoValue() {
Reset();
mUnit = eStyleUnit_Auto;
mValue.mInt = 0;
}
void nsStyleCoord::SetNoneValue() {
Reset();
mUnit = eStyleUnit_None;
mValue.mInt = 0;
}
// accessors that are not inlined
double nsStyleCoord::GetAngleValueInDegrees() const {
// Note that this extends the value from float to double.
return GetAngleValue();
}
double nsStyleCoord::GetAngleValueInRadians() const {
return GetAngleValueInDegrees() * M_PI / 180.0;
}
nscoord nsStyleCoord::ComputeComputedCalc(nscoord aPercentageBasis) const {
Calc* calc = GetCalcValue();
return calc->mLength +
NSToCoordFloorClamped(aPercentageBasis * calc->mPercent);
}
nscoord nsStyleCoord::ComputeCoordPercentCalc(nscoord aPercentageBasis) const {
switch (GetUnit()) {
case eStyleUnit_Coord:
return GetCoordValue();
case eStyleUnit_Percent:
return NSToCoordFloorClamped(aPercentageBasis * GetPercentValue());
case eStyleUnit_Calc:
return ComputeComputedCalc(aPercentageBasis);
default:
MOZ_ASSERT_UNREACHABLE("Unexpected unit!");
return 0;
}
}
nsStyleSides::nsStyleSides() {
NS_FOR_CSS_SIDES(i) { mUnits[i] = eStyleUnit_Null; }
mozilla::PodArrayZero(mValues);
}
nsStyleSides::nsStyleSides(const nsStyleSides& aOther) {
NS_FOR_CSS_SIDES(i) { mUnits[i] = eStyleUnit_Null; }
*this = aOther;
}
nsStyleSides::~nsStyleSides() { Reset(); }
nsStyleSides& nsStyleSides::operator=(const nsStyleSides& aCopy) {
if (this != &aCopy) {
NS_FOR_CSS_SIDES(i) {
nsStyleCoord::SetValue(mUnits[i], mValues[i], aCopy.mUnits[i],
aCopy.mValues[i]);
}
}
return *this;
}
bool nsStyleSides::operator==(const nsStyleSides& aOther) const {
NS_FOR_CSS_SIDES(i) {
if (nsStyleCoord(mValues[i], (nsStyleUnit)mUnits[i]) !=
nsStyleCoord(aOther.mValues[i], (nsStyleUnit)aOther.mUnits[i])) {
return false;
}
}
return true;
}
void nsStyleSides::Reset() {
NS_FOR_CSS_SIDES(i) { nsStyleCoord::Reset(mUnits[i], mValues[i]); }
}
nsStyleCorners::nsStyleCorners() {
NS_FOR_CSS_HALF_CORNERS(i) { mUnits[i] = eStyleUnit_Null; }
mozilla::PodArrayZero(mValues);
}
nsStyleCorners::nsStyleCorners(const nsStyleCorners& aOther) {
NS_FOR_CSS_HALF_CORNERS(i) { mUnits[i] = eStyleUnit_Null; }
*this = aOther;
}
nsStyleCorners::~nsStyleCorners() { Reset(); }
nsStyleCorners& nsStyleCorners::operator=(const nsStyleCorners& aCopy) {
if (this != &aCopy) {
NS_FOR_CSS_HALF_CORNERS(i) {
nsStyleCoord::SetValue(mUnits[i], mValues[i], aCopy.mUnits[i],
aCopy.mValues[i]);
}
}
return *this;
}
bool nsStyleCorners::operator==(const nsStyleCorners& aOther) const {
NS_FOR_CSS_HALF_CORNERS(i) {
if (nsStyleCoord(mValues[i], (nsStyleUnit)mUnits[i]) !=
nsStyleCoord(aOther.mValues[i], (nsStyleUnit)aOther.mUnits[i])) {
return false;
}
}
return true;
}
void nsStyleCorners::Reset() {
NS_FOR_CSS_HALF_CORNERS(i) { nsStyleCoord::Reset(mUnits[i], mValues[i]); }
}
// Validation of SideIsVertical.
#define CASE(side, result) \
static_assert(SideIsVertical(side) == result, "SideIsVertical is wrong")
CASE(eSideTop, false);
CASE(eSideRight, true);
CASE(eSideBottom, false);
CASE(eSideLeft, true);
#undef CASE
// Validation of HalfCornerIsX.
#define CASE(corner, result) \
static_assert(HalfCornerIsX(corner) == result, "HalfCornerIsX is wrong")
CASE(eCornerTopLeftX, true);
CASE(eCornerTopLeftY, false);
CASE(eCornerTopRightX, true);
CASE(eCornerTopRightY, false);
CASE(eCornerBottomRightX, true);
CASE(eCornerBottomRightY, false);
CASE(eCornerBottomLeftX, true);
CASE(eCornerBottomLeftY, false);
#undef CASE
// Validation of HalfToFullCorner.
#define CASE(corner, result) \
static_assert(HalfToFullCorner(corner) == result, \
"HalfToFullCorner is " \
"wrong")
CASE(eCornerTopLeftX, eCornerTopLeft);
CASE(eCornerTopLeftY, eCornerTopLeft);
CASE(eCornerTopRightX, eCornerTopRight);
CASE(eCornerTopRightY, eCornerTopRight);
CASE(eCornerBottomRightX, eCornerBottomRight);
CASE(eCornerBottomRightY, eCornerBottomRight);
CASE(eCornerBottomLeftX, eCornerBottomLeft);
CASE(eCornerBottomLeftY, eCornerBottomLeft);
#undef CASE
// Validation of FullToHalfCorner.
#define CASE(corner, vert, result) \
static_assert(FullToHalfCorner(corner, vert) == result, \
"FullToHalfCorner is wrong")
CASE(eCornerTopLeft, false, eCornerTopLeftX);
CASE(eCornerTopLeft, true, eCornerTopLeftY);
CASE(eCornerTopRight, false, eCornerTopRightX);
CASE(eCornerTopRight, true, eCornerTopRightY);
CASE(eCornerBottomRight, false, eCornerBottomRightX);
CASE(eCornerBottomRight, true, eCornerBottomRightY);
CASE(eCornerBottomLeft, false, eCornerBottomLeftX);
CASE(eCornerBottomLeft, true, eCornerBottomLeftY);
#undef CASE
// Validation of SideToFullCorner.
#define CASE(side, second, result) \
static_assert(SideToFullCorner(side, second) == result, \
"SideToFullCorner is wrong")
CASE(eSideTop, false, eCornerTopLeft);
CASE(eSideTop, true, eCornerTopRight);
CASE(eSideRight, false, eCornerTopRight);
CASE(eSideRight, true, eCornerBottomRight);
CASE(eSideBottom, false, eCornerBottomRight);
CASE(eSideBottom, true, eCornerBottomLeft);
CASE(eSideLeft, false, eCornerBottomLeft);
CASE(eSideLeft, true, eCornerTopLeft);
#undef CASE
// Validation of SideToHalfCorner.
#define CASE(side, second, parallel, result) \
static_assert(SideToHalfCorner(side, second, parallel) == result, \
"SideToHalfCorner is wrong")
CASE(eSideTop, false, true, eCornerTopLeftX);
CASE(eSideTop, false, false, eCornerTopLeftY);
CASE(eSideTop, true, true, eCornerTopRightX);
CASE(eSideTop, true, false, eCornerTopRightY);
CASE(eSideRight, false, false, eCornerTopRightX);
CASE(eSideRight, false, true, eCornerTopRightY);
CASE(eSideRight, true, false, eCornerBottomRightX);
CASE(eSideRight, true, true, eCornerBottomRightY);
CASE(eSideBottom, false, true, eCornerBottomRightX);
CASE(eSideBottom, false, false, eCornerBottomRightY);
CASE(eSideBottom, true, true, eCornerBottomLeftX);
CASE(eSideBottom, true, false, eCornerBottomLeftY);
CASE(eSideLeft, false, false, eCornerBottomLeftX);
CASE(eSideLeft, false, true, eCornerBottomLeftY);
CASE(eSideLeft, true, false, eCornerTopLeftX);
CASE(eSideLeft, true, true, eCornerTopLeftY);
#undef CASE