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 | |
Wei Li | 0e2e5d7 | 2016-03-03 11:28:06 -0800 | [diff] [blame] | 9 | #include <set> |
| 10 | |
dsinclair | c6c425a | 2016-09-29 12:01:30 -0700 | [diff] [blame] | 11 | #include "core/fpdfapi/fpdf_parser/cpdf_array.h" |
| 12 | #include "core/fpdfapi/fpdf_parser/cpdf_document.h" |
dsinclair | 41872fa | 2016-10-04 11:29:35 -0700 | [diff] [blame^] | 13 | #include "core/fpdfapi/page/cpdf_page.h" |
dsinclair | 1727aee | 2016-09-29 13:12:56 -0700 | [diff] [blame] | 14 | #include "core/fpdfdoc/cpdf_bookmark.h" |
| 15 | #include "core/fpdfdoc/cpdf_bookmarktree.h" |
dsinclair | 114e46a | 2016-09-29 17:18:21 -0700 | [diff] [blame] | 16 | #include "fpdfsdk/fsdk_define.h" |
Wei Li | 0e2e5d7 | 2016-03-03 11:28:06 -0800 | [diff] [blame] | 17 | #include "third_party/base/stl_util.h" |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 18 | |
Lei Zhang | bdf72c3 | 2015-08-14 19:24:08 -0700 | [diff] [blame] | 19 | namespace { |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 20 | |
Lei Zhang | bdf72c3 | 2015-08-14 19:24:08 -0700 | [diff] [blame] | 21 | CPDF_Bookmark FindBookmark(const CPDF_BookmarkTree& tree, |
| 22 | CPDF_Bookmark bookmark, |
Wei Li | 0e2e5d7 | 2016-03-03 11:28:06 -0800 | [diff] [blame] | 23 | const CFX_WideString& title, |
| 24 | std::set<CPDF_Dictionary*>* visited) { |
| 25 | // Return if already checked to avoid circular calling. |
| 26 | if (pdfium::ContainsKey(*visited, bookmark.GetDict())) |
| 27 | return CPDF_Bookmark(); |
| 28 | visited->insert(bookmark.GetDict()); |
| 29 | |
Wei Li | 0fc6b25 | 2016-03-01 16:29:41 -0800 | [diff] [blame] | 30 | if (bookmark.GetDict() && |
| 31 | bookmark.GetTitle().CompareNoCase(title.c_str()) == 0) { |
Wei Li | 0e2e5d7 | 2016-03-03 11:28:06 -0800 | [diff] [blame] | 32 | // First check this item. |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 33 | return bookmark; |
| 34 | } |
Wei Li | 0e2e5d7 | 2016-03-03 11:28:06 -0800 | [diff] [blame] | 35 | |
| 36 | // Go into children items. |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 37 | CPDF_Bookmark child = tree.GetFirstChild(bookmark); |
Wei Li | 0e2e5d7 | 2016-03-03 11:28:06 -0800 | [diff] [blame] | 38 | while (child.GetDict() && !pdfium::ContainsKey(*visited, child.GetDict())) { |
| 39 | // Check this item and its children. |
| 40 | CPDF_Bookmark found = FindBookmark(tree, child, title, visited); |
Wei Li | 0fc6b25 | 2016-03-01 16:29:41 -0800 | [diff] [blame] | 41 | if (found.GetDict()) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 42 | return found; |
| 43 | child = tree.GetNextSibling(child); |
| 44 | } |
| 45 | return CPDF_Bookmark(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Lei Zhang | bdf72c3 | 2015-08-14 19:24:08 -0700 | [diff] [blame] | 48 | CPDF_LinkList* GetLinkList(CPDF_Page* page) { |
| 49 | if (!page) |
| 50 | return nullptr; |
| 51 | |
Lei Zhang | bdf72c3 | 2015-08-14 19:24:08 -0700 | [diff] [blame] | 52 | CPDF_Document* pDoc = page->m_pDocument; |
tsepez | 5ce0968 | 2016-05-25 16:16:32 -0700 | [diff] [blame] | 53 | std::unique_ptr<CPDF_LinkList>* pHolder = pDoc->LinksContext(); |
| 54 | if (!pHolder->get()) |
| 55 | pHolder->reset(new CPDF_LinkList); |
| 56 | return pHolder->get(); |
Lei Zhang | bdf72c3 | 2015-08-14 19:24:08 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | } // namespace |
| 60 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 61 | DLLEXPORT FPDF_BOOKMARK STDCALL |
| 62 | FPDFBookmark_GetFirstChild(FPDF_DOCUMENT document, FPDF_BOOKMARK pDict) { |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 63 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
| 64 | if (!pDoc) |
| 65 | return nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 66 | CPDF_BookmarkTree tree(pDoc); |
Dan Sinclair | f1251c1 | 2015-10-20 16:24:45 -0400 | [diff] [blame] | 67 | CPDF_Bookmark bookmark = |
| 68 | CPDF_Bookmark(ToDictionary(static_cast<CPDF_Object*>(pDict))); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 69 | return tree.GetFirstChild(bookmark).GetDict(); |
Bo Xu | 4d62b6b | 2015-01-10 22:52:59 -0800 | [diff] [blame] | 70 | } |
| 71 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 72 | DLLEXPORT FPDF_BOOKMARK STDCALL |
| 73 | FPDFBookmark_GetNextSibling(FPDF_DOCUMENT document, FPDF_BOOKMARK pDict) { |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 74 | if (!pDict) |
| 75 | return nullptr; |
| 76 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
| 77 | if (!pDoc) |
| 78 | return nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 79 | CPDF_BookmarkTree tree(pDoc); |
Dan Sinclair | f1251c1 | 2015-10-20 16:24:45 -0400 | [diff] [blame] | 80 | CPDF_Bookmark bookmark = |
| 81 | CPDF_Bookmark(ToDictionary(static_cast<CPDF_Object*>(pDict))); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 82 | return tree.GetNextSibling(bookmark).GetDict(); |
Bo Xu | 4d62b6b | 2015-01-10 22:52:59 -0800 | [diff] [blame] | 83 | } |
| 84 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 85 | DLLEXPORT unsigned long STDCALL FPDFBookmark_GetTitle(FPDF_BOOKMARK pDict, |
| 86 | void* buffer, |
| 87 | unsigned long buflen) { |
| 88 | if (!pDict) |
| 89 | return 0; |
Dan Sinclair | f1251c1 | 2015-10-20 16:24:45 -0400 | [diff] [blame] | 90 | CPDF_Bookmark bookmark(ToDictionary(static_cast<CPDF_Object*>(pDict))); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 91 | CFX_WideString title = bookmark.GetTitle(); |
| 92 | CFX_ByteString encodedTitle = title.UTF16LE_Encode(); |
| 93 | unsigned long len = encodedTitle.GetLength(); |
| 94 | if (buffer && buflen >= len) { |
| 95 | FXSYS_memcpy(buffer, encodedTitle.c_str(), len); |
| 96 | } |
| 97 | return len; |
Bo Xu | 4d62b6b | 2015-01-10 22:52:59 -0800 | [diff] [blame] | 98 | } |
| 99 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 100 | DLLEXPORT FPDF_BOOKMARK STDCALL FPDFBookmark_Find(FPDF_DOCUMENT document, |
| 101 | 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); |
| 108 | FX_STRSIZE len = CFX_WideString::WStringLength(title); |
| 109 | CFX_WideString encodedTitle = CFX_WideString::FromUTF16LE(title, len); |
Wei Li | 0e2e5d7 | 2016-03-03 11:28:06 -0800 | [diff] [blame] | 110 | std::set<CPDF_Dictionary*> visited; |
| 111 | return FindBookmark(tree, CPDF_Bookmark(), encodedTitle, &visited).GetDict(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 114 | DLLEXPORT FPDF_DEST STDCALL FPDFBookmark_GetDest(FPDF_DOCUMENT document, |
| 115 | FPDF_BOOKMARK pDict) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 116 | if (!pDict) |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 117 | return nullptr; |
| 118 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
| 119 | if (!pDoc) |
| 120 | return nullptr; |
Dan Sinclair | f1251c1 | 2015-10-20 16:24:45 -0400 | [diff] [blame] | 121 | CPDF_Bookmark bookmark(ToDictionary(static_cast<CPDF_Object*>(pDict))); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 122 | CPDF_Dest dest = bookmark.GetDest(pDoc); |
Wei Li | 0fc6b25 | 2016-03-01 16:29:41 -0800 | [diff] [blame] | 123 | if (dest.GetObject()) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 124 | return dest.GetObject(); |
| 125 | // If this bookmark is not directly associated with a dest, we try to get |
| 126 | // action |
| 127 | CPDF_Action action = bookmark.GetAction(); |
Wei Li | 0fc6b25 | 2016-03-01 16:29:41 -0800 | [diff] [blame] | 128 | if (!action.GetDict()) |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 129 | return nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 130 | return action.GetDest(pDoc).GetObject(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 133 | DLLEXPORT FPDF_ACTION STDCALL FPDFBookmark_GetAction(FPDF_BOOKMARK pDict) { |
| 134 | if (!pDict) |
thestig | 1cd352e | 2016-06-07 17:53:06 -0700 | [diff] [blame] | 135 | return nullptr; |
Dan Sinclair | f1251c1 | 2015-10-20 16:24:45 -0400 | [diff] [blame] | 136 | CPDF_Bookmark bookmark(ToDictionary(static_cast<CPDF_Object*>(pDict))); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 137 | return bookmark.GetAction().GetDict(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 140 | DLLEXPORT unsigned long STDCALL FPDFAction_GetType(FPDF_ACTION pDict) { |
| 141 | if (!pDict) |
Lei Zhang | e0947b3 | 2015-09-17 14:51:48 -0700 | [diff] [blame] | 142 | return PDFACTION_UNSUPPORTED; |
| 143 | |
Dan Sinclair | f1251c1 | 2015-10-20 16:24:45 -0400 | [diff] [blame] | 144 | CPDF_Action action(ToDictionary(static_cast<CPDF_Object*>(pDict))); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 145 | CPDF_Action::ActionType type = action.GetType(); |
| 146 | switch (type) { |
| 147 | case CPDF_Action::GoTo: |
| 148 | return PDFACTION_GOTO; |
| 149 | case CPDF_Action::GoToR: |
| 150 | return PDFACTION_REMOTEGOTO; |
| 151 | case CPDF_Action::URI: |
| 152 | return PDFACTION_URI; |
| 153 | case CPDF_Action::Launch: |
| 154 | return PDFACTION_LAUNCH; |
| 155 | default: |
| 156 | return PDFACTION_UNSUPPORTED; |
| 157 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 160 | DLLEXPORT FPDF_DEST STDCALL FPDFAction_GetDest(FPDF_DOCUMENT document, |
| 161 | FPDF_ACTION pDict) { |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 162 | if (!pDict) |
Lei Zhang | e0947b3 | 2015-09-17 14:51:48 -0700 | [diff] [blame] | 163 | return nullptr; |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 164 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
| 165 | if (!pDoc) |
| 166 | return nullptr; |
Dan Sinclair | f1251c1 | 2015-10-20 16:24:45 -0400 | [diff] [blame] | 167 | CPDF_Action action(ToDictionary(static_cast<CPDF_Object*>(pDict))); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 168 | return action.GetDest(pDoc).GetObject(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Dan Sinclair | f766ad2 | 2016-03-14 13:51:24 -0400 | [diff] [blame] | 171 | DLLEXPORT unsigned long STDCALL FPDFAction_GetFilePath(FPDF_ACTION pDict, |
| 172 | void* buffer, |
| 173 | unsigned long buflen) { |
Lei Zhang | e0947b3 | 2015-09-17 14:51:48 -0700 | [diff] [blame] | 174 | unsigned long type = FPDFAction_GetType(pDict); |
| 175 | if (type != PDFACTION_REMOTEGOTO && type != PDFACTION_LAUNCH) |
| 176 | return 0; |
| 177 | |
Dan Sinclair | f1251c1 | 2015-10-20 16:24:45 -0400 | [diff] [blame] | 178 | CPDF_Action action(ToDictionary(static_cast<CPDF_Object*>(pDict))); |
Lei Zhang | e0947b3 | 2015-09-17 14:51:48 -0700 | [diff] [blame] | 179 | CFX_ByteString path = action.GetFilePath().UTF8Encode(); |
| 180 | unsigned long len = path.GetLength() + 1; |
| 181 | if (buffer && buflen >= len) |
| 182 | FXSYS_memcpy(buffer, path.c_str(), len); |
| 183 | return len; |
| 184 | } |
| 185 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 186 | DLLEXPORT unsigned long STDCALL FPDFAction_GetURIPath(FPDF_DOCUMENT document, |
| 187 | FPDF_ACTION pDict, |
| 188 | void* buffer, |
| 189 | unsigned long buflen) { |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 190 | if (!pDict) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 191 | return 0; |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 192 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
| 193 | if (!pDoc) |
| 194 | return 0; |
Dan Sinclair | f1251c1 | 2015-10-20 16:24:45 -0400 | [diff] [blame] | 195 | CPDF_Action action(ToDictionary(static_cast<CPDF_Object*>(pDict))); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 196 | CFX_ByteString path = action.GetURI(pDoc); |
| 197 | unsigned long len = path.GetLength() + 1; |
Lei Zhang | e0947b3 | 2015-09-17 14:51:48 -0700 | [diff] [blame] | 198 | if (buffer && buflen >= len) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 199 | FXSYS_memcpy(buffer, path.c_str(), len); |
| 200 | return len; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 201 | } |
| 202 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 203 | DLLEXPORT unsigned long STDCALL FPDFDest_GetPageIndex(FPDF_DOCUMENT document, |
| 204 | FPDF_DEST pDict) { |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 205 | if (!pDict) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 206 | return 0; |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 207 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
| 208 | if (!pDoc) |
| 209 | return 0; |
Dan Sinclair | 2b11dc1 | 2015-10-22 15:02:06 -0400 | [diff] [blame] | 210 | CPDF_Dest dest(static_cast<CPDF_Array*>(pDict)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 211 | return dest.GetPageIndex(pDoc); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Dan Sinclair | f766ad2 | 2016-03-14 13:51:24 -0400 | [diff] [blame] | 214 | DLLEXPORT FPDF_LINK STDCALL FPDFLink_GetLinkAtPoint(FPDF_PAGE page, |
| 215 | double x, |
| 216 | double y) { |
Tom Sepez | db0be96 | 2015-10-16 14:00:21 -0700 | [diff] [blame] | 217 | CPDF_Page* pPage = CPDFPageFromFPDFPage(page); |
Lei Zhang | bdf72c3 | 2015-08-14 19:24:08 -0700 | [diff] [blame] | 218 | if (!pPage) |
| 219 | return nullptr; |
| 220 | |
| 221 | CPDF_LinkList* pLinkList = GetLinkList(pPage); |
| 222 | if (!pLinkList) |
| 223 | return nullptr; |
| 224 | |
| 225 | return pLinkList->GetLinkAtPoint(pPage, (FX_FLOAT)x, (FX_FLOAT)y, nullptr) |
| 226 | .GetDict(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 227 | } |
| 228 | |
Dan Sinclair | f766ad2 | 2016-03-14 13:51:24 -0400 | [diff] [blame] | 229 | DLLEXPORT int STDCALL FPDFLink_GetLinkZOrderAtPoint(FPDF_PAGE page, |
| 230 | double x, |
| 231 | double y) { |
Tom Sepez | db0be96 | 2015-10-16 14:00:21 -0700 | [diff] [blame] | 232 | CPDF_Page* pPage = CPDFPageFromFPDFPage(page); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 233 | if (!pPage) |
Lei Zhang | bdf72c3 | 2015-08-14 19:24:08 -0700 | [diff] [blame] | 234 | return -1; |
| 235 | |
| 236 | CPDF_LinkList* pLinkList = GetLinkList(pPage); |
| 237 | if (!pLinkList) |
| 238 | return -1; |
| 239 | |
| 240 | int z_order = -1; |
| 241 | pLinkList->GetLinkAtPoint(pPage, (FX_FLOAT)x, (FX_FLOAT)y, &z_order); |
| 242 | return z_order; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 243 | } |
| 244 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 245 | DLLEXPORT FPDF_DEST STDCALL FPDFLink_GetDest(FPDF_DOCUMENT document, |
| 246 | FPDF_LINK pDict) { |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 247 | if (!pDict) |
Lei Zhang | e0947b3 | 2015-09-17 14:51:48 -0700 | [diff] [blame] | 248 | return nullptr; |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 249 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
| 250 | if (!pDoc) |
| 251 | return nullptr; |
Dan Sinclair | f1251c1 | 2015-10-20 16:24:45 -0400 | [diff] [blame] | 252 | CPDF_Link link(ToDictionary(static_cast<CPDF_Object*>(pDict))); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 253 | FPDF_DEST dest = link.GetDest(pDoc).GetObject(); |
| 254 | if (dest) |
| 255 | return dest; |
| 256 | // If this link is not directly associated with a dest, we try to get action |
| 257 | CPDF_Action action = link.GetAction(); |
Wei Li | 0fc6b25 | 2016-03-01 16:29:41 -0800 | [diff] [blame] | 258 | if (!action.GetDict()) |
Lei Zhang | e0947b3 | 2015-09-17 14:51:48 -0700 | [diff] [blame] | 259 | return nullptr; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 260 | return action.GetDest(pDoc).GetObject(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 263 | DLLEXPORT FPDF_ACTION STDCALL FPDFLink_GetAction(FPDF_LINK pDict) { |
| 264 | if (!pDict) |
Lei Zhang | e0947b3 | 2015-09-17 14:51:48 -0700 | [diff] [blame] | 265 | return nullptr; |
| 266 | |
Dan Sinclair | f1251c1 | 2015-10-20 16:24:45 -0400 | [diff] [blame] | 267 | CPDF_Link link(ToDictionary(static_cast<CPDF_Object*>(pDict))); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 268 | return link.GetAction().GetDict(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 269 | } |
| 270 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 271 | DLLEXPORT FPDF_BOOL STDCALL FPDFLink_Enumerate(FPDF_PAGE page, |
| 272 | int* startPos, |
| 273 | FPDF_LINK* linkAnnot) { |
Tom Sepez | db0be96 | 2015-10-16 14:00:21 -0700 | [diff] [blame] | 274 | if (!startPos || !linkAnnot) |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 275 | return FALSE; |
Tom Sepez | db0be96 | 2015-10-16 14:00:21 -0700 | [diff] [blame] | 276 | CPDF_Page* pPage = CPDFPageFromFPDFPage(page); |
| 277 | if (!pPage || !pPage->m_pFormDict) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 278 | return FALSE; |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 279 | CPDF_Array* pAnnots = pPage->m_pFormDict->GetArrayFor("Annots"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 280 | if (!pAnnots) |
| 281 | return FALSE; |
Wei Li | e1aebd4 | 2016-04-11 10:02:09 -0700 | [diff] [blame] | 282 | for (size_t i = *startPos; i < pAnnots->GetCount(); i++) { |
Dan Sinclair | f1251c1 | 2015-10-20 16:24:45 -0400 | [diff] [blame] | 283 | CPDF_Dictionary* pDict = |
tsepez | bd56755 | 2016-03-29 14:51:50 -0700 | [diff] [blame] | 284 | ToDictionary(static_cast<CPDF_Object*>(pAnnots->GetDirectObjectAt(i))); |
Dan Sinclair | f1251c1 | 2015-10-20 16:24:45 -0400 | [diff] [blame] | 285 | if (!pDict) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 286 | continue; |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 287 | if (pDict->GetStringFor("Subtype") == "Link") { |
Wei Li | e1aebd4 | 2016-04-11 10:02:09 -0700 | [diff] [blame] | 288 | *startPos = static_cast<int>(i + 1); |
| 289 | *linkAnnot = static_cast<FPDF_LINK>(pDict); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 290 | return TRUE; |
| 291 | } |
| 292 | } |
| 293 | return FALSE; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 294 | } |
| 295 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 296 | DLLEXPORT FPDF_BOOL STDCALL FPDFLink_GetAnnotRect(FPDF_LINK linkAnnot, |
| 297 | FS_RECTF* rect) { |
| 298 | if (!linkAnnot || !rect) |
| 299 | return FALSE; |
Dan Sinclair | f1251c1 | 2015-10-20 16:24:45 -0400 | [diff] [blame] | 300 | CPDF_Dictionary* pAnnotDict = |
| 301 | ToDictionary(static_cast<CPDF_Object*>(linkAnnot)); |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 302 | CFX_FloatRect rt = pAnnotDict->GetRectFor("Rect"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 303 | rect->left = rt.left; |
| 304 | rect->bottom = rt.bottom; |
| 305 | rect->right = rt.right; |
| 306 | rect->top = rt.top; |
| 307 | return TRUE; |
| 308 | } |
| 309 | |
| 310 | DLLEXPORT int STDCALL FPDFLink_CountQuadPoints(FPDF_LINK linkAnnot) { |
| 311 | if (!linkAnnot) |
| 312 | return 0; |
Dan Sinclair | f1251c1 | 2015-10-20 16:24:45 -0400 | [diff] [blame] | 313 | CPDF_Dictionary* pAnnotDict = |
| 314 | ToDictionary(static_cast<CPDF_Object*>(linkAnnot)); |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 315 | CPDF_Array* pArray = pAnnotDict->GetArrayFor("QuadPoints"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 316 | if (!pArray) |
| 317 | return 0; |
Wei Li | e1aebd4 | 2016-04-11 10:02:09 -0700 | [diff] [blame] | 318 | return static_cast<int>(pArray->GetCount() / 8); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | DLLEXPORT FPDF_BOOL STDCALL FPDFLink_GetQuadPoints(FPDF_LINK linkAnnot, |
| 322 | int quadIndex, |
| 323 | FS_QUADPOINTSF* quadPoints) { |
| 324 | if (!linkAnnot || !quadPoints) |
| 325 | return FALSE; |
Dan Sinclair | f1251c1 | 2015-10-20 16:24:45 -0400 | [diff] [blame] | 326 | CPDF_Dictionary* pAnnotDict = |
| 327 | ToDictionary(static_cast<CPDF_Object*>(linkAnnot)); |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 328 | CPDF_Array* pArray = pAnnotDict->GetArrayFor("QuadPoints"); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 329 | if (pArray) { |
Wei Li | e1aebd4 | 2016-04-11 10:02:09 -0700 | [diff] [blame] | 330 | if (quadIndex < 0 || |
| 331 | static_cast<size_t>(quadIndex) >= pArray->GetCount() / 8 || |
| 332 | (static_cast<size_t>(quadIndex * 8 + 7) >= pArray->GetCount())) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 333 | return FALSE; |
Wei Li | 9b76113 | 2016-01-29 15:44:20 -0800 | [diff] [blame] | 334 | quadPoints->x1 = pArray->GetNumberAt(quadIndex * 8); |
| 335 | quadPoints->y1 = pArray->GetNumberAt(quadIndex * 8 + 1); |
| 336 | quadPoints->x2 = pArray->GetNumberAt(quadIndex * 8 + 2); |
| 337 | quadPoints->y2 = pArray->GetNumberAt(quadIndex * 8 + 3); |
| 338 | quadPoints->x3 = pArray->GetNumberAt(quadIndex * 8 + 4); |
| 339 | quadPoints->y3 = pArray->GetNumberAt(quadIndex * 8 + 5); |
| 340 | quadPoints->x4 = pArray->GetNumberAt(quadIndex * 8 + 6); |
| 341 | quadPoints->y4 = pArray->GetNumberAt(quadIndex * 8 + 7); |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 342 | return TRUE; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 343 | } |
| 344 | return FALSE; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 345 | } |
| 346 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 347 | DLLEXPORT unsigned long STDCALL FPDF_GetMetaText(FPDF_DOCUMENT doc, |
| 348 | FPDF_BYTESTRING tag, |
| 349 | void* buffer, |
| 350 | unsigned long buflen) { |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 351 | if (!tag) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 352 | return 0; |
Tom Sepez | 471a103 | 2015-10-15 16:17:18 -0700 | [diff] [blame] | 353 | CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(doc); |
| 354 | if (!pDoc) |
| 355 | return 0; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 356 | CPDF_Dictionary* pInfo = pDoc->GetInfo(); |
| 357 | if (!pInfo) |
| 358 | return 0; |
dsinclair | 38fd844 | 2016-09-15 10:15:32 -0700 | [diff] [blame] | 359 | CFX_WideString text = pInfo->GetUnicodeTextFor(tag); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 360 | // Use UTF-16LE encoding |
| 361 | CFX_ByteString encodedText = text.UTF16LE_Encode(); |
| 362 | unsigned long len = encodedText.GetLength(); |
| 363 | if (buffer && buflen >= len) { |
| 364 | FXSYS_memcpy(buffer, encodedText.c_str(), len); |
| 365 | } |
| 366 | return len; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 367 | } |