blob: 70a27a5011061bfa91960d1884bc32d69fdcd369 [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_doc.h"
8
Dan Sinclair85c8e7f2016-11-21 13:50:32 -05009#include <memory>
Wei Li0e2e5d72016-03-03 11:28:06 -080010#include <set>
11
dsinclair41872fa2016-10-04 11:29:35 -070012#include "core/fpdfapi/page/cpdf_page.h"
dsinclair488b7ad2016-10-04 11:55:50 -070013#include "core/fpdfapi/parser/cpdf_array.h"
14#include "core/fpdfapi/parser/cpdf_document.h"
dsinclair1727aee2016-09-29 13:12:56 -070015#include "core/fpdfdoc/cpdf_bookmark.h"
16#include "core/fpdfdoc/cpdf_bookmarktree.h"
dsinclairc59fa882016-11-08 06:55:40 -080017#include "core/fpdfdoc/cpdf_dest.h"
thestig733e0682016-11-23 05:52:39 -080018#include "core/fpdfdoc/cpdf_pagelabel.h"
dsinclair114e46a2016-09-29 17:18:21 -070019#include "fpdfsdk/fsdk_define.h"
tsepeza9caab92016-12-14 05:57:10 -080020#include "third_party/base/ptr_util.h"
Wei Li0e2e5d72016-03-03 11:28:06 -080021#include "third_party/base/stl_util.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070022
Lei Zhangbdf72c32015-08-14 19:24:08 -070023namespace {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070024
Lei Zhangbdf72c32015-08-14 19:24:08 -070025CPDF_Bookmark FindBookmark(const CPDF_BookmarkTree& tree,
26 CPDF_Bookmark bookmark,
Wei Li0e2e5d72016-03-03 11:28:06 -080027 const CFX_WideString& title,
28 std::set<CPDF_Dictionary*>* visited) {
29 // Return if already checked to avoid circular calling.
30 if (pdfium::ContainsKey(*visited, bookmark.GetDict()))
31 return CPDF_Bookmark();
32 visited->insert(bookmark.GetDict());
33
Wei Li0fc6b252016-03-01 16:29:41 -080034 if (bookmark.GetDict() &&
35 bookmark.GetTitle().CompareNoCase(title.c_str()) == 0) {
Wei Li0e2e5d72016-03-03 11:28:06 -080036 // First check this item.
Nico Weber9d8ec5a2015-08-04 13:00:21 -070037 return bookmark;
38 }
Wei Li0e2e5d72016-03-03 11:28:06 -080039
40 // Go into children items.
Nico Weber9d8ec5a2015-08-04 13:00:21 -070041 CPDF_Bookmark child = tree.GetFirstChild(bookmark);
Wei Li0e2e5d72016-03-03 11:28:06 -080042 while (child.GetDict() && !pdfium::ContainsKey(*visited, child.GetDict())) {
43 // Check this item and its children.
44 CPDF_Bookmark found = FindBookmark(tree, child, title, visited);
Wei Li0fc6b252016-03-01 16:29:41 -080045 if (found.GetDict())
Nico Weber9d8ec5a2015-08-04 13:00:21 -070046 return found;
47 child = tree.GetNextSibling(child);
48 }
49 return CPDF_Bookmark();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070050}
51
Lei Zhangbdf72c32015-08-14 19:24:08 -070052CPDF_LinkList* GetLinkList(CPDF_Page* page) {
53 if (!page)
54 return nullptr;
55
Lei Zhangbdf72c32015-08-14 19:24:08 -070056 CPDF_Document* pDoc = page->m_pDocument;
tsepez5ce09682016-05-25 16:16:32 -070057 std::unique_ptr<CPDF_LinkList>* pHolder = pDoc->LinksContext();
58 if (!pHolder->get())
tsepeza9caab92016-12-14 05:57:10 -080059 *pHolder = pdfium::MakeUnique<CPDF_LinkList>();
tsepez5ce09682016-05-25 16:16:32 -070060 return pHolder->get();
Lei Zhangbdf72c32015-08-14 19:24:08 -070061}
62
thestig733e0682016-11-23 05:52:39 -080063unsigned long Utf16EncodeMaybeCopyAndReturnLength(const CFX_WideString& text,
64 void* buffer,
65 unsigned long buflen) {
66 CFX_ByteString encodedText = text.UTF16LE_Encode();
67 unsigned long len = encodedText.GetLength();
thestig9067fd62016-11-23 14:10:06 -080068 if (buffer && len <= buflen)
Dan Sinclair1c5d0b42017-04-03 15:05:11 -040069 memcpy(buffer, encodedText.c_str(), len);
thestig733e0682016-11-23 05:52:39 -080070 return len;
71}
72
Lei Zhangbdf72c32015-08-14 19:24:08 -070073} // namespace
74
Nico Weber9d8ec5a2015-08-04 13:00:21 -070075DLLEXPORT FPDF_BOOKMARK STDCALL
76FPDFBookmark_GetFirstChild(FPDF_DOCUMENT document, FPDF_BOOKMARK pDict) {
Tom Sepez471a1032015-10-15 16:17:18 -070077 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
78 if (!pDoc)
79 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070080 CPDF_BookmarkTree tree(pDoc);
Dan Sinclairf1251c12015-10-20 16:24:45 -040081 CPDF_Bookmark bookmark =
82 CPDF_Bookmark(ToDictionary(static_cast<CPDF_Object*>(pDict)));
Nico Weber9d8ec5a2015-08-04 13:00:21 -070083 return tree.GetFirstChild(bookmark).GetDict();
Bo Xu4d62b6b2015-01-10 22:52:59 -080084}
85
Nico Weber9d8ec5a2015-08-04 13:00:21 -070086DLLEXPORT FPDF_BOOKMARK STDCALL
87FPDFBookmark_GetNextSibling(FPDF_DOCUMENT document, FPDF_BOOKMARK pDict) {
Tom Sepez471a1032015-10-15 16:17:18 -070088 if (!pDict)
89 return nullptr;
90 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
91 if (!pDoc)
92 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070093 CPDF_BookmarkTree tree(pDoc);
Dan Sinclairf1251c12015-10-20 16:24:45 -040094 CPDF_Bookmark bookmark =
95 CPDF_Bookmark(ToDictionary(static_cast<CPDF_Object*>(pDict)));
Nico Weber9d8ec5a2015-08-04 13:00:21 -070096 return tree.GetNextSibling(bookmark).GetDict();
Bo Xu4d62b6b2015-01-10 22:52:59 -080097}
98
Nico Weber9d8ec5a2015-08-04 13:00:21 -070099DLLEXPORT unsigned long STDCALL FPDFBookmark_GetTitle(FPDF_BOOKMARK pDict,
100 void* buffer,
101 unsigned long buflen) {
102 if (!pDict)
103 return 0;
Dan Sinclairf1251c12015-10-20 16:24:45 -0400104 CPDF_Bookmark bookmark(ToDictionary(static_cast<CPDF_Object*>(pDict)));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700105 CFX_WideString title = bookmark.GetTitle();
thestig733e0682016-11-23 05:52:39 -0800106 return Utf16EncodeMaybeCopyAndReturnLength(title, buffer, buflen);
Bo Xu4d62b6b2015-01-10 22:52:59 -0800107}
108
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700109DLLEXPORT FPDF_BOOKMARK STDCALL FPDFBookmark_Find(FPDF_DOCUMENT document,
110 FPDF_WIDESTRING title) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700111 if (!title || title[0] == 0)
Tom Sepez471a1032015-10-15 16:17:18 -0700112 return nullptr;
113 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
114 if (!pDoc)
115 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700116 CPDF_BookmarkTree tree(pDoc);
117 FX_STRSIZE len = CFX_WideString::WStringLength(title);
118 CFX_WideString encodedTitle = CFX_WideString::FromUTF16LE(title, len);
Wei Li0e2e5d72016-03-03 11:28:06 -0800119 std::set<CPDF_Dictionary*> visited;
120 return FindBookmark(tree, CPDF_Bookmark(), encodedTitle, &visited).GetDict();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700121}
122
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700123DLLEXPORT FPDF_DEST STDCALL FPDFBookmark_GetDest(FPDF_DOCUMENT document,
124 FPDF_BOOKMARK pDict) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700125 if (!pDict)
Tom Sepez471a1032015-10-15 16:17:18 -0700126 return nullptr;
127 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
128 if (!pDoc)
129 return nullptr;
Dan Sinclairf1251c12015-10-20 16:24:45 -0400130 CPDF_Bookmark bookmark(ToDictionary(static_cast<CPDF_Object*>(pDict)));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700131 CPDF_Dest dest = bookmark.GetDest(pDoc);
Wei Li0fc6b252016-03-01 16:29:41 -0800132 if (dest.GetObject())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700133 return dest.GetObject();
134 // If this bookmark is not directly associated with a dest, we try to get
135 // action
136 CPDF_Action action = bookmark.GetAction();
Wei Li0fc6b252016-03-01 16:29:41 -0800137 if (!action.GetDict())
Tom Sepez471a1032015-10-15 16:17:18 -0700138 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700139 return action.GetDest(pDoc).GetObject();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700140}
141
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700142DLLEXPORT FPDF_ACTION STDCALL FPDFBookmark_GetAction(FPDF_BOOKMARK pDict) {
143 if (!pDict)
thestig1cd352e2016-06-07 17:53:06 -0700144 return nullptr;
Dan Sinclairf1251c12015-10-20 16:24:45 -0400145 CPDF_Bookmark bookmark(ToDictionary(static_cast<CPDF_Object*>(pDict)));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700146 return bookmark.GetAction().GetDict();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700147}
148
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700149DLLEXPORT unsigned long STDCALL FPDFAction_GetType(FPDF_ACTION pDict) {
150 if (!pDict)
Lei Zhange0947b32015-09-17 14:51:48 -0700151 return PDFACTION_UNSUPPORTED;
152
Dan Sinclairf1251c12015-10-20 16:24:45 -0400153 CPDF_Action action(ToDictionary(static_cast<CPDF_Object*>(pDict)));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700154 CPDF_Action::ActionType type = action.GetType();
155 switch (type) {
156 case CPDF_Action::GoTo:
157 return PDFACTION_GOTO;
158 case CPDF_Action::GoToR:
159 return PDFACTION_REMOTEGOTO;
160 case CPDF_Action::URI:
161 return PDFACTION_URI;
162 case CPDF_Action::Launch:
163 return PDFACTION_LAUNCH;
164 default:
165 return PDFACTION_UNSUPPORTED;
166 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700167}
168
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700169DLLEXPORT FPDF_DEST STDCALL FPDFAction_GetDest(FPDF_DOCUMENT document,
170 FPDF_ACTION pDict) {
Tom Sepez471a1032015-10-15 16:17:18 -0700171 if (!pDict)
Lei Zhange0947b32015-09-17 14:51:48 -0700172 return nullptr;
Tom Sepez471a1032015-10-15 16:17:18 -0700173 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
174 if (!pDoc)
175 return nullptr;
Dan Sinclairf1251c12015-10-20 16:24:45 -0400176 CPDF_Action action(ToDictionary(static_cast<CPDF_Object*>(pDict)));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700177 return action.GetDest(pDoc).GetObject();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700178}
179
Dan Sinclairf766ad22016-03-14 13:51:24 -0400180DLLEXPORT unsigned long STDCALL FPDFAction_GetFilePath(FPDF_ACTION pDict,
181 void* buffer,
182 unsigned long buflen) {
Lei Zhange0947b32015-09-17 14:51:48 -0700183 unsigned long type = FPDFAction_GetType(pDict);
184 if (type != PDFACTION_REMOTEGOTO && type != PDFACTION_LAUNCH)
185 return 0;
186
Dan Sinclairf1251c12015-10-20 16:24:45 -0400187 CPDF_Action action(ToDictionary(static_cast<CPDF_Object*>(pDict)));
Lei Zhange0947b32015-09-17 14:51:48 -0700188 CFX_ByteString path = action.GetFilePath().UTF8Encode();
189 unsigned long len = path.GetLength() + 1;
thestig9067fd62016-11-23 14:10:06 -0800190 if (buffer && len <= buflen)
Dan Sinclair1c5d0b42017-04-03 15:05:11 -0400191 memcpy(buffer, path.c_str(), len);
Lei Zhange0947b32015-09-17 14:51:48 -0700192 return len;
193}
194
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700195DLLEXPORT unsigned long STDCALL FPDFAction_GetURIPath(FPDF_DOCUMENT document,
196 FPDF_ACTION pDict,
197 void* buffer,
198 unsigned long buflen) {
Tom Sepez471a1032015-10-15 16:17:18 -0700199 if (!pDict)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700200 return 0;
Tom Sepez471a1032015-10-15 16:17:18 -0700201 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
202 if (!pDoc)
203 return 0;
Dan Sinclairf1251c12015-10-20 16:24:45 -0400204 CPDF_Action action(ToDictionary(static_cast<CPDF_Object*>(pDict)));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700205 CFX_ByteString path = action.GetURI(pDoc);
206 unsigned long len = path.GetLength() + 1;
thestig9067fd62016-11-23 14:10:06 -0800207 if (buffer && len <= buflen)
Dan Sinclair1c5d0b42017-04-03 15:05:11 -0400208 memcpy(buffer, path.c_str(), len);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700209 return len;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700210}
211
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700212DLLEXPORT unsigned long STDCALL FPDFDest_GetPageIndex(FPDF_DOCUMENT document,
213 FPDF_DEST pDict) {
Tom Sepez471a1032015-10-15 16:17:18 -0700214 if (!pDict)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700215 return 0;
Tom Sepez471a1032015-10-15 16:17:18 -0700216 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
217 if (!pDoc)
218 return 0;
Dan Sinclair2b11dc12015-10-22 15:02:06 -0400219 CPDF_Dest dest(static_cast<CPDF_Array*>(pDict));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700220 return dest.GetPageIndex(pDoc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700221}
222
dsinclairc59fa882016-11-08 06:55:40 -0800223DLLEXPORT FPDF_BOOL STDCALL FPDFDest_GetLocationInPage(FPDF_DEST pDict,
224 FPDF_BOOL* hasXVal,
225 FPDF_BOOL* hasYVal,
226 FPDF_BOOL* hasZoomVal,
227 FS_FLOAT* x,
228 FS_FLOAT* y,
229 FS_FLOAT* zoom) {
230 if (!pDict)
231 return false;
232
Tom Sepezfe91c6c2017-05-16 15:33:20 -0700233 auto dest = pdfium::MakeUnique<CPDF_Dest>(static_cast<CPDF_Object*>(pDict));
dsinclairc59fa882016-11-08 06:55:40 -0800234
235 // FPDF_BOOL is an int, GetXYZ expects bools.
236 bool bHasX;
237 bool bHasY;
238 bool bHasZoom;
239 if (!dest->GetXYZ(&bHasX, &bHasY, &bHasZoom, x, y, zoom))
240 return false;
241
242 *hasXVal = bHasX;
243 *hasYVal = bHasY;
244 *hasZoomVal = bHasZoom;
245 return true;
246}
247
Dan Sinclairf766ad22016-03-14 13:51:24 -0400248DLLEXPORT FPDF_LINK STDCALL FPDFLink_GetLinkAtPoint(FPDF_PAGE page,
249 double x,
250 double y) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700251 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Lei Zhangbdf72c32015-08-14 19:24:08 -0700252 if (!pPage)
253 return nullptr;
254
255 CPDF_LinkList* pLinkList = GetLinkList(pPage);
256 if (!pLinkList)
257 return nullptr;
258
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500259 return pLinkList
Dan Sinclair05df0752017-03-14 14:43:42 -0400260 ->GetLinkAtPoint(pPage,
261 CFX_PointF(static_cast<float>(x), static_cast<float>(y)),
262 nullptr)
Lei Zhangbdf72c32015-08-14 19:24:08 -0700263 .GetDict();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700264}
265
Dan Sinclairf766ad22016-03-14 13:51:24 -0400266DLLEXPORT int STDCALL FPDFLink_GetLinkZOrderAtPoint(FPDF_PAGE page,
267 double x,
268 double y) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700269 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700270 if (!pPage)
Lei Zhangbdf72c32015-08-14 19:24:08 -0700271 return -1;
272
273 CPDF_LinkList* pLinkList = GetLinkList(pPage);
274 if (!pLinkList)
275 return -1;
276
277 int z_order = -1;
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500278 pLinkList->GetLinkAtPoint(
Dan Sinclair05df0752017-03-14 14:43:42 -0400279 pPage, CFX_PointF(static_cast<float>(x), static_cast<float>(y)),
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500280 &z_order);
Lei Zhangbdf72c32015-08-14 19:24:08 -0700281 return z_order;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700282}
283
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700284DLLEXPORT FPDF_DEST STDCALL FPDFLink_GetDest(FPDF_DOCUMENT document,
285 FPDF_LINK pDict) {
Tom Sepez471a1032015-10-15 16:17:18 -0700286 if (!pDict)
Lei Zhange0947b32015-09-17 14:51:48 -0700287 return nullptr;
Tom Sepez471a1032015-10-15 16:17:18 -0700288 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
289 if (!pDoc)
290 return nullptr;
Dan Sinclairf1251c12015-10-20 16:24:45 -0400291 CPDF_Link link(ToDictionary(static_cast<CPDF_Object*>(pDict)));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700292 FPDF_DEST dest = link.GetDest(pDoc).GetObject();
293 if (dest)
294 return dest;
295 // If this link is not directly associated with a dest, we try to get action
296 CPDF_Action action = link.GetAction();
Wei Li0fc6b252016-03-01 16:29:41 -0800297 if (!action.GetDict())
Lei Zhange0947b32015-09-17 14:51:48 -0700298 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700299 return action.GetDest(pDoc).GetObject();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700300}
301
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700302DLLEXPORT FPDF_ACTION STDCALL FPDFLink_GetAction(FPDF_LINK pDict) {
303 if (!pDict)
Lei Zhange0947b32015-09-17 14:51:48 -0700304 return nullptr;
305
Dan Sinclairf1251c12015-10-20 16:24:45 -0400306 CPDF_Link link(ToDictionary(static_cast<CPDF_Object*>(pDict)));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700307 return link.GetAction().GetDict();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700308}
309
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700310DLLEXPORT FPDF_BOOL STDCALL FPDFLink_Enumerate(FPDF_PAGE page,
311 int* startPos,
312 FPDF_LINK* linkAnnot) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700313 if (!startPos || !linkAnnot)
tsepez4cf55152016-11-02 14:37:54 -0700314 return false;
Tom Sepezdb0be962015-10-16 14:00:21 -0700315 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
316 if (!pPage || !pPage->m_pFormDict)
tsepez4cf55152016-11-02 14:37:54 -0700317 return false;
dsinclair38fd8442016-09-15 10:15:32 -0700318 CPDF_Array* pAnnots = pPage->m_pFormDict->GetArrayFor("Annots");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700319 if (!pAnnots)
tsepez4cf55152016-11-02 14:37:54 -0700320 return false;
Wei Lie1aebd42016-04-11 10:02:09 -0700321 for (size_t i = *startPos; i < pAnnots->GetCount(); i++) {
Dan Sinclairf1251c12015-10-20 16:24:45 -0400322 CPDF_Dictionary* pDict =
tsepezbd567552016-03-29 14:51:50 -0700323 ToDictionary(static_cast<CPDF_Object*>(pAnnots->GetDirectObjectAt(i)));
Dan Sinclairf1251c12015-10-20 16:24:45 -0400324 if (!pDict)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700325 continue;
dsinclair38fd8442016-09-15 10:15:32 -0700326 if (pDict->GetStringFor("Subtype") == "Link") {
Wei Lie1aebd42016-04-11 10:02:09 -0700327 *startPos = static_cast<int>(i + 1);
328 *linkAnnot = static_cast<FPDF_LINK>(pDict);
tsepez4cf55152016-11-02 14:37:54 -0700329 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700330 }
331 }
tsepez4cf55152016-11-02 14:37:54 -0700332 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700333}
334
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700335DLLEXPORT FPDF_BOOL STDCALL FPDFLink_GetAnnotRect(FPDF_LINK linkAnnot,
336 FS_RECTF* rect) {
337 if (!linkAnnot || !rect)
tsepez4cf55152016-11-02 14:37:54 -0700338 return false;
Dan Sinclairf1251c12015-10-20 16:24:45 -0400339 CPDF_Dictionary* pAnnotDict =
340 ToDictionary(static_cast<CPDF_Object*>(linkAnnot));
dsinclair38fd8442016-09-15 10:15:32 -0700341 CFX_FloatRect rt = pAnnotDict->GetRectFor("Rect");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700342 rect->left = rt.left;
343 rect->bottom = rt.bottom;
344 rect->right = rt.right;
345 rect->top = rt.top;
tsepez4cf55152016-11-02 14:37:54 -0700346 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700347}
348
349DLLEXPORT int STDCALL FPDFLink_CountQuadPoints(FPDF_LINK linkAnnot) {
350 if (!linkAnnot)
351 return 0;
Dan Sinclairf1251c12015-10-20 16:24:45 -0400352 CPDF_Dictionary* pAnnotDict =
353 ToDictionary(static_cast<CPDF_Object*>(linkAnnot));
dsinclair38fd8442016-09-15 10:15:32 -0700354 CPDF_Array* pArray = pAnnotDict->GetArrayFor("QuadPoints");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700355 if (!pArray)
356 return 0;
Wei Lie1aebd42016-04-11 10:02:09 -0700357 return static_cast<int>(pArray->GetCount() / 8);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700358}
359
360DLLEXPORT FPDF_BOOL STDCALL FPDFLink_GetQuadPoints(FPDF_LINK linkAnnot,
361 int quadIndex,
362 FS_QUADPOINTSF* quadPoints) {
363 if (!linkAnnot || !quadPoints)
tsepez4cf55152016-11-02 14:37:54 -0700364 return false;
Dan Sinclairf1251c12015-10-20 16:24:45 -0400365 CPDF_Dictionary* pAnnotDict =
366 ToDictionary(static_cast<CPDF_Object*>(linkAnnot));
dsinclair38fd8442016-09-15 10:15:32 -0700367 CPDF_Array* pArray = pAnnotDict->GetArrayFor("QuadPoints");
thestig733e0682016-11-23 05:52:39 -0800368 if (!pArray)
369 return false;
370
371 if (quadIndex < 0 ||
372 static_cast<size_t>(quadIndex) >= pArray->GetCount() / 8 ||
373 (static_cast<size_t>(quadIndex * 8 + 7) >= pArray->GetCount())) {
374 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700375 }
thestig733e0682016-11-23 05:52:39 -0800376
377 quadPoints->x1 = pArray->GetNumberAt(quadIndex * 8);
378 quadPoints->y1 = pArray->GetNumberAt(quadIndex * 8 + 1);
379 quadPoints->x2 = pArray->GetNumberAt(quadIndex * 8 + 2);
380 quadPoints->y2 = pArray->GetNumberAt(quadIndex * 8 + 3);
381 quadPoints->x3 = pArray->GetNumberAt(quadIndex * 8 + 4);
382 quadPoints->y3 = pArray->GetNumberAt(quadIndex * 8 + 5);
383 quadPoints->x4 = pArray->GetNumberAt(quadIndex * 8 + 6);
384 quadPoints->y4 = pArray->GetNumberAt(quadIndex * 8 + 7);
385 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700386}
387
thestig733e0682016-11-23 05:52:39 -0800388DLLEXPORT unsigned long STDCALL FPDF_GetMetaText(FPDF_DOCUMENT document,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700389 FPDF_BYTESTRING tag,
390 void* buffer,
391 unsigned long buflen) {
Tom Sepez471a1032015-10-15 16:17:18 -0700392 if (!tag)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700393 return 0;
thestig733e0682016-11-23 05:52:39 -0800394 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Tom Sepez471a1032015-10-15 16:17:18 -0700395 if (!pDoc)
396 return 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700397 CPDF_Dictionary* pInfo = pDoc->GetInfo();
398 if (!pInfo)
399 return 0;
dsinclair38fd8442016-09-15 10:15:32 -0700400 CFX_WideString text = pInfo->GetUnicodeTextFor(tag);
thestig733e0682016-11-23 05:52:39 -0800401 return Utf16EncodeMaybeCopyAndReturnLength(text, buffer, buflen);
402}
403
dsinclair6bdb56c2016-12-06 09:53:27 -0800404DLLEXPORT unsigned long STDCALL FPDF_GetPageLabel(FPDF_DOCUMENT document,
405 int page_index,
406 void* buffer,
407 unsigned long buflen) {
thestig733e0682016-11-23 05:52:39 -0800408 if (page_index < 0)
409 return 0;
410
411 // CPDF_PageLabel can deal with NULL |document|.
412 CPDF_PageLabel label(CPDFDocumentFromFPDFDocument(document));
413 CFX_WideString str;
414 if (!label.GetLabel(page_index, &str))
415 return 0;
416 return Utf16EncodeMaybeCopyAndReturnLength(str, buffer, buflen);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700417}