Revert "FX_BOOL considered harmful, part 2."
[pdfium.git] / fpdfsdk / src / fpdftext.cpp
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 #include "../../core/include/fpdfdoc/fpdf_doc.h"
8 #include "../../core/include/fpdftext/fpdf_text.h"
9 #include "../../public/fpdf_text.h"
10
11 #ifdef _WIN32
12 #include <tchar.h>
13 #endif
14
15         // jabdelmalek: commented out to build on Linux.  Not used.
16         // extern HANDLE g_hModule;
17
18 DLLEXPORT FPDF_TEXTPAGE STDCALL FPDFText_LoadPage(FPDF_PAGE page)
19 {
20         if (!page) return NULL;
21         IPDF_TextPage* textpage=NULL;
22         CPDF_ViewerPreferences viewRef(((CPDF_Page*)page)->m_pDocument);
23         textpage=IPDF_TextPage::CreateTextPage((CPDF_Page*)page,viewRef.IsDirectionR2L());
24         textpage->ParseTextPage();
25         return textpage;
26 }
27 DLLEXPORT void STDCALL FPDFText_ClosePage(FPDF_TEXTPAGE text_page)
28 {
29     delete (IPDF_TextPage*)text_page;
30 }
31 DLLEXPORT int STDCALL FPDFText_CountChars(FPDF_TEXTPAGE text_page)
32 {
33         if (!text_page) return  -1;
34         IPDF_TextPage* textpage=(IPDF_TextPage*)text_page;
35         return  textpage->CountChars();
36 }
37 DLLEXPORT unsigned int STDCALL FPDFText_GetUnicode(FPDF_TEXTPAGE text_page, int index)
38 {
39         if (!text_page) return -1;
40         IPDF_TextPage* textpage=(IPDF_TextPage*)text_page;
41
42         if (index<0 || index>=textpage->CountChars()) return 0;
43
44         FPDF_CHAR_INFO  charinfo;
45         textpage->GetCharInfo(index,charinfo);
46         return charinfo.m_Unicode;
47 }
48 DLLEXPORT double STDCALL FPDFText_GetFontSize(FPDF_TEXTPAGE text_page, int index)
49 {
50         if (!text_page) return 0;
51         IPDF_TextPage* textpage=(IPDF_TextPage*)text_page;
52
53         if (index<0 || index>=textpage->CountChars()) return 0;
54
55         FPDF_CHAR_INFO  charinfo;
56         textpage->GetCharInfo(index,charinfo);
57         return charinfo.m_FontSize;
58 }
59
60 DLLEXPORT void STDCALL FPDFText_GetCharBox(FPDF_TEXTPAGE text_page, int index,double* left,
61                                                                                                         double* right, double* bottom, double* top)
62 {
63         if (!text_page) return;
64         IPDF_TextPage* textpage=(IPDF_TextPage*)text_page;
65
66         if (index<0 || index>=textpage->CountChars()) return ;
67         FPDF_CHAR_INFO  charinfo;
68         textpage->GetCharInfo(index,charinfo);
69         *left=charinfo.m_CharBox.left;
70         *right=charinfo.m_CharBox.right;
71         *bottom=charinfo.m_CharBox.bottom;
72         *top=charinfo.m_CharBox.top;
73 }
74
75 //select
76 DLLEXPORT int STDCALL FPDFText_GetCharIndexAtPos(FPDF_TEXTPAGE text_page,double x,double y,double xTorelance,double yTorelance)
77 {
78         if (!text_page) return -3;
79         IPDF_TextPage* textpage=(IPDF_TextPage*)text_page;
80         return textpage->GetIndexAtPos((FX_FLOAT)x,(FX_FLOAT)y,(FX_FLOAT)xTorelance,(FX_FLOAT)yTorelance);
81 }
82
83 DLLEXPORT int STDCALL FPDFText_GetText(FPDF_TEXTPAGE text_page,int start,int count,unsigned short* result)
84 {
85         if (!text_page) return 0;
86         IPDF_TextPage* textpage=(IPDF_TextPage*)text_page;
87
88         if (start>=textpage->CountChars()) return 0;
89
90         CFX_WideString str=textpage->GetPageText(start,count);
91         if(str.GetLength()>count)
92                 str = str.Left(count);
93
94         CFX_ByteString cbUTF16str = str.UTF16LE_Encode();
95         FXSYS_memcpy(result,cbUTF16str.GetBuffer(cbUTF16str.GetLength()),cbUTF16str.GetLength());
96         cbUTF16str.ReleaseBuffer(cbUTF16str.GetLength());
97
98         return cbUTF16str.GetLength()/sizeof(unsigned short);
99 }
100
101 DLLEXPORT int STDCALL FPDFText_CountRects(FPDF_TEXTPAGE text_page,int start,int count)
102 {
103         if (!text_page) return 0;
104         IPDF_TextPage* textpage=(IPDF_TextPage*)text_page;
105         return  textpage->CountRects(start,count);
106
107 }
108 DLLEXPORT void STDCALL FPDFText_GetRect(FPDF_TEXTPAGE text_page,int rect_index, double* left,double*  top,
109                                                                                 double*  right, double*  bottom)
110 {
111         if (!text_page) return;
112         IPDF_TextPage* textpage=(IPDF_TextPage*)text_page;
113         CFX_FloatRect   rect;
114         textpage->GetRect(rect_index,rect.left,rect.top,rect.right,rect.bottom);
115         *left=rect.left;
116         *top=rect.top;
117         *right=rect.right;
118         *bottom=rect.bottom;
119 }
120
121 DLLEXPORT int STDCALL FPDFText_GetBoundedText(FPDF_TEXTPAGE text_page,double left, double top,
122                                                                                           double right, double bottom,unsigned short* buffer,int buflen)
123 {
124         if (!text_page) return 0;
125         IPDF_TextPage* textpage=(IPDF_TextPage*)text_page;
126         CFX_FloatRect rect((FX_FLOAT)left,(FX_FLOAT)bottom,(FX_FLOAT)right,(FX_FLOAT)top);
127         CFX_WideString str=textpage->GetTextByRect(rect);
128
129         if (buflen<=0 || buffer==NULL)
130         {
131                 return str.GetLength();
132         }
133
134         CFX_ByteString cbUTF16Str = str.UTF16LE_Encode();
135         int len = cbUTF16Str.GetLength()/sizeof(unsigned short);
136         int size = buflen > len ? len : buflen;
137         FXSYS_memcpy(buffer,cbUTF16Str.GetBuffer(size*sizeof(unsigned short)),size*sizeof(unsigned short));
138         cbUTF16Str.ReleaseBuffer(size*sizeof(unsigned short));
139
140         return size;
141
142 }
143
144 //Search
145 //-1 for end
146 DLLEXPORT FPDF_SCHHANDLE STDCALL FPDFText_FindStart(FPDF_TEXTPAGE text_page,FPDF_WIDESTRING findwhat,unsigned long flags,int start_index)
147 {
148         if (!text_page) return NULL;
149         IPDF_TextPageFind* textpageFind=NULL;
150         textpageFind=IPDF_TextPageFind::CreatePageFind((IPDF_TextPage*)text_page);
151         FX_STRSIZE len = CFX_WideString::WStringLength(findwhat);
152         textpageFind->FindFirst(CFX_WideString::FromUTF16LE(findwhat, len),flags,start_index);
153         return textpageFind;
154 }
155 DLLEXPORT FPDF_BOOL     STDCALL FPDFText_FindNext(FPDF_SCHHANDLE handle)
156 {
157         if (!handle) return FALSE;
158         IPDF_TextPageFind* textpageFind=(IPDF_TextPageFind*)handle;
159         return  textpageFind->FindNext();
160 }
161 DLLEXPORT FPDF_BOOL STDCALL FPDFText_FindPrev(FPDF_SCHHANDLE handle)
162 {
163         if (!handle) return FALSE;
164         IPDF_TextPageFind* textpageFind=(IPDF_TextPageFind*)handle;
165         return  textpageFind->FindPrev();
166 }
167 DLLEXPORT int STDCALL FPDFText_GetSchResultIndex(FPDF_SCHHANDLE handle)
168 {
169         if (!handle) return 0;
170         IPDF_TextPageFind* textpageFind=(IPDF_TextPageFind*)handle;
171         return textpageFind->GetCurOrder();
172 }
173 DLLEXPORT int STDCALL FPDFText_GetSchCount(FPDF_SCHHANDLE handle)
174 {
175         if (!handle) return 0;
176         IPDF_TextPageFind* textpageFind=(IPDF_TextPageFind*)handle;
177         return textpageFind->GetMatchedCount();
178 }
179 DLLEXPORT void STDCALL FPDFText_FindClose(FPDF_SCHHANDLE handle)
180 {
181         if (!handle) return;
182         IPDF_TextPageFind* textpageFind=(IPDF_TextPageFind*)handle;
183         delete  textpageFind;
184         handle=NULL;
185 }
186
187 //web link
188 DLLEXPORT FPDF_PAGELINK STDCALL FPDFLink_LoadWebLinks(FPDF_TEXTPAGE text_page)
189 {
190         if (!text_page) return NULL;
191         IPDF_LinkExtract* pageLink=NULL;
192         pageLink=IPDF_LinkExtract::CreateLinkExtract();
193         pageLink->ExtractLinks((IPDF_TextPage*)text_page);
194         return pageLink;
195 }
196 DLLEXPORT int STDCALL FPDFLink_CountWebLinks(FPDF_PAGELINK link_page)
197 {
198         if (!link_page) return 0;
199         IPDF_LinkExtract* pageLink=(IPDF_LinkExtract*)link_page;
200         return  pageLink->CountLinks();
201 }
202 DLLEXPORT int STDCALL FPDFLink_GetURL(FPDF_PAGELINK link_page,int link_index, unsigned short* buffer,int buflen)
203 {
204         if (!link_page) return 0;
205         IPDF_LinkExtract* pageLink=(IPDF_LinkExtract*)link_page;
206         CFX_WideString url=pageLink->GetURL(link_index);
207
208         CFX_ByteString cbUTF16URL = url.UTF16LE_Encode();
209         int len= cbUTF16URL.GetLength()/sizeof(unsigned short);
210         if (buffer==NULL || buflen<=0)
211                 return len;
212         int size=len<buflen ? len :buflen;
213         if (size>0)
214         {
215                 FXSYS_memcpy(buffer,cbUTF16URL.GetBuffer(size*sizeof(unsigned short)),size*sizeof(unsigned short));
216                 cbUTF16URL.ReleaseBuffer(size*sizeof(unsigned short));
217         }
218         return size;
219 }
220 DLLEXPORT int STDCALL FPDFLink_CountRects(FPDF_PAGELINK link_page,int link_index)
221 {
222         if (!link_page) return 0;
223         IPDF_LinkExtract* pageLink=(IPDF_LinkExtract*)link_page;
224         CFX_RectArray rectArray;
225         pageLink->GetRects(link_index,rectArray);
226         return rectArray.GetSize();
227 }
228 DLLEXPORT void STDCALL FPDFLink_GetRect(FPDF_PAGELINK link_page,int link_index,  int rect_index, double* left,
229                                                                                 double*  top,double*  right, double*  bottom)
230 {
231         if (!link_page) return;
232         IPDF_LinkExtract* pageLink=(IPDF_LinkExtract*)link_page;
233         CFX_RectArray rectArray;
234         pageLink->GetRects(link_index,rectArray);
235         if (rect_index >= 0 && rect_index < rectArray.GetSize()) {
236                 CFX_FloatRect rect=rectArray.GetAt(rect_index);
237                 *left=rect.left;
238                 *right=rect.right;
239                 *top=rect.top;
240                 *bottom=rect.bottom;
241         }
242 }
243 DLLEXPORT void  STDCALL FPDFLink_CloseWebLinks(FPDF_PAGELINK link_page)
244 {
245     delete (IPDF_LinkExtract*)link_page;
246 }
247