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