blob: 76eddda4a3f6dada251085db899c09810aae45c1 [file] [log] [blame]
Dan Sinclair1770c022016-03-14 14:14:16 -04001// 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
tsepez788ac382016-05-19 21:06:16 -070010#include <unordered_map>
11
Dan Sinclaira8a28e02016-03-23 15:41:39 -040012#include "core/fxcrt/include/fx_ext.h"
Dan Sinclair1770c022016-03-14 14:14:16 -040013#include "xfa/fde/css/fde_cssdeclaration.h"
14
dsinclaire6ebf7a2016-04-28 06:34:24 -070015class CFDE_CSSSyntaxParser;
16
17class CFDE_CSSSelector : public CFX_Target {
Dan Sinclair1770c022016-03-14 14:14:16 -040018 public:
19 CFDE_CSSSelector(FDE_CSSSELECTORTYPE eType,
20 const FX_WCHAR* psz,
21 int32_t iLen,
tsepezb6853cf2016-04-25 11:23:43 -070022 bool bIgnoreCase)
Dan Sinclair1770c022016-03-14 14:14:16 -040023 : m_eType(eType),
tsepezb6853cf2016-04-25 11:23:43 -070024 m_dwHash(FX_HashCode_GetW(CFX_WideStringC(psz, iLen), bIgnoreCase)),
25 m_pNext(nullptr) {}
Dan Sinclair1770c022016-03-14 14:14:16 -040026 virtual FDE_CSSSELECTORTYPE GetType() const { return m_eType; }
27
tsepezdeee3d22016-03-25 14:38:58 -070028 virtual uint32_t GetNameHash() const { return m_dwHash; }
Dan Sinclair1770c022016-03-14 14:14:16 -040029
dsinclaire6ebf7a2016-04-28 06:34:24 -070030 virtual CFDE_CSSSelector* GetNextSelector() const { return m_pNext; }
tsepez4d06f832016-05-03 14:13:29 -070031 static CFDE_CSSSelector* FromString(IFX_MemoryAllocator* pStaticStore,
Dan Sinclair1770c022016-03-14 14:14:16 -040032 const FX_WCHAR* psz,
33 int32_t iLen);
dsinclaire6ebf7a2016-04-28 06:34:24 -070034 void SetNext(CFDE_CSSSelector* pNext) { m_pNext = pNext; }
Dan Sinclair1770c022016-03-14 14:14:16 -040035
36 protected:
tsepez4d06f832016-05-03 14:13:29 -070037 static CFDE_CSSSelector* ParseSelector(IFX_MemoryAllocator* pStaticStore,
Dan Sinclair1770c022016-03-14 14:14:16 -040038 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;
tsepezdeee3d22016-03-25 14:38:58 -070044 uint32_t m_dwHash;
dsinclaire6ebf7a2016-04-28 06:34:24 -070045 CFDE_CSSSelector* m_pNext;
Dan Sinclair1770c022016-03-14 14:14:16 -040046};
dsinclaire6ebf7a2016-04-28 06:34:24 -070047typedef CFX_ArrayTemplate<CFDE_CSSSelector*> CFDE_CSSSelectorArray;
tsepezb6853cf2016-04-25 11:23:43 -070048
Dan Sinclair1770c022016-03-14 14:14:16 -040049class CFDE_CSSStyleRule : public IFDE_CSSStyleRule, public CFX_Target {
50 public:
51 CFDE_CSSStyleRule() : m_ppSelector(NULL), m_iSelectors(0) {}
dsinclaire6ebf7a2016-04-28 06:34:24 -070052 int32_t CountSelectorLists() const override { return m_iSelectors; }
53 CFDE_CSSSelector* GetSelectorList(int32_t index) const override {
Dan Sinclair1770c022016-03-14 14:14:16 -040054 return m_ppSelector[index];
55 }
56
dsinclaire6ebf7a2016-04-28 06:34:24 -070057 CFDE_CSSDeclaration* GetDeclaration() override { return &m_Declaration; }
Dan Sinclair1770c022016-03-14 14:14:16 -040058 CFDE_CSSDeclaration& GetDeclImp() { return m_Declaration; }
tsepez4d06f832016-05-03 14:13:29 -070059 void SetSelector(IFX_MemoryAllocator* pStaticStore,
Dan Sinclair1770c022016-03-14 14:14:16 -040060 const CFDE_CSSSelectorArray& list);
61
62 protected:
63 CFDE_CSSDeclaration m_Declaration;
dsinclaire6ebf7a2016-04-28 06:34:24 -070064 CFDE_CSSSelector** m_ppSelector;
Dan Sinclair1770c022016-03-14 14:14:16 -040065 int32_t m_iSelectors;
66};
67class CFDE_CSSMediaRule : public IFDE_CSSMediaRule, public CFX_Target {
68 public:
tsepezdeee3d22016-03-25 14:38:58 -070069 CFDE_CSSMediaRule(uint32_t dwMediaList) : m_dwMediaList(dwMediaList) {}
Dan Sinclair1770c022016-03-14 14:14:16 -040070 ~CFDE_CSSMediaRule();
71
tsepezdeee3d22016-03-25 14:38:58 -070072 virtual uint32_t GetMediaList() const { return m_dwMediaList; }
Dan Sinclair1770c022016-03-14 14:14:16 -040073
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:
tsepezdeee3d22016-03-25 14:38:58 -070081 uint32_t m_dwMediaList;
Dan Sinclair1770c022016-03-14 14:14:16 -040082 CFDE_CSSRuleArray m_RuleArray;
83};
84class CFDE_CSSFontFaceRule : public IFDE_CSSFontFaceRule, public CFX_Target {
85 public:
dsinclaire6ebf7a2016-04-28 06:34:24 -070086 CFDE_CSSDeclaration* GetDeclaration() override { return &m_Declaration; }
Dan Sinclair1770c022016-03-14 14:14:16 -040087 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;
98class CFDE_CSSStyleSheet : public IFDE_CSSStyleSheet, public CFX_Target {
99 public:
tsepezdeee3d22016-03-25 14:38:58 -0700100 CFDE_CSSStyleSheet(uint32_t dwMediaList);
tsepeza2d699f2016-05-23 17:19:20 -0700101 ~CFDE_CSSStyleSheet() override;
Dan Sinclair1770c022016-03-14 14:14:16 -0400102
tsepeza2d699f2016-05-23 17:19:20 -0700103 // 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 Sinclair1770c022016-03-14 14:14:16 -0400109 szUrl = m_szUrl;
110 return szUrl.GetLength() > 0;
111 }
tsepeza2d699f2016-05-23 17:19:20 -0700112 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 Sinclair1770c022016-03-14 14:14:16 -0400117 FX_BOOL LoadFromStream(const CFX_WideString& szUrl,
118 IFX_Stream* pStream,
Tom Sepez62a70f92016-03-21 15:00:20 -0700119 uint16_t wCodePage);
Dan Sinclair1770c022016-03-14 14:14:16 -0400120 FX_BOOL LoadFromBuffer(const CFX_WideString& szUrl,
121 const FX_WCHAR* pBuffer,
122 int32_t iBufSize,
Tom Sepez62a70f92016-03-21 15:00:20 -0700123 uint16_t wCodePage);
Dan Sinclair1770c022016-03-14 14:14:16 -0400124
125 protected:
126 void Reset();
dsinclaire6ebf7a2016-04-28 06:34:24 -0700127 FX_BOOL LoadFromSyntax(CFDE_CSSSyntaxParser* pSyntax);
128 FDE_CSSSYNTAXSTATUS LoadStyleRule(CFDE_CSSSyntaxParser* pSyntax,
Dan Sinclair1770c022016-03-14 14:14:16 -0400129 CFDE_CSSRuleArray& ruleArray);
dsinclaire6ebf7a2016-04-28 06:34:24 -0700130 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 Sinclair1770c022016-03-14 14:14:16 -0400134 CFDE_CSSRuleArray& ruleArray);
dsinclaire6ebf7a2016-04-28 06:34:24 -0700135 FDE_CSSSYNTAXSTATUS SkipRuleSet(CFDE_CSSSyntaxParser* pSyntax);
Tom Sepez62a70f92016-03-21 15:00:20 -0700136 uint16_t m_wCodePage;
137 uint16_t m_wRefCount;
tsepezdeee3d22016-03-25 14:38:58 -0700138 uint32_t m_dwMediaList;
tsepez4d06f832016-05-03 14:13:29 -0700139 IFX_MemoryAllocator* m_pAllocator;
Dan Sinclair1770c022016-03-14 14:14:16 -0400140 CFDE_CSSRuleArray m_RuleArray;
141 CFX_WideString m_szUrl;
142 CFDE_CSSSelectorArray m_Selectors;
tsepez788ac382016-05-19 21:06:16 -0700143 std::unordered_map<uint32_t, FX_WCHAR*> m_StringCache;
Dan Sinclair1770c022016-03-14 14:14:16 -0400144};
145
146#endif // XFA_FDE_CSS_FDE_CSSSTYLESHEET_H_