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