blob: 0c8e26ceacd85f034645c8037fd93f61cb6090cd [file] [log] [blame]
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001// 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 Zhanga6d9f0e2015-06-13 00:48:38 -07004
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07005// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
Lei Zhangb4e7f302015-11-06 15:52:32 -08007#include "public/fpdf_doc.h"
8
Dan Sinclair85c8e7f2016-11-21 13:50:32 -05009#include <memory>
Wei Li0e2e5d72016-03-03 11:28:06 -080010#include <set>
11
dsinclair41872fa2016-10-04 11:29:35 -070012#include "core/fpdfapi/page/cpdf_page.h"
dsinclair488b7ad2016-10-04 11:55:50 -070013#include "core/fpdfapi/parser/cpdf_array.h"
14#include "core/fpdfapi/parser/cpdf_document.h"
dsinclair1727aee2016-09-29 13:12:56 -070015#include "core/fpdfdoc/cpdf_bookmark.h"
16#include "core/fpdfdoc/cpdf_bookmarktree.h"
dsinclairc59fa882016-11-08 06:55:40 -080017#include "core/fpdfdoc/cpdf_dest.h"
thestig733e0682016-11-23 05:52:39 -080018#include "core/fpdfdoc/cpdf_pagelabel.h"
dsinclair114e46a2016-09-29 17:18:21 -070019#include "fpdfsdk/fsdk_define.h"
tsepeza9caab92016-12-14 05:57:10 -080020#include "third_party/base/ptr_util.h"
Wei Li0e2e5d72016-03-03 11:28:06 -080021#include "third_party/base/stl_util.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070022
Lei Zhangbdf72c32015-08-14 19:24:08 -070023namespace {
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070024
Lei Zhangbdf72c32015-08-14 19:24:08 -070025CPDF_Bookmark FindBookmark(const CPDF_BookmarkTree& tree,
26 CPDF_Bookmark bookmark,
Wei Li0e2e5d72016-03-03 11:28:06 -080027 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 Li0fc6b252016-03-01 16:29:41 -080034 if (bookmark.GetDict() &&
35 bookmark.GetTitle().CompareNoCase(title.c_str()) == 0) {
Wei Li0e2e5d72016-03-03 11:28:06 -080036 // First check this item.
Nico Weber9d8ec5a2015-08-04 13:00:21 -070037 return bookmark;
38 }
Wei Li0e2e5d72016-03-03 11:28:06 -080039
40 // Go into children items.
Nico Weber9d8ec5a2015-08-04 13:00:21 -070041 CPDF_Bookmark child = tree.GetFirstChild(bookmark);
Wei Li0e2e5d72016-03-03 11:28:06 -080042 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 Li0fc6b252016-03-01 16:29:41 -080045 if (found.GetDict())
Nico Weber9d8ec5a2015-08-04 13:00:21 -070046 return found;
47 child = tree.GetNextSibling(child);
48 }
49 return CPDF_Bookmark();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070050}
51
Lei Zhangbdf72c32015-08-14 19:24:08 -070052CPDF_LinkList* GetLinkList(CPDF_Page* page) {
53 if (!page)
54 return nullptr;
55
Lei Zhangbdf72c32015-08-14 19:24:08 -070056 CPDF_Document* pDoc = page->m_pDocument;
tsepez5ce09682016-05-25 16:16:32 -070057 std::unique_ptr<CPDF_LinkList>* pHolder = pDoc->LinksContext();
58 if (!pHolder->get())
tsepeza9caab92016-12-14 05:57:10 -080059 *pHolder = pdfium::MakeUnique<CPDF_LinkList>();
tsepez5ce09682016-05-25 16:16:32 -070060 return pHolder->get();
Lei Zhangbdf72c32015-08-14 19:24:08 -070061}
62
thestig733e0682016-11-23 05:52:39 -080063unsigned 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();
thestig9067fd62016-11-23 14:10:06 -080068 if (buffer && len <= buflen)
Dan Sinclair1c5d0b42017-04-03 15:05:11 -040069 memcpy(buffer, encodedText.c_str(), len);
thestig733e0682016-11-23 05:52:39 -080070 return len;
71}
72
Lei Zhangbdf72c32015-08-14 19:24:08 -070073} // namespace
74
Nico Weber9d8ec5a2015-08-04 13:00:21 -070075DLLEXPORT FPDF_BOOKMARK STDCALL
76FPDFBookmark_GetFirstChild(FPDF_DOCUMENT document, FPDF_BOOKMARK pDict) {
Tom Sepez471a1032015-10-15 16:17:18 -070077 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
78 if (!pDoc)
79 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070080 CPDF_BookmarkTree tree(pDoc);
Dan Sinclairf1251c12015-10-20 16:24:45 -040081 CPDF_Bookmark bookmark =
82 CPDF_Bookmark(ToDictionary(static_cast<CPDF_Object*>(pDict)));
Nico Weber9d8ec5a2015-08-04 13:00:21 -070083 return tree.GetFirstChild(bookmark).GetDict();
Bo Xu4d62b6b2015-01-10 22:52:59 -080084}
85
Nico Weber9d8ec5a2015-08-04 13:00:21 -070086DLLEXPORT FPDF_BOOKMARK STDCALL
87FPDFBookmark_GetNextSibling(FPDF_DOCUMENT document, FPDF_BOOKMARK pDict) {
Tom Sepez471a1032015-10-15 16:17:18 -070088 if (!pDict)
89 return nullptr;
90 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
91 if (!pDoc)
92 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070093 CPDF_BookmarkTree tree(pDoc);
Dan Sinclairf1251c12015-10-20 16:24:45 -040094 CPDF_Bookmark bookmark =
95 CPDF_Bookmark(ToDictionary(static_cast<CPDF_Object*>(pDict)));
Nico Weber9d8ec5a2015-08-04 13:00:21 -070096 return tree.GetNextSibling(bookmark).GetDict();
Bo Xu4d62b6b2015-01-10 22:52:59 -080097}
98
Nico Weber9d8ec5a2015-08-04 13:00:21 -070099DLLEXPORT unsigned long STDCALL FPDFBookmark_GetTitle(FPDF_BOOKMARK pDict,
100 void* buffer,
101 unsigned long buflen) {
102 if (!pDict)
103 return 0;
Dan Sinclairf1251c12015-10-20 16:24:45 -0400104 CPDF_Bookmark bookmark(ToDictionary(static_cast<CPDF_Object*>(pDict)));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700105 CFX_WideString title = bookmark.GetTitle();
thestig733e0682016-11-23 05:52:39 -0800106 return Utf16EncodeMaybeCopyAndReturnLength(title, buffer, buflen);
Bo Xu4d62b6b2015-01-10 22:52:59 -0800107}
108
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700109DLLEXPORT FPDF_BOOKMARK STDCALL FPDFBookmark_Find(FPDF_DOCUMENT document,
110 FPDF_WIDESTRING title) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700111 if (!title || title[0] == 0)
Tom Sepez471a1032015-10-15 16:17:18 -0700112 return nullptr;
113 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
114 if (!pDoc)
115 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700116 CPDF_BookmarkTree tree(pDoc);
117 FX_STRSIZE len = CFX_WideString::WStringLength(title);
118 CFX_WideString encodedTitle = CFX_WideString::FromUTF16LE(title, len);
Wei Li0e2e5d72016-03-03 11:28:06 -0800119 std::set<CPDF_Dictionary*> visited;
120 return FindBookmark(tree, CPDF_Bookmark(), encodedTitle, &visited).GetDict();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700121}
122
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700123DLLEXPORT FPDF_DEST STDCALL FPDFBookmark_GetDest(FPDF_DOCUMENT document,
124 FPDF_BOOKMARK pDict) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700125 if (!pDict)
Tom Sepez471a1032015-10-15 16:17:18 -0700126 return nullptr;
127 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
128 if (!pDoc)
129 return nullptr;
Dan Sinclairf1251c12015-10-20 16:24:45 -0400130 CPDF_Bookmark bookmark(ToDictionary(static_cast<CPDF_Object*>(pDict)));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700131 CPDF_Dest dest = bookmark.GetDest(pDoc);
Wei Li0fc6b252016-03-01 16:29:41 -0800132 if (dest.GetObject())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700133 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 Li0fc6b252016-03-01 16:29:41 -0800137 if (!action.GetDict())
Tom Sepez471a1032015-10-15 16:17:18 -0700138 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700139 return action.GetDest(pDoc).GetObject();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700140}
141
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700142DLLEXPORT FPDF_ACTION STDCALL FPDFBookmark_GetAction(FPDF_BOOKMARK pDict) {
143 if (!pDict)
thestig1cd352e2016-06-07 17:53:06 -0700144 return nullptr;
Dan Sinclairf1251c12015-10-20 16:24:45 -0400145 CPDF_Bookmark bookmark(ToDictionary(static_cast<CPDF_Object*>(pDict)));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700146 return bookmark.GetAction().GetDict();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700147}
148
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700149DLLEXPORT unsigned long STDCALL FPDFAction_GetType(FPDF_ACTION pDict) {
150 if (!pDict)
Lei Zhange0947b32015-09-17 14:51:48 -0700151 return PDFACTION_UNSUPPORTED;
152
Dan Sinclairf1251c12015-10-20 16:24:45 -0400153 CPDF_Action action(ToDictionary(static_cast<CPDF_Object*>(pDict)));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700154 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-Malek3f3b45c2014-05-23 17:28:10 -0700167}
168
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700169DLLEXPORT FPDF_DEST STDCALL FPDFAction_GetDest(FPDF_DOCUMENT document,
170 FPDF_ACTION pDict) {
Tom Sepez471a1032015-10-15 16:17:18 -0700171 if (!pDict)
Lei Zhange0947b32015-09-17 14:51:48 -0700172 return nullptr;
Tom Sepez471a1032015-10-15 16:17:18 -0700173 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
174 if (!pDoc)
175 return nullptr;
Dan Sinclairf1251c12015-10-20 16:24:45 -0400176 CPDF_Action action(ToDictionary(static_cast<CPDF_Object*>(pDict)));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700177 return action.GetDest(pDoc).GetObject();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700178}
179
Dan Sinclairf766ad22016-03-14 13:51:24 -0400180DLLEXPORT unsigned long STDCALL FPDFAction_GetFilePath(FPDF_ACTION pDict,
181 void* buffer,
182 unsigned long buflen) {
Lei Zhange0947b32015-09-17 14:51:48 -0700183 unsigned long type = FPDFAction_GetType(pDict);
184 if (type != PDFACTION_REMOTEGOTO && type != PDFACTION_LAUNCH)
185 return 0;
186
Dan Sinclairf1251c12015-10-20 16:24:45 -0400187 CPDF_Action action(ToDictionary(static_cast<CPDF_Object*>(pDict)));
Lei Zhange0947b32015-09-17 14:51:48 -0700188 CFX_ByteString path = action.GetFilePath().UTF8Encode();
189 unsigned long len = path.GetLength() + 1;
thestig9067fd62016-11-23 14:10:06 -0800190 if (buffer && len <= buflen)
Dan Sinclair1c5d0b42017-04-03 15:05:11 -0400191 memcpy(buffer, path.c_str(), len);
Lei Zhange0947b32015-09-17 14:51:48 -0700192 return len;
193}
194
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700195DLLEXPORT unsigned long STDCALL FPDFAction_GetURIPath(FPDF_DOCUMENT document,
196 FPDF_ACTION pDict,
197 void* buffer,
198 unsigned long buflen) {
Tom Sepez471a1032015-10-15 16:17:18 -0700199 if (!pDict)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700200 return 0;
Tom Sepez471a1032015-10-15 16:17:18 -0700201 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
202 if (!pDoc)
203 return 0;
Dan Sinclairf1251c12015-10-20 16:24:45 -0400204 CPDF_Action action(ToDictionary(static_cast<CPDF_Object*>(pDict)));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700205 CFX_ByteString path = action.GetURI(pDoc);
206 unsigned long len = path.GetLength() + 1;
thestig9067fd62016-11-23 14:10:06 -0800207 if (buffer && len <= buflen)
Dan Sinclair1c5d0b42017-04-03 15:05:11 -0400208 memcpy(buffer, path.c_str(), len);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700209 return len;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700210}
211
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700212DLLEXPORT unsigned long STDCALL FPDFDest_GetPageIndex(FPDF_DOCUMENT document,
213 FPDF_DEST pDict) {
Tom Sepez471a1032015-10-15 16:17:18 -0700214 if (!pDict)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700215 return 0;
Tom Sepez471a1032015-10-15 16:17:18 -0700216 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
217 if (!pDoc)
218 return 0;
Dan Sinclair2b11dc12015-10-22 15:02:06 -0400219 CPDF_Dest dest(static_cast<CPDF_Array*>(pDict));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700220 return dest.GetPageIndex(pDoc);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700221}
222
dsinclairc59fa882016-11-08 06:55:40 -0800223DLLEXPORT 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
233 std::unique_ptr<CPDF_Dest> dest(
234 new CPDF_Dest(static_cast<CPDF_Object*>(pDict)));
235
236 // FPDF_BOOL is an int, GetXYZ expects bools.
237 bool bHasX;
238 bool bHasY;
239 bool bHasZoom;
240 if (!dest->GetXYZ(&bHasX, &bHasY, &bHasZoom, x, y, zoom))
241 return false;
242
243 *hasXVal = bHasX;
244 *hasYVal = bHasY;
245 *hasZoomVal = bHasZoom;
246 return true;
247}
248
Dan Sinclairf766ad22016-03-14 13:51:24 -0400249DLLEXPORT FPDF_LINK STDCALL FPDFLink_GetLinkAtPoint(FPDF_PAGE page,
250 double x,
251 double y) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700252 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Lei Zhangbdf72c32015-08-14 19:24:08 -0700253 if (!pPage)
254 return nullptr;
255
256 CPDF_LinkList* pLinkList = GetLinkList(pPage);
257 if (!pLinkList)
258 return nullptr;
259
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500260 return pLinkList
Dan Sinclair05df0752017-03-14 14:43:42 -0400261 ->GetLinkAtPoint(pPage,
262 CFX_PointF(static_cast<float>(x), static_cast<float>(y)),
263 nullptr)
Lei Zhangbdf72c32015-08-14 19:24:08 -0700264 .GetDict();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700265}
266
Dan Sinclairf766ad22016-03-14 13:51:24 -0400267DLLEXPORT int STDCALL FPDFLink_GetLinkZOrderAtPoint(FPDF_PAGE page,
268 double x,
269 double y) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700270 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700271 if (!pPage)
Lei Zhangbdf72c32015-08-14 19:24:08 -0700272 return -1;
273
274 CPDF_LinkList* pLinkList = GetLinkList(pPage);
275 if (!pLinkList)
276 return -1;
277
278 int z_order = -1;
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500279 pLinkList->GetLinkAtPoint(
Dan Sinclair05df0752017-03-14 14:43:42 -0400280 pPage, CFX_PointF(static_cast<float>(x), static_cast<float>(y)),
Dan Sinclairb45ea1f2017-02-21 14:27:59 -0500281 &z_order);
Lei Zhangbdf72c32015-08-14 19:24:08 -0700282 return z_order;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700283}
284
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700285DLLEXPORT FPDF_DEST STDCALL FPDFLink_GetDest(FPDF_DOCUMENT document,
286 FPDF_LINK pDict) {
Tom Sepez471a1032015-10-15 16:17:18 -0700287 if (!pDict)
Lei Zhange0947b32015-09-17 14:51:48 -0700288 return nullptr;
Tom Sepez471a1032015-10-15 16:17:18 -0700289 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
290 if (!pDoc)
291 return nullptr;
Dan Sinclairf1251c12015-10-20 16:24:45 -0400292 CPDF_Link link(ToDictionary(static_cast<CPDF_Object*>(pDict)));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700293 FPDF_DEST dest = link.GetDest(pDoc).GetObject();
294 if (dest)
295 return dest;
296 // If this link is not directly associated with a dest, we try to get action
297 CPDF_Action action = link.GetAction();
Wei Li0fc6b252016-03-01 16:29:41 -0800298 if (!action.GetDict())
Lei Zhange0947b32015-09-17 14:51:48 -0700299 return nullptr;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700300 return action.GetDest(pDoc).GetObject();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700301}
302
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700303DLLEXPORT FPDF_ACTION STDCALL FPDFLink_GetAction(FPDF_LINK pDict) {
304 if (!pDict)
Lei Zhange0947b32015-09-17 14:51:48 -0700305 return nullptr;
306
Dan Sinclairf1251c12015-10-20 16:24:45 -0400307 CPDF_Link link(ToDictionary(static_cast<CPDF_Object*>(pDict)));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700308 return link.GetAction().GetDict();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700309}
310
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700311DLLEXPORT FPDF_BOOL STDCALL FPDFLink_Enumerate(FPDF_PAGE page,
312 int* startPos,
313 FPDF_LINK* linkAnnot) {
Tom Sepezdb0be962015-10-16 14:00:21 -0700314 if (!startPos || !linkAnnot)
tsepez4cf55152016-11-02 14:37:54 -0700315 return false;
Tom Sepezdb0be962015-10-16 14:00:21 -0700316 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
317 if (!pPage || !pPage->m_pFormDict)
tsepez4cf55152016-11-02 14:37:54 -0700318 return false;
dsinclair38fd8442016-09-15 10:15:32 -0700319 CPDF_Array* pAnnots = pPage->m_pFormDict->GetArrayFor("Annots");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700320 if (!pAnnots)
tsepez4cf55152016-11-02 14:37:54 -0700321 return false;
Wei Lie1aebd42016-04-11 10:02:09 -0700322 for (size_t i = *startPos; i < pAnnots->GetCount(); i++) {
Dan Sinclairf1251c12015-10-20 16:24:45 -0400323 CPDF_Dictionary* pDict =
tsepezbd567552016-03-29 14:51:50 -0700324 ToDictionary(static_cast<CPDF_Object*>(pAnnots->GetDirectObjectAt(i)));
Dan Sinclairf1251c12015-10-20 16:24:45 -0400325 if (!pDict)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700326 continue;
dsinclair38fd8442016-09-15 10:15:32 -0700327 if (pDict->GetStringFor("Subtype") == "Link") {
Wei Lie1aebd42016-04-11 10:02:09 -0700328 *startPos = static_cast<int>(i + 1);
329 *linkAnnot = static_cast<FPDF_LINK>(pDict);
tsepez4cf55152016-11-02 14:37:54 -0700330 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700331 }
332 }
tsepez4cf55152016-11-02 14:37:54 -0700333 return false;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700334}
335
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700336DLLEXPORT FPDF_BOOL STDCALL FPDFLink_GetAnnotRect(FPDF_LINK linkAnnot,
337 FS_RECTF* rect) {
338 if (!linkAnnot || !rect)
tsepez4cf55152016-11-02 14:37:54 -0700339 return false;
Dan Sinclairf1251c12015-10-20 16:24:45 -0400340 CPDF_Dictionary* pAnnotDict =
341 ToDictionary(static_cast<CPDF_Object*>(linkAnnot));
dsinclair38fd8442016-09-15 10:15:32 -0700342 CFX_FloatRect rt = pAnnotDict->GetRectFor("Rect");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700343 rect->left = rt.left;
344 rect->bottom = rt.bottom;
345 rect->right = rt.right;
346 rect->top = rt.top;
tsepez4cf55152016-11-02 14:37:54 -0700347 return true;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700348}
349
350DLLEXPORT int STDCALL FPDFLink_CountQuadPoints(FPDF_LINK linkAnnot) {
351 if (!linkAnnot)
352 return 0;
Dan Sinclairf1251c12015-10-20 16:24:45 -0400353 CPDF_Dictionary* pAnnotDict =
354 ToDictionary(static_cast<CPDF_Object*>(linkAnnot));
dsinclair38fd8442016-09-15 10:15:32 -0700355 CPDF_Array* pArray = pAnnotDict->GetArrayFor("QuadPoints");
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700356 if (!pArray)
357 return 0;
Wei Lie1aebd42016-04-11 10:02:09 -0700358 return static_cast<int>(pArray->GetCount() / 8);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700359}
360
361DLLEXPORT FPDF_BOOL STDCALL FPDFLink_GetQuadPoints(FPDF_LINK linkAnnot,
362 int quadIndex,
363 FS_QUADPOINTSF* quadPoints) {
364 if (!linkAnnot || !quadPoints)
tsepez4cf55152016-11-02 14:37:54 -0700365 return false;
Dan Sinclairf1251c12015-10-20 16:24:45 -0400366 CPDF_Dictionary* pAnnotDict =
367 ToDictionary(static_cast<CPDF_Object*>(linkAnnot));
dsinclair38fd8442016-09-15 10:15:32 -0700368 CPDF_Array* pArray = pAnnotDict->GetArrayFor("QuadPoints");
thestig733e0682016-11-23 05:52:39 -0800369 if (!pArray)
370 return false;
371
372 if (quadIndex < 0 ||
373 static_cast<size_t>(quadIndex) >= pArray->GetCount() / 8 ||
374 (static_cast<size_t>(quadIndex * 8 + 7) >= pArray->GetCount())) {
375 return false;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700376 }
thestig733e0682016-11-23 05:52:39 -0800377
378 quadPoints->x1 = pArray->GetNumberAt(quadIndex * 8);
379 quadPoints->y1 = pArray->GetNumberAt(quadIndex * 8 + 1);
380 quadPoints->x2 = pArray->GetNumberAt(quadIndex * 8 + 2);
381 quadPoints->y2 = pArray->GetNumberAt(quadIndex * 8 + 3);
382 quadPoints->x3 = pArray->GetNumberAt(quadIndex * 8 + 4);
383 quadPoints->y3 = pArray->GetNumberAt(quadIndex * 8 + 5);
384 quadPoints->x4 = pArray->GetNumberAt(quadIndex * 8 + 6);
385 quadPoints->y4 = pArray->GetNumberAt(quadIndex * 8 + 7);
386 return true;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700387}
388
thestig733e0682016-11-23 05:52:39 -0800389DLLEXPORT unsigned long STDCALL FPDF_GetMetaText(FPDF_DOCUMENT document,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700390 FPDF_BYTESTRING tag,
391 void* buffer,
392 unsigned long buflen) {
Tom Sepez471a1032015-10-15 16:17:18 -0700393 if (!tag)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700394 return 0;
thestig733e0682016-11-23 05:52:39 -0800395 CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
Tom Sepez471a1032015-10-15 16:17:18 -0700396 if (!pDoc)
397 return 0;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700398 CPDF_Dictionary* pInfo = pDoc->GetInfo();
399 if (!pInfo)
400 return 0;
dsinclair38fd8442016-09-15 10:15:32 -0700401 CFX_WideString text = pInfo->GetUnicodeTextFor(tag);
thestig733e0682016-11-23 05:52:39 -0800402 return Utf16EncodeMaybeCopyAndReturnLength(text, buffer, buflen);
403}
404
dsinclair6bdb56c2016-12-06 09:53:27 -0800405DLLEXPORT unsigned long STDCALL FPDF_GetPageLabel(FPDF_DOCUMENT document,
406 int page_index,
407 void* buffer,
408 unsigned long buflen) {
thestig733e0682016-11-23 05:52:39 -0800409 if (page_index < 0)
410 return 0;
411
412 // CPDF_PageLabel can deal with NULL |document|.
413 CPDF_PageLabel label(CPDFDocumentFromFPDFDocument(document));
414 CFX_WideString str;
415 if (!label.GetLabel(page_index, &str))
416 return 0;
417 return Utf16EncodeMaybeCopyAndReturnLength(str, buffer, buflen);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700418}