blob: acfc79238a3961f734102c8d1951d69c31976c37 [file] [log] [blame]
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001// 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.
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07004
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07005// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
Lei Zhange5b0bd12015-06-19 17:15:41 -07007#include "../../core/include/fpdfdoc/fpdf_doc.h"
8#include "../../core/include/fpdftext/fpdf_text.h"
Tom Sepez1ed8a212015-05-11 15:25:39 -07009#include "../../public/fpdf_text.h"
Bo Xufdc00a72014-10-28 23:03:33 -070010#include "../include/fpdfxfa/fpdfxfa_doc.h"
11#include "../include/fpdfxfa/fpdfxfa_page.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070012
13#ifdef _WIN32
14#include <tchar.h>
15#endif
16
Bo Xu8daab312014-07-14 12:13:53 -070017 // jabdelmalek: commented out to build on Linux. Not used.
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070018 // extern HANDLE g_hModule;
19
20DLLEXPORT FPDF_TEXTPAGE STDCALL FPDFText_LoadPage(FPDF_PAGE page)
21{
22 if (!page) return NULL;
23 IPDF_TextPage* textpage=NULL;
Bo Xufdc00a72014-10-28 23:03:33 -070024 CPDFXFA_Page* pPage = (CPDFXFA_Page*)page;
25 if (!pPage->GetPDFPage()) return NULL;
26 CPDFXFA_Document* pDoc = pPage->GetDocument();
27 CPDF_ViewerPreferences viewRef(pDoc->GetPDFDoc());
28 textpage=IPDF_TextPage::CreateTextPage((CPDF_Page*)pPage->GetPDFPage(),viewRef.IsDirectionR2L());
Bo Xud4e406e2014-08-13 11:03:19 -070029 textpage->ParseTextPage();
Bo Xufdc00a72014-10-28 23:03:33 -070030
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070031 return textpage;
32}
33DLLEXPORT void STDCALL FPDFText_ClosePage(FPDF_TEXTPAGE text_page)
34{
35 if (text_page){
36 IPDF_TextPage* textpage=(IPDF_TextPage*)text_page;
37 delete textpage;
38 text_page=NULL;
39 }
40}
41DLLEXPORT int STDCALL FPDFText_CountChars(FPDF_TEXTPAGE text_page)
42{
43 if (!text_page) return -1;
44 IPDF_TextPage* textpage=(IPDF_TextPage*)text_page;
45 return textpage->CountChars();
46}
47DLLEXPORT unsigned int STDCALL FPDFText_GetUnicode(FPDF_TEXTPAGE text_page, int index)
48{
49 if (!text_page) return -1;
50 IPDF_TextPage* textpage=(IPDF_TextPage*)text_page;
51
52 if (index<0 || index>=textpage->CountChars()) return 0;
53
54 FPDF_CHAR_INFO charinfo;
55 textpage->GetCharInfo(index,charinfo);
56 return charinfo.m_Unicode;
57}
58DLLEXPORT double STDCALL FPDFText_GetFontSize(FPDF_TEXTPAGE text_page, int index)
59{
60 if (!text_page) return 0;
61 IPDF_TextPage* textpage=(IPDF_TextPage*)text_page;
62
63 if (index<0 || index>=textpage->CountChars()) return 0;
64
65 FPDF_CHAR_INFO charinfo;
66 textpage->GetCharInfo(index,charinfo);
67 return charinfo.m_FontSize;
68}
69
70DLLEXPORT void STDCALL FPDFText_GetCharBox(FPDF_TEXTPAGE text_page, int index,double* left,
71 double* right, double* bottom, double* top)
72{
73 if (!text_page) return;
74 IPDF_TextPage* textpage=(IPDF_TextPage*)text_page;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070075
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070076 if (index<0 || index>=textpage->CountChars()) return ;
77 FPDF_CHAR_INFO charinfo;
78 textpage->GetCharInfo(index,charinfo);
79 *left=charinfo.m_CharBox.left;
80 *right=charinfo.m_CharBox.right;
81 *bottom=charinfo.m_CharBox.bottom;
82 *top=charinfo.m_CharBox.top;
83}
84
85//select
86DLLEXPORT int STDCALL FPDFText_GetCharIndexAtPos(FPDF_TEXTPAGE text_page,double x,double y,double xTorelance,double yTorelance)
87{
88 if (!text_page) return -3;
89 IPDF_TextPage* textpage=(IPDF_TextPage*)text_page;
90 return textpage->GetIndexAtPos((FX_FLOAT)x,(FX_FLOAT)y,(FX_FLOAT)xTorelance,(FX_FLOAT)yTorelance);
91}
92
93DLLEXPORT int STDCALL FPDFText_GetText(FPDF_TEXTPAGE text_page,int start,int count,unsigned short* result)
94{
95 if (!text_page) return 0;
96 IPDF_TextPage* textpage=(IPDF_TextPage*)text_page;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070097
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070098 if (start>=textpage->CountChars()) return 0;
99
100 CFX_WideString str=textpage->GetPageText(start,count);
101 if(str.GetLength()>count)
102 str = str.Left(count);
103
104 CFX_ByteString cbUTF16str = str.UTF16LE_Encode();
105 FXSYS_memcpy(result,cbUTF16str.GetBuffer(cbUTF16str.GetLength()),cbUTF16str.GetLength());
106 cbUTF16str.ReleaseBuffer(cbUTF16str.GetLength());
107
108 return cbUTF16str.GetLength()/sizeof(unsigned short);
109}
110
111DLLEXPORT int STDCALL FPDFText_CountRects(FPDF_TEXTPAGE text_page,int start,int count)
112{
113 if (!text_page) return 0;
114 IPDF_TextPage* textpage=(IPDF_TextPage*)text_page;
115 return textpage->CountRects(start,count);
116
117}
118DLLEXPORT void STDCALL FPDFText_GetRect(FPDF_TEXTPAGE text_page,int rect_index, double* left,double* top,
119 double* right, double* bottom)
120{
121 if (!text_page) return;
122 IPDF_TextPage* textpage=(IPDF_TextPage*)text_page;
123 CFX_FloatRect rect;
124 textpage->GetRect(rect_index,rect.left,rect.top,rect.right,rect.bottom);
125 *left=rect.left;
126 *top=rect.top;
127 *right=rect.right;
128 *bottom=rect.bottom;
129}
130
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700131DLLEXPORT int STDCALL FPDFText_GetBoundedText(FPDF_TEXTPAGE text_page,double left, double top,
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700132 double right, double bottom,unsigned short* buffer,int buflen)
133{
134 if (!text_page) return 0;
135 IPDF_TextPage* textpage=(IPDF_TextPage*)text_page;
136 CFX_FloatRect rect((FX_FLOAT)left,(FX_FLOAT)bottom,(FX_FLOAT)right,(FX_FLOAT)top);
137 CFX_WideString str=textpage->GetTextByRect(rect);
138
139 if (buflen<=0 || buffer==NULL)
140 {
141 return str.GetLength();
142 }
143
144 CFX_ByteString cbUTF16Str = str.UTF16LE_Encode();
145 int len = cbUTF16Str.GetLength()/sizeof(unsigned short);
146 int size = buflen > len ? len : buflen;
147 FXSYS_memcpy(buffer,cbUTF16Str.GetBuffer(size*sizeof(unsigned short)),size*sizeof(unsigned short));
148 cbUTF16Str.ReleaseBuffer(size*sizeof(unsigned short));
149
150 return size;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700151
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700152}
153
154//Search
155//-1 for end
156DLLEXPORT FPDF_SCHHANDLE STDCALL FPDFText_FindStart(FPDF_TEXTPAGE text_page,FPDF_WIDESTRING findwhat,unsigned long flags,int start_index)
157{
158 if (!text_page) return NULL;
159 IPDF_TextPageFind* textpageFind=NULL;
Bo Xud4e406e2014-08-13 11:03:19 -0700160 textpageFind=IPDF_TextPageFind::CreatePageFind((IPDF_TextPage*)text_page);
161 FX_STRSIZE len = CFX_WideString::WStringLength(findwhat);
162 textpageFind->FindFirst(CFX_WideString::FromUTF16LE(findwhat, len),flags,start_index);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700163 return textpageFind;
164}
165DLLEXPORT FPDF_BOOL STDCALL FPDFText_FindNext(FPDF_SCHHANDLE handle)
166{
167 if (!handle) return FALSE;
168 IPDF_TextPageFind* textpageFind=(IPDF_TextPageFind*)handle;
169 return textpageFind->FindNext();
170}
171DLLEXPORT FPDF_BOOL STDCALL FPDFText_FindPrev(FPDF_SCHHANDLE handle)
172{
173 if (!handle) return FALSE;
174 IPDF_TextPageFind* textpageFind=(IPDF_TextPageFind*)handle;
175 return textpageFind->FindPrev();
176}
177DLLEXPORT int STDCALL FPDFText_GetSchResultIndex(FPDF_SCHHANDLE handle)
178{
179 if (!handle) return 0;
180 IPDF_TextPageFind* textpageFind=(IPDF_TextPageFind*)handle;
181 return textpageFind->GetCurOrder();
182}
183DLLEXPORT int STDCALL FPDFText_GetSchCount(FPDF_SCHHANDLE handle)
184{
185 if (!handle) return 0;
186 IPDF_TextPageFind* textpageFind=(IPDF_TextPageFind*)handle;
187 return textpageFind->GetMatchedCount();
188}
189DLLEXPORT void STDCALL FPDFText_FindClose(FPDF_SCHHANDLE handle)
190{
191 if (!handle) return;
192 IPDF_TextPageFind* textpageFind=(IPDF_TextPageFind*)handle;
193 delete textpageFind;
194 handle=NULL;
195}
196
197//web link
198DLLEXPORT FPDF_PAGELINK STDCALL FPDFLink_LoadWebLinks(FPDF_TEXTPAGE text_page)
199{
200 if (!text_page) return NULL;
201 IPDF_LinkExtract* pageLink=NULL;
Bo Xud4e406e2014-08-13 11:03:19 -0700202 pageLink=IPDF_LinkExtract::CreateLinkExtract();
203 pageLink->ExtractLinks((IPDF_TextPage*)text_page);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700204 return pageLink;
205}
206DLLEXPORT int STDCALL FPDFLink_CountWebLinks(FPDF_PAGELINK link_page)
207{
208 if (!link_page) return 0;
209 IPDF_LinkExtract* pageLink=(IPDF_LinkExtract*)link_page;
210 return pageLink->CountLinks();
211}
212DLLEXPORT int STDCALL FPDFLink_GetURL(FPDF_PAGELINK link_page,int link_index, unsigned short* buffer,int buflen)
213{
214 if (!link_page) return 0;
215 IPDF_LinkExtract* pageLink=(IPDF_LinkExtract*)link_page;
216 CFX_WideString url=pageLink->GetURL(link_index);
217
218 CFX_ByteString cbUTF16URL = url.UTF16LE_Encode();
219 int len= cbUTF16URL.GetLength()/sizeof(unsigned short);
220 if (buffer==NULL || buflen<=0)
221 return len;
222 int size=len<buflen ? len :buflen;
223 if (size>0)
224 {
225 FXSYS_memcpy(buffer,cbUTF16URL.GetBuffer(size*sizeof(unsigned short)),size*sizeof(unsigned short));
226 cbUTF16URL.ReleaseBuffer(size*sizeof(unsigned short));
227 }
228 return size;
229}
230DLLEXPORT int STDCALL FPDFLink_CountRects(FPDF_PAGELINK link_page,int link_index)
231{
232 if (!link_page) return 0;
233 IPDF_LinkExtract* pageLink=(IPDF_LinkExtract*)link_page;
234 CFX_RectArray rectArray;
235 pageLink->GetRects(link_index,rectArray);
236 return rectArray.GetSize();
237}
238DLLEXPORT void STDCALL FPDFLink_GetRect(FPDF_PAGELINK link_page,int link_index, int rect_index, double* left,
239 double* top,double* right, double* bottom)
240{
241 if (!link_page) return;
242 IPDF_LinkExtract* pageLink=(IPDF_LinkExtract*)link_page;
243 CFX_RectArray rectArray;
244 pageLink->GetRects(link_index,rectArray);
Tom Sepez526f6d52015-01-28 15:49:13 -0800245 if (rect_index >= 0 && rect_index < rectArray.GetSize()) {
246 CFX_FloatRect rect=rectArray.GetAt(rect_index);
247 *left=rect.left;
248 *right=rect.right;
249 *top=rect.top;
250 *bottom=rect.bottom;
251 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700252}
253DLLEXPORT void STDCALL FPDFLink_CloseWebLinks(FPDF_PAGELINK link_page)
254{
255 if (!link_page) return;
256 IPDF_LinkExtract* pageLink=(IPDF_LinkExtract*)link_page;
257 delete pageLink;
258 pageLink =NULL;
259}
260