summaryrefslogtreecommitdiff
path: root/fpdfsdk/src/fpdftext.cpp
blob: e0a5abf90ac3cd07cd5d90afeedc9bd7ff25701b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// Copyright 2014 PDFium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com

#include "public/fpdf_text.h"

#include "../../core/include/fpdfdoc/fpdf_doc.h"
#include "../../core/include/fpdftext/fpdf_text.h"
#include "../include/fsdk_define.h"

#ifdef _WIN32
#include <tchar.h>
#endif

DLLEXPORT FPDF_TEXTPAGE STDCALL FPDFText_LoadPage(FPDF_PAGE page) {
  CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
  if (!pPage)
    return nullptr;
  CPDF_ViewerPreferences viewRef(pPage->m_pDocument);
  IPDF_TextPage* textpage =
      IPDF_TextPage::CreateTextPage(pPage, viewRef.IsDirectionR2L());
  textpage->ParseTextPage();
  return textpage;
}
DLLEXPORT void STDCALL FPDFText_ClosePage(FPDF_TEXTPAGE text_page) {
  delete (IPDF_TextPage*)text_page;
}
DLLEXPORT int STDCALL FPDFText_CountChars(FPDF_TEXTPAGE text_page) {
  if (!text_page)
    return -1;
  IPDF_TextPage* textpage = (IPDF_TextPage*)text_page;
  return textpage->CountChars();
}
DLLEXPORT unsigned int STDCALL FPDFText_GetUnicode(FPDF_TEXTPAGE text_page,
                                                   int index) {
  if (!text_page)
    return -1;
  IPDF_TextPage* textpage = (IPDF_TextPage*)text_page;

  if (index < 0 || index >= textpage->CountChars())
    return 0;

  FPDF_CHAR_INFO charinfo;
  textpage->GetCharInfo(index, charinfo);
  return charinfo.m_Unicode;
}
DLLEXPORT double STDCALL FPDFText_GetFontSize(FPDF_TEXTPAGE text_page,
                                              int index) {
  if (!text_page)
    return 0;
  IPDF_TextPage* textpage = (IPDF_TextPage*)text_page;

  if (index < 0 || index >= textpage->CountChars())
    return 0;

  FPDF_CHAR_INFO charinfo;
  textpage->GetCharInfo(index, charinfo);
  return charinfo.m_FontSize;
}

DLLEXPORT void STDCALL FPDFText_GetCharBox(FPDF_TEXTPAGE text_page,
                                           int index,
                                           double* left,
                                           double* right,
                                           double* bottom,
                                           double* top) {
  if (!text_page)
    return;
  IPDF_TextPage* textpage = (IPDF_TextPage*)text_page;

  if (index < 0 || index >= textpage->CountChars())
    return;
  FPDF_CHAR_INFO charinfo;
  textpage->GetCharInfo(index, charinfo);
  *left = charinfo.m_CharBox.left;
  *right = charinfo.m_CharBox.right;
  *bottom = charinfo.m_CharBox.bottom;
  *top = charinfo.m_CharBox.top;
}

// select
DLLEXPORT int STDCALL FPDFText_GetCharIndexAtPos(FPDF_TEXTPAGE text_page,
                                                 double x,
                                                 double y,
                                                 double xTolerance,
                                                 double yTolerance) {
  if (!text_page)
    return -3;
  IPDF_TextPage* textpage = (IPDF_TextPage*)text_page;
  return textpage->GetIndexAtPos((FX_FLOAT)x, (FX_FLOAT)y, (FX_FLOAT)xTolerance,
                                 (FX_FLOAT)yTolerance);
}

