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_APP_CXFA_TEXTPARSER_H_
8 #define XFA_FXFA_APP_CXFA_TEXTPARSER_H_
9 
10 #include <map>
11 #include <memory>
12 
13 #include "core/fxcrt/cfx_retain_ptr.h"
14 #include "core/fxcrt/fx_string.h"
15 #include "core/fxcrt/fx_system.h"
16 #include "core/fxge/fx_dib.h"
17 #include "xfa/fgas/font/cfgas_gefont.h"
18 
19 class CFDE_CSSComputedStyle;
20 class CFDE_CSSStyleSelector;
21 class CFDE_CSSStyleSheet;
22 class CFDE_XMLNode;
23 class CXFA_CSSTagProvider;
24 class CXFA_TextParseContext;
25 class CXFA_TextProvider;
26 class CXFA_TextTabstopsContext;
27 
28 class CXFA_TextParser {
29  public:
30   CXFA_TextParser();
31   virtual ~CXFA_TextParser();
32 
33   void Reset();
34   void DoParse(CFDE_XMLNode* pXMLContainer, CXFA_TextProvider* pTextProvider);
35 
36   CFX_RetainPtr<CFDE_CSSComputedStyle> CreateRootStyle(
37       CXFA_TextProvider* pTextProvider);
38   CFX_RetainPtr<CFDE_CSSComputedStyle> ComputeStyle(
39       CFDE_XMLNode* pXMLNode,
40       CFDE_CSSComputedStyle* pParentStyle);
41 
IsParsed()42   bool IsParsed() const { return m_bParsed; }
43 
44   int32_t GetVAlign(CXFA_TextProvider* pTextProvider) const;
45 
46   FX_FLOAT GetTabInterval(CFDE_CSSComputedStyle* pStyle) const;
47   int32_t CountTabs(CFDE_CSSComputedStyle* pStyle) const;
48 
49   bool IsSpaceRun(CFDE_CSSComputedStyle* pStyle) const;
50   bool GetTabstops(CFDE_CSSComputedStyle* pStyle,
51                    CXFA_TextTabstopsContext* pTabstopContext);
52 
53   CFX_RetainPtr<CFGAS_GEFont> GetFont(CXFA_TextProvider* pTextProvider,
54                                       CFDE_CSSComputedStyle* pStyle) const;
55   FX_FLOAT GetFontSize(CXFA_TextProvider* pTextProvider,
56                        CFDE_CSSComputedStyle* pStyle) const;
57 
58   int32_t GetHorScale(CXFA_TextProvider* pTextProvider,
59                       CFDE_CSSComputedStyle* pStyle,
60                       CFDE_XMLNode* pXMLNode) const;
61   int32_t GetVerScale(CXFA_TextProvider* pTextProvider,
62                       CFDE_CSSComputedStyle* pStyle) const;
63 
64   void GetUnderline(CXFA_TextProvider* pTextProvider,
65                     CFDE_CSSComputedStyle* pStyle,
66                     int32_t& iUnderline,
67                     int32_t& iPeriod) const;
68   void GetLinethrough(CXFA_TextProvider* pTextProvider,
69                       CFDE_CSSComputedStyle* pStyle,
70                       int32_t& iLinethrough) const;
71   FX_ARGB GetColor(CXFA_TextProvider* pTextProvider,
72                    CFDE_CSSComputedStyle* pStyle) const;
73   FX_FLOAT GetBaseline(CXFA_TextProvider* pTextProvider,
74                        CFDE_CSSComputedStyle* pStyle) const;
75   FX_FLOAT GetLineHeight(CXFA_TextProvider* pTextProvider,
76                          CFDE_CSSComputedStyle* pStyle,
77                          bool bFirst,
78                          FX_FLOAT fVerScale) const;
79 
80   bool GetEmbbedObj(CXFA_TextProvider* pTextProvider,
81                     CFDE_XMLNode* pXMLNode,
82                     CFX_WideString& wsValue);
83   CXFA_TextParseContext* GetParseContextFromMap(CFDE_XMLNode* pXMLNode);
84 
85  protected:
86   bool TagValidate(const CFX_WideString& str) const;
87 
88  private:
89   void InitCSSData(CXFA_TextProvider* pTextProvider);
90   void ParseRichText(CFDE_XMLNode* pXMLNode,
91                      CFDE_CSSComputedStyle* pParentStyle);
92   std::unique_ptr<CXFA_CSSTagProvider> ParseTagInfo(CFDE_XMLNode* pXMLNode);
93   std::unique_ptr<CFDE_CSSStyleSheet> LoadDefaultSheetStyle();
94   CFX_RetainPtr<CFDE_CSSComputedStyle> CreateStyle(
95       CFDE_CSSComputedStyle* pParentStyle);
96 
97   std::unique_ptr<CFDE_CSSStyleSelector> m_pSelector;
98   std::map<CFDE_XMLNode*, CXFA_TextParseContext*> m_mapXMLNodeToParseContext;
99   bool m_bParsed;
100   bool m_cssInitialized;
101 };
102 
103 #endif  // XFA_FXFA_APP_CXFA_TEXTPARSER_H_
104