1 // Copyright 2017 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_FXFA_CXFA_TEXTPARSER_H_
8 #define XFA_FXFA_CXFA_TEXTPARSER_H_
9 
10 #include <map>
11 #include <memory>
12 
13 #include "core/fxcrt/fx_string.h"
14 #include "core/fxcrt/fx_system.h"
15 #include "core/fxcrt/retain_ptr.h"
16 #include "core/fxge/fx_dib.h"
17 #include "third_party/base/optional.h"
18 #include "xfa/fxfa/fxfa_basic.h"
19 
20 class CFGAS_GEFont;
21 class CFX_CSSComputedStyle;
22 class CFX_CSSStyleSelector;
23 class CFX_CSSStyleSheet;
24 class CFX_XMLNode;
25 class CXFA_FFDoc;
26 class CXFA_TextParseContext;
27 class CXFA_TextProvider;
28 class CXFA_TextTabstopsContext;
29 
30 class CXFA_TextParser {
31  public:
32   CXFA_TextParser();
33   virtual ~CXFA_TextParser();
34 
35   void Reset();
36   void DoParse(const CFX_XMLNode* pXMLContainer,
37                CXFA_TextProvider* pTextProvider);
38 
39   RetainPtr<CFX_CSSComputedStyle> CreateRootStyle(
40       CXFA_TextProvider* pTextProvider);
41   RetainPtr<CFX_CSSComputedStyle> ComputeStyle(
42       const CFX_XMLNode* pXMLNode,
43       CFX_CSSComputedStyle* pParentStyle);
44 
IsParsed()45   bool IsParsed() const { return m_bParsed; }
46 
47   XFA_AttributeValue GetVAlign(CXFA_TextProvider* pTextProvider) const;
48 
49   float GetTabInterval(CFX_CSSComputedStyle* pStyle) const;
50   int32_t CountTabs(CFX_CSSComputedStyle* pStyle) const;
51 
52   bool IsSpaceRun(CFX_CSSComputedStyle* pStyle) const;
53   bool GetTabstops(CFX_CSSComputedStyle* pStyle,
54                    CXFA_TextTabstopsContext* pTabstopContext);
55 
56   RetainPtr<CFGAS_GEFont> GetFont(CXFA_FFDoc* doc,
57                                   CXFA_TextProvider* pTextProvider,
58                                   CFX_CSSComputedStyle* pStyle) const;
59   float GetFontSize(CXFA_TextProvider* pTextProvider,
60                     CFX_CSSComputedStyle* pStyle) const;
61 
62   int32_t GetHorScale(CXFA_TextProvider* pTextProvider,
63                       CFX_CSSComputedStyle* pStyle,
64                       const CFX_XMLNode* pXMLNode) const;
65   int32_t GetVerScale(CXFA_TextProvider* pTextProvider,
66                       CFX_CSSComputedStyle* pStyle) const;
67 
68   void GetUnderline(CXFA_TextProvider* pTextProvider,
69                     CFX_CSSComputedStyle* pStyle,
70                     int32_t& iUnderline,
71                     XFA_AttributeValue& iPeriod) const;
72   void GetLinethrough(CXFA_TextProvider* pTextProvider,
73                       CFX_CSSComputedStyle* pStyle,
74                       int32_t& iLinethrough) const;
75   FX_ARGB GetColor(CXFA_TextProvider* pTextProvider,
76                    CFX_CSSComputedStyle* pStyle) const;
77   float GetBaseline(CXFA_TextProvider* pTextProvider,
78                     CFX_CSSComputedStyle* pStyle) const;
79   float GetLineHeight(CXFA_TextProvider* pTextProvider,
80                       CFX_CSSComputedStyle* pStyle,
81                       bool bFirst,
82                       float fVerScale) const;
83 
84   Optional<WideString> GetEmbeddedObj(const CXFA_TextProvider* pTextProvider,
85                                       const CFX_XMLNode* pXMLNode);
86   CXFA_TextParseContext* GetParseContextFromMap(const CFX_XMLNode* pXMLNode);
87 
88  protected:
89   bool TagValidate(const WideString& str) const;
90 
91  private:
92   class TagProvider {
93    public:
94     TagProvider();
95     ~TagProvider();
96 
GetTagName()97     WideString GetTagName() { return m_wsTagName; }
98 
SetTagName(const WideString & wsName)99     void SetTagName(const WideString& wsName) { m_wsTagName = wsName; }
SetAttribute(const WideString & wsAttr,const WideString & wsValue)100     void SetAttribute(const WideString& wsAttr, const WideString& wsValue) {
101       m_Attributes.insert({wsAttr, wsValue});
102     }
103 
GetAttribute(const WideString & wsAttr)104     WideString GetAttribute(const WideString& wsAttr) {
105       return m_Attributes[wsAttr];
106     }
107 
108     bool m_bTagAvailable;
109     bool m_bContent;
110 
111    private:
112     WideString m_wsTagName;
113     std::map<WideString, WideString> m_Attributes;
114   };
115 
116   // static
117   std::unique_ptr<TagProvider> ParseTagInfo(const CFX_XMLNode* pXMLNode);
118 
119   void InitCSSData(CXFA_TextProvider* pTextProvider);
120   void ParseRichText(const CFX_XMLNode* pXMLNode,
121                      CFX_CSSComputedStyle* pParentStyle);
122   std::unique_ptr<CFX_CSSStyleSheet> LoadDefaultSheetStyle();
123   RetainPtr<CFX_CSSComputedStyle> CreateStyle(
124       CFX_CSSComputedStyle* pParentStyle);
125 
126   bool m_bParsed;
127   bool m_cssInitialized;
128   std::unique_ptr<CFX_CSSStyleSelector> m_pSelector;
129   std::map<const CFX_XMLNode*, std::unique_ptr<CXFA_TextParseContext>>
130       m_mapXMLNodeToParseContext;
131 };
132 
133 #endif  // XFA_FXFA_CXFA_TEXTPARSER_H_
134