2017-10-27 20:33:53 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* 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/. */
|
2006-03-25 08:47:31 +03:00
|
|
|
|
|
|
|
/* representation of simple property values within CSS declarations */
|
|
|
|
|
1998-10-08 05:29:29 +04:00
|
|
|
#include "nsCSSValue.h"
|
2010-08-19 23:33:43 +04:00
|
|
|
|
2018-01-26 01:14:13 +03:00
|
|
|
#include "mozilla/CORSMode.h"
|
2018-04-13 22:34:37 +03:00
|
|
|
#include "mozilla/FontPropertyTypes.h"
|
2017-09-15 23:11:37 +03:00
|
|
|
#include "mozilla/ServoBindings.h"
|
2017-02-02 23:10:10 +03:00
|
|
|
#include "mozilla/ServoStyleSet.h"
|
2017-09-15 23:11:37 +03:00
|
|
|
#include "mozilla/ServoTypes.h"
|
2016-09-26 15:03:25 +03:00
|
|
|
#include "mozilla/StyleSheetInlines.h"
|
2013-12-12 06:09:44 +04:00
|
|
|
#include "mozilla/Likely.h"
|
|
|
|
#include "mozilla/MemoryReporting.h"
|
|
|
|
#include "mozilla/css/ImageLoader.h"
|
|
|
|
#include "gfxFontConstants.h"
|
|
|
|
#include "imgRequestProxy.h"
|
2019-01-02 16:05:23 +03:00
|
|
|
#include "mozilla/dom/Document.h"
|
2010-08-19 23:33:43 +04:00
|
|
|
#include "nsCSSProps.h"
|
2011-05-11 19:28:53 +04:00
|
|
|
#include "nsNetUtil.h"
|
2013-09-16 05:06:52 +04:00
|
|
|
#include "nsPresContext.h"
|
2013-12-12 06:09:44 +04:00
|
|
|
#include "nsStyleUtil.h"
|
2013-09-23 15:55:35 +04:00
|
|
|
#include "nsDeviceContext.h"
|
2016-11-02 11:58:31 +03:00
|
|
|
#include "nsContentUtils.h"
|
2004-03-09 06:57:51 +03:00
|
|
|
|
2013-11-15 06:42:57 +04:00
|
|
|
using namespace mozilla;
|
2016-12-01 21:37:44 +03:00
|
|
|
using namespace mozilla::css;
|
2019-01-02 16:05:23 +03:00
|
|
|
using namespace mozilla::dom;
|
1998-10-08 05:29:29 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
nsCSSValue::nsCSSValue(int32_t aValue, nsCSSUnit aUnit) : mUnit(aUnit) {
|
2018-04-05 11:27:33 +03:00
|
|
|
MOZ_ASSERT(aUnit == eCSSUnit_Integer || aUnit == eCSSUnit_Enumerated,
|
2015-02-10 01:34:50 +03:00
|
|
|
"not an int value");
|
2018-04-05 11:27:33 +03:00
|
|
|
if (aUnit == eCSSUnit_Integer || aUnit == eCSSUnit_Enumerated) {
|
1998-10-08 05:29:29 +04:00
|
|
|
mValue.mInt = aValue;
|
|
|
|
} else {
|
|
|
|
mUnit = eCSSUnit_Null;
|
|
|
|
mValue.mInt = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCSSValue::nsCSSValue(float aValue, nsCSSUnit aUnit) : mUnit(aUnit) {
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(eCSSUnit_Percent <= aUnit, "not a float value");
|
1998-10-08 05:29:29 +04:00
|
|
|
if (eCSSUnit_Percent <= aUnit) {
|
|
|
|
mValue.mFloat = aValue;
|
2013-05-02 00:55:13 +04:00
|
|
|
MOZ_ASSERT(!mozilla::IsNaN(mValue.mFloat));
|
1998-10-08 05:29:29 +04:00
|
|
|
} else {
|
|
|
|
mUnit = eCSSUnit_Null;
|
|
|
|
mValue.mInt = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCSSValue::nsCSSValue(const nsCSSValue& aCopy) : mUnit(aCopy.mUnit) {
|
2019-05-19 03:47:18 +03:00
|
|
|
if (eCSSUnit_Percent <= mUnit) {
|
2008-03-13 01:05:27 +03:00
|
|
|
mValue.mFloat = aCopy.mValue.mFloat;
|
2013-05-02 00:55:13 +04:00
|
|
|
MOZ_ASSERT(!mozilla::IsNaN(mValue.mFloat));
|
2018-04-05 11:27:33 +03:00
|
|
|
} else if (eCSSUnit_Integer <= mUnit && mUnit <= eCSSUnit_Enumerated) {
|
1998-10-08 05:29:29 +04:00
|
|
|
mValue.mInt = aCopy.mValue.mInt;
|
|
|
|
} else {
|
2019-05-19 03:47:18 +03:00
|
|
|
MOZ_ASSERT_UNREACHABLE("unknown unit");
|
1998-10-08 05:29:29 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCSSValue& nsCSSValue::operator=(const nsCSSValue& aCopy) {
|
2005-04-03 20:47:01 +04:00
|
|
|
if (this != &aCopy) {
|
2019-05-19 03:47:18 +03:00
|
|
|
this->~nsCSSValue();
|
2005-04-03 20:47:01 +04:00
|
|
|
new (this) nsCSSValue(aCopy);
|
1998-10-08 05:29:29 +04:00
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2016-05-20 03:09:28 +03:00
|
|
|
nsCSSValue& nsCSSValue::operator=(nsCSSValue&& aOther) {
|
|
|
|
MOZ_ASSERT(this != &aOther, "Self assigment with rvalue reference");
|
|
|
|
|
|
|
|
Reset();
|
|
|
|
mUnit = aOther.mUnit;
|
|
|
|
mValue = aOther.mValue;
|
|
|
|
aOther.mUnit = eCSSUnit_Null;
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool nsCSSValue::operator==(const nsCSSValue& aOther) const {
|
2019-05-19 03:47:18 +03:00
|
|
|
if (mUnit != aOther.mUnit) {
|
|
|
|
return false;
|
1998-10-08 05:29:29 +04:00
|
|
|
}
|
2019-05-19 03:47:18 +03:00
|
|
|
if ((eCSSUnit_Integer <= mUnit) && (mUnit <= eCSSUnit_Enumerated)) {
|
|
|
|
return mValue.mInt == aOther.mValue.mInt;
|
|
|
|
}
|
|
|
|
return mValue.mFloat == aOther.mValue.mFloat;
|
1998-10-08 05:29:29 +04:00
|
|
|
}
|
|
|
|
|
2018-10-13 03:41:03 +03:00
|
|
|
double nsCSSValue::GetAngleValueInDegrees() const {
|
|
|
|
// Note that this extends the value from float to double.
|
|
|
|
return GetAngleValue();
|
2009-11-02 22:36:43 +03:00
|
|
|
}
|
|
|
|
|
2018-10-13 03:41:03 +03:00
|
|
|
double nsCSSValue::GetAngleValueInRadians() const {
|
|
|
|
return GetAngleValueInDegrees() * M_PI / 180.0;
|
2016-10-15 22:15:36 +03:00
|
|
|
}
|
|
|
|
|
2010-08-13 13:58:01 +04:00
|
|
|
nscoord nsCSSValue::GetPixelLength() const {
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(IsPixelLengthUnit(), "not a fixed length unit");
|
2010-08-13 13:58:01 +04:00
|
|
|
|
2010-08-13 13:58:02 +04:00
|
|
|
double scaleFactor;
|
2010-08-13 13:58:01 +04:00
|
|
|
switch (mUnit) {
|
2010-08-13 13:58:02 +04:00
|
|
|
case eCSSUnit_Pixel:
|
|
|
|
return nsPresContext::CSSPixelsToAppUnits(mValue.mFloat);
|
|
|
|
case eCSSUnit_Pica:
|
|
|
|
scaleFactor = 16.0;
|
|
|
|
break;
|
|
|
|
case eCSSUnit_Point:
|
|
|
|
scaleFactor = 4 / 3.0;
|
|
|
|
break;
|
|
|
|
case eCSSUnit_Inch:
|
|
|
|
scaleFactor = 96.0;
|
|
|
|
break;
|
|
|
|
case eCSSUnit_Millimeter:
|
|
|
|
scaleFactor = 96 / 25.4;
|
|
|
|
break;
|
|
|
|
case eCSSUnit_Centimeter:
|
|
|
|
scaleFactor = 96 / 2.54;
|
2016-05-23 10:18:34 +03:00
|
|
|
break;
|
|
|
|
case eCSSUnit_Quarter:
|
|
|
|
scaleFactor = 96 / 101.6;
|
2018-11-30 13:46:48 +03:00
|
|
|
break;
|
2010-08-13 13:58:01 +04:00
|
|
|
default:
|
|
|
|
NS_ERROR("should never get here");
|
|
|
|
return 0;
|
2003-12-29 22:07:53 +03:00
|
|
|
}
|
2010-08-13 13:58:02 +04:00
|
|
|
return nsPresContext::CSSPixelsToAppUnits(float(mValue.mFloat * scaleFactor));
|
2003-12-29 22:07:53 +03:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
void nsCSSValue::SetIntValue(int32_t aValue, nsCSSUnit aUnit) {
|
2018-04-05 11:27:33 +03:00
|
|
|
MOZ_ASSERT(aUnit == eCSSUnit_Integer || aUnit == eCSSUnit_Enumerated,
|
2015-02-10 01:34:50 +03:00
|
|
|
"not an int value");
|
1998-10-08 05:29:29 +04:00
|
|
|
Reset();
|
2018-04-05 11:27:33 +03:00
|
|
|
if (aUnit == eCSSUnit_Integer || aUnit == eCSSUnit_Enumerated) {
|
1998-10-08 05:29:29 +04:00
|
|
|
mUnit = aUnit;
|
|
|
|
mValue.mInt = aValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-12-29 22:07:53 +03:00
|
|
|
void nsCSSValue::SetPercentValue(float aValue) {
|
|
|
|
Reset();
|
|
|
|
mUnit = eCSSUnit_Percent;
|
|
|
|
mValue.mFloat = aValue;
|
2013-05-02 00:55:13 +04:00
|
|
|
MOZ_ASSERT(!mozilla::IsNaN(mValue.mFloat));
|
2003-12-29 22:07:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void nsCSSValue::SetFloatValue(float aValue, nsCSSUnit aUnit) {
|
2016-07-20 10:14:05 +03:00
|
|
|
MOZ_ASSERT(IsFloatUnit(aUnit), "not a float value");
|
2003-12-29 22:07:53 +03:00
|
|
|
Reset();
|
2016-07-20 10:14:05 +03:00
|
|
|
if (IsFloatUnit(aUnit)) {
|
2003-12-29 22:07:53 +03:00
|
|
|
mUnit = aUnit;
|
|
|
|
mValue.mFloat = aValue;
|
2013-05-02 00:55:13 +04:00
|
|
|
MOZ_ASSERT(!mozilla::IsNaN(mValue.mFloat));
|
2003-12-29 22:07:53 +03:00
|
|
|
}
|
|
|
|
}
|