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