diff options
Diffstat (limited to 'xfa/fde')
38 files changed, 1555 insertions, 1408 deletions
diff --git a/xfa/fde/css/cfde_cssaccelerator.cpp b/xfa/fde/css/cfde_cssaccelerator.cpp new file mode 100644 index 0000000000..40fa13e058 --- /dev/null +++ b/xfa/fde/css/cfde_cssaccelerator.cpp @@ -0,0 +1,23 @@ +// Copyright 2017 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 "xfa/fde/css/cfde_cssaccelerator.h" + +#include "third_party/base/ptr_util.h" + +CFDE_CSSAccelerator::CFDE_CSSAccelerator() {} + +CFDE_CSSAccelerator::~CFDE_CSSAccelerator() {} + +void CFDE_CSSAccelerator::OnEnterTag(CXFA_CSSTagProvider* pTag) { + stack_.push(pdfium::MakeUnique<CFDE_CSSTagCache>(top(), pTag)); +} + +void CFDE_CSSAccelerator::OnLeaveTag(CXFA_CSSTagProvider* pTag) { + ASSERT(!stack_.empty()); + ASSERT(stack_.top()->GetTag() == pTag); + stack_.pop(); +} diff --git a/xfa/fde/css/cfde_cssaccelerator.h b/xfa/fde/css/cfde_cssaccelerator.h new file mode 100644 index 0000000000..4ef493d79a --- /dev/null +++ b/xfa/fde/css/cfde_cssaccelerator.h @@ -0,0 +1,40 @@ +// Copyright 2017 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 XFA_FDE_CSS_CFDE_CSSACCELERATOR_H_ +#define XFA_FDE_CSS_CFDE_CSSACCELERATOR_H_ + +#include <memory> +#include <stack> + +#include "xfa/fde/css/cfde_csstagcache.h" + +class CXFA_CSSTagProvider; + +class CFDE_CSSAccelerator { + public: + CFDE_CSSAccelerator(); + ~CFDE_CSSAccelerator(); + + void OnEnterTag(CXFA_CSSTagProvider* pTag); + void OnLeaveTag(CXFA_CSSTagProvider* pTag); + + void Clear() { + std::stack<std::unique_ptr<CFDE_CSSTagCache>> tmp; + stack_.swap(tmp); + } + + CFDE_CSSTagCache* top() const { + if (stack_.empty()) + return nullptr; + return stack_.top().get(); + } + + private: + std::stack<std::unique_ptr<CFDE_CSSTagCache>> stack_; +}; + +#endif // XFA_FDE_CSS_CFDE_CSSACCELERATOR_H_ diff --git a/xfa/fde/css/cfde_csscomputedstyle.cpp b/xfa/fde/css/cfde_csscomputedstyle.cpp new file mode 100644 index 0000000000..010f573c04 --- /dev/null +++ b/xfa/fde/css/cfde_csscomputedstyle.cpp @@ -0,0 +1,204 @@ +// Copyright 2017 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 "xfa/fde/css/cfde_csscomputedstyle.h" + +#include "third_party/base/stl_util.h" +#include "xfa/fde/css/cfde_cssstringvalue.h" +#include "xfa/fde/css/cfde_cssvaluelist.h" + +CFDE_CSSComputedStyle::CFDE_CSSComputedStyle() : m_dwRefCount(1) {} + +CFDE_CSSComputedStyle::~CFDE_CSSComputedStyle() {} + +uint32_t CFDE_CSSComputedStyle::Retain() { + return ++m_dwRefCount; +} + +uint32_t CFDE_CSSComputedStyle::Release() { + uint32_t dwRefCount = --m_dwRefCount; + if (dwRefCount == 0) + delete this; + return dwRefCount; +} + +bool CFDE_CSSComputedStyle::GetCustomStyle(const CFX_WideStringC& wsName, + CFX_WideString& wsValue) const { + for (int32_t i = pdfium::CollectionSize<int32_t>(m_CustomProperties) - 2; + i > -1; i -= 2) { + if (wsName == m_CustomProperties[i]) { + wsValue = m_CustomProperties[i + 1]; + return true; + } + } + return false; +} + +int32_t CFDE_CSSComputedStyle::CountFontFamilies() const { + return m_InheritedData.m_pFontFamily + ? m_InheritedData.m_pFontFamily->CountValues() + : 0; +} + +const CFX_WideString CFDE_CSSComputedStyle::GetFontFamily(int32_t index) const { + return static_cast<CFDE_CSSStringValue*>( + m_InheritedData.m_pFontFamily->GetValue(index)) + ->Value(); +} + +uint16_t CFDE_CSSComputedStyle::GetFontWeight() const { + return m_InheritedData.m_wFontWeight; +} + +FDE_CSSFontVariant CFDE_CSSComputedStyle::GetFontVariant() const { + return m_InheritedData.m_eFontVariant; +} + +FDE_CSSFontStyle CFDE_CSSComputedStyle::GetFontStyle() const { + return m_InheritedData.m_eFontStyle; +} + +FX_FLOAT CFDE_CSSComputedStyle::GetFontSize() const { + return m_InheritedData.m_fFontSize; +} + +FX_ARGB CFDE_CSSComputedStyle::GetColor() const { + return m_InheritedData.m_dwFontColor; +} + +void CFDE_CSSComputedStyle::SetFontWeight(uint16_t wFontWeight) { + m_InheritedData.m_wFontWeight = wFontWeight; +} + +void CFDE_CSSComputedStyle::SetFontVariant(FDE_CSSFontVariant eFontVariant) { + m_InheritedData.m_eFontVariant = eFontVariant; +} + +void CFDE_CSSComputedStyle::SetFontStyle(FDE_CSSFontStyle eFontStyle) { + m_InheritedData.m_eFontStyle = eFontStyle; +} + +void CFDE_CSSComputedStyle::SetFontSize(FX_FLOAT fFontSize) { + m_InheritedData.m_fFontSize = fFontSize; +} + +void CFDE_CSSComputedStyle::SetColor(FX_ARGB dwFontColor) { + m_InheritedData.m_dwFontColor = dwFontColor; +} + +const FDE_CSSRect* CFDE_CSSComputedStyle::GetBorderWidth() const { + return m_NonInheritedData.m_bHasBorder ? &(m_NonInheritedData.m_BorderWidth) + : nullptr; +} + +const FDE_CSSRect* CFDE_CSSComputedStyle::GetMarginWidth() const { + return m_NonInheritedData.m_bHasMargin ? &(m_NonInheritedData.m_MarginWidth) + : nullptr; +} + +const FDE_CSSRect* CFDE_CSSComputedStyle::GetPaddingWidth() const { + return m_NonInheritedData.m_bHasPadding ? &(m_NonInheritedData.m_PaddingWidth) + : nullptr; +} + +void CFDE_CSSComputedStyle::SetMarginWidth(const FDE_CSSRect& rect) { + m_NonInheritedData.m_MarginWidth = rect; + m_NonInheritedData.m_bHasMargin = true; +} + +void CFDE_CSSComputedStyle::SetPaddingWidth(const FDE_CSSRect& rect) { + m_NonInheritedData.m_PaddingWidth = rect; + m_NonInheritedData.m_bHasPadding = true; +} + +FDE_CSSDisplay CFDE_CSSComputedStyle::GetDisplay() const { + return m_NonInheritedData.m_eDisplay; +} + +FX_FLOAT CFDE_CSSComputedStyle::GetLineHeight() const { + return m_InheritedData.m_fLineHeight; +} + +const FDE_CSSLength& CFDE_CSSComputedStyle::GetTextIndent() const { + return m_InheritedData.m_TextIndent; +} + +FDE_CSSTextAlign CFDE_CSSComputedStyle::GetTextAlign() const { + return m_InheritedData.m_eTextAlign; +} + +FDE_CSSVerticalAlign CFDE_CSSComputedStyle::GetVerticalAlign() const { + return m_NonInheritedData.m_eVerticalAlign; +} + +FX_FLOAT CFDE_CSSComputedStyle::GetNumberVerticalAlign() const { + return m_NonInheritedData.m_fVerticalAlign; +} + +uint32_t CFDE_CSSComputedStyle::GetTextDecoration() const { + return m_NonInheritedData.m_dwTextDecoration; +} + +const FDE_CSSLength& CFDE_CSSComputedStyle::GetLetterSpacing() const { + return m_InheritedData.m_LetterSpacing; +} + +void CFDE_CSSComputedStyle::SetLineHeight(FX_FLOAT fLineHeight) { + m_InheritedData.m_fLineHeight = fLineHeight; +} + +void CFDE_CSSComputedStyle::SetTextIndent(const FDE_CSSLength& textIndent) { + m_InheritedData.m_TextIndent = textIndent; +} + +void CFDE_CSSComputedStyle::SetTextAlign(FDE_CSSTextAlign eTextAlign) { + m_InheritedData.m_eTextAlign = eTextAlign; +} + +void CFDE_CSSComputedStyle::SetNumberVerticalAlign(FX_FLOAT fAlign) { + m_NonInheritedData.m_eVerticalAlign = FDE_CSSVerticalAlign::Number, + m_NonInheritedData.m_fVerticalAlign = fAlign; +} + +void CFDE_CSSComputedStyle::SetTextDecoration(uint32_t dwTextDecoration) { + m_NonInheritedData.m_dwTextDecoration = dwTextDecoration; +} + +void CFDE_CSSComputedStyle::SetLetterSpacing( + const FDE_CSSLength& letterSpacing) { + m_InheritedData.m_LetterSpacing = letterSpacing; +} + +void CFDE_CSSComputedStyle::AddCustomStyle(const CFX_WideString& wsName, + const CFX_WideString& wsValue) { + m_CustomProperties.push_back(wsName); + m_CustomProperties.push_back(wsValue); +} + +CFDE_CSSComputedStyle::InheritedData::InheritedData() + : m_LetterSpacing(FDE_CSSLengthUnit::Normal), + m_WordSpacing(FDE_CSSLengthUnit::Normal), + m_TextIndent(FDE_CSSLengthUnit::Point, 0), + m_pFontFamily(nullptr), + m_fFontSize(12.0f), + m_fLineHeight(14.0f), + m_dwFontColor(0xFF000000), + m_wFontWeight(400), + m_eFontVariant(FDE_CSSFontVariant::Normal), + m_eFontStyle(FDE_CSSFontStyle::Normal), + m_eTextAlign(FDE_CSSTextAlign::Left) {} + +CFDE_CSSComputedStyle::NonInheritedData::NonInheritedData() + : m_MarginWidth(FDE_CSSLengthUnit::Point, 0), + m_BorderWidth(FDE_CSSLengthUnit::Point, 0), + m_PaddingWidth(FDE_CSSLengthUnit::Point, 0), + m_fVerticalAlign(0.0f), + m_eDisplay(FDE_CSSDisplay::Inline), + m_eVerticalAlign(FDE_CSSVerticalAlign::Baseline), + m_dwTextDecoration(0), + m_bHasMargin(false), + m_bHasBorder(false), + m_bHasPadding(false) {} diff --git a/xfa/fde/css/cfde_csscomputedstyle.h b/xfa/fde/css/cfde_csscomputedstyle.h new file mode 100644 index 0000000000..73eb996f0d --- /dev/null +++ b/xfa/fde/css/cfde_csscomputedstyle.h @@ -0,0 +1,112 @@ +// Copyright 2017 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 XFA_FDE_CSS_CFDE_CSSCOMPUTEDSTYLE_H_ +#define XFA_FDE_CSS_CFDE_CSSCOMPUTEDSTYLE_H_ + +#include <vector> + +#include "core/fxcrt/fx_basic.h" +#include "core/fxcrt/fx_string.h" +#include "xfa/fde/css/fde_css.h" + +class CFDE_CSSValueList; + +class CFDE_CSSComputedStyle : public IFX_Retainable { + public: + class InheritedData { + public: + InheritedData(); + + FDE_CSSLength m_LetterSpacing; + FDE_CSSLength m_WordSpacing; + FDE_CSSLength m_TextIndent; + CFDE_CSSValueList* m_pFontFamily; + FX_FLOAT m_fFontSize; + FX_FLOAT m_fLineHeight; + FX_ARGB m_dwFontColor; + uint16_t m_wFontWeight; + FDE_CSSFontVariant m_eFontVariant; + FDE_CSSFontStyle m_eFontStyle; + FDE_CSSTextAlign m_eTextAlign; + }; + + class NonInheritedData { + public: + NonInheritedData(); + + FDE_CSSRect m_MarginWidth; + FDE_CSSRect m_BorderWidth; + FDE_CSSRect m_PaddingWidth; + FDE_CSSLength m_Top; + FDE_CSSLength m_Bottom; + FDE_CSSLength m_Left; + FDE_CSSLength m_Right; + FX_FLOAT m_fVerticalAlign; + FDE_CSSDisplay m_eDisplay; + FDE_CSSVerticalAlign m_eVerticalAlign; + uint8_t m_dwTextDecoration; + bool m_bHasMargin; + bool m_bHasBorder; + bool m_bHasPadding; + }; + + CFDE_CSSComputedStyle(); + ~CFDE_CSSComputedStyle() override; + + // IFX_Retainable + uint32_t Retain() override; + uint32_t Release() override; + + int32_t CountFontFamilies() const; + const CFX_WideString GetFontFamily(int32_t index) const; + uint16_t GetFontWeight() const; + FDE_CSSFontVariant GetFontVariant() const; + FDE_CSSFontStyle GetFontStyle() const; + FX_FLOAT GetFontSize() const; + FX_ARGB GetColor() const; + void SetFontWeight(uint16_t wFontWeight); + void SetFontVariant(FDE_CSSFontVariant eFontVariant); + void SetFontStyle(FDE_CSSFontStyle eFontStyle); + void SetFontSize(FX_FLOAT fFontSize); + void SetColor(FX_ARGB dwFontColor); + + const FDE_CSSRect* GetBorderWidth() const; + const FDE_CSSRect* GetMarginWidth() const; + const FDE_CSSRect* GetPaddingWidth() const; + void SetMarginWidth(const FDE_CSSRect& rect); + void SetPaddingWidth(const FDE_CSSRect& rect); + + FDE_CSSDisplay GetDisplay() const; + + FX_FLOAT GetLineHeight() const; + const FDE_CSSLength& GetTextIndent() const; + FDE_CSSTextAlign GetTextAlign() const; + FDE_CSSVerticalAlign GetVerticalAlign() const; + FX_FLOAT GetNumberVerticalAlign() const; + uint32_t GetTextDecoration() const; + const FDE_CSSLength& GetLetterSpacing() const; + void SetLineHeight(FX_FLOAT fLineHeight); + void SetTextIndent(const FDE_CSSLength& textIndent); + void SetTextAlign(FDE_CSSTextAlign eTextAlign); + void SetNumberVerticalAlign(FX_FLOAT fAlign); + void SetTextDecoration(uint32_t dwTextDecoration); + void SetLetterSpacing(const FDE_CSSLength& letterSpacing); + void AddCustomStyle(const CFX_WideString& wsName, + const CFX_WideString& wsValue); + + bool GetCustomStyle(const CFX_WideStringC& wsName, + CFX_WideString& wsValue) const; + + InheritedData m_InheritedData; + NonInheritedData m_NonInheritedData; + + private: + uint32_t m_dwRefCount; + std::vector<CFX_WideString> m_CustomProperties; +}; + +#endif // XFA_FDE_CSS_CFDE_CSSCOMPUTEDSTYLE_H_ diff --git a/xfa/fde/css/cfde_csscustomproperty.h b/xfa/fde/css/cfde_csscustomproperty.h new file mode 100644 index 0000000000..6e99630e1c --- /dev/null +++ b/xfa/fde/css/cfde_csscustomproperty.h @@ -0,0 +1,18 @@ +// Copyright 2017 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 XFA_FDE_CSS_CFDE_CSSCUSTOMPROPERTY_H_ +#define XFA_FDE_CSS_CFDE_CSSCUSTOMPROPERTY_H_ + +#include "core/fxcrt/fx_string.h" + +class CFDE_CSSCustomProperty { + public: + const FX_WCHAR* pwsName; + const FX_WCHAR* pwsValue; +}; + +#endif // XFA_FDE_CSS_CFDE_CSSCUSTOMPROPERTY_H_ diff --git a/xfa/fde/css/fde_cssdeclaration.cpp b/xfa/fde/css/cfde_cssdeclaration.cpp index e82b279c04..4ed20eb0cc 100644 --- a/xfa/fde/css/fde_cssdeclaration.cpp +++ b/xfa/fde/css/cfde_cssdeclaration.cpp @@ -4,17 +4,131 @@ // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com -#include "xfa/fde/css/fde_cssdeclaration.h" +#include "xfa/fde/css/cfde_cssdeclaration.h" #include "core/fxcrt/fx_ext.h" #include "third_party/base/ptr_util.h" #include "xfa/fde/css/cfde_csscolorvalue.h" +#include "xfa/fde/css/cfde_csscustomproperty.h" #include "xfa/fde/css/cfde_cssenumvalue.h" #include "xfa/fde/css/cfde_cssnumbervalue.h" +#include "xfa/fde/css/cfde_csspropertyholder.h" #include "xfa/fde/css/cfde_cssstringvalue.h" #include "xfa/fde/css/cfde_cssvaluelist.h" #include "xfa/fde/css/cfde_cssvaluelistparser.h" +namespace { + +uint8_t Hex2Dec(uint8_t hexHigh, uint8_t hexLow) { + return (FXSYS_toHexDigit(hexHigh) << 4) + FXSYS_toHexDigit(hexLow); +} + +bool ParseCSSNumber(const FX_WCHAR* pszValue, + int32_t iValueLen, + FX_FLOAT& fValue, + FDE_CSSNumberType& eUnit) { + ASSERT(pszValue && iValueLen > 0); + int32_t iUsedLen = 0; + fValue = FXSYS_wcstof(pszValue, iValueLen, &iUsedLen); + if (iUsedLen <= 0) + return false; + + iValueLen -= iUsedLen; + pszValue += iUsedLen; + eUnit = FDE_CSSNumberType::Number; + if (iValueLen >= 1 && *pszValue == '%') { + eUnit = FDE_CSSNumberType::Percent; + } else if (iValueLen == 2) { + const FDE_CSSLengthUnitTable* pUnit = + FDE_GetCSSLengthUnitByName(CFX_WideStringC(pszValue, 2)); + if (pUnit) + eUnit = pUnit->wValue; + } + return true; +} + +} // namespace + +// static +bool CFDE_CSSDeclaration::ParseCSSString(const FX_WCHAR* pszValue, + int32_t iValueLen, + int32_t* iOffset, + int32_t* iLength) { + ASSERT(pszValue && iValueLen > 0); + *iOffset = 0; + *iLength = iValueLen; + if (iValueLen >= 2) { + FX_WCHAR first = pszValue[0], last = pszValue[iValueLen - 1]; + if ((first == '\"' && last == '\"') || (first == '\'' && last == '\'')) { + *iOffset = 1; + *iLength -= 2; + } + } + return iValueLen > 0; +} + +// static. +bool CFDE_CSSDeclaration::ParseCSSColor(const FX_WCHAR* pszValue, + int32_t iValueLen, + FX_ARGB* dwColor) { + ASSERT(pszValue && iValueLen > 0); + ASSERT(dwColor); + + if (*pszValue == '#') { + switch (iValueLen) { + case 4: { + uint8_t red = Hex2Dec((uint8_t)pszValue[1], (uint8_t)pszValue[1]); + uint8_t green = Hex2Dec((uint8_t)pszValue[2], (uint8_t)pszValue[2]); + uint8_t blue = Hex2Dec((uint8_t)pszValue[3], (uint8_t)pszValue[3]); + *dwColor = ArgbEncode(255, red, green, blue); + return true; + } + case 7: { + uint8_t red = Hex2Dec((uint8_t)pszValue[1], (uint8_t)pszValue[2]); + uint8_t green = Hex2Dec((uint8_t)pszValue[3], (uint8_t)pszValue[4]); + uint8_t blue = Hex2Dec((uint8_t)pszValue[5], (uint8_t)pszValue[6]); + *dwColor = ArgbEncode(255, red, green, blue); + return true; + } + default: + return false; + } + } + + if (iValueLen >= 10) { + if (pszValue[iValueLen - 1] != ')' || FXSYS_wcsnicmp(L"rgb(", pszValue, 4)) + return false; + + uint8_t rgb[3] = {0}; + FX_FLOAT fValue; + FDE_CSSPrimitiveType eType; + CFDE_CSSValueListParser list(pszValue + 4, iValueLen - 5, ','); + for (int32_t i = 0; i < 3; ++i) { + if (!list.NextValue(eType, pszValue, iValueLen)) + return false; + if (eType != FDE_CSSPrimitiveType::Number) + return false; + FDE_CSSNumberType eNumType; + if (!ParseCSSNumber(pszValue, iValueLen, fValue, eNumType)) + return false; + + rgb[i] = eNumType == FDE_CSSNumberType::Percent + ? FXSYS_round(fValue * 2.55f) + : FXSYS_round(fValue); + } + *dwColor = ArgbEncode(255, rgb[0], rgb[1], rgb[2]); + return true; + } + + const FDE_CSSCOLORTABLE* pColor = + FDE_GetCSSColorByName(CFX_WideStringC(pszValue, iValueLen)); + if (!pColor) + return false; + + *dwColor = pColor->dwValue; + return true; +} + CFDE_CSSDeclaration::CFDE_CSSDeclaration() {} CFDE_CSSDeclaration::~CFDE_CSSDeclaration() {} @@ -55,7 +169,7 @@ const FX_WCHAR* CFDE_CSSDeclaration::CopyToLocal( void CFDE_CSSDeclaration::AddPropertyHolder(FDE_CSSProperty eProperty, CFX_RetainPtr<CFDE_CSSValue> pValue, bool bImportant) { - auto pHolder = pdfium::MakeUnique<FDE_CSSPropertyHolder>(); + auto pHolder = pdfium::MakeUnique<CFDE_CSSPropertyHolder>(); pHolder->bImportant = bImportant; pHolder->eProperty = eProperty; pHolder->pValue = pValue; @@ -179,7 +293,7 @@ void CFDE_CSSDeclaration::AddProperty(const FDE_CSSPropertyArgs* pArgs, int32_t iNameLen, const FX_WCHAR* pszValue, int32_t iValueLen) { - auto pProperty = pdfium::MakeUnique<FDE_CSSCustomProperty>(); + auto pProperty = pdfium::MakeUnique<CFDE_CSSCustomProperty>(); pProperty->pwsName = CopyToLocal(pArgs, pszName, iNameLen); pProperty->pwsValue = CopyToLocal(pArgs, pszValue, iValueLen); custom_properties_.push_back(std::move(pProperty)); @@ -191,7 +305,7 @@ CFX_RetainPtr<CFDE_CSSValue> CFDE_CSSDeclaration::ParseNumber( int32_t iValueLen) { FX_FLOAT fValue; FDE_CSSNumberType eUnit; - if (!FDE_ParseCSSNumber(pszValue, iValueLen, fValue, eUnit)) + if (!ParseCSSNumber(pszValue, iValueLen, fValue, eUnit)) return nullptr; return pdfium::MakeRetain<CFDE_CSSNumberValue>(eUnit, fValue); } @@ -211,7 +325,7 @@ CFX_RetainPtr<CFDE_CSSValue> CFDE_CSSDeclaration::ParseColor( const FX_WCHAR* pszValue, int32_t iValueLen) { FX_ARGB dwColor; - if (!FDE_ParseCSSColor(pszValue, iValueLen, dwColor)) + if (!ParseCSSColor(pszValue, iValueLen, &dwColor)) return nullptr; return pdfium::MakeRetain<CFDE_CSSColorValue>(dwColor); } @@ -221,7 +335,7 @@ CFX_RetainPtr<CFDE_CSSValue> CFDE_CSSDeclaration::ParseString( const FX_WCHAR* pszValue, int32_t iValueLen) { int32_t iOffset; - if (!FDE_ParseCSSString(pszValue, iValueLen, &iOffset, &iValueLen)) + if (!ParseCSSString(pszValue, iValueLen, &iOffset, &iValueLen)) return nullptr; if (iValueLen <= 0) @@ -249,7 +363,7 @@ void CFDE_CSSDeclaration::ParseValueListProperty( if (dwType & FDE_CSSVALUETYPE_MaybeNumber) { FX_FLOAT fValue; FDE_CSSNumberType eNumType; - if (FDE_ParseCSSNumber(pszValue, iValueLen, fValue, eNumType)) + if (ParseCSSNumber(pszValue, iValueLen, fValue, eNumType)) list.push_back( pdfium::MakeRetain<CFDE_CSSNumberValue>(eNumType, fValue)); } @@ -257,7 +371,7 @@ void CFDE_CSSDeclaration::ParseValueListProperty( case FDE_CSSPrimitiveType::String: if (dwType & FDE_CSSVALUETYPE_MaybeColor) { FX_ARGB dwColor; - if (FDE_ParseCSSColor(pszValue, iValueLen, dwColor)) { + if (ParseCSSColor(pszValue, iValueLen, &dwColor)) { list.push_back(pdfium::MakeRetain<CFDE_CSSColorValue>(dwColor)); continue; } @@ -280,7 +394,7 @@ void CFDE_CSSDeclaration::ParseValueListProperty( case FDE_CSSPrimitiveType::RGB: if (dwType & FDE_CSSVALUETYPE_MaybeColor) { FX_ARGB dwColor; - if (FDE_ParseCSSColor(pszValue, iValueLen, dwColor)) { + if (ParseCSSColor(pszValue, iValueLen, &dwColor)) { list.push_back(pdfium::MakeRetain<CFDE_CSSColorValue>(dwColor)); } } @@ -372,7 +486,7 @@ bool CFDE_CSSDeclaration::ParseBorderProperty( FX_FLOAT fValue; FDE_CSSNumberType eNumType; - if (FDE_ParseCSSNumber(pszValue, iValueLen, fValue, eNumType)) + if (ParseCSSNumber(pszValue, iValueLen, fValue, eNumType)) pWidth = pdfium::MakeRetain<CFDE_CSSNumberValue>(eNumType, fValue); break; } @@ -485,7 +599,7 @@ void CFDE_CSSDeclaration::ParseFontProperty(const FDE_CSSPropertyArgs* pArgs, case FDE_CSSPrimitiveType::Number: { FX_FLOAT fValue; FDE_CSSNumberType eNumType; - if (!FDE_ParseCSSNumber(pszValue, iValueLen, fValue, eNumType)) + if (!ParseCSSNumber(pszValue, iValueLen, fValue, eNumType)) break; if (eType == FDE_CSSPrimitiveType::Number) { switch ((int32_t)fValue) { @@ -546,7 +660,3 @@ void CFDE_CSSDeclaration::ParseFontProperty(const FDE_CSSPropertyArgs* pArgs, size_t CFDE_CSSDeclaration::PropertyCountForTesting() const { return properties_.size(); } - -FDE_CSSPropertyHolder::FDE_CSSPropertyHolder() {} - -FDE_CSSPropertyHolder::~FDE_CSSPropertyHolder() {} diff --git a/xfa/fde/css/fde_cssdeclaration.h b/xfa/fde/css/cfde_cssdeclaration.h index 72657184eb..777864efea 100644 --- a/xfa/fde/css/fde_cssdeclaration.h +++ b/xfa/fde/css/cfde_cssdeclaration.h @@ -4,8 +4,8 @@ // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com -#ifndef XFA_FDE_CSS_FDE_CSSDECLARATION_H_ -#define XFA_FDE_CSS_FDE_CSSDECLARATION_H_ +#ifndef XFA_FDE_CSS_CFDE_CSSDECLARATION_H_ +#define XFA_FDE_CSS_CFDE_CSSDECLARATION_H_ #include <memory> #include <unordered_map> @@ -14,33 +14,28 @@ #include "xfa/fde/css/fde_cssdatatable.h" -class FDE_CSSPropertyHolder { - public: - FDE_CSSPropertyHolder(); - ~FDE_CSSPropertyHolder(); - - FDE_CSSProperty eProperty; - bool bImportant; - CFX_RetainPtr<CFDE_CSSValue> pValue; -}; - -class FDE_CSSCustomProperty { - public: - const FX_WCHAR* pwsName; - const FX_WCHAR* pwsValue; -}; - struct FDE_CSSPropertyArgs { std::unordered_map<uint32_t, FX_WCHAR*>* pStringCache; const FDE_CSSPropertyTable* pProperty; }; +class CFDE_CSSPropertyHolder; +class CFDE_CSSCustomProperty; + class CFDE_CSSDeclaration { public: using const_prop_iterator = - std::vector<std::unique_ptr<FDE_CSSPropertyHolder>>::const_iterator; + std::vector<std::unique_ptr<CFDE_CSSPropertyHolder>>::const_iterator; using const_custom_iterator = - std::vector<std::unique_ptr<FDE_CSSCustomProperty>>::const_iterator; + std::vector<std::unique_ptr<CFDE_CSSCustomProperty>>::const_iterator; + + static bool ParseCSSString(const FX_WCHAR* pszValue, + int32_t iValueLen, + int32_t* iOffset, + int32_t* iLength); + static bool ParseCSSColor(const FX_WCHAR* pszValue, + int32_t iValueLen, + FX_ARGB* dwColor); CFDE_CSSDeclaration(); ~CFDE_CSSDeclaration(); @@ -68,7 +63,11 @@ class CFDE_CSSDeclaration { size_t PropertyCountForTesting() const; - protected: + FX_ARGB ParseColorForTest(const FX_WCHAR* pszValue, + int32_t iValueLen, + FX_ARGB* dwColor) const; + + private: void ParseFontProperty(const FDE_CSSPropertyArgs* pArgs, const FX_WCHAR* pszValue, int32_t iValueLen, @@ -105,8 +104,8 @@ class CFDE_CSSDeclaration { CFX_RetainPtr<CFDE_CSSValue> pValue, bool bImportant); - std::vector<std::unique_ptr<FDE_CSSPropertyHolder>> properties_; - std::vector<std::unique_ptr<FDE_CSSCustomProperty>> custom_properties_; + std::vector<std::unique_ptr<CFDE_CSSPropertyHolder>> properties_; + std::vector<std::unique_ptr<CFDE_CSSCustomProperty>> custom_properties_; }; -#endif // XFA_FDE_CSS_FDE_CSSDECLARATION_H_ +#endif // XFA_FDE_CSS_CFDE_CSSDECLARATION_H_ diff --git a/xfa/fde/css/fde_cssdatatable_unittest.cpp b/xfa/fde/css/cfde_cssdeclaration_unittest.cpp index d602bff046..48a3c72c39 100644 --- a/xfa/fde/css/fde_cssdatatable_unittest.cpp +++ b/xfa/fde/css/cfde_cssdeclaration_unittest.cpp @@ -1,59 +1,60 @@ -// Copyright 2016 PDFium Authors. All rights reserved. +// Copyright 2017 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. -#include "xfa/fde/css/fde_cssdatatable.h" +#include "xfa/fde/css/cfde_cssdeclaration.h" #include "testing/gtest/include/gtest/gtest.h" -TEST(FDE_ParseCSSColor, HexEncodingParsing) { +TEST(CFDE_CSSDecalration, HexEncodingParsing) { FX_ARGB color; // Length value invalid. - EXPECT_FALSE(FDE_ParseCSSColor(L"#000", 3, color)); - EXPECT_FALSE(FDE_ParseCSSColor(L"#000000", 5, color)); - EXPECT_FALSE(FDE_ParseCSSColor(L"#000000", 8, color)); + EXPECT_FALSE(CFDE_CSSDeclaration::ParseCSSColor(L"#000", 3, &color)); + EXPECT_FALSE(CFDE_CSSDeclaration::ParseCSSColor(L"#000000", 5, &color)); + EXPECT_FALSE(CFDE_CSSDeclaration::ParseCSSColor(L"#000000", 8, &color)); // Invalid characters - EXPECT_TRUE(FDE_ParseCSSColor(L"#zxytlm", 7, color)); + EXPECT_TRUE(CFDE_CSSDeclaration::ParseCSSColor(L"#zxytlm", 7, &color)); EXPECT_EQ(0, FXARGB_R(color)); EXPECT_EQ(0, FXARGB_G(color)); EXPECT_EQ(0, FXARGB_B(color)); - EXPECT_TRUE(FDE_ParseCSSColor(L"#000", 4, color)); + EXPECT_TRUE(CFDE_CSSDeclaration::ParseCSSColor(L"#000", 4, &color)); EXPECT_EQ(0, FXARGB_R(color)); EXPECT_EQ(0, FXARGB_G(color)); EXPECT_EQ(0, FXARGB_B(color)); - EXPECT_TRUE(FDE_ParseCSSColor(L"#FFF", 4, color)); + EXPECT_TRUE(CFDE_CSSDeclaration::ParseCSSColor(L"#FFF", 4, &color)); EXPECT_EQ(255, FXARGB_R(color)); EXPECT_EQ(255, FXARGB_G(color)); EXPECT_EQ(255, FXARGB_B(color)); - EXPECT_TRUE(FDE_ParseCSSColor(L"#F0F0F0", 7, color)); + EXPECT_TRUE(CFDE_CSSDeclaration::ParseCSSColor(L"#F0F0F0", 7, &color)); EXPECT_EQ(240, FXARGB_R(color)); EXPECT_EQ(240, FXARGB_G(color)); EXPECT_EQ(240, FXARGB_B(color)); // Upper and lower case characters. - EXPECT_TRUE(FDE_ParseCSSColor(L"#1b2F3c", 7, color)); + EXPECT_TRUE(CFDE_CSSDeclaration::ParseCSSColor(L"#1b2F3c", 7, &color)); EXPECT_EQ(27, FXARGB_R(color)); EXPECT_EQ(47, FXARGB_G(color)); EXPECT_EQ(60, FXARGB_B(color)); } -TEST(FDE_ParseCSSColor, RGBEncodingParsing) { +TEST(CFDE_CSSDecalration, RGBEncodingParsing) { FX_ARGB color; // Invalid input for rgb() syntax. - EXPECT_FALSE(FDE_ParseCSSColor(L"blahblahblah", 11, color)); + EXPECT_FALSE(CFDE_CSSDeclaration::ParseCSSColor(L"blahblahblah", 11, &color)); - EXPECT_TRUE(FDE_ParseCSSColor(L"rgb(0, 0, 0)", 12, color)); + EXPECT_TRUE(CFDE_CSSDeclaration::ParseCSSColor(L"rgb(0, 0, 0)", 12, &color)); EXPECT_EQ(0, FXARGB_R(color)); EXPECT_EQ(0, FXARGB_G(color)); EXPECT_EQ(0, FXARGB_B(color)); - EXPECT_TRUE(FDE_ParseCSSColor(L"rgb(128,255,48)", 15, color)); + EXPECT_TRUE( + CFDE_CSSDeclaration::ParseCSSColor(L"rgb(128,255,48)", 15, &color)); EXPECT_EQ(128, FXARGB_R(color)); EXPECT_EQ(255, FXARGB_G(color)); EXPECT_EQ(48, FXARGB_B(color)); diff --git a/xfa/fde/css/cfde_cssfontfacerule.cpp b/xfa/fde/css/cfde_cssfontfacerule.cpp new file mode 100644 index 0000000000..5d56eb8c6c --- /dev/null +++ b/xfa/fde/css/cfde_cssfontfacerule.cpp @@ -0,0 +1,12 @@ +// Copyright 2017 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 "xfa/fde/css/cfde_cssfontfacerule.h" + +CFDE_CSSFontFaceRule::CFDE_CSSFontFaceRule() + : CFDE_CSSRule(FDE_CSSRuleType::FontFace) {} + +CFDE_CSSFontFaceRule::~CFDE_CSSFontFaceRule() {} diff --git a/xfa/fde/css/cfde_cssfontfacerule.h b/xfa/fde/css/cfde_cssfontfacerule.h new file mode 100644 index 0000000000..e5249ccf18 --- /dev/null +++ b/xfa/fde/css/cfde_cssfontfacerule.h @@ -0,0 +1,24 @@ +// Copyright 2017 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 XFA_FDE_CSS_CFDE_CSSFONTFACERULE_H_ +#define XFA_FDE_CSS_CFDE_CSSFONTFACERULE_H_ + +#include "xfa/fde/css/cfde_cssdeclaration.h" +#include "xfa/fde/css/cfde_cssrule.h" + +class CFDE_CSSFontFaceRule : public CFDE_CSSRule { + public: + CFDE_CSSFontFaceRule(); + ~CFDE_CSSFontFaceRule() override; + + CFDE_CSSDeclaration* GetDeclaration() { return &m_Declaration; } + + private: + CFDE_CSSDeclaration m_Declaration; +}; + +#endif // XFA_FDE_CSS_CFDE_CSSFONTFACERULE_H_ diff --git a/xfa/fde/css/cfde_cssmediarule.cpp b/xfa/fde/css/cfde_cssmediarule.cpp new file mode 100644 index 0000000000..751f9d9fa1 --- /dev/null +++ b/xfa/fde/css/cfde_cssmediarule.cpp @@ -0,0 +1,26 @@ +// Copyright 2017 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 "xfa/fde/css/cfde_cssmediarule.h" + +#include "third_party/base/stl_util.h" + +CFDE_CSSMediaRule::CFDE_CSSMediaRule(uint32_t dwMediaList) + : CFDE_CSSRule(FDE_CSSRuleType::Media), m_dwMediaList(dwMediaList) {} + +CFDE_CSSMediaRule::~CFDE_CSSMediaRule() {} + +uint32_t CFDE_CSSMediaRule::GetMediaList() const { + return m_dwMediaList; +} + +int32_t CFDE_CSSMediaRule::CountRules() const { + return pdfium::CollectionSize<int32_t>(m_RuleArray); +} + +CFDE_CSSRule* CFDE_CSSMediaRule::GetRule(int32_t index) { + return m_RuleArray[index].get(); +} diff --git a/xfa/fde/css/cfde_cssmediarule.h b/xfa/fde/css/cfde_cssmediarule.h new file mode 100644 index 0000000000..59febb2008 --- /dev/null +++ b/xfa/fde/css/cfde_cssmediarule.h @@ -0,0 +1,31 @@ +// Copyright 2017 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 XFA_FDE_CSS_CFDE_CSSMEDIARULE_H_ +#define XFA_FDE_CSS_CFDE_CSSMEDIARULE_H_ + +#include <memory> +#include <vector> + +#include "xfa/fde/css/cfde_cssrule.h" + +class CFDE_CSSMediaRule : public CFDE_CSSRule { + public: + explicit CFDE_CSSMediaRule(uint32_t dwMediaList); + ~CFDE_CSSMediaRule() override; + + uint32_t GetMediaList() const; + int32_t CountRules() const; + CFDE_CSSRule* GetRule(int32_t index); + + std::vector<std::unique_ptr<CFDE_CSSRule>>& GetArray() { return m_RuleArray; } + + protected: + uint32_t m_dwMediaList; + std::vector<std::unique_ptr<CFDE_CSSRule>> m_RuleArray; +}; + +#endif // XFA_FDE_CSS_CFDE_CSSMEDIARULE_H_ diff --git a/xfa/fde/css/cfde_csspropertyholder.cpp b/xfa/fde/css/cfde_csspropertyholder.cpp new file mode 100644 index 0000000000..9f47c8f08b --- /dev/null +++ b/xfa/fde/css/cfde_csspropertyholder.cpp @@ -0,0 +1,11 @@ +// Copyright 2017 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 "xfa/fde/css/cfde_csspropertyholder.h" + +CFDE_CSSPropertyHolder::CFDE_CSSPropertyHolder() {} + +CFDE_CSSPropertyHolder::~CFDE_CSSPropertyHolder() {} diff --git a/xfa/fde/css/cfde_csspropertyholder.h b/xfa/fde/css/cfde_csspropertyholder.h new file mode 100644 index 0000000000..7f8526d92e --- /dev/null +++ b/xfa/fde/css/cfde_csspropertyholder.h @@ -0,0 +1,24 @@ +// Copyright 2017 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 XFA_FDE_CSS_CFDE_CSSPROPERTYHOLDER_H_ +#define XFA_FDE_CSS_CFDE_CSSPROPERTYHOLDER_H_ + +#include "core/fxcrt/cfx_retain_ptr.h" +#include "xfa/fde/css/cfde_cssvalue.h" +#include "xfa/fde/css/fde_css.h" + +class CFDE_CSSPropertyHolder { + public: + CFDE_CSSPropertyHolder(); + ~CFDE_CSSPropertyHolder(); + + FDE_CSSProperty eProperty; + bool bImportant; + CFX_RetainPtr<CFDE_CSSValue> pValue; +}; + +#endif // XFA_FDE_CSS_CFDE_CSSPROPERTYHOLDER_H_ diff --git a/xfa/fde/css/cfde_cssrulecollection.cpp b/xfa/fde/css/cfde_cssrulecollection.cpp index 5c386effeb..286f619b68 100644 --- a/xfa/fde/css/cfde_cssrulecollection.cpp +++ b/xfa/fde/css/cfde_cssrulecollection.cpp @@ -4,16 +4,20 @@ // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com -#include "xfa/fde/css/fde_cssstyleselector.h" +#include "xfa/fde/css/cfde_cssrulecollection.h" #include <algorithm> #include <map> #include <memory> -#include "xfa/fde/css/fde_csscache.h" -#include "xfa/fde/css/fde_cssdeclaration.h" -#include "xfa/fde/css/fde_cssstylesheet.h" -#include "xfa/fde/css/fde_csssyntax.h" +#include "xfa/fde/css/cfde_cssdeclaration.h" +#include "xfa/fde/css/cfde_cssmediarule.h" +#include "xfa/fde/css/cfde_cssrule.h" +#include "xfa/fde/css/cfde_cssselector.h" +#include "xfa/fde/css/cfde_cssstylerule.h" +#include "xfa/fde/css/cfde_cssstylesheet.h" +#include "xfa/fde/css/cfde_csssyntaxparser.h" +#include "xfa/fde/css/cfde_csstagcache.h" #define FDE_CSSUNIVERSALHASH ('*') @@ -60,7 +64,7 @@ void CFDE_CSSRuleCollection::AddRulesFrom(CFDE_CSSStyleSheet* pStyleSheet, for (int32_t i = 0; i < iSelectors; ++i) { CFDE_CSSSelector* pSelector = pStyleRule->GetSelectorList(i); if (pSelector->GetType() == FDE_CSSSelectorType::Pseudo) { - FDE_CSSRuleData* pData = NewRuleData(pSelector, pDeclaration); + Data* pData = NewRuleData(pSelector, pDeclaration); AddRuleTo(&m_pPseudoRules, pData); continue; } @@ -71,7 +75,7 @@ void CFDE_CSSRuleCollection::AddRulesFrom(CFDE_CSSStyleSheet* pStyleSheet, } CFDE_CSSSelector* pNext = pSelector->GetNextSelector(); if (!pNext) { - FDE_CSSRuleData* pData = NewRuleData(pSelector, pDeclaration); + Data* pData = NewRuleData(pSelector, pDeclaration); AddRuleTo(&m_pUniversalRules, pData); continue; } @@ -109,13 +113,12 @@ void CFDE_CSSRuleCollection::AddRulesFrom(CFDE_CSSStyleSheet* pStyleSheet, } } -void CFDE_CSSRuleCollection::AddRuleTo( - std::map<uint32_t, FDE_CSSRuleData*>* pMap, - uint32_t dwKey, - CFDE_CSSSelector* pSel, - CFDE_CSSDeclaration* pDecl) { - FDE_CSSRuleData* pData = NewRuleData(pSel, pDecl); - FDE_CSSRuleData* pList = (*pMap)[dwKey]; +void CFDE_CSSRuleCollection::AddRuleTo(std::map<uint32_t, Data*>* pMap, + uint32_t dwKey, + CFDE_CSSSelector* pSel, + CFDE_CSSDeclaration* pDecl) { + Data* pData = NewRuleData(pSel, pDecl); + Data* pList = (*pMap)[dwKey]; if (!pList) { (*pMap)[dwKey] = pData; } else if (AddRuleTo(&pList, pData)) { @@ -123,8 +126,7 @@ void CFDE_CSSRuleCollection::AddRuleTo( } } -bool CFDE_CSSRuleCollection::AddRuleTo(FDE_CSSRuleData** pList, - FDE_CSSRuleData* pData) { +bool CFDE_CSSRuleCollection::AddRuleTo(Data** pList, Data* pData) { if (*pList) { pData->pNext = (*pList)->pNext; (*pList)->pNext = pData; @@ -134,8 +136,23 @@ bool CFDE_CSSRuleCollection::AddRuleTo(FDE_CSSRuleData** pList, return true; } -FDE_CSSRuleData* CFDE_CSSRuleCollection::NewRuleData( +CFDE_CSSRuleCollection::Data* CFDE_CSSRuleCollection::NewRuleData( CFDE_CSSSelector* pSel, CFDE_CSSDeclaration* pDecl) { - return new FDE_CSSRuleData(pSel, pDecl, ++m_iSelectors); + return new Data(pSel, pDecl, ++m_iSelectors); +} + +CFDE_CSSRuleCollection::Data::Data(CFDE_CSSSelector* pSel, + CFDE_CSSDeclaration* pDecl, + uint32_t dwPos) + : pSelector(pSel), pDeclaration(pDecl), dwPriority(dwPos), pNext(nullptr) { + static const uint32_t s_Specific[5] = {0x00010000, 0x00010000, 0x00100000, + 0x00100000, 0x01000000}; + for (; pSel; pSel = pSel->GetNextSelector()) { + FDE_CSSSelectorType eType = pSel->GetType(); + if (eType > FDE_CSSSelectorType::Descendant || + pSel->GetNameHash() != FDE_CSSUNIVERSALHASH) { + dwPriority += s_Specific[static_cast<int>(eType)]; + } + } } diff --git a/xfa/fde/css/cfde_cssrulecollection.h b/xfa/fde/css/cfde_cssrulecollection.h new file mode 100644 index 0000000000..1433a5a5f2 --- /dev/null +++ b/xfa/fde/css/cfde_cssrulecollection.h @@ -0,0 +1,79 @@ +// Copyright 2017 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 XFA_FDE_CSS_CFDE_CSSRULECOLLECTION_H_ +#define XFA_FDE_CSS_CFDE_CSSRULECOLLECTION_H_ + +#include <map> + +#include "core/fxcrt/fx_basic.h" + +class CFDE_CSSDeclaration; +class CFDE_CSSRule; +class CFDE_CSSSelector; +class CFDE_CSSStyleSheet; +class CFGAS_FontMgr; + +class CFDE_CSSRuleCollection { + public: + class Data { + public: + Data(CFDE_CSSSelector* pSel, CFDE_CSSDeclaration* pDecl, uint32_t dwPos); + + CFDE_CSSSelector* const pSelector; + CFDE_CSSDeclaration* const pDeclaration; + uint32_t dwPriority; + Data* pNext; + }; + + CFDE_CSSRuleCollection(); + ~CFDE_CSSRuleCollection(); + + void AddRulesFrom(const CFX_ArrayTemplate<CFDE_CSSStyleSheet*>& sheets, + uint32_t dwMediaList, + CFGAS_FontMgr* pFontMgr); + void Clear(); + int32_t CountSelectors() const { return m_iSelectors; } + + Data* GetIDRuleData(uint32_t dwIDHash) { + auto it = m_IDRules.find(dwIDHash); + return it != m_IDRules.end() ? it->second : nullptr; + } + + Data* GetTagRuleData(uint32_t dwTagHash) { + auto it = m_TagRules.find(dwTagHash); + return it != m_TagRules.end() ? it->second : nullptr; + } + + Data* GetClassRuleData(uint32_t dwIDHash) { + auto it = m_ClassRules.find(dwIDHash); + return it != m_ClassRules.end() ? it->second : nullptr; + } + + Data* GetUniversalRuleData() { return m_pUniversalRules; } + Data* GetPseudoRuleData() { return m_pPseudoRules; } + + protected: + void AddRulesFrom(CFDE_CSSStyleSheet* pStyleSheet, + CFDE_CSSRule* pRule, + uint32_t dwMediaList, + CFGAS_FontMgr* pFontMgr); + void AddRuleTo(std::map<uint32_t, Data*>* pMap, + uint32_t dwKey, + CFDE_CSSSelector* pSel, + CFDE_CSSDeclaration* pDecl); + bool AddRuleTo(Data** pList, Data* pData); + Data* NewRuleData(CFDE_CSSSelector* pSel, CFDE_CSSDeclaration* pDecl); + + std::map<uint32_t, Data*> m_IDRules; + std::map<uint32_t, Data*> m_TagRules; + std::map<uint32_t, Data*> m_ClassRules; + Data* m_pUniversalRules; + Data* m_pPseudoRules; + int32_t m_iSelectors; +}; + +#endif // XFA_FDE_CSS_CFDE_CSSRULECOLLECTION_H_ diff --git a/xfa/fde/css/cfde_cssselector.cpp b/xfa/fde/css/cfde_cssselector.cpp new file mode 100644 index 0000000000..4d32bd1cfa --- /dev/null +++ b/xfa/fde/css/cfde_cssselector.cpp @@ -0,0 +1,162 @@ +// Copyright 2017 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 "xfa/fde/css/cfde_cssselector.h" + +#include <utility> + +#include "third_party/base/ptr_util.h" + +namespace { + +bool IsCSSChar(FX_WCHAR wch) { + return (wch >= 'a' && wch <= 'z') || (wch >= 'A' && wch <= 'Z'); +} + +int32_t GetCSSPseudoLen(const FX_WCHAR* psz, const FX_WCHAR* pEnd) { + ASSERT(*psz == ':'); + const FX_WCHAR* pStart = psz; + while (psz < pEnd) { + FX_WCHAR wch = *psz; + if (IsCSSChar(wch) || wch == ':') + ++psz; + else + break; + } + return psz - pStart; +} + +int32_t GetCSSNameLen(const FX_WCHAR* psz, const FX_WCHAR* pEnd) { + const FX_WCHAR* pStart = psz; + while (psz < pEnd) { + FX_WCHAR wch = *psz; + if (IsCSSChar(wch) || (wch >= '0' && wch <= '9') || wch == '_' || + wch == '-') { + ++psz; + } else { + break; + } + } + return psz - pStart; +} + +} // namespace + +CFDE_CSSSelector::CFDE_CSSSelector(FDE_CSSSelectorType eType, + const FX_WCHAR* psz, + int32_t iLen, + bool bIgnoreCase) + : m_eType(eType), + m_dwHash(FX_HashCode_GetW(CFX_WideStringC(psz, iLen), bIgnoreCase)) {} + +CFDE_CSSSelector::~CFDE_CSSSelector() {} + +FDE_CSSSelectorType CFDE_CSSSelector::GetType() const { + return m_eType; +} + +uint32_t CFDE_CSSSelector::GetNameHash() const { + return m_dwHash; +} + +CFDE_CSSSelector* CFDE_CSSSelector::GetNextSelector() const { + return m_pNext.get(); +} + +std::unique_ptr<CFDE_CSSSelector> CFDE_CSSSelector::ReleaseNextSelector() { + return std::move(m_pNext); +} + +std::unique_ptr<CFDE_CSSSelector> CFDE_CSSSelector::FromString( + const FX_WCHAR* psz, + int32_t iLen) { + ASSERT(psz && iLen > 0); + + const FX_WCHAR* pStart = psz; + const FX_WCHAR* pEnd = psz + iLen; + for (; psz < pEnd; ++psz) { + switch (*psz) { + case '>': + case '[': + case '+': + return nullptr; + } + } + + std::unique_ptr<CFDE_CSSSelector> pFirst = nullptr; + CFDE_CSSSelector* pLast = nullptr; + std::unique_ptr<CFDE_CSSSelector> pPseudoFirst = nullptr; + CFDE_CSSSelector* pPseudoLast = nullptr; + + for (psz = pStart; psz < pEnd;) { + FX_WCHAR wch = *psz; + if (wch == '.' || wch == '#') { + if (psz == pStart || psz[-1] == ' ') { + auto p = pdfium::MakeUnique<CFDE_CSSSelector>( + FDE_CSSSelectorType::Element, L"*", 1, true); + + if (pFirst) { + pFirst->SetType(FDE_CSSSelectorType::Descendant); + p->SetNext(std::move(pFirst)); + } + pFirst = std::move(p); + pLast = pFirst.get(); + } + ASSERT(pLast); + + int32_t iNameLen = GetCSSNameLen(++psz, pEnd); + if (iNameLen == 0) + return nullptr; + + FDE_CSSSelectorType eType = + wch == '.' ? FDE_CSSSelectorType::Class : FDE_CSSSelectorType::ID; + auto p = + pdfium::MakeUnique<CFDE_CSSSelector>(eType, psz, iNameLen, false); + + p->SetNext(pLast->ReleaseNextSelector()); + pLast->SetNext(std::move(p)); + pLast = pLast->GetNextSelector(); + psz += iNameLen; + } else if (IsCSSChar(wch) || wch == '*') { + int32_t iNameLen = wch == '*' ? 1 : GetCSSNameLen(psz, pEnd); + if (iNameLen == 0) + return nullptr; + + auto p = pdfium::MakeUnique<CFDE_CSSSelector>( + FDE_CSSSelectorType::Element, psz, iNameLen, true); + if (pFirst) { + pFirst->SetType(FDE_CSSSelectorType::Descendant); + p->SetNext(std::move(pFirst)); + } + pFirst = std::move(p); + pLast = pFirst.get(); + psz += iNameLen; + } else if (wch == ':') { + int32_t iNameLen = GetCSSPseudoLen(psz, pEnd); + if (iNameLen == 0) + return nullptr; + + auto p = pdfium::MakeUnique<CFDE_CSSSelector>(FDE_CSSSelectorType::Pseudo, + psz, iNameLen, true); + CFDE_CSSSelector* ptr = p.get(); + if (pPseudoFirst) + pPseudoLast->SetNext(std::move(p)); + else + pPseudoFirst = std::move(p); + pPseudoLast = ptr; + psz += iNameLen; + } else if (wch == ' ') { + psz++; + } else { + return nullptr; + } + } + if (!pPseudoFirst) + return pFirst; + + pPseudoLast->SetNext(std::move(pFirst)); + return pPseudoFirst; +} diff --git a/xfa/fde/css/cfde_cssselector.h b/xfa/fde/css/cfde_cssselector.h new file mode 100644 index 0000000000..18a7c340e5 --- /dev/null +++ b/xfa/fde/css/cfde_cssselector.h @@ -0,0 +1,44 @@ +// Copyright 2017 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 XFA_FDE_CSS_CFDE_CSSSELECTOR_H_ +#define XFA_FDE_CSS_CFDE_CSSSELECTOR_H_ + +#include <memory> +#include <utility> + +#include "core/fxcrt/fx_string.h" +#include "xfa/fde/css/fde_css.h" + +class CFDE_CSSSelector { + public: + static std::unique_ptr<CFDE_CSSSelector> FromString(const FX_WCHAR* psz, + int32_t iLen); + + CFDE_CSSSelector(FDE_CSSSelectorType eType, + const FX_WCHAR* psz, + int32_t iLen, + bool bIgnoreCase); + ~CFDE_CSSSelector(); + + FDE_CSSSelectorType GetType() const; + uint32_t GetNameHash() const; + CFDE_CSSSelector* GetNextSelector() const; + std::unique_ptr<CFDE_CSSSelector> ReleaseNextSelector(); + + void SetNext(std::unique_ptr<CFDE_CSSSelector> pNext) { + m_pNext = std::move(pNext); + } + + private: + void SetType(FDE_CSSSelectorType eType) { m_eType = eType; } + + FDE_CSSSelectorType m_eType; + uint32_t m_dwHash; + std::unique_ptr<CFDE_CSSSelector> m_pNext; +}; + +#endif // XFA_FDE_CSS_CFDE_CSSSELECTOR_H_ diff --git a/xfa/fde/css/cfde_cssstylerule.cpp b/xfa/fde/css/cfde_cssstylerule.cpp new file mode 100644 index 0000000000..09c4a43f34 --- /dev/null +++ b/xfa/fde/css/cfde_cssstylerule.cpp @@ -0,0 +1,30 @@ +// Copyright 2017 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 "xfa/fde/css/cfde_cssstylerule.h" + +CFDE_CSSStyleRule::CFDE_CSSStyleRule() : CFDE_CSSRule(FDE_CSSRuleType::Style) {} + +CFDE_CSSStyleRule::~CFDE_CSSStyleRule() {} + +size_t CFDE_CSSStyleRule::CountSelectorLists() const { + return m_ppSelector.size(); +} + +CFDE_CSSSelector* CFDE_CSSStyleRule::GetSelectorList(int32_t index) const { + return m_ppSelector[index].get(); +} + +CFDE_CSSDeclaration* CFDE_CSSStyleRule::GetDeclaration() { + return &m_Declaration; +} + +void CFDE_CSSStyleRule::SetSelector( + std::vector<std::unique_ptr<CFDE_CSSSelector>>* list) { + ASSERT(m_ppSelector.empty()); + + m_ppSelector.swap(*list); +} diff --git a/xfa/fde/css/cfde_cssstylerule.h b/xfa/fde/css/cfde_cssstylerule.h new file mode 100644 index 0000000000..bed0a5ec12 --- /dev/null +++ b/xfa/fde/css/cfde_cssstylerule.h @@ -0,0 +1,33 @@ +// Copyright 2017 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 XFA_FDE_CSS_CFDE_CSSSTYLERULE_H_ +#define XFA_FDE_CSS_CFDE_CSSSTYLERULE_H_ + +#include <memory> +#include <vector> + +#include "xfa/fde/css/cfde_cssdeclaration.h" +#include "xfa/fde/css/cfde_cssrule.h" +#include "xfa/fde/css/cfde_cssselector.h" + +class CFDE_CSSStyleRule : public CFDE_CSSRule { + public: + CFDE_CSSStyleRule(); + ~CFDE_CSSStyleRule() override; + + size_t CountSelectorLists() const; + CFDE_CSSSelector* GetSelectorList(int32_t index) const; + CFDE_CSSDeclaration* GetDeclaration(); + + void SetSelector(std::vector<std::unique_ptr<CFDE_CSSSelector>>* list); + + private: + CFDE_CSSDeclaration m_Declaration; + std::vector<std::unique_ptr<CFDE_CSSSelector>> m_ppSelector; +}; + +#endif // XFA_FDE_CSS_CFDE_CSSSTYLERULE_H_ diff --git a/xfa/fde/css/fde_cssstyleselector.cpp b/xfa/fde/css/cfde_cssstyleselector.cpp index 7184ce3f8a..1539894017 100644 --- a/xfa/fde/css/fde_cssstyleselector.cpp +++ b/xfa/fde/css/cfde_cssstyleselector.cpp @@ -4,21 +4,22 @@ // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com -#include "xfa/fde/css/fde_cssstyleselector.h" +#include "xfa/fde/css/cfde_cssstyleselector.h" #include <algorithm> -#include <memory> #include "third_party/base/ptr_util.h" -#include "third_party/base/stl_util.h" +#include "xfa/fde/css/cfde_cssaccelerator.h" #include "xfa/fde/css/cfde_csscolorvalue.h" +#include "xfa/fde/css/cfde_csscomputedstyle.h" +#include "xfa/fde/css/cfde_csscustomproperty.h" +#include "xfa/fde/css/cfde_cssdeclaration.h" #include "xfa/fde/css/cfde_cssenumvalue.h" -#include "xfa/fde/css/cfde_cssstringvalue.h" +#include "xfa/fde/css/cfde_csspropertyholder.h" +#include "xfa/fde/css/cfde_cssselector.h" +#include "xfa/fde/css/cfde_csssyntaxparser.h" +#include "xfa/fde/css/cfde_csstagcache.h" #include "xfa/fde/css/cfde_cssvaluelist.h" -#include "xfa/fde/css/fde_csscache.h" -#include "xfa/fde/css/fde_cssdeclaration.h" -#include "xfa/fde/css/fde_cssstylesheet.h" -#include "xfa/fde/css/fde_csssyntax.h" #include "xfa/fxfa/app/cxfa_csstagprovider.h" namespace { @@ -37,21 +38,6 @@ const T* ToValue(const CFDE_CSSValue* val) { #define FDE_CSSUNIVERSALHASH ('*') -FDE_CSSRuleData::FDE_CSSRuleData(CFDE_CSSSelector* pSel, - CFDE_CSSDeclaration* pDecl, - uint32_t dwPos) - : pSelector(pSel), pDeclaration(pDecl), dwPriority(dwPos), pNext(nullptr) { - static const uint32_t s_Specific[5] = {0x00010000, 0x00010000, 0x00100000, - 0x00100000, 0x01000000}; - for (; pSel; pSel = pSel->GetNextSelector()) { - FDE_CSSSelectorType eType = pSel->GetType(); - if (eType > FDE_CSSSelectorType::Descendant || - pSel->GetNameHash() != FDE_CSSUNIVERSALHASH) { - dwPriority += s_Specific[static_cast<int>(eType)]; - } - } -} - CFDE_CSSStyleSelector::CFDE_CSSStyleSelector(CFGAS_FontMgr* pFontMgr) : m_pFontMgr(pFontMgr), m_fDefFontSize(12.0f) { m_ePriorities[static_cast<int32_t>(FDE_CSSStyleSheetPriority::High)] = @@ -136,7 +122,7 @@ int32_t CFDE_CSSStyleSelector::MatchDeclarations( CFX_ArrayTemplate<CFDE_CSSDeclaration*>& matchedDecls, FDE_CSSPseudo ePseudoType) { ASSERT(pTag); - FDE_CSSTagCache* pCache = m_pAccelerator->GetTopElement(); + CFDE_CSSTagCache* pCache = m_pAccelerator->top(); ASSERT(pCache && pCache->GetTag() == pTag); matchedDecls.RemoveAt(0, matchedDecls.GetSize()); @@ -165,7 +151,8 @@ int32_t CFDE_CSSStyleSelector::MatchDeclarations( } std::sort(m_MatchedRules.begin(), m_MatchedRules.end(), - [](const FDE_CSSRuleData* p1, const FDE_CSSRuleData* p2) { + [](const CFDE_CSSRuleCollection::Data* p1, + const CFDE_CSSRuleCollection::Data* p2) { return p1->dwPriority < p2->dwPriority; }); for (const auto& rule : m_MatchedRules) @@ -175,8 +162,8 @@ int32_t CFDE_CSSStyleSelector::MatchDeclarations( return matchedDecls.GetSize(); } -void CFDE_CSSStyleSelector::MatchRules(FDE_CSSTagCache* pCache, - FDE_CSSRuleData* pList, +void CFDE_CSSStyleSelector::MatchRules(CFDE_CSSTagCache* pCache, + CFDE_CSSRuleCollection::Data* pList, FDE_CSSPseudo ePseudoType) { while (pList) { if (MatchSelector(pCache, pList->pSelector, ePseudoType)) @@ -185,7 +172,7 @@ void CFDE_CSSStyleSelector::MatchRules(FDE_CSSTagCache* pCache, } } -bool CFDE_CSSStyleSelector::MatchSelector(FDE_CSSTagCache* pCache, +bool CFDE_CSSStyleSelector::MatchSelector(CFDE_CSSTagCache* pCache, CFDE_CSSSelector* pSel, FDE_CSSPseudo ePseudoType) { uint32_t dwHash; @@ -819,196 +806,3 @@ FDE_CSSFontVariant CFDE_CSSStyleSelector::ToFontVariant( ? FDE_CSSFontVariant::SmallCaps : FDE_CSSFontVariant::Normal; } - -CFDE_CSSComputedStyle::CFDE_CSSComputedStyle() : m_dwRefCount(1) {} - -CFDE_CSSComputedStyle::~CFDE_CSSComputedStyle() {} - -uint32_t CFDE_CSSComputedStyle::Retain() { - return ++m_dwRefCount; -} - -uint32_t CFDE_CSSComputedStyle::Release() { - uint32_t dwRefCount = --m_dwRefCount; - if (dwRefCount == 0) - delete this; - return dwRefCount; -} - -bool CFDE_CSSComputedStyle::GetCustomStyle(const CFX_WideStringC& wsName, - CFX_WideString& wsValue) const { - for (int32_t i = pdfium::CollectionSize<int32_t>(m_CustomProperties) - 2; - i > -1; i -= 2) { - if (wsName == m_CustomProperties[i]) { - wsValue = m_CustomProperties[i + 1]; - return true; - } - } - return false; -} - -int32_t CFDE_CSSComputedStyle::CountFontFamilies() const { - return m_InheritedData.m_pFontFamily - ? m_InheritedData.m_pFontFamily->CountValues() - : 0; -} - -const CFX_WideString CFDE_CSSComputedStyle::GetFontFamily(int32_t index) const { - return ToValue<CFDE_CSSStringValue>( - m_InheritedData.m_pFontFamily->GetValue(index)) - ->Value(); -} - -uint16_t CFDE_CSSComputedStyle::GetFontWeight() const { - return m_InheritedData.m_wFontWeight; -} - -FDE_CSSFontVariant CFDE_CSSComputedStyle::GetFontVariant() const { - return m_InheritedData.m_eFontVariant; -} - -FDE_CSSFontStyle CFDE_CSSComputedStyle::GetFontStyle() const { - return m_InheritedData.m_eFontStyle; -} - -FX_FLOAT CFDE_CSSComputedStyle::GetFontSize() const { - return m_InheritedData.m_fFontSize; -} - -FX_ARGB CFDE_CSSComputedStyle::GetColor() const { - return m_InheritedData.m_dwFontColor; -} - -void CFDE_CSSComputedStyle::SetFontWeight(uint16_t wFontWeight) { - m_InheritedData.m_wFontWeight = wFontWeight; -} - -void CFDE_CSSComputedStyle::SetFontVariant(FDE_CSSFontVariant eFontVariant) { - m_InheritedData.m_eFontVariant = eFontVariant; -} - -void CFDE_CSSComputedStyle::SetFontStyle(FDE_CSSFontStyle eFontStyle) { - m_InheritedData.m_eFontStyle = eFontStyle; -} - -void CFDE_CSSComputedStyle::SetFontSize(FX_FLOAT fFontSize) { - m_InheritedData.m_fFontSize = fFontSize; -} - -void CFDE_CSSComputedStyle::SetColor(FX_ARGB dwFontColor) { - m_InheritedData.m_dwFontColor = dwFontColor; -} - -const FDE_CSSRect* CFDE_CSSComputedStyle::GetBorderWidth() const { - return m_NonInheritedData.m_bHasBorder ? &(m_NonInheritedData.m_BorderWidth) - : nullptr; -} - -const FDE_CSSRect* CFDE_CSSComputedStyle::GetMarginWidth() const { - return m_NonInheritedData.m_bHasMargin ? &(m_NonInheritedData.m_MarginWidth) - : nullptr; -} - -const FDE_CSSRect* CFDE_CSSComputedStyle::GetPaddingWidth() const { - return m_NonInheritedData.m_bHasPadding ? &(m_NonInheritedData.m_PaddingWidth) - : nullptr; -} - -void CFDE_CSSComputedStyle::SetMarginWidth(const FDE_CSSRect& rect) { - m_NonInheritedData.m_MarginWidth = rect; - m_NonInheritedData.m_bHasMargin = true; -} - -void CFDE_CSSComputedStyle::SetPaddingWidth(const FDE_CSSRect& rect) { - m_NonInheritedData.m_PaddingWidth = rect; - m_NonInheritedData.m_bHasPadding = true; -} - -FDE_CSSDisplay CFDE_CSSComputedStyle::GetDisplay() const { - return m_NonInheritedData.m_eDisplay; -} - -FX_FLOAT CFDE_CSSComputedStyle::GetLineHeight() const { - return m_InheritedData.m_fLineHeight; -} - -const FDE_CSSLength& CFDE_CSSComputedStyle::GetTextIndent() const { - return m_InheritedData.m_TextIndent; -} - -FDE_CSSTextAlign CFDE_CSSComputedStyle::GetTextAlign() const { - return m_InheritedData.m_eTextAlign; -} - -FDE_CSSVerticalAlign CFDE_CSSComputedStyle::GetVerticalAlign() const { - return m_NonInheritedData.m_eVerticalAlign; -} - -FX_FLOAT CFDE_CSSComputedStyle::GetNumberVerticalAlign() const { - return m_NonInheritedData.m_fVerticalAlign; -} - -uint32_t CFDE_CSSComputedStyle::GetTextDecoration() const { - return m_NonInheritedData.m_dwTextDecoration; -} - -const FDE_CSSLength& CFDE_CSSComputedStyle::GetLetterSpacing() const { - return m_InheritedData.m_LetterSpacing; -} - -void CFDE_CSSComputedStyle::SetLineHeight(FX_FLOAT fLineHeight) { - m_InheritedData.m_fLineHeight = fLineHeight; -} - -void CFDE_CSSComputedStyle::SetTextIndent(const FDE_CSSLength& textIndent) { - m_InheritedData.m_TextIndent = textIndent; -} - -void CFDE_CSSComputedStyle::SetTextAlign(FDE_CSSTextAlign eTextAlign) { - m_InheritedData.m_eTextAlign = eTextAlign; -} - -void CFDE_CSSComputedStyle::SetNumberVerticalAlign(FX_FLOAT fAlign) { - m_NonInheritedData.m_eVerticalAlign = FDE_CSSVerticalAlign::Number, - m_NonInheritedData.m_fVerticalAlign = fAlign; -} - -void CFDE_CSSComputedStyle::SetTextDecoration(uint32_t dwTextDecoration) { - m_NonInheritedData.m_dwTextDecoration = dwTextDecoration; -} - -void CFDE_CSSComputedStyle::SetLetterSpacing( - const FDE_CSSLength& letterSpacing) { - m_InheritedData.m_LetterSpacing = letterSpacing; -} - -void CFDE_CSSComputedStyle::AddCustomStyle(const CFX_WideString& wsName, - const CFX_WideString& wsValue) { - m_CustomProperties.push_back(wsName); - m_CustomProperties.push_back(wsValue); -} - -CFDE_CSSInheritedData::CFDE_CSSInheritedData() - : m_LetterSpacing(FDE_CSSLengthUnit::Normal), - m_WordSpacing(FDE_CSSLengthUnit::Normal), - m_TextIndent(FDE_CSSLengthUnit::Point, 0), - m_pFontFamily(nullptr), - m_fFontSize(12.0f), - m_fLineHeight(14.0f), - m_dwFontColor(0xFF000000), - m_wFontWeight(400), - m_eFontVariant(FDE_CSSFontVariant::Normal), - m_eFontStyle(FDE_CSSFontStyle::Normal), - m_eTextAlign(FDE_CSSTextAlign::Left) {} - -CFDE_CSSNonInheritedData::CFDE_CSSNonInheritedData() - : m_MarginWidth(FDE_CSSLengthUnit::Point, 0), - m_BorderWidth(FDE_CSSLengthUnit::Point, 0), - m_PaddingWidth(FDE_CSSLengthUnit::Point, 0), - m_fVerticalAlign(0.0f), - m_eDisplay(FDE_CSSDisplay::Inline), - m_eVerticalAlign(FDE_CSSVerticalAlign::Baseline), - m_dwTextDecoration(0), - m_bHasMargin(false), - m_bHasBorder(false), - m_bHasPadding(false) {} diff --git a/xfa/fde/css/cfde_cssstyleselector.h b/xfa/fde/css/cfde_cssstyleselector.h new file mode 100644 index 0000000000..6a7ae99216 --- /dev/null +++ b/xfa/fde/css/cfde_cssstyleselector.h @@ -0,0 +1,95 @@ +// Copyright 2017 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 XFA_FDE_CSS_CFDE_CSSSTYLESELECTOR_H_ +#define XFA_FDE_CSS_CFDE_CSSSTYLESELECTOR_H_ + +#include <memory> +#include <vector> + +#include "core/fxcrt/fx_basic.h" +#include "core/fxcrt/fx_system.h" +#include "xfa/fde/css/cfde_cssrulecollection.h" +#include "xfa/fde/css/fde_css.h" + +class CFDE_CSSAccelerator; +class CFDE_CSSComputedStyle; +class CFDE_CSSDeclaration; +class CFDE_CSSSelector; +class CFDE_CSSStyleSheet; +class CFDE_CSSTagCache; +class CFDE_CSSValue; +class CFDE_CSSValueList; +class CFGAS_FontMgr; +class CXFA_CSSTagProvider; + +class CFDE_CSSStyleSelector { + public: + explicit CFDE_CSSStyleSelector(CFGAS_FontMgr* pFontMgr); + ~CFDE_CSSStyleSelector(); + + void SetDefFontSize(FX_FLOAT fFontSize); + + bool SetStyleSheet(FDE_CSSStyleSheetGroup eType, CFDE_CSSStyleSheet* pSheet); + bool SetStyleSheets(FDE_CSSStyleSheetGroup eType, + const CFX_ArrayTemplate<CFDE_CSSStyleSheet*>* pArray); + void SetStylePriority(FDE_CSSStyleSheetGroup eType, + FDE_CSSStyleSheetPriority ePriority); + void UpdateStyleIndex(uint32_t dwMediaList); + CFDE_CSSAccelerator* InitAccelerator(); + CFDE_CSSComputedStyle* CreateComputedStyle( + CFDE_CSSComputedStyle* pParentStyle); + int32_t MatchDeclarations( + CXFA_CSSTagProvider* pTag, + CFX_ArrayTemplate<CFDE_CSSDeclaration*>& matchedDecls, + FDE_CSSPseudo ePseudoType = FDE_CSSPseudo::NONE); + void ComputeStyle(CXFA_CSSTagProvider* pTag, + const CFDE_CSSDeclaration** ppDeclArray, + int32_t iDeclCount, + CFDE_CSSComputedStyle* pDestStyle); + + protected: + void Reset(); + void MatchRules(CFDE_CSSTagCache* pCache, + CFDE_CSSRuleCollection::Data* pList, + FDE_CSSPseudo ePseudoType); + bool MatchSelector(CFDE_CSSTagCache* pCache, + CFDE_CSSSelector* pSel, + FDE_CSSPseudo ePseudoType); + void AppendInlineStyle(CFDE_CSSDeclaration* pDecl, + const FX_WCHAR* psz, + int32_t iLen); + void ApplyDeclarations(bool bPriority, + const CFDE_CSSDeclaration** ppDeclArray, + int32_t iDeclCount, + CFDE_CSSComputedStyle* pDestStyle); + void ApplyProperty(FDE_CSSProperty eProperty, + CFDE_CSSValue* pValue, + CFDE_CSSComputedStyle* pComputedStyle); + + bool SetLengthWithPercent(FDE_CSSLength& width, + FDE_CSSPrimitiveType eType, + CFDE_CSSValue* pValue, + FX_FLOAT fFontSize); + FX_FLOAT ToFontSize(FDE_CSSPropertyValue eValue, FX_FLOAT fCurFontSize); + FDE_CSSDisplay ToDisplay(FDE_CSSPropertyValue eValue); + FDE_CSSTextAlign ToTextAlign(FDE_CSSPropertyValue eValue); + uint16_t ToFontWeight(FDE_CSSPropertyValue eValue); + FDE_CSSFontStyle ToFontStyle(FDE_CSSPropertyValue eValue); + FDE_CSSVerticalAlign ToVerticalAlign(FDE_CSSPropertyValue eValue); + uint32_t ToTextDecoration(CFDE_CSSValueList* pList); + FDE_CSSFontVariant ToFontVariant(FDE_CSSPropertyValue eValue); + + CFGAS_FontMgr* const m_pFontMgr; + FX_FLOAT m_fDefFontSize; + CFX_ArrayTemplate<CFDE_CSSStyleSheet*> m_SheetGroups[3]; + CFDE_CSSRuleCollection m_RuleCollection[3]; + FDE_CSSStyleSheetGroup m_ePriorities[3]; + std::unique_ptr<CFDE_CSSAccelerator> m_pAccelerator; + std::vector<CFDE_CSSRuleCollection::Data*> m_MatchedRules; +}; + +#endif // XFA_FDE_CSS_CFDE_CSSSTYLESELECTOR_H_ diff --git a/xfa/fde/css/fde_cssstylesheet.cpp b/xfa/fde/css/cfde_cssstylesheet.cpp index 5bbd9fd49e..4ff1b97916 100644 --- a/xfa/fde/css/fde_cssstylesheet.cpp +++ b/xfa/fde/css/cfde_cssstylesheet.cpp @@ -4,51 +4,20 @@ // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com -#include "xfa/fde/css/fde_cssstylesheet.h" +#include "xfa/fde/css/cfde_cssstylesheet.h" -#include <memory> +#include <utility> #include "third_party/base/ptr_util.h" #include "third_party/base/stl_util.h" +#include "xfa/fde/css/cfde_cssdeclaration.h" +#include "xfa/fde/css/cfde_cssfontfacerule.h" +#include "xfa/fde/css/cfde_cssmediarule.h" +#include "xfa/fde/css/cfde_cssrule.h" +#include "xfa/fde/css/cfde_cssstylerule.h" #include "xfa/fde/css/fde_cssdatatable.h" -#include "xfa/fde/css/fde_csssyntax.h" #include "xfa/fgas/crt/fgas_codepage.h" -namespace { - -bool IsCSSChar(FX_WCHAR wch) { - return (wch >= 'a' && wch <= 'z') || (wch >= 'A' && wch <= 'Z'); -} - -int32_t GetCSSPseudoLen(const FX_WCHAR* psz, const FX_WCHAR* pEnd) { - ASSERT(*psz == ':'); - const FX_WCHAR* pStart = psz; - while (psz < pEnd) { - FX_WCHAR wch = *psz; - if (IsCSSChar(wch) || wch == ':') - ++psz; - else - break; - } - return psz - pStart; -} - -int32_t GetCSSNameLen(const FX_WCHAR* psz, const FX_WCHAR* pEnd) { - const FX_WCHAR* pStart = psz; - while (psz < pEnd) { - FX_WCHAR wch = *psz; - if (IsCSSChar(wch) || (wch >= '0' && wch <= '9') || wch == '_' || - wch == '-') { - ++psz; - } else { - break; - } - } - return psz - pStart; -} - -} // namespace - CFDE_CSSStyleSheet::CFDE_CSSStyleSheet() : m_wRefCount(1), m_dwMediaList(FDE_CSSMEDIATYPE_ALL) { ASSERT(m_dwMediaList > 0); @@ -321,164 +290,3 @@ FDE_CSSSyntaxStatus CFDE_CSSStyleSheet::SkipRuleSet( } } } - -CFDE_CSSStyleRule::CFDE_CSSStyleRule() : CFDE_CSSRule(FDE_CSSRuleType::Style) {} - -CFDE_CSSStyleRule::~CFDE_CSSStyleRule() {} - -size_t CFDE_CSSStyleRule::CountSelectorLists() const { - return m_ppSelector.size(); -} - -CFDE_CSSSelector* CFDE_CSSStyleRule::GetSelectorList(int32_t index) const { - return m_ppSelector[index].get(); -} - -CFDE_CSSDeclaration* CFDE_CSSStyleRule::GetDeclaration() { - return &m_Declaration; -} - -void CFDE_CSSStyleRule::SetSelector( - std::vector<std::unique_ptr<CFDE_CSSSelector>>* list) { - ASSERT(m_ppSelector.empty()); - - m_ppSelector.swap(*list); -} - -CFDE_CSSMediaRule::CFDE_CSSMediaRule(uint32_t dwMediaList) - : CFDE_CSSRule(FDE_CSSRuleType::Media), m_dwMediaList(dwMediaList) {} - -CFDE_CSSMediaRule::~CFDE_CSSMediaRule() {} - -uint32_t CFDE_CSSMediaRule::GetMediaList() const { - return m_dwMediaList; -} - -int32_t CFDE_CSSMediaRule::CountRules() const { - return pdfium::CollectionSize<int32_t>(m_RuleArray); -} - -CFDE_CSSRule* CFDE_CSSMediaRule::GetRule(int32_t index) { - return m_RuleArray[index].get(); -} - -CFDE_CSSSelector::CFDE_CSSSelector(FDE_CSSSelectorType eType, - const FX_WCHAR* psz, - int32_t iLen, - bool bIgnoreCase) - : m_eType(eType), - m_dwHash(FX_HashCode_GetW(CFX_WideStringC(psz, iLen), bIgnoreCase)) {} - -CFDE_CSSSelector::~CFDE_CSSSelector() {} - -FDE_CSSSelectorType CFDE_CSSSelector::GetType() const { - return m_eType; -} - -uint32_t CFDE_CSSSelector::GetNameHash() const { - return m_dwHash; -} - -CFDE_CSSSelector* CFDE_CSSSelector::GetNextSelector() const { - return m_pNext.get(); -} - -std::unique_ptr<CFDE_CSSSelector> CFDE_CSSSelector::ReleaseNextSelector() { - return std::move(m_pNext); -} - -std::unique_ptr<CFDE_CSSSelector> CFDE_CSSSelector::FromString( - const FX_WCHAR* psz, - int32_t iLen) { - ASSERT(psz && iLen > 0); - - const FX_WCHAR* pStart = psz; - const FX_WCHAR* pEnd = psz + iLen; - for (; psz < pEnd; ++psz) { - switch (*psz) { - case '>': - case '[': - case '+': - return nullptr; - } - } - - std::unique_ptr<CFDE_CSSSelector> pFirst = nullptr; - CFDE_CSSSelector* pLast = nullptr; - std::unique_ptr<CFDE_CSSSelector> pPseudoFirst = nullptr; - CFDE_CSSSelector* pPseudoLast = nullptr; - - for (psz = pStart; psz < pEnd;) { - FX_WCHAR wch = *psz; - if (wch == '.' || wch == '#') { - if (psz == pStart || psz[-1] == ' ') { - auto p = pdfium::MakeUnique<CFDE_CSSSelector>( - FDE_CSSSelectorType::Element, L"*", 1, true); - - if (pFirst) { - pFirst->SetType(FDE_CSSSelectorType::Descendant); - p->SetNext(std::move(pFirst)); - } - pFirst = std::move(p); - pLast = pFirst.get(); - } - ASSERT(pLast); - - int32_t iNameLen = GetCSSNameLen(++psz, pEnd); - if (iNameLen == 0) - return nullptr; - - FDE_CSSSelectorType eType = - wch == '.' ? FDE_CSSSelectorType::Class : FDE_CSSSelectorType::ID; - auto p = - pdfium::MakeUnique<CFDE_CSSSelector>(eType, psz, iNameLen, false); - - p->SetNext(pLast->ReleaseNextSelector()); - pLast->SetNext(std::move(p)); - pLast = pLast->GetNextSelector(); - psz += iNameLen; - } else if (IsCSSChar(wch) || wch == '*') { - int32_t iNameLen = wch == '*' ? 1 : GetCSSNameLen(psz, pEnd); - if (iNameLen == 0) - return nullptr; - - auto p = pdfium::MakeUnique<CFDE_CSSSelector>( - FDE_CSSSelectorType::Element, psz, iNameLen, true); - if (pFirst) { - pFirst->SetType(FDE_CSSSelectorType::Descendant); - p->SetNext(std::move(pFirst)); - } - pFirst = std::move(p); - pLast = pFirst.get(); - psz += iNameLen; - } else if (wch == ':') { - int32_t iNameLen = GetCSSPseudoLen(psz, pEnd); - if (iNameLen == 0) - return nullptr; - - auto p = pdfium::MakeUnique<CFDE_CSSSelector>(FDE_CSSSelectorType::Pseudo, - psz, iNameLen, true); - CFDE_CSSSelector* ptr = p.get(); - if (pPseudoFirst) - pPseudoLast->SetNext(std::move(p)); - else - pPseudoFirst = std::move(p); - pPseudoLast = ptr; - psz += iNameLen; - } else if (wch == ' ') { - psz++; - } else { - return nullptr; - } - } - if (!pPseudoFirst) - return pFirst; - - pPseudoLast->SetNext(std::move(pFirst)); - return pPseudoFirst; -} - -CFDE_CSSFontFaceRule::CFDE_CSSFontFaceRule() - : CFDE_CSSRule(FDE_CSSRuleType::FontFace) {} - -CFDE_CSSFontFaceRule::~CFDE_CSSFontFaceRule() {} diff --git a/xfa/fde/css/cfde_cssstylesheet.h b/xfa/fde/css/cfde_cssstylesheet.h new file mode 100644 index 0000000000..2268efa2e4 --- /dev/null +++ b/xfa/fde/css/cfde_cssstylesheet.h @@ -0,0 +1,55 @@ +// Copyright 2017 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 XFA_FDE_CSS_CFDE_CSSSTYLESHEET_H_ +#define XFA_FDE_CSS_CFDE_CSSSTYLESHEET_H_ + +#include <memory> +#include <unordered_map> +#include <vector> + +#include "core/fxcrt/fx_basic.h" +#include "core/fxcrt/fx_string.h" +#include "xfa/fde/css/cfde_csssyntaxparser.h" + +class CFDE_CSSRule; + +class CFDE_CSSStyleSheet : public IFX_Retainable { + public: + CFDE_CSSStyleSheet(); + ~CFDE_CSSStyleSheet() override; + + // IFX_Retainable: + uint32_t Retain() override; + uint32_t Release() override; + + bool LoadFromBuffer(const FX_WCHAR* pBuffer, int32_t iBufSize); + + uint32_t GetMediaList() const; + int32_t CountRules() const; + CFDE_CSSRule* GetRule(int32_t index); + + private: + void Reset(); + bool LoadFromSyntax(CFDE_CSSSyntaxParser* pSyntax); + FDE_CSSSyntaxStatus LoadStyleRule( + CFDE_CSSSyntaxParser* pSyntax, + std::vector<std::unique_ptr<CFDE_CSSRule>>* ruleArray); + FDE_CSSSyntaxStatus LoadImportRule(CFDE_CSSSyntaxParser* pSyntax); + FDE_CSSSyntaxStatus LoadPageRule(CFDE_CSSSyntaxParser* pSyntax); + FDE_CSSSyntaxStatus LoadMediaRule(CFDE_CSSSyntaxParser* pSyntax); + FDE_CSSSyntaxStatus LoadFontFaceRule( + CFDE_CSSSyntaxParser* pSyntax, + std::vector<std::unique_ptr<CFDE_CSSRule>>* ruleArray); + FDE_CSSSyntaxStatus SkipRuleSet(CFDE_CSSSyntaxParser* pSyntax); + + uint16_t m_wRefCount; + uint32_t m_dwMediaList; + std::vector<std::unique_ptr<CFDE_CSSRule>> m_RuleArray; + std::unordered_map<uint32_t, FX_WCHAR*> m_StringCache; +}; + +#endif // XFA_FDE_CSS_CFDE_CSSSTYLESHEET_H_ diff --git a/xfa/fde/css/cfde_cssstylesheet_unittest.cpp b/xfa/fde/css/cfde_cssstylesheet_unittest.cpp index e65e36d252..080b45aa1b 100644 --- a/xfa/fde/css/cfde_cssstylesheet_unittest.cpp +++ b/xfa/fde/css/cfde_cssstylesheet_unittest.cpp @@ -4,7 +4,7 @@ // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com -#include "xfa/fde/css/fde_cssstylesheet.h" +#include "xfa/fde/css/cfde_cssstylesheet.h" #include <memory> #include <vector> @@ -12,8 +12,11 @@ #include "testing/gtest/include/gtest/gtest.h" #include "third_party/base/ptr_util.h" #include "third_party/base/stl_util.h" +#include "xfa/fde/css/cfde_cssdeclaration.h" #include "xfa/fde/css/cfde_cssenumvalue.h" #include "xfa/fde/css/cfde_cssnumbervalue.h" +#include "xfa/fde/css/cfde_cssrule.h" +#include "xfa/fde/css/cfde_cssstylerule.h" #include "xfa/fde/css/cfde_cssvaluelist.h" class CFDE_CSSStyleSheetTest : public testing::Test { diff --git a/xfa/fde/css/fde_csssyntax.cpp b/xfa/fde/css/cfde_csssyntaxparser.cpp index ef3f8d89a2..a24b4eee66 100644 --- a/xfa/fde/css/fde_csssyntax.cpp +++ b/xfa/fde/css/cfde_csssyntaxparser.cpp @@ -4,20 +4,35 @@ // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com -#include "xfa/fde/css/fde_csssyntax.h" +#include "xfa/fde/css/cfde_csssyntaxparser.h" #include <algorithm> +#include "xfa/fde/css/cfde_cssdeclaration.h" #include "xfa/fde/css/fde_cssdatatable.h" #include "xfa/fgas/crt/fgas_codepage.h" namespace { -bool FDE_IsSelectorStart(FX_WCHAR wch) { +bool IsSelectorStart(FX_WCHAR wch) { return wch == '.' || wch == '#' || wch == '*' || (wch >= 'a' && wch <= 'z') || (wch >= 'A' && wch <= 'Z'); } +bool ParseCSSURI(const FX_WCHAR* pszValue, int32_t* iOffset, int32_t* iLength) { + ASSERT(pszValue && *iLength > 0); + if (*iLength < 6 || pszValue[*iLength - 1] != ')' || + FXSYS_wcsnicmp(L"url(", pszValue, 4)) { + return false; + } + if (CFDE_CSSDeclaration::ParseCSSString(pszValue + 4, *iLength - 5, iOffset, + iLength)) { + *iOffset += 4; + return true; + } + return false; +} + } // namespace CFDE_CSSSyntaxParser::CFDE_CSSSyntaxParser() @@ -90,7 +105,7 @@ FDE_CSSSyntaxStatus CFDE_CSSSyntaxParser::DoSyntaxParse() { default: if (wch <= ' ') { m_TextPlane.MoveNext(); - } else if (FDE_IsSelectorStart(wch)) { + } else if (IsSelectorStart(wch)) { SwitchMode(FDE_CSSSyntaxMode::Selector); return FDE_CSSSyntaxStatus::StyleRule; } else { @@ -248,8 +263,8 @@ FDE_CSSSyntaxStatus CFDE_CSSSyntaxParser::DoSyntaxParse() { if (wch <= ' ' || wch == ';') { int32_t iURIStart, iURILength = m_TextData.GetLength(); - if (iURILength > 0 && FDE_ParseCSSURI(m_TextData.GetBuffer(), - &iURIStart, &iURILength)) { + if (iURILength > 0 && + ParseCSSURI(m_TextData.GetBuffer(), &iURIStart, &iURILength)) { m_TextData.Subtract(iURIStart, iURILength); SwitchMode(FDE_CSSSyntaxMode::MediaType); if (IsImportEnabled()) @@ -363,80 +378,3 @@ const FX_WCHAR* CFDE_CSSSyntaxParser::GetCurrentString(int32_t& iLength) const { iLength = m_iTextDatLen; return m_TextData.GetBuffer(); } - -CFDE_CSSTextBuf::CFDE_CSSTextBuf() - : m_bExtBuf(false), - m_pBuffer(nullptr), - m_iBufLen(0), - m_iDatLen(0), - m_iDatPos(0) {} - -CFDE_CSSTextBuf::~CFDE_CSSTextBuf() { - Reset(); -} - -void CFDE_CSSTextBuf::Reset() { - if (!m_bExtBuf) { - FX_Free(m_pBuffer); - m_pBuffer = nullptr; - } - m_iDatPos = m_iDatLen = m_iBufLen; -} - -bool CFDE_CSSTextBuf::AttachBuffer(const FX_WCHAR* pBuffer, int32_t iBufLen) { - Reset(); - m_pBuffer = const_cast<FX_WCHAR*>(pBuffer); - m_iDatLen = m_iBufLen = iBufLen; - return m_bExtBuf = true; -} - -bool CFDE_CSSTextBuf::EstimateSize(int32_t iAllocSize) { - ASSERT(iAllocSize > 0); - Clear(); - m_bExtBuf = false; - return ExpandBuf(iAllocSize); -} - -int32_t CFDE_CSSTextBuf::LoadFromStream( - const CFX_RetainPtr<IFGAS_Stream>& pTxtStream, - int32_t iStreamOffset, - int32_t iMaxChars, - bool& bEOS) { - ASSERT(iStreamOffset >= 0 && iMaxChars > 0); - Clear(); - m_bExtBuf = false; - if (!ExpandBuf(iMaxChars)) - return 0; - - if (pTxtStream->GetPosition() != iStreamOffset) - pTxtStream->Seek(FX_STREAMSEEK_Begin, iStreamOffset); - - m_iDatLen = pTxtStream->ReadString(m_pBuffer, iMaxChars, bEOS); - return m_iDatLen; -} - -bool CFDE_CSSTextBuf::ExpandBuf(int32_t iDesiredSize) { - if (m_bExtBuf) - return false; - if (!m_pBuffer) - m_pBuffer = FX_Alloc(FX_WCHAR, iDesiredSize); - else if (m_iBufLen != iDesiredSize) - m_pBuffer = FX_Realloc(FX_WCHAR, m_pBuffer, iDesiredSize); - else - return true; - - if (!m_pBuffer) { - m_iBufLen = 0; - return false; - } - m_iBufLen = iDesiredSize; - return true; -} - -void CFDE_CSSTextBuf::Subtract(int32_t iStart, int32_t iLength) { - ASSERT(iStart >= 0 && iLength >= 0); - - iLength = std::max(std::min(iLength, m_iDatLen - iStart), 0); - FXSYS_memmove(m_pBuffer, m_pBuffer + iStart, iLength * sizeof(FX_WCHAR)); - m_iDatLen = iLength; -} diff --git a/xfa/fde/css/cfde_csssyntaxparser.h b/xfa/fde/css/cfde_csssyntaxparser.h new file mode 100644 index 0000000000..131fe4bf21 --- /dev/null +++ b/xfa/fde/css/cfde_csssyntaxparser.h @@ -0,0 +1,88 @@ +// Copyright 2017 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 XFA_FDE_CSS_CFDE_CSSSYNTAXPARSER_H_ +#define XFA_FDE_CSS_CFDE_CSSSYNTAXPARSER_H_ + +#include <stack> + +#include "xfa/fde/css/cfde_csstextbuf.h" + +#define FDE_CSSSYNTAXCHECK_AllowCharset 1 +#define FDE_CSSSYNTAXCHECK_AllowImport 2 + +enum class FDE_CSSSyntaxMode { + RuleSet, + Comment, + AtRule, + UnknownRule, + Charset, + Import, + MediaRule, + URI, + MediaType, + Selector, + PropertyName, + PropertyValue, +}; + +enum class FDE_CSSSyntaxStatus : uint8_t { + Error, + EOS, + None, + Charset, + ImportRule, + ImportClose, + PageRule, + StyleRule, + FontFaceRule, + MediaRule, + MediaType, + URI, + Selector, + DeclOpen, + DeclClose, + PropertyName, + PropertyValue, +}; + +class CFDE_CSSSyntaxParser { + public: + CFDE_CSSSyntaxParser(); + ~CFDE_CSSSyntaxParser(); + + bool Init(const FX_WCHAR* pBuffer, + int32_t iBufferSize, + int32_t iTextDatSize = 32, + bool bOnlyDeclaration = false); + FDE_CSSSyntaxStatus DoSyntaxParse(); + const FX_WCHAR* GetCurrentString(int32_t& iLength) const; + + protected: + void Reset(bool bOnlyDeclaration); + void SwitchMode(FDE_CSSSyntaxMode eMode); + int32_t SwitchToComment(); + + bool RestoreMode(); + bool AppendChar(FX_WCHAR wch); + int32_t SaveTextData(); + bool IsCharsetEnabled() const { + return (m_dwCheck & FDE_CSSSYNTAXCHECK_AllowCharset) != 0; + } + void DisableCharset() { m_dwCheck = FDE_CSSSYNTAXCHECK_AllowImport; } + bool IsImportEnabled() const; + void DisableImport() { m_dwCheck = 0; } + + CFDE_CSSTextBuf m_TextData; + CFDE_CSSTextBuf m_TextPlane; + int32_t m_iTextDatLen; + uint32_t m_dwCheck; + FDE_CSSSyntaxMode m_eMode; + FDE_CSSSyntaxStatus m_eStatus; + std::stack<FDE_CSSSyntaxMode> m_ModeStack; +}; + +#endif // XFA_FDE_CSS_CFDE_CSSSYNTAXPARSER_H_ diff --git a/xfa/fde/css/cfde_csstagcache.cpp b/xfa/fde/css/cfde_csstagcache.cpp new file mode 100644 index 0000000000..13e37982df --- /dev/null +++ b/xfa/fde/css/cfde_csstagcache.cpp @@ -0,0 +1,44 @@ +// Copyright 2017 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 "xfa/fde/css/cfde_csstagcache.h" + +#include <algorithm> + +#include "core/fxcrt/fx_ext.h" +#include "xfa/fxfa/app/cxfa_csstagprovider.h" + +CFDE_CSSTagCache::CFDE_CSSTagCache(CFDE_CSSTagCache* parent, + CXFA_CSSTagProvider* tag) + : pTag(tag), pParent(parent), dwIDHash(0), dwTagHash(0), iClassIndex(0) { + static const uint32_t s_dwIDHash = FX_HashCode_GetW(L"id", true); + static const uint32_t s_dwClassHash = FX_HashCode_GetW(L"class", true); + dwTagHash = FX_HashCode_GetW(pTag->GetTagName().AsStringC(), true); + + for (auto it : *pTag) { + CFX_WideString wsValue = it.first; + CFX_WideString wsName = it.second; + uint32_t dwNameHash = FX_HashCode_GetW(wsName.AsStringC(), true); + if (dwNameHash == s_dwClassHash) { + uint32_t dwHash = FX_HashCode_GetW(wsValue.AsStringC(), false); + dwClassHashes.push_back(dwHash); + } else if (dwNameHash == s_dwIDHash) { + dwIDHash = FX_HashCode_GetW(wsValue.AsStringC(), false); + } + } +} + +CFDE_CSSTagCache::CFDE_CSSTagCache(const CFDE_CSSTagCache& it) + : pTag(it.pTag), + pParent(it.pParent), + dwIDHash(it.dwIDHash), + dwTagHash(it.dwTagHash), + iClassIndex(0) { + std::copy(it.dwClassHashes.begin(), it.dwClassHashes.end(), + dwClassHashes.begin()); +} + +CFDE_CSSTagCache::~CFDE_CSSTagCache() {} diff --git a/xfa/fde/css/cfde_csstagcache.h b/xfa/fde/css/cfde_csstagcache.h new file mode 100644 index 0000000000..6d9b4e7054 --- /dev/null +++ b/xfa/fde/css/cfde_csstagcache.h @@ -0,0 +1,46 @@ +// Copyright 2017 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 XFA_FDE_CSS_CFDE_CSSTAGCACHE_H_ +#define XFA_FDE_CSS_CFDE_CSSTAGCACHE_H_ + +#include <vector> + +#include "core/fxcrt/fx_system.h" +#include "third_party/base/stl_util.h" + +class CXFA_CSSTagProvider; + +class CFDE_CSSTagCache { + public: + CFDE_CSSTagCache(CFDE_CSSTagCache* parent, CXFA_CSSTagProvider* tag); + CFDE_CSSTagCache(const CFDE_CSSTagCache& it); + ~CFDE_CSSTagCache(); + + CFDE_CSSTagCache* GetParent() const { return pParent; } + CXFA_CSSTagProvider* GetTag() const { return pTag; } + uint32_t HashID() const { return dwIDHash; } + uint32_t HashTag() const { return dwTagHash; } + int32_t CountHashClass() const { + return pdfium::CollectionSize<int32_t>(dwClassHashes); + } + void SetClassIndex(int32_t index) { iClassIndex = index; } + uint32_t HashClass() const { + return iClassIndex < pdfium::CollectionSize<int32_t>(dwClassHashes) + ? dwClassHashes[iClassIndex] + : 0; + } + + private: + CXFA_CSSTagProvider* pTag; + CFDE_CSSTagCache* pParent; + uint32_t dwIDHash; + uint32_t dwTagHash; + int32_t iClassIndex; + std::vector<uint32_t> dwClassHashes; +}; + +#endif // XFA_FDE_CSS_CFDE_CSSTAGCACHE_H_ diff --git a/xfa/fde/css/cfde_csstextbuf.cpp b/xfa/fde/css/cfde_csstextbuf.cpp new file mode 100644 index 0000000000..83b2899f19 --- /dev/null +++ b/xfa/fde/css/cfde_csstextbuf.cpp @@ -0,0 +1,86 @@ +// Copyright 2017 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 "xfa/fde/css/cfde_csstextbuf.h" + +#include <algorithm> + +CFDE_CSSTextBuf::CFDE_CSSTextBuf() + : m_bExtBuf(false), + m_pBuffer(nullptr), + m_iBufLen(0), + m_iDatLen(0), + m_iDatPos(0) {} + +CFDE_CSSTextBuf::~CFDE_CSSTextBuf() { + Reset(); +} + +void CFDE_CSSTextBuf::Reset() { + if (!m_bExtBuf) { + FX_Free(m_pBuffer); + m_pBuffer = nullptr; + } + m_iDatPos = m_iDatLen = m_iBufLen; +} + +bool CFDE_CSSTextBuf::AttachBuffer(const FX_WCHAR* pBuffer, int32_t iBufLen) { + Reset(); + m_pBuffer = const_cast<FX_WCHAR*>(pBuffer); + m_iDatLen = m_iBufLen = iBufLen; + return m_bExtBuf = true; +} + +bool CFDE_CSSTextBuf::EstimateSize(int32_t iAllocSize) { + ASSERT(iAllocSize > 0); + Clear(); + m_bExtBuf = false; + return ExpandBuf(iAllocSize); +} + +int32_t CFDE_CSSTextBuf::LoadFromStream( + const CFX_RetainPtr<IFGAS_Stream>& pTxtStream, + int32_t iStreamOffset, + int32_t iMaxChars, + bool& bEOS) { + ASSERT(iStreamOffset >= 0 && iMaxChars > 0); + Clear(); + m_bExtBuf = false; + if (!ExpandBuf(iMaxChars)) + return 0; + + if (pTxtStream->GetPosition() != iStreamOffset) + pTxtStream->Seek(FX_STREAMSEEK_Begin, iStreamOffset); + + m_iDatLen = pTxtStream->ReadString(m_pBuffer, iMaxChars, bEOS); + return m_iDatLen; +} + +bool CFDE_CSSTextBuf::ExpandBuf(int32_t iDesiredSize) { + if (m_bExtBuf) + return false; + if (!m_pBuffer) + m_pBuffer = FX_Alloc(FX_WCHAR, iDesiredSize); + else if (m_iBufLen != iDesiredSize) + m_pBuffer = FX_Realloc(FX_WCHAR, m_pBuffer, iDesiredSize); + else + return true; + + if (!m_pBuffer) { + m_iBufLen = 0; + return false; + } + m_iBufLen = iDesiredSize; + return true; +} + +void CFDE_CSSTextBuf::Subtract(int32_t iStart, int32_t iLength) { + ASSERT(iStart >= 0 && iLength >= 0); + + iLength = std::max(std::min(iLength, m_iDatLen - iStart), 0); + FXSYS_memmove(m_pBuffer, m_pBuffer + iStart, iLength * sizeof(FX_WCHAR)); + m_iDatLen = iLength; +} diff --git a/xfa/fde/css/fde_csssyntax.h b/xfa/fde/css/cfde_csstextbuf.h index 81475cb58a..df151e02d8 100644 --- a/xfa/fde/css/fde_csssyntax.h +++ b/xfa/fde/css/cfde_csstextbuf.h @@ -1,16 +1,15 @@ -// Copyright 2014 PDFium Authors. All rights reserved. +// Copyright 2017 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 XFA_FDE_CSS_FDE_CSSSYNTAX_H_ -#define XFA_FDE_CSS_FDE_CSSSYNTAX_H_ - -#include <stack> +#ifndef XFA_FDE_CSS_CFDE_CSSTEXTBUF_H_ +#define XFA_FDE_CSS_CFDE_CSSTEXTBUF_H_ #include "core/fxcrt/cfx_retain_ptr.h" -#include "xfa/fde/css/fde_css.h" +#include "core/fxcrt/fx_memory.h" +#include "core/fxcrt/fx_system.h" #include "xfa/fgas/crt/fgas_stream.h" class CFDE_CSSTextBuf { @@ -35,9 +34,8 @@ class CFDE_CSSTextBuf { void Reset(); int32_t TrimEnd() { - while (m_iDatLen > 0 && m_pBuffer[m_iDatLen - 1] <= ' ') { + while (m_iDatLen > 0 && m_pBuffer[m_iDatLen - 1] <= ' ') --m_iDatLen; - } AppendChar(0); return --m_iDatLen; } @@ -65,58 +63,4 @@ class CFDE_CSSTextBuf { int32_t m_iDatPos; }; -#define FDE_CSSSYNTAXCHECK_AllowCharset 1 -#define FDE_CSSSYNTAXCHECK_AllowImport 2 - -enum class FDE_CSSSyntaxMode { - RuleSet, - Comment, - AtRule, - UnknownRule, - Charset, - Import, - MediaRule, - URI, - MediaType, - Selector, - PropertyName, - PropertyValue, -}; - -class CFDE_CSSSyntaxParser { - public: - CFDE_CSSSyntaxParser(); - ~CFDE_CSSSyntaxParser(); - - bool Init(const FX_WCHAR* pBuffer, - int32_t iBufferSize, - int32_t iTextDatSize = 32, - bool bOnlyDeclaration = false); - FDE_CSSSyntaxStatus DoSyntaxParse(); - const FX_WCHAR* GetCurrentString(int32_t& iLength) const; - - protected: - void Reset(bool bOnlyDeclaration); - void SwitchMode(FDE_CSSSyntaxMode eMode); - int32_t SwitchToComment(); - - bool RestoreMode(); - bool AppendChar(FX_WCHAR wch); - int32_t SaveTextData(); - bool IsCharsetEnabled() const { - return (m_dwCheck & FDE_CSSSYNTAXCHECK_AllowCharset) != 0; - } - void DisableCharset() { m_dwCheck = FDE_CSSSYNTAXCHECK_AllowImport; } - bool IsImportEnabled() const; - void DisableImport() { m_dwCheck = 0; } - - CFDE_CSSTextBuf m_TextData; - CFDE_CSSTextBuf m_TextPlane; - int32_t m_iTextDatLen; - uint32_t m_dwCheck; - FDE_CSSSyntaxMode m_eMode; - FDE_CSSSyntaxStatus m_eStatus; - std::stack<FDE_CSSSyntaxMode> m_ModeStack; -}; - -#endif // XFA_FDE_CSS_FDE_CSSSYNTAX_H_ +#endif // XFA_FDE_CSS_CFDE_CSSTEXTBUF_H_ diff --git a/xfa/fde/css/fde_css.h b/xfa/fde/css/fde_css.h index f9bd7673ad..2194657670 100644 --- a/xfa/fde/css/fde_css.h +++ b/xfa/fde/css/fde_css.h @@ -149,26 +149,6 @@ enum class FDE_CSSSelectorType : uint8_t { enum class FDE_CSSRuleType : uint8_t { Style, Media, FontFace }; -enum class FDE_CSSSyntaxStatus : uint8_t { - Error, - EOS, - None, - Charset, - ImportRule, - ImportClose, - PageRule, - StyleRule, - FontFaceRule, - MediaRule, - MediaType, - URI, - Selector, - DeclOpen, - DeclClose, - PropertyName, - PropertyValue, -}; - enum class FDE_CSSLengthUnit : uint8_t { Auto, None, diff --git a/xfa/fde/css/fde_csscache.cpp b/xfa/fde/css/fde_csscache.cpp deleted file mode 100644 index bf4176b787..0000000000 --- a/xfa/fde/css/fde_csscache.cpp +++ /dev/null @@ -1,66 +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 "xfa/fde/css/fde_csscache.h" - -#include <algorithm> - -#include "core/fxcrt/fx_ext.h" -#include "xfa/fxfa/app/cxfa_csstagprovider.h" - -FDE_CSSTagCache::FDE_CSSTagCache(FDE_CSSTagCache* parent, - CXFA_CSSTagProvider* tag) - : pTag(tag), - pParent(parent), - dwIDHash(0), - dwTagHash(0), - iClassIndex(0), - dwClassHashs(1) { - static const uint32_t s_dwIDHash = FX_HashCode_GetW(L"id", true); - static const uint32_t s_dwClassHash = FX_HashCode_GetW(L"class", true); - dwTagHash = FX_HashCode_GetW(pTag->GetTagName().AsStringC(), true); - - for (auto it : *pTag) { - CFX_WideString wsValue = it.first; - CFX_WideString wsName = it.second; - uint32_t dwNameHash = FX_HashCode_GetW(wsName.AsStringC(), true); - if (dwNameHash == s_dwClassHash) { - uint32_t dwHash = FX_HashCode_GetW(wsValue.AsStringC(), false); - dwClassHashs.Add(dwHash); - } else if (dwNameHash == s_dwIDHash) { - dwIDHash = FX_HashCode_GetW(wsValue.AsStringC(), false); - } - } -} - -FDE_CSSTagCache::FDE_CSSTagCache(const FDE_CSSTagCache& it) - : pTag(it.pTag), - pParent(it.pParent), - dwIDHash(it.dwIDHash), - dwTagHash(it.dwTagHash), - iClassIndex(0), - dwClassHashs(1) { - if (it.dwClassHashs.GetSize() > 0) - dwClassHashs.Copy(it.dwClassHashs, 0, -1); -} - -FDE_CSSTagCache::~FDE_CSSTagCache() {} - -CFDE_CSSAccelerator::CFDE_CSSAccelerator() : m_Stack(100) {} - -CFDE_CSSAccelerator::~CFDE_CSSAccelerator() {} - -void CFDE_CSSAccelerator::OnEnterTag(CXFA_CSSTagProvider* pTag) { - FDE_CSSTagCache* pTop = GetTopElement(); - FDE_CSSTagCache item(pTop, pTag); - m_Stack.Push(item); -} - -void CFDE_CSSAccelerator::OnLeaveTag(CXFA_CSSTagProvider* pTag) { - ASSERT(m_Stack.GetTopElement()); - ASSERT(m_Stack.GetTopElement()->GetTag() == pTag); - m_Stack.Pop(); -} diff --git a/xfa/fde/css/fde_csscache.h b/xfa/fde/css/fde_csscache.h deleted file mode 100644 index fd46880c1f..0000000000 --- a/xfa/fde/css/fde_csscache.h +++ /dev/null @@ -1,59 +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 XFA_FDE_CSS_FDE_CSSCACHE_H_ -#define XFA_FDE_CSS_FDE_CSSCACHE_H_ - -#include <map> - -#include "xfa/fde/css/fde_css.h" - -class CXFA_CSSTagProvider; - -class FDE_CSSTagCache { - public: - FDE_CSSTagCache(FDE_CSSTagCache* parent, CXFA_CSSTagProvider* tag); - FDE_CSSTagCache(const FDE_CSSTagCache& it); - ~FDE_CSSTagCache(); - - FDE_CSSTagCache* GetParent() const { return pParent; } - CXFA_CSSTagProvider* GetTag() const { return pTag; } - uint32_t HashID() const { return dwIDHash; } - uint32_t HashTag() const { return dwTagHash; } - int32_t CountHashClass() const { return dwClassHashs.GetSize(); } - void SetClassIndex(int32_t index) { iClassIndex = index; } - uint32_t HashClass() const { - return iClassIndex < dwClassHashs.GetSize() - ? dwClassHashs.GetAt(iClassIndex) - : 0; - } - - private: - CXFA_CSSTagProvider* pTag; - FDE_CSSTagCache* pParent; - uint32_t dwIDHash; - uint32_t dwTagHash; - int32_t iClassIndex; - CFX_BaseArrayTemplate<uint32_t> dwClassHashs; -}; - -class CFDE_CSSAccelerator { - public: - CFDE_CSSAccelerator(); - ~CFDE_CSSAccelerator(); - - void OnEnterTag(CXFA_CSSTagProvider* pTag); - void OnLeaveTag(CXFA_CSSTagProvider* pTag); - - void Clear() { m_Stack.RemoveAll(false); } - - FDE_CSSTagCache* GetTopElement() const { return m_Stack.GetTopElement(); } - - private: - CFX_ObjectStackTemplate<FDE_CSSTagCache> m_Stack; -}; - -#endif // XFA_FDE_CSS_FDE_CSSCACHE_H_ diff --git a/xfa/fde/css/fde_cssdatatable.cpp b/xfa/fde/css/fde_cssdatatable.cpp index d645e2e508..fd4ef6bffb 100644 --- a/xfa/fde/css/fde_cssdatatable.cpp +++ b/xfa/fde/css/fde_cssdatatable.cpp @@ -9,104 +9,10 @@ #include <utility> #include "core/fxcrt/fx_ext.h" +#include "xfa/fde/css/cfde_cssstyleselector.h" #include "xfa/fde/css/cfde_cssvaluelistparser.h" -#include "xfa/fde/css/fde_cssstyleselector.h" #include "xfa/fgas/crt/fgas_codepage.h" -namespace { - -uint8_t Hex2Dec(uint8_t hexHigh, uint8_t hexLow) { - return (FXSYS_toHexDigit(hexHigh) << 4) + FXSYS_toHexDigit(hexLow); -} - -} // namespace - -bool FDE_CSSLengthToFloat(const FDE_CSSLength& len, - FX_FLOAT fPercentBase, - FX_FLOAT& fResult) { - switch (len.GetUnit()) { - case FDE_CSSLengthUnit::Point: - fResult = len.GetValue(); - return true; - case FDE_CSSLengthUnit::Percent: - fResult = len.GetValue() * fPercentBase; - return true; - default: - return false; - } -} -CFX_FloatRect FDE_CSSBoundaryToRect(CFDE_CSSComputedStyle* pBoundStyle, - FX_FLOAT fContainerWidth, - bool bPadding, - bool bBorder, - bool bMargin) { - FX_FLOAT fResult; - const FDE_CSSRect* pRect; - CFX_FloatRect rect(0, 0, 0, 0); - if (bPadding) { - pRect = pBoundStyle->GetPaddingWidth(); - if (pRect) { - if (FDE_CSSLengthToFloat(pRect->left, fContainerWidth, fResult)) { - rect.left += fResult; - } - if (FDE_CSSLengthToFloat(pRect->top, fContainerWidth, fResult)) { - rect.top += fResult; - } - if (FDE_CSSLengthToFloat(pRect->right, fContainerWidth, fResult)) { - rect.right += fResult; - } - if (FDE_CSSLengthToFloat(pRect->bottom, fContainerWidth, fResult)) { - rect.bottom += fResult; - } - } - } - if (bBorder) { - pRect = pBoundStyle->GetBorderWidth(); - if (pRect) { - if (FDE_CSSLengthToFloat(pRect->left, fContainerWidth, fResult)) { - rect.left += fResult; - } - if (FDE_CSSLengthToFloat(pRect->top, fContainerWidth, fResult)) { - rect.top += fResult; - } - if (FDE_CSSLengthToFloat(pRect->right, fContainerWidth, fResult)) { - rect.right += fResult; - } - if (FDE_CSSLengthToFloat(pRect->bottom, fContainerWidth, fResult)) { - rect.bottom += fResult; - } - } - } - if (bMargin) { - pRect = pBoundStyle->GetMarginWidth(); - if (pRect) { - if (FDE_CSSLengthToFloat(pRect->left, fContainerWidth, fResult)) { - rect.left += fResult; - } - if (FDE_CSSLengthToFloat(pRect->top, fContainerWidth, fResult)) { - rect.top += fResult; - } - if (FDE_CSSLengthToFloat(pRect->right, fContainerWidth, fResult)) { - rect.right += fResult; - } - if (FDE_CSSLengthToFloat(pRect->bottom, fContainerWidth, fResult)) { - rect.bottom += fResult; - } - } - } - return rect; -} -uint32_t FDE_CSSFontStyleToFDE(CFDE_CSSComputedStyle* pFontStyle) { - uint32_t dwFontStyle = FX_FONTSTYLE_Normal; - if (pFontStyle->GetFontStyle() == FDE_CSSFontStyle::Italic) { - dwFontStyle |= FX_FONTSTYLE_Italic; - } - if (pFontStyle->GetFontWeight() >= 700) { - dwFontStyle |= FX_FONTSTYLE_Bold; - } - return dwFontStyle; -} - static const FDE_CSSPropertyTable g_FDE_CSSProperties[] = { {FDE_CSSProperty::BorderLeft, L"border-left", 0x04080036, FDE_CSSVALUETYPE_Shorthand}, @@ -353,11 +259,6 @@ const FDE_CSSPropertyValueTable* FDE_GetCSSPropertyValueByName( return nullptr; } -const FDE_CSSPropertyValueTable* FDE_GetCSSPropertyValueByEnum( - FDE_CSSPropertyValue eName) { - return g_FDE_CSSPropertyValues + static_cast<int>(eName); -} - const FDE_CSSMEDIATYPETABLE* FDE_GetCSSMediaTypeByName( const CFX_WideStringC& wsName) { ASSERT(!wsName.IsEmpty()); @@ -421,120 +322,3 @@ const FDE_CSSCOLORTABLE* FDE_GetCSSColorByName(const CFX_WideStringC& wsName) { } while (iStart <= iEnd); return nullptr; } - -bool FDE_ParseCSSNumber(const FX_WCHAR* pszValue, - int32_t iValueLen, - FX_FLOAT& fValue, - FDE_CSSNumberType& eUnit) { - ASSERT(pszValue && iValueLen > 0); - int32_t iUsedLen = 0; - fValue = FXSYS_wcstof(pszValue, iValueLen, &iUsedLen); - if (iUsedLen <= 0) - return false; - - iValueLen -= iUsedLen; - pszValue += iUsedLen; - eUnit = FDE_CSSNumberType::Number; - if (iValueLen >= 1 && *pszValue == '%') { - eUnit = FDE_CSSNumberType::Percent; - } else if (iValueLen == 2) { - const FDE_CSSLengthUnitTable* pUnit = - FDE_GetCSSLengthUnitByName(CFX_WideStringC(pszValue, 2)); - if (pUnit) - eUnit = pUnit->wValue; - } - return true; -} - -bool FDE_ParseCSSString(const FX_WCHAR* pszValue, - int32_t iValueLen, - int32_t* iOffset, - int32_t* iLength) { - ASSERT(pszValue && iValueLen > 0); - *iOffset = 0; - *iLength = iValueLen; - if (iValueLen >= 2) { - FX_WCHAR first = pszValue[0], last = pszValue[iValueLen - 1]; - if ((first == '\"' && last == '\"') || (first == '\'' && last == '\'')) { - *iOffset = 1; - *iLength -= 2; - } - } - return iValueLen > 0; -} - -bool FDE_ParseCSSURI(const FX_WCHAR* pszValue, - int32_t* iOffset, - int32_t* iLength) { - ASSERT(pszValue && *iLength > 0); - if (*iLength < 6 || pszValue[*iLength - 1] != ')' || - FXSYS_wcsnicmp(L"url(", pszValue, 4)) { - return false; - } - if (FDE_ParseCSSString(pszValue + 4, *iLength - 5, iOffset, iLength)) { - *iOffset += 4; - return true; - } - return false; -} - -bool FDE_ParseCSSColor(const FX_WCHAR* pszValue, - int32_t iValueLen, - FX_ARGB& dwColor) { - ASSERT(pszValue && iValueLen > 0); - - if (*pszValue == '#') { - switch (iValueLen) { - case 4: { - uint8_t red = Hex2Dec((uint8_t)pszValue[1], (uint8_t)pszValue[1]); - uint8_t green = Hex2Dec((uint8_t)pszValue[2], (uint8_t)pszValue[2]); - uint8_t blue = Hex2Dec((uint8_t)pszValue[3], (uint8_t)pszValue[3]); - dwColor = ArgbEncode(255, red, green, blue); - return true; - } - case 7: { - uint8_t red = Hex2Dec((uint8_t)pszValue[1], (uint8_t)pszValue[2]); - uint8_t green = Hex2Dec((uint8_t)pszValue[3], (uint8_t)pszValue[4]); - uint8_t blue = Hex2Dec((uint8_t)pszValue[5], (uint8_t)pszValue[6]); - dwColor = ArgbEncode(255, red, green, blue); - return true; - } - default: - return false; - } - } - - if (iValueLen >= 10) { - if (pszValue[iValueLen - 1] != ')' || FXSYS_wcsnicmp(L"rgb(", pszValue, 4)) - return false; - - uint8_t rgb[3] = {0}; - FX_FLOAT fValue; - FDE_CSSPrimitiveType eType; - CFDE_CSSValueListParser list(pszValue + 4, iValueLen - 5, ','); - for (int32_t i = 0; i < 3; ++i) { - if (!list.NextValue(eType, pszValue, iValueLen)) - return false; - if (eType != FDE_CSSPrimitiveType::Number) - return false; - FDE_CSSNumberType eNumType; - if (!FDE_ParseCSSNumber(pszValue, iValueLen, fValue, eNumType)) - return false; - - rgb[i] = eNumType == FDE_CSSNumberType::Percent - ? FXSYS_round(fValue * 2.55f) - : FXSYS_round(fValue); - } - dwColor = ArgbEncode(255, rgb[0], rgb[1], rgb[2]); - return true; - } - - const FDE_CSSCOLORTABLE* pColor = - FDE_GetCSSColorByName(CFX_WideStringC(pszValue, iValueLen)); - if (!pColor) - return false; - - dwColor = pColor->dwValue; - return true; -} - diff --git a/xfa/fde/css/fde_cssdatatable.h b/xfa/fde/css/fde_cssdatatable.h index f2cc2b45d4..4769f16658 100644 --- a/xfa/fde/css/fde_cssdatatable.h +++ b/xfa/fde/css/fde_cssdatatable.h @@ -25,65 +25,48 @@ struct FDE_CSSPropertyTable { uint32_t dwType; }; -const FDE_CSSPropertyTable* FDE_GetCSSPropertyByName( - const CFX_WideStringC& wsName); -const FDE_CSSPropertyTable* FDE_GetCSSPropertyByEnum(FDE_CSSProperty eName); - struct FDE_CSSPropertyValueTable { FDE_CSSPropertyValue eName; const FX_WCHAR* pszName; uint32_t dwHash; }; -const FDE_CSSPropertyValueTable* FDE_GetCSSPropertyValueByName( - const CFX_WideStringC& wsName); -const FDE_CSSPropertyValueTable* FDE_GetCSSPropertyValueByEnum( - FDE_CSSPropertyValue eName); - struct FDE_CSSMEDIATYPETABLE { uint16_t wHash; uint16_t wValue; }; -const FDE_CSSMEDIATYPETABLE* FDE_GetCSSMediaTypeByName( - const CFX_WideStringC& wsName); - struct FDE_CSSLengthUnitTable { uint16_t wHash; FDE_CSSNumberType wValue; }; -const FDE_CSSLengthUnitTable* FDE_GetCSSLengthUnitByName( - const CFX_WideStringC& wsName); - struct FDE_CSSCOLORTABLE { uint32_t dwHash; FX_ARGB dwValue; }; -const FDE_CSSCOLORTABLE* FDE_GetCSSColorByName(const CFX_WideStringC& wsName); - struct FDE_CSSPseudoTable { FDE_CSSPseudo eName; const FX_WCHAR* pszName; uint32_t dwHash; }; -const FDE_CSSPseudoTable* FDE_GetCSSPseudoByEnum(FDE_CSSPseudo ePseudo); +const FDE_CSSPropertyTable* FDE_GetCSSPropertyByName( + const CFX_WideStringC& wsName); +const FDE_CSSPropertyTable* FDE_GetCSSPropertyByEnum(FDE_CSSProperty eName); + +const FDE_CSSPropertyValueTable* FDE_GetCSSPropertyValueByName( + const CFX_WideStringC& wsName); -bool FDE_ParseCSSNumber(const FX_WCHAR* pszValue, - int32_t iValueLen, - FX_FLOAT& fValue, - FDE_CSSNumberType& eUnit); -bool FDE_ParseCSSString(const FX_WCHAR* pszValue, - int32_t iValueLen, - int32_t* iOffset, - int32_t* iLength); -bool FDE_ParseCSSColor(const FX_WCHAR* pszValue, - int32_t iValueLen, - FX_ARGB& dwColor); -bool FDE_ParseCSSURI(const FX_WCHAR* pszValue, - int32_t* iOffset, - int32_t* iLength); +const FDE_CSSMEDIATYPETABLE* FDE_GetCSSMediaTypeByName( + const CFX_WideStringC& wsName); + +const FDE_CSSLengthUnitTable* FDE_GetCSSLengthUnitByName( + const CFX_WideStringC& wsName); + +const FDE_CSSCOLORTABLE* FDE_GetCSSColorByName(const CFX_WideStringC& wsName); + +const FDE_CSSPseudoTable* FDE_GetCSSPseudoByEnum(FDE_CSSPseudo ePseudo); #endif // XFA_FDE_CSS_FDE_CSSDATATABLE_H_ diff --git a/xfa/fde/css/fde_cssstyleselector.h b/xfa/fde/css/fde_cssstyleselector.h deleted file mode 100644 index f4c811b1e7..0000000000 --- a/xfa/fde/css/fde_cssstyleselector.h +++ /dev/null @@ -1,249 +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 XFA_FDE_CSS_FDE_CSSSTYLESELECTOR_H_ -#define XFA_FDE_CSS_FDE_CSSSTYLESELECTOR_H_ - -#include <map> -#include <memory> -#include <vector> - -#include "core/fxcrt/fx_ext.h" -#include "xfa/fde/css/fde_css.h" -#include "xfa/fde/css/fde_csscache.h" -#include "xfa/fde/css/fde_cssdeclaration.h" - -class CFDE_CSSAccelerator; -class CFDE_CSSComputedStyle; -class CFDE_CSSRule; -class CFDE_CSSSelector; -class CFDE_CSSStyleSheet; -class CFDE_CSSValueList; -class CXFA_CSSTagProvider; - -class FDE_CSSRuleData { - public: - FDE_CSSRuleData(CFDE_CSSSelector* pSel, - CFDE_CSSDeclaration* pDecl, - uint32_t dwPos); - - CFDE_CSSSelector* const pSelector; - CFDE_CSSDeclaration* const pDeclaration; - uint32_t dwPriority; - FDE_CSSRuleData* pNext; -}; - -class CFDE_CSSRuleCollection { - public: - CFDE_CSSRuleCollection(); - ~CFDE_CSSRuleCollection(); - - void AddRulesFrom(const CFX_ArrayTemplate<CFDE_CSSStyleSheet*>& sheets, - uint32_t dwMediaList, - CFGAS_FontMgr* pFontMgr); - void Clear(); - int32_t CountSelectors() const { return m_iSelectors; } - - FDE_CSSRuleData* GetIDRuleData(uint32_t dwIDHash) { - auto it = m_IDRules.find(dwIDHash); - return it != m_IDRules.end() ? it->second : nullptr; - } - - FDE_CSSRuleData* GetTagRuleData(uint32_t dwTagHash) { - auto it = m_TagRules.find(dwTagHash); - return it != m_TagRules.end() ? it->second : nullptr; - } - - FDE_CSSRuleData* GetClassRuleData(uint32_t dwIDHash) { - auto it = m_ClassRules.find(dwIDHash); - return it != m_ClassRules.end() ? it->second : nullptr; - } - - FDE_CSSRuleData* GetUniversalRuleData() { return m_pUniversalRules; } - FDE_CSSRuleData* GetPseudoRuleData() { return m_pPseudoRules; } - - protected: - void AddRulesFrom(CFDE_CSSStyleSheet* pStyleSheet, - CFDE_CSSRule* pRule, - uint32_t dwMediaList, - CFGAS_FontMgr* pFontMgr); - void AddRuleTo(std::map<uint32_t, FDE_CSSRuleData*>* pMap, - uint32_t dwKey, - CFDE_CSSSelector* pSel, - CFDE_CSSDeclaration* pDecl); - bool AddRuleTo(FDE_CSSRuleData** pList, FDE_CSSRuleData* pData); - FDE_CSSRuleData* NewRuleData(CFDE_CSSSelector* pSel, - CFDE_CSSDeclaration* pDecl); - - std::map<uint32_t, FDE_CSSRuleData*> m_IDRules; - std::map<uint32_t, FDE_CSSRuleData*> m_TagRules; - std::map<uint32_t, FDE_CSSRuleData*> m_ClassRules; - FDE_CSSRuleData* m_pUniversalRules; - FDE_CSSRuleData* m_pPseudoRules; - int32_t m_iSelectors; -}; - -class CFDE_CSSStyleSelector { - public: - explicit CFDE_CSSStyleSelector(CFGAS_FontMgr* pFontMgr); - ~CFDE_CSSStyleSelector(); - - void SetDefFontSize(FX_FLOAT fFontSize); - - bool SetStyleSheet(FDE_CSSStyleSheetGroup eType, CFDE_CSSStyleSheet* pSheet); - bool SetStyleSheets(FDE_CSSStyleSheetGroup eType, - const CFX_ArrayTemplate<CFDE_CSSStyleSheet*>* pArray); - void SetStylePriority(FDE_CSSStyleSheetGroup eType, - FDE_CSSStyleSheetPriority ePriority); - void UpdateStyleIndex(uint32_t dwMediaList); - CFDE_CSSAccelerator* InitAccelerator(); - CFDE_CSSComputedStyle* CreateComputedStyle( - CFDE_CSSComputedStyle* pParentStyle); - int32_t MatchDeclarations( - CXFA_CSSTagProvider* pTag, - CFX_ArrayTemplate<CFDE_CSSDeclaration*>& matchedDecls, - FDE_CSSPseudo ePseudoType = FDE_CSSPseudo::NONE); - void ComputeStyle(CXFA_CSSTagProvider* pTag, - const CFDE_CSSDeclaration** ppDeclArray, - int32_t iDeclCount, - CFDE_CSSComputedStyle* pDestStyle); - - protected: - void Reset(); - void MatchRules(FDE_CSSTagCache* pCache, - FDE_CSSRuleData* pList, - FDE_CSSPseudo ePseudoType); - bool MatchSelector(FDE_CSSTagCache* pCache, - CFDE_CSSSelector* pSel, - FDE_CSSPseudo ePseudoType); - void AppendInlineStyle(CFDE_CSSDeclaration* pDecl, - const FX_WCHAR* psz, - int32_t iLen); - void ApplyDeclarations(bool bPriority, - const CFDE_CSSDeclaration** ppDeclArray, - int32_t iDeclCount, - CFDE_CSSComputedStyle* pDestStyle); - void ApplyProperty(FDE_CSSProperty eProperty, - CFDE_CSSValue* pValue, - CFDE_CSSComputedStyle* pComputedStyle); - - bool SetLengthWithPercent(FDE_CSSLength& width, - FDE_CSSPrimitiveType eType, - CFDE_CSSValue* pValue, - FX_FLOAT fFontSize); - FX_FLOAT ToFontSize(FDE_CSSPropertyValue eValue, FX_FLOAT fCurFontSize); - FDE_CSSDisplay ToDisplay(FDE_CSSPropertyValue eValue); - FDE_CSSTextAlign ToTextAlign(FDE_CSSPropertyValue eValue); - uint16_t ToFontWeight(FDE_CSSPropertyValue eValue); - FDE_CSSFontStyle ToFontStyle(FDE_CSSPropertyValue eValue); - FDE_CSSVerticalAlign ToVerticalAlign(FDE_CSSPropertyValue eValue); - uint32_t ToTextDecoration(CFDE_CSSValueList* pList); - FDE_CSSFontVariant ToFontVariant(FDE_CSSPropertyValue eValue); - - CFGAS_FontMgr* const m_pFontMgr; - FX_FLOAT m_fDefFontSize; - CFX_ArrayTemplate<CFDE_CSSStyleSheet*> m_SheetGroups[3]; - CFDE_CSSRuleCollection m_RuleCollection[3]; - FDE_CSSStyleSheetGroup m_ePriorities[3]; - std::unique_ptr<CFDE_CSSAccelerator> m_pAccelerator; - std::vector<FDE_CSSRuleData*> m_MatchedRules; -}; - -class CFDE_CSSInheritedData { - public: - CFDE_CSSInheritedData(); - - FDE_CSSLength m_LetterSpacing; - FDE_CSSLength m_WordSpacing; - FDE_CSSLength m_TextIndent; - CFDE_CSSValueList* m_pFontFamily; - FX_FLOAT m_fFontSize; - FX_FLOAT m_fLineHeight; - FX_ARGB m_dwFontColor; - uint16_t m_wFontWeight; - FDE_CSSFontVariant m_eFontVariant; - FDE_CSSFontStyle m_eFontStyle; - FDE_CSSTextAlign m_eTextAlign; -}; - -class CFDE_CSSNonInheritedData { - public: - CFDE_CSSNonInheritedData(); - - FDE_CSSRect m_MarginWidth; - FDE_CSSRect m_BorderWidth; - FDE_CSSRect m_PaddingWidth; - FDE_CSSLength m_Top; - FDE_CSSLength m_Bottom; - FDE_CSSLength m_Left; - FDE_CSSLength m_Right; - FX_FLOAT m_fVerticalAlign; - FDE_CSSDisplay m_eDisplay; - FDE_CSSVerticalAlign m_eVerticalAlign; - uint8_t m_dwTextDecoration; - bool m_bHasMargin; - bool m_bHasBorder; - bool m_bHasPadding; -}; - -class CFDE_CSSComputedStyle : public IFX_Retainable { - public: - CFDE_CSSComputedStyle(); - ~CFDE_CSSComputedStyle() override; - - // IFX_Retainable - uint32_t Retain() override; - uint32_t Release() override; - - int32_t CountFontFamilies() const; - const CFX_WideString GetFontFamily(int32_t index) const; - uint16_t GetFontWeight() const; - FDE_CSSFontVariant GetFontVariant() const; - FDE_CSSFontStyle GetFontStyle() const; - FX_FLOAT GetFontSize() const; - FX_ARGB GetColor() const; - void SetFontWeight(uint16_t wFontWeight); - void SetFontVariant(FDE_CSSFontVariant eFontVariant); - void SetFontStyle(FDE_CSSFontStyle eFontStyle); - void SetFontSize(FX_FLOAT fFontSize); - void SetColor(FX_ARGB dwFontColor); - - const FDE_CSSRect* GetBorderWidth() const; - const FDE_CSSRect* GetMarginWidth() const; - const FDE_CSSRect* GetPaddingWidth() const; - void SetMarginWidth(const FDE_CSSRect& rect); - void SetPaddingWidth(const FDE_CSSRect& rect); - - FDE_CSSDisplay GetDisplay() const; - - FX_FLOAT GetLineHeight() const; - const FDE_CSSLength& GetTextIndent() const; - FDE_CSSTextAlign GetTextAlign() const; - FDE_CSSVerticalAlign GetVerticalAlign() const; - FX_FLOAT GetNumberVerticalAlign() const; - uint32_t GetTextDecoration() const; - const FDE_CSSLength& GetLetterSpacing() const; - void SetLineHeight(FX_FLOAT fLineHeight); - void SetTextIndent(const FDE_CSSLength& textIndent); - void SetTextAlign(FDE_CSSTextAlign eTextAlign); - void SetNumberVerticalAlign(FX_FLOAT fAlign); - void SetTextDecoration(uint32_t dwTextDecoration); - void SetLetterSpacing(const FDE_CSSLength& letterSpacing); - void AddCustomStyle(const CFX_WideString& wsName, - const CFX_WideString& wsValue); - - bool GetCustomStyle(const CFX_WideStringC& wsName, - CFX_WideString& wsValue) const; - - CFDE_CSSInheritedData m_InheritedData; - CFDE_CSSNonInheritedData m_NonInheritedData; - - private: - uint32_t m_dwRefCount; - std::vector<CFX_WideString> m_CustomProperties; -}; - -#endif // XFA_FDE_CSS_FDE_CSSSTYLESELECTOR_H_ diff --git a/xfa/fde/css/fde_cssstylesheet.h b/xfa/fde/css/fde_cssstylesheet.h deleted file mode 100644 index aa05a9dda1..0000000000 --- a/xfa/fde/css/fde_cssstylesheet.h +++ /dev/null @@ -1,127 +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 XFA_FDE_CSS_FDE_CSSSTYLESHEET_H_ -#define XFA_FDE_CSS_FDE_CSSSTYLESHEET_H_ - -#include <memory> -#include <unordered_map> -#include <utility> -#include <vector> - -#include "core/fxcrt/fx_ext.h" -#include "xfa/fde/css/cfde_cssrule.h" -#include "xfa/fde/css/fde_cssdeclaration.h" - -class CFDE_CSSSyntaxParser; - -class CFDE_CSSSelector { - public: - static std::unique_ptr<CFDE_CSSSelector> FromString(const FX_WCHAR* psz, - int32_t iLen); - - CFDE_CSSSelector(FDE_CSSSelectorType eType, - const FX_WCHAR* psz, - int32_t iLen, - bool bIgnoreCase); - ~CFDE_CSSSelector(); - - FDE_CSSSelectorType GetType() const; - uint32_t GetNameHash() const; - CFDE_CSSSelector* GetNextSelector() const; - std::unique_ptr<CFDE_CSSSelector> ReleaseNextSelector(); - - void SetNext(std::unique_ptr<CFDE_CSSSelector> pNext) { - m_pNext = std::move(pNext); - } - - protected: - void SetType(FDE_CSSSelectorType eType) { m_eType = eType; } - - FDE_CSSSelectorType m_eType; - uint32_t m_dwHash; - std::unique_ptr<CFDE_CSSSelector> m_pNext; -}; - -class CFDE_CSSStyleRule : public CFDE_CSSRule { - public: - CFDE_CSSStyleRule(); - ~CFDE_CSSStyleRule() override; - - size_t CountSelectorLists() const; - CFDE_CSSSelector* GetSelectorList(int32_t index) const; - CFDE_CSSDeclaration* GetDeclaration(); - - void SetSelector(std::vector<std::unique_ptr<CFDE_CSSSelector>>* list); - - private: - CFDE_CSSDeclaration m_Declaration; - std::vector<std::unique_ptr<CFDE_CSSSelector>> m_ppSelector; -}; - -class CFDE_CSSMediaRule : public CFDE_CSSRule { - public: - explicit CFDE_CSSMediaRule(uint32_t dwMediaList); - ~CFDE_CSSMediaRule() override; - - uint32_t GetMediaList() const; - int32_t CountRules() const; - CFDE_CSSRule* GetRule(int32_t index); - - std::vector<std::unique_ptr<CFDE_CSSRule>>& GetArray() { return m_RuleArray; } - - protected: - uint32_t m_dwMediaList; - std::vector<std::unique_ptr<CFDE_CSSRule>> m_RuleArray; -}; - -class CFDE_CSSFontFaceRule : public CFDE_CSSRule { - public: - CFDE_CSSFontFaceRule(); - ~CFDE_CSSFontFaceRule() override; - - CFDE_CSSDeclaration* GetDeclaration() { return &m_Declaration; } - - private: - CFDE_CSSDeclaration m_Declaration; -}; - -class CFDE_CSSStyleSheet : public IFX_Retainable { - public: - CFDE_CSSStyleSheet(); - ~CFDE_CSSStyleSheet() override; - - // IFX_Retainable: - uint32_t Retain() override; - uint32_t Release() override; - - bool LoadFromBuffer(const FX_WCHAR* pBuffer, int32_t iBufSize); - - uint32_t GetMediaList() const; - int32_t CountRules() const; - CFDE_CSSRule* GetRule(int32_t index); - - private: - void Reset(); - bool LoadFromSyntax(CFDE_CSSSyntaxParser* pSyntax); - FDE_CSSSyntaxStatus LoadStyleRule( - CFDE_CSSSyntaxParser* pSyntax, - std::vector<std::unique_ptr<CFDE_CSSRule>>* ruleArray); - FDE_CSSSyntaxStatus LoadImportRule(CFDE_CSSSyntaxParser* pSyntax); - FDE_CSSSyntaxStatus LoadPageRule(CFDE_CSSSyntaxParser* pSyntax); - FDE_CSSSyntaxStatus LoadMediaRule(CFDE_CSSSyntaxParser* pSyntax); - FDE_CSSSyntaxStatus LoadFontFaceRule( - CFDE_CSSSyntaxParser* pSyntax, - std::vector<std::unique_ptr<CFDE_CSSRule>>* ruleArray); - FDE_CSSSyntaxStatus SkipRuleSet(CFDE_CSSSyntaxParser* pSyntax); - - uint16_t m_wRefCount; - uint32_t m_dwMediaList; - std::vector<std::unique_ptr<CFDE_CSSRule>> m_RuleArray; - std::unordered_map<uint32_t, FX_WCHAR*> m_StringCache; -}; - -#endif // XFA_FDE_CSS_FDE_CSSSTYLESHEET_H_ |