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"
9 #include "../include/fpdfxfa/fpdfxfa_doc.h"
10 #include "../include/fpdfxfa/fpdfxfa_page.h"
12 static int THISMODULE = 0;
14 static CPDF_Bookmark FindBookmark(const CPDF_BookmarkTree& tree, CPDF_Bookmark bookmark, const CFX_WideString& title)
16 if (bookmark && bookmark.GetTitle().CompareNoCase(title) == 0) {
17 // First check this item
20 // go into children items
21 CPDF_Bookmark child = tree.GetFirstChild(bookmark);
24 CPDF_Bookmark found = FindBookmark(tree, child, title);
27 child = tree.GetNextSibling(child);
29 return CPDF_Bookmark();
32 DLLEXPORT FPDF_BOOKMARK STDCALL FPDFBookmark_GetFirstChild(FPDF_DOCUMENT document, FPDF_BOOKMARK pDict)
34 if (!document || !pDict)
36 CPDF_Document* pDoc = ((CPDFXFA_Document*)document)->GetPDFDoc();
37 CPDF_BookmarkTree tree(pDoc);
38 CPDF_Bookmark bookmark = CPDF_Bookmark((CPDF_Dictionary*)pDict);
39 return tree.GetFirstChild(bookmark).GetDict();
42 DLLEXPORT FPDF_BOOKMARK STDCALL FPDFBookmark_GetNextSibling(FPDF_DOCUMENT document, FPDF_BOOKMARK pDict)
44 if (!document || !pDict)
46 CPDF_Document* pDoc = ((CPDFXFA_Document*)document)->GetPDFDoc();
47 CPDF_BookmarkTree tree(pDoc);
48 CPDF_Bookmark bookmark = CPDF_Bookmark((CPDF_Dictionary*)pDict);
49 return tree.GetNextSibling(bookmark).GetDict();
52 DLLEXPORT unsigned long STDCALL FPDFBookmark_GetTitle(FPDF_BOOKMARK pDict, void* buffer, unsigned long buflen)
56 CPDF_Bookmark bookmark((CPDF_Dictionary*)pDict);
57 CFX_WideString title = bookmark.GetTitle();
58 CFX_ByteString encodedTitle = title.UTF16LE_Encode();
59 unsigned long len = encodedTitle.GetLength();
60 if (buffer && buflen >= len) {
61 FXSYS_memcpy(buffer, encodedTitle.c_str(), len);
66 DLLEXPORT FPDF_BOOKMARK STDCALL FPDFBookmark_Find(FPDF_DOCUMENT document, FPDF_WIDESTRING title)
70 if (!title || title[0] == 0)
72 CPDF_Document* pDoc = ((CPDFXFA_Document*)document)->GetPDFDoc();
73 CPDF_BookmarkTree tree(pDoc);
74 FX_STRSIZE len = CFX_WideString::WStringLength(title);
75 CFX_WideString encodedTitle = CFX_WideString::FromUTF16LE(title, len);
76 return FindBookmark(tree, CPDF_Bookmark(), encodedTitle).GetDict();
79 DLLEXPORT FPDF_DEST STDCALL FPDFBookmark_GetDest(FPDF_DOCUMENT document, FPDF_BOOKMARK pDict)
85 CPDF_Bookmark bookmark((CPDF_Dictionary*)pDict);
86 CPDF_Document* pDoc = ((CPDFXFA_Document*)document)->GetPDFDoc();
87 CPDF_Dest dest = bookmark.GetDest(pDoc);
90 // If this bookmark is not directly associated with a dest, we try to get action
91 CPDF_Action action = bookmark.GetAction();
94 return action.GetDest(pDoc);
97 DLLEXPORT FPDF_ACTION STDCALL FPDFBookmark_GetAction(FPDF_BOOKMARK pDict)
101 CPDF_Bookmark bookmark((CPDF_Dictionary*)pDict);
102 return bookmark.GetAction();
105 DLLEXPORT unsigned long STDCALL FPDFAction_GetType(FPDF_ACTION pDict)
109 CPDF_Action action = (CPDF_Dictionary*)pDict;
110 CPDF_Action::ActionType type = action.GetType();
112 case CPDF_Action::GoTo:
113 return PDFACTION_GOTO;
114 case CPDF_Action::GoToR:
115 return PDFACTION_REMOTEGOTO;
116 case CPDF_Action::URI:
117 return PDFACTION_URI;
118 case CPDF_Action::Launch:
119 return PDFACTION_LAUNCH;
121 return PDFACTION_UNSUPPORTED;
123 return PDFACTION_UNSUPPORTED;
126 DLLEXPORT FPDF_DEST STDCALL FPDFAction_GetDest(FPDF_DOCUMENT document, FPDF_ACTION pDict)
132 CPDF_Document* pDoc = ((CPDFXFA_Document*)document)->GetPDFDoc();
133 CPDF_Action action = (CPDF_Dictionary*)pDict;
134 return action.GetDest(pDoc);
137 DLLEXPORT unsigned long STDCALL FPDFAction_GetURIPath(FPDF_DOCUMENT document, FPDF_ACTION pDict,
138 void* buffer, unsigned long buflen)
144 CPDF_Document* pDoc = ((CPDFXFA_Document*)document)->GetPDFDoc();
145 CPDF_Action action = (CPDF_Dictionary*)pDict;
146 CFX_ByteString path = action.GetURI(pDoc);
147 unsigned long len = path.GetLength() + 1;
148 if (buffer != NULL && buflen >= len)
149 FXSYS_memcpy(buffer, path.c_str(), len);
153 DLLEXPORT unsigned long STDCALL FPDFDest_GetPageIndex(FPDF_DOCUMENT document, FPDF_DEST pDict)
159 CPDF_Document* pDoc = ((CPDFXFA_Document*)document)->GetPDFDoc();
160 CPDF_Dest dest = (CPDF_Array*)pDict;
161 return dest.GetPageIndex(pDoc);
164 static void ReleaseLinkList(FX_LPVOID data)
166 delete (CPDF_LinkList*)data;
169 DLLEXPORT FPDF_LINK STDCALL FPDFLink_GetLinkAtPoint(FPDF_PAGE page, double x, double y)
173 CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
176 // Link list is stored with the document
177 CPDF_Document* pDoc = pPage->m_pDocument;
178 CPDF_LinkList* pLinkList = (CPDF_LinkList*)pDoc->GetPrivateData(&THISMODULE);
180 pLinkList = FX_NEW CPDF_LinkList(pDoc);
181 pDoc->SetPrivateData(&THISMODULE, pLinkList, ReleaseLinkList);
183 return pLinkList->GetLinkAtPoint(pPage, (FX_FLOAT)x, (FX_FLOAT)y);
186 DLLEXPORT FPDF_DEST STDCALL FPDFLink_GetDest(FPDF_DOCUMENT document, FPDF_LINK pDict)
190 CPDF_Document* pDoc = ((CPDFXFA_Document*)document)->GetPDFDoc();
193 CPDF_Link link = (CPDF_Dictionary*)pDict;
195 FPDF_DEST dest = link.GetDest(pDoc);
198 // If this link is not directly associated with a dest, we try to get action
199 CPDF_Action action = link.GetAction();
202 return action.GetDest(pDoc);
205 DLLEXPORT FPDF_ACTION STDCALL FPDFLink_GetAction(FPDF_LINK pDict)
209 CPDF_Link link = (CPDF_Dictionary*)pDict;
210 return link.GetAction();
213 DLLEXPORT FPDF_BOOL STDCALL FPDFLink_Enumerate(FPDF_PAGE page, int* startPos, FPDF_LINK* linkAnnot)
215 if(!page || !startPos || !linkAnnot)
217 CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
218 if(!pPage->m_pFormDict)
220 CPDF_Array* pAnnots = pPage->m_pFormDict->GetArray("Annots");
223 for (int i = *startPos; i < (int)pAnnots->GetCount(); i++) {
224 CPDF_Dictionary* pDict = (CPDF_Dictionary*)pAnnots->GetElementValue(i);
225 if (!pDict || pDict->GetType() != PDFOBJ_DICTIONARY)
227 if(pDict->GetString(FX_BSTRC("Subtype")).Equal(FX_BSTRC("Link"))) {
229 *linkAnnot = (FPDF_LINK)pDict;
236 DLLEXPORT FPDF_BOOL STDCALL FPDFLink_GetAnnotRect(FPDF_LINK linkAnnot, FS_RECTF* rect)
238 if(!linkAnnot || !rect)
240 CPDF_Dictionary* pAnnotDict = (CPDF_Dictionary*)linkAnnot;
241 CPDF_Rect rt = pAnnotDict->GetRect(FX_BSTRC("Rect"));
242 rect->left = rt.left;
243 rect->bottom = rt.bottom;
244 rect->right = rt.right;
249 DLLEXPORT int STDCALL FPDFLink_CountQuadPoints(FPDF_LINK linkAnnot)
253 CPDF_Dictionary* pAnnotDict = (CPDF_Dictionary*)linkAnnot;
254 CPDF_Array* pArray = pAnnotDict->GetArray(FX_BSTRC("QuadPoints"));
258 return pArray->GetCount() / 8;
261 DLLEXPORT FPDF_BOOL STDCALL FPDFLink_GetQuadPoints(FPDF_LINK linkAnnot, int quadIndex, FS_QUADPOINTSF* quadPoints)
263 if(!linkAnnot || !quadPoints)
265 CPDF_Dictionary* pAnnotDict = (CPDF_Dictionary*)linkAnnot;
266 CPDF_Array* pArray = pAnnotDict->GetArray(FX_BSTRC("QuadPoints"));
268 if (quadIndex < 0 || quadIndex >= (int)pArray->GetCount()/8 || ((quadIndex*8+7) >= (int)pArray->GetCount()))
270 quadPoints->x1 = pArray->GetNumber(quadIndex*8);
271 quadPoints->y1 = pArray->GetNumber(quadIndex*8+1);
272 quadPoints->x2 = pArray->GetNumber(quadIndex*8+2);
273 quadPoints->y2 = pArray->GetNumber(quadIndex*8+3);
274 quadPoints->x3 = pArray->GetNumber(quadIndex*8+4);
275 quadPoints->y3 = pArray->GetNumber(quadIndex*8+5);
276 quadPoints->x4 = pArray->GetNumber(quadIndex*8+6);
277 quadPoints->y4 = pArray->GetNumber(quadIndex*8+7);
283 DLLEXPORT unsigned long STDCALL FPDF_GetMetaText(FPDF_DOCUMENT doc, FPDF_BYTESTRING tag,
284 void* buffer, unsigned long buflen)
288 CPDF_Document* pDoc = ((CPDFXFA_Document*)doc)->GetPDFDoc();
289 // Get info dictionary
290 CPDF_Dictionary* pInfo = pDoc->GetInfo();
293 CFX_WideString text = pInfo->GetUnicodeText(tag);
294 // Use UTF-16LE encoding
295 CFX_ByteString encodedText = text.UTF16LE_Encode();
296 unsigned long len = encodedText.GetLength();
297 if (buffer && buflen >= len) {
298 FXSYS_memcpy(buffer, encodedText.c_str(), len);