nsCSSValue.cpp (3165B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 /* representation of simple property values within CSS declarations */ 8 9 #include "nsCSSValue.h" 10 11 #include "gfxFontConstants.h" 12 #include "imgRequestProxy.h" 13 #include "mozilla/CORSMode.h" 14 #include "mozilla/FontPropertyTypes.h" 15 #include "mozilla/ServoBindings.h" 16 #include "mozilla/ServoStyleSet.h" 17 #include "mozilla/ServoTypes.h" 18 #include "mozilla/StyleSheetInlines.h" 19 #include "mozilla/css/ImageLoader.h" 20 #include "mozilla/dom/Document.h" 21 #include "nsCSSProps.h" 22 #include "nsContentUtils.h" 23 #include "nsDeviceContext.h" 24 #include "nsNetUtil.h" 25 #include "nsPresContext.h" 26 #include "nsStyleUtil.h" 27 28 using namespace mozilla; 29 using namespace mozilla::css; 30 using namespace mozilla::dom; 31 32 nsCSSValue::nsCSSValue(float aValue, nsCSSUnit aUnit) : mUnit(aUnit) { 33 MOZ_ASSERT(eCSSUnit_Null == aUnit, "not a float value"); 34 mValue = aValue; 35 MOZ_ASSERT(!std::isnan(mValue)); 36 } 37 38 nsCSSValue::nsCSSValue(const nsCSSValue& aCopy) : mUnit(aCopy.mUnit) { 39 if (eCSSUnit_Null != mUnit) { 40 mValue = aCopy.mValue; 41 MOZ_ASSERT(!std::isnan(mValue)); 42 } else { 43 MOZ_ASSERT_UNREACHABLE("unknown unit"); 44 } 45 } 46 47 nsCSSValue& nsCSSValue::operator=(const nsCSSValue& aCopy) { 48 if (this != &aCopy) { 49 this->~nsCSSValue(); 50 new (this) nsCSSValue(aCopy); 51 } 52 return *this; 53 } 54 55 nsCSSValue& nsCSSValue::operator=(nsCSSValue&& aOther) { 56 MOZ_ASSERT(this != &aOther, "Self assigment with rvalue reference"); 57 58 Reset(); 59 mUnit = aOther.mUnit; 60 mValue = aOther.mValue; 61 aOther.mUnit = eCSSUnit_Null; 62 63 return *this; 64 } 65 66 bool nsCSSValue::operator==(const nsCSSValue& aOther) const { 67 if (mUnit != aOther.mUnit) { 68 return false; 69 } 70 return mValue == aOther.mValue; 71 } 72 73 nscoord nsCSSValue::GetPixelLength() const { 74 MOZ_ASSERT(IsPixelLengthUnit(), "not a fixed length unit"); 75 76 double scaleFactor; 77 switch (mUnit) { 78 case eCSSUnit_Pixel: 79 return nsPresContext::CSSPixelsToAppUnits(mValue); 80 case eCSSUnit_Pica: 81 scaleFactor = 16.0; 82 break; 83 case eCSSUnit_Point: 84 scaleFactor = 4 / 3.0; 85 break; 86 case eCSSUnit_Inch: 87 scaleFactor = 96.0; 88 break; 89 case eCSSUnit_Millimeter: 90 scaleFactor = 96 / 25.4; 91 break; 92 case eCSSUnit_Centimeter: 93 scaleFactor = 96 / 2.54; 94 break; 95 case eCSSUnit_Quarter: 96 scaleFactor = 96 / 101.6; 97 break; 98 default: 99 NS_ERROR("should never get here"); 100 return 0; 101 } 102 return nsPresContext::CSSPixelsToAppUnits(float(mValue * scaleFactor)); 103 } 104 105 void nsCSSValue::SetPercentValue(float aValue) { 106 Reset(); 107 mUnit = eCSSUnit_Percent; 108 mValue = aValue; 109 MOZ_ASSERT(!std::isnan(mValue)); 110 } 111 112 void nsCSSValue::SetFloatValue(float aValue, nsCSSUnit aUnit) { 113 MOZ_ASSERT(IsFloatUnit(aUnit), "not a float value"); 114 Reset(); 115 if (IsFloatUnit(aUnit)) { 116 mUnit = aUnit; 117 mValue = aValue; 118 MOZ_ASSERT(!std::isnan(mValue)); 119 } 120 }