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, |
Wei Li | 0e2e5d7 | 2016-03-03 11:28:06 -0800 | [diff] [blame] | 27 | const CFX_WideString& title, |
| 28 | std::set<CPDF_Dictionary*>* visited) { |
| 29 | // Return if already checked to avoid circular calling. |
| 30 | if (pdfium::ContainsKey(*visited, bookmark.GetDict())) |
| 31 | return CPDF_Bookmark(); |
| 32 | visited->insert(bookmark.GetDict()); |
| 33 | |
Wei 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 | |
Lei Zhang | bdf72c3 | 2015-08-14 19:24:08 -0700 | [diff] [blame] | 56 | CPDF_Document* pDoc = page->m_pDocument; |
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 | |
thestig | 733e068 | 2016-11-23 05:52:39 -0800 | [diff] [blame] | 63 | unsigned long Utf16EncodeMaybeCopyAndReturnLength(const CFX_WideString& text, |
| 64 | void* buffer, |
| 65 | unsigned long buflen) { |
| 66 | CFX_ByteString encodedText = text.UTF16LE_Encode(); |
| 67 | unsigned long len = encodedText.GetLength(); |
thestig | 9067fd6 | 2016-11-23 14:10:06 -0800 | [diff] [blame] | 68 | if (buffer && len <= buflen) |
Dan Sinclair | 1c5d0b4 | 2017-04-03 15:05:11 -0400 | [diff] [blame] | 69 | memcpy(buffer, encodedText.c_str(), len); |
thestig | 733e068 | 2016-11-23 05:52:39 -0800 | [diff] [blame] | 70 | return len; |
| 71 | } |
| 72 | |
Lei Zhang | bdf72c3 | 2015-08-14 19:24:08 -0700 | [diff] [blame] | 73 | } // namespace |
| 74 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 75 | DLLEXPORT FPDF_BOOKMARK STDCALL |
| 76 | FPDFBookmark_GetFirstChild(FPDF_DOCUMENT document, FPDF_BOOKMARK pDict) { |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 77 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
| 78 | if (!pDoc) |
| 79 | return nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 80 | CPDF_BookmarkTree tree(pDoc); |
Dan Sinclair | f1251c1 | 2015-10-20 16:24:45 -0400 | [diff] [blame] | 81 | CPDF_Bookmark bookmark = |
| 82 | CPDF_Bookmark(ToDictionary(static_cast<CPDF_Object*>(pDict))); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 83 | return tree.GetFirstChild(bookmark).GetDict(); |
Bo Xu | 4d62b6b | 2015-01-10 22:52:59 -0800 | [diff] [blame] | 84 | } |
| 85 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 86 | DLLEXPORT FPDF_BOOKMARK STDCALL |
| 87 | FPDFBookmark_GetNextSibling(FPDF_DOCUMENT document, FPDF_BOOKMARK pDict) { |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 88 | if (!pDict) |
| 89 | return nullptr; |
| 90 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
| 91 | if (!pDoc) |
| 92 | return nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 93 | CPDF_BookmarkTree tree(pDoc); |
Dan Sinclair | f1251c1 | 2015-10-20 16:24:45 -0400 | [diff] [blame] | 94 | CPDF_Bookmark bookmark = |
| 95 | CPDF_Bookmark(ToDictionary(static_cast<CPDF_Object*>(pDict))); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 96 | return tree.GetNextSibling(bookmark).GetDict(); |
Bo Xu | 4d62b6b | 2015-01-10 22:52:59 -0800 | [diff] [blame] | 97 | } |
| 98 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 99 | DLLEXPORT unsigned long STDCALL FPDFBookmark_GetTitle(FPDF_BOOKMARK pDict, |
| 100 | void* buffer, |
| 101 | unsigned long buflen) { |
| 102 | if (!pDict) |
| 103 | return 0; |
Dan Sinclair | f1251c1 | 2015-10-20 16:24:45 -0400 | [diff] [blame] | 104 | CPDF_Bookmark bookmark(ToDictionary(static_cast<CPDF_Object*>(pDict))); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 105 | CFX_WideString title = bookmark.GetTitle(); |
thestig | 733e068 | 2016-11-23 05:52:39 -0800 | [diff] [blame] | 106 | return Utf16EncodeMaybeCopyAndReturnLength(title, buffer, buflen); |
Bo Xu | 4d62b6b | 2015-01-10 22:52:59 -0800 | [diff] [blame] | 107 | } |
| 108 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 109 | DLLEXPORT FPDF_BOOKMARK STDCALL FPDFBookmark_Find(FPDF_DOCUMENT document, |
| 110 | FPDF_WIDESTRING title) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 111 | if (!title || title[0] == 0) |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 112 | return nullptr; |
| 113 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
| 114 | if (!pDoc) |
| 115 | return nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 116 | CPDF_BookmarkTree tree(pDoc); |
| 117 | FX_STRSIZE len = CFX_WideString::WStringLength(title); |
| 118 | CFX_WideString encodedTitle = CFX_WideString::FromUTF16LE(title, len); |
Wei Li | 0e2e5d7 | 2016-03-03 11:28:06 -0800 | [diff] [blame] | 119 | std::set<CPDF_Dictionary*> visited; |
| 120 | return FindBookmark(tree, CPDF_Bookmark(), encodedTitle, &visited).GetDict(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 123 | DLLEXPORT FPDF_DEST STDCALL FPDFBookmark_GetDest(FPDF_DOCUMENT document, |
| 124 | FPDF_BOOKMARK pDict) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 125 | if (!pDict) |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 126 | return nullptr; |
| 127 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
| 128 | if (!pDoc) |
| 129 | return nullptr; |
Dan Sinclair | f1251c1 | 2015-10-20 16:24:45 -0400 | [diff] [blame] | 130 | CPDF_Bookmark bookmark(ToDictionary(static_cast<CPDF_Object*>(pDict))); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 131 | CPDF_Dest dest = bookmark.GetDest(pDoc); |
Wei Li | 0fc6b25 | 2016-03-01 16:29:41 -0800 | [diff] [blame] | 132 | if (dest.GetObject()) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 133 | return dest.GetObject(); |
| 134 | // If this bookmark is not directly associated with a dest, we try to get |
| 135 | // action |
| 136 | CPDF_Action action = bookmark.GetAction(); |
Wei Li | 0fc6b25 | 2016-03-01 16:29:41 -0800 | [diff] [blame] | 137 | if (!action.GetDict()) |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 138 | return nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 139 | return action.GetDest(pDoc).GetObject(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 142 | DLLEXPORT FPDF_ACTION STDCALL FPDFBookmark_GetAction(FPDF_BOOKMARK pDict) { |
| 143 | if (!pDict) |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 144 | return nullptr; |
Dan Sinclair | f1251c1 | 2015-10-20 16:24:45 -0400 | [diff] [blame] | 145 | CPDF_Bookmark bookmark(ToDictionary(static_cast<CPDF_Object*>(pDict))); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 146 | return bookmark.GetAction().GetDict(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 149 | DLLEXPORT unsigned long STDCALL FPDFAction_GetType(FPDF_ACTION pDict) { |
| 150 | if (!pDict) |
Lei Zhang | e0947b3 | 2015-09-17 14:51:48 -0700 | [diff] [blame] | 151 | return PDFACTION_UNSUPPORTED; |
| 152 | |
Dan Sinclair | f1251c1 | 2015-10-20 16:24:45 -0400 | [diff] [blame] | 153 | CPDF_Action action(ToDictionary(static_cast<CPDF_Object*>(pDict))); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 154 | CPDF_Action::ActionType type = action.GetType(); |
| 155 | switch (type) { |
| 156 | case CPDF_Action::GoTo: |
| 157 | return PDFACTION_GOTO; |
| 158 | case CPDF_Action::GoToR: |
| 159 | return PDFACTION_REMOTEGOTO; |
| 160 | case CPDF_Action::URI: |
| 161 | return PDFACTION_URI; |
| 162 | case CPDF_Action::Launch: |
| 163 | return PDFACTION_LAUNCH; |
| 164 | default: |
| 165 | return PDFACTION_UNSUPPORTED; |
| 166 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 169 | DLLEXPORT FPDF_DEST STDCALL FPDFAction_GetDest(FPDF_DOCUMENT document, |
| 170 | FPDF_ACTION pDict) { |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 171 | if (!pDict) |
Lei Zhang | e0947b3 | 2015-09-17 14:51:48 -0700 | [diff] [blame] | 172 | return nullptr; |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 173 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
| 174 | if (!pDoc) |
| 175 | return nullptr; |
Dan Sinclair | f1251c1 | 2015-10-20 16:24:45 -0400 | [diff] [blame] | 176 | CPDF_Action action(ToDictionary(static_cast<CPDF_Object*>(pDict))); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 177 | return action.GetDest(pDoc).GetObject(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Dan Sinclair | f766ad2 | 2016-03-14 13:51:24 -0400 | [diff] [blame] | 180 | DLLEXPORT unsigned long STDCALL FPDFAction_GetFilePath(FPDF_ACTION pDict, |
| 181 | void* buffer, |
| 182 | unsigned long buflen) { |
Lei Zhang | e0947b3 | 2015-09-17 14:51:48 -0700 | [diff] [blame] | 183 | unsigned long type = FPDFAction_GetType(pDict); |
| 184 | if (type != PDFACTION_REMOTEGOTO && type != PDFACTION_LAUNCH) |
| 185 | return 0; |
| 186 | |
Dan Sinclair | f1251c1 | 2015-10-20 16:24:45 -0400 | [diff] [blame] | 187 | CPDF_Action action(ToDictionary(static_cast<CPDF_Object*>(pDict))); |
Lei Zhang | e0947b3 | 2015-09-17 14:51:48 -0700 | [diff] [blame] | 188 | CFX_ByteString path = action.GetFilePath().UTF8Encode(); |
| 189 | unsigned long len = path.GetLength() + 1; |
thestig | 9067fd6 | 2016-11-23 14:10:06 -0800 | [diff] [blame] | 190 | if (buffer && len <= buflen) |
Dan Sinclair | 1c5d0b4 | 2017-04-03 15:05:11 -0400 | [diff] [blame] | 191 | memcpy(buffer, path.c_str(), len); |
Lei Zhang | e0947b3 | 2015-09-17 14:51:48 -0700 | [diff] [blame] | 192 | return len; |
| 193 | } |
| 194 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 195 | DLLEXPORT unsigned long STDCALL FPDFAction_GetURIPath(FPDF_DOCUMENT document, |
| 196 | FPDF_ACTION pDict, |
| 197 | void* buffer, |
| 198 | unsigned long buflen) { |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 199 | if (!pDict) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 200 | return 0; |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 201 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
| 202 | if (!pDoc) |
| 203 | return 0; |
Dan Sinclair | f1251c1 | 2015-10-20 16:24:45 -0400 | [diff] [blame] | 204 | CPDF_Action action(ToDictionary(static_cast<CPDF_Object*>(pDict))); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 205 | CFX_ByteString path = action.GetURI(pDoc); |
| 206 | unsigned long len = path.GetLength() + 1; |
thestig | 9067fd6 | 2016-11-23 14:10:06 -0800 | [diff] [blame] | 207 | if (buffer && len <= buflen) |
Dan Sinclair | 1c5d0b4 | 2017-04-03 15:05:11 -0400 | [diff] [blame] | 208 | memcpy(buffer, path.c_str(), len); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 209 | return len; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 212 | DLLEXPORT unsigned long STDCALL FPDFDest_GetPageIndex(FPDF_DOCUMENT document, |
| 213 | FPDF_DEST pDict) { |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 214 | if (!pDict) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 215 | return 0; |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 216 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
| 217 | if (!pDoc) |
| 218 | return 0; |
Dan Sinclair | 2b11dc1 | 2015-10-22 15:02:06 -0400 | [diff] [blame] | 219 | CPDF_Dest dest(static_cast<CPDF_Array*>(pDict)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 220 | return dest.GetPageIndex(pDoc); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 221 | } |
| 222 | |
dsinclair | c59fa88 | 2016-11-08 06:55:40 -0800 | [diff] [blame] | 223 | DLLEXPORT FPDF_BOOL STDCALL FPDFDest_GetLocationInPage(FPDF_DEST pDict, |
| 224 | FPDF_BOOL* hasXVal, |
| 225 | FPDF_BOOL* hasYVal, |
| 226 | FPDF_BOOL* hasZoomVal, |
| 227 | FS_FLOAT* x, |
| 228 | FS_FLOAT* y, |
| 229 | FS_FLOAT* zoom) { |
| 230 | if (!pDict) |
| 231 | return false; |
| 232 | |
Tom Sepez | fe91c6c | 2017-05-16 15:33:20 -0700 | [diff] [blame^] | 233 | auto dest = pdfium::MakeUnique<CPDF_Dest>(static_cast<CPDF_Object*>(pDict)); |
dsinclair | c59fa88 | 2016-11-08 06:55:40 -0800 | [diff] [blame] | 234 | |
| 235 | // FPDF_BOOL is an int, GetXYZ expects bools. |
| 236 | bool bHasX; |
| 237 | bool bHasY; |
| 238 | bool bHasZoom; |
| 239 | if (!dest->GetXYZ(&bHasX, &bHasY, &bHasZoom, x, y, zoom)) |
| 240 | return false; |
| 241 | |
| 242 | *hasXVal = bHasX; |
| 243 | *hasYVal = bHasY; |
| 244 | *hasZoomVal = bHasZoom; |
| 245 | return true; |
| 246 | } |
| 247 | |
Dan Sinclair | f766ad2 | 2016-03-14 13:51:24 -0400 | [diff] [blame] | 248 | DLLEXPORT FPDF_LINK STDCALL FPDFLink_GetLinkAtPoint(FPDF_PAGE page, |
| 249 | double x, |
| 250 | double y) { |
Tom Sepez | db0be96 | 2015-10-16 14:00:21 -0700 | [diff] [blame] | 251 | CPDF_Page* pPage = CPDFPageFromFPDFPage(page); |
Lei Zhang | bdf72c3 | 2015-08-14 19:24:08 -0700 | [diff] [blame] | 252 | if (!pPage) |
| 253 | return nullptr; |
| 254 | |
| 255 | CPDF_LinkList* pLinkList = GetLinkList(pPage); |
| 256 | if (!pLinkList) |
| 257 | return nullptr; |
| 258 | |
Dan Sinclair | b45ea1f | 2017-02-21 14:27:59 -0500 | [diff] [blame] | 259 | return pLinkList |
Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 260 | ->GetLinkAtPoint(pPage, |
| 261 | CFX_PointF(static_cast<float>(x), static_cast<float>(y)), |
| 262 | nullptr) |
Lei Zhang | bdf72c3 | 2015-08-14 19:24:08 -0700 | [diff] [blame] | 263 | .GetDict(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 264 | } |
| 265 | |
Dan Sinclair | f766ad2 | 2016-03-14 13:51:24 -0400 | [diff] [blame] | 266 | DLLEXPORT int STDCALL FPDFLink_GetLinkZOrderAtPoint(FPDF_PAGE page, |
| 267 | double x, |
| 268 | double y) { |
Tom Sepez | db0be96 | 2015-10-16 14:00:21 -0700 | [diff] [blame] | 269 | CPDF_Page* pPage = CPDFPageFromFPDFPage(page); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 270 | if (!pPage) |
Lei Zhang | bdf72c3 | 2015-08-14 19:24:08 -0700 | [diff] [blame] | 271 | return -1; |
| 272 | |
| 273 | CPDF_LinkList* pLinkList = GetLinkList(pPage); |
| 274 | if (!pLinkList) |
| 275 | return -1; |
| 276 | |
| 277 | int z_order = -1; |
Dan Sinclair | b45ea1f | 2017-02-21 14:27:59 -0500 | [diff] [blame] | 278 | pLinkList->GetLinkAtPoint( |
Dan Sinclair | 05df075 | 2017-03-14 14:43:42 -0400 | [diff] [blame] | 279 | pPage, CFX_PointF(static_cast<float>(x), static_cast<float>(y)), |
Dan Sinclair | b45ea1f | 2017-02-21 14:27:59 -0500 | [diff] [blame] | 280 | &z_order); |
Lei Zhang | bdf72c3 | 2015-08-14 19:24:08 -0700 | [diff] [blame] | 281 | return z_order; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 282 | } |
| 283 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 284 | DLLEXPORT FPDF_DEST STDCALL FPDFLink_GetDest(FPDF_DOCUMENT document, |
| 285 | FPDF_LINK pDict) { |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 286 | if (!pDict) |
Lei Zhang | e0947b3 | 2015-09-17 14:51:48 -0700 | [diff] [blame] | 287 | return nullptr; |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 288 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
| 289 | if (!pDoc) |
| 290 | return nullptr; |
Dan Sinclair | f1251c1 | 2015-10-20 16:24:45 -0400 | [diff] [blame] | 291 | CPDF_Link link(ToDictionary(static_cast<CPDF_Object*>(pDict))); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 292 | FPDF_DEST dest = link.GetDest(pDoc).GetObject(); |
| 293 | if (dest) |
| 294 | return dest; |
| 295 | // If this link is not directly associated with a dest, we try to get action |
| 296 | CPDF_Action action = link.GetAction(); |
Wei Li | 0fc6b25 | 2016-03-01 16:29:41 -0800 | [diff] [blame] | 297 | if (!action.GetDict()) |
Lei Zhang | e0947b3 | 2015-09-17 14:51:48 -0700 | [diff] [blame] | 298 | return nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 299 | return action.GetDest(pDoc).GetObject(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 300 | } |
| 301 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 302 | DLLEXPORT FPDF_ACTION STDCALL FPDFLink_GetAction(FPDF_LINK pDict) { |
| 303 | if (!pDict) |
Lei Zhang | e0947b3 | 2015-09-17 14:51:48 -0700 | [diff] [blame] | 304 | return nullptr; |
| 305 | |
Dan Sinclair | f1251c1 | 2015-10-20 16:24:45 -0400 | [diff] [blame] | 306 | CPDF_Link link(ToDictionary(static_cast<CPDF_Object*>(pDict))); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 307 | return link.GetAction().GetDict(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 308 | } |
| 309 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 310 | DLLEXPORT FPDF_BOOL STDCALL FPDFLink_Enumerate(FPDF_PAGE page, |
| 311 | int* startPos, |
| 312 | FPDF_LINK* linkAnnot) { |
Tom Sepez | db0be96 | 2015-10-16 14:00:21 -0700 | [diff] [blame] | 313 | if (!startPos || !linkAnnot) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 314 | return false; |
Tom Sepez | db0be96 | 2015-10-16 14:00:21 -0700 | [diff] [blame] | 315 | CPDF_Page* pPage = CPDFPageFromFPDFPage(page); |
| 316 | if (!pPage || !pPage->m_pFormDict) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 317 | return false; |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 318 | CPDF_Array* pAnnots = pPage->m_pFormDict->GetArrayFor("Annots"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 319 | if (!pAnnots) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 320 | return false; |
Wei Li | e1aebd4 | 2016-04-11 10:02:09 -0700 | [diff] [blame] | 321 | for (size_t i = *startPos; i < pAnnots->GetCount(); i++) { |
Dan Sinclair | f1251c1 | 2015-10-20 16:24:45 -0400 | [diff] [blame] | 322 | CPDF_Dictionary* pDict = |
tsepez | bd56755 | 2016-03-29 14:51:50 -0700 | [diff] [blame] | 323 | ToDictionary(static_cast<CPDF_Object*>(pAnnots->GetDirectObjectAt(i))); |
Dan Sinclair | f1251c1 | 2015-10-20 16:24:45 -0400 | [diff] [blame] | 324 | if (!pDict) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 325 | continue; |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 326 | if (pDict->GetStringFor("Subtype") == "Link") { |
Wei Li | e1aebd4 | 2016-04-11 10:02:09 -0700 | [diff] [blame] | 327 | *startPos = static_cast<int>(i + 1); |
| 328 | *linkAnnot = static_cast<FPDF_LINK>(pDict); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 329 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 330 | } |
| 331 | } |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 332 | return false; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 333 | } |
| 334 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 335 | DLLEXPORT FPDF_BOOL STDCALL FPDFLink_GetAnnotRect(FPDF_LINK linkAnnot, |
| 336 | FS_RECTF* rect) { |
| 337 | if (!linkAnnot || !rect) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 338 | return false; |
Dan Sinclair | f1251c1 | 2015-10-20 16:24:45 -0400 | [diff] [blame] | 339 | CPDF_Dictionary* pAnnotDict = |
| 340 | ToDictionary(static_cast<CPDF_Object*>(linkAnnot)); |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 341 | CFX_FloatRect rt = pAnnotDict->GetRectFor("Rect"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 342 | rect->left = rt.left; |
| 343 | rect->bottom = rt.bottom; |
| 344 | rect->right = rt.right; |
| 345 | rect->top = rt.top; |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 346 | return true; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | DLLEXPORT int STDCALL FPDFLink_CountQuadPoints(FPDF_LINK linkAnnot) { |
| 350 | if (!linkAnnot) |
| 351 | return 0; |
Dan Sinclair | f1251c1 | 2015-10-20 16:24:45 -0400 | [diff] [blame] | 352 | CPDF_Dictionary* pAnnotDict = |
| 353 | ToDictionary(static_cast<CPDF_Object*>(linkAnnot)); |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 354 | CPDF_Array* pArray = pAnnotDict->GetArrayFor("QuadPoints"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 355 | if (!pArray) |
| 356 | return 0; |
Wei Li | e1aebd4 | 2016-04-11 10:02:09 -0700 | [diff] [blame] | 357 | return static_cast<int>(pArray->GetCount() / 8); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | DLLEXPORT FPDF_BOOL STDCALL FPDFLink_GetQuadPoints(FPDF_LINK linkAnnot, |
| 361 | int quadIndex, |
| 362 | FS_QUADPOINTSF* quadPoints) { |
| 363 | if (!linkAnnot || !quadPoints) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 364 | return false; |
Dan Sinclair | f1251c1 | 2015-10-20 16:24:45 -0400 | [diff] [blame] | 365 | CPDF_Dictionary* pAnnotDict = |
| 366 | ToDictionary(static_cast<CPDF_Object*>(linkAnnot)); |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 367 | CPDF_Array* pArray = pAnnotDict->GetArrayFor("QuadPoints"); |
thestig | 733e068 | 2016-11-23 05:52:39 -0800 | [diff] [blame] | 368 | if (!pArray) |
| 369 | return false; |
| 370 | |
| 371 | if (quadIndex < 0 || |
| 372 | static_cast<size_t>(quadIndex) >= pArray->GetCount() / 8 || |
| 373 | (static_cast<size_t>(quadIndex * 8 + 7) >= pArray->GetCount())) { |
| 374 | return false; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 375 | } |
thestig | 733e068 | 2016-11-23 05:52:39 -0800 | [diff] [blame] | 376 | |
| 377 | quadPoints->x1 = pArray->GetNumberAt(quadIndex * 8); |
| 378 | quadPoints->y1 = pArray->GetNumberAt(quadIndex * 8 + 1); |
| 379 | quadPoints->x2 = pArray->GetNumberAt(quadIndex * 8 + 2); |
| 380 | quadPoints->y2 = pArray->GetNumberAt(quadIndex * 8 + 3); |
| 381 | quadPoints->x3 = pArray->GetNumberAt(quadIndex * 8 + 4); |
| 382 | quadPoints->y3 = pArray->GetNumberAt(quadIndex * 8 + 5); |
| 383 | quadPoints->x4 = pArray->GetNumberAt(quadIndex * 8 + 6); |
| 384 | quadPoints->y4 = pArray->GetNumberAt(quadIndex * 8 + 7); |
| 385 | return true; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 386 | } |
| 387 | |
thestig | 733e068 | 2016-11-23 05:52:39 -0800 | [diff] [blame] | 388 | DLLEXPORT unsigned long STDCALL FPDF_GetMetaText(FPDF_DOCUMENT document, |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 389 | FPDF_BYTESTRING tag, |
| 390 | void* buffer, |
| 391 | unsigned long buflen) { |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 392 | if (!tag) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 393 | return 0; |
thestig | 733e068 | 2016-11-23 05:52:39 -0800 | [diff] [blame] | 394 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 395 | if (!pDoc) |
| 396 | return 0; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 397 | CPDF_Dictionary* pInfo = pDoc->GetInfo(); |
| 398 | if (!pInfo) |
| 399 | return 0; |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 400 | CFX_WideString text = pInfo->GetUnicodeTextFor(tag); |
thestig | 733e068 | 2016-11-23 05:52:39 -0800 | [diff] [blame] | 401 | return Utf16EncodeMaybeCopyAndReturnLength(text, buffer, buflen); |
| 402 | } |
| 403 | |
dsinclair | 6bdb56c | 2016-12-06 09:53:27 -0800 | [diff] [blame] | 404 | DLLEXPORT unsigned long STDCALL FPDF_GetPageLabel(FPDF_DOCUMENT document, |
| 405 | int page_index, |
| 406 | void* buffer, |
| 407 | unsigned long buflen) { |
thestig | 733e068 | 2016-11-23 05:52:39 -0800 | [diff] [blame] | 408 | if (page_index < 0) |
| 409 | return 0; |
| 410 | |
| 411 | // CPDF_PageLabel can deal with NULL |document|. |
| 412 | CPDF_PageLabel label(CPDFDocumentFromFPDFDocument(document)); |
| 413 | CFX_WideString str; |
| 414 | if (!label.GetLabel(page_index, &str)) |
| 415 | return 0; |
| 416 | return Utf16EncodeMaybeCopyAndReturnLength(str, buffer, buflen); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 417 | } |