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.
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
7 #include "../include/fsdk_define.h"
8 #include "../../public/fpdf_doc.h"
14 CPDF_Bookmark FindBookmark(const CPDF_BookmarkTree& tree,
15 CPDF_Bookmark bookmark,
16 const CFX_WideString& title) {
17 if (bookmark && bookmark.GetTitle().CompareNoCase(title.c_str()) == 0) {
18 // First check this item
21 // go into children items
22 CPDF_Bookmark child = tree.GetFirstChild(bookmark);
25 CPDF_Bookmark found = FindBookmark(tree, child, title);
28 child = tree.GetNextSibling(child);
30 return CPDF_Bookmark();
33 void ReleaseLinkList(void* data) {
34 delete (CPDF_LinkList*)data;
37 CPDF_LinkList* GetLinkList(CPDF_Page* page) {
41 // Link list is stored with the document
42 CPDF_Document* pDoc = page->m_pDocument;
43 CPDF_LinkList* pLinkList = (CPDF_LinkList*)pDoc->GetPrivateData(&THISMODULE);
45 pLinkList = new CPDF_LinkList;
46 pDoc->SetPrivateData(&THISMODULE, pLinkList, ReleaseLinkList);
53 DLLEXPORT FPDF_BOOKMARK STDCALL
54 FPDFBookmark_GetFirstChild(FPDF_DOCUMENT document, FPDF_BOOKMARK pDict) {
57 CPDF_Document* pDoc = (CPDF_Document*)document;
58 CPDF_BookmarkTree tree(pDoc);
59 CPDF_Bookmark bookmark = CPDF_Bookmark((CPDF_Dictionary*)pDict);
60 return tree.GetFirstChild(bookmark).GetDict();
63 DLLEXPORT FPDF_BOOKMARK STDCALL
64 FPDFBookmark_GetNextSibling(FPDF_DOCUMENT document, FPDF_BOOKMARK pDict) {
65 if (!document || !pDict)
67 CPDF_Document* pDoc = (CPDF_Document*)document;
68 CPDF_BookmarkTree tree(pDoc);
69 CPDF_Bookmark bookmark = CPDF_Bookmark((CPDF_Dictionary*)pDict);
70 return tree.GetNextSibling(bookmark).GetDict();
73 DLLEXPORT unsigned long STDCALL FPDFBookmark_GetTitle(FPDF_BOOKMARK pDict,
75 unsigned long buflen) {
78 CPDF_Bookmark bookmark((CPDF_Dictionary*)pDict);
79 CFX_WideString title = bookmark.GetTitle();
80 CFX_ByteString encodedTitle = title.UTF16LE_Encode();
81 unsigned long len = encodedTitle.GetLength();
82 if (buffer && buflen >= len) {
83 FXSYS_memcpy(buffer, encodedTitle.c_str(), len);
88 DLLEXPORT FPDF_BOOKMARK STDCALL FPDFBookmark_Find(FPDF_DOCUMENT document,
89 FPDF_WIDESTRING title) {
92 if (!title || title[0] == 0)
94 CPDF_Document* pDoc = (CPDF_Document*)document;
95 CPDF_BookmarkTree tree(pDoc);
96 FX_STRSIZE len = CFX_WideString::WStringLength(title);
97 CFX_WideString encodedTitle = CFX_WideString::FromUTF16LE(title, len);
98 return FindBookmark(tree, CPDF_Bookmark(), encodedTitle).GetDict();
101 DLLEXPORT FPDF_DEST STDCALL FPDFBookmark_GetDest(FPDF_DOCUMENT document,
102 FPDF_BOOKMARK pDict) {
107 CPDF_Bookmark bookmark((CPDF_Dictionary*)pDict);
108 CPDF_Document* pDoc = (CPDF_Document*)document;
109 CPDF_Dest dest = bookmark.GetDest(pDoc);
111 return dest.GetObject();
112 // If this bookmark is not directly associated with a dest, we try to get
114 CPDF_Action action = bookmark.GetAction();
117 return action.GetDest(pDoc).GetObject();
120 DLLEXPORT FPDF_ACTION STDCALL FPDFBookmark_GetAction(FPDF_BOOKMARK pDict) {
123 CPDF_Bookmark bookmark((CPDF_Dictionary*)pDict);
124 return bookmark.GetAction().GetDict();
127 DLLEXPORT unsigned long STDCALL FPDFAction_GetType(FPDF_ACTION pDict) {
129 return PDFACTION_UNSUPPORTED;
131 CPDF_Action action((CPDF_Dictionary*)pDict);
132 CPDF_Action::ActionType type = action.GetType();
134 case CPDF_Action::GoTo:
135 return PDFACTION_GOTO;
136 case CPDF_Action::GoToR:
137 return PDFACTION_REMOTEGOTO;
138 case CPDF_Action::URI:
139 return PDFACTION_URI;
140 case CPDF_Action::Launch:
141 return PDFACTION_LAUNCH;
143 return PDFACTION_UNSUPPORTED;
147 DLLEXPORT FPDF_DEST STDCALL FPDFAction_GetDest(FPDF_DOCUMENT document,
149 if (!document || !pDict)
152 CPDF_Document* pDoc = (CPDF_Document*)document;
153 CPDF_Action action((CPDF_Dictionary*)pDict);
154 return action.GetDest(pDoc).GetObject();
157 DLLEXPORT unsigned long STDCALL
158 FPDFAction_GetFilePath(FPDF_ACTION pDict, void* buffer, unsigned long buflen) {
159 unsigned long type = FPDFAction_GetType(pDict);
160 if (type != PDFACTION_REMOTEGOTO && type != PDFACTION_LAUNCH)
163 CPDF_Action action((CPDF_Dictionary*)pDict);
164 CFX_ByteString path = action.GetFilePath().UTF8Encode();
165 unsigned long len = path.GetLength() + 1;
166 if (buffer && buflen >= len)
167 FXSYS_memcpy(buffer, path.c_str(), len);
171 DLLEXPORT unsigned long STDCALL FPDFAction_GetURIPath(FPDF_DOCUMENT document,
174 unsigned long buflen) {
175 if (!document || !pDict)
178 CPDF_Document* pDoc = (CPDF_Document*)document;
179 CPDF_Action action((CPDF_Dictionary*)pDict);
180 CFX_ByteString path = action.GetURI(pDoc);
181 unsigned long len = path.GetLength() + 1;
182 if (buffer && buflen >= len)
183 FXSYS_memcpy(buffer, path.c_str(), len);
187 DLLEXPORT unsigned long STDCALL FPDFDest_GetPageIndex(FPDF_DOCUMENT document,
189 if (!document || !pDict)
192 CPDF_Document* pDoc = (CPDF_Document*)document;
193 CPDF_Dest dest((CPDF_Array*)pDict);
194 return dest.GetPageIndex(pDoc);
197 DLLEXPORT FPDF_LINK STDCALL
198 FPDFLink_GetLinkAtPoint(FPDF_PAGE page, double x, double y) {
199 CPDF_Page* pPage = (CPDF_Page*)page;
200 CPDF_LinkList* pLinkList = GetLinkList(pPage);
204 return pLinkList->GetLinkAtPoint(pPage, (FX_FLOAT)x, (FX_FLOAT)y, nullptr)
208 DLLEXPORT int STDCALL
209 FPDFLink_GetLinkZOrderAtPoint(FPDF_PAGE page, double x, double y) {
210 CPDF_Page* pPage = (CPDF_Page*)page;
211 CPDF_LinkList* pLinkList = GetLinkList(pPage);
216 pLinkList->GetLinkAtPoint(pPage, (FX_FLOAT)x, (FX_FLOAT)y, &z_order);
220 DLLEXPORT FPDF_DEST STDCALL FPDFLink_GetDest(FPDF_DOCUMENT document,
222 if (!document || !pDict)
225 CPDF_Document* pDoc = (CPDF_Document*)document;
226 CPDF_Link link((CPDF_Dictionary*)pDict);
227 FPDF_DEST dest = link.GetDest(pDoc).GetObject();
230 // If this link is not directly associated with a dest, we try to get action
231 CPDF_Action action = link.GetAction();
234 return action.GetDest(pDoc).GetObject();
237 DLLEXPORT FPDF_ACTION STDCALL FPDFLink_GetAction(FPDF_LINK pDict) {
241 CPDF_Link link((CPDF_Dictionary*)pDict);
242 return link.GetAction().GetDict();
245 DLLEXPORT FPDF_BOOL STDCALL FPDFLink_Enumerate(FPDF_PAGE page,
247 FPDF_LINK* linkAnnot) {
248 if (!page || !startPos || !linkAnnot)
250 CPDF_Page* pPage = (CPDF_Page*)page;
251 if (!pPage->m_pFormDict)
253 CPDF_Array* pAnnots = pPage->m_pFormDict->GetArray("Annots");
256 for (int i = *startPos; i < (int)pAnnots->GetCount(); i++) {
257 CPDF_Dictionary* pDict = (CPDF_Dictionary*)pAnnots->GetElementValue(i);
258 if (!pDict || pDict->GetType() != PDFOBJ_DICTIONARY)
260 if (pDict->GetString(FX_BSTRC("Subtype")).Equal(FX_BSTRC("Link"))) {
262 *linkAnnot = (FPDF_LINK)pDict;
269 DLLEXPORT FPDF_BOOL STDCALL FPDFLink_GetAnnotRect(FPDF_LINK linkAnnot,
271 if (!linkAnnot || !rect)
273 CPDF_Dictionary* pAnnotDict = (CPDF_Dictionary*)linkAnnot;
274 CPDF_Rect rt = pAnnotDict->GetRect(FX_BSTRC("Rect"));
275 rect->left = rt.left;
276 rect->bottom = rt.bottom;
277 rect->right = rt.right;
282 DLLEXPORT int STDCALL FPDFLink_CountQuadPoints(FPDF_LINK linkAnnot) {
285 CPDF_Dictionary* pAnnotDict = (CPDF_Dictionary*)linkAnnot;
286 CPDF_Array* pArray = pAnnotDict->GetArray(FX_BSTRC("QuadPoints"));
289 return pArray->GetCount() / 8;
292 DLLEXPORT FPDF_BOOL STDCALL FPDFLink_GetQuadPoints(FPDF_LINK linkAnnot,
294 FS_QUADPOINTSF* quadPoints) {
295 if (!linkAnnot || !quadPoints)
297 CPDF_Dictionary* pAnnotDict = (CPDF_Dictionary*)linkAnnot;
298 CPDF_Array* pArray = pAnnotDict->GetArray(FX_BSTRC("QuadPoints"));
300 if (quadIndex < 0 || quadIndex >= (int)pArray->GetCount() / 8 ||
301 ((quadIndex * 8 + 7) >= (int)pArray->GetCount()))
303 quadPoints->x1 = pArray->GetNumber(quadIndex * 8);
304 quadPoints->y1 = pArray->GetNumber(quadIndex * 8 + 1);
305 quadPoints->x2 = pArray->GetNumber(quadIndex * 8 + 2);
306 quadPoints->y2 = pArray->GetNumber(quadIndex * 8 + 3);
307 quadPoints->x3 = pArray->GetNumber(quadIndex * 8 + 4);
308 quadPoints->y3 = pArray->GetNumber(quadIndex * 8 + 5);
309 quadPoints->x4 = pArray->GetNumber(quadIndex * 8 + 6);
310 quadPoints->y4 = pArray->GetNumber(quadIndex * 8 + 7);
316 DLLEXPORT unsigned long STDCALL FPDF_GetMetaText(FPDF_DOCUMENT doc,
319 unsigned long buflen) {
322 CPDF_Document* pDoc = (CPDF_Document*)doc;
323 // Get info dictionary
324 CPDF_Dictionary* pInfo = pDoc->GetInfo();
327 CFX_WideString text = pInfo->GetUnicodeText(tag);
328 // Use UTF-16LE encoding
329 CFX_ByteString encodedText = text.UTF16LE_Encode();
330 unsigned long len = encodedText.GetLength();
331 if (buffer && buflen >= len) {
332 FXSYS_memcpy(buffer, encodedText.c_str(), len);