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