XFA: merge patch from CL 834703002, add APIs for getting bookmarks and named destinat...
[pdfium.git] / fpdfsdk / src / fpdfdoc.cpp
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.
4  
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
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"
11
12 static int THISMODULE = 0;
13
14 static CPDF_Bookmark FindBookmark(const CPDF_BookmarkTree& tree, CPDF_Bookmark bookmark, const CFX_WideString& title)
15 {
16         if (bookmark && bookmark.GetTitle().CompareNoCase(title) == 0) {
17                 // First check this item
18                 return bookmark;
19         }
20         // go into children items
21         CPDF_Bookmark child = tree.GetFirstChild(bookmark);
22         while (child) {
23                 // check if this item
24                 CPDF_Bookmark found = FindBookmark(tree, child, title);
25                 if (found)
26                         return found;
27                 child = tree.GetNextSibling(child);
28         }
29         return CPDF_Bookmark();
30 }
31
32 DLLEXPORT FPDF_BOOKMARK STDCALL FPDFBookmark_GetFirstChild(FPDF_DOCUMENT document, FPDF_BOOKMARK pDict)
33 {
34     if (!document || !pDict)
35         return NULL;
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();
40 }
41
42 DLLEXPORT FPDF_BOOKMARK STDCALL FPDFBookmark_GetNextSibling(FPDF_DOCUMENT document, FPDF_BOOKMARK pDict)
43 {
44     if (!document || !pDict)
45         return NULL;
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();
50 }
51
52 DLLEXPORT unsigned long STDCALL FPDFBookmark_GetTitle(FPDF_BOOKMARK pDict, void* buffer, unsigned long buflen)
53 {
54     if (!pDict)
55         return 0;
56     CPDF_Bookmark bookmark((CPDF_Dictionary*)pDict);
57     CFX_WideString title = bookmark.GetTitle();
58     CFX_ByteString encodedTitle = title.UTF16LE_Encode(FALSE);
59     unsigned long len = encodedTitle.GetLength();
60     if (buffer && buflen >= len + 2) {
61         FXSYS_memcpy(buffer, encodedTitle.c_str(), len);
62         ((FX_BYTE*)buffer)[len] = 0;
63         ((FX_BYTE*)buffer)[len + 1] = 0;
64     }
65     return len + 2;
66 }
67
68 DLLEXPORT FPDF_BOOKMARK STDCALL FPDFBookmark_Find(FPDF_DOCUMENT document, FPDF_WIDESTRING title)
69 {
70         if (!document)
71                 return NULL;
72         if (!title || title[0] == 0)
73                 return NULL;
74         CPDF_Document* pDoc = ((CPDFXFA_Document*)document)->GetPDFDoc();
75         CPDF_BookmarkTree tree(pDoc);
76         FX_STRSIZE len = CFX_WideString::WStringLength(title);
77         CFX_WideString encodedTitle = CFX_WideString::FromUTF16LE(title, len);
78         return FindBookmark(tree, CPDF_Bookmark(), encodedTitle).GetDict();
79 }
80
81 DLLEXPORT FPDF_DEST STDCALL FPDFBookmark_GetDest(FPDF_DOCUMENT document, FPDF_BOOKMARK pDict)
82 {
83         if (!document)
84                 return NULL;
85         if (!pDict)
86                 return NULL;
87         CPDF_Bookmark bookmark((CPDF_Dictionary*)pDict);
88         CPDF_Document* pDoc = ((CPDFXFA_Document*)document)->GetPDFDoc();
89         CPDF_Dest dest = bookmark.GetDest(pDoc);
90         if (dest)
91                 return dest;
92         // If this bookmark is not directly associated with a dest, we try to get action
93         CPDF_Action action = bookmark.GetAction();
94         if (!action)
95                 return NULL;
96         return action.GetDest(pDoc);
97 }
98
99 DLLEXPORT FPDF_ACTION STDCALL FPDFBookmark_GetAction(FPDF_BOOKMARK pDict)
100 {
101         if (!pDict)
102                 return NULL;
103         CPDF_Bookmark bookmark((CPDF_Dictionary*)pDict);
104         return bookmark.GetAction();
105 }
106
107 DLLEXPORT unsigned long STDCALL FPDFAction_GetType(FPDF_ACTION pDict)
108 {
109         if (!pDict)
110                 return 0;
111         CPDF_Action action = (CPDF_Dictionary*)pDict;
112         CPDF_Action::ActionType type = action.GetType();
113         switch (type) {
114                 case CPDF_Action::GoTo:
115                         return PDFACTION_GOTO;
116                 case CPDF_Action::GoToR:
117                         return PDFACTION_REMOTEGOTO;
118                 case CPDF_Action::URI:
119                         return PDFACTION_URI;
120                 case CPDF_Action::Launch:
121                         return PDFACTION_LAUNCH;
122                 default:
123                         return PDFACTION_UNSUPPORTED;
124         }
125         return PDFACTION_UNSUPPORTED;
126 }
127
128 DLLEXPORT FPDF_DEST STDCALL FPDFAction_GetDest(FPDF_DOCUMENT document, FPDF_ACTION pDict)
129 {
130         if (!document)
131                 return NULL;
132         if (!pDict)
133                 return NULL;
134         CPDF_Document* pDoc = ((CPDFXFA_Document*)document)->GetPDFDoc();
135         CPDF_Action action = (CPDF_Dictionary*)pDict;
136         return action.GetDest(pDoc);
137 }
138
139 DLLEXPORT unsigned long STDCALL FPDFAction_GetURIPath(FPDF_DOCUMENT document, FPDF_ACTION pDict,
140                                                                                           void* buffer, unsigned long buflen)
141 {
142         if (!document)
143                 return 0;
144         if (!pDict)
145                 return 0;
146         CPDF_Document* pDoc = ((CPDFXFA_Document*)document)->GetPDFDoc();
147         CPDF_Action action = (CPDF_Dictionary*)pDict;
148         CFX_ByteString path = action.GetURI(pDoc);
149         unsigned long len = path.GetLength() + 1;
150         if (buffer != NULL && buflen >= len)
151                 FXSYS_memcpy(buffer, path.c_str(), len);
152         return len;
153 }
154
155 DLLEXPORT unsigned long STDCALL FPDFDest_GetPageIndex(FPDF_DOCUMENT document, FPDF_DEST pDict)
156 {
157         if (!document)
158                 return 0;
159         if (!pDict)
160                 return 0;
161         CPDF_Document* pDoc = ((CPDFXFA_Document*)document)->GetPDFDoc();
162         CPDF_Dest dest = (CPDF_Array*)pDict;
163         return dest.GetPageIndex(pDoc);
164 }
165
166 static void ReleaseLinkList(FX_LPVOID data)
167 {
168         delete (CPDF_LinkList*)data;
169 }
170
171 DLLEXPORT FPDF_LINK STDCALL FPDFLink_GetLinkAtPoint(FPDF_PAGE page, double x, double y)
172 {
173         if (!page)
174                 return NULL;
175     CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
176         if (!pPage)
177                 return NULL;
178         // Link list is stored with the document
179         CPDF_Document* pDoc = pPage->m_pDocument;
180         CPDF_LinkList* pLinkList = (CPDF_LinkList*)pDoc->GetPrivateData(&THISMODULE);
181         if (!pLinkList) {
182                 pLinkList = FX_NEW CPDF_LinkList(pDoc);
183                 pDoc->SetPrivateData(&THISMODULE, pLinkList, ReleaseLinkList);
184         }
185         return pLinkList->GetLinkAtPoint(pPage, (FX_FLOAT)x, (FX_FLOAT)y);
186 }
187
188 DLLEXPORT FPDF_DEST STDCALL FPDFLink_GetDest(FPDF_DOCUMENT document, FPDF_LINK pDict)
189 {
190         if (!document)
191                 return NULL;
192     CPDF_Document* pDoc = ((CPDFXFA_Document*)document)->GetPDFDoc();
193         if (!pDict)
194                 return NULL;
195         CPDF_Link link = (CPDF_Dictionary*)pDict;
196
197         FPDF_DEST dest = link.GetDest(pDoc);
198         if (dest)
199                 return dest;
200         // If this link is not directly associated with a dest, we try to get action
201         CPDF_Action action = link.GetAction();
202         if (!action)
203                 return NULL;
204         return action.GetDest(pDoc);
205 }
206
207 DLLEXPORT FPDF_ACTION STDCALL FPDFLink_GetAction(FPDF_LINK pDict)
208 {
209         if (!pDict)
210                 return NULL;
211         CPDF_Link link = (CPDF_Dictionary*)pDict;
212         return link.GetAction();
213 }
214
215 DLLEXPORT FPDF_BOOL STDCALL FPDFLink_Enumerate(FPDF_PAGE page, int* startPos, FPDF_LINK* linkAnnot)
216 {
217         if(!page || !startPos || !linkAnnot)
218                 return FALSE;
219     CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
220         if(!pPage->m_pFormDict)
221                 return FALSE;
222         CPDF_Array* pAnnots = pPage->m_pFormDict->GetArray("Annots");
223         if(!pAnnots)
224                 return FALSE;
225         for (int i = *startPos; i < (int)pAnnots->GetCount(); i++) {
226                 CPDF_Dictionary* pDict = (CPDF_Dictionary*)pAnnots->GetElementValue(i);
227                 if (!pDict || pDict->GetType() != PDFOBJ_DICTIONARY)
228                         continue;
229                 if(pDict->GetString(FX_BSTRC("Subtype")).Equal(FX_BSTRC("Link"))) {
230                         *startPos = i + 1;
231                         *linkAnnot = (FPDF_LINK)pDict; 
232                         return TRUE;
233                 }
234         }
235         return FALSE;
236 }
237
238 DLLEXPORT FPDF_BOOL STDCALL FPDFLink_GetAnnotRect(FPDF_LINK linkAnnot, FS_RECTF* rect)
239 {
240         if(!linkAnnot || !rect)
241                 return FALSE;
242         CPDF_Dictionary* pAnnotDict = (CPDF_Dictionary*)linkAnnot;
243         CPDF_Rect rt = pAnnotDict->GetRect(FX_BSTRC("Rect"));
244         rect->left = rt.left;
245         rect->bottom = rt.bottom;
246         rect->right = rt.right;
247         rect->top = rt.top;
248         return TRUE;
249 }
250
251 DLLEXPORT int STDCALL FPDFLink_CountQuadPoints(FPDF_LINK linkAnnot)
252 {
253         if(!linkAnnot)
254                 return 0;
255         CPDF_Dictionary* pAnnotDict = (CPDF_Dictionary*)linkAnnot;
256         CPDF_Array* pArray = pAnnotDict->GetArray(FX_BSTRC("QuadPoints"));
257         if (!pArray)
258                 return 0;
259         else
260                 return pArray->GetCount() / 8;
261 }
262
263 DLLEXPORT FPDF_BOOL STDCALL FPDFLink_GetQuadPoints(FPDF_LINK linkAnnot, int quadIndex, FS_QUADPOINTSF* quadPoints)
264 {
265         if(!linkAnnot || !quadPoints)
266                 return FALSE;
267         CPDF_Dictionary* pAnnotDict = (CPDF_Dictionary*)linkAnnot;
268         CPDF_Array* pArray = pAnnotDict->GetArray(FX_BSTRC("QuadPoints"));
269         if (pArray) {
270                 if (quadIndex < 0 || quadIndex >= (int)pArray->GetCount()/8 || ((quadIndex*8+7) >= (int)pArray->GetCount()))
271                         return FALSE;
272                 quadPoints->x1 = pArray->GetNumber(quadIndex*8);
273                 quadPoints->y1 = pArray->GetNumber(quadIndex*8+1);
274                 quadPoints->x2 = pArray->GetNumber(quadIndex*8+2);
275                 quadPoints->y2 = pArray->GetNumber(quadIndex*8+3);
276                 quadPoints->x3 = pArray->GetNumber(quadIndex*8+4);
277                 quadPoints->y3 = pArray->GetNumber(quadIndex*8+5);
278                 quadPoints->x4 = pArray->GetNumber(quadIndex*8+6);
279                 quadPoints->y4 = pArray->GetNumber(quadIndex*8+7);
280                 return TRUE;
281         } 
282         return FALSE;
283 }
284
285 DLLEXPORT unsigned long STDCALL FPDF_GetMetaText(FPDF_DOCUMENT doc, FPDF_BYTESTRING tag,
286                                                                                                  void* buffer, unsigned long buflen)
287 {
288         if (!doc || !tag)
289                 return 0;
290     CPDF_Document* pDoc = ((CPDFXFA_Document*)doc)->GetPDFDoc();
291         // Get info dictionary
292         CPDF_Dictionary* pInfo = pDoc->GetInfo();
293         if (!pInfo)
294                 return 0;
295         CFX_WideString text = pInfo->GetUnicodeText(tag);
296         // Use UTF-16LE encoding
297         CFX_ByteString encodedText = text.UTF16LE_Encode();
298         unsigned long len = encodedText.GetLength();
299         if (buffer && buflen >= len + 2) {
300                 FXSYS_memcpy(buffer, encodedText.c_str(), len);
301                 // use double zero as trailer
302                 ((FX_BYTE*)buffer)[len] = 0;
303                 ((FX_BYTE*)buffer)[len + 1] = 0;
304         }
305         return len+2;
306 }