Nest CFX_CSSNumberType in CFX_CSSNumberValue
-- Rename to Unit since that is what it represents.
-- Add k to constants.
-- Apply style guide naming to getters to avoid name conflicts.
Change-Id: Icfa5c09e1fb20e57a202c7c3eb67b788649ed177
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/80690
Commit-Queue: Tom Sepez <tsepez@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
diff --git a/core/fxcrt/css/cfx_cssdata.cpp b/core/fxcrt/css/cfx_cssdata.cpp
index 0e9afb1..cd80427 100644
--- a/core/fxcrt/css/cfx_cssdata.cpp
+++ b/core/fxcrt/css/cfx_cssdata.cpp
@@ -258,10 +258,14 @@
#undef PVAL
const CFX_CSSData::LengthUnit lengthUnitTable[] = {
- {L"cm", CFX_CSSNumberType::CentiMeters}, {L"em", CFX_CSSNumberType::EMS},
- {L"ex", CFX_CSSNumberType::EXS}, {L"in", CFX_CSSNumberType::Inches},
- {L"mm", CFX_CSSNumberType::MilliMeters}, {L"pc", CFX_CSSNumberType::Picas},
- {L"pt", CFX_CSSNumberType::Points}, {L"px", CFX_CSSNumberType::Pixels},
+ {L"cm", CFX_CSSNumberValue::Unit::kCentiMeters},
+ {L"em", CFX_CSSNumberValue::Unit::kEMS},
+ {L"ex", CFX_CSSNumberValue::Unit::kEXS},
+ {L"in", CFX_CSSNumberValue::Unit::kInches},
+ {L"mm", CFX_CSSNumberValue::Unit::kMilliMeters},
+ {L"pc", CFX_CSSNumberValue::Unit::kPicas},
+ {L"pt", CFX_CSSNumberValue::Unit::kPoints},
+ {L"px", CFX_CSSNumberValue::Unit::kPixels},
};
// 16 colours from CSS 2.0 + alternate spelling of grey/gray.
diff --git a/core/fxcrt/css/cfx_cssdata.h b/core/fxcrt/css/cfx_cssdata.h
index 86414a7..b0d81d4 100644
--- a/core/fxcrt/css/cfx_cssdata.h
+++ b/core/fxcrt/css/cfx_cssdata.h
@@ -28,7 +28,7 @@
struct LengthUnit {
const wchar_t* value;
- CFX_CSSNumberType type;
+ CFX_CSSNumberValue::Unit type;
};
struct Color {
diff --git a/core/fxcrt/css/cfx_cssdeclaration.cpp b/core/fxcrt/css/cfx_cssdeclaration.cpp
index fcdad47..35bf63e 100644
--- a/core/fxcrt/css/cfx_cssdeclaration.cpp
+++ b/core/fxcrt/css/cfx_cssdeclaration.cpp
@@ -31,7 +31,7 @@
bool ParseCSSNumber(const wchar_t* pszValue,
int32_t iValueLen,
float* pValue,
- CFX_CSSNumberType* pOutUnit) {
+ CFX_CSSNumberValue::Unit* pOutUnit) {
DCHECK(pszValue);
DCHECK(iValueLen > 0);
@@ -42,9 +42,9 @@
iValueLen -= iUsedLen;
pszValue += iUsedLen;
- *pOutUnit = CFX_CSSNumberType::Number;
+ *pOutUnit = CFX_CSSNumberValue::Unit::kNumber;
if (iValueLen >= 1 && *pszValue == '%') {
- *pOutUnit = CFX_CSSNumberType::Percent;
+ *pOutUnit = CFX_CSSNumberValue::Unit::kPercent;
} else if (iValueLen == 2) {
const CFX_CSSData::LengthUnit* pUnit =
CFX_CSSData::GetLengthUnitByName(WideStringView(pszValue, 2));
@@ -118,11 +118,11 @@
return false;
if (eType != CFX_CSSPrimitiveType::Number)
return false;
- CFX_CSSNumberType eNumType;
+ CFX_CSSNumberValue::Unit eNumType;
if (!ParseCSSNumber(pszValue, iValueLen, &fValue, &eNumType))
return false;
- rgb[i] = eNumType == CFX_CSSNumberType::Percent
+ rgb[i] = eNumType == CFX_CSSNumberValue::Unit::kPercent
? FXSYS_roundf(fValue * 2.55f)
: FXSYS_roundf(fValue);
}
@@ -289,7 +289,7 @@
RetainPtr<CFX_CSSValue> CFX_CSSDeclaration::ParseNumber(const wchar_t* pszValue,
int32_t iValueLen) {
float fValue;
- CFX_CSSNumberType eUnit;
+ CFX_CSSNumberValue::Unit eUnit;
if (!ParseCSSNumber(pszValue, iValueLen, &fValue, &eUnit))
return nullptr;
return pdfium::MakeRetain<CFX_CSSNumberValue>(eUnit, fValue);
@@ -340,7 +340,7 @@
case CFX_CSSPrimitiveType::Number:
if (dwType & CFX_CSSVALUETYPE_MaybeNumber) {
float fValue;
- CFX_CSSNumberType eNumType;
+ CFX_CSSNumberValue::Unit eNumType;
if (ParseCSSNumber(pszValue, iValueLen, &fValue, &eNumType))
list.push_back(
pdfium::MakeRetain<CFX_CSSNumberValue>(eNumType, fValue));
@@ -462,7 +462,7 @@
continue;
float fValue;
- CFX_CSSNumberType eNumType;
+ CFX_CSSNumberValue::Unit eNumType;
if (ParseCSSNumber(pszValue, iValueLen, &fValue, &eNumType))
pWidth = pdfium::MakeRetain<CFX_CSSNumberValue>(eNumType, fValue);
break;
@@ -495,10 +495,10 @@
break;
}
}
- if (!pWidth)
- pWidth =
- pdfium::MakeRetain<CFX_CSSNumberValue>(CFX_CSSNumberType::Number, 0.0f);
-
+ if (!pWidth) {
+ pWidth = pdfium::MakeRetain<CFX_CSSNumberValue>(
+ CFX_CSSNumberValue::Unit::kNumber, 0.0f);
+ }
return true;
}
@@ -574,7 +574,7 @@
}
case CFX_CSSPrimitiveType::Number: {
float fValue;
- CFX_CSSNumberType eNumType;
+ CFX_CSSNumberValue::Unit eNumType;
if (!ParseCSSNumber(pszValue, iValueLen, &fValue, &eNumType))
break;
if (eType == CFX_CSSPrimitiveType::Number) {
@@ -588,9 +588,10 @@
case 700:
case 800:
case 900:
- if (!pWeight)
+ if (!pWeight) {
pWeight = pdfium::MakeRetain<CFX_CSSNumberValue>(
- CFX_CSSNumberType::Number, fValue);
+ CFX_CSSNumberValue::Unit::kNumber, fValue);
+ }
continue;
}
}
diff --git a/core/fxcrt/css/cfx_cssnumbervalue.cpp b/core/fxcrt/css/cfx_cssnumbervalue.cpp
index f0c73cf..abb9901 100644
--- a/core/fxcrt/css/cfx_cssnumbervalue.cpp
+++ b/core/fxcrt/css/cfx_cssnumbervalue.cpp
@@ -6,33 +6,33 @@
#include "core/fxcrt/css/cfx_cssnumbervalue.h"
-CFX_CSSNumberValue::CFX_CSSNumberValue(CFX_CSSNumberType type, float value)
- : CFX_CSSValue(CFX_CSSPrimitiveType::Number), type_(type), value_(value) {
- if (type_ == CFX_CSSNumberType::Number && fabs(value_) < 0.001f)
+CFX_CSSNumberValue::CFX_CSSNumberValue(Unit unit, float value)
+ : CFX_CSSValue(CFX_CSSPrimitiveType::Number), unit_(unit), value_(value) {
+ if (unit_ == Unit::kNumber && fabs(value_) < 0.001f)
value_ = 0.0f;
}
CFX_CSSNumberValue::~CFX_CSSNumberValue() = default;
float CFX_CSSNumberValue::Apply(float percentBase) const {
- switch (type_) {
- case CFX_CSSNumberType::Pixels:
- case CFX_CSSNumberType::Number:
+ switch (unit_) {
+ case Unit::kPixels:
+ case Unit::kNumber:
return value_ * 72 / 96;
- case CFX_CSSNumberType::EMS:
- case CFX_CSSNumberType::EXS:
+ case Unit::kEMS:
+ case Unit::kEXS:
return value_ * percentBase;
- case CFX_CSSNumberType::Percent:
+ case Unit::kPercent:
return value_ * percentBase / 100.0f;
- case CFX_CSSNumberType::CentiMeters:
+ case Unit::kCentiMeters:
return value_ * 28.3464f;
- case CFX_CSSNumberType::MilliMeters:
+ case Unit::kMilliMeters:
return value_ * 2.8346f;
- case CFX_CSSNumberType::Inches:
+ case Unit::kInches:
return value_ * 72.0f;
- case CFX_CSSNumberType::Picas:
+ case Unit::kPicas:
return value_ / 12.0f;
- case CFX_CSSNumberType::Points:
+ case Unit::kPoints:
return value_;
}
return value_;
diff --git a/core/fxcrt/css/cfx_cssnumbervalue.h b/core/fxcrt/css/cfx_cssnumbervalue.h
index a977750..b0999b3 100644
--- a/core/fxcrt/css/cfx_cssnumbervalue.h
+++ b/core/fxcrt/css/cfx_cssnumbervalue.h
@@ -10,31 +10,30 @@
#include "core/fxcrt/css/cfx_cssvalue.h"
#include "core/fxcrt/fx_system.h"
-enum class CFX_CSSNumberType {
- Number,
- Percent,
- EMS,
- EXS,
- Pixels,
- CentiMeters,
- MilliMeters,
- Inches,
- Points,
- Picas,
-};
-
class CFX_CSSNumberValue final : public CFX_CSSValue {
public:
- CFX_CSSNumberValue(CFX_CSSNumberType type, float value);
+ enum class Unit {
+ kNumber,
+ kPercent,
+ kEMS,
+ kEXS,
+ kPixels,
+ kCentiMeters,
+ kMilliMeters,
+ kInches,
+ kPoints,
+ kPicas,
+ };
+
+ CFX_CSSNumberValue(Unit unit, float value);
~CFX_CSSNumberValue() override;
- float Value() const { return value_; }
- CFX_CSSNumberType Kind() const { return type_; }
-
+ Unit unit() const { return unit_; }
+ float value() const { return value_; }
float Apply(float percentBase) const;
private:
- CFX_CSSNumberType type_;
+ Unit unit_;
float value_;
};
diff --git a/core/fxcrt/css/cfx_cssstyleselector.cpp b/core/fxcrt/css/cfx_cssstyleselector.cpp
index c31a75e..772407c 100644
--- a/core/fxcrt/css/cfx_cssstyleselector.cpp
+++ b/core/fxcrt/css/cfx_cssstyleselector.cpp
@@ -194,9 +194,9 @@
case CFX_CSSProperty::LineHeight:
if (eType == CFX_CSSPrimitiveType::Number) {
RetainPtr<CFX_CSSNumberValue> v = pValue.As<CFX_CSSNumberValue>();
- if (v->Kind() == CFX_CSSNumberType::Number) {
+ if (v->unit() == CFX_CSSNumberValue::Unit::kNumber) {
pComputedStyle->m_InheritedData.m_fLineHeight =
- v->Value() * pComputedStyle->m_InheritedData.m_fFontSize;
+ v->value() * pComputedStyle->m_InheritedData.m_fFontSize;
} else {
pComputedStyle->m_InheritedData.m_fLineHeight =
v->Apply(pComputedStyle->m_InheritedData.m_fFontSize);
@@ -220,7 +220,7 @@
ToFontWeight(pValue.As<CFX_CSSEnumValue>()->Value());
} else if (eType == CFX_CSSPrimitiveType::Number) {
int32_t iValue =
- static_cast<int32_t>(pValue.As<CFX_CSSNumberValue>()->Value()) /
+ static_cast<int32_t>(pValue.As<CFX_CSSNumberValue>()->value()) /
100;
if (iValue >= 1 && iValue <= 9) {
pComputedStyle->m_InheritedData.m_wFontWeight = iValue * 100;
@@ -346,8 +346,8 @@
pComputedStyle->m_InheritedData.m_LetterSpacing.Set(
CFX_CSSLengthUnit::Normal);
} else if (eType == CFX_CSSPrimitiveType::Number) {
- if (pValue.As<CFX_CSSNumberValue>()->Kind() ==
- CFX_CSSNumberType::Percent) {
+ if (pValue.As<CFX_CSSNumberValue>()->unit() ==
+ CFX_CSSNumberValue::Unit::kPercent) {
break;
}
@@ -361,8 +361,8 @@
pComputedStyle->m_InheritedData.m_WordSpacing.Set(
CFX_CSSLengthUnit::Normal);
} else if (eType == CFX_CSSPrimitiveType::Number) {
- if (pValue.As<CFX_CSSNumberValue>()->Kind() ==
- CFX_CSSNumberType::Percent) {
+ if (pValue.As<CFX_CSSNumberValue>()->unit() ==
+ CFX_CSSNumberValue::Unit::kPercent) {
break;
}
SetLengthWithPercent(pComputedStyle->m_InheritedData.m_WordSpacing,
@@ -478,9 +478,9 @@
float fFontSize) {
if (eType == CFX_CSSPrimitiveType::Number) {
RetainPtr<CFX_CSSNumberValue> v = pValue.As<CFX_CSSNumberValue>();
- if (v->Kind() == CFX_CSSNumberType::Percent) {
+ if (v->unit() == CFX_CSSNumberValue::Unit::kPercent) {
width.Set(CFX_CSSLengthUnit::Percent,
- pValue.As<CFX_CSSNumberValue>()->Value() / 100.0f);
+ pValue.As<CFX_CSSNumberValue>()->value() / 100.0f);
return width.NonZero();
}
diff --git a/core/fxcrt/css/cfx_cssstylesheet_unittest.cpp b/core/fxcrt/css/cfx_cssstylesheet_unittest.cpp
index 8895de2..f470295 100644
--- a/core/fxcrt/css/cfx_cssstylesheet_unittest.cpp
+++ b/core/fxcrt/css/cfx_cssstylesheet_unittest.cpp
@@ -55,14 +55,16 @@
EXPECT_EQ(decl_->PropertyCountForTesting(), decl_count);
}
- void VerifyFloat(CFX_CSSProperty prop, float val, CFX_CSSNumberType type) {
+ void VerifyFloat(CFX_CSSProperty prop,
+ float val,
+ CFX_CSSNumberValue::Unit unit) {
DCHECK(decl_);
bool important;
RetainPtr<CFX_CSSValue> v = decl_->GetProperty(prop, &important);
EXPECT_EQ(v->GetType(), CFX_CSSPrimitiveType::Number);
- EXPECT_EQ(v.As<CFX_CSSNumberValue>()->Kind(), type);
- EXPECT_EQ(v.As<CFX_CSSNumberValue>()->Value(), val);
+ EXPECT_EQ(v.As<CFX_CSSNumberValue>()->unit(), unit);
+ EXPECT_EQ(v.As<CFX_CSSNumberValue>()->value(), val);
}
void VerifyEnum(CFX_CSSProperty prop, CFX_CSSPropertyValue val) {
@@ -151,13 +153,13 @@
EXPECT_EQ(4u, decl_->PropertyCountForTesting());
VerifyFloat(CFX_CSSProperty::BorderLeftWidth, 10.0f,
- CFX_CSSNumberType::Pixels);
+ CFX_CSSNumberValue::Unit::kPixels);
VerifyFloat(CFX_CSSProperty::BorderRightWidth, 10.0f,
- CFX_CSSNumberType::Pixels);
+ CFX_CSSNumberValue::Unit::kPixels);
VerifyFloat(CFX_CSSProperty::BorderTopWidth, 10.0f,
- CFX_CSSNumberType::Pixels);
+ CFX_CSSNumberValue::Unit::kPixels);
VerifyFloat(CFX_CSSProperty::BorderBottomWidth, 10.0f,
- CFX_CSSNumberType::Pixels);
+ CFX_CSSNumberValue::Unit::kPixels);
style = sheet_->GetRule(1);
ASSERT_TRUE(style);
@@ -179,10 +181,14 @@
decl_ = style->GetDeclaration();
ASSERT_TRUE(decl_);
EXPECT_EQ(4u, decl_->PropertyCountForTesting());
- VerifyFloat(CFX_CSSProperty::PaddingLeft, 0.0f, CFX_CSSNumberType::Number);
- VerifyFloat(CFX_CSSProperty::PaddingRight, 0.0f, CFX_CSSNumberType::Number);
- VerifyFloat(CFX_CSSProperty::PaddingTop, 0.0f, CFX_CSSNumberType::Number);
- VerifyFloat(CFX_CSSProperty::PaddingBottom, 0.0f, CFX_CSSNumberType::Number);
+ VerifyFloat(CFX_CSSProperty::PaddingLeft, 0.0f,
+ CFX_CSSNumberValue::Unit::kNumber);
+ VerifyFloat(CFX_CSSProperty::PaddingRight, 0.0f,
+ CFX_CSSNumberValue::Unit::kNumber);
+ VerifyFloat(CFX_CSSProperty::PaddingTop, 0.0f,
+ CFX_CSSNumberValue::Unit::kNumber);
+ VerifyFloat(CFX_CSSProperty::PaddingBottom, 0.0f,
+ CFX_CSSNumberValue::Unit::kNumber);
}
TEST_F(CFX_CSSStyleSheetTest, ParseChildSelectors) {
@@ -214,13 +220,13 @@
EXPECT_EQ(4u, decl_->PropertyCountForTesting());
VerifyFloat(CFX_CSSProperty::BorderLeftWidth, 10.0f,
- CFX_CSSNumberType::Pixels);
+ CFX_CSSNumberValue::Unit::kPixels);
VerifyFloat(CFX_CSSProperty::BorderRightWidth, 10.0f,
- CFX_CSSNumberType::Pixels);
+ CFX_CSSNumberValue::Unit::kPixels);
VerifyFloat(CFX_CSSProperty::BorderTopWidth, 10.0f,
- CFX_CSSNumberType::Pixels);
+ CFX_CSSNumberValue::Unit::kPixels);
VerifyFloat(CFX_CSSProperty::BorderBottomWidth, 10.0f,
- CFX_CSSNumberType::Pixels);
+ CFX_CSSNumberValue::Unit::kPixels);
}
TEST_F(CFX_CSSStyleSheetTest, ParseUnhandledSelectors) {
@@ -247,27 +253,32 @@
TEST_F(CFX_CSSStyleSheetTest, ParseBorder) {
LoadAndVerifyDecl(L"a { border: 5px; }", {L"a"}, 4);
- VerifyFloat(CFX_CSSProperty::BorderLeftWidth, 5.0, CFX_CSSNumberType::Pixels);
+ VerifyFloat(CFX_CSSProperty::BorderLeftWidth, 5.0,
+ CFX_CSSNumberValue::Unit::kPixels);
VerifyFloat(CFX_CSSProperty::BorderRightWidth, 5.0,
- CFX_CSSNumberType::Pixels);
- VerifyFloat(CFX_CSSProperty::BorderTopWidth, 5.0, CFX_CSSNumberType::Pixels);
+ CFX_CSSNumberValue::Unit::kPixels);
+ VerifyFloat(CFX_CSSProperty::BorderTopWidth, 5.0,
+ CFX_CSSNumberValue::Unit::kPixels);
VerifyFloat(CFX_CSSProperty::BorderBottomWidth, 5.0,
- CFX_CSSNumberType::Pixels);
+ CFX_CSSNumberValue::Unit::kPixels);
}
TEST_F(CFX_CSSStyleSheetTest, ParseBorderFull) {
LoadAndVerifyDecl(L"a { border: 5px solid red; }", {L"a"}, 4);
- VerifyFloat(CFX_CSSProperty::BorderLeftWidth, 5.0, CFX_CSSNumberType::Pixels);
+ VerifyFloat(CFX_CSSProperty::BorderLeftWidth, 5.0,
+ CFX_CSSNumberValue::Unit::kPixels);
VerifyFloat(CFX_CSSProperty::BorderRightWidth, 5.0,
- CFX_CSSNumberType::Pixels);
- VerifyFloat(CFX_CSSProperty::BorderTopWidth, 5.0, CFX_CSSNumberType::Pixels);
+ CFX_CSSNumberValue::Unit::kPixels);
+ VerifyFloat(CFX_CSSProperty::BorderTopWidth, 5.0,
+ CFX_CSSNumberValue::Unit::kPixels);
VerifyFloat(CFX_CSSProperty::BorderBottomWidth, 5.0,
- CFX_CSSNumberType::Pixels);
+ CFX_CSSNumberValue::Unit::kPixels);
}
TEST_F(CFX_CSSStyleSheetTest, ParseBorderLeft) {
LoadAndVerifyDecl(L"a { border-left: 2.5pc; }", {L"a"}, 1);
- VerifyFloat(CFX_CSSProperty::BorderLeftWidth, 2.5, CFX_CSSNumberType::Picas);
+ VerifyFloat(CFX_CSSProperty::BorderLeftWidth, 2.5,
+ CFX_CSSNumberValue::Unit::kPicas);
}
TEST_F(CFX_CSSStyleSheetTest, ParseBorderLeftThick) {
@@ -277,37 +288,39 @@
TEST_F(CFX_CSSStyleSheetTest, ParseBorderRight) {
LoadAndVerifyDecl(L"a { border-right: 2.5pc; }", {L"a"}, 1);
- VerifyFloat(CFX_CSSProperty::BorderRightWidth, 2.5, CFX_CSSNumberType::Picas);
+ VerifyFloat(CFX_CSSProperty::BorderRightWidth, 2.5,
+ CFX_CSSNumberValue::Unit::kPicas);
}
TEST_F(CFX_CSSStyleSheetTest, ParseBorderTop) {
LoadAndVerifyDecl(L"a { border-top: 2.5pc; }", {L"a"}, 1);
- VerifyFloat(CFX_CSSProperty::BorderTopWidth, 2.5, CFX_CSSNumberType::Picas);
+ VerifyFloat(CFX_CSSProperty::BorderTopWidth, 2.5,
+ CFX_CSSNumberValue::Unit::kPicas);
}
TEST_F(CFX_CSSStyleSheetTest, ParseBorderBottom) {
LoadAndVerifyDecl(L"a { border-bottom: 2.5pc; }", {L"a"}, 1);
VerifyFloat(CFX_CSSProperty::BorderBottomWidth, 2.5,
- CFX_CSSNumberType::Picas);
+ CFX_CSSNumberValue::Unit::kPicas);
}
TEST_F(CFX_CSSStyleSheetTest, ParseWithCommentsInSelector) {
LoadAndVerifyDecl(L"/**{*/a/**}*/ { border-bottom: 2.5pc; }", {L"a"}, 1);
VerifyFloat(CFX_CSSProperty::BorderBottomWidth, 2.5,
- CFX_CSSNumberType::Picas);
+ CFX_CSSNumberValue::Unit::kPicas);
}
TEST_F(CFX_CSSStyleSheetTest, ParseWithCommentsInProperty) {
LoadAndVerifyDecl(L"a { /*}*/border-bottom: 2.5pc; }", {L"a"}, 1);
VerifyFloat(CFX_CSSProperty::BorderBottomWidth, 2.5,
- CFX_CSSNumberType::Picas);
+ CFX_CSSNumberValue::Unit::kPicas);
}
TEST_F(CFX_CSSStyleSheetTest, ParseWithCommentsInValue) {
LoadAndVerifyDecl(L"a { border-bottom: /*;*/2.5pc;/* color:red;*/ }", {L"a"},
1);
VerifyFloat(CFX_CSSProperty::BorderBottomWidth, 2.5,
- CFX_CSSNumberType::Picas);
+ CFX_CSSNumberValue::Unit::kPicas);
}
TEST_F(CFX_CSSStyleSheetTest, ParseWithUnterminatedCommentInSelector) {