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