• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 The PDFium Authors
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 <stdint.h>
11 
12 #include <map>
13 #include <memory>
14 #include <vector>
15 
16 #include "core/fxcrt/css/cfx_css.h"
17 #include "core/fxcrt/css/cfx_csscomputedstyle.h"
18 #include "core/fxcrt/css/cfx_cssdeclaration.h"
19 #include "core/fxcrt/retain_ptr.h"
20 #include "core/fxcrt/widestring.h"
21 #include "core/fxge/dib/fx_dib.h"
22 #include "fxjs/gc/heap.h"
23 #include "third_party/abseil-cpp/absl/types/optional.h"
24 #include "v8/include/cppgc/garbage-collected.h"
25 #include "xfa/fxfa/fxfa_basic.h"
26 
27 class CFGAS_GEFont;
28 class CFX_CSSStyleSelector;
29 class CFX_CSSStyleSheet;
30 class CFX_XMLNode;
31 class CXFA_FFDoc;
32 class CXFA_TextProvider;
33 class CXFA_TextTabstopsContext;
34 
35 class CXFA_TextParser : public cppgc::GarbageCollected<CXFA_TextParser> {
36  public:
37   class Context {
38    public:
39     Context();
40     ~Context();
41 
42     void SetParentStyle(RetainPtr<const CFX_CSSComputedStyle> style);
GetParentStyle()43     RetainPtr<const CFX_CSSComputedStyle> GetParentStyle() const {
44       return m_pParentStyle;
45     }
46 
SetDisplay(CFX_CSSDisplay eDisplay)47     void SetDisplay(CFX_CSSDisplay eDisplay) { m_eDisplay = eDisplay; }
GetDisplay()48     CFX_CSSDisplay GetDisplay() const { return m_eDisplay; }
49 
50     void SetDecls(std::vector<const CFX_CSSDeclaration*>&& decl);
GetDecls()51     const std::vector<const CFX_CSSDeclaration*>& GetDecls() const {
52       return decls_;
53     }
54 
55    private:
56     RetainPtr<const CFX_CSSComputedStyle> m_pParentStyle;
57     CFX_CSSDisplay m_eDisplay = CFX_CSSDisplay::None;
58     std::vector<const CFX_CSSDeclaration*> decls_;
59   };
60 
61   CONSTRUCT_VIA_MAKE_GARBAGE_COLLECTED;
62   virtual ~CXFA_TextParser();
63 
Trace(cppgc::Visitor * visitor)64   void Trace(cppgc::Visitor* visitor) const {}
65 
66   void Reset();
67   void DoParse(const CFX_XMLNode* pXMLContainer,
68                CXFA_TextProvider* pTextProvider);
69 
70   RetainPtr<CFX_CSSComputedStyle> CreateRootStyle(
71       CXFA_TextProvider* pTextProvider);
72   RetainPtr<CFX_CSSComputedStyle> ComputeStyle(
73       const CFX_XMLNode* pXMLNode,
74       RetainPtr<const CFX_CSSComputedStyle> pParentStyle);
75 
IsParsed()76   bool IsParsed() const { return m_bParsed; }
77 
78   XFA_AttributeValue GetVAlign(CXFA_TextProvider* pTextProvider) const;
79 
80   float GetTabInterval(const CFX_CSSComputedStyle* pStyle) const;
81   int32_t CountTabs(const CFX_CSSComputedStyle* pStyle) const;
82 
83   bool IsSpaceRun(const CFX_CSSComputedStyle* pStyle) const;
84   bool GetTabstops(const CFX_CSSComputedStyle* pStyle,
85                    CXFA_TextTabstopsContext* pTabstopContext);
86 
87   RetainPtr<CFGAS_GEFont> GetFont(CXFA_FFDoc* doc,
88                                   CXFA_TextProvider* pTextProvider,
89                                   const CFX_CSSComputedStyle* pStyle) const;
90   float GetFontSize(CXFA_TextProvider* pTextProvider,
91                     const CFX_CSSComputedStyle* pStyle) const;
92   int32_t GetHorScale(CXFA_TextProvider* pTextProvider,
93                       const CFX_CSSComputedStyle* pStyle,
94                       const CFX_XMLNode* pXMLNode) const;
95   int32_t GetVerScale(CXFA_TextProvider* pTextProvider,
96                       const CFX_CSSComputedStyle* pStyle) const;
97   int32_t GetUnderline(CXFA_TextProvider* pTextProvider,
98                        const CFX_CSSComputedStyle* pStyle) const;
99   XFA_AttributeValue GetUnderlinePeriod(
100       CXFA_TextProvider* pTextProvider,
101       const CFX_CSSComputedStyle* pStyle) const;
102   int32_t GetLinethrough(CXFA_TextProvider* pTextProvider,
103                          const CFX_CSSComputedStyle* pStyle) const;
104   FX_ARGB GetColor(CXFA_TextProvider* pTextProvider,
105                    const CFX_CSSComputedStyle* pStyle) const;
106   float GetBaseline(CXFA_TextProvider* pTextProvider,
107                     const CFX_CSSComputedStyle* pStyle) const;
108   float GetLineHeight(CXFA_TextProvider* pTextProvider,
109                       const CFX_CSSComputedStyle* pStyle,
110                       bool bFirst,
111                       float fVerScale) const;
112 
113   absl::optional<WideString> GetEmbeddedObj(
114       const CXFA_TextProvider* pTextProvider,
115       const CFX_XMLNode* pXMLNode);
116   Context* GetParseContextFromMap(const CFX_XMLNode* pXMLNode);
117 
118  protected:
119   CXFA_TextParser();
120 
121   bool TagValidate(const WideString& str) const;
122 
123  private:
124   class TagProvider {
125    public:
126     TagProvider();
127     ~TagProvider();
128 
GetTagName()129     WideString GetTagName() { return m_wsTagName; }
130 
SetTagName(const WideString & wsName)131     void SetTagName(const WideString& wsName) { m_wsTagName = wsName; }
SetAttribute(const WideString & wsAttr,const WideString & wsValue)132     void SetAttribute(const WideString& wsAttr, const WideString& wsValue) {
133       m_Attributes.insert({wsAttr, wsValue});
134     }
135 
GetAttribute(const WideString & wsAttr)136     WideString GetAttribute(const WideString& wsAttr) {
137       return m_Attributes[wsAttr];
138     }
139 
140     bool m_bTagAvailable = false;
141     bool m_bContent = false;
142 
143    private:
144     WideString m_wsTagName;
145     std::map<WideString, WideString> m_Attributes;
146   };
147 
148   // static
149   std::unique_ptr<TagProvider> ParseTagInfo(const CFX_XMLNode* pXMLNode);
150 
151   void InitCSSData(CXFA_TextProvider* pTextProvider);
152   void ParseRichText(const CFX_XMLNode* pXMLNode,
153                      const CFX_CSSComputedStyle* pParentStyle);
154   std::unique_ptr<CFX_CSSStyleSheet> LoadDefaultSheetStyle();
155   RetainPtr<CFX_CSSComputedStyle> CreateStyle(
156       const CFX_CSSComputedStyle* pParentStyle);
157 
158   bool m_bParsed = false;
159   bool m_cssInitialized = false;
160   std::unique_ptr<CFX_CSSStyleSelector> m_pSelector;
161   std::map<const CFX_XMLNode*, std::unique_ptr<Context>>
162       m_mapXMLNodeToParseContext;
163 };
164 
165 #endif  // XFA_FXFA_CXFA_TEXTPARSER_H_
166