Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 1 | // Copyright 2014 PDFium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | |
| 7 | #ifndef XFA_FDE_CSS_FDE_CSSSTYLESHEET_H_ |
| 8 | #define XFA_FDE_CSS_FDE_CSSSTYLESHEET_H_ |
| 9 | |
tsepez | 788ac38 | 2016-05-19 21:06:16 -0700 | [diff] [blame] | 10 | #include <unordered_map> |
| 11 | |
Dan Sinclair | a8a28e0 | 2016-03-23 15:41:39 -0400 | [diff] [blame] | 12 | #include "core/fxcrt/include/fx_ext.h" |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 13 | #include "xfa/fde/css/fde_cssdeclaration.h" |
| 14 | |
dsinclair | e6ebf7a | 2016-04-28 06:34:24 -0700 | [diff] [blame] | 15 | class CFDE_CSSSyntaxParser; |
| 16 | |
| 17 | class CFDE_CSSSelector : public CFX_Target { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 18 | public: |
| 19 | CFDE_CSSSelector(FDE_CSSSELECTORTYPE eType, |
| 20 | const FX_WCHAR* psz, |
| 21 | int32_t iLen, |
tsepez | b6853cf | 2016-04-25 11:23:43 -0700 | [diff] [blame] | 22 | bool bIgnoreCase) |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 23 | : m_eType(eType), |
tsepez | b6853cf | 2016-04-25 11:23:43 -0700 | [diff] [blame] | 24 | m_dwHash(FX_HashCode_GetW(CFX_WideStringC(psz, iLen), bIgnoreCase)), |
| 25 | m_pNext(nullptr) {} |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 26 | virtual FDE_CSSSELECTORTYPE GetType() const { return m_eType; } |
| 27 | |
tsepez | deee3d2 | 2016-03-25 14:38:58 -0700 | [diff] [blame] | 28 | virtual uint32_t GetNameHash() const { return m_dwHash; } |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 29 | |
dsinclair | e6ebf7a | 2016-04-28 06:34:24 -0700 | [diff] [blame] | 30 | virtual CFDE_CSSSelector* GetNextSelector() const { return m_pNext; } |
tsepez | 4d06f83 | 2016-05-03 14:13:29 -0700 | [diff] [blame] | 31 | static CFDE_CSSSelector* FromString(IFX_MemoryAllocator* pStaticStore, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 32 | const FX_WCHAR* psz, |
| 33 | int32_t iLen); |
dsinclair | e6ebf7a | 2016-04-28 06:34:24 -0700 | [diff] [blame] | 34 | void SetNext(CFDE_CSSSelector* pNext) { m_pNext = pNext; } |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 35 | |
| 36 | protected: |
tsepez | 4d06f83 | 2016-05-03 14:13:29 -0700 | [diff] [blame] | 37 | static CFDE_CSSSelector* ParseSelector(IFX_MemoryAllocator* pStaticStore, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 38 | const FX_WCHAR* psz, |
| 39 | int32_t& iOff, |
| 40 | int32_t iLen, |
| 41 | FDE_CSSSELECTORTYPE eType); |
| 42 | void SetType(FDE_CSSSELECTORTYPE eType) { m_eType = eType; } |
| 43 | FDE_CSSSELECTORTYPE m_eType; |
tsepez | deee3d2 | 2016-03-25 14:38:58 -0700 | [diff] [blame] | 44 | uint32_t m_dwHash; |
dsinclair | e6ebf7a | 2016-04-28 06:34:24 -0700 | [diff] [blame] | 45 | CFDE_CSSSelector* m_pNext; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 46 | }; |
dsinclair | e6ebf7a | 2016-04-28 06:34:24 -0700 | [diff] [blame] | 47 | typedef CFX_ArrayTemplate<CFDE_CSSSelector*> CFDE_CSSSelectorArray; |
tsepez | b6853cf | 2016-04-25 11:23:43 -0700 | [diff] [blame] | 48 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 49 | class CFDE_CSSStyleRule : public IFDE_CSSStyleRule, public CFX_Target { |
| 50 | public: |
| 51 | CFDE_CSSStyleRule() : m_ppSelector(NULL), m_iSelectors(0) {} |
dsinclair | e6ebf7a | 2016-04-28 06:34:24 -0700 | [diff] [blame] | 52 | int32_t CountSelectorLists() const override { return m_iSelectors; } |
| 53 | CFDE_CSSSelector* GetSelectorList(int32_t index) const override { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 54 | return m_ppSelector[index]; |
| 55 | } |
| 56 | |
dsinclair | e6ebf7a | 2016-04-28 06:34:24 -0700 | [diff] [blame] | 57 | CFDE_CSSDeclaration* GetDeclaration() override { return &m_Declaration; } |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 58 | CFDE_CSSDeclaration& GetDeclImp() { return m_Declaration; } |
tsepez | 4d06f83 | 2016-05-03 14:13:29 -0700 | [diff] [blame] | 59 | void SetSelector(IFX_MemoryAllocator* pStaticStore, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 60 | const CFDE_CSSSelectorArray& list); |
| 61 | |
| 62 | protected: |
| 63 | CFDE_CSSDeclaration m_Declaration; |
dsinclair | e6ebf7a | 2016-04-28 06:34:24 -0700 | [diff] [blame] | 64 | CFDE_CSSSelector** m_ppSelector; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 65 | int32_t m_iSelectors; |
| 66 | }; |
| 67 | class CFDE_CSSMediaRule : public IFDE_CSSMediaRule, public CFX_Target { |
| 68 | public: |
tsepez | deee3d2 | 2016-03-25 14:38:58 -0700 | [diff] [blame] | 69 | CFDE_CSSMediaRule(uint32_t dwMediaList) : m_dwMediaList(dwMediaList) {} |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 70 | ~CFDE_CSSMediaRule(); |
| 71 | |
tsepez | deee3d2 | 2016-03-25 14:38:58 -0700 | [diff] [blame] | 72 | virtual uint32_t GetMediaList() const { return m_dwMediaList; } |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 73 | |
| 74 | virtual int32_t CountRules() const { return m_RuleArray.GetSize(); } |
| 75 | virtual IFDE_CSSRule* GetRule(int32_t index) { |
| 76 | return m_RuleArray.GetAt(index); |
| 77 | } |
| 78 | CFDE_CSSRuleArray& GetArray() { return m_RuleArray; } |
| 79 | |
| 80 | protected: |
tsepez | deee3d2 | 2016-03-25 14:38:58 -0700 | [diff] [blame] | 81 | uint32_t m_dwMediaList; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 82 | CFDE_CSSRuleArray m_RuleArray; |
| 83 | }; |
| 84 | class CFDE_CSSFontFaceRule : public IFDE_CSSFontFaceRule, public CFX_Target { |
| 85 | public: |
dsinclair | e6ebf7a | 2016-04-28 06:34:24 -0700 | [diff] [blame] | 86 | CFDE_CSSDeclaration* GetDeclaration() override { return &m_Declaration; } |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 87 | CFDE_CSSDeclaration& GetDeclImp() { return m_Declaration; } |
| 88 | |
| 89 | protected: |
| 90 | CFDE_CSSDeclaration m_Declaration; |
| 91 | }; |
| 92 | #define FDE_CSSSWITCHDEFAULTS() \ |
| 93 | case FDE_CSSSYNTAXSTATUS_EOS: \ |
| 94 | return FDE_CSSSYNTAXSTATUS_EOS; \ |
| 95 | case FDE_CSSSYNTAXSTATUS_Error: \ |
| 96 | default: \ |
| 97 | return FDE_CSSSYNTAXSTATUS_Error; |
| 98 | class CFDE_CSSStyleSheet : public IFDE_CSSStyleSheet, public CFX_Target { |
| 99 | public: |
tsepez | deee3d2 | 2016-03-25 14:38:58 -0700 | [diff] [blame] | 100 | CFDE_CSSStyleSheet(uint32_t dwMediaList); |
tsepez | a2d699f | 2016-05-23 17:19:20 -0700 | [diff] [blame^] | 101 | ~CFDE_CSSStyleSheet() override; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 102 | |
tsepez | a2d699f | 2016-05-23 17:19:20 -0700 | [diff] [blame^] | 103 | // IFX_Retainable: |
| 104 | uint32_t Retain() override; |
| 105 | uint32_t Release() override; |
| 106 | |
| 107 | // IFDE_CSSStyleSheet: |
| 108 | FX_BOOL GetUrl(CFX_WideString& szUrl) override { |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 109 | szUrl = m_szUrl; |
| 110 | return szUrl.GetLength() > 0; |
| 111 | } |
tsepez | a2d699f | 2016-05-23 17:19:20 -0700 | [diff] [blame^] | 112 | uint32_t GetMediaList() const override { return m_dwMediaList; } |
| 113 | uint16_t GetCodePage() const override { return m_wCodePage; } |
| 114 | int32_t CountRules() const override; |
| 115 | IFDE_CSSRule* GetRule(int32_t index) override; |
| 116 | |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 117 | FX_BOOL LoadFromStream(const CFX_WideString& szUrl, |
| 118 | IFX_Stream* pStream, |
Tom Sepez | 62a70f9 | 2016-03-21 15:00:20 -0700 | [diff] [blame] | 119 | uint16_t wCodePage); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 120 | FX_BOOL LoadFromBuffer(const CFX_WideString& szUrl, |
| 121 | const FX_WCHAR* pBuffer, |
| 122 | int32_t iBufSize, |
Tom Sepez | 62a70f9 | 2016-03-21 15:00:20 -0700 | [diff] [blame] | 123 | uint16_t wCodePage); |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 124 | |
| 125 | protected: |
| 126 | void Reset(); |
dsinclair | e6ebf7a | 2016-04-28 06:34:24 -0700 | [diff] [blame] | 127 | FX_BOOL LoadFromSyntax(CFDE_CSSSyntaxParser* pSyntax); |
| 128 | FDE_CSSSYNTAXSTATUS LoadStyleRule(CFDE_CSSSyntaxParser* pSyntax, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 129 | CFDE_CSSRuleArray& ruleArray); |
dsinclair | e6ebf7a | 2016-04-28 06:34:24 -0700 | [diff] [blame] | 130 | FDE_CSSSYNTAXSTATUS LoadImportRule(CFDE_CSSSyntaxParser* pSyntax); |
| 131 | FDE_CSSSYNTAXSTATUS LoadPageRule(CFDE_CSSSyntaxParser* pSyntax); |
| 132 | FDE_CSSSYNTAXSTATUS LoadMediaRule(CFDE_CSSSyntaxParser* pSyntax); |
| 133 | FDE_CSSSYNTAXSTATUS LoadFontFaceRule(CFDE_CSSSyntaxParser* pSyntax, |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 134 | CFDE_CSSRuleArray& ruleArray); |
dsinclair | e6ebf7a | 2016-04-28 06:34:24 -0700 | [diff] [blame] | 135 | FDE_CSSSYNTAXSTATUS SkipRuleSet(CFDE_CSSSyntaxParser* pSyntax); |
Tom Sepez | 62a70f9 | 2016-03-21 15:00:20 -0700 | [diff] [blame] | 136 | uint16_t m_wCodePage; |
| 137 | uint16_t m_wRefCount; |
tsepez | deee3d2 | 2016-03-25 14:38:58 -0700 | [diff] [blame] | 138 | uint32_t m_dwMediaList; |
tsepez | 4d06f83 | 2016-05-03 14:13:29 -0700 | [diff] [blame] | 139 | IFX_MemoryAllocator* m_pAllocator; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 140 | CFDE_CSSRuleArray m_RuleArray; |
| 141 | CFX_WideString m_szUrl; |
| 142 | CFDE_CSSSelectorArray m_Selectors; |
tsepez | 788ac38 | 2016-05-19 21:06:16 -0700 | [diff] [blame] | 143 | std::unordered_map<uint32_t, FX_WCHAR*> m_StringCache; |
Dan Sinclair | 1770c02 | 2016-03-14 14:14:16 -0400 | [diff] [blame] | 144 | }; |
| 145 | |
| 146 | #endif // XFA_FDE_CSS_FDE_CSSSTYLESHEET_H_ |