Clean up bookmark related codes.
[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_Find(FPDF_DOCUMENT document, FPDF_WIDESTRING title)
31 {
32         if (!document)
33                 return NULL;
34         if (!title || title[0] == 0)
35                 return NULL;
36         CPDF_Document* pDoc = (CPDF_Document*)document;
37         CPDF_BookmarkTree tree(pDoc);
38         FX_STRSIZE len = CFX_WideString::WStringLength(title);
39         CFX_WideString encodedTitle = CFX_WideString::FromUTF16LE(title, len);
40         return FindBookmark(tree, CPDF_Bookmark(), encodedTitle).GetDict();
41 }
42
43 DLLEXPORT FPDF_DEST STDCALL FPDFBookmark_GetDest(FPDF_DOCUMENT document, FPDF_BOOKMARK pDict)
44 {
45         if (!document)
46                 return NULL;
47         if (!pDict)
48                 return NULL;
49         CPDF_Bookmark bookmark((CPDF_Dictionary*)pDict);
50         CPDF_Document* pDoc = (CPDF_Document*)document;
51         CPDF_Dest dest = bookmark.GetDest(pDoc);
52         if (dest)
53                 return dest;
54         // If this bookmark is not directly associated with a dest, we try to get action
55         CPDF_Action action = bookmark.GetAction();
56         if (!action)
57                 return NULL;
58         return action.GetDest(pDoc);
59 }
60
61 DLLEXPORT FPDF_ACTION STDCALL FPDFBookmark_GetAction(FPDF_BOOKMARK pDict)
62 {
63         if (!pDict)
64                 return NULL;
65         CPDF_Bookmark bookmark((CPDF_Dictionary*)pDict);
66         return bookmark.GetAction();
67 }
68
69 DLLEXPORT unsigned long STDCALL FPDFAction_GetType(FPDF_ACTION pDict)
70 {
71         if (!pDict)
72                 return 0;
73         CPDF_Action action = (CPDF_Dictionary*)pDict;
74         CPDF_Action::ActionType type = action.GetType();
75         switch (type) {
76                 case CPDF_Action::GoTo:
77                         return PDFACTION_GOTO;
78                 case CPDF_Action::GoToR:
79                         return PDFACTION_REMOTEGOTO;
80                 case CPDF_Action::URI:
81                         return PDFACTION_URI;
82                 case CPDF_Action::Launch:
83                         return PDFACTION_LAUNCH;
84                 default:
85                         return PDFACTION_UNSUPPORTED;
86         }
87         return PDFACTION_UNSUPPORTED;
88 }
89
90 DLLEXPORT FPDF_DEST STDCALL FPDFAction_GetDest(FPDF_DOCUMENT document, FPDF_ACTION pDict)
91 {
92         if (!document)
93                 return NULL;
94         if (!pDict)
95                 return NULL;
96         CPDF_Document* pDoc = (CPDF_Document*)document;
97         CPDF_Action action = (CPDF_Dictionary*)pDict;
98         return action.GetDest(pDoc);
99 }
100
101 DLLEXPORT unsigned long STDCALL FPDFAction_GetURIPath(FPDF_DOCUMENT document, FPDF_ACTION pDict,
102                                                                                           void* buffer, unsigned long buflen)
103 {
104         if (!document)
105                 return 0;
106         if (!pDict)
107                 return 0;
108         CPDF_Document* pDoc = (CPDF_Document*)document;
109         CPDF_Action action = (CPDF_Dictionary*)pDict;
110         CFX_ByteString path = action.GetURI(pDoc);
111         unsigned long len = path.GetLength() + 1;
112         if (buffer != NULL && buflen >= len)
113                 FXSYS_memcpy(buffer, path.c_str(), len);
114         return len;
115 }
116
117 DLLEXPORT unsigned long STDCALL FPDFDest_GetPageIndex(FPDF_DOCUMENT document, FPDF_DEST pDict)
118 {
119         if (!document)
120                 return 0;
121         if (!pDict)
122                 return 0;
123         CPDF_Document* pDoc = (CPDF_Document*)document;
124         CPDF_Dest dest = (CPDF_Array*)pDict;
125         return dest.GetPageIndex(pDoc);
126 }
127
128 static void ReleaseLinkList(FX_LPVOID data)
129 {
130         delete (CPDF_LinkList*)data;
131 }
132
133 DLLEXPORT FPDF_LINK STDCALL FPDFLink_GetLinkAtPoint(FPDF_PAGE page, double x, double y)
134 {
135         if (!page)
136                 return NULL;
137         CPDF_Page* pPage = (CPDF_Page*)page;
138         // Link list is stored with the document
139         CPDF_Document* pDoc = pPage->m_pDocument;
140         CPDF_LinkList* pLinkList = (CPDF_LinkList*)pDoc->GetPrivateData(&THISMODULE);
141         if (!pLinkList) {
142                 pLinkList = FX_NEW CPDF_LinkList(pDoc);
143                 pDoc->SetPrivateData(&THISMODULE, pLinkList, ReleaseLinkList);
144         }
145         return pLinkList->GetLinkAtPoint(pPage, (FX_FLOAT)x, (FX_FLOAT)y);
146 }
147
148 DLLEXPORT FPDF_DEST STDCALL FPDFLink_GetDest(FPDF_DOCUMENT document, FPDF_LINK pDict)
149 {
150         if (!document)
151                 return NULL;
152         CPDF_Document* pDoc = (CPDF_Document*)document;
153         if (!pDict)
154                 return NULL;
155         CPDF_Link link = (CPDF_Dictionary*)pDict;
156
157         FPDF_DEST dest = link.GetDest(pDoc);
158         if (dest)
159                 return dest;
160         // If this link is not directly associated with a dest, we try to get action
161         CPDF_Action action = link.GetAction();
162         if (!action)
163                 return NULL;
164         return action.GetDest(pDoc);
165 }
166
167 DLLEXPORT FPDF_ACTION STDCALL FPDFLink_GetAction(FPDF_LINK pDict)
168 {
169         if (!pDict)
170                 return NULL;
171         CPDF_Link link = (CPDF_Dictionary*)pDict;
172         return link.GetAction();
173 }
174
175 DLLEXPORT FPDF_BOOL STDCALL FPDFLink_Enumerate(FPDF_PAGE page, int* startPos, FPDF_LINK* linkAnnot)
176 {
177         if(!page || !startPos || !linkAnnot)
178                 return FALSE;
179         CPDF_Page* pPage = (CPDF_Page*)page;
180         if(!pPage->m_pFormDict)
181                 return FALSE;
182         CPDF_Array* pAnnots = pPage->m_pFormDict->GetArray("Annots");
183         if(!pAnnots)
184                 return FALSE;
185         for (int i = *startPos; i < (int)pAnnots->GetCount(); i++) {
186                 CPDF_Dictionary* pDict = (CPDF_Dictionary*)pAnnots->GetElementValue(i);
187                 if (!pDict || pDict->GetType() != PDFOBJ_DICTIONARY)
188                         continue;
189                 if(pDict->GetString(FX_BSTRC("Subtype")).Equal(FX_BSTRC("Link"))) {
190                         *startPos = i + 1;
191                         *linkAnnot = (FPDF_LINK)pDict; 
192                         return TRUE;
193                 }
194         }
195         return FALSE;
196 }
197
198 DLLEXPORT FPDF_BOOL STDCALL FPDFLink_GetAnnotRect(FPDF_LINK linkAnnot, FS_RECTF* rect)
199 {
200         if(!linkAnnot || !rect)
201                 return FALSE;
202         CPDF_Dictionary* pAnnotDict = (CPDF_Dictionary*)linkAnnot;
203         CPDF_Rect rt = pAnnotDict->GetRect(FX_BSTRC("Rect"));
204         rect->left = rt.left;
205         rect->bottom = rt.bottom;
206         rect->right = rt.right;
207         rect->top = rt.top;
208         return TRUE;
209 }
210
211 DLLEXPORT int STDCALL FPDFLink_CountQuadPoints(FPDF_LINK linkAnnot)
212 {
213         if(!linkAnnot)
214                 return 0;
215         CPDF_Dictionary* pAnnotDict = (CPDF_Dictionary*)linkAnnot;
216         CPDF_Array* pArray = pAnnotDict->GetArray(FX_BSTRC("QuadPoints"));
217         if (!pArray)
218                 return 0;
219         else
220                 return pArray->GetCount() / 8;
221 }
222
223 DLLEXPORT FPDF_BOOL STDCALL FPDFLink_GetQuadPoints(FPDF_LINK linkAnnot, int quadIndex, FS_QUADPOINTSF* quadPoints)
224 {
225         if(!linkAnnot || !quadPoints)
226                 return FALSE;
227         CPDF_Dictionary* pAnnotDict = (CPDF_Dictionary*)linkAnnot;
228         CPDF_Array* pArray = pAnnotDict->GetArray(FX_BSTRC("QuadPoints"));
229         if (pArray) {
230                 if (quadIndex < 0 || quadIndex >= (int)pArray->GetCount()/8 || ((quadIndex*8+7) >= (int)pArray->GetCount()))
231                         return FALSE;
232                 quadPoints->x1 = pArray->GetNumber(quadIndex*8);
233                 quadPoints->y1 = pArray->GetNumber(quadIndex*8+1);
234                 quadPoints->x2 = pArray->GetNumber(quadIndex*8+2);
235                 quadPoints->y2 = pArray->GetNumber(quadIndex*8+3);
236                 quadPoints->x3 = pArray->GetNumber(quadIndex*8+4);
237                 quadPoints->y3 = pArray->GetNumber(quadIndex*8+5);
238                 quadPoints->x4 = pArray->GetNumber(quadIndex*8+6);
239                 quadPoints->y4 = pArray->GetNumber(quadIndex*8+7);
240                 return TRUE;
241         } 
242         return FALSE;
243 }
244
245 DLLEXPORT unsigned long STDCALL FPDF_GetMetaText(FPDF_DOCUMENT doc, FPDF_BYTESTRING tag,
246                                                                                                  void* buffer, unsigned long buflen)
247 {
248         if (!doc || !tag)
249                 return 0;
250         CPDF_Document* pDoc = (CPDF_Document*)doc;
251         // Get info dictionary
252         CPDF_Dictionary* pInfo = pDoc->GetInfo();
253         if (!pInfo)
254                 return 0;
255         CFX_WideString text = pInfo->GetUnicodeText(tag);
256         // Use UTF-16LE encoding
257         CFX_ByteString encodedText = text.UTF16LE_Encode();
258         unsigned long len = encodedText.GetLength();
259         if (buffer && buflen >= len + 2) {
260                 FXSYS_memcpy(buffer, encodedText.c_str(), len);
261                 // use double zero as trailer
262                 ((FX_BYTE*)buffer)[len] = 0;
263                 ((FX_BYTE*)buffer)[len + 1] = 0;
264         }
265         return len+2;
266 }