blob: c536c73254c5f6a28a5512cc65466c040eeb6679 [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,
Ryan Harrison275e2602017-09-18 14:23:18 -040027 const WideString& title,
Wei Li0e2e5d72016-03-03 11:28:06 -080028 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
Tom Sepez4cb82ee2017-05-22 15:15:30 -070056 CPDF_Document* pDoc = page->m_pDocument.Get();
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
63} // namespace
64
Dan Sinclair00d2ad12017-08-10 14:13:02 -040065FPDF_EXPORT FPDF_BOOKMARK FPDF_CALLCONV
Nico Weber9d8ec5a2015-08-04 13:00:21 -070066FPDFBookmark_GetFirstChild(FPDF_DOCUMENT document, FPDF_BOOKMARK pDict) {
Tom Sepez471a1032015-10-15 16:17:18 -070067 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
68 if (!pDoc)
69 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070070 CPDF_BookmarkTree tree(pDoc);
Dan Sinclairf1251c12015-10-20 16:24:45 -040071 CPDF_Bookmark bookmark =
72 CPDF_Bookmark(ToDictionary(static_cast<CPDF_Object*>(pDict)));
Nico Weber9d8ec5a2015-08-04 13:00:21 -070073 return tree.GetFirstChild(bookmark).GetDict();
Bo Xu4d62b6b2015-01-10 22:52:59 -080074}
75
Dan Sinclair00d2ad12017-08-10 14:13:02 -040076FPDF_EXPORT FPDF_BOOKMARK FPDF_CALLCONV
Nico Weber9d8ec5a2015-08-04 13:00:21 -070077FPDFBookmark_GetNextSibling(FPDF_DOCUMENT document, FPDF_BOOKMARK pDict) {
Tom Sepez471a1032015-10-15 16:17:18 -070078 if (!pDict)
79 return nullptr;
80 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
81 if (!pDoc)
82 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070083 CPDF_BookmarkTree tree(pDoc);
Dan Sinclairf1251c12015-10-20 16:24:45 -040084 CPDF_Bookmark bookmark =
85 CPDF_Bookmark(ToDictionary(static_cast<CPDF_Object*>(pDict)));
Nico Weber9d8ec5a2015-08-04 13:00:21 -070086 return tree.GetNextSibling(bookmark).GetDict();
Bo Xu4d62b6b2015-01-10 22:52:59 -080087}
88
Dan Sinclair00d2ad12017-08-10 14:13:02 -040089FPDF_EXPORT unsigned long FPDF_CALLCONV
90FPDFBookmark_GetTitle(FPDF_BOOKMARK pDict, void* buffer, unsigned long buflen) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070091 if (!pDict)
92 return 0;
Dan Sinclairf1251c12015-10-20 16:24:45 -040093 CPDF_Bookmark bookmark(ToDictionary(static_cast<CPDF_Object*>(pDict)));
Ryan Harrison275e2602017-09-18 14:23:18 -040094 WideString title = bookmark.GetTitle();
thestig733e0682016-11-23 05:52:39 -080095 return Utf16EncodeMaybeCopyAndReturnLength(title, buffer, buflen);
Bo Xu4d62b6b2015-01-10 22:52:59 -080096}
97
Dan Sinclair00d2ad12017-08-10 14:13:02 -040098FPDF_EXPORT FPDF_BOOKMARK FPDF_CALLCONV
99FPDFBookmark_Find(FPDF_DOCUMENT document, FPDF_WIDESTRING title) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700100 if (!title || title[0] == 0)
Tom Sepez471a1032015-10-15 16:17:18 -0700101 return nullptr;
102 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
103 if (!pDoc)
104 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700105 CPDF_BookmarkTree tree(pDoc);
Ryan Harrison875e98c2017-09-27 10:53:11 -0400106 size_t len = WideString::WStringLength(title);
Ryan Harrison275e2602017-09-18 14:23:18 -0400107 WideString encodedTitle = WideString::FromUTF16LE(title, len);
Wei Li0e2e5d72016-03-03 11:28:06 -0800108 std::set<CPDF_Dictionary*> visited;
109 return FindBookmark(tree, CPDF_Bookmark(), encodedTitle, &visited).GetDict();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700110}
111
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400112FPDF_EXPORT FPDF_DEST FPDF_CALLCONV FPDFBookmark_GetDest(FPDF_DOCUMENT document,
113 FPDF_BOOKMARK pDict) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700114 if (!pDict)
Tom Sepez471a1032015-10-15 16:17:18 -0700115 return nullptr;
116 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
117 if (!pDoc)
118 return nullptr;
Dan Sinclairf1251c12015-10-20 16:24:45 -0400119 CPDF_Bookmark bookmark(ToDictionary(static_cast<CPDF_Object*>(pDict)));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700120 CPDF_Dest dest = bookmark.GetDest(pDoc);
Wei Li0fc6b252016-03-01 16:29:41 -0800121 if (dest.GetObject())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700122 return dest.GetObject();
123 // If this bookmark is not directly associated with a dest, we try to get
124 // action
125 CPDF_Action action = bookmark.GetAction();
Wei Li0fc6b252016-03-01 16:29:41 -0800126 if (!action.GetDict())
Tom Sepez471a1032015-10-15 16:17:18 -0700127 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700128 return action.GetDest(pDoc).GetObject();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700129}
130
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400131FPDF_EXPORT FPDF_ACTION FPDF_CALLCONV
132FPDFBookmark_GetAction(FPDF_BOOKMARK pDict) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700133 if (!pDict)
thestig1cd352e2016-06-07 17:53:06 -0700134 return nullptr;
Dan Sinclairf1251c12015-10-20 16:24:45 -0400135 CPDF_Bookmark bookmark(ToDictionary(static_cast<CPDF_Object*>(pDict)));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700136 return bookmark.GetAction().GetDict();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700137}
138
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400139FPDF_EXPORT unsigned long FPDF_CALLCONV FPDFAction_GetType(FPDF_ACTION pDict) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700140 if (!pDict)
Lei Zhange0947b32015-09-17 14:51:48 -0700141 return PDFACTION_UNSUPPORTED;
142
Dan Sinclairf1251c12015-10-20 16:24:45 -0400143 CPDF_Action action(ToDictionary(static_cast<CPDF_Object*>(pDict)));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700144 CPDF_Action::ActionType type = action.GetType();
145 switch (type) {
146 case CPDF_Action::GoTo:
147 return PDFACTION_GOTO;
148 case CPDF_Action::GoToR:
149 return PDFACTION_REMOTEGOTO;
150 case CPDF_Action::URI:
151 return PDFACTION_URI;
152 case CPDF_Action::Launch:
153 return PDFACTION_LAUNCH;
154 default:
155 return PDFACTION_UNSUPPORTED;
156 }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700157}
158
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400159FPDF_EXPORT FPDF_DEST FPDF_CALLCONV FPDFAction_GetDest(FPDF_DOCUMENT document,
160 FPDF_ACTION pDict) {
Tom Sepez471a1032015-10-15 16:17:18 -0700161 if (!pDict)
Lei Zhange0947b32015-09-17 14:51:48 -0700162 return nullptr;
Tom Sepez471a1032015-10-15 16:17:18 -0700163 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
164 if (!pDoc)
165 return nullptr;
Dan Sinclairf1251c12015-10-20 16:24:45 -0400166 CPDF_Action action(ToDictionary(static_cast<CPDF_Object*>(pDict)));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700167 return action.GetDest(pDoc).GetObject();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700168}
169
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400170FPDF_EXPORT unsigned long FPDF_CALLCONV
171FPDFAction_GetFilePath(FPDF_ACTION pDict, void* buffer, unsigned long buflen) {
Lei Zhange0947b32015-09-17 14:51:48 -0700172 unsigned long type = FPDFAction_GetType(pDict);
173 if (type != PDFACTION_REMOTEGOTO && type != PDFACTION_LAUNCH)
174 return 0;
175
Dan Sinclairf1251c12015-10-20 16:24:45 -0400176 CPDF_Action action(ToDictionary(static_cast<CPDF_Object*>(pDict)));
Ryan Harrison275e2602017-09-18 14:23:18 -0400177 ByteString path = action.GetFilePath().UTF8Encode();
Lei Zhange0947b32015-09-17 14:51:48 -0700178 unsigned long len = path.GetLength() + 1;
thestig9067fd62016-11-23 14:10:06 -0800179 if (buffer && len <= buflen)
Dan Sinclair1c5d0b42017-04-03 15:05:11 -0400180 memcpy(buffer, path.c_str(), len);
Lei Zhange0947b32015-09-17 14:51:48 -0700181 return len;
182}
183
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400184FPDF_EXPORT unsigned long FPDF_CALLCONV
185FPDFAction_GetURIPath(FPDF_DOCUMENT document,
186 FPDF_ACTION pDict,
187 void* buffer,
188 unsigned long buflen) {
Tom Sepez471a1032015-10-15 16:17:18 -0700189 if (!pDict)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700190 return 0;
Tom Sepez471a1032015-10-15 16:17:18 -0700191 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
192 if (!pDoc)
193 return 0;
Dan Sinclairf1251c12015-10-20 16:24:45 -0400194 CPDF_Action action(ToDictionary(static_cast<CPDF_Object*>(pDict)));
Ryan Harrison275e2602017-09-18 14:23:18 -0400195 ByteString path = action.GetURI(pDoc);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700196 unsigned long len = path.GetLength() + 1;
thestig9067fd62016-11-23 14:10:06 -0800197 if (buffer && len <= buflen)
Dan Sinclair1c5d0b42017-04-03 15:05:11 -0400198 memcpy(buffer, path.c_str(), len);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700199 return len;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700200}
201
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400202FPDF_EXPORT unsigned long FPDF_CALLCONV
203FPDFDest_GetPageIndex(FPDF_DOCUMENT document, FPDF_DEST pDict) {
Tom Sepez471a1032015-10-15 16:17:18 -0700204 if (!pDict)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700205 return 0;
Tom Sepez471a1032015-10-15 16:17:18 -0700206 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
207 if (!pDoc)
208 return 0;
Dan Sinclair2b11dc12015-10-22 15:02:06 -0400209 CPDF_Dest dest(static_cast<CPDF_Array*>(pDict));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700210 return dest.GetPageIndex(pDoc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700211}
212
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400213FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
214FPDFDest_GetLocationInPage(FPDF_DEST pDict,
215 FPDF_BOOL* hasXVal,
216 FPDF_BOOL* hasYVal,
217 FPDF_BOOL* hasZoomVal,
218 FS_FLOAT* x,
219 FS_FLOAT* y,
220 FS_FLOAT* zoom) {
dsinclairc59fa882016-11-08 06:55:40 -0800221 if (!pDict)
222 return false;
223
Tom Sepezfe91c6c2017-05-16 15:33:20 -0700224 auto dest = pdfium::MakeUnique<CPDF_Dest>(static_cast<CPDF_Object*>(pDict));
dsinclairc59fa882016-11-08 06:55:40 -0800225
226 // FPDF_BOOL is an int, GetXYZ expects bools.
227 bool bHasX;
228 bool bHasY;
229 bool bHasZoom;
230 if (!dest->GetXYZ(&bHasX, &bHasY, &bHasZoom, x, y, zoom))
231 return false;
232
233 *hasXVal = bHasX;
234 *hasYVal = bHasY;
235 *hasZoomVal = bHasZoom;
236 return true;
237}
238
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400239FPDF_EXPORT FPDF_LINK FPDF_CALLCONV FPDFLink_GetLinkAtPoint(FPDF_PAGE page,
240 double x,
241 double y) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700242 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Lei Zhangbdf72c32015-08-14 19:24:08 -0700243 if (!pPage)
244 return nullptr;
245
246 CPDF_LinkList* pLinkList = GetLinkList(pPage);
247 if (!pLinkList)
248 return nullptr;
249
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500250 return pLinkList
Dan Sinclair05df0752017-03-14 14:43:42 -0400251 ->GetLinkAtPoint(pPage,
252 CFX_PointF(static_cast<float>(x), static_cast<float>(y)),
253 nullptr)
Lei Zhangbdf72c32015-08-14 19:24:08 -0700254 .GetDict();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700255}
256
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400257FPDF_EXPORT int FPDF_CALLCONV FPDFLink_GetLinkZOrderAtPoint(FPDF_PAGE page,
258 double x,
259 double y) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700260 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700261 if (!pPage)
Lei Zhangbdf72c32015-08-14 19:24:08 -0700262 return -1;
263
264 CPDF_LinkList* pLinkList = GetLinkList(pPage);
265 if (!pLinkList)
266 return -1;
267
268 int z_order = -1;
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500269 pLinkList->GetLinkAtPoint(
Dan Sinclair05df0752017-03-14 14:43:42 -0400270 pPage, CFX_PointF(static_cast<float>(x), static_cast<float>(y)),
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500271 &z_order);
Lei Zhangbdf72c32015-08-14 19:24:08 -0700272 return z_order;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700273}
274
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400275FPDF_EXPORT FPDF_DEST FPDF_CALLCONV FPDFLink_GetDest(FPDF_DOCUMENT document,
276 FPDF_LINK pDict) {
Tom Sepez471a1032015-10-15 16:17:18 -0700277 if (!pDict)
Lei Zhange0947b32015-09-17 14:51:48 -0700278 return nullptr;
Tom Sepez471a1032015-10-15 16:17:18 -0700279 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
280 if (!pDoc)
281 return nullptr;
Dan Sinclairf1251c12015-10-20 16:24:45 -0400282 CPDF_Link link(ToDictionary(static_cast<CPDF_Object*>(pDict)));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700283 FPDF_DEST dest = link.GetDest(pDoc).GetObject();
284 if (dest)
285 return dest;
286 // If this link is not directly associated with a dest, we try to get action
287 CPDF_Action action = link.GetAction();
Wei Li0fc6b252016-03-01 16:29:41 -0800288 if (!action.GetDict())
Lei Zhange0947b32015-09-17 14:51:48 -0700289 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700290 return action.GetDest(pDoc).GetObject();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700291}
292
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400293FPDF_EXPORT FPDF_ACTION FPDF_CALLCONV FPDFLink_GetAction(FPDF_LINK pDict) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700294 if (!pDict)
Lei Zhange0947b32015-09-17 14:51:48 -0700295 return nullptr;
296
Dan Sinclairf1251c12015-10-20 16:24:45 -0400297 CPDF_Link link(ToDictionary(static_cast<CPDF_Object*>(pDict)));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700298 return link.GetAction().GetDict();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700299}
300
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400301FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFLink_Enumerate(FPDF_PAGE page,
302 int* startPos,
303 FPDF_LINK* linkAnnot) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700304 if (!startPos || !linkAnnot)
tsepez4cf55152016-11-02 14:37:54 -0700305 return false;
Tom Sepezdb0be962015-10-16 14:00:21 -0700306 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
307 if (!pPage || !pPage->m_pFormDict)
tsepez4cf55152016-11-02 14:37:54 -0700308 return false;
dsinclair38fd8442016-09-15 10:15:32 -0700309 CPDF_Array* pAnnots = pPage->m_pFormDict->GetArrayFor("Annots");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700310 if (!pAnnots)
tsepez4cf55152016-11-02 14:37:54 -0700311 return false;
Wei Lie1aebd42016-04-11 10:02:09 -0700312 for (size_t i = *startPos; i < pAnnots->GetCount(); i++) {
Dan Sinclairf1251c12015-10-20 16:24:45 -0400313 CPDF_Dictionary* pDict =
tsepezbd567552016-03-29 14:51:50 -0700314 ToDictionary(static_cast<CPDF_Object*>(pAnnots->GetDirectObjectAt(i)));
Dan Sinclairf1251c12015-10-20 16:24:45 -0400315 if (!pDict)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700316 continue;
dsinclair38fd8442016-09-15 10:15:32 -0700317 if (pDict->GetStringFor("Subtype") == "Link") {
Wei Lie1aebd42016-04-11 10:02:09 -0700318 *startPos = static_cast<int>(i + 1);
319 *linkAnnot = static_cast<FPDF_LINK>(pDict);
tsepez4cf55152016-11-02 14:37:54 -0700320 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700321 }
322 }
tsepez4cf55152016-11-02 14:37:54 -0700323 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700324}
325
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400326FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFLink_GetAnnotRect(FPDF_LINK linkAnnot,
327 FS_RECTF* rect) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700328 if (!linkAnnot || !rect)
tsepez4cf55152016-11-02 14:37:54 -0700329 return false;
Dan Sinclairf1251c12015-10-20 16:24:45 -0400330 CPDF_Dictionary* pAnnotDict =
331 ToDictionary(static_cast<CPDF_Object*>(linkAnnot));
dsinclair38fd8442016-09-15 10:15:32 -0700332 CFX_FloatRect rt = pAnnotDict->GetRectFor("Rect");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700333 rect->left = rt.left;
334 rect->bottom = rt.bottom;
335 rect->right = rt.right;
336 rect->top = rt.top;
tsepez4cf55152016-11-02 14:37:54 -0700337 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700338}
339
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400340FPDF_EXPORT int FPDF_CALLCONV FPDFLink_CountQuadPoints(FPDF_LINK linkAnnot) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700341 if (!linkAnnot)
342 return 0;
Dan Sinclairf1251c12015-10-20 16:24:45 -0400343 CPDF_Dictionary* pAnnotDict =
344 ToDictionary(static_cast<CPDF_Object*>(linkAnnot));
dsinclair38fd8442016-09-15 10:15:32 -0700345 CPDF_Array* pArray = pAnnotDict->GetArrayFor("QuadPoints");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700346 if (!pArray)
347 return 0;
Wei Lie1aebd42016-04-11 10:02:09 -0700348 return static_cast<int>(pArray->GetCount() / 8);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700349}
350
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400351FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
352FPDFLink_GetQuadPoints(FPDF_LINK linkAnnot,
353 int quadIndex,
354 FS_QUADPOINTSF* quadPoints) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700355 if (!linkAnnot || !quadPoints)
tsepez4cf55152016-11-02 14:37:54 -0700356 return false;
Dan Sinclairf1251c12015-10-20 16:24:45 -0400357 CPDF_Dictionary* pAnnotDict =
358 ToDictionary(static_cast<CPDF_Object*>(linkAnnot));
dsinclair38fd8442016-09-15 10:15:32 -0700359 CPDF_Array* pArray = pAnnotDict->GetArrayFor("QuadPoints");
thestig733e0682016-11-23 05:52:39 -0800360 if (!pArray)
361 return false;
362
363 if (quadIndex < 0 ||
364 static_cast<size_t>(quadIndex) >= pArray->GetCount() / 8 ||
365 (static_cast<size_t>(quadIndex * 8 + 7) >= pArray->GetCount())) {
366 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700367 }
thestig733e0682016-11-23 05:52:39 -0800368
369 quadPoints->x1 = pArray->GetNumberAt(quadIndex * 8);
370 quadPoints->y1 = pArray->GetNumberAt(quadIndex * 8 + 1);
371 quadPoints->x2 = pArray->GetNumberAt(quadIndex * 8 + 2);
372 quadPoints->y2 = pArray->GetNumberAt(quadIndex * 8 + 3);
373 quadPoints->x3 = pArray->GetNumberAt(quadIndex * 8 + 4);
374 quadPoints->y3 = pArray->GetNumberAt(quadIndex * 8 + 5);
375 quadPoints->x4 = pArray->GetNumberAt(quadIndex * 8 + 6);
376 quadPoints->y4 = pArray->GetNumberAt(quadIndex * 8 + 7);
377 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700378}
379
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400380FPDF_EXPORT unsigned long FPDF_CALLCONV FPDF_GetMetaText(FPDF_DOCUMENT document,
381 FPDF_BYTESTRING tag,
382 void* buffer,
383 unsigned long buflen) {
Tom Sepez471a1032015-10-15 16:17:18 -0700384 if (!tag)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700385 return 0;
thestig733e0682016-11-23 05:52:39 -0800386 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Tom Sepez471a1032015-10-15 16:17:18 -0700387 if (!pDoc)
388 return 0;
Henrique Nakashimab73ce7b2017-06-19 16:04:34 -0400389 pDoc->LoadDocumentInfo();
Lei Zhang01581062017-08-30 14:19:26 -0700390 const CPDF_Dictionary* pInfo = pDoc->GetInfo();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700391 if (!pInfo)
392 return 0;
Ryan Harrison275e2602017-09-18 14:23:18 -0400393 WideString text = pInfo->GetUnicodeTextFor(tag);
thestig733e0682016-11-23 05:52:39 -0800394 return Utf16EncodeMaybeCopyAndReturnLength(text, buffer, buflen);
395}
396
Dan Sinclair00d2ad12017-08-10 14:13:02 -0400397FPDF_EXPORT unsigned long FPDF_CALLCONV
398FPDF_GetPageLabel(FPDF_DOCUMENT document,
399 int page_index,
400 void* buffer,
401 unsigned long buflen) {
thestig733e0682016-11-23 05:52:39 -0800402 if (page_index < 0)
403 return 0;
404
405 // CPDF_PageLabel can deal with NULL |document|.
406 CPDF_PageLabel label(CPDFDocumentFromFPDFDocument(document));
Ryan Harrison275e2602017-09-18 14:23:18 -0400407 WideString str;
thestig733e0682016-11-23 05:52:39 -0800408 if (!label.GetLabel(page_index, &str))
409 return 0;
410 return Utf16EncodeMaybeCopyAndReturnLength(str, buffer, buflen);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700411}