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" |
dsinclair | 1727aee | 2016-09-29 13:12:56 -0700 | [diff] [blame] | 15 | #include "core/fpdfdoc/cpdf_bookmark.h" |
| 16 | #include "core/fpdfdoc/cpdf_bookmarktree.h" |
dsinclair | c59fa88 | 2016-11-08 06:55:40 -0800 | [diff] [blame] | 17 | #include "core/fpdfdoc/cpdf_dest.h" |
thestig | 733e068 | 2016-11-23 05:52:39 -0800 | [diff] [blame] | 18 | #include "core/fpdfdoc/cpdf_pagelabel.h" |
dsinclair | 114e46a | 2016-09-29 17:18:21 -0700 | [diff] [blame] | 19 | #include "fpdfsdk/fsdk_define.h" |
tsepez | a9caab9 | 2016-12-14 05:57:10 -0800 | [diff] [blame] | 20 | #include "third_party/base/ptr_util.h" |
Wei Li | 0e2e5d7 | 2016-03-03 11:28:06 -0800 | [diff] [blame] | 21 | #include "third_party/base/stl_util.h" |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 22 | |
Lei Zhang | bdf72c3 | 2015-08-14 19:24:08 -0700 | [diff] [blame] | 23 | namespace { |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 24 | |
Lei Zhang | bdf72c3 | 2015-08-14 19:24:08 -0700 | [diff] [blame] | 25 | CPDF_Bookmark FindBookmark(const CPDF_BookmarkTree& tree, |
| 26 | CPDF_Bookmark bookmark, |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 27 | const WideString& title, |
Wei Li | 0e2e5d7 | 2016-03-03 11:28:06 -0800 | [diff] [blame] | 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 Li | 0fc6b25 | 2016-03-01 16:29:41 -0800 | [diff] [blame] | 34 | if (bookmark.GetDict() && |
| 35 | bookmark.GetTitle().CompareNoCase(title.c_str()) == 0) { |
Wei Li | 0e2e5d7 | 2016-03-03 11:28:06 -0800 | [diff] [blame] | 36 | // First check this item. |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 37 | return bookmark; |
| 38 | } |
Wei Li | 0e2e5d7 | 2016-03-03 11:28:06 -0800 | [diff] [blame] | 39 | |
| 40 | // Go into children items. |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 41 | CPDF_Bookmark child = tree.GetFirstChild(bookmark); |
Wei Li | 0e2e5d7 | 2016-03-03 11:28:06 -0800 | [diff] [blame] | 42 | 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 Li | 0fc6b25 | 2016-03-01 16:29:41 -0800 | [diff] [blame] | 45 | if (found.GetDict()) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 46 | return found; |
| 47 | child = tree.GetNextSibling(child); |
| 48 | } |
| 49 | return CPDF_Bookmark(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Lei Zhang | bdf72c3 | 2015-08-14 19:24:08 -0700 | [diff] [blame] | 52 | CPDF_LinkList* GetLinkList(CPDF_Page* page) { |
| 53 | if (!page) |
| 54 | return nullptr; |
| 55 | |
Tom Sepez | 4cb82ee | 2017-05-22 15:15:30 -0700 | [diff] [blame] | 56 | CPDF_Document* pDoc = page->m_pDocument.Get(); |
tsepez | 5ce0968 | 2016-05-25 16:16:32 -0700 | [diff] [blame] | 57 | std::unique_ptr<CPDF_LinkList>* pHolder = pDoc->LinksContext(); |
| 58 | if (!pHolder->get()) |
tsepez | a9caab9 | 2016-12-14 05:57:10 -0800 | [diff] [blame] | 59 | *pHolder = pdfium::MakeUnique<CPDF_LinkList>(); |
tsepez | 5ce0968 | 2016-05-25 16:16:32 -0700 | [diff] [blame] | 60 | return pHolder->get(); |
Lei Zhang | bdf72c3 | 2015-08-14 19:24:08 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | } // namespace |
| 64 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 65 | FPDF_EXPORT FPDF_BOOKMARK FPDF_CALLCONV |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 66 | FPDFBookmark_GetFirstChild(FPDF_DOCUMENT document, FPDF_BOOKMARK pDict) { |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 67 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
| 68 | if (!pDoc) |
| 69 | return nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 70 | CPDF_BookmarkTree tree(pDoc); |
Dan Sinclair | f1251c1 | 2015-10-20 16:24:45 -0400 | [diff] [blame] | 71 | CPDF_Bookmark bookmark = |
| 72 | CPDF_Bookmark(ToDictionary(static_cast<CPDF_Object*>(pDict))); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 73 | return 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; |
| 80 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
| 81 | if (!pDoc) |
| 82 | return nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 83 | CPDF_BookmarkTree tree(pDoc); |
Dan Sinclair | f1251c1 | 2015-10-20 16:24:45 -0400 | [diff] [blame] | 84 | CPDF_Bookmark bookmark = |
| 85 | CPDF_Bookmark(ToDictionary(static_cast<CPDF_Object*>(pDict))); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 86 | return tree.GetNextSibling(bookmark).GetDict(); |
Bo Xu | 4d62b6b | 2015-01-10 22:52:59 -0800 | [diff] [blame] | 87 | } |
| 88 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 89 | FPDF_EXPORT unsigned long FPDF_CALLCONV |
| 90 | FPDFBookmark_GetTitle(FPDF_BOOKMARK pDict, void* buffer, unsigned long buflen) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 91 | if (!pDict) |
| 92 | return 0; |
Dan Sinclair | f1251c1 | 2015-10-20 16:24:45 -0400 | [diff] [blame] | 93 | CPDF_Bookmark bookmark(ToDictionary(static_cast<CPDF_Object*>(pDict))); |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 94 | WideString title = bookmark.GetTitle(); |
thestig | 733e068 | 2016-11-23 05:52:39 -0800 | [diff] [blame] | 95 | return Utf16EncodeMaybeCopyAndReturnLength(title, buffer, buflen); |
Bo Xu | 4d62b6b | 2015-01-10 22:52:59 -0800 | [diff] [blame] | 96 | } |
| 97 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 98 | FPDF_EXPORT FPDF_BOOKMARK FPDF_CALLCONV |
| 99 | FPDFBookmark_Find(FPDF_DOCUMENT document, FPDF_WIDESTRING title) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 100 | if (!title || title[0] == 0) |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 101 | return nullptr; |
| 102 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
| 103 | if (!pDoc) |
| 104 | return nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 105 | CPDF_BookmarkTree tree(pDoc); |
Ryan Harrison | 875e98c | 2017-09-27 10:53:11 -0400 | [diff] [blame] | 106 | size_t len = WideString::WStringLength(title); |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 107 | WideString encodedTitle = WideString::FromUTF16LE(title, len); |
Wei Li | 0e2e5d7 | 2016-03-03 11:28:06 -0800 | [diff] [blame] | 108 | std::set<CPDF_Dictionary*> visited; |
| 109 | return FindBookmark(tree, CPDF_Bookmark(), encodedTitle, &visited).GetDict(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 112 | FPDF_EXPORT FPDF_DEST FPDF_CALLCONV FPDFBookmark_GetDest(FPDF_DOCUMENT document, |
| 113 | FPDF_BOOKMARK pDict) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 114 | if (!pDict) |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 115 | return nullptr; |
| 116 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
| 117 | if (!pDoc) |
| 118 | return nullptr; |
Dan Sinclair | f1251c1 | 2015-10-20 16:24:45 -0400 | [diff] [blame] | 119 | CPDF_Bookmark bookmark(ToDictionary(static_cast<CPDF_Object*>(pDict))); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 120 | CPDF_Dest dest = bookmark.GetDest(pDoc); |
Wei Li | 0fc6b25 | 2016-03-01 16:29:41 -0800 | [diff] [blame] | 121 | if (dest.GetObject()) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 122 | 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 Li | 0fc6b25 | 2016-03-01 16:29:41 -0800 | [diff] [blame] | 126 | if (!action.GetDict()) |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 127 | return nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 128 | return action.GetDest(pDoc).GetObject(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 131 | FPDF_EXPORT FPDF_ACTION FPDF_CALLCONV |
| 132 | FPDFBookmark_GetAction(FPDF_BOOKMARK pDict) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 133 | if (!pDict) |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 134 | return nullptr; |
Dan Sinclair | f1251c1 | 2015-10-20 16:24:45 -0400 | [diff] [blame] | 135 | CPDF_Bookmark bookmark(ToDictionary(static_cast<CPDF_Object*>(pDict))); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 136 | return bookmark.GetAction().GetDict(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 139 | FPDF_EXPORT unsigned long FPDF_CALLCONV FPDFAction_GetType(FPDF_ACTION pDict) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 140 | if (!pDict) |
Lei Zhang | e0947b3 | 2015-09-17 14:51:48 -0700 | [diff] [blame] | 141 | return PDFACTION_UNSUPPORTED; |
| 142 | |
Dan Sinclair | f1251c1 | 2015-10-20 16:24:45 -0400 | [diff] [blame] | 143 | CPDF_Action action(ToDictionary(static_cast<CPDF_Object*>(pDict))); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 144 | 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-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 159 | FPDF_EXPORT FPDF_DEST FPDF_CALLCONV FPDFAction_GetDest(FPDF_DOCUMENT document, |
| 160 | FPDF_ACTION pDict) { |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 161 | if (!pDict) |
Lei Zhang | e0947b3 | 2015-09-17 14:51:48 -0700 | [diff] [blame] | 162 | return nullptr; |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 163 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
| 164 | if (!pDoc) |
| 165 | return nullptr; |
Dan Sinclair | f1251c1 | 2015-10-20 16:24:45 -0400 | [diff] [blame] | 166 | CPDF_Action action(ToDictionary(static_cast<CPDF_Object*>(pDict))); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 167 | return action.GetDest(pDoc).GetObject(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 170 | FPDF_EXPORT unsigned long FPDF_CALLCONV |
| 171 | FPDFAction_GetFilePath(FPDF_ACTION pDict, void* buffer, unsigned long buflen) { |
Lei Zhang | e0947b3 | 2015-09-17 14:51:48 -0700 | [diff] [blame] | 172 | unsigned long type = FPDFAction_GetType(pDict); |
| 173 | if (type != PDFACTION_REMOTEGOTO && type != PDFACTION_LAUNCH) |
| 174 | return 0; |
| 175 | |
Dan Sinclair | f1251c1 | 2015-10-20 16:24:45 -0400 | [diff] [blame] | 176 | CPDF_Action action(ToDictionary(static_cast<CPDF_Object*>(pDict))); |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 177 | ByteString path = action.GetFilePath().UTF8Encode(); |
Lei Zhang | e0947b3 | 2015-09-17 14:51:48 -0700 | [diff] [blame] | 178 | unsigned long len = path.GetLength() + 1; |
thestig | 9067fd6 | 2016-11-23 14:10:06 -0800 | [diff] [blame] | 179 | if (buffer && len <= buflen) |
Dan Sinclair | 1c5d0b4 | 2017-04-03 15:05:11 -0400 | [diff] [blame] | 180 | memcpy(buffer, path.c_str(), len); |
Lei Zhang | e0947b3 | 2015-09-17 14:51:48 -0700 | [diff] [blame] | 181 | return len; |
| 182 | } |
| 183 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 184 | FPDF_EXPORT unsigned long FPDF_CALLCONV |
| 185 | FPDFAction_GetURIPath(FPDF_DOCUMENT document, |
| 186 | FPDF_ACTION pDict, |
| 187 | void* buffer, |
| 188 | unsigned long buflen) { |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 189 | if (!pDict) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 190 | return 0; |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 191 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
| 192 | if (!pDoc) |
| 193 | return 0; |
Dan Sinclair | f1251c1 | 2015-10-20 16:24:45 -0400 | [diff] [blame] | 194 | CPDF_Action action(ToDictionary(static_cast<CPDF_Object*>(pDict))); |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 195 | ByteString path = action.GetURI(pDoc); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 196 | unsigned long len = path.GetLength() + 1; |
thestig | 9067fd6 | 2016-11-23 14:10:06 -0800 | [diff] [blame] | 197 | if (buffer && len <= buflen) |
Dan Sinclair | 1c5d0b4 | 2017-04-03 15:05:11 -0400 | [diff] [blame] | 198 | memcpy(buffer, path.c_str(), len); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 199 | return len; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 202 | FPDF_EXPORT unsigned long FPDF_CALLCONV |
| 203 | FPDFDest_GetPageIndex(FPDF_DOCUMENT document, FPDF_DEST pDict) { |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 204 | if (!pDict) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 205 | return 0; |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 206 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
| 207 | if (!pDoc) |
| 208 | return 0; |
Dan Sinclair | 2b11dc1 | 2015-10-22 15:02:06 -0400 | [diff] [blame] | 209 | CPDF_Dest dest(static_cast<CPDF_Array*>(pDict)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 210 | return dest.GetPageIndex(pDoc); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 211 | } |
| 212 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 213 | FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV |
| 214 | FPDFDest_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) { |
dsinclair | c59fa88 | 2016-11-08 06:55:40 -0800 | [diff] [blame] | 221 | if (!pDict) |
| 222 | return false; |
| 223 | |
Tom Sepez | fe91c6c | 2017-05-16 15:33:20 -0700 | [diff] [blame] | 224 | auto dest = pdfium::MakeUnique<CPDF_Dest>(static_cast<CPDF_Object*>(pDict)); |
dsinclair | c59fa88 | 2016-11-08 06:55:40 -0800 | [diff] [blame] | 225 | |
| 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 Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 239 | FPDF_EXPORT FPDF_LINK FPDF_CALLCONV FPDFLink_GetLinkAtPoint(FPDF_PAGE page, |
| 240 | double x, |
| 241 | double y) { |
Tom Sepez | db0be96 | 2015-10-16 14:00:21 -0700 | [diff] [blame] | 242 | CPDF_Page* pPage = CPDFPageFromFPDFPage(page); |
Lei Zhang | bdf72c3 | 2015-08-14 19:24:08 -0700 | [diff] [blame] | 243 | if (!pPage) |
| 244 | return nullptr; |
| 245 | |
| 246 | CPDF_LinkList* pLinkList = GetLinkList(pPage); |
| 247 | if (!pLinkList) |
| 248 | return nullptr; |
| 249 | |
Dan Sinclair | b45ea1f | 2017-02-21 14:27:59 -0500 | [diff] [blame] | 250 | return pLinkList |
Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 251 | ->GetLinkAtPoint(pPage, |
| 252 | CFX_PointF(static_cast<float>(x), static_cast<float>(y)), |
| 253 | nullptr) |
Lei Zhang | bdf72c3 | 2015-08-14 19:24:08 -0700 | [diff] [blame] | 254 | .GetDict(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 255 | } |
| 256 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 257 | FPDF_EXPORT int FPDF_CALLCONV FPDFLink_GetLinkZOrderAtPoint(FPDF_PAGE page, |
| 258 | double x, |
| 259 | double y) { |
Tom Sepez | db0be96 | 2015-10-16 14:00:21 -0700 | [diff] [blame] | 260 | CPDF_Page* pPage = CPDFPageFromFPDFPage(page); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 261 | if (!pPage) |
Lei Zhang | bdf72c3 | 2015-08-14 19:24:08 -0700 | [diff] [blame] | 262 | return -1; |
| 263 | |
| 264 | CPDF_LinkList* pLinkList = GetLinkList(pPage); |
| 265 | if (!pLinkList) |
| 266 | return -1; |
| 267 | |
| 268 | int z_order = -1; |
Dan Sinclair | b45ea1f | 2017-02-21 14:27:59 -0500 | [diff] [blame] | 269 | pLinkList->GetLinkAtPoint( |
Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 270 | pPage, CFX_PointF(static_cast<float>(x), static_cast<float>(y)), |
Dan Sinclair | b45ea1f | 2017-02-21 14:27:59 -0500 | [diff] [blame] | 271 | &z_order); |
Lei Zhang | bdf72c3 | 2015-08-14 19:24:08 -0700 | [diff] [blame] | 272 | return z_order; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 273 | } |
| 274 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 275 | FPDF_EXPORT FPDF_DEST FPDF_CALLCONV FPDFLink_GetDest(FPDF_DOCUMENT document, |
| 276 | FPDF_LINK pDict) { |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 277 | if (!pDict) |
Lei Zhang | e0947b3 | 2015-09-17 14:51:48 -0700 | [diff] [blame] | 278 | return nullptr; |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 279 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
| 280 | if (!pDoc) |
| 281 | return nullptr; |
Dan Sinclair | f1251c1 | 2015-10-20 16:24:45 -0400 | [diff] [blame] | 282 | CPDF_Link link(ToDictionary(static_cast<CPDF_Object*>(pDict))); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 283 | 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 Li | 0fc6b25 | 2016-03-01 16:29:41 -0800 | [diff] [blame] | 288 | if (!action.GetDict()) |
Lei Zhang | e0947b3 | 2015-09-17 14:51:48 -0700 | [diff] [blame] | 289 | return nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 290 | return action.GetDest(pDoc).GetObject(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 291 | } |
| 292 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 293 | FPDF_EXPORT FPDF_ACTION FPDF_CALLCONV FPDFLink_GetAction(FPDF_LINK pDict) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 294 | if (!pDict) |
Lei Zhang | e0947b3 | 2015-09-17 14:51:48 -0700 | [diff] [blame] | 295 | return nullptr; |
| 296 | |
Dan Sinclair | f1251c1 | 2015-10-20 16:24:45 -0400 | [diff] [blame] | 297 | CPDF_Link link(ToDictionary(static_cast<CPDF_Object*>(pDict))); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 298 | return link.GetAction().GetDict(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 299 | } |
| 300 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 301 | FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFLink_Enumerate(FPDF_PAGE page, |
| 302 | int* startPos, |
| 303 | FPDF_LINK* linkAnnot) { |
Tom Sepez | db0be96 | 2015-10-16 14:00:21 -0700 | [diff] [blame] | 304 | if (!startPos || !linkAnnot) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 305 | return false; |
Tom Sepez | db0be96 | 2015-10-16 14:00:21 -0700 | [diff] [blame] | 306 | CPDF_Page* pPage = CPDFPageFromFPDFPage(page); |
| 307 | if (!pPage || !pPage->m_pFormDict) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 308 | return false; |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 309 | CPDF_Array* pAnnots = pPage->m_pFormDict->GetArrayFor("Annots"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 310 | if (!pAnnots) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 311 | return false; |
Wei Li | e1aebd4 | 2016-04-11 10:02:09 -0700 | [diff] [blame] | 312 | for (size_t i = *startPos; i < pAnnots->GetCount(); i++) { |
Dan Sinclair | f1251c1 | 2015-10-20 16:24:45 -0400 | [diff] [blame] | 313 | CPDF_Dictionary* pDict = |
tsepez | bd56755 | 2016-03-29 14:51:50 -0700 | [diff] [blame] | 314 | ToDictionary(static_cast<CPDF_Object*>(pAnnots->GetDirectObjectAt(i))); |
Dan Sinclair | f1251c1 | 2015-10-20 16:24:45 -0400 | [diff] [blame] | 315 | if (!pDict) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 316 | continue; |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 317 | if (pDict->GetStringFor("Subtype") == "Link") { |
Wei Li | e1aebd4 | 2016-04-11 10:02:09 -0700 | [diff] [blame] | 318 | *startPos = static_cast<int>(i + 1); |
| 319 | *linkAnnot = static_cast<FPDF_LINK>(pDict); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 320 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 321 | } |
| 322 | } |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 323 | return false; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 324 | } |
| 325 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 326 | FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFLink_GetAnnotRect(FPDF_LINK linkAnnot, |
| 327 | FS_RECTF* rect) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 328 | if (!linkAnnot || !rect) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 329 | return false; |
Dan Sinclair | f1251c1 | 2015-10-20 16:24:45 -0400 | [diff] [blame] | 330 | CPDF_Dictionary* pAnnotDict = |
| 331 | ToDictionary(static_cast<CPDF_Object*>(linkAnnot)); |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 332 | CFX_FloatRect rt = pAnnotDict->GetRectFor("Rect"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 333 | rect->left = rt.left; |
| 334 | rect->bottom = rt.bottom; |
| 335 | rect->right = rt.right; |
| 336 | rect->top = rt.top; |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 337 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 338 | } |
| 339 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 340 | FPDF_EXPORT int FPDF_CALLCONV FPDFLink_CountQuadPoints(FPDF_LINK linkAnnot) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 341 | if (!linkAnnot) |
| 342 | return 0; |
Dan Sinclair | f1251c1 | 2015-10-20 16:24:45 -0400 | [diff] [blame] | 343 | CPDF_Dictionary* pAnnotDict = |
| 344 | ToDictionary(static_cast<CPDF_Object*>(linkAnnot)); |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 345 | CPDF_Array* pArray = pAnnotDict->GetArrayFor("QuadPoints"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 346 | if (!pArray) |
| 347 | return 0; |
Wei Li | e1aebd4 | 2016-04-11 10:02:09 -0700 | [diff] [blame] | 348 | return static_cast<int>(pArray->GetCount() / 8); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 349 | } |
| 350 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 351 | FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV |
| 352 | FPDFLink_GetQuadPoints(FPDF_LINK linkAnnot, |
| 353 | int quadIndex, |
| 354 | FS_QUADPOINTSF* quadPoints) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 355 | if (!linkAnnot || !quadPoints) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 356 | return false; |
Dan Sinclair | f1251c1 | 2015-10-20 16:24:45 -0400 | [diff] [blame] | 357 | CPDF_Dictionary* pAnnotDict = |
| 358 | ToDictionary(static_cast<CPDF_Object*>(linkAnnot)); |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 359 | CPDF_Array* pArray = pAnnotDict->GetArrayFor("QuadPoints"); |
thestig | 733e068 | 2016-11-23 05:52:39 -0800 | [diff] [blame] | 360 | 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 Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 367 | } |
thestig | 733e068 | 2016-11-23 05:52:39 -0800 | [diff] [blame] | 368 | |
| 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-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 378 | } |
| 379 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 380 | FPDF_EXPORT unsigned long FPDF_CALLCONV FPDF_GetMetaText(FPDF_DOCUMENT document, |
| 381 | FPDF_BYTESTRING tag, |
| 382 | void* buffer, |
| 383 | unsigned long buflen) { |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 384 | if (!tag) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 385 | return 0; |
thestig | 733e068 | 2016-11-23 05:52:39 -0800 | [diff] [blame] | 386 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 387 | if (!pDoc) |
| 388 | return 0; |
Henrique Nakashima | b73ce7b | 2017-06-19 16:04:34 -0400 | [diff] [blame] | 389 | pDoc->LoadDocumentInfo(); |
Lei Zhang | 0158106 | 2017-08-30 14:19:26 -0700 | [diff] [blame] | 390 | const CPDF_Dictionary* pInfo = pDoc->GetInfo(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 391 | if (!pInfo) |
| 392 | return 0; |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 393 | WideString text = pInfo->GetUnicodeTextFor(tag); |
thestig | 733e068 | 2016-11-23 05:52:39 -0800 | [diff] [blame] | 394 | return Utf16EncodeMaybeCopyAndReturnLength(text, buffer, buflen); |
| 395 | } |
| 396 | |
Dan Sinclair | 00d2ad1 | 2017-08-10 14:13:02 -0400 | [diff] [blame] | 397 | FPDF_EXPORT unsigned long FPDF_CALLCONV |
| 398 | FPDF_GetPageLabel(FPDF_DOCUMENT document, |
| 399 | int page_index, |
| 400 | void* buffer, |
| 401 | unsigned long buflen) { |
thestig | 733e068 | 2016-11-23 05:52:39 -0800 | [diff] [blame] | 402 | if (page_index < 0) |
| 403 | return 0; |
| 404 | |
| 405 | // CPDF_PageLabel can deal with NULL |document|. |
| 406 | CPDF_PageLabel label(CPDFDocumentFromFPDFDocument(document)); |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 407 | WideString str; |
thestig | 733e068 | 2016-11-23 05:52:39 -0800 | [diff] [blame] | 408 | if (!label.GetLabel(page_index, &str)) |
| 409 | return 0; |
| 410 | return Utf16EncodeMaybeCopyAndReturnLength(str, buffer, buflen); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 411 | } |