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