From 41b45a88f14ea546d1b46b7c30a1c9c1db778ac3 Mon Sep 17 00:00:00 2001 From: Ryan Harrison Date: Wed, 31 Jan 2018 15:47:32 +0000 Subject: Clean up CSS property table Reorganizes the free functions to be static methods on a class. Converts the Table struct to be an Entry struct defined in the class. Change-Id: I9eca0dfb15187af10cbe2dc4b4c2756d55d957be Reviewed-on: https://pdfium-review.googlesource.com/24712 Commit-Queue: Ryan Harrison Reviewed-by: dsinclair --- BUILD.gn | 4 +- core/fxcrt/css/cfx_cssdatatable.cpp | 139 ------------------------------- core/fxcrt/css/cfx_cssdatatable.h | 28 ------- core/fxcrt/css/cfx_cssdeclaration.cpp | 20 ++--- core/fxcrt/css/cfx_cssdeclaration.h | 6 +- core/fxcrt/css/cfx_csspropertytable.cpp | 142 ++++++++++++++++++++++++++++++++ core/fxcrt/css/cfx_csspropertytable.h | 31 +++++++ core/fxcrt/css/cfx_cssstyleselector.cpp | 17 ++-- core/fxcrt/css/cfx_cssstylesheet.cpp | 14 ++-- core/fxcrt/css/cfx_csssyntaxparser.cpp | 2 +- 10 files changed, 205 insertions(+), 198 deletions(-) delete mode 100644 core/fxcrt/css/cfx_cssdatatable.cpp delete mode 100644 core/fxcrt/css/cfx_cssdatatable.h create mode 100644 core/fxcrt/css/cfx_csspropertytable.cpp create mode 100644 core/fxcrt/css/cfx_csspropertytable.h diff --git a/BUILD.gn b/BUILD.gn index bcb0e7ef32..1323f2a838 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -903,8 +903,6 @@ jumbo_static_library("fxcrt") { "core/fxcrt/css/cfx_csscomputedstyle.h", "core/fxcrt/css/cfx_csscustomproperty.cpp", "core/fxcrt/css/cfx_csscustomproperty.h", - "core/fxcrt/css/cfx_cssdatatable.cpp", - "core/fxcrt/css/cfx_cssdatatable.h", "core/fxcrt/css/cfx_cssdeclaration.cpp", "core/fxcrt/css/cfx_cssdeclaration.h", "core/fxcrt/css/cfx_cssenumvalue.cpp", @@ -915,6 +913,8 @@ jumbo_static_library("fxcrt") { "core/fxcrt/css/cfx_cssnumbervalue.h", "core/fxcrt/css/cfx_csspropertyholder.cpp", "core/fxcrt/css/cfx_csspropertyholder.h", + "core/fxcrt/css/cfx_csspropertytable.cpp", + "core/fxcrt/css/cfx_csspropertytable.h", "core/fxcrt/css/cfx_cssrulecollection.cpp", "core/fxcrt/css/cfx_cssrulecollection.h", "core/fxcrt/css/cfx_cssselector.cpp", diff --git a/core/fxcrt/css/cfx_cssdatatable.cpp b/core/fxcrt/css/cfx_cssdatatable.cpp deleted file mode 100644 index 8b2100aaa2..0000000000 --- a/core/fxcrt/css/cfx_cssdatatable.cpp +++ /dev/null @@ -1,139 +0,0 @@ -// Copyright 2014 PDFium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com - -#include "core/fxcrt/css/cfx_cssdatatable.h" - -#include -#include - -#include "core/fxcrt/css/cfx_cssstyleselector.h" -#include "core/fxcrt/css/cfx_cssvaluelistparser.h" -#include "core/fxcrt/fx_codepage.h" -#include "core/fxcrt/fx_extension.h" - -static const CFX_CSSPropertyTable g_CFX_CSSProperties[] = { - {CFX_CSSProperty::BorderLeft, L"border-left", 0x04080036, - CFX_CSSVALUETYPE_Shorthand}, - {CFX_CSSProperty::Top, L"top", 0x0BEDAF33, - CFX_CSSVALUETYPE_Primitive | CFX_CSSVALUETYPE_MaybeEnum | - CFX_CSSVALUETYPE_MaybeNumber}, - {CFX_CSSProperty::Margin, L"margin", 0x0CB016BE, - CFX_CSSVALUETYPE_List | CFX_CSSVALUETYPE_MaybeEnum | - CFX_CSSVALUETYPE_MaybeNumber}, - {CFX_CSSProperty::TextIndent, L"text-indent", 0x169ADB74, - CFX_CSSVALUETYPE_Primitive | CFX_CSSVALUETYPE_MaybeNumber}, - {CFX_CSSProperty::Right, L"right", 0x193ADE3E, - CFX_CSSVALUETYPE_Primitive | CFX_CSSVALUETYPE_MaybeEnum | - CFX_CSSVALUETYPE_MaybeNumber}, - {CFX_CSSProperty::PaddingLeft, L"padding-left", 0x228CF02F, - CFX_CSSVALUETYPE_Primitive | CFX_CSSVALUETYPE_MaybeNumber}, - {CFX_CSSProperty::MarginLeft, L"margin-left", 0x297C5656, - CFX_CSSVALUETYPE_Primitive | CFX_CSSVALUETYPE_MaybeNumber | - CFX_CSSVALUETYPE_MaybeEnum}, - {CFX_CSSProperty::Border, L"border", 0x2A23349E, - CFX_CSSVALUETYPE_Shorthand}, - {CFX_CSSProperty::BorderTop, L"border-top", 0x2B866ADE, - CFX_CSSVALUETYPE_Shorthand}, - {CFX_CSSProperty::Bottom, L"bottom", 0x399F02B5, - CFX_CSSVALUETYPE_Primitive | CFX_CSSVALUETYPE_MaybeEnum | - CFX_CSSVALUETYPE_MaybeNumber}, - {CFX_CSSProperty::PaddingRight, L"padding-right", 0x3F616AC2, - CFX_CSSVALUETYPE_Primitive | CFX_CSSVALUETYPE_MaybeNumber}, - {CFX_CSSProperty::BorderBottom, L"border-bottom", 0x452CE780, - CFX_CSSVALUETYPE_Shorthand}, - {CFX_CSSProperty::FontFamily, L"font-family", 0x574686E6, - CFX_CSSVALUETYPE_List | CFX_CSSVALUETYPE_MaybeString}, - {CFX_CSSProperty::FontWeight, L"font-weight", 0x6692F60C, - CFX_CSSVALUETYPE_Primitive | CFX_CSSVALUETYPE_MaybeEnum | - CFX_CSSVALUETYPE_MaybeNumber}, - {CFX_CSSProperty::Color, L"color", 0x6E67921F, - CFX_CSSVALUETYPE_Primitive | CFX_CSSVALUETYPE_MaybeEnum | - CFX_CSSVALUETYPE_MaybeColor}, - {CFX_CSSProperty::LetterSpacing, L"letter-spacing", 0x70536102, - CFX_CSSVALUETYPE_Primitive | CFX_CSSVALUETYPE_MaybeEnum | - CFX_CSSVALUETYPE_MaybeNumber}, - {CFX_CSSProperty::TextAlign, L"text-align", 0x7553F1BD, - CFX_CSSVALUETYPE_Primitive | CFX_CSSVALUETYPE_MaybeEnum}, - {CFX_CSSProperty::BorderRightWidth, L"border-right-width", 0x8F5A6036, - CFX_CSSVALUETYPE_Primitive | CFX_CSSVALUETYPE_MaybeEnum | - CFX_CSSVALUETYPE_MaybeNumber}, - {CFX_CSSProperty::VerticalAlign, L"vertical-align", 0x934A87D2, - CFX_CSSVALUETYPE_Primitive | CFX_CSSVALUETYPE_MaybeEnum | - CFX_CSSVALUETYPE_MaybeNumber}, - {CFX_CSSProperty::PaddingTop, L"padding-top", 0x959D22B7, - CFX_CSSVALUETYPE_Primitive | CFX_CSSVALUETYPE_MaybeNumber}, - {CFX_CSSProperty::FontVariant, L"font-variant", 0x9C785779, - CFX_CSSVALUETYPE_Primitive | CFX_CSSVALUETYPE_MaybeEnum}, - {CFX_CSSProperty::BorderWidth, L"border-width", 0xA8DE4FEB, - CFX_CSSVALUETYPE_List | CFX_CSSVALUETYPE_MaybeEnum | - CFX_CSSVALUETYPE_MaybeNumber}, - {CFX_CSSProperty::BorderBottomWidth, L"border-bottom-width", 0xAE41204D, - CFX_CSSVALUETYPE_Primitive | CFX_CSSVALUETYPE_MaybeEnum | - CFX_CSSVALUETYPE_MaybeNumber}, - {CFX_CSSProperty::BorderRight, L"border-right", 0xB78E9EA9, - CFX_CSSVALUETYPE_Shorthand}, - {CFX_CSSProperty::FontSize, L"font-size", 0xB93956DF, - CFX_CSSVALUETYPE_Primitive | CFX_CSSVALUETYPE_MaybeEnum | - CFX_CSSVALUETYPE_MaybeNumber}, - {CFX_CSSProperty::BorderSpacing, L"border-spacing", 0xC72030F0, - CFX_CSSVALUETYPE_List | CFX_CSSVALUETYPE_MaybeNumber}, - {CFX_CSSProperty::FontStyle, L"font-style", 0xCB1950F5, - CFX_CSSVALUETYPE_Primitive | CFX_CSSVALUETYPE_MaybeEnum}, - {CFX_CSSProperty::Font, L"font", 0xCD308B77, CFX_CSSVALUETYPE_Shorthand}, - {CFX_CSSProperty::LineHeight, L"line-height", 0xCFCACE2E, - CFX_CSSVALUETYPE_Primitive | CFX_CSSVALUETYPE_MaybeEnum | - CFX_CSSVALUETYPE_MaybeNumber}, - {CFX_CSSProperty::MarginRight, L"margin-right", 0xD13C58C9, - CFX_CSSVALUETYPE_Primitive | CFX_CSSVALUETYPE_MaybeNumber | - CFX_CSSVALUETYPE_MaybeEnum}, - {CFX_CSSProperty::BorderLeftWidth, L"border-left-width", 0xD1E93D83, - CFX_CSSVALUETYPE_Primitive | CFX_CSSVALUETYPE_MaybeEnum | - CFX_CSSVALUETYPE_MaybeNumber}, - {CFX_CSSProperty::Display, L"display", 0xD4224C36, - CFX_CSSVALUETYPE_Primitive | CFX_CSSVALUETYPE_MaybeEnum}, - {CFX_CSSProperty::PaddingBottom, L"padding-bottom", 0xE555B3B9, - CFX_CSSVALUETYPE_Primitive | CFX_CSSVALUETYPE_MaybeNumber}, - {CFX_CSSProperty::BorderTopWidth, L"border-top-width", 0xED2CB62B, - CFX_CSSVALUETYPE_Primitive | CFX_CSSVALUETYPE_MaybeEnum | - CFX_CSSVALUETYPE_MaybeNumber}, - {CFX_CSSProperty::WordSpacing, L"word-spacing", 0xEDA63BAE, - CFX_CSSVALUETYPE_Primitive | CFX_CSSVALUETYPE_MaybeEnum | - CFX_CSSVALUETYPE_MaybeNumber}, - {CFX_CSSProperty::Left, L"left", 0xF5AD782B, - CFX_CSSVALUETYPE_Primitive | CFX_CSSVALUETYPE_MaybeEnum | - CFX_CSSVALUETYPE_MaybeNumber}, - {CFX_CSSProperty::TextDecoration, L"text-decoration", 0xF7C634BA, - CFX_CSSVALUETYPE_List | CFX_CSSVALUETYPE_MaybeEnum}, - {CFX_CSSProperty::Padding, L"padding", 0xF8C373F7, - CFX_CSSVALUETYPE_List | CFX_CSSVALUETYPE_MaybeNumber}, - {CFX_CSSProperty::MarginBottom, L"margin-bottom", 0xF93485A0, - CFX_CSSVALUETYPE_Primitive | CFX_CSSVALUETYPE_MaybeNumber | - CFX_CSSVALUETYPE_MaybeEnum}, - {CFX_CSSProperty::MarginTop, L"margin-top", 0xFE51DCFE, - CFX_CSSVALUETYPE_Primitive | CFX_CSSVALUETYPE_MaybeNumber | - CFX_CSSVALUETYPE_MaybeEnum}, -}; - -const CFX_CSSPropertyTable* CFX_GetCSSPropertyByName(WideStringView name) { - if (name.IsEmpty()) - return nullptr; - - uint32_t hash = FX_HashCode_GetW(name, true); - - auto cmpFunc = [](const CFX_CSSPropertyTable& iter, const uint32_t& hash) { - return iter.dwHash < hash; - }; - - auto* result = std::lower_bound(std::begin(g_CFX_CSSProperties), - std::end(g_CFX_CSSProperties), hash, cmpFunc); - if (result != std::end(g_CFX_CSSProperties) && result->dwHash == hash) - return result; - return nullptr; -} - -const CFX_CSSPropertyTable* CFX_GetCSSPropertyByEnum(CFX_CSSProperty eName) { - return &g_CFX_CSSProperties[static_cast(eName)]; -} diff --git a/core/fxcrt/css/cfx_cssdatatable.h b/core/fxcrt/css/cfx_cssdatatable.h deleted file mode 100644 index b8476175f6..0000000000 --- a/core/fxcrt/css/cfx_cssdatatable.h +++ /dev/null @@ -1,28 +0,0 @@ -// Copyright 2014 PDFium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com - -#ifndef CORE_FXCRT_CSS_CFX_CSSDATATABLE_H_ -#define CORE_FXCRT_CSS_CFX_CSSDATATABLE_H_ - -#include -#include - -#include "core/fxcrt/css/cfx_css.h" -#include "core/fxcrt/css/cfx_cssnumbervalue.h" -#include "core/fxcrt/css/cfx_cssvalue.h" -#include "core/fxcrt/fx_system.h" - -struct CFX_CSSPropertyTable { - CFX_CSSProperty eName; - const wchar_t* pszName; - uint32_t dwHash; - uint32_t dwType; -}; - -const CFX_CSSPropertyTable* CFX_GetCSSPropertyByName(WideStringView wsName); -const CFX_CSSPropertyTable* CFX_GetCSSPropertyByEnum(CFX_CSSProperty eName); - -#endif // CORE_FXCRT_CSS_CFX_CSSDATATABLE_H_ diff --git a/core/fxcrt/css/cfx_cssdeclaration.cpp b/core/fxcrt/css/cfx_cssdeclaration.cpp index 90bdf4e0ea..e771f6277d 100644 --- a/core/fxcrt/css/cfx_cssdeclaration.cpp +++ b/core/fxcrt/css/cfx_cssdeclaration.cpp @@ -307,7 +307,7 @@ void CFX_CSSDeclaration::AddPropertyHolder(CFX_CSSProperty eProperty, properties_.push_back(std::move(pHolder)); } -void CFX_CSSDeclaration::AddProperty(const CFX_CSSPropertyTable* pTable, +void CFX_CSSDeclaration::AddProperty(const CFX_CSSPropertyTable::Entry* pEntry, const WideStringView& value) { ASSERT(!value.IsEmpty()); @@ -321,7 +321,7 @@ void CFX_CSSDeclaration::AddProperty(const CFX_CSSPropertyTable* pTable, bImportant = true; } - const uint32_t dwType = pTable->dwType; + const uint32_t dwType = pEntry->dwType; switch (dwType & 0x0F) { case CFX_CSSVALUETYPE_Primitive: { static const uint32_t g_ValueGuessOrder[] = { @@ -353,7 +353,7 @@ void CFX_CSSDeclaration::AddProperty(const CFX_CSSPropertyTable* pTable, break; } if (pCSSValue) { - AddPropertyHolder(pTable->eName, pCSSValue, bImportant); + AddPropertyHolder(pEntry->eName, pCSSValue, bImportant); return; } @@ -364,7 +364,7 @@ void CFX_CSSDeclaration::AddProperty(const CFX_CSSPropertyTable* pTable, } case CFX_CSSVALUETYPE_Shorthand: { RetainPtr pWidth; - switch (pTable->eName) { + switch (pEntry->eName) { case CFX_CSSProperty::Font: ParseFontProperty(pszValue, iValueLen, bImportant); return; @@ -414,7 +414,7 @@ void CFX_CSSDeclaration::AddProperty(const CFX_CSSPropertyTable* pTable, } } break; case CFX_CSSVALUETYPE_List: - ParseValueListProperty(pTable, pszValue, iValueLen, bImportant); + ParseValueListProperty(pEntry, pszValue, iValueLen, bImportant); return; default: NOTREACHED(); @@ -466,15 +466,15 @@ RetainPtr CFX_CSSDeclaration::ParseString(const wchar_t* pszValue, } void CFX_CSSDeclaration::ParseValueListProperty( - const CFX_CSSPropertyTable* pTable, + const CFX_CSSPropertyTable::Entry* pEntry, const wchar_t* pszValue, int32_t iValueLen, bool bImportant) { wchar_t separator = - (pTable->eName == CFX_CSSProperty::FontFamily) ? ',' : ' '; + (pEntry->eName == CFX_CSSProperty::FontFamily) ? ',' : ' '; CFX_CSSValueListParser parser(pszValue, iValueLen, separator); - const uint32_t dwType = pTable->dwType; + const uint32_t dwType = pEntry->dwType; CFX_CSSPrimitiveType eType; std::vector> list; while (parser.NextValue(&eType, &pszValue, &iValueLen)) { @@ -524,7 +524,7 @@ void CFX_CSSDeclaration::ParseValueListProperty( if (list.empty()) return; - switch (pTable->eName) { + switch (pEntry->eName) { case CFX_CSSProperty::BorderWidth: Add4ValuesProperty(list, bImportant, CFX_CSSProperty::BorderLeftWidth, CFX_CSSProperty::BorderTopWidth, @@ -545,7 +545,7 @@ void CFX_CSSDeclaration::ParseValueListProperty( return; default: { auto pList = pdfium::MakeRetain(list); - AddPropertyHolder(pTable->eName, pList, bImportant); + AddPropertyHolder(pEntry->eName, pList, bImportant); return; } } diff --git a/core/fxcrt/css/cfx_cssdeclaration.h b/core/fxcrt/css/cfx_cssdeclaration.h index 7dd0bba90e..0dad046a19 100644 --- a/core/fxcrt/css/cfx_cssdeclaration.h +++ b/core/fxcrt/css/cfx_cssdeclaration.h @@ -11,7 +11,7 @@ #include #include -#include "core/fxcrt/css/cfx_cssdatatable.h" +#include "core/fxcrt/css/cfx_csspropertytable.h" class CFX_CSSPropertyHolder; class CFX_CSSCustomProperty; @@ -47,7 +47,7 @@ class CFX_CSSDeclaration { bool empty() const { return properties_.empty(); } - void AddProperty(const CFX_CSSPropertyTable* pTable, + void AddProperty(const CFX_CSSPropertyTable::Entry* pEntry, const WideStringView& value); void AddProperty(const WideString& prop, const WideString& value); @@ -64,7 +64,7 @@ class CFX_CSSDeclaration { bool ParseBorderProperty(const wchar_t* pszValue, int32_t iValueLen, RetainPtr& pWidth) const; - void ParseValueListProperty(const CFX_CSSPropertyTable* pTable, + void ParseValueListProperty(const CFX_CSSPropertyTable::Entry* pEntry, const wchar_t* pszValue, int32_t iValueLen, bool bImportant); diff --git a/core/fxcrt/css/cfx_csspropertytable.cpp b/core/fxcrt/css/cfx_csspropertytable.cpp new file mode 100644 index 0000000000..61294d26b2 --- /dev/null +++ b/core/fxcrt/css/cfx_csspropertytable.cpp @@ -0,0 +1,142 @@ +// Copyright 2014 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "core/fxcrt/css/cfx_csspropertytable.h" + +#include +#include + +#include "core/fxcrt/css/cfx_cssstyleselector.h" +#include "core/fxcrt/css/cfx_cssvaluelistparser.h" +#include "core/fxcrt/fx_codepage.h" +#include "core/fxcrt/fx_extension.h" + +namespace { +const CFX_CSSPropertyTable::Entry propertyTable[] = { + {CFX_CSSProperty::BorderLeft, L"border-left", 0x04080036, + CFX_CSSVALUETYPE_Shorthand}, + {CFX_CSSProperty::Top, L"top", 0x0BEDAF33, + CFX_CSSVALUETYPE_Primitive | CFX_CSSVALUETYPE_MaybeEnum | + CFX_CSSVALUETYPE_MaybeNumber}, + {CFX_CSSProperty::Margin, L"margin", 0x0CB016BE, + CFX_CSSVALUETYPE_List | CFX_CSSVALUETYPE_MaybeEnum | + CFX_CSSVALUETYPE_MaybeNumber}, + {CFX_CSSProperty::TextIndent, L"text-indent", 0x169ADB74, + CFX_CSSVALUETYPE_Primitive | CFX_CSSVALUETYPE_MaybeNumber}, + {CFX_CSSProperty::Right, L"right", 0x193ADE3E, + CFX_CSSVALUETYPE_Primitive | CFX_CSSVALUETYPE_MaybeEnum | + CFX_CSSVALUETYPE_MaybeNumber}, + {CFX_CSSProperty::PaddingLeft, L"padding-left", 0x228CF02F, + CFX_CSSVALUETYPE_Primitive | CFX_CSSVALUETYPE_MaybeNumber}, + {CFX_CSSProperty::MarginLeft, L"margin-left", 0x297C5656, + CFX_CSSVALUETYPE_Primitive | CFX_CSSVALUETYPE_MaybeNumber | + CFX_CSSVALUETYPE_MaybeEnum}, + {CFX_CSSProperty::Border, L"border", 0x2A23349E, + CFX_CSSVALUETYPE_Shorthand}, + {CFX_CSSProperty::BorderTop, L"border-top", 0x2B866ADE, + CFX_CSSVALUETYPE_Shorthand}, + {CFX_CSSProperty::Bottom, L"bottom", 0x399F02B5, + CFX_CSSVALUETYPE_Primitive | CFX_CSSVALUETYPE_MaybeEnum | + CFX_CSSVALUETYPE_MaybeNumber}, + {CFX_CSSProperty::PaddingRight, L"padding-right", 0x3F616AC2, + CFX_CSSVALUETYPE_Primitive | CFX_CSSVALUETYPE_MaybeNumber}, + {CFX_CSSProperty::BorderBottom, L"border-bottom", 0x452CE780, + CFX_CSSVALUETYPE_Shorthand}, + {CFX_CSSProperty::FontFamily, L"font-family", 0x574686E6, + CFX_CSSVALUETYPE_List | CFX_CSSVALUETYPE_MaybeString}, + {CFX_CSSProperty::FontWeight, L"font-weight", 0x6692F60C, + CFX_CSSVALUETYPE_Primitive | CFX_CSSVALUETYPE_MaybeEnum | + CFX_CSSVALUETYPE_MaybeNumber}, + {CFX_CSSProperty::Color, L"color", 0x6E67921F, + CFX_CSSVALUETYPE_Primitive | CFX_CSSVALUETYPE_MaybeEnum | + CFX_CSSVALUETYPE_MaybeColor}, + {CFX_CSSProperty::LetterSpacing, L"letter-spacing", 0x70536102, + CFX_CSSVALUETYPE_Primitive | CFX_CSSVALUETYPE_MaybeEnum | + CFX_CSSVALUETYPE_MaybeNumber}, + {CFX_CSSProperty::TextAlign, L"text-align", 0x7553F1BD, + CFX_CSSVALUETYPE_Primitive | CFX_CSSVALUETYPE_MaybeEnum}, + {CFX_CSSProperty::BorderRightWidth, L"border-right-width", 0x8F5A6036, + CFX_CSSVALUETYPE_Primitive | CFX_CSSVALUETYPE_MaybeEnum | + CFX_CSSVALUETYPE_MaybeNumber}, + {CFX_CSSProperty::VerticalAlign, L"vertical-align", 0x934A87D2, + CFX_CSSVALUETYPE_Primitive | CFX_CSSVALUETYPE_MaybeEnum | + CFX_CSSVALUETYPE_MaybeNumber}, + {CFX_CSSProperty::PaddingTop, L"padding-top", 0x959D22B7, + CFX_CSSVALUETYPE_Primitive | CFX_CSSVALUETYPE_MaybeNumber}, + {CFX_CSSProperty::FontVariant, L"font-variant", 0x9C785779, + CFX_CSSVALUETYPE_Primitive | CFX_CSSVALUETYPE_MaybeEnum}, + {CFX_CSSProperty::BorderWidth, L"border-width", 0xA8DE4FEB, + CFX_CSSVALUETYPE_List | CFX_CSSVALUETYPE_MaybeEnum | + CFX_CSSVALUETYPE_MaybeNumber}, + {CFX_CSSProperty::BorderBottomWidth, L"border-bottom-width", 0xAE41204D, + CFX_CSSVALUETYPE_Primitive | CFX_CSSVALUETYPE_MaybeEnum | + CFX_CSSVALUETYPE_MaybeNumber}, + {CFX_CSSProperty::BorderRight, L"border-right", 0xB78E9EA9, + CFX_CSSVALUETYPE_Shorthand}, + {CFX_CSSProperty::FontSize, L"font-size", 0xB93956DF, + CFX_CSSVALUETYPE_Primitive | CFX_CSSVALUETYPE_MaybeEnum | + CFX_CSSVALUETYPE_MaybeNumber}, + {CFX_CSSProperty::BorderSpacing, L"border-spacing", 0xC72030F0, + CFX_CSSVALUETYPE_List | CFX_CSSVALUETYPE_MaybeNumber}, + {CFX_CSSProperty::FontStyle, L"font-style", 0xCB1950F5, + CFX_CSSVALUETYPE_Primitive | CFX_CSSVALUETYPE_MaybeEnum}, + {CFX_CSSProperty::Font, L"font", 0xCD308B77, CFX_CSSVALUETYPE_Shorthand}, + {CFX_CSSProperty::LineHeight, L"line-height", 0xCFCACE2E, + CFX_CSSVALUETYPE_Primitive | CFX_CSSVALUETYPE_MaybeEnum | + CFX_CSSVALUETYPE_MaybeNumber}, + {CFX_CSSProperty::MarginRight, L"margin-right", 0xD13C58C9, + CFX_CSSVALUETYPE_Primitive | CFX_CSSVALUETYPE_MaybeNumber | + CFX_CSSVALUETYPE_MaybeEnum}, + {CFX_CSSProperty::BorderLeftWidth, L"border-left-width", 0xD1E93D83, + CFX_CSSVALUETYPE_Primitive | CFX_CSSVALUETYPE_MaybeEnum | + CFX_CSSVALUETYPE_MaybeNumber}, + {CFX_CSSProperty::Display, L"display", 0xD4224C36, + CFX_CSSVALUETYPE_Primitive | CFX_CSSVALUETYPE_MaybeEnum}, + {CFX_CSSProperty::PaddingBottom, L"padding-bottom", 0xE555B3B9, + CFX_CSSVALUETYPE_Primitive | CFX_CSSVALUETYPE_MaybeNumber}, + {CFX_CSSProperty::BorderTopWidth, L"border-top-width", 0xED2CB62B, + CFX_CSSVALUETYPE_Primitive | CFX_CSSVALUETYPE_MaybeEnum | + CFX_CSSVALUETYPE_MaybeNumber}, + {CFX_CSSProperty::WordSpacing, L"word-spacing", 0xEDA63BAE, + CFX_CSSVALUETYPE_Primitive | CFX_CSSVALUETYPE_MaybeEnum | + CFX_CSSVALUETYPE_MaybeNumber}, + {CFX_CSSProperty::Left, L"left", 0xF5AD782B, + CFX_CSSVALUETYPE_Primitive | CFX_CSSVALUETYPE_MaybeEnum | + CFX_CSSVALUETYPE_MaybeNumber}, + {CFX_CSSProperty::TextDecoration, L"text-decoration", 0xF7C634BA, + CFX_CSSVALUETYPE_List | CFX_CSSVALUETYPE_MaybeEnum}, + {CFX_CSSProperty::Padding, L"padding", 0xF8C373F7, + CFX_CSSVALUETYPE_List | CFX_CSSVALUETYPE_MaybeNumber}, + {CFX_CSSProperty::MarginBottom, L"margin-bottom", 0xF93485A0, + CFX_CSSVALUETYPE_Primitive | CFX_CSSVALUETYPE_MaybeNumber | + CFX_CSSVALUETYPE_MaybeEnum}, + {CFX_CSSProperty::MarginTop, L"margin-top", 0xFE51DCFE, + CFX_CSSVALUETYPE_Primitive | CFX_CSSVALUETYPE_MaybeNumber | + CFX_CSSVALUETYPE_MaybeEnum}, +}; +} // namespace + +const CFX_CSSPropertyTable::Entry* CFX_CSSPropertyTable::GetByName( + WideStringView name) { + if (name.IsEmpty()) + return nullptr; + + uint32_t hash = FX_HashCode_GetW(name, true); + + auto cmpFunc = [](const CFX_CSSPropertyTable::Entry& iter, + const uint32_t& hash) { return iter.dwHash < hash; }; + + auto* result = std::lower_bound(std::begin(propertyTable), + std::end(propertyTable), hash, cmpFunc); + if (result != std::end(propertyTable) && result->dwHash == hash) + return result; + return nullptr; +} + +const CFX_CSSPropertyTable::Entry* CFX_CSSPropertyTable::GetByEnum( + CFX_CSSProperty property) { + return &propertyTable[static_cast(property)]; +} diff --git a/core/fxcrt/css/cfx_csspropertytable.h b/core/fxcrt/css/cfx_csspropertytable.h new file mode 100644 index 0000000000..924e962676 --- /dev/null +++ b/core/fxcrt/css/cfx_csspropertytable.h @@ -0,0 +1,31 @@ +// Copyright 2018 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef CORE_FXCRT_CSS_CFX_CSSPROPERTYTABLE_H_ +#define CORE_FXCRT_CSS_CFX_CSSPROPERTYTABLE_H_ + +#include +#include + +#include "core/fxcrt/css/cfx_css.h" +#include "core/fxcrt/css/cfx_cssnumbervalue.h" +#include "core/fxcrt/css/cfx_cssvalue.h" +#include "core/fxcrt/string_view_template.h" + +class CFX_CSSPropertyTable { + public: + struct Entry { + CFX_CSSProperty eName; + const wchar_t* pszName; + uint32_t dwHash; + uint32_t dwType; + }; + + static const Entry* GetByName(WideStringView nam); + static const Entry* GetByEnum(CFX_CSSProperty property); +}; + +#endif // CORE_FXCRT_CSS_CFX_CSSPROPERTYTABLE_H_ diff --git a/core/fxcrt/css/cfx_cssstyleselector.cpp b/core/fxcrt/css/cfx_cssstyleselector.cpp index cb9ab2f499..f079a35f59 100644 --- a/core/fxcrt/css/cfx_cssstyleselector.cpp +++ b/core/fxcrt/css/cfx_cssstyleselector.cpp @@ -90,8 +90,9 @@ void CFX_CSSStyleSelector::ComputeStyle( if (!styleString.IsEmpty()) AppendInlineStyle(pDecl.get(), styleString); if (!alignString.IsEmpty()) { - pDecl->AddProperty(CFX_GetCSSPropertyByEnum(CFX_CSSProperty::TextAlign), - alignString.AsStringView()); + pDecl->AddProperty( + CFX_CSSPropertyTable::GetByEnum(CFX_CSSProperty::TextAlign), + alignString.AsStringView()); } } ApplyDeclarations(declArray, pDecl.get(), pDest); @@ -143,21 +144,21 @@ void CFX_CSSStyleSelector::AppendInlineStyle(CFX_CSSDeclaration* pDecl, auto pSyntax = pdfium::MakeUnique( style.c_str(), style.GetLength(), 32, true); int32_t iLen2 = 0; - const CFX_CSSPropertyTable* table = nullptr; + const CFX_CSSPropertyTable::Entry* entry = nullptr; WideString wsName; while (1) { CFX_CSSSyntaxStatus eStatus = pSyntax->DoSyntaxParse(); if (eStatus == CFX_CSSSyntaxStatus::PropertyName) { WideStringView strValue = pSyntax->GetCurrentString(); - table = CFX_GetCSSPropertyByName(strValue); - if (!table) + entry = CFX_CSSPropertyTable::GetByName(strValue); + if (!entry) wsName = WideString(strValue); } else if (eStatus == CFX_CSSSyntaxStatus::PropertyValue) { - if (table || iLen2 > 0) { + if (entry || iLen2 > 0) { WideStringView strValue = pSyntax->GetCurrentString(); if (!strValue.IsEmpty()) { - if (table) - pDecl->AddProperty(table, strValue); + if (entry) + pDecl->AddProperty(entry, strValue); else if (iLen2 > 0) pDecl->AddProperty(wsName, WideString(strValue)); } diff --git a/core/fxcrt/css/cfx_cssstylesheet.cpp b/core/fxcrt/css/cfx_cssstylesheet.cpp index 183765f3b0..01c68fce7d 100644 --- a/core/fxcrt/css/cfx_cssstylesheet.cpp +++ b/core/fxcrt/css/cfx_cssstylesheet.cpp @@ -8,8 +8,8 @@ #include -#include "core/fxcrt/css/cfx_cssdatatable.h" #include "core/fxcrt/css/cfx_cssdeclaration.h" +#include "core/fxcrt/css/cfx_csspropertytable.h" #include "core/fxcrt/css/cfx_cssstylerule.h" #include "core/fxcrt/fx_codepage.h" #include "third_party/base/ptr_util.h" @@ -62,7 +62,7 @@ CFX_CSSSyntaxStatus CFX_CSSStyleSheet::LoadStyleRule( CFX_CSSStyleRule* pStyleRule = nullptr; int32_t iValueLen = 0; - const CFX_CSSPropertyTable* propertyTable = nullptr; + const CFX_CSSPropertyTable::Entry* entry = nullptr; WideString wsName; while (1) { switch (pSyntax->DoSyntaxParse()) { @@ -75,18 +75,18 @@ CFX_CSSSyntaxStatus CFX_CSSStyleSheet::LoadStyleRule( } case CFX_CSSSyntaxStatus::PropertyName: { WideStringView strValue = pSyntax->GetCurrentString(); - propertyTable = CFX_GetCSSPropertyByName(strValue); - if (!propertyTable) + entry = CFX_CSSPropertyTable::GetByName(strValue); + if (!entry) wsName = WideString(strValue); break; } case CFX_CSSSyntaxStatus::PropertyValue: { - if (propertyTable || iValueLen > 0) { + if (entry || iValueLen > 0) { WideStringView strValue = pSyntax->GetCurrentString(); auto* decl = pStyleRule->GetDeclaration(); if (!strValue.IsEmpty()) { - if (propertyTable) { - decl->AddProperty(propertyTable, strValue); + if (entry) { + decl->AddProperty(entry, strValue); } else { decl->AddProperty(wsName, WideString(strValue)); } diff --git a/core/fxcrt/css/cfx_csssyntaxparser.cpp b/core/fxcrt/css/cfx_csssyntaxparser.cpp index 066b82b5d6..e24c17c16c 100644 --- a/core/fxcrt/css/cfx_csssyntaxparser.cpp +++ b/core/fxcrt/css/cfx_csssyntaxparser.cpp @@ -8,8 +8,8 @@ #include -#include "core/fxcrt/css/cfx_cssdatatable.h" #include "core/fxcrt/css/cfx_cssdeclaration.h" +#include "core/fxcrt/css/cfx_csspropertytable.h" #include "core/fxcrt/fx_codepage.h" #include "core/fxcrt/fx_extension.h" #include "third_party/base/logging.h" -- cgit v1.2.3