DLLEXPORT int STDCALL FPDFText_GetText(FPDF_TEXTPAGE text_page,
                                       int start,
                                       int count,
                                       unsigned short* result) {
  if (!text_page)
    return 0;
  IPDF_TextPage* textpage = (IPDF_TextPage*)text_page;

  if (start >= textpage->CountChars())
    return 0;

  CFX_WideString str = textpage->GetPageText(start, count);
  if (str.GetLength() > count)
    str = str.Left(count);

  CFX_ByteString cbUTF16str = str.UTF16LE_Encode();
  FXSYS_memcpy(result, cbUTF16str.GetBuffer(cbUTF16str.GetLength()),
               cbUTF16str.GetLength());
  cbUTF16str.ReleaseBuffer(cbUTF16str.GetLength());

  return cbUTF16str.GetLength() / sizeof(unsigned short);
}

DLLEXPORT int STDCALL FPDFText_CountRects(FPDF_TEXTPAGE text_page,
                                          int start,
                                          int count) {
  if (!text_page)
    return 0;
  IPDF_TextPage* textpage = (IPDF_TextPage*)text_page;
  return textpage->CountRects(start, count);
}
DLLEXPORT void STDCALL FPDFText_GetRect(FPDF_TEXTPAGE text_page,
                                        int rect_index,
                                        double* left,
                                        double* top,
                                        double* right,
                                        double* bottom) {
  if (!text_page)
    return;
  IPDF_TextPage* textpage = (IPDF_TextPage*)text_page;
  CFX_FloatRect rect;
  textpage->GetRect(rect_index, rect.left, rect.top, rect.right, rect.bottom);
  *left = rect.left;
  *top = rect.top;
  *right = rect.right;
  *bottom = rect.bottom;
}

DLLEXPORT int STDCALL FPDFText_GetBoundedText(FPDF_TEXTPAGE text_page,
                                              double left,
                                              double top,
                                              double right,
                                              double bottom,
                                              unsigned short* buffer,
                                              int buflen) {
  if (!text_page)
    return 0;
  IPDF_TextPage* textpage = (IPDF_TextPage*)text_page;
  CFX_FloatRect rect((FX_FLOAT)left, (FX_FLOAT)bottom, (FX_FLOAT)right,
                     (FX_FLOAT)top);
  CFX_WideString str = textpage->GetTextByRect(rect);

  if (buflen <= 0 || buffer == NULL) {
    return str.GetLength();
  }

  CFX_ByteString cbUTF16Str = str.UTF16LE_Encode();
  int len = cbUTF16Str.GetLength() / sizeof(unsigned short);
  int size = buflen > len ? len : buflen;
  FXSYS_memcpy(buffer, cbUTF16Str.GetBuffer(size * sizeof(unsigned short)),
               size * sizeof(unsigned short));
  cbUTF16Str.ReleaseBuffer(size * sizeof(unsigned short));

  return size;
}

// Search
//-1 for end
DLLEXPORT FPDF_SCHHANDLE STDCALL FPDFText_FindStart(FPDF_TEXTPAGE text_page,
                                                    FPDF_WIDESTRING findwhat,
                                                    unsigned long flags,
                                                    int start_index) {
  if (!text_page)
    return NULL;
  IPDF_TextPageFind* textpageFind = NULL;
  textpageFind = IPDF_TextPageFind::CreatePageFind((IPDF_TextPage*)text_page);
  FX_STRSIZE len = CFX_WideString::WStringLength(findwhat);
  textpageFind->FindFirst(CFX_WideString::FromUTF16LE(findwhat, len), flags,
                          start_index);
  return textpageFind;
}
DLLEXPORT FPDF_BOOL STDCALL FPDFText_FindNext(FPDF_SCHHANDLE handle) {
  if (!handle)
    return FALSE;
  IPDF_TextPageFind* textpageFind = (IPDF_TextPageFind*)handle;
  return textpageFind->FindNext();
}
DLLEXPORT FPDF_BOOL STDCALL FPDFText_FindPrev(FPDF_SCHHANDLE handle) {
  if (!handle)
    return FALSE;
  IPDF_TextPageFind* textpageFind = (IPDF_TextPageFind*)handle;
  return textpageFind->FindPrev();
}
DLLEXPORT int STDCALL FPDFText_GetSchResultIndex(FPDF_SCHHANDLE handle) {
  if (!handle)
    return 0;
  IPDF_TextPageFind* textpageFind = (IPDF_TextPageFind*)handle;
  return textpageFind->GetCurOrder();
}
DLLEXPORT int STDCALL FPDFText_GetSchCount(FPDF_SCHHANDLE handle) {
  if (!handle)
    return 0;
  IPDF_TextPageFind* textpageFind = (IPDF_TextPageFind*)handle;
  return textpageFind->GetMatchedCount();
}
DLLEXPORT void STDCALL FPDFText_FindClose(FPDF_SCHHANDLE handle) {
  if (!handle)
    return;
  IPDF_TextPageFind* textpageFind = (IPDF_TextPageFind*)handle;
  delete textpageFind;
  handle = NULL;
}

