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