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