// web link
DLLEXPORT FPDF_PAGELINK STDCALL FPDFLink_LoadWebLinks(FPDF_TEXTPAGE text_page) {
  if (!text_page)
    return NULL;
  IPDF_LinkExtract* pageLink = NULL;
  pageLink = IPDF_LinkExtract::CreateLinkExtract();
  pageLink->ExtractLinks((IPDF_TextPage*)text_page);
  return pageLink;
}
DLLEXPORT int STDCALL FPDFLink_CountWebLinks(FPDF_PAGELINK link_page) {
  if (!link_page)
    return 0;
  IPDF_LinkExtract* pageLink = (IPDF_LinkExtract*)link_page;
  return pageLink->CountLinks();
}
DLLEXPORT int STDCALL FPDFLink_GetURL(FPDF_PAGELINK link_page,
                                      int link_index,
                                      unsigned short* buffer,
                                      int buflen) {
  if (!link_page)
    return 0;
  IPDF_LinkExtract* pageLink = (IPDF_LinkExtract*)link_page;
  CFX_WideString url = pageLink->GetURL(link_index);

  CFX_ByteString cbUTF16URL = url.UTF16LE_Encode();
  int len = cbUTF16URL.GetLength() / sizeof(unsigned short);
  if (buffer == NULL || buflen <= 0)
    return len;
  int size = len < buflen ? len : buflen;
  if (size > 0) {
    FXSYS_memcpy(buffer, cbUTF16URL.GetBuffer(size * sizeof(unsigned short)),
                 size * sizeof(unsigned short));
    cbUTF16URL.ReleaseBuffer(size * sizeof(unsigned short));
  }
  return size;
}
DLLEXPORT int STDCALL FPDFLink_CountRects(FPDF_PAGELINK link_page,
                                          int link_index) {
  if (!link_page)
    return 0;
  IPDF_LinkExtract* pageLink = (IPDF_LinkExtract*)link_page;
  CFX_RectArray rectArray;
  pageLink->GetRects(link_index, rectArray);
  return rectArray.GetSize();
}
DLLEXPORT void STDCALL FPDFLink_GetRect(FPDF_PAGELINK link_page,
                                        int link_index,
                                        int rect_index,
                                        double* left,
                                        double* top,
                                        double* right,
                                        double* bottom) {
  if (!link_page)
    return;
  IPDF_LinkExtract* pageLink = (IPDF_LinkExtract*)link_page;
  CFX_RectArray rectArray;
  pageLink->GetRects(link_index, rectArray);
  if (rect_index >= 0 && rect_index < rectArray.GetSize()) {
    CFX_FloatRect rect = rectArray.GetAt(rect_index);
    *left = rect.left;
    *right = rect.right;
    *top = rect.top;
    *bottom = rect.bottom;
  }
}
DLLEXPORT void STDCALL FPDFLink_CloseWebLinks(FPDF_PAGELINK link_page) {
  delete (IPDF_LinkExtract*)link_page;
}