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