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"
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.c_str()) == 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);
87 return dest.GetObject();
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).GetObject();
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).GetObject();
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(void* 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 = new CPDF_LinkList(pDoc);
177 pDoc->SetPrivateData(&THISMODULE, pLinkList, ReleaseLinkList);
179 return pLinkList->GetLinkAtPoint(pPage, (FX_FLOAT)x, (FX_FLOAT)y).GetDict();
182 DLLEXPORT FPDF_DEST STDCALL FPDFLink_GetDest(FPDF_DOCUMENT document, FPDF_LINK pDict)
188 CPDF_Document* pDoc = (CPDF_Document*)document;
189 CPDF_Link link((CPDF_Dictionary*)pDict);
190 FPDF_DEST dest = link.GetDest(pDoc).GetObject();
193 // If this link is not directly associated with a dest, we try to get action
194 CPDF_Action action = link.GetAction();
197 return action.GetDest(pDoc).GetObject();
200 DLLEXPORT FPDF_ACTION STDCALL FPDFLink_GetAction(FPDF_LINK pDict)
204 CPDF_Link link((CPDF_Dictionary*)pDict);
205 return link.GetAction().GetDict();
208 DLLEXPORT FPDF_BOOL STDCALL FPDFLink_Enumerate(FPDF_PAGE page, int* startPos, FPDF_LINK* linkAnnot)
210 if(!page || !startPos || !linkAnnot)
212 CPDF_Page* pPage = (CPDF_Page*)page;
213 if(!pPage->m_pFormDict)
215 CPDF_Array* pAnnots = pPage->m_pFormDict->GetArray("Annots");
218 for (int i = *startPos; i < (int)pAnnots->GetCount(); i++) {
219 CPDF_Dictionary* pDict = (CPDF_Dictionary*)pAnnots->GetElementValue(i);
220 if (!pDict || pDict->GetType() != PDFOBJ_DICTIONARY)
222 if(pDict->GetString(FX_BSTRC("Subtype")).Equal(FX_BSTRC("Link"))) {
224 *linkAnnot = (FPDF_LINK)pDict;
231 DLLEXPORT FPDF_BOOL STDCALL FPDFLink_GetAnnotRect(FPDF_LINK linkAnnot, FS_RECTF* rect)
233 if(!linkAnnot || !rect)
235 CPDF_Dictionary* pAnnotDict = (CPDF_Dictionary*)linkAnnot;
236 CPDF_Rect rt = pAnnotDict->GetRect(FX_BSTRC("Rect"));
237 rect->left = rt.left;
238 rect->bottom = rt.bottom;
239 rect->right = rt.right;
244 DLLEXPORT int STDCALL FPDFLink_CountQuadPoints(FPDF_LINK linkAnnot)
248 CPDF_Dictionary* pAnnotDict = (CPDF_Dictionary*)linkAnnot;
249 CPDF_Array* pArray = pAnnotDict->GetArray(FX_BSTRC("QuadPoints"));
252 return pArray->GetCount() / 8;
255 DLLEXPORT FPDF_BOOL STDCALL FPDFLink_GetQuadPoints(FPDF_LINK linkAnnot, int quadIndex, FS_QUADPOINTSF* quadPoints)
257 if(!linkAnnot || !quadPoints)
259 CPDF_Dictionary* pAnnotDict = (CPDF_Dictionary*)linkAnnot;
260 CPDF_Array* pArray = pAnnotDict->GetArray(FX_BSTRC("QuadPoints"));
262 if (quadIndex < 0 || quadIndex >= (int)pArray->GetCount()/8 || ((quadIndex*8+7) >= (int)pArray->GetCount()))
264 quadPoints->x1 = pArray->GetNumber(quadIndex*8);
265 quadPoints->y1 = pArray->GetNumber(quadIndex*8+1);
266 quadPoints->x2 = pArray->GetNumber(quadIndex*8+2);
267 quadPoints->y2 = pArray->GetNumber(quadIndex*8+3);
268 quadPoints->x3 = pArray->GetNumber(quadIndex*8+4);
269 quadPoints->y3 = pArray->GetNumber(quadIndex*8+5);
270 quadPoints->x4 = pArray->GetNumber(quadIndex*8+6);
271 quadPoints->y4 = pArray->GetNumber(quadIndex*8+7);
277 DLLEXPORT unsigned long STDCALL FPDF_GetMetaText(FPDF_DOCUMENT doc, FPDF_BYTESTRING tag,
278 void* buffer, unsigned long buflen)
282 CPDF_Document* pDoc = (CPDF_Document*)doc;
283 // Get info dictionary
284 CPDF_Dictionary* pInfo = pDoc->GetInfo();
287 CFX_WideString text = pInfo->GetUnicodeText(tag);
288 // Use UTF-16LE encoding
289 CFX_ByteString encodedText = text.UTF16LE_Encode();
290 unsigned long len = encodedText.GetLength();
291 if (buffer && buflen >= len) {
292 FXSYS_memcpy(buffer, encodedText.c_str(), len);