Revert "Fix else-after-returns throughout pdfium."
[pdfium.git] / core / src / fpdftext / text_int.h
1 // 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 CORE_SRC_FPDFTEXT_TEXT_INT_H_
8 #define CORE_SRC_FPDFTEXT_TEXT_INT_H_
9
10 class CPDF_TextPage;
11 class CPDF_LinkExtract;
12 class CPDF_TextPageFind;
13 class CPDF_DocProgressiveSearch;
14 #define FPDFTEXT_CHAR_ERROR                     -1
15 #define FPDFTEXT_CHAR_NORMAL            0
16 #define FPDFTEXT_CHAR_GENERATED         1
17 #define FPDFTEXT_CHAR_UNUNICODE         2
18 #define FPDFTEXT_CHAR_HYPHEN            3
19 #define FPDFTEXT_CHAR_PIECE                     4
20 #define FPDFTEXT_MC_PASS                        0
21 #define FPDFTEXT_MC_DONE                        1
22 #define FPDFTEXT_MC_DELAY                       2
23 typedef struct _PAGECHAR_INFO {
24     int                                 m_CharCode;
25     FX_WCHAR                    m_Unicode;
26     FX_FLOAT                    m_OriginX;
27     FX_FLOAT                    m_OriginY;
28     int32_t                     m_Flag;
29     CFX_FloatRect               m_CharBox;
30     CPDF_TextObject*    m_pTextObj;
31     CFX_AffineMatrix    m_Matrix;
32     int                                 m_Index;
33 } PAGECHAR_INFO;
34 typedef CFX_SegmentedArray<PAGECHAR_INFO> PAGECHAR_InfoArray;
35 typedef struct {
36     int m_Start;
37     int m_nCount;
38 } FPDF_SEGMENT;
39 typedef CFX_ArrayTemplate<FPDF_SEGMENT> SEGMENT_Array;
40 typedef struct {
41     CPDF_TextObject*    m_pTextObj;
42     CFX_AffineMatrix    m_formMatrix;
43 } PDFTEXT_Obj;
44 typedef CFX_ArrayTemplate<PDFTEXT_Obj> LINEOBJ;
45 class CPDF_TextPage: public IPDF_TextPage
46 {
47 public:
48     CPDF_TextPage(const CPDF_Page* pPage, int flags = 0);
49     CPDF_TextPage(const CPDF_PageObjects* pPage, int flags = 0);
50     CPDF_TextPage(const CPDF_Page* pPage, CPDFText_ParseOptions ParserOptions);
51     virtual FX_BOOL                                     ParseTextPage();
52     virtual void                                        NormalizeObjects(FX_BOOL bNormalize);
53     virtual     FX_BOOL                                 IsParsered() const
54     {
55         return m_IsParsered;
56     }
57     virtual ~CPDF_TextPage() {};
58 public:
59     virtual int CharIndexFromTextIndex(int TextIndex)const ;
60     virtual int TextIndexFromCharIndex(int CharIndex)const;
61     virtual int                                         CountChars() const;
62     virtual     void                                    GetCharInfo(int index, FPDF_CHAR_INFO & info) const;
63     virtual void                                        GetRectArray(int start, int nCount, CFX_RectArray& rectArray) const;
64     virtual int                                         GetIndexAtPos(CPDF_Point point, FX_FLOAT xTorelance, FX_FLOAT yTorelance) const;
65     virtual int                                         GetIndexAtPos(FX_FLOAT x, FX_FLOAT y, FX_FLOAT xTorelance,
66             FX_FLOAT yTorelance) const;
67     virtual CFX_WideString                      GetTextByRect(const CFX_FloatRect& rect) const;
68     virtual void                                        GetRectsArrayByRect(const CFX_FloatRect& rect, CFX_RectArray& resRectArray) const;
69     virtual     int                                             GetOrderByDirection(int order, int direction) const;
70     virtual     CFX_WideString                  GetPageText(int start = 0, int nCount = -1) const;
71
72     virtual int                                         CountRects(int start, int nCount);
73     virtual     void                                    GetRect(int rectIndex, FX_FLOAT& left, FX_FLOAT& top
74                                             , FX_FLOAT& right, FX_FLOAT &bottom) const;
75     virtual FX_BOOL                                     GetBaselineRotate(int rectIndex, int& Rotate);
76     virtual FX_BOOL                                     GetBaselineRotate(const CFX_FloatRect& rect, int& Rotate);
77     virtual     int                                             CountBoundedSegments(FX_FLOAT left, FX_FLOAT top,
78             FX_FLOAT right, FX_FLOAT bottom, FX_BOOL bContains = FALSE);
79     virtual     void                                    GetBoundedSegment(int index, int& start, int& count) const;
80     virtual int                                         GetWordBreak(int index, int direction) const;
81 public:
82     const       PAGECHAR_InfoArray*             GetCharList() const
83     {
84         return &m_charList;
85     }
86     static      FX_BOOL                                 IsRectIntersect(const CFX_FloatRect& rect1, const CFX_FloatRect& rect2);
87     static      FX_BOOL                                 IsLetter(FX_WCHAR unicode);
88 private:
89     FX_BOOL                                                     IsHyphen(FX_WCHAR curChar);
90     FX_BOOL                                                     IsControlChar(PAGECHAR_INFO* pCharInfo);
91     FX_BOOL                                                     GetBaselineRotate(int start, int end, int& Rotate);
92     void                                                        ProcessObject();
93     void                                                        ProcessFormObject(CPDF_FormObject*      pFormObj, const CFX_AffineMatrix& formMatrix);
94     void                                                        ProcessTextObject(PDFTEXT_Obj pObj);
95     void                                                        ProcessTextObject(CPDF_TextObject*      pTextObj, const CFX_AffineMatrix& formMatrix, FX_POSITION ObjPos);
96     int                                                         ProcessInsertObject(const CPDF_TextObject* pObj, const CFX_AffineMatrix& formMatrix);
97     FX_BOOL                                                     GenerateCharInfo(FX_WCHAR unicode, PAGECHAR_INFO& info);
98     FX_BOOL                                                     IsSameAsPreTextObject(CPDF_TextObject* pTextObj, FX_POSITION ObjPos);
99     FX_BOOL                                                     IsSameTextObject(CPDF_TextObject* pTextObj1, CPDF_TextObject* pTextObj2);
100     int                                                         GetCharWidth(FX_DWORD charCode, CPDF_Font* pFont) const;
101     void                                                        CloseTempLine();
102     void                                                        OnPiece(IFX_BidiChar* pBidi, CFX_WideString& str);
103     int32_t     PreMarkedContent(PDFTEXT_Obj pObj);
104     void                ProcessMarkedContent(PDFTEXT_Obj pObj);
105     void                CheckMarkedContentObject(int32_t& start, int32_t& nCount) const;
106     void                FindPreviousTextObject(void);
107     void                AddCharInfoByLRDirection(CFX_WideString& str, int i);
108     void                AddCharInfoByRLDirection(CFX_WideString& str, int i);
109     int32_t     GetTextObjectWritingMode(const CPDF_TextObject* pTextObj);
110     int32_t     FindTextlineFlowDirection();
111     void SwapTempTextBuf(int32_t iCharListStartAppend,
112                          int32_t iBufStartAppend);
113     FX_BOOL IsRightToLeft(const CPDF_TextObject* pTextObj,
114                           const CPDF_Font* pFont,
115                           int nItems) const;
116 protected:
117     CPDFText_ParseOptions                       m_ParseOptions;
118     CFX_WordArray                                       m_CharIndex;
119     const CPDF_PageObjects*                     m_pPage;
120     PAGECHAR_InfoArray                          m_charList;
121     CFX_WideTextBuf                                     m_TextBuf;
122     PAGECHAR_InfoArray                          m_TempCharList;
123     CFX_WideTextBuf                                     m_TempTextBuf;
124     int                                                         m_parserflag;
125     CPDF_TextObject*                            m_pPreTextObj;
126     CFX_AffineMatrix                            m_perMatrix;
127     FX_BOOL                                                     m_IsParsered;
128     CFX_AffineMatrix                            m_DisplayMatrix;
129
130     SEGMENT_Array                                       m_Segment;
131     CFX_RectArray                                       m_SelRects;
132     LINEOBJ                                                     m_LineObj;
133     FX_BOOL                                                     m_TextlineDir;
134     CFX_FloatRect                                       m_CurlineRect;
135 };
136 class CPDF_TextPageFind: public IPDF_TextPageFind
137 {
138 public:
139     CPDF_TextPageFind(const IPDF_TextPage* pTextPage);
140     virtual                                                     ~CPDF_TextPageFind() {};
141 public:
142     virtual     FX_BOOL                                 FindFirst(const CFX_WideString& findwhat, int flags, int startPos = 0);
143     virtual     FX_BOOL                                 FindNext();
144     virtual     FX_BOOL                                 FindPrev();
145
146     virtual void                                        GetRectArray(CFX_RectArray& rects) const;
147     virtual int                                         GetCurOrder() const;
148     virtual int                                         GetMatchedCount()const;
149 protected:
150     void                                                        ExtractFindWhat(const CFX_WideString& findwhat);
151     FX_BOOL                                                     IsMatchWholeWord(const CFX_WideString& csPageText, int startPos, int endPos);
152     FX_BOOL                                                     ExtractSubString(CFX_WideString& rString, const FX_WCHAR* lpszFullString,
153             int iSubString, FX_WCHAR chSep);
154     CFX_WideString                                      MakeReverse(const CFX_WideString& str);
155     int                                                         ReverseFind(const CFX_WideString& csPageText, const CFX_WideString& csWord, int nStartPos, int& WordLength);
156     int                                                         GetCharIndex(int index) const;
157 private:
158     CFX_WordArray                                       m_CharIndex;
159     const IPDF_TextPage*                        m_pTextPage;
160     CFX_WideString                                      m_strText;
161     CFX_WideString                                      m_findWhat;
162     int                                                         m_flags;
163     CFX_WideStringArray                         m_csFindWhatArray;
164     int                                                         m_findNextStart;
165     int                                                         m_findPreStart;
166     FX_BOOL                                                     m_bMatchCase;
167     FX_BOOL                                                     m_bMatchWholeWord;
168     int                                                         m_resStart;
169     int                                                         m_resEnd;
170     CFX_RectArray                                       m_resArray;
171     FX_BOOL                                                     m_IsFind;
172 };
173 class CPDF_LinkExt
174 {
175 public:
176     CPDF_LinkExt() {};
177     int                                                         m_Start;
178     int                                                         m_Count;
179     CFX_WideString                                      m_strUrl;
180     virtual                                                     ~CPDF_LinkExt() {};
181 };
182 typedef CFX_ArrayTemplate<CPDF_LinkExt*> LINK_InfoArray;
183 class CPDF_LinkExtract: public IPDF_LinkExtract
184 {
185 public:
186     CPDF_LinkExtract();
187     virtual                                                     ~CPDF_LinkExtract();
188     virtual FX_BOOL                                     ExtractLinks(const IPDF_TextPage* pTextPage);
189     virtual     FX_BOOL                                 IsExtract() const
190     {
191         return m_IsParserd;
192     }
193 public:
194     virtual int                                         CountLinks() const;
195     virtual     CFX_WideString                  GetURL(int index) const;
196     virtual     void                                    GetBoundedSegment(int index, int& start, int& count) const;
197     virtual     void                                    GetRects(int index, CFX_RectArray& rects)const;
198 protected:
199     void                                                        parserLink();
200     void                                                        DeleteLinkList();
201     FX_BOOL                                                     CheckWebLink(CFX_WideString& strBeCheck);
202     FX_BOOL                                                     CheckMailLink(CFX_WideString& str);
203     FX_BOOL                                                     AppendToLinkList(int start, int count, const CFX_WideString& strUrl);
204 private:
205     LINK_InfoArray                                      m_LinkList;
206     const CPDF_TextPage*                        m_pTextPage;
207     CFX_WideString                                      m_strPageText;
208     FX_BOOL                                                     m_IsParserd;
209 };
210 FX_STRSIZE FX_Unicode_GetNormalization(FX_WCHAR wch, FX_WCHAR* pDst);
211 void NormalizeString(CFX_WideString& str);
212 void NormalizeCompositeChar(FX_WCHAR wChar, CFX_WideString& sDest);
213
214 #endif  // CORE_SRC_FPDFTEXT_TEXT_INT_H_