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 "../include/fpdfdoc.h"
10 static int THISMODULE = 0;
12 static CPDF_Bookmark FindBookmark(const CPDF_BookmarkTree& tree, CPDF_Bookmark bookmark, const CFX_WideString& title)
14 if (bookmark && bookmark.GetTitle().CompareNoCase(title) == 0) {
15 // First check this item
18 // go into children items
19 CPDF_Bookmark child = tree.GetFirstChild(bookmark);
22 CPDF_Bookmark found = FindBookmark(tree, child, title);
25 child = tree.GetNextSibling(child);
27 return CPDF_Bookmark();
30 DLLEXPORT FPDF_BOOKMARK STDCALL FPDFBookmark_GetFirstChild(FPDF_DOCUMENT document, FPDF_BOOKMARK pDict)
34 CPDF_Document* pDoc = (CPDF_Document*)document;
35 CPDF_BookmarkTree tree(pDoc);
36 CPDF_Bookmark bookmark = CPDF_Bookmark((CPDF_Dictionary*)pDict);
37 return tree.GetFirstChild(bookmark).GetDict();
40 DLLEXPORT FPDF_BOOKMARK STDCALL FPDFBookmark_GetNextSibling(FPDF_DOCUMENT document, FPDF_BOOKMARK pDict)
42 if (!document || !pDict)
44 CPDF_Document* pDoc = (CPDF_Document*)document;
45 CPDF_BookmarkTree tree(pDoc);
46 CPDF_Bookmark bookmark = CPDF_Bookmark((CPDF_Dictionary*)pDict);
47 return tree.GetNextSibling(bookmark).GetDict();
50 DLLEXPORT unsigned long STDCALL FPDFBookmark_GetTitle(FPDF_BOOKMARK pDict, void* buffer, unsigned long buflen)
54 CPDF_Bookmark bookmark((CPDF_Dictionary*)pDict);
55 CFX_WideString title = bookmark.GetTitle();
56 CFX_ByteString encodedTitle = title.UTF16LE_Encode();
57 unsigned long len = encodedTitle.GetLength();
58 if (buffer && buflen >= len) {
59 FXSYS_memcpy(buffer, encodedTitle.c_str(), len);
64 DLLEXPORT FPDF_BOOKMARK STDCALL FPDFBookmark_Find(FPDF_DOCUMENT document, FPDF_WIDESTRING title)
68 if (!title || title[0] == 0)
70 CPDF_Document* pDoc = (CPDF_Document*)document;
71 CPDF_BookmarkTree tree(pDoc);
72 FX_STRSIZE len = CFX_WideString::WStringLength(title);
73 CFX_WideString encodedTitle = CFX_WideString::FromUTF16LE(title, len);
74 return FindBookmark(tree, CPDF_Bookmark(), encodedTitle).GetDict();
77 DLLEXPORT FPDF_DEST STDCALL FPDFBookmark_GetDest(FPDF_DOCUMENT document, FPDF_BOOKMARK pDict)
83 CPDF_Bookmark bookmark((CPDF_Dictionary*)pDict);
84 CPDF_Document* pDoc = (CPDF_Document*)document;
85 CPDF_Dest dest = bookmark.GetDest(pDoc);
88 // If this bookmark is not directly associated with a dest, we try to get action
89 CPDF_Action action = bookmark.GetAction();
92 return action.GetDest(pDoc);
95 DLLEXPORT FPDF_ACTION STDCALL FPDFBookmark_GetAction(FPDF_BOOKMARK pDict)
99 CPDF_Bookmark bookmark((CPDF_Dictionary*)pDict);
100 return bookmark.GetAction().GetDict();
103 DLLEXPORT unsigned long STDCALL FPDFAction_GetType(FPDF_ACTION pDict)
107 CPDF_Action action((CPDF_Dictionary*)pDict);
108 CPDF_Action::ActionType type = action.GetType();
110 case CPDF_Action::GoTo:
111 return PDFACTION_GOTO;
112 case CPDF_Action::GoToR:
113 return PDFACTION_REMOTEGOTO;
114 case CPDF_Action::URI:
115 return PDFACTION_URI;
116 case CPDF_Action::Launch:
117 return PDFACTION_LAUNCH;
119 return PDFACTION_UNSUPPORTED;
121 return PDFACTION_UNSUPPORTED;
124 DLLEXPORT FPDF_DEST STDCALL FPDFAction_GetDest(FPDF_DOCUMENT document, FPDF_ACTION pDict)
130 CPDF_Document* pDoc = (CPDF_Document*)document;
131 CPDF_Action action((CPDF_Dictionary*)pDict);
132 return action.GetDest(pDoc);
135 DLLEXPORT unsigned long STDCALL FPDFAction_GetURIPath(FPDF_DOCUMENT document, FPDF_ACTION pDict,
136 void* buffer, unsigned long buflen)
142 CPDF_Document* pDoc = (CPDF_Document*)document;
143 CPDF_Action action((CPDF_Dictionary*)pDict);
144 CFX_ByteString path = action.GetURI(pDoc);
145 unsigned long len = path.GetLength() + 1;
146 if (buffer != NULL && buflen >= len)
147 FXSYS_memcpy(buffer, path.c_str(), len);
151 DLLEXPORT unsigned long STDCALL FPDFDest_GetPageIndex(FPDF_DOCUMENT document, FPDF_DEST pDict)
157 CPDF_Document* pDoc = (CPDF_Document*)document;
158 CPDF_Dest dest = (CPDF_Array*)pDict;
159 return dest.GetPageIndex(pDoc);
162 static void ReleaseLinkList(FX_LPVOID data)
164 delete (CPDF_LinkList*)data;
167 DLLEXPORT FPDF_LINK STDCALL FPDFLink_GetLinkAtPoint(FPDF_PAGE page, double x, double y)
171 CPDF_Page* pPage = (CPDF_Page*)page;
172 // Link list is stored with the document
173 CPDF_Document* pDoc = pPage->m_pDocument;
174 CPDF_LinkList* pLinkList = (CPDF_LinkList*)pDoc->GetPrivateData(&THISMODULE);
176 pLinkList = FX_NEW CPDF_LinkList(pDoc);
177 pDoc->SetPrivateData(&THISMODULE, pLinkList, ReleaseLinkList);
179 return pLinkList->GetLinkAtPoint(pPage, (FX_FLOAT)x, (FX_FLOAT)y);
182 DLLEXPORT FPDF_DEST STDCALL FPDFLink_GetDest(FPDF_DOCUMENT document, FPDF_LINK pDict)
186 CPDF_Document* pDoc = (CPDF_Document*)document;
189 CPDF_Link link = (CPDF_Dictionary*)pDict;
191 FPDF_DEST dest = link.GetDest(pDoc);
194 // If this link is not directly associated with a dest, we try to get action
195 CPDF_Action action = link.GetAction();
198 return action.GetDest(pDoc);
201 DLLEXPORT FPDF_ACTION STDCALL FPDFLink_GetAction(FPDF_LINK pDict)
205 CPDF_Link link = (CPDF_Dictionary*)pDict;
206 return link.GetAction().GetDict();
209 DLLEXPORT FPDF_BOOL STDCALL FPDFLink_Enumerate(FPDF_PAGE page, int* startPos, FPDF_LINK* linkAnnot)
211 if(!page || !startPos || !linkAnnot)
213 CPDF_Page* pPage = (CPDF_Page*)page;
214 if(!pPage->m_pFormDict)
216 CPDF_Array* pAnnots = pPage->m_pFormDict->GetArray("Annots");
219 for (int i = *startPos; i < (int)pAnnots->GetCount(); i++) {
220 CPDF_Dictionary* pDict = (CPDF_Dictionary*)pAnnots->GetElementValue(i);
221 if (!pDict || pDict->GetType() != PDFOBJ_DICTIONARY)
223 if(pDict->GetString(FX_BSTRC("Subtype")).Equal(FX_BSTRC("Link"))) {
225 *linkAnnot = (FPDF_LINK)pDict;
232 DLLEXPORT FPDF_BOOL STDCALL FPDFLink_GetAnnotRect(FPDF_LINK linkAnnot, FS_RECTF* rect)
234 if(!linkAnnot || !rect)
236 CPDF_Dictionary* pAnnotDict = (CPDF_Dictionary*)linkAnnot;
237 CPDF_Rect rt = pAnnotDict->GetRect(FX_BSTRC("Rect"));
238 rect->left = rt.left;
239 rect->bottom = rt.bottom;
240 rect->right = rt.right;
245 DLLEXPORT int STDCALL FPDFLink_CountQuadPoints(FPDF_LINK linkAnnot)
249 CPDF_Dictionary* pAnnotDict = (CPDF_Dictionary*)linkAnnot;
250 CPDF_Array* pArray = pAnnotDict->GetArray(FX_BSTRC("QuadPoints"));
254 return pArray->GetCount() / 8;
257 DLLEXPORT FPDF_BOOL STDCALL FPDFLink_GetQuadPoints(FPDF_LINK linkAnnot, int quadIndex, FS_QUADPOINTSF* quadPoints)
259 if(!linkAnnot || !quadPoints)
261 CPDF_Dictionary* pAnnotDict = (CPDF_Dictionary*)linkAnnot;
262 CPDF_Array* pArray = pAnnotDict->GetArray(FX_BSTRC("QuadPoints"));
264 if (quadIndex < 0 || quadIndex >= (int)pArray->GetCount()/8 || ((quadIndex*8+7) >= (int)pArray->GetCount()))
266 quadPoints->x1 = pArray->GetNumber(quadIndex*8);
267 quadPoints->y1 = pArray->GetNumber(quadIndex*8+1);
268 quadPoints->x2 = pArray->GetNumber(quadIndex*8+2);
269 quadPoints->y2 = pArray->GetNumber(quadIndex*8+3);
270 quadPoints->x3 = pArray->GetNumber(quadIndex*8+4);
271 quadPoints->y3 = pArray->GetNumber(quadIndex*8+5);
272 quadPoints->x4 = pArray->GetNumber(quadIndex*8+6);
273 quadPoints->y4 = pArray->GetNumber(quadIndex*8+7);
279 DLLEXPORT unsigned long STDCALL FPDF_GetMetaText(FPDF_DOCUMENT doc, FPDF_BYTESTRING tag,
280 void* buffer, unsigned long buflen)
284 CPDF_Document* pDoc = (CPDF_Document*)doc;
285 // Get info dictionary
286 CPDF_Dictionary* pInfo = pDoc->GetInfo();
289 CFX_WideString text = pInfo->GetUnicodeText(tag);
290 // Use UTF-16LE encoding
291 CFX_ByteString encodedText = text.UTF16LE_Encode();
292 unsigned long len = encodedText.GetLength();
293 if (buffer && buflen >= len) {
294 FXSYS_memcpy(buffer, encodedText.c_str(), len);