blob: 70acf54ec7b33b6cf8de30ca6870f9cabee83aa7 [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
32CPDF_TextPage* CPDFTextPageFromFPDFTextPage(FPDF_TEXTPAGE text_page) {
33 return static_cast<CPDF_TextPage*>(text_page);
34}
35
36CPDF_TextPageFind* CPDFTextPageFindFromFPDFSchHandle(FPDF_SCHHANDLE handle) {
37 return static_cast<CPDF_TextPageFind*>(handle);
38}
39
40CPDF_LinkExtract* CPDFLinkExtractFromFPDFPageLink(FPDF_PAGELINK link) {
41 return static_cast<CPDF_LinkExtract*>(link);
42}
43
44} // namespace
45
Dan Sinclair00d2ad12017-08-10 14:13:02 -040046FPDF_EXPORT FPDF_TEXTPAGE FPDF_CALLCONV FPDFText_LoadPage(FPDF_PAGE page) {
Tom Sepez1b246282015-11-25 15:15:31 -080047 CPDF_Page* pPDFPage = CPDFPageFromFPDFPage(page);
48 if (!pPDFPage)
49 return nullptr;
dsinclaira28ae382016-04-19 10:39:24 -070050
Tom Sepez40e9ff32015-11-30 12:39:54 -080051#ifdef PDF_ENABLE_XFA
Nico Weber9d8ec5a2015-08-04 13:00:21 -070052 CPDFXFA_Page* pPage = (CPDFXFA_Page*)page;
dsinclair521b7502016-11-02 13:02:28 -070053 CPDFXFA_Context* pContext = pPage->GetContext();
54 CPDF_ViewerPreferences viewRef(pContext->GetPDFDoc());
Tom Sepez40e9ff32015-11-30 12:39:54 -080055#else // PDF_ENABLE_XFA
Tom Sepez4cb82ee2017-05-22 15:15:30 -070056 CPDF_ViewerPreferences viewRef(pPDFPage->m_pDocument.Get());
Tom Sepez40e9ff32015-11-30 12:39:54 -080057#endif // PDF_ENABLE_XFA
dsinclaira28ae382016-04-19 10:39:24 -070058
dsinclair8f4bf9a2016-05-04 13:51:51 -070059 CPDF_TextPage* textpage = new CPDF_TextPage(
60 pPDFPage, viewRef.IsDirectionR2L() ? FPDFText_Direction::Right
61 : FPDFText_Direction::Left);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070062 textpage->ParseTextPage();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070063 return textpage;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070064}
dsinclaira28ae382016-04-19 10:39:24 -070065
Dan Sinclair00d2ad12017-08-10 14:13:02 -040066FPDF_EXPORT void FPDF_CALLCONV FPDFText_ClosePage(FPDF_TEXTPAGE text_page) {
dsinclaira28ae382016-04-19 10:39:24 -070067 delete CPDFTextPageFromFPDFTextPage(text_page);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070068}
dsinclaira28ae382016-04-19 10:39:24 -070069
Dan Sinclair00d2ad12017-08-10 14:13:02 -040070FPDF_EXPORT int FPDF_CALLCONV FPDFText_CountChars(FPDF_TEXTPAGE text_page) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070071 if (!text_page)
72 return -1;
dsinclaira28ae382016-04-19 10:39:24 -070073
74 CPDF_TextPage* textpage = CPDFTextPageFromFPDFTextPage(text_page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070075 return textpage->CountChars();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070076}
Lei Zhang0f2ea022016-01-11 12:01:23 -080077
Dan Sinclair00d2ad12017-08-10 14:13:02 -040078FPDF_EXPORT unsigned int FPDF_CALLCONV
79FPDFText_GetUnicode(FPDF_TEXTPAGE text_page, int index) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070080 if (!text_page)
Wei Lid4e8f122016-03-21 11:20:44 -070081 return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070082
dsinclaira28ae382016-04-19 10:39:24 -070083 CPDF_TextPage* textpage = CPDFTextPageFromFPDFTextPage(text_page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070084 if (index < 0 || index >= textpage->CountChars())
85 return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070086
Nico Weber9d8ec5a2015-08-04 13:00:21 -070087 FPDF_CHAR_INFO charinfo;
Lei Zhang0f2ea022016-01-11 12:01:23 -080088 textpage->GetCharInfo(index, &charinfo);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070089 return charinfo.m_Unicode;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070090}
Lei Zhang0f2ea022016-01-11 12:01:23 -080091
Dan Sinclair00d2ad12017-08-10 14:13:02 -040092FPDF_EXPORT double FPDF_CALLCONV FPDFText_GetFontSize(FPDF_TEXTPAGE text_page,
93 int index) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070094 if (!text_page)
95 return 0;
dsinclaira28ae382016-04-19 10:39:24 -070096 CPDF_TextPage* textpage = CPDFTextPageFromFPDFTextPage(text_page);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070097
Nico Weber9d8ec5a2015-08-04 13:00:21 -070098 if (index < 0 || index >= textpage->CountChars())
99 return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700100
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700101 FPDF_CHAR_INFO charinfo;
Lei Zhang0f2ea022016-01-11 12:01:23 -0800102 textpage->GetCharInfo(index, &charinfo);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700103 return charinfo.m_FontSize;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700104}
105
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400106FPDF_EXPORT void FPDF_CALLCONV FPDFText_GetCharBox(FPDF_TEXTPAGE text_page,
107 int index,
108 double* left,
109 double* right,
110 double* bottom,
111 double* top) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700112 if (!text_page)
113 return;
dsinclaira28ae382016-04-19 10:39:24 -0700114 CPDF_TextPage* textpage = CPDFTextPageFromFPDFTextPage(text_page);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700115
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700116 if (index < 0 || index >= textpage->CountChars())
117 return;
118 FPDF_CHAR_INFO charinfo;
Lei Zhang0f2ea022016-01-11 12:01:23 -0800119 textpage->GetCharInfo(index, &charinfo);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700120 *left = charinfo.m_CharBox.left;
121 *right = charinfo.m_CharBox.right;
122 *bottom = charinfo.m_CharBox.bottom;
123 *top = charinfo.m_CharBox.top;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700124}
125
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700126// select
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400127FPDF_EXPORT int FPDF_CALLCONV
128FPDFText_GetCharIndexAtPos(FPDF_TEXTPAGE text_page,
129 double x,
130 double y,
131 double xTolerance,
132 double yTolerance) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700133 if (!text_page)
134 return -3;
dsinclaira28ae382016-04-19 10:39:24 -0700135
136 CPDF_TextPage* textpage = CPDFTextPageFromFPDFTextPage(text_page);
Dan Sinclaird476adc2017-02-21 14:31:41 -0500137 return textpage->GetIndexAtPos(
Dan Sinclair05df0752017-03-14 14:43:42 -0400138 CFX_PointF(static_cast<float>(x), static_cast<float>(y)),
139 CFX_SizeF(static_cast<float>(xTolerance),
140 static_cast<float>(yTolerance)));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700141}
142
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400143FPDF_EXPORT int FPDF_CALLCONV FPDFText_GetText(FPDF_TEXTPAGE text_page,
144 int start,
145 int count,
146 unsigned short* result) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700147 if (!text_page)
148 return 0;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700149
dsinclaira28ae382016-04-19 10:39:24 -0700150 CPDF_TextPage* textpage = CPDFTextPageFromFPDFTextPage(text_page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700151 if (start >= textpage->CountChars())
152 return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700153
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700154 CFX_WideString str = textpage->GetPageText(start, count);
155 if (str.GetLength() > count)
156 str = str.Left(count);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700157
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700158 CFX_ByteString cbUTF16str = str.UTF16LE_Encode();
Dan Sinclair1c5d0b42017-04-03 15:05:11 -0400159 memcpy(result, cbUTF16str.GetBuffer(cbUTF16str.GetLength()),
160 cbUTF16str.GetLength());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700161 cbUTF16str.ReleaseBuffer(cbUTF16str.GetLength());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700162
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700163 return cbUTF16str.GetLength() / sizeof(unsigned short);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700164}
165
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400166FPDF_EXPORT int FPDF_CALLCONV FPDFText_CountRects(FPDF_TEXTPAGE text_page,
167 int start,
168 int count) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700169 if (!text_page)
170 return 0;
dsinclaira28ae382016-04-19 10:39:24 -0700171
172 CPDF_TextPage* textpage = CPDFTextPageFromFPDFTextPage(text_page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700173 return textpage->CountRects(start, count);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700174}
dsinclaira28ae382016-04-19 10:39:24 -0700175
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400176FPDF_EXPORT void FPDF_CALLCONV FPDFText_GetRect(FPDF_TEXTPAGE text_page,
177 int rect_index,
178 double* left,
179 double* top,
180 double* right,
181 double* bottom) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700182 if (!text_page)
183 return;
dsinclaira28ae382016-04-19 10:39:24 -0700184
185 CPDF_TextPage* textpage = CPDFTextPageFromFPDFTextPage(text_page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700186 CFX_FloatRect rect;
187 textpage->GetRect(rect_index, rect.left, rect.top, rect.right, rect.bottom);
188 *left = rect.left;
189 *top = rect.top;
190 *right = rect.right;
191 *bottom = rect.bottom;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700192}
193
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400194FPDF_EXPORT int FPDF_CALLCONV FPDFText_GetBoundedText(FPDF_TEXTPAGE text_page,
195 double left,
196 double top,
197 double right,
198 double bottom,
199 unsigned short* buffer,
200 int buflen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700201 if (!text_page)
202 return 0;
dsinclaira28ae382016-04-19 10:39:24 -0700203
204 CPDF_TextPage* textpage = CPDFTextPageFromFPDFTextPage(text_page);
Dan Sinclair05df0752017-03-14 14:43:42 -0400205 CFX_FloatRect rect((float)left, (float)bottom, (float)right, (float)top);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700206 CFX_WideString str = textpage->GetTextByRect(rect);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700207
dsinclaira28ae382016-04-19 10:39:24 -0700208 if (buflen <= 0 || !buffer)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700209 return str.GetLength();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700210
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700211 CFX_ByteString cbUTF16Str = str.UTF16LE_Encode();
212 int len = cbUTF16Str.GetLength() / sizeof(unsigned short);
213 int size = buflen > len ? len : buflen;
Dan Sinclair1c5d0b42017-04-03 15:05:11 -0400214 memcpy(buffer, cbUTF16Str.GetBuffer(size * sizeof(unsigned short)),
215 size * sizeof(unsigned short));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700216 cbUTF16Str.ReleaseBuffer(size * sizeof(unsigned short));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700217
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700218 return size;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700219}
220
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700221// Search
Dan Sinclair50cce602016-02-24 09:51:16 -0500222// -1 for end
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400223FPDF_EXPORT FPDF_SCHHANDLE FPDF_CALLCONV
224FPDFText_FindStart(FPDF_TEXTPAGE text_page,
225 FPDF_WIDESTRING findwhat,
226 unsigned long flags,
227 int start_index) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700228 if (!text_page)
dsinclaira28ae382016-04-19 10:39:24 -0700229 return nullptr;
230
231 CPDF_TextPageFind* textpageFind =
232 new CPDF_TextPageFind(CPDFTextPageFromFPDFTextPage(text_page));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700233 FX_STRSIZE len = CFX_WideString::WStringLength(findwhat);
234 textpageFind->FindFirst(CFX_WideString::FromUTF16LE(findwhat, len), flags,
235 start_index);
236 return textpageFind;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700237}
dsinclaira28ae382016-04-19 10:39:24 -0700238
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400239FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFText_FindNext(FPDF_SCHHANDLE handle) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700240 if (!handle)
tsepez4cf55152016-11-02 14:37:54 -0700241 return false;
dsinclaira28ae382016-04-19 10:39:24 -0700242
243 CPDF_TextPageFind* textpageFind = CPDFTextPageFindFromFPDFSchHandle(handle);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700244 return textpageFind->FindNext();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700245}
dsinclaira28ae382016-04-19 10:39:24 -0700246
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400247FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFText_FindPrev(FPDF_SCHHANDLE handle) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700248 if (!handle)
tsepez4cf55152016-11-02 14:37:54 -0700249 return false;
dsinclaira28ae382016-04-19 10:39:24 -0700250
251 CPDF_TextPageFind* textpageFind = CPDFTextPageFindFromFPDFSchHandle(handle);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700252 return textpageFind->FindPrev();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700253}
dsinclaira28ae382016-04-19 10:39:24 -0700254
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400255FPDF_EXPORT int FPDF_CALLCONV
256FPDFText_GetSchResultIndex(FPDF_SCHHANDLE handle) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700257 if (!handle)
258 return 0;
dsinclaira28ae382016-04-19 10:39:24 -0700259
260 CPDF_TextPageFind* textpageFind = CPDFTextPageFindFromFPDFSchHandle(handle);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700261 return textpageFind->GetCurOrder();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700262}
dsinclaira28ae382016-04-19 10:39:24 -0700263
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400264FPDF_EXPORT int FPDF_CALLCONV FPDFText_GetSchCount(FPDF_SCHHANDLE handle) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700265 if (!handle)
266 return 0;
dsinclaira28ae382016-04-19 10:39:24 -0700267
268 CPDF_TextPageFind* textpageFind = CPDFTextPageFindFromFPDFSchHandle(handle);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700269 return textpageFind->GetMatchedCount();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700270}
dsinclaira28ae382016-04-19 10:39:24 -0700271
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400272FPDF_EXPORT void FPDF_CALLCONV FPDFText_FindClose(FPDF_SCHHANDLE handle) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700273 if (!handle)
274 return;
dsinclaira28ae382016-04-19 10:39:24 -0700275
276 CPDF_TextPageFind* textpageFind = CPDFTextPageFindFromFPDFSchHandle(handle);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700277 delete textpageFind;
dsinclaira28ae382016-04-19 10:39:24 -0700278 handle = nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700279}
280
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700281// web link
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400282FPDF_EXPORT FPDF_PAGELINK FPDF_CALLCONV
283FPDFLink_LoadWebLinks(FPDF_TEXTPAGE text_page) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700284 if (!text_page)
dsinclaira28ae382016-04-19 10:39:24 -0700285 return nullptr;
286
tsepez69141182016-04-21 10:43:39 -0700287 CPDF_LinkExtract* pageLink =
288 new CPDF_LinkExtract(CPDFTextPageFromFPDFTextPage(text_page));
289 pageLink->ExtractLinks();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700290 return pageLink;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700291}
dsinclaira28ae382016-04-19 10:39:24 -0700292
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400293FPDF_EXPORT int FPDF_CALLCONV FPDFLink_CountWebLinks(FPDF_PAGELINK link_page) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700294 if (!link_page)
295 return 0;
dsinclaira28ae382016-04-19 10:39:24 -0700296
297 CPDF_LinkExtract* pageLink = CPDFLinkExtractFromFPDFPageLink(link_page);
tsepez69141182016-04-21 10:43:39 -0700298 return pdfium::base::checked_cast<int>(pageLink->CountLinks());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700299}
dsinclaira28ae382016-04-19 10:39:24 -0700300
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400301FPDF_EXPORT int FPDF_CALLCONV FPDFLink_GetURL(FPDF_PAGELINK link_page,
302 int link_index,
303 unsigned short* buffer,
304 int buflen) {
tsepez69141182016-04-21 10:43:39 -0700305 CFX_WideString wsUrl(L"");
306 if (link_page && link_index >= 0) {
307 CPDF_LinkExtract* pageLink = CPDFLinkExtractFromFPDFPageLink(link_page);
308 wsUrl = pageLink->GetURL(link_index);
309 }
310 CFX_ByteString cbUTF16URL = wsUrl.UTF16LE_Encode();
311 int required = cbUTF16URL.GetLength() / sizeof(unsigned short);
Lei Zhang412e9082015-12-14 18:34:00 -0800312 if (!buffer || buflen <= 0)
tsepez69141182016-04-21 10:43:39 -0700313 return required;
dsinclaira28ae382016-04-19 10:39:24 -0700314
tsepez69141182016-04-21 10:43:39 -0700315 int size = std::min(required, buflen);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700316 if (size > 0) {
dsinclaira28ae382016-04-19 10:39:24 -0700317 int buf_size = size * sizeof(unsigned short);
Dan Sinclair1c5d0b42017-04-03 15:05:11 -0400318 memcpy(buffer, cbUTF16URL.GetBuffer(buf_size), buf_size);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700319 }
320 return size;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700321}
dsinclaira28ae382016-04-19 10:39:24 -0700322
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400323FPDF_EXPORT int FPDF_CALLCONV FPDFLink_CountRects(FPDF_PAGELINK link_page,
324 int link_index) {
tsepez69141182016-04-21 10:43:39 -0700325 if (!link_page || link_index < 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700326 return 0;
dsinclaira28ae382016-04-19 10:39:24 -0700327
328 CPDF_LinkExtract* pageLink = CPDFLinkExtractFromFPDFPageLink(link_page);
tsepezdf964df2016-04-21 12:09:41 -0700329 return pdfium::CollectionSize<int>(pageLink->GetRects(link_index));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700330}
dsinclaira28ae382016-04-19 10:39:24 -0700331
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400332FPDF_EXPORT void FPDF_CALLCONV FPDFLink_GetRect(FPDF_PAGELINK link_page,
333 int link_index,
334 int rect_index,
335 double* left,
336 double* top,
337 double* right,
338 double* bottom) {
tsepez69141182016-04-21 10:43:39 -0700339 if (!link_page || link_index < 0 || rect_index < 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700340 return;
dsinclaira28ae382016-04-19 10:39:24 -0700341
tsepez69141182016-04-21 10:43:39 -0700342 CPDF_LinkExtract* pageLink = CPDFLinkExtractFromFPDFPageLink(link_page);
tsepezdf964df2016-04-21 12:09:41 -0700343 std::vector<CFX_FloatRect> rectArray = pageLink->GetRects(link_index);
344 if (rect_index >= pdfium::CollectionSize<int>(rectArray))
tsepez69141182016-04-21 10:43:39 -0700345 return;
346
tsepezdf964df2016-04-21 12:09:41 -0700347 *left = rectArray[rect_index].left;
348 *right = rectArray[rect_index].right;
349 *top = rectArray[rect_index].top;
350 *bottom = rectArray[rect_index].bottom;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700351}
tsepez69141182016-04-21 10:43:39 -0700352
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400353FPDF_EXPORT void FPDF_CALLCONV FPDFLink_CloseWebLinks(FPDF_PAGELINK link_page) {
dsinclaira28ae382016-04-19 10:39:24 -0700354 delete CPDFLinkExtractFromFPDFPageLink(link_page);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700355}