Bug 1874684 - Part 37: Fix unified compilation. r=allstarschh
[gecko.git] / layout / style / nsCSSValue.cpp
blob5d815c25f23bd50bd3894f83dd639d881ee72222
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/. */
7 /* representation of simple property values within CSS declarations */
9 #include "nsCSSValue.h"
11 #include "mozilla/CORSMode.h"
12 #include "mozilla/FontPropertyTypes.h"
13 #include "mozilla/ServoBindings.h"
14 #include "mozilla/ServoStyleSet.h"
15 #include "mozilla/ServoTypes.h"
16 #include "mozilla/StyleSheetInlines.h"
17 #include "mozilla/Likely.h"
18 #include "mozilla/MemoryReporting.h"
19 #include "mozilla/css/ImageLoader.h"
20 #include "gfxFontConstants.h"
21 #include "imgRequestProxy.h"
22 #include "mozilla/dom/Document.h"
23 #include "nsCSSProps.h"
24 #include "nsNetUtil.h"
25 #include "nsPresContext.h"
26 #include "nsStyleUtil.h"
27 #include "nsDeviceContext.h"
28 #include "nsContentUtils.h"
30 using namespace mozilla;
31 using namespace mozilla::css;
32 using namespace mozilla::dom;
34 nsCSSValue::nsCSSValue(float aValue, nsCSSUnit aUnit) : mUnit(aUnit) {
35 MOZ_ASSERT(eCSSUnit_Null == aUnit, "not a float value");
36 mValue = aValue;
37 MOZ_ASSERT(!std::isnan(mValue));
40 nsCSSValue::nsCSSValue(const nsCSSValue& aCopy) : mUnit(aCopy.mUnit) {
41 if (eCSSUnit_Null != mUnit) {
42 mValue = aCopy.mValue;
43 MOZ_ASSERT(!std::isnan(mValue));
44 } else {
45 MOZ_ASSERT_UNREACHABLE("unknown unit");
49 nsCSSValue& nsCSSValue::operator=(const nsCSSValue& aCopy) {
50 if (this != &aCopy) {
51 this->~nsCSSValue();
52 new (this) nsCSSValue(aCopy);
54 return *this;
57 nsCSSValue& nsCSSValue::operator=(nsCSSValue&& aOther) {
58 MOZ_ASSERT(this != &aOther, "Self assigment with rvalue reference");
60 Reset();
61 mUnit = aOther.mUnit;
62 mValue = aOther.mValue;
63 aOther.mUnit = eCSSUnit_Null;
65 return *this;
68 bool nsCSSValue::operator==(const nsCSSValue& aOther) const {
69 if (mUnit != aOther.mUnit) {
70 return false;
72 return mValue == aOther.mValue;
75 nscoord nsCSSValue::GetPixelLength() const {
76 MOZ_ASSERT(IsPixelLengthUnit(), "not a fixed length unit");
78 double scaleFactor;
79 switch (mUnit) {
80 case eCSSUnit_Pixel:
81 return nsPresContext::CSSPixelsToAppUnits(mValue);
82 case eCSSUnit_Pica:
83 scaleFactor = 16.0;
84 break;
85 case eCSSUnit_Point:
86 scaleFactor = 4 / 3.0;
87 break;
88 case eCSSUnit_Inch:
89 scaleFactor = 96.0;
90 break;
91 case eCSSUnit_Millimeter:
92 scaleFactor = 96 / 25.4;
93 break;
94 case eCSSUnit_Centimeter:
95 scaleFactor = 96 / 2.54;
96 break;
97 case eCSSUnit_Quarter:
98 scaleFactor = 96 / 101.6;
99 break;
100 default:
101 NS_ERROR("should never get here");
102 return 0;
104 return nsPresContext::CSSPixelsToAppUnits(float(mValue * scaleFactor));
107 void nsCSSValue::SetPercentValue(float aValue) {
108 Reset();
109 mUnit = eCSSUnit_Percent;
110 mValue = aValue;
111 MOZ_ASSERT(!std::isnan(mValue));
114 void nsCSSValue::SetFloatValue(float aValue, nsCSSUnit aUnit) {
115 MOZ_ASSERT(IsFloatUnit(aUnit), "not a float value");
116 Reset();
117 if (IsFloatUnit(aUnit)) {
118 mUnit = aUnit;
119 mValue = aValue;
120 MOZ_ASSERT(!std::isnan(mValue));