blob: 916233c56e749cfff07491feea316be312c9ebc3 [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
Henrique Nakashima7e805d12017-08-10 15:13:19 +000046DLLEXPORT FPDF_TEXTPAGE STDCALL 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
Henrique Nakashima7e805d12017-08-10 15:13:19 +000066DLLEXPORT void STDCALL 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
Henrique Nakashima7e805d12017-08-10 15:13:19 +000070DLLEXPORT int STDCALL 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
Henrique Nakashima7e805d12017-08-10 15:13:19 +000078DLLEXPORT unsigned int STDCALL FPDFText_GetUnicode(FPDF_TEXTPAGE text_page,
79 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
Henrique Nakashima7e805d12017-08-10 15:13:19 +000092DLLEXPORT double STDCALL 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
Henrique Nakashima7e805d12017-08-10 15:13:19 +0000106DLLEXPORT void STDCALL 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
Henrique Nakashima7e805d12017-08-10 15:13:19 +0000127DLLEXPORT int STDCALL FPDFText_GetCharIndexAtPos(FPDF_TEXTPAGE text_page,
128 double x,
129 double y,
130 double xTolerance,
131 double yTolerance) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700132 if (!text_page)
133 return -3;
dsinclaira28ae382016-04-19 10:39:24 -0700134
135 CPDF_TextPage* textpage = CPDFTextPageFromFPDFTextPage(text_page);
Dan Sinclaird476adc2017-02-21 14:31:41 -0500136 return textpage->GetIndexAtPos(
Dan Sinclair05df0752017-03-14 14:43:42 -0400137 CFX_PointF(static_cast<float>(x), static_cast<float>(y)),
138 CFX_SizeF(static_cast<float>(xTolerance),
139 static_cast<float>(yTolerance)));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700140}
141
Henrique Nakashima7e805d12017-08-10 15:13:19 +0000142DLLEXPORT int STDCALL FPDFText_GetText(FPDF_TEXTPAGE text_page,
143 int start,
144 int count,
145 unsigned short* result) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700146 if (!text_page)
147 return 0;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -0700148
dsinclaira28ae382016-04-19 10:39:24 -0700149 CPDF_TextPage* textpage = CPDFTextPageFromFPDFTextPage(text_page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700150 if (start >= textpage->CountChars())
151 return 0;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700152
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700153 CFX_WideString str = textpage->GetPageText(start, count);
154 if (str.GetLength() > count)
155 str = str.Left(count);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700156
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700157 CFX_ByteString cbUTF16str = str.UTF16LE_Encode();
Dan Sinclair1c5d0b42017-04-03 15:05:11 -0400158 memcpy(result, cbUTF16str.GetBuffer(cbUTF16str.GetLength()),
159 cbUTF16str.GetLength());
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700160 cbUTF16str.ReleaseBuffer(cbUTF16str.GetLength());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700161
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700162 return cbUTF16str.GetLength() / sizeof(unsigned short);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700163}
164
Henrique Nakashima7e805d12017-08-10 15:13:19 +0000165DLLEXPORT int STDCALL FPDFText_CountRects(FPDF_TEXTPAGE text_page,
166 int start,
167 int count) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700168 if (!text_page)
169 return 0;
dsinclaira28ae382016-04-19 10:39:24 -0700170
171 CPDF_TextPage* textpage = CPDFTextPageFromFPDFTextPage(text_page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700172 return textpage->CountRects(start, count);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700173}
dsinclaira28ae382016-04-19 10:39:24 -0700174
Henrique Nakashima7e805d12017-08-10 15:13:19 +0000175DLLEXPORT void STDCALL FPDFText_GetRect(FPDF_TEXTPAGE text_page,
176 int rect_index,
177 double* left,
178 double* top,
179 double* right,
180 double* bottom) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700181 if (!text_page)
182 return;
dsinclaira28ae382016-04-19 10:39:24 -0700183
184 CPDF_TextPage* textpage = CPDFTextPageFromFPDFTextPage(text_page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700185 CFX_FloatRect rect;
186 textpage->GetRect(rect_index, rect.left, rect.top, rect.right, rect.bottom);
187 *left = rect.left;
188 *top = rect.top;
189 *right = rect.right;
190 *bottom = rect.bottom;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700191}
192
Henrique Nakashima7e805d12017-08-10 15:13:19 +0000193DLLEXPORT int STDCALL FPDFText_GetBoundedText(FPDF_TEXTPAGE text_page,
194 double left,
195 double top,
196 double right,
197 double bottom,
198 unsigned short* buffer,
199 int buflen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700200 if (!text_page)
201 return 0;
dsinclaira28ae382016-04-19 10:39:24 -0700202
203 CPDF_TextPage* textpage = CPDFTextPageFromFPDFTextPage(text_page);
Dan Sinclair05df0752017-03-14 14:43:42 -0400204 CFX_FloatRect rect((float)left, (float)bottom, (float)right, (float)top);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700205 CFX_WideString str = textpage->GetTextByRect(rect);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700206
dsinclaira28ae382016-04-19 10:39:24 -0700207 if (buflen <= 0 || !buffer)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700208 return str.GetLength();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700209
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700210 CFX_ByteString cbUTF16Str = str.UTF16LE_Encode();
211 int len = cbUTF16Str.GetLength() / sizeof(unsigned short);
212 int size = buflen > len ? len : buflen;
Dan Sinclair1c5d0b42017-04-03 15:05:11 -0400213 memcpy(buffer, cbUTF16Str.GetBuffer(size * sizeof(unsigned short)),
214 size * sizeof(unsigned short));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700215 cbUTF16Str.ReleaseBuffer(size * sizeof(unsigned short));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700216
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700217 return size;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700218}
219
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700220// Search
Dan Sinclair50cce602016-02-24 09:51:16 -0500221// -1 for end
Henrique Nakashima7e805d12017-08-10 15:13:19 +0000222DLLEXPORT FPDF_SCHHANDLE STDCALL FPDFText_FindStart(FPDF_TEXTPAGE text_page,
223 FPDF_WIDESTRING findwhat,
224 unsigned long flags,
225 int start_index) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700226 if (!text_page)
dsinclaira28ae382016-04-19 10:39:24 -0700227 return nullptr;
228
229 CPDF_TextPageFind* textpageFind =
230 new CPDF_TextPageFind(CPDFTextPageFromFPDFTextPage(text_page));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700231 FX_STRSIZE len = CFX_WideString::WStringLength(findwhat);
232 textpageFind->FindFirst(CFX_WideString::FromUTF16LE(findwhat, len), flags,
233 start_index);
234 return textpageFind;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700235}
dsinclaira28ae382016-04-19 10:39:24 -0700236
Henrique Nakashima7e805d12017-08-10 15:13:19 +0000237DLLEXPORT FPDF_BOOL STDCALL FPDFText_FindNext(FPDF_SCHHANDLE handle) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700238 if (!handle)
tsepez4cf55152016-11-02 14:37:54 -0700239 return false;
dsinclaira28ae382016-04-19 10:39:24 -0700240
241 CPDF_TextPageFind* textpageFind = CPDFTextPageFindFromFPDFSchHandle(handle);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700242 return textpageFind->FindNext();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700243}
dsinclaira28ae382016-04-19 10:39:24 -0700244
Henrique Nakashima7e805d12017-08-10 15:13:19 +0000245DLLEXPORT FPDF_BOOL STDCALL FPDFText_FindPrev(FPDF_SCHHANDLE handle) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700246 if (!handle)
tsepez4cf55152016-11-02 14:37:54 -0700247 return false;
dsinclaira28ae382016-04-19 10:39:24 -0700248
249 CPDF_TextPageFind* textpageFind = CPDFTextPageFindFromFPDFSchHandle(handle);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700250 return textpageFind->FindPrev();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700251}
dsinclaira28ae382016-04-19 10:39:24 -0700252
Henrique Nakashima7e805d12017-08-10 15:13:19 +0000253DLLEXPORT int STDCALL FPDFText_GetSchResultIndex(FPDF_SCHHANDLE handle) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700254 if (!handle)
255 return 0;
dsinclaira28ae382016-04-19 10:39:24 -0700256
257 CPDF_TextPageFind* textpageFind = CPDFTextPageFindFromFPDFSchHandle(handle);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700258 return textpageFind->GetCurOrder();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700259}
dsinclaira28ae382016-04-19 10:39:24 -0700260
Henrique Nakashima7e805d12017-08-10 15:13:19 +0000261DLLEXPORT int STDCALL FPDFText_GetSchCount(FPDF_SCHHANDLE handle) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700262 if (!handle)
263 return 0;
dsinclaira28ae382016-04-19 10:39:24 -0700264
265 CPDF_TextPageFind* textpageFind = CPDFTextPageFindFromFPDFSchHandle(handle);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700266 return textpageFind->GetMatchedCount();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700267}
dsinclaira28ae382016-04-19 10:39:24 -0700268
Henrique Nakashima7e805d12017-08-10 15:13:19 +0000269DLLEXPORT void STDCALL FPDFText_FindClose(FPDF_SCHHANDLE handle) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700270 if (!handle)
271 return;
dsinclaira28ae382016-04-19 10:39:24 -0700272
273 CPDF_TextPageFind* textpageFind = CPDFTextPageFindFromFPDFSchHandle(handle);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700274 delete textpageFind;
dsinclaira28ae382016-04-19 10:39:24 -0700275 handle = nullptr;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700276}
277
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700278// web link
Henrique Nakashima7e805d12017-08-10 15:13:19 +0000279DLLEXPORT FPDF_PAGELINK STDCALL FPDFLink_LoadWebLinks(FPDF_TEXTPAGE text_page) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700280 if (!text_page)
dsinclaira28ae382016-04-19 10:39:24 -0700281 return nullptr;
282
tsepez69141182016-04-21 10:43:39 -0700283 CPDF_LinkExtract* pageLink =
284 new CPDF_LinkExtract(CPDFTextPageFromFPDFTextPage(text_page));
285 pageLink->ExtractLinks();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700286 return pageLink;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700287}
dsinclaira28ae382016-04-19 10:39:24 -0700288
Henrique Nakashima7e805d12017-08-10 15:13:19 +0000289DLLEXPORT int STDCALL FPDFLink_CountWebLinks(FPDF_PAGELINK link_page) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700290 if (!link_page)
291 return 0;
dsinclaira28ae382016-04-19 10:39:24 -0700292
293 CPDF_LinkExtract* pageLink = CPDFLinkExtractFromFPDFPageLink(link_page);
tsepez69141182016-04-21 10:43:39 -0700294 return pdfium::base::checked_cast<int>(pageLink->CountLinks());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700295}
dsinclaira28ae382016-04-19 10:39:24 -0700296
Henrique Nakashima7e805d12017-08-10 15:13:19 +0000297DLLEXPORT int STDCALL FPDFLink_GetURL(FPDF_PAGELINK link_page,
298 int link_index,
299 unsigned short* buffer,
300 int buflen) {
tsepez69141182016-04-21 10:43:39 -0700301 CFX_WideString wsUrl(L"");
302 if (link_page && link_index >= 0) {
303 CPDF_LinkExtract* pageLink = CPDFLinkExtractFromFPDFPageLink(link_page);
304 wsUrl = pageLink->GetURL(link_index);
305 }
306 CFX_ByteString cbUTF16URL = wsUrl.UTF16LE_Encode();
307 int required = cbUTF16URL.GetLength() / sizeof(unsigned short);
Lei Zhang412e9082015-12-14 18:34:00 -0800308 if (!buffer || buflen <= 0)
tsepez69141182016-04-21 10:43:39 -0700309 return required;
dsinclaira28ae382016-04-19 10:39:24 -0700310
tsepez69141182016-04-21 10:43:39 -0700311 int size = std::min(required, buflen);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700312 if (size > 0) {
dsinclaira28ae382016-04-19 10:39:24 -0700313 int buf_size = size * sizeof(unsigned short);
Dan Sinclair1c5d0b42017-04-03 15:05:11 -0400314 memcpy(buffer, cbUTF16URL.GetBuffer(buf_size), buf_size);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700315 }
316 return size;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700317}
dsinclaira28ae382016-04-19 10:39:24 -0700318
Henrique Nakashima7e805d12017-08-10 15:13:19 +0000319DLLEXPORT int STDCALL FPDFLink_CountRects(FPDF_PAGELINK link_page,
320 int link_index) {
tsepez69141182016-04-21 10:43:39 -0700321 if (!link_page || link_index < 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700322 return 0;
dsinclaira28ae382016-04-19 10:39:24 -0700323
324 CPDF_LinkExtract* pageLink = CPDFLinkExtractFromFPDFPageLink(link_page);
tsepezdf964df2016-04-21 12:09:41 -0700325 return pdfium::CollectionSize<int>(pageLink->GetRects(link_index));
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700326}
dsinclaira28ae382016-04-19 10:39:24 -0700327
Henrique Nakashima7e805d12017-08-10 15:13:19 +0000328DLLEXPORT void STDCALL FPDFLink_GetRect(FPDF_PAGELINK link_page,
329 int link_index,
330 int rect_index,
331 double* left,
332 double* top,
333 double* right,
334 double* bottom) {
tsepez69141182016-04-21 10:43:39 -0700335 if (!link_page || link_index < 0 || rect_index < 0)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700336 return;
dsinclaira28ae382016-04-19 10:39:24 -0700337
tsepez69141182016-04-21 10:43:39 -0700338 CPDF_LinkExtract* pageLink = CPDFLinkExtractFromFPDFPageLink(link_page);
tsepezdf964df2016-04-21 12:09:41 -0700339 std::vector<CFX_FloatRect> rectArray = pageLink->GetRects(link_index);
340 if (rect_index >= pdfium::CollectionSize<int>(rectArray))
tsepez69141182016-04-21 10:43:39 -0700341 return;
342
tsepezdf964df2016-04-21 12:09:41 -0700343 *left = rectArray[rect_index].left;
344 *right = rectArray[rect_index].right;
345 *top = rectArray[rect_index].top;
346 *bottom = rectArray[rect_index].bottom;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700347}
tsepez69141182016-04-21 10:43:39 -0700348
Henrique Nakashima7e805d12017-08-10 15:13:19 +0000349DLLEXPORT void STDCALL FPDFLink_CloseWebLinks(FPDF_PAGELINK link_page) {
dsinclaira28ae382016-04-19 10:39:24 -0700350 delete CPDFLinkExtractFromFPDFPageLink(link_page);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700351}