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.
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
7 #include "../include/fsdk_define.h"
8 #include "../include/fpdftext.h"
14 // jabdelmalek: commented out to build on Linux. Not used.
15 // extern HANDLE g_hModule;
17 DLLEXPORT FPDF_TEXTPAGE STDCALL FPDFText_LoadPage(FPDF_PAGE page)
19 if (!page) return NULL;
20 IPDF_TextPage* textpage=NULL;
23 CPDF_ViewerPreferences viewRef(((CPDF_Page*)page)->m_pDocument);
24 textpage=IPDF_TextPage::CreateTextPage((CPDF_Page*)page,viewRef.IsDirectionR2L());
25 textpage->ParseTextPage();
35 DLLEXPORT void STDCALL FPDFText_ClosePage(FPDF_TEXTPAGE text_page)
38 IPDF_TextPage* textpage=(IPDF_TextPage*)text_page;
43 DLLEXPORT int STDCALL FPDFText_CountChars(FPDF_TEXTPAGE text_page)
45 if (!text_page) return -1;
46 IPDF_TextPage* textpage=(IPDF_TextPage*)text_page;
47 return textpage->CountChars();
49 DLLEXPORT unsigned int STDCALL FPDFText_GetUnicode(FPDF_TEXTPAGE text_page, int index)
51 if (!text_page) return -1;
52 IPDF_TextPage* textpage=(IPDF_TextPage*)text_page;
54 if (index<0 || index>=textpage->CountChars()) return 0;
56 FPDF_CHAR_INFO charinfo;
57 textpage->GetCharInfo(index,charinfo);
58 return charinfo.m_Unicode;
60 DLLEXPORT double STDCALL FPDFText_GetFontSize(FPDF_TEXTPAGE text_page, int index)
62 if (!text_page) return 0;
63 IPDF_TextPage* textpage=(IPDF_TextPage*)text_page;
65 if (index<0 || index>=textpage->CountChars()) return 0;
67 FPDF_CHAR_INFO charinfo;
68 textpage->GetCharInfo(index,charinfo);
69 return charinfo.m_FontSize;
72 DLLEXPORT void STDCALL FPDFText_GetCharBox(FPDF_TEXTPAGE text_page, int index,double* left,
73 double* right, double* bottom, double* top)
75 if (!text_page) return;
76 IPDF_TextPage* textpage=(IPDF_TextPage*)text_page;
78 if (index<0 || index>=textpage->CountChars()) return ;
79 FPDF_CHAR_INFO charinfo;
80 textpage->GetCharInfo(index,charinfo);
81 *left=charinfo.m_CharBox.left;
82 *right=charinfo.m_CharBox.right;
83 *bottom=charinfo.m_CharBox.bottom;
84 *top=charinfo.m_CharBox.top;
88 DLLEXPORT int STDCALL FPDFText_GetCharIndexAtPos(FPDF_TEXTPAGE text_page,double x,double y,double xTorelance,double yTorelance)
90 if (!text_page) return -3;
91 IPDF_TextPage* textpage=(IPDF_TextPage*)text_page;
92 return textpage->GetIndexAtPos((FX_FLOAT)x,(FX_FLOAT)y,(FX_FLOAT)xTorelance,(FX_FLOAT)yTorelance);
95 DLLEXPORT int STDCALL FPDFText_GetText(FPDF_TEXTPAGE text_page,int start,int count,unsigned short* result)
97 if (!text_page) return 0;
98 IPDF_TextPage* textpage=(IPDF_TextPage*)text_page;
100 if (start>=textpage->CountChars()) return 0;
102 CFX_WideString str=textpage->GetPageText(start,count);
103 if(str.GetLength()>count)
104 str = str.Left(count);
106 CFX_ByteString cbUTF16str = str.UTF16LE_Encode();
107 FXSYS_memcpy(result,cbUTF16str.GetBuffer(cbUTF16str.GetLength()),cbUTF16str.GetLength());
108 cbUTF16str.ReleaseBuffer(cbUTF16str.GetLength());
110 return cbUTF16str.GetLength()/sizeof(unsigned short);
113 DLLEXPORT int STDCALL FPDFText_CountRects(FPDF_TEXTPAGE text_page,int start,int count)
115 if (!text_page) return 0;
116 IPDF_TextPage* textpage=(IPDF_TextPage*)text_page;
117 return textpage->CountRects(start,count);
120 DLLEXPORT void STDCALL FPDFText_GetRect(FPDF_TEXTPAGE text_page,int rect_index, double* left,double* top,
121 double* right, double* bottom)
123 if (!text_page) return;
124 IPDF_TextPage* textpage=(IPDF_TextPage*)text_page;
126 textpage->GetRect(rect_index,rect.left,rect.top,rect.right,rect.bottom);
133 DLLEXPORT int STDCALL FPDFText_GetBoundedText(FPDF_TEXTPAGE text_page,double left, double top,
134 double right, double bottom,unsigned short* buffer,int buflen)
136 if (!text_page) return 0;
137 IPDF_TextPage* textpage=(IPDF_TextPage*)text_page;
138 CFX_FloatRect rect((FX_FLOAT)left,(FX_FLOAT)bottom,(FX_FLOAT)right,(FX_FLOAT)top);
139 CFX_WideString str=textpage->GetTextByRect(rect);
141 if (buflen<=0 || buffer==NULL)
143 return str.GetLength();
146 CFX_ByteString cbUTF16Str = str.UTF16LE_Encode();
147 int len = cbUTF16Str.GetLength()/sizeof(unsigned short);
148 int size = buflen > len ? len : buflen;
149 FXSYS_memcpy(buffer,cbUTF16Str.GetBuffer(size*sizeof(unsigned short)),size*sizeof(unsigned short));
150 cbUTF16Str.ReleaseBuffer(size*sizeof(unsigned short));
158 DLLEXPORT FPDF_SCHHANDLE STDCALL FPDFText_FindStart(FPDF_TEXTPAGE text_page,FPDF_WIDESTRING findwhat,unsigned long flags,int start_index)
160 if (!text_page) return NULL;
161 IPDF_TextPageFind* textpageFind=NULL;
164 textpageFind=IPDF_TextPageFind::CreatePageFind((IPDF_TextPage*)text_page);
165 textpageFind->FindFirst(CFX_WideString::FromUTF16LE(findwhat),flags,start_index);
175 DLLEXPORT FPDF_BOOL STDCALL FPDFText_FindNext(FPDF_SCHHANDLE handle)
177 if (!handle) return FALSE;
178 IPDF_TextPageFind* textpageFind=(IPDF_TextPageFind*)handle;
179 return textpageFind->FindNext();
181 DLLEXPORT FPDF_BOOL STDCALL FPDFText_FindPrev(FPDF_SCHHANDLE handle)
183 if (!handle) return FALSE;
184 IPDF_TextPageFind* textpageFind=(IPDF_TextPageFind*)handle;
185 return textpageFind->FindPrev();
187 DLLEXPORT int STDCALL FPDFText_GetSchResultIndex(FPDF_SCHHANDLE handle)
189 if (!handle) return 0;
190 IPDF_TextPageFind* textpageFind=(IPDF_TextPageFind*)handle;
191 return textpageFind->GetCurOrder();
193 DLLEXPORT int STDCALL FPDFText_GetSchCount(FPDF_SCHHANDLE handle)
195 if (!handle) return 0;
196 IPDF_TextPageFind* textpageFind=(IPDF_TextPageFind*)handle;
197 return textpageFind->GetMatchedCount();
199 DLLEXPORT void STDCALL FPDFText_FindClose(FPDF_SCHHANDLE handle)
202 IPDF_TextPageFind* textpageFind=(IPDF_TextPageFind*)handle;
208 DLLEXPORT FPDF_PAGELINK STDCALL FPDFLink_LoadWebLinks(FPDF_TEXTPAGE text_page)
210 if (!text_page) return NULL;
211 IPDF_LinkExtract* pageLink=NULL;
214 pageLink=IPDF_LinkExtract::CreateLinkExtract();
215 pageLink->ExtractLinks((IPDF_TextPage*)text_page);
225 DLLEXPORT int STDCALL FPDFLink_CountWebLinks(FPDF_PAGELINK link_page)
227 if (!link_page) return 0;
228 IPDF_LinkExtract* pageLink=(IPDF_LinkExtract*)link_page;
229 return pageLink->CountLinks();
231 DLLEXPORT int STDCALL FPDFLink_GetURL(FPDF_PAGELINK link_page,int link_index, unsigned short* buffer,int buflen)
233 if (!link_page) return 0;
234 IPDF_LinkExtract* pageLink=(IPDF_LinkExtract*)link_page;
235 CFX_WideString url=pageLink->GetURL(link_index);
237 CFX_ByteString cbUTF16URL = url.UTF16LE_Encode();
238 int len= cbUTF16URL.GetLength()/sizeof(unsigned short);
239 if (buffer==NULL || buflen<=0)
241 int size=len<buflen ? len :buflen;
244 FXSYS_memcpy(buffer,cbUTF16URL.GetBuffer(size*sizeof(unsigned short)),size*sizeof(unsigned short));
245 cbUTF16URL.ReleaseBuffer(size*sizeof(unsigned short));
249 DLLEXPORT int STDCALL FPDFLink_CountRects(FPDF_PAGELINK link_page,int link_index)
251 if (!link_page) return 0;
252 IPDF_LinkExtract* pageLink=(IPDF_LinkExtract*)link_page;
253 CFX_RectArray rectArray;
254 pageLink->GetRects(link_index,rectArray);
255 return rectArray.GetSize();
257 DLLEXPORT void STDCALL FPDFLink_GetRect(FPDF_PAGELINK link_page,int link_index, int rect_index, double* left,
258 double* top,double* right, double* bottom)
260 if (!link_page) return;
261 IPDF_LinkExtract* pageLink=(IPDF_LinkExtract*)link_page;
262 CFX_RectArray rectArray;
263 pageLink->GetRects(link_index,rectArray);
265 rect=rectArray.GetAt(rect_index);
271 DLLEXPORT void STDCALL FPDFLink_CloseWebLinks(FPDF_PAGELINK link_page)
273 if (!link_page) return;
274 IPDF_LinkExtract* pageLink=(IPDF_LinkExtract*)link_page;