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