blob: ae6841febc8025f71ff846816b7e76c78944a249 [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 Zhangb4e7f302015-11-06 15:52:32 -08007#include "public/fpdf_text.h"
8
Dan Sinclair85c8e7f2016-11-21 13:50:32 -05009#include <algorithm>
10#include <vector>
11
dsinclair41872fa2016-10-04 11:29:35 -070012#include "core/fpdfapi/page/cpdf_page.h"
dsinclair1727aee2016-09-29 13:12:56 -070013#include "core/fpdfdoc/cpdf_viewerpreferences.h"
dsinclaire0307862016-09-29 13:25:38 -070014#include "core/fpdftext/cpdf_linkextract.h"
15#include "core/fpdftext/cpdf_textpage.h"
16#include "core/fpdftext/cpdf_textpagefind.h"
dsinclair114e46a2016-09-29 17:18:21 -070017#include "fpdfsdk/fsdk_define.h"
tsepez69141182016-04-21 10:43:39 -070018#include "third_party/base/numerics/safe_conversions.h"
tsepezdf964df2016-04-21 12:09:41 -070019#include "third_party/base/stl_util.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070020
Tom Sepez40e9ff32015-11-30 12:39:54 -080021#ifdef PDF_ENABLE_XFA
dsinclair521b7502016-11-02 13:02:28 -070022#include "fpdfsdk/fpdfxfa/cpdfxfa_context.h"
dsinclair4d29e782016-10-04 14:02:47 -070023#include "fpdfsdk/fpdfxfa/cpdfxfa_page.h"
Tom Sepez40e9ff32015-11-30 12:39:54 -080024#endif // PDF_ENABLE_XFA
25
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070026#ifdef _WIN32
27#include <tchar.h>
28#endif
29
dsinclaira28ae382016-04-19 10:39:24 -070030namespace {
31
Ryan Harrison2bf05a62017-09-05 11:48:55 -040032constexpr size_t kBytesPerCharacter = sizeof(unsigned short);
33
dsinclaira28ae382016-04-19 10:39:24 -070034CPDF_TextPage* CPDFTextPageFromFPDFTextPage(FPDF_TEXTPAGE text_page) {
35 return static_cast<CPDF_TextPage*>(text_page);
36}
37
38CPDF_TextPageFind* CPDFTextPageFindFromFPDFSchHandle(FPDF_SCHHANDLE handle) {
39 return static_cast<CPDF_TextPageFind*>(handle);
40}
41
42CPDF_LinkExtract* CPDFLinkExtractFromFPDFPageLink(FPDF_PAGELINK link) {
43 return static_cast<CPDF_LinkExtract*>(link);
44}
45
46} // namespace
47
Dan Sinclair00d2ad12017-08-10 14:13:02 -040048FPDF_EXPORT FPDF_TEXTPAGE FPDF_CALLCONV FPDFText_LoadPage(FPDF_PAGE page) {
Tom Sepez1b246282015-11-25 15:15:31 -080049 CPDF_Page* pPDFPage = CPDFPageFromFPDFPage(page);
50 if (!pPDFPage)
51 return nullptr;
dsinclaira28ae382016-04-19 10:39:24 -070052
Tom Sepez40e9ff32015-11-30 12:39:54 -080053#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -070054 CPDFXFA_Page* pPage = (CPDFXFA_Page*)page;
dsinclair521b7502016-11-02 13:02:28 -070055 CPDFXFA_Context* pContext = pPage->GetContext();
56 CPDF_ViewerPreferences viewRef(pContext->GetPDFDoc());
Tom Sepez40e9ff32015-11-30 12:39:54 -080057#else // PDF_ENABLE_XFA
Tom Sepez4cb82ee2017-05-22 15:15:30 -070058 CPDF_ViewerPreferences viewRef(pPDFPage->m_pDocument.Get());
Tom Sepez40e9ff32015-11-30 12:39:54 -080059#endif // PDF_ENABLE_XFA
dsinclaira28ae382016-04-19 10:39:24 -070060
dsinclair8f4bf9a2016-05-04 13:51:51 -070061 CPDF_TextPage* textpage = new CPDF_TextPage(
62 pPDFPage, viewRef.IsDirectionR2L() ? FPDFText_Direction::Right
63 : FPDFText_Direction::Left);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070064 textpage->ParseTextPage();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070065 return textpage;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070066}
dsinclaira28ae382016-04-19 10:39:24 -070067
Dan Sinclair00d2ad12017-08-10 14:13:02 -040068FPDF_EXPORT void FPDF_CALLCONV FPDFText_ClosePage(FPDF_TEXTPAGE text_page) {
dsinclaira28ae382016-04-19 10:39:24 -070069 delete CPDFTextPageFromFPDFTextPage(text_page);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070070}
dsinclaira28ae382016-04-19 10:39:24 -070071
Dan Sinclair00d2ad12017-08-10 14:13:02 -040072FPDF_EXPORT int FPDF_CALLCONV FPDFText_CountChars(FPDF_TEXTPAGE text_page) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070073 if (!text_page)
74 return -1;
dsinclaira28ae382016-04-19 10:39:24 -070075
76 CPDF_TextPage* textpage = CPDFTextPageFromFPDFTextPage(text_page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070077 return textpage->CountChars();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070078}
Lei Zhang0f2ea022016-01-11 12:01:23 -080079
Dan Sinclair00d2ad12017-08-10 14:13:02 -040080FPDF_EXPORT unsigned int FPDF_CALLCONV
81FPDFText_GetUnicode(FPDF_TEXTPAGE text_page, int index) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070082 if (!text_page)
Wei Lid4e8f122016-03-21 11:20:44 -070083 return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070084
dsinclaira28ae382016-04-19 10:39:24 -070085 CPDF_TextPage* textpage = CPDFTextPageFromFPDFTextPage(text_page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070086 if (index < 0 || index >= textpage->CountChars())
87 return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070088
Nico Weber9d8ec5a2015-08-04 13:00:21 -070089 FPDF_CHAR_INFO charinfo;
Lei Zhang0f2ea022016-01-11 12:01:23 -080090 textpage->GetCharInfo(index, &charinfo);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070091 return charinfo.m_Unicode;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070092}
Lei Zhang0f2ea022016-01-11 12:01:23 -080093
Dan Sinclair00d2ad12017-08-10 14:13:02 -040094FPDF_EXPORT double FPDF_CALLCONV FPDFText_GetFontSize(FPDF_TEXTPAGE text_page,
95 int index) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070096 if (!text_page)
97 return 0;
dsinclaira28ae382016-04-19 10:39:24 -070098 CPDF_TextPage* textpage = CPDFTextPageFromFPDFTextPage(text_page);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070099
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700100 if (index < 0 || index >= textpage->CountChars())
101 return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700102
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700103 FPDF_CHAR_INFO charinfo;
Lei Zhang0f2ea022016-01-11 12:01:23 -0800104 textpage->GetCharInfo(index, &charinfo);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700105 return charinfo.m_FontSize;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700106}
107
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400108FPDF_EXPORT void FPDF_CALLCONV FPDFText_GetCharBox(FPDF_TEXTPAGE text_page,
109 int index,
110 double* left,
111 double* right,
112 double* bottom,
113 double* top) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700114 if (!text_page)
115 return;
dsinclaira28ae382016-04-19 10:39:24 -0700116 CPDF_TextPage* textpage = CPDFTextPageFromFPDFTextPage(text_page);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700117
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700118 if (index < 0 || index >= textpage->CountChars())
119 return;
120 FPDF_CHAR_INFO charinfo;
Lei Zhang0f2ea022016-01-11 12:01:23 -0800121 textpage->GetCharInfo(index, &charinfo);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700122 *left = charinfo.m_CharBox.left;
123 *right = charinfo.m_CharBox.right;
124 *bottom = charinfo.m_CharBox.bottom;
125 *top = charinfo.m_CharBox.top;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700126}
127
Andrew Weintraubd3002342017-08-11 11:36:51 -0400128FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
129FPDFText_GetCharOrigin(FPDF_TEXTPAGE text_page,
130 int index,
131 double* x,
132 double* y) {
133 if (!text_page)
134 return false;
135 CPDF_TextPage* textpage = CPDFTextPageFromFPDFTextPage(text_page);
136
137 if (index < 0 || index >= textpage->CountChars())
138 return false;
139 FPDF_CHAR_INFO charinfo;
140 textpage->GetCharInfo(index, &charinfo);
141 *x = charinfo.m_Origin.x;
142 *y = charinfo.m_Origin.y;
143 return true;
144}
145
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700146// select
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400147FPDF_EXPORT int FPDF_CALLCONV
148FPDFText_GetCharIndexAtPos(FPDF_TEXTPAGE text_page,
149 double x,
150 double y,
151 double xTolerance,
152 double yTolerance) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700153 if (!text_page)
154 return -3;
dsinclaira28ae382016-04-19 10:39:24 -0700155
156 CPDF_TextPage* textpage = CPDFTextPageFromFPDFTextPage(text_page);
Dan Sinclaird476adc2017-02-21 14:31:41 -0500157 return textpage->GetIndexAtPos(
Dan Sinclair05df0752017-03-14 14:43:42 -0400158 CFX_PointF(static_cast<float>(x), static_cast<float>(y)),
159 CFX_SizeF(static_cast<float>(xTolerance),
160 static_cast<float>(yTolerance)));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700161}
162
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400163FPDF_EXPORT int FPDF_CALLCONV FPDFText_GetText(FPDF_TEXTPAGE text_page,
164 int start,
165 int count,
166 unsigned short* result) {
Ryan Harrisonc5ac0572017-08-31 16:37:48 -0400167 if (start < 0 || count < 1 || !result || !text_page)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700168 return 0;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700169
dsinclaira28ae382016-04-19 10:39:24 -0700170 CPDF_TextPage* textpage = CPDFTextPageFromFPDFTextPage(text_page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700171 if (start >= textpage->CountChars())
172 return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700173
Ryan Harrison275e2602017-09-18 14:23:18 -0400174 WideString str = textpage->GetPageText(start, count - 1);
Ryan Harrisonc5ac0572017-08-31 16:37:48 -0400175 if (str.GetLength() <= 0)
176 return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700177
Ryan Harrison875e98c2017-09-27 10:53:11 -0400178 if (str.GetLength() > static_cast<size_t>(count))
179 str = str.Left(static_cast<size_t>(count));
Ryan Harrisone420d9c2017-09-11 15:59:02 -0400180
Ryan Harrisonc5ac0572017-08-31 16:37:48 -0400181 // UFT16LE_Encode doesn't handle surrogate pairs properly, so it is expected
182 // the number of items to stay the same.
Ryan Harrison275e2602017-09-18 14:23:18 -0400183 ByteString cbUTF16str = str.UTF16LE_Encode();
Ryan Harrison2bf05a62017-09-05 11:48:55 -0400184 ASSERT(cbUTF16str.GetLength() / kBytesPerCharacter <=
Ryan Harrisonc5ac0572017-08-31 16:37:48 -0400185 static_cast<size_t>(count));
Dan Sinclair1c5d0b42017-04-03 15:05:11 -0400186 memcpy(result, cbUTF16str.GetBuffer(cbUTF16str.GetLength()),
187 cbUTF16str.GetLength());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700188
Ryan Harrison2bf05a62017-09-05 11:48:55 -0400189 return cbUTF16str.GetLength() / kBytesPerCharacter;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700190}
191
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400192FPDF_EXPORT int FPDF_CALLCONV FPDFText_CountRects(FPDF_TEXTPAGE text_page,
193 int start,
194 int count) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700195 if (!text_page)
196 return 0;
dsinclaira28ae382016-04-19 10:39:24 -0700197
198 CPDF_TextPage* textpage = CPDFTextPageFromFPDFTextPage(text_page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700199 return textpage->CountRects(start, count);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700200}
dsinclaira28ae382016-04-19 10:39:24 -0700201
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400202FPDF_EXPORT void FPDF_CALLCONV FPDFText_GetRect(FPDF_TEXTPAGE text_page,
203 int rect_index,
204 double* left,
205 double* top,
206 double* right,
207 double* bottom) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700208 if (!text_page)
209 return;
dsinclaira28ae382016-04-19 10:39:24 -0700210
211 CPDF_TextPage* textpage = CPDFTextPageFromFPDFTextPage(text_page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700212 CFX_FloatRect rect;
213 textpage->GetRect(rect_index, rect.left, rect.top, rect.right, rect.bottom);
214 *left = rect.left;
215 *top = rect.top;
216 *right = rect.right;
217 *bottom = rect.bottom;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700218}
219
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400220FPDF_EXPORT int FPDF_CALLCONV FPDFText_GetBoundedText(FPDF_TEXTPAGE text_page,
221 double left,
222 double top,
223 double right,
224 double bottom,
225 unsigned short* buffer,
226 int buflen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700227 if (!text_page)
228 return 0;
dsinclaira28ae382016-04-19 10:39:24 -0700229
230 CPDF_TextPage* textpage = CPDFTextPageFromFPDFTextPage(text_page);
Dan Sinclair05df0752017-03-14 14:43:42 -0400231 CFX_FloatRect rect((float)left, (float)bottom, (float)right, (float)top);
Ryan Harrison275e2602017-09-18 14:23:18 -0400232 WideString str = textpage->GetTextByRect(rect);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700233
dsinclaira28ae382016-04-19 10:39:24 -0700234 if (buflen <= 0 || !buffer)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700235 return str.GetLength();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700236
Ryan Harrison275e2602017-09-18 14:23:18 -0400237 ByteString cbUTF16Str = str.UTF16LE_Encode();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700238 int len = cbUTF16Str.GetLength() / sizeof(unsigned short);
239 int size = buflen > len ? len : buflen;
Dan Sinclair1c5d0b42017-04-03 15:05:11 -0400240 memcpy(buffer, cbUTF16Str.GetBuffer(size * sizeof(unsigned short)),
241 size * sizeof(unsigned short));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700242 cbUTF16Str.ReleaseBuffer(size * sizeof(unsigned short));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700243
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700244 return size;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700245}
246
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700247// Search
Dan Sinclair50cce602016-02-24 09:51:16 -0500248// -1 for end
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400249FPDF_EXPORT FPDF_SCHHANDLE FPDF_CALLCONV
250FPDFText_FindStart(FPDF_TEXTPAGE text_page,
251 FPDF_WIDESTRING findwhat,
252 unsigned long flags,
253 int start_index) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700254 if (!text_page)
dsinclaira28ae382016-04-19 10:39:24 -0700255 return nullptr;
256
257 CPDF_TextPageFind* textpageFind =
258 new CPDF_TextPageFind(CPDFTextPageFromFPDFTextPage(text_page));
Ryan Harrison875e98c2017-09-27 10:53:11 -0400259 size_t len = WideString::WStringLength(findwhat);
Ryan Harrison275e2602017-09-18 14:23:18 -0400260 textpageFind->FindFirst(WideString::FromUTF16LE(findwhat, len), flags,
Ryan Harrison61cb1122017-08-28 13:36:18 -0400261 start_index >= 0
Ryan Harrison875e98c2017-09-27 10:53:11 -0400262 ? pdfium::Optional<size_t>(start_index)
263 : pdfium::Optional<size_t>());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700264 return textpageFind;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700265}
dsinclaira28ae382016-04-19 10:39:24 -0700266
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400267FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFText_FindNext(FPDF_SCHHANDLE handle) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700268 if (!handle)
tsepez4cf55152016-11-02 14:37:54 -0700269 return false;
dsinclaira28ae382016-04-19 10:39:24 -0700270
271 CPDF_TextPageFind* textpageFind = CPDFTextPageFindFromFPDFSchHandle(handle);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700272 return textpageFind->FindNext();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700273}
dsinclaira28ae382016-04-19 10:39:24 -0700274
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400275FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFText_FindPrev(FPDF_SCHHANDLE handle) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700276 if (!handle)
tsepez4cf55152016-11-02 14:37:54 -0700277 return false;
dsinclaira28ae382016-04-19 10:39:24 -0700278
279 CPDF_TextPageFind* textpageFind = CPDFTextPageFindFromFPDFSchHandle(handle);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700280 return textpageFind->FindPrev();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700281}
dsinclaira28ae382016-04-19 10:39:24 -0700282
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400283FPDF_EXPORT int FPDF_CALLCONV
284FPDFText_GetSchResultIndex(FPDF_SCHHANDLE handle) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700285 if (!handle)
286 return 0;
dsinclaira28ae382016-04-19 10:39:24 -0700287
288 CPDF_TextPageFind* textpageFind = CPDFTextPageFindFromFPDFSchHandle(handle);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700289 return textpageFind->GetCurOrder();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700290}
dsinclaira28ae382016-04-19 10:39:24 -0700291
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400292FPDF_EXPORT int FPDF_CALLCONV FPDFText_GetSchCount(FPDF_SCHHANDLE handle) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700293 if (!handle)
294 return 0;
dsinclaira28ae382016-04-19 10:39:24 -0700295
296 CPDF_TextPageFind* textpageFind = CPDFTextPageFindFromFPDFSchHandle(handle);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700297 return textpageFind->GetMatchedCount();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700298}
dsinclaira28ae382016-04-19 10:39:24 -0700299
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400300FPDF_EXPORT void FPDF_CALLCONV FPDFText_FindClose(FPDF_SCHHANDLE handle) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700301 if (!handle)
302 return;
dsinclaira28ae382016-04-19 10:39:24 -0700303
304 CPDF_TextPageFind* textpageFind = CPDFTextPageFindFromFPDFSchHandle(handle);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700305 delete textpageFind;
dsinclaira28ae382016-04-19 10:39:24 -0700306 handle = nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700307}
308
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700309// web link
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400310FPDF_EXPORT FPDF_PAGELINK FPDF_CALLCONV
311FPDFLink_LoadWebLinks(FPDF_TEXTPAGE text_page) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700312 if (!text_page)
dsinclaira28ae382016-04-19 10:39:24 -0700313 return nullptr;
314
tsepez69141182016-04-21 10:43:39 -0700315 CPDF_LinkExtract* pageLink =
316 new CPDF_LinkExtract(CPDFTextPageFromFPDFTextPage(text_page));
317 pageLink->ExtractLinks();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700318 return pageLink;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700319}
dsinclaira28ae382016-04-19 10:39:24 -0700320
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400321FPDF_EXPORT int FPDF_CALLCONV FPDFLink_CountWebLinks(FPDF_PAGELINK link_page) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700322 if (!link_page)
323 return 0;
dsinclaira28ae382016-04-19 10:39:24 -0700324
325 CPDF_LinkExtract* pageLink = CPDFLinkExtractFromFPDFPageLink(link_page);
tsepez69141182016-04-21 10:43:39 -0700326 return pdfium::base::checked_cast<int>(pageLink->CountLinks());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700327}
dsinclaira28ae382016-04-19 10:39:24 -0700328
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400329FPDF_EXPORT int FPDF_CALLCONV FPDFLink_GetURL(FPDF_PAGELINK link_page,
330 int link_index,
331 unsigned short* buffer,
332 int buflen) {
Ryan Harrison275e2602017-09-18 14:23:18 -0400333 WideString wsUrl(L"");
tsepez69141182016-04-21 10:43:39 -0700334 if (link_page && link_index >= 0) {
335 CPDF_LinkExtract* pageLink = CPDFLinkExtractFromFPDFPageLink(link_page);
336 wsUrl = pageLink->GetURL(link_index);
337 }
Ryan Harrison275e2602017-09-18 14:23:18 -0400338 ByteString cbUTF16URL = wsUrl.UTF16LE_Encode();
tsepez69141182016-04-21 10:43:39 -0700339 int required = cbUTF16URL.GetLength() / sizeof(unsigned short);
Lei Zhang412e9082015-12-14 18:34:00 -0800340 if (!buffer || buflen <= 0)
tsepez69141182016-04-21 10:43:39 -0700341 return required;
dsinclaira28ae382016-04-19 10:39:24 -0700342
tsepez69141182016-04-21 10:43:39 -0700343 int size = std::min(required, buflen);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700344 if (size > 0) {
dsinclaira28ae382016-04-19 10:39:24 -0700345 int buf_size = size * sizeof(unsigned short);
Dan Sinclair1c5d0b42017-04-03 15:05:11 -0400346 memcpy(buffer, cbUTF16URL.GetBuffer(buf_size), buf_size);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700347 }
348 return size;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700349}
dsinclaira28ae382016-04-19 10:39:24 -0700350
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400351FPDF_EXPORT int FPDF_CALLCONV FPDFLink_CountRects(FPDF_PAGELINK link_page,
352 int link_index) {
tsepez69141182016-04-21 10:43:39 -0700353 if (!link_page || link_index < 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700354 return 0;
dsinclaira28ae382016-04-19 10:39:24 -0700355
356 CPDF_LinkExtract* pageLink = CPDFLinkExtractFromFPDFPageLink(link_page);
tsepezdf964df2016-04-21 12:09:41 -0700357 return pdfium::CollectionSize<int>(pageLink->GetRects(link_index));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700358}
dsinclaira28ae382016-04-19 10:39:24 -0700359
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400360FPDF_EXPORT void FPDF_CALLCONV FPDFLink_GetRect(FPDF_PAGELINK link_page,
361 int link_index,
362 int rect_index,
363 double* left,
364 double* top,
365 double* right,
366 double* bottom) {
tsepez69141182016-04-21 10:43:39 -0700367 if (!link_page || link_index < 0 || rect_index < 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700368 return;
dsinclaira28ae382016-04-19 10:39:24 -0700369
tsepez69141182016-04-21 10:43:39 -0700370 CPDF_LinkExtract* pageLink = CPDFLinkExtractFromFPDFPageLink(link_page);
tsepezdf964df2016-04-21 12:09:41 -0700371 std::vector<CFX_FloatRect> rectArray = pageLink->GetRects(link_index);
372 if (rect_index >= pdfium::CollectionSize<int>(rectArray))
tsepez69141182016-04-21 10:43:39 -0700373 return;
374
tsepezdf964df2016-04-21 12:09:41 -0700375 *left = rectArray[rect_index].left;
376 *right = rectArray[rect_index].right;
377 *top = rectArray[rect_index].top;
378 *bottom = rectArray[rect_index].bottom;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700379}
tsepez69141182016-04-21 10:43:39 -0700380
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400381FPDF_EXPORT void FPDF_CALLCONV FPDFLink_CloseWebLinks(FPDF_PAGELINK link_page) {
dsinclaira28ae382016-04-19 10:39:24 -0700382 delete CPDFLinkExtractFromFPDFPageLink(link_page);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700383}