John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 1 | // 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 Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 4 | |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 5 | // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | |
Lei Zhang | b4e7f30 | 2015-11-06 15:52:32 -0800 | [diff] [blame] | 7 | #include "public/fpdf_doc.h" |
| 8 | |
Dan Sinclair | 85c8e7f | 2016-11-21 13:50:32 -0500 | [diff] [blame] | 9 | #include <memory> |
Wei Li | 0e2e5d7 | 2016-03-03 11:28:06 -0800 | [diff] [blame] | 10 | #include <set> |
| 11 | |
dsinclair | 41872fa | 2016-10-04 11:29:35 -0700 | [diff] [blame] | 12 | #include "core/fpdfapi/page/cpdf_page.h" |
dsinclair | 488b7ad | 2016-10-04 11:55:50 -0700 | [diff] [blame] | 13 | #include "core/fpdfapi/parser/cpdf_array.h" |
| 14 | #include "core/fpdfapi/parser/cpdf_document.h" |
Ralf Sippl | 1638179 | 2018-04-12 21:20:26 +0000 | [diff] [blame] | 15 | #include "core/fpdfapi/parser/cpdf_number.h" |
dsinclair | 1727aee | 2016-09-29 13:12:56 -0700 | [diff] [blame] | 16 | #include "core/fpdfdoc/cpdf_bookmark.h" |
| 17 | #include "core/fpdfdoc/cpdf_bookmarktree.h" |
dsinclair | c59fa88 | 2016-11-08 06:55:40 -0800 | [diff] [blame] | 18 | #include "core/fpdfdoc/cpdf_dest.h" |
thestig | 733e068 | 2016-11-23 05:52:39 -0800 | [diff] [blame] | 19 | #include "core/fpdfdoc/cpdf_pagelabel.h" |
Dan Sinclair | 00d47a6 | 2018-03-28 18:39:04 +0000 | [diff] [blame] | 20 | #include "fpdfsdk/cpdfsdk_helpers.h" |
tsepez | a9caab9 | 2016-12-14 05:57:10 -0800 | [diff] [blame] | 21 | #include "third_party/base/ptr_util.h" |
Wei Li | 0e2e5d7 | 2016-03-03 11:28:06 -0800 | [diff] [blame] | 22 | #include "third_party/base/stl_util.h" |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 23 | |
Lei Zhang | bdf72c3 | 2015-08-14 19:24:08 -0700 | [diff] [blame] | 24 | namespace { |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 25 | |
Lei Zhang | bdf72c3 | 2015-08-14 19:24:08 -0700 | [diff] [blame] | 26 | CPDF_Bookmark FindBookmark(const CPDF_BookmarkTree& tree, |
| 27 | CPDF_Bookmark bookmark, |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 28 | const WideString& title, |
Wei Li | 0e2e5d7 | 2016-03-03 11:28:06 -0800 | [diff] [blame] | 29 | std::set<CPDF_Dictionary*>* visited) { |
| 30 | // Return if already checked to avoid circular calling. |
| 31 | if (pdfium::ContainsKey(*visited, bookmark.GetDict())) |
| 32 | return CPDF_Bookmark(); |
| 33 | visited->insert(bookmark.GetDict()); |
| 34 | |
Wei Li | 0fc6b25 | 2016-03-01 16:29:41 -0800 | [diff] [blame] | 35 | if (bookmark.GetDict() && |
| 36 | bookmark.GetTitle().CompareNoCase(title.c_str()) == 0) { |
Wei Li | 0e2e5d7 | 2016-03-03 11:28:06 -0800 | [diff] [blame] | 37 | // First check this item. |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 38 | return bookmark; |
| 39 | } |
Wei Li | 0e2e5d7 | 2016-03-03 11:28:06 -0800 | [diff] [blame] | 40 | |
| 41 | // Go into children items. |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 42 | CPDF_Bookmark child = tree.GetFirstChild(bookmark); |
Wei Li | 0e2e5d7 | 2016-03-03 11:28:06 -0800 | [diff] [blame] | 43 | while (child.GetDict() && !pdfium::ContainsKey(*visited, child.GetDict())) { |
| 44 | // Check this item and its children. |
| 45 | CPDF_Bookmark found = FindBookmark(tree, child, title, visited); |
Wei Li | 0fc6b25 | 2016-03-01 16:29:41 -0800 | [diff] [blame] | 46 | if (found.GetDict()) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 47 | return found; |
| 48 | child = tree.GetNextSibling(child); |
| 49 | } |
| 50 | return CPDF_Bookmark(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Lei Zhang | bdf72c3 | 2015-08-14 19:24:08 -0700 | [diff] [blame] | 53 | CPDF_LinkList* GetLinkList(CPDF_Page* page) { |
| 54 | if (!page) |
| 55 | return nullptr; |
| 56 | |
Tom Sepez | 4cb82ee | 2017-05-22 15:15:30 -0700 | [diff] [blame] | 57 | CPDF_Document* pDoc = page->m_pDocument.Get(); |
tsepez | 5ce0968 | 2016-05-25 16:16:32 -0700 | [diff] [blame] | 58 | std::unique_ptr<CPDF_LinkList>* pHolder = pDoc->LinksContext(); |
| 59 | if (!pHolder->get()) |
tsepez | a9caab9 | 2016-12-14 05:57:10 -0800 | [diff] [blame] | 60 | *pHolder = pdfium::MakeUnique<CPDF_LinkList>(); |
tsepez | 5ce0968 | 2016-05-25 16:16:32 -0700 | [diff] [blame] | 61 | return pHolder->get(); |
Lei Zhang | bdf72c3 | 2015-08-14 19:24:08 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Dan Sinclair | 7aba472 | 2018-03-28 17:04:16 +0000 | [diff] [blame] | 64 | } // namespace |
Lei Zhang | aaed698 | 2018-03-22 18:39:05 +0000 | [diff] [blame] | 65 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 66 | FPDF_EXPORT FPDF_BOOKMARK FPDF_CALLCONV |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 67 | FPDFBookmark_GetFirstChild(FPDF_DOCUMENT document, FPDF_BOOKMARK pDict) { |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 68 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
| 69 | if (!pDoc) |
| 70 | return nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 71 | CPDF_BookmarkTree tree(pDoc); |
Lei Zhang | 11767d3 | 2018-03-21 18:59:44 +0000 | [diff] [blame] | 72 | CPDF_Bookmark bookmark(CPDFDictionaryFromFPDFBookmark(pDict)); |
Tom Sepez | 525147a | 2018-05-03 17:19:53 +0000 | [diff] [blame^] | 73 | return FPDFBookmarkFromCPDFDictionary(tree.GetFirstChild(bookmark).GetDict()); |
Bo Xu | 4d62b6b | 2015-01-10 22:52:59 -0800 | [diff] [blame] | 74 | } |
| 75 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 76 | FPDF_EXPORT FPDF_BOOKMARK FPDF_CALLCONV |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 77 | FPDFBookmark_GetNextSibling(FPDF_DOCUMENT document, FPDF_BOOKMARK pDict) { |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 78 | if (!pDict) |
| 79 | return nullptr; |
Tom Sepez | 525147a | 2018-05-03 17:19:53 +0000 | [diff] [blame^] | 80 | |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 81 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
| 82 | if (!pDoc) |
| 83 | return nullptr; |
Tom Sepez | 525147a | 2018-05-03 17:19:53 +0000 | [diff] [blame^] | 84 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 85 | CPDF_BookmarkTree tree(pDoc); |
Lei Zhang | 11767d3 | 2018-03-21 18:59:44 +0000 | [diff] [blame] | 86 | CPDF_Bookmark bookmark(CPDFDictionaryFromFPDFBookmark(pDict)); |
Tom Sepez | 525147a | 2018-05-03 17:19:53 +0000 | [diff] [blame^] | 87 | return FPDFBookmarkFromCPDFDictionary( |
| 88 | tree.GetNextSibling(bookmark).GetDict()); |
Bo Xu | 4d62b6b | 2015-01-10 22:52:59 -0800 | [diff] [blame] | 89 | } |
| 90 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 91 | FPDF_EXPORT unsigned long FPDF_CALLCONV |
| 92 | FPDFBookmark_GetTitle(FPDF_BOOKMARK pDict, void* buffer, unsigned long buflen) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 93 | if (!pDict) |
| 94 | return 0; |
Lei Zhang | 11767d3 | 2018-03-21 18:59:44 +0000 | [diff] [blame] | 95 | CPDF_Bookmark bookmark(CPDFDictionaryFromFPDFBookmark(pDict)); |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 96 | WideString title = bookmark.GetTitle(); |
thestig | 733e068 | 2016-11-23 05:52:39 -0800 | [diff] [blame] | 97 | return Utf16EncodeMaybeCopyAndReturnLength(title, buffer, buflen); |
Bo Xu | 4d62b6b | 2015-01-10 22:52:59 -0800 | [diff] [blame] | 98 | } |
| 99 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 100 | FPDF_EXPORT FPDF_BOOKMARK FPDF_CALLCONV |
| 101 | FPDFBookmark_Find(FPDF_DOCUMENT document, FPDF_WIDESTRING title) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 102 | if (!title || title[0] == 0) |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 103 | return nullptr; |
| 104 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
| 105 | if (!pDoc) |
| 106 | return nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 107 | CPDF_BookmarkTree tree(pDoc); |
Ryan Harrison | 875e98c | 2017-09-27 10:53:11 -0400 | [diff] [blame] | 108 | size_t len = WideString::WStringLength(title); |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 109 | WideString encodedTitle = WideString::FromUTF16LE(title, len); |
Wei Li | 0e2e5d7 | 2016-03-03 11:28:06 -0800 | [diff] [blame] | 110 | std::set<CPDF_Dictionary*> visited; |
Tom Sepez | 525147a | 2018-05-03 17:19:53 +0000 | [diff] [blame^] | 111 | return FPDFBookmarkFromCPDFDictionary( |
| 112 | FindBookmark(tree, CPDF_Bookmark(), encodedTitle, &visited).GetDict()); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 115 | FPDF_EXPORT FPDF_DEST FPDF_CALLCONV FPDFBookmark_GetDest(FPDF_DOCUMENT document, |
| 116 | FPDF_BOOKMARK pDict) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 117 | if (!pDict) |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 118 | return nullptr; |
| 119 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
| 120 | if (!pDoc) |
| 121 | return nullptr; |
Lei Zhang | 11767d3 | 2018-03-21 18:59:44 +0000 | [diff] [blame] | 122 | CPDF_Bookmark bookmark(CPDFDictionaryFromFPDFBookmark(pDict)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 123 | CPDF_Dest dest = bookmark.GetDest(pDoc); |
Wei Li | 0fc6b25 | 2016-03-01 16:29:41 -0800 | [diff] [blame] | 124 | if (dest.GetObject()) |
Tom Sepez | 525147a | 2018-05-03 17:19:53 +0000 | [diff] [blame^] | 125 | return FPDFDestFromCPDFArray(dest.GetObject()); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 126 | // If this bookmark is not directly associated with a dest, we try to get |
| 127 | // action |
| 128 | CPDF_Action action = bookmark.GetAction(); |
Wei Li | 0fc6b25 | 2016-03-01 16:29:41 -0800 | [diff] [blame] | 129 | if (!action.GetDict()) |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 130 | return nullptr; |
Tom Sepez | 525147a | 2018-05-03 17:19:53 +0000 | [diff] [blame^] | 131 | return FPDFDestFromCPDFArray(action.GetDest(pDoc).GetObject()); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 134 | FPDF_EXPORT FPDF_ACTION FPDF_CALLCONV |
| 135 | FPDFBookmark_GetAction(FPDF_BOOKMARK pDict) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 136 | if (!pDict) |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 137 | return nullptr; |
Lei Zhang | 11767d3 | 2018-03-21 18:59:44 +0000 | [diff] [blame] | 138 | CPDF_Bookmark bookmark(CPDFDictionaryFromFPDFBookmark(pDict)); |
Tom Sepez | 525147a | 2018-05-03 17:19:53 +0000 | [diff] [blame^] | 139 | return FPDFActionFromCPDFDictionary(bookmark.GetAction().GetDict()); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 142 | FPDF_EXPORT unsigned long FPDF_CALLCONV FPDFAction_GetType(FPDF_ACTION pDict) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 143 | if (!pDict) |
Lei Zhang | e0947b3 | 2015-09-17 14:51:48 -0700 | [diff] [blame] | 144 | return PDFACTION_UNSUPPORTED; |
| 145 | |
Lei Zhang | 11767d3 | 2018-03-21 18:59:44 +0000 | [diff] [blame] | 146 | CPDF_Action action(CPDFDictionaryFromFPDFAction(pDict)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 147 | CPDF_Action::ActionType type = action.GetType(); |
| 148 | switch (type) { |
| 149 | case CPDF_Action::GoTo: |
| 150 | return PDFACTION_GOTO; |
| 151 | case CPDF_Action::GoToR: |
| 152 | return PDFACTION_REMOTEGOTO; |
| 153 | case CPDF_Action::URI: |
| 154 | return PDFACTION_URI; |
| 155 | case CPDF_Action::Launch: |
| 156 | return PDFACTION_LAUNCH; |
| 157 | default: |
| 158 | return PDFACTION_UNSUPPORTED; |
| 159 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 162 | FPDF_EXPORT FPDF_DEST FPDF_CALLCONV FPDFAction_GetDest(FPDF_DOCUMENT document, |
| 163 | FPDF_ACTION pDict) { |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 164 | if (!pDict) |
Lei Zhang | e0947b3 | 2015-09-17 14:51:48 -0700 | [diff] [blame] | 165 | return nullptr; |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 166 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
| 167 | if (!pDoc) |
| 168 | return nullptr; |
Lei Zhang | 11767d3 | 2018-03-21 18:59:44 +0000 | [diff] [blame] | 169 | CPDF_Action action(CPDFDictionaryFromFPDFAction(pDict)); |
Tom Sepez | 525147a | 2018-05-03 17:19:53 +0000 | [diff] [blame^] | 170 | return FPDFDestFromCPDFArray(action.GetDest(pDoc).GetObject()); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 171 | } |
| 172 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 173 | FPDF_EXPORT unsigned long FPDF_CALLCONV |
| 174 | FPDFAction_GetFilePath(FPDF_ACTION pDict, void* buffer, unsigned long buflen) { |
Lei Zhang | e0947b3 | 2015-09-17 14:51:48 -0700 | [diff] [blame] | 175 | unsigned long type = FPDFAction_GetType(pDict); |
| 176 | if (type != PDFACTION_REMOTEGOTO && type != PDFACTION_LAUNCH) |
| 177 | return 0; |
| 178 | |
Lei Zhang | 11767d3 | 2018-03-21 18:59:44 +0000 | [diff] [blame] | 179 | CPDF_Action action(CPDFDictionaryFromFPDFAction(pDict)); |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 180 | ByteString path = action.GetFilePath().UTF8Encode(); |
Lei Zhang | e0947b3 | 2015-09-17 14:51:48 -0700 | [diff] [blame] | 181 | unsigned long len = path.GetLength() + 1; |
thestig | 9067fd6 | 2016-11-23 14:10:06 -0800 | [diff] [blame] | 182 | if (buffer && len <= buflen) |
Dan Sinclair | 1c5d0b4 | 2017-04-03 15:05:11 -0400 | [diff] [blame] | 183 | memcpy(buffer, path.c_str(), len); |
Lei Zhang | e0947b3 | 2015-09-17 14:51:48 -0700 | [diff] [blame] | 184 | return len; |
| 185 | } |
| 186 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 187 | FPDF_EXPORT unsigned long FPDF_CALLCONV |
| 188 | FPDFAction_GetURIPath(FPDF_DOCUMENT document, |
| 189 | FPDF_ACTION pDict, |
| 190 | void* buffer, |
| 191 | unsigned long buflen) { |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 192 | if (!pDict) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 193 | return 0; |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 194 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
| 195 | if (!pDoc) |
| 196 | return 0; |
Lei Zhang | 11767d3 | 2018-03-21 18:59:44 +0000 | [diff] [blame] | 197 | CPDF_Action action(CPDFDictionaryFromFPDFAction(pDict)); |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 198 | ByteString path = action.GetURI(pDoc); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 199 | unsigned long len = path.GetLength() + 1; |
thestig | 9067fd6 | 2016-11-23 14:10:06 -0800 | [diff] [blame] | 200 | if (buffer && len <= buflen) |
Dan Sinclair | 1c5d0b4 | 2017-04-03 15:05:11 -0400 | [diff] [blame] | 201 | memcpy(buffer, path.c_str(), len); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 202 | return len; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 205 | FPDF_EXPORT unsigned long FPDF_CALLCONV |
Lei Zhang | aca3efc | 2018-03-16 20:27:04 +0000 | [diff] [blame] | 206 | FPDFDest_GetPageIndex(FPDF_DOCUMENT document, FPDF_DEST dest) { |
| 207 | if (!dest) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 208 | return 0; |
Henrique Nakashima | 71a7d37 | 2018-02-01 17:07:13 +0000 | [diff] [blame] | 209 | |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 210 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
| 211 | if (!pDoc) |
| 212 | return 0; |
Henrique Nakashima | 71a7d37 | 2018-02-01 17:07:13 +0000 | [diff] [blame] | 213 | |
Tom Sepez | 525147a | 2018-05-03 17:19:53 +0000 | [diff] [blame^] | 214 | CPDF_Dest destination(CPDFArrayFromFPDFDest(dest)); |
Lei Zhang | aca3efc | 2018-03-16 20:27:04 +0000 | [diff] [blame] | 215 | return destination.GetPageIndexDeprecated(pDoc); |
Henrique Nakashima | 71a7d37 | 2018-02-01 17:07:13 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Henrique Nakashima | e899dd7 | 2018-02-01 19:17:04 +0000 | [diff] [blame] | 218 | FPDF_EXPORT int FPDF_CALLCONV FPDFDest_GetDestPageIndex(FPDF_DOCUMENT document, |
Lei Zhang | aca3efc | 2018-03-16 20:27:04 +0000 | [diff] [blame] | 219 | FPDF_DEST dest) { |
| 220 | if (!dest) |
Henrique Nakashima | 71a7d37 | 2018-02-01 17:07:13 +0000 | [diff] [blame] | 221 | return -1; |
| 222 | |
| 223 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
| 224 | if (!pDoc) |
| 225 | return -1; |
| 226 | |
Tom Sepez | 525147a | 2018-05-03 17:19:53 +0000 | [diff] [blame^] | 227 | CPDF_Dest destination(CPDFArrayFromFPDFDest(dest)); |
Lei Zhang | aca3efc | 2018-03-16 20:27:04 +0000 | [diff] [blame] | 228 | return destination.GetDestPageIndex(pDoc); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Henrique Nakashima | de3f3fc | 2018-01-04 11:54:18 -0500 | [diff] [blame] | 231 | FPDF_EXPORT unsigned long FPDF_CALLCONV |
Lei Zhang | 326c38c | 2018-01-10 20:10:05 +0000 | [diff] [blame] | 232 | FPDFDest_GetView(FPDF_DEST pDict, |
| 233 | unsigned long* pNumParams, |
| 234 | FS_FLOAT* pParams) { |
Henrique Nakashima | de3f3fc | 2018-01-04 11:54:18 -0500 | [diff] [blame] | 235 | if (!pDict) { |
Lei Zhang | 326c38c | 2018-01-10 20:10:05 +0000 | [diff] [blame] | 236 | *pNumParams = 0; |
Henrique Nakashima | de3f3fc | 2018-01-04 11:54:18 -0500 | [diff] [blame] | 237 | return 0; |
| 238 | } |
| 239 | |
Tom Sepez | 525147a | 2018-05-03 17:19:53 +0000 | [diff] [blame^] | 240 | CPDF_Dest destination(CPDFArrayFromFPDFDest(pDict)); |
Lei Zhang | 11767d3 | 2018-03-21 18:59:44 +0000 | [diff] [blame] | 241 | unsigned long nParams = destination.GetNumParams(); |
Lei Zhang | 326c38c | 2018-01-10 20:10:05 +0000 | [diff] [blame] | 242 | ASSERT(nParams <= 4); |
| 243 | *pNumParams = nParams; |
| 244 | for (unsigned long i = 0; i < nParams; ++i) |
Lei Zhang | 11767d3 | 2018-03-21 18:59:44 +0000 | [diff] [blame] | 245 | pParams[i] = destination.GetParam(i); |
| 246 | return destination.GetZoomMode(); |
Henrique Nakashima | de3f3fc | 2018-01-04 11:54:18 -0500 | [diff] [blame] | 247 | } |
| 248 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 249 | FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV |
| 250 | FPDFDest_GetLocationInPage(FPDF_DEST pDict, |
| 251 | FPDF_BOOL* hasXVal, |
| 252 | FPDF_BOOL* hasYVal, |
| 253 | FPDF_BOOL* hasZoomVal, |
| 254 | FS_FLOAT* x, |
| 255 | FS_FLOAT* y, |
| 256 | FS_FLOAT* zoom) { |
dsinclair | c59fa88 | 2016-11-08 06:55:40 -0800 | [diff] [blame] | 257 | if (!pDict) |
| 258 | return false; |
| 259 | |
Tom Sepez | 525147a | 2018-05-03 17:19:53 +0000 | [diff] [blame^] | 260 | auto dest = pdfium::MakeUnique<CPDF_Dest>(CPDFArrayFromFPDFDest(pDict)); |
dsinclair | c59fa88 | 2016-11-08 06:55:40 -0800 | [diff] [blame] | 261 | |
| 262 | // FPDF_BOOL is an int, GetXYZ expects bools. |
| 263 | bool bHasX; |
| 264 | bool bHasY; |
| 265 | bool bHasZoom; |
| 266 | if (!dest->GetXYZ(&bHasX, &bHasY, &bHasZoom, x, y, zoom)) |
| 267 | return false; |
| 268 | |
| 269 | *hasXVal = bHasX; |
| 270 | *hasYVal = bHasY; |
| 271 | *hasZoomVal = bHasZoom; |
| 272 | return true; |
| 273 | } |
| 274 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 275 | FPDF_EXPORT FPDF_LINK FPDF_CALLCONV FPDFLink_GetLinkAtPoint(FPDF_PAGE page, |
| 276 | double x, |
| 277 | double y) { |
Tom Sepez | db0be96 | 2015-10-16 14:00:21 -0700 | [diff] [blame] | 278 | CPDF_Page* pPage = CPDFPageFromFPDFPage(page); |
Lei Zhang | bdf72c3 | 2015-08-14 19:24:08 -0700 | [diff] [blame] | 279 | if (!pPage) |
| 280 | return nullptr; |
| 281 | |
| 282 | CPDF_LinkList* pLinkList = GetLinkList(pPage); |
| 283 | if (!pLinkList) |
| 284 | return nullptr; |
| 285 | |
Tom Sepez | 525147a | 2018-05-03 17:19:53 +0000 | [diff] [blame^] | 286 | CPDF_Link link = pLinkList->GetLinkAtPoint( |
| 287 | pPage, CFX_PointF(static_cast<float>(x), static_cast<float>(y)), nullptr); |
| 288 | |
| 289 | return FPDFLinkFromCPDFDictionary(link.GetDict()); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 290 | } |
| 291 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 292 | FPDF_EXPORT int FPDF_CALLCONV FPDFLink_GetLinkZOrderAtPoint(FPDF_PAGE page, |
| 293 | double x, |
| 294 | double y) { |
Tom Sepez | db0be96 | 2015-10-16 14:00:21 -0700 | [diff] [blame] | 295 | CPDF_Page* pPage = CPDFPageFromFPDFPage(page); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 296 | if (!pPage) |
Lei Zhang | bdf72c3 | 2015-08-14 19:24:08 -0700 | [diff] [blame] | 297 | return -1; |
| 298 | |
| 299 | CPDF_LinkList* pLinkList = GetLinkList(pPage); |
| 300 | if (!pLinkList) |
| 301 | return -1; |
| 302 | |
| 303 | int z_order = -1; |
Dan Sinclair | b45ea1f | 2017-02-21 14:27:59 -0500 | [diff] [blame] | 304 | pLinkList->GetLinkAtPoint( |
Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 305 | pPage, CFX_PointF(static_cast<float>(x), static_cast<float>(y)), |
Dan Sinclair | b45ea1f | 2017-02-21 14:27:59 -0500 | [diff] [blame] | 306 | &z_order); |
Lei Zhang | bdf72c3 | 2015-08-14 19:24:08 -0700 | [diff] [blame] | 307 | return z_order; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 308 | } |
| 309 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 310 | FPDF_EXPORT FPDF_DEST FPDF_CALLCONV FPDFLink_GetDest(FPDF_DOCUMENT document, |
| 311 | FPDF_LINK pDict) { |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 312 | if (!pDict) |
Lei Zhang | e0947b3 | 2015-09-17 14:51:48 -0700 | [diff] [blame] | 313 | return nullptr; |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 314 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
| 315 | if (!pDoc) |
| 316 | return nullptr; |
Lei Zhang | 11767d3 | 2018-03-21 18:59:44 +0000 | [diff] [blame] | 317 | CPDF_Link link(CPDFDictionaryFromFPDFLink(pDict)); |
Tom Sepez | 525147a | 2018-05-03 17:19:53 +0000 | [diff] [blame^] | 318 | FPDF_DEST dest = FPDFDestFromCPDFArray(link.GetDest(pDoc).GetObject()); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 319 | if (dest) |
| 320 | return dest; |
| 321 | // If this link is not directly associated with a dest, we try to get action |
| 322 | CPDF_Action action = link.GetAction(); |
Wei Li | 0fc6b25 | 2016-03-01 16:29:41 -0800 | [diff] [blame] | 323 | if (!action.GetDict()) |
Lei Zhang | e0947b3 | 2015-09-17 14:51:48 -0700 | [diff] [blame] | 324 | return nullptr; |
Tom Sepez | 525147a | 2018-05-03 17:19:53 +0000 | [diff] [blame^] | 325 | return FPDFDestFromCPDFArray(action.GetDest(pDoc).GetObject()); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 326 | } |
| 327 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 328 | FPDF_EXPORT FPDF_ACTION FPDF_CALLCONV FPDFLink_GetAction(FPDF_LINK pDict) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 329 | if (!pDict) |
Lei Zhang | e0947b3 | 2015-09-17 14:51:48 -0700 | [diff] [blame] | 330 | return nullptr; |
| 331 | |
Lei Zhang | 11767d3 | 2018-03-21 18:59:44 +0000 | [diff] [blame] | 332 | CPDF_Link link(CPDFDictionaryFromFPDFLink(pDict)); |
Tom Sepez | 525147a | 2018-05-03 17:19:53 +0000 | [diff] [blame^] | 333 | return FPDFActionFromCPDFDictionary(link.GetAction().GetDict()); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 334 | } |
| 335 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 336 | FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFLink_Enumerate(FPDF_PAGE page, |
Lei Zhang | c3cffbe | 2018-03-21 13:37:46 +0000 | [diff] [blame] | 337 | int* start_pos, |
| 338 | FPDF_LINK* link_annot) { |
| 339 | if (!start_pos || !link_annot) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 340 | return false; |
Tom Sepez | db0be96 | 2015-10-16 14:00:21 -0700 | [diff] [blame] | 341 | CPDF_Page* pPage = CPDFPageFromFPDFPage(page); |
| 342 | if (!pPage || !pPage->m_pFormDict) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 343 | return false; |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 344 | CPDF_Array* pAnnots = pPage->m_pFormDict->GetArrayFor("Annots"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 345 | if (!pAnnots) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 346 | return false; |
Lei Zhang | c3cffbe | 2018-03-21 13:37:46 +0000 | [diff] [blame] | 347 | for (size_t i = *start_pos; i < pAnnots->GetCount(); i++) { |
Lei Zhang | 11767d3 | 2018-03-21 18:59:44 +0000 | [diff] [blame] | 348 | CPDF_Dictionary* pDict = ToDictionary(pAnnots->GetDirectObjectAt(i)); |
Dan Sinclair | f1251c1 | 2015-10-20 16:24:45 -0400 | [diff] [blame] | 349 | if (!pDict) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 350 | continue; |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 351 | if (pDict->GetStringFor("Subtype") == "Link") { |
Lei Zhang | c3cffbe | 2018-03-21 13:37:46 +0000 | [diff] [blame] | 352 | *start_pos = static_cast<int>(i + 1); |
Tom Sepez | 525147a | 2018-05-03 17:19:53 +0000 | [diff] [blame^] | 353 | *link_annot = FPDFLinkFromCPDFDictionary(pDict); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 354 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 355 | } |
| 356 | } |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 357 | return false; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 358 | } |
| 359 | |
Lei Zhang | c3cffbe | 2018-03-21 13:37:46 +0000 | [diff] [blame] | 360 | FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFLink_GetAnnotRect(FPDF_LINK link_annot, |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 361 | FS_RECTF* rect) { |
Lei Zhang | c3cffbe | 2018-03-21 13:37:46 +0000 | [diff] [blame] | 362 | if (!link_annot || !rect) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 363 | return false; |
Lei Zhang | 11767d3 | 2018-03-21 18:59:44 +0000 | [diff] [blame] | 364 | CPDF_Dictionary* pAnnotDict = CPDFDictionaryFromFPDFLink(link_annot); |
Lei Zhang | 367e7de | 2017-10-31 13:32:17 +0000 | [diff] [blame] | 365 | FSRECTFFromCFXFloatRect(pAnnotDict->GetRectFor("Rect"), rect); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 366 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 367 | } |
| 368 | |
Lei Zhang | c3cffbe | 2018-03-21 13:37:46 +0000 | [diff] [blame] | 369 | FPDF_EXPORT int FPDF_CALLCONV FPDFLink_CountQuadPoints(FPDF_LINK link_annot) { |
Lei Zhang | aaed698 | 2018-03-22 18:39:05 +0000 | [diff] [blame] | 370 | const CPDF_Array* pArray = |
| 371 | GetQuadPointsArrayFromDictionary(CPDFDictionaryFromFPDFLink(link_annot)); |
| 372 | return pArray ? static_cast<int>(pArray->GetCount() / 8) : 0; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 373 | } |
| 374 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 375 | FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV |
Lei Zhang | c3cffbe | 2018-03-21 13:37:46 +0000 | [diff] [blame] | 376 | FPDFLink_GetQuadPoints(FPDF_LINK link_annot, |
| 377 | int quad_index, |
| 378 | FS_QUADPOINTSF* quad_points) { |
Lei Zhang | aaed698 | 2018-03-22 18:39:05 +0000 | [diff] [blame] | 379 | if (!quad_points || quad_index < 0) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 380 | return false; |
Ralf Sippl | 1638179 | 2018-04-12 21:20:26 +0000 | [diff] [blame] | 381 | |
| 382 | CPDF_Dictionary* pLinkDict = CPDFDictionaryFromFPDFLink(link_annot); |
| 383 | if (!pLinkDict) |
| 384 | return false; |
| 385 | |
| 386 | const CPDF_Array* pArray = GetQuadPointsArrayFromDictionary(pLinkDict); |
| 387 | if (!pArray) |
| 388 | return false; |
| 389 | |
| 390 | return GetQuadPointsAtIndex(pArray, static_cast<size_t>(quad_index), |
| 391 | quad_points); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 392 | } |
| 393 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 394 | FPDF_EXPORT unsigned long FPDF_CALLCONV FPDF_GetMetaText(FPDF_DOCUMENT document, |
| 395 | FPDF_BYTESTRING tag, |
| 396 | void* buffer, |
| 397 | unsigned long buflen) { |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 398 | if (!tag) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 399 | return 0; |
thestig | 733e068 | 2016-11-23 05:52:39 -0800 | [diff] [blame] | 400 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 401 | if (!pDoc) |
| 402 | return 0; |
Henrique Nakashima | b73ce7b | 2017-06-19 16:04:34 -0400 | [diff] [blame] | 403 | pDoc->LoadDocumentInfo(); |
Lei Zhang | 0158106 | 2017-08-30 14:19:26 -0700 | [diff] [blame] | 404 | const CPDF_Dictionary* pInfo = pDoc->GetInfo(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 405 | if (!pInfo) |
| 406 | return 0; |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 407 | WideString text = pInfo->GetUnicodeTextFor(tag); |
thestig | 733e068 | 2016-11-23 05:52:39 -0800 | [diff] [blame] | 408 | return Utf16EncodeMaybeCopyAndReturnLength(text, buffer, buflen); |
| 409 | } |
| 410 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 411 | FPDF_EXPORT unsigned long FPDF_CALLCONV |
| 412 | FPDF_GetPageLabel(FPDF_DOCUMENT document, |
| 413 | int page_index, |
| 414 | void* buffer, |
| 415 | unsigned long buflen) { |
thestig | 733e068 | 2016-11-23 05:52:39 -0800 | [diff] [blame] | 416 | if (page_index < 0) |
| 417 | return 0; |
| 418 | |
| 419 | // CPDF_PageLabel can deal with NULL |document|. |
| 420 | CPDF_PageLabel label(CPDFDocumentFromFPDFDocument(document)); |
Ryan Harrison | 8eeee77 | 2018-01-05 13:39:22 -0500 | [diff] [blame] | 421 | Optional<WideString> str = label.GetLabel(page_index); |
| 422 | return str.has_value() |
| 423 | ? Utf16EncodeMaybeCopyAndReturnLength(str.value(), buffer, buflen) |
| 424 | : 0; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 425 | } |