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) {
130 CPDF_Action action((CPDF_Dictionary*)pDict);
131 CPDF_Action::ActionType type = action.GetType();
133 case CPDF_Action::GoTo:
134 return PDFACTION_GOTO;
135 case CPDF_Action::GoToR:
136 return PDFACTION_REMOTEGOTO;
137 case CPDF_Action::URI:
138 return PDFACTION_URI;
139 case CPDF_Action::Launch:
140 return PDFACTION_LAUNCH;
142 return PDFACTION_UNSUPPORTED;
144 return PDFACTION_UNSUPPORTED;
147 DLLEXPORT FPDF_DEST STDCALL FPDFAction_GetDest(FPDF_DOCUMENT document,
153 CPDF_Document* pDoc = (CPDF_Document*)document;
154 CPDF_Action action((CPDF_Dictionary*)pDict);
155 return action.GetDest(pDoc).GetObject();
158 DLLEXPORT unsigned long STDCALL FPDFAction_GetURIPath(FPDF_DOCUMENT document,
161 unsigned long buflen) {
166 CPDF_Document* pDoc = (CPDF_Document*)document;
167 CPDF_Action action((CPDF_Dictionary*)pDict);
168 CFX_ByteString path = action.GetURI(pDoc);
169 unsigned long len = path.GetLength() + 1;
170 if (buffer != NULL && buflen >= len)
171 FXSYS_memcpy(buffer, path.c_str(), len);
175 DLLEXPORT unsigned long STDCALL FPDFDest_GetPageIndex(FPDF_DOCUMENT document,
181 CPDF_Document* pDoc = (CPDF_Document*)document;
182 CPDF_Dest dest((CPDF_Array*)pDict);
183 return dest.GetPageIndex(pDoc);
186 DLLEXPORT FPDF_LINK STDCALL
187 FPDFLink_GetLinkAtPoint(FPDF_PAGE page, double x, double y) {
188 CPDF_Page* pPage = (CPDF_Page*)page;
189 CPDF_LinkList* pLinkList = GetLinkList(pPage);
193 return pLinkList->GetLinkAtPoint(pPage, (FX_FLOAT)x, (FX_FLOAT)y, nullptr)
197 DLLEXPORT int STDCALL
198 FPDFLink_GetLinkZOrderAtPoint(FPDF_PAGE page, double x, double y) {
199 CPDF_Page* pPage = (CPDF_Page*)page;
200 CPDF_LinkList* pLinkList = GetLinkList(pPage);
205 pLinkList->GetLinkAtPoint(pPage, (FX_FLOAT)x, (FX_FLOAT)y, &z_order);
209 DLLEXPORT FPDF_DEST STDCALL FPDFLink_GetDest(FPDF_DOCUMENT document,
215 CPDF_Document* pDoc = (CPDF_Document*)document;
216 CPDF_Link link((CPDF_Dictionary*)pDict);
217 FPDF_DEST dest = link.GetDest(pDoc).GetObject();
220 // If this link is not directly associated with a dest, we try to get action
221 CPDF_Action action = link.GetAction();
224 return action.GetDest(pDoc).GetObject();
227 DLLEXPORT FPDF_ACTION STDCALL FPDFLink_GetAction(FPDF_LINK pDict) {
230 CPDF_Link link((CPDF_Dictionary*)pDict);
231 return link.GetAction().GetDict();
234 DLLEXPORT FPDF_BOOL STDCALL FPDFLink_Enumerate(FPDF_PAGE page,
236 FPDF_LINK* linkAnnot) {
237 if (!page || !startPos || !linkAnnot)
239 CPDF_Page* pPage = (CPDF_Page*)page;
240 if (!pPage->m_pFormDict)
242 CPDF_Array* pAnnots = pPage->m_pFormDict->GetArray("Annots");
245 for (int i = *startPos; i < (int)pAnnots->GetCount(); i++) {
246 CPDF_Dictionary* pDict = (CPDF_Dictionary*)pAnnots->GetElementValue(i);
247 if (!pDict || pDict->GetType() != PDFOBJ_DICTIONARY)
249 if (pDict->GetString(FX_BSTRC("Subtype")).Equal(FX_BSTRC("Link"))) {
251 *linkAnnot = (FPDF_LINK)pDict;
258 DLLEXPORT FPDF_BOOL STDCALL FPDFLink_GetAnnotRect(FPDF_LINK linkAnnot,
260 if (!linkAnnot || !rect)
262 CPDF_Dictionary* pAnnotDict = (CPDF_Dictionary*)linkAnnot;
263 CPDF_Rect rt = pAnnotDict->GetRect(FX_BSTRC("Rect"));
264 rect->left = rt.left;
265 rect->bottom = rt.bottom;
266 rect->right = rt.right;
271 DLLEXPORT int STDCALL FPDFLink_CountQuadPoints(FPDF_LINK linkAnnot) {
274 CPDF_Dictionary* pAnnotDict = (CPDF_Dictionary*)linkAnnot;
275 CPDF_Array* pArray = pAnnotDict->GetArray(FX_BSTRC("QuadPoints"));
278 return pArray->GetCount() / 8;
281 DLLEXPORT FPDF_BOOL STDCALL FPDFLink_GetQuadPoints(FPDF_LINK linkAnnot,
283 FS_QUADPOINTSF* quadPoints) {
284 if (!linkAnnot || !quadPoints)
286 CPDF_Dictionary* pAnnotDict = (CPDF_Dictionary*)linkAnnot;
287 CPDF_Array* pArray = pAnnotDict->GetArray(FX_BSTRC("QuadPoints"));
289 if (quadIndex < 0 || quadIndex >= (int)pArray->GetCount() / 8 ||
290 ((quadIndex * 8 + 7) >= (int)pArray->GetCount()))
292 quadPoints->x1 = pArray->GetNumber(quadIndex * 8);
293 quadPoints->y1 = pArray->GetNumber(quadIndex * 8 + 1);
294 quadPoints->x2 = pArray->GetNumber(quadIndex * 8 + 2);
295 quadPoints->y2 = pArray->GetNumber(quadIndex * 8 + 3);
296 quadPoints->x3 = pArray->GetNumber(quadIndex * 8 + 4);
297 quadPoints->y3 = pArray->GetNumber(quadIndex * 8 + 5);
298 quadPoints->x4 = pArray->GetNumber(quadIndex * 8 + 6);
299 quadPoints->y4 = pArray->GetNumber(quadIndex * 8 + 7);
305 DLLEXPORT unsigned long STDCALL FPDF_GetMetaText(FPDF_DOCUMENT doc,
308 unsigned long buflen) {
311 CPDF_Document* pDoc = (CPDF_Document*)doc;
312 // Get info dictionary
313 CPDF_Dictionary* pInfo = pDoc->GetInfo();
316 CFX_WideString text = pInfo->GetUnicodeText(tag);
317 // Use UTF-16LE encoding
318 CFX_ByteString encodedText = text.UTF16LE_Encode();
319 unsigned long len = encodedText.GetLength();
320 if (buffer && buflen >= len) {
321 FXSYS_memcpy(buffer, encodedText.c_str(), len);