| OLD | NEW |
| (Empty) |
| 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 "core/fpdfdoc/include/cpdf_action.h" | |
| 8 | |
| 9 #include <vector> | |
| 10 | |
| 11 #include "core/fpdfapi/fpdf_parser/include/cpdf_array.h" | |
| 12 #include "core/fpdfapi/fpdf_parser/include/cpdf_document.h" | |
| 13 #include "core/fpdfdoc/include/cpdf_aaction.h" | |
| 14 #include "core/fpdfdoc/include/cpdf_actionfields.h" | |
| 15 #include "core/fpdfdoc/include/cpdf_docjsactions.h" | |
| 16 #include "core/fpdfdoc/include/cpdf_filespec.h" | |
| 17 #include "core/fpdfdoc/include/cpdf_nametree.h" | |
| 18 | |
| 19 namespace { | |
| 20 | |
| 21 const FX_CHAR* const g_sATypes[] = { | |
| 22 "Unknown", "GoTo", "GoToR", "GoToE", "Launch", | |
| 23 "Thread", "URI", "Sound", "Movie", "Hide", | |
| 24 "Named", "SubmitForm", "ResetForm", "ImportData", "JavaScript", | |
| 25 "SetOCGState", "Rendition", "Trans", "GoTo3DView", nullptr}; | |
| 26 | |
| 27 const FX_CHAR* g_sAATypes[] = {"E", "X", "D", "U", "Fo", "Bl", "PO", "PC", | |
| 28 "PV", "PI", "O", "C", "K", "F", "V", "C", | |
| 29 "WC", "WS", "DS", "WP", "DP", ""}; | |
| 30 | |
| 31 } // namespace | |
| 32 | |
| 33 CPDF_Dest CPDF_Action::GetDest(CPDF_Document* pDoc) const { | |
| 34 if (!m_pDict) { | |
| 35 return CPDF_Dest(); | |
| 36 } | |
| 37 CFX_ByteString type = m_pDict->GetStringBy("S"); | |
| 38 if (type != "GoTo" && type != "GoToR") { | |
| 39 return CPDF_Dest(); | |
| 40 } | |
| 41 CPDF_Object* pDest = m_pDict->GetDirectObjectBy("D"); | |
| 42 if (!pDest) { | |
| 43 return CPDF_Dest(); | |
| 44 } | |
| 45 if (pDest->IsString() || pDest->IsName()) { | |
| 46 CPDF_NameTree name_tree(pDoc, "Dests"); | |
| 47 return CPDF_Dest(name_tree.LookupNamedDest(pDoc, pDest->GetString())); | |
| 48 } | |
| 49 if (CPDF_Array* pArray = pDest->AsArray()) | |
| 50 return CPDF_Dest(pArray); | |
| 51 return CPDF_Dest(); | |
| 52 } | |
| 53 | |
| 54 CPDF_Action::ActionType CPDF_Action::GetType() const { | |
| 55 if (!m_pDict) | |
| 56 return Unknown; | |
| 57 | |
| 58 CFX_ByteString csType = m_pDict->GetStringBy("S"); | |
| 59 if (csType.IsEmpty()) | |
| 60 return Unknown; | |
| 61 | |
| 62 for (int i = 0; g_sATypes[i]; ++i) { | |
| 63 if (csType == g_sATypes[i]) | |
| 64 return static_cast<ActionType>(i); | |
| 65 } | |
| 66 return Unknown; | |
| 67 } | |
| 68 | |
| 69 CFX_WideString CPDF_Action::GetFilePath() const { | |
| 70 CFX_ByteString type = m_pDict->GetStringBy("S"); | |
| 71 if (type != "GoToR" && type != "Launch" && type != "SubmitForm" && | |
| 72 type != "ImportData") { | |
| 73 return CFX_WideString(); | |
| 74 } | |
| 75 CPDF_Object* pFile = m_pDict->GetDirectObjectBy("F"); | |
| 76 CFX_WideString path; | |
| 77 if (!pFile) { | |
| 78 if (type == "Launch") { | |
| 79 CPDF_Dictionary* pWinDict = m_pDict->GetDictBy("Win"); | |
| 80 if (pWinDict) { | |
| 81 return CFX_WideString::FromLocal( | |
| 82 pWinDict->GetStringBy("F").AsStringC()); | |
| 83 } | |
| 84 } | |
| 85 return path; | |
| 86 } | |
| 87 CPDF_FileSpec filespec(pFile); | |
| 88 filespec.GetFileName(&path); | |
| 89 return path; | |
| 90 } | |
| 91 CFX_ByteString CPDF_Action::GetURI(CPDF_Document* pDoc) const { | |
| 92 CFX_ByteString csURI; | |
| 93 if (!m_pDict) { | |
| 94 return csURI; | |
| 95 } | |
| 96 if (m_pDict->GetStringBy("S") != "URI") { | |
| 97 return csURI; | |
| 98 } | |
| 99 csURI = m_pDict->GetStringBy("URI"); | |
| 100 CPDF_Dictionary* pRoot = pDoc->GetRoot(); | |
| 101 CPDF_Dictionary* pURI = pRoot->GetDictBy("URI"); | |
| 102 if (pURI) { | |
| 103 if (csURI.Find(":", 0) < 1) { | |
| 104 csURI = pURI->GetStringBy("Base") + csURI; | |
| 105 } | |
| 106 } | |
| 107 return csURI; | |
| 108 } | |
| 109 size_t CPDF_ActionFields::GetFieldsCount() const { | |
| 110 if (!m_pAction) { | |
| 111 return 0; | |
| 112 } | |
| 113 CPDF_Dictionary* pDict = m_pAction->GetDict(); | |
| 114 if (!pDict) { | |
| 115 return 0; | |
| 116 } | |
| 117 CFX_ByteString csType = pDict->GetStringBy("S"); | |
| 118 CPDF_Object* pFields = nullptr; | |
| 119 if (csType == "Hide") { | |
| 120 pFields = pDict->GetDirectObjectBy("T"); | |
| 121 } else { | |
| 122 pFields = pDict->GetArrayBy("Fields"); | |
| 123 } | |
| 124 if (!pFields) | |
| 125 return 0; | |
| 126 if (pFields->IsDictionary()) | |
| 127 return 1; | |
| 128 if (pFields->IsString()) | |
| 129 return 1; | |
| 130 if (CPDF_Array* pArray = pFields->AsArray()) | |
| 131 return pArray->GetCount(); | |
| 132 return 0; | |
| 133 } | |
| 134 | |
| 135 std::vector<CPDF_Object*> CPDF_ActionFields::GetAllFields() const { | |
| 136 std::vector<CPDF_Object*> fields; | |
| 137 if (!m_pAction) | |
| 138 return fields; | |
| 139 | |
| 140 CPDF_Dictionary* pDict = m_pAction->GetDict(); | |
| 141 if (!pDict) | |
| 142 return fields; | |
| 143 | |
| 144 CFX_ByteString csType = pDict->GetStringBy("S"); | |
| 145 CPDF_Object* pFields; | |
| 146 if (csType == "Hide") | |
| 147 pFields = pDict->GetDirectObjectBy("T"); | |
| 148 else | |
| 149 pFields = pDict->GetArrayBy("Fields"); | |
| 150 if (!pFields) | |
| 151 return fields; | |
| 152 | |
| 153 if (pFields->IsDictionary() || pFields->IsString()) { | |
| 154 fields.push_back(pFields); | |
| 155 } else if (CPDF_Array* pArray = pFields->AsArray()) { | |
| 156 for (size_t i = 0; i < pArray->GetCount(); ++i) { | |
| 157 CPDF_Object* pObj = pArray->GetDirectObjectAt(i); | |
| 158 if (pObj) { | |
| 159 fields.push_back(pObj); | |
| 160 } | |
| 161 } | |
| 162 } | |
| 163 return fields; | |
| 164 } | |
| 165 | |
| 166 CPDF_Object* CPDF_ActionFields::GetField(size_t iIndex) const { | |
| 167 if (!m_pAction) { | |
| 168 return nullptr; | |
| 169 } | |
| 170 CPDF_Dictionary* pDict = m_pAction->GetDict(); | |
| 171 if (!pDict) { | |
| 172 return nullptr; | |
| 173 } | |
| 174 CFX_ByteString csType = pDict->GetStringBy("S"); | |
| 175 CPDF_Object* pFields = nullptr; | |
| 176 if (csType == "Hide") { | |
| 177 pFields = pDict->GetDirectObjectBy("T"); | |
| 178 } else { | |
| 179 pFields = pDict->GetArrayBy("Fields"); | |
| 180 } | |
| 181 if (!pFields) { | |
| 182 return nullptr; | |
| 183 } | |
| 184 CPDF_Object* pFindObj = nullptr; | |
| 185 if (pFields->IsDictionary() || pFields->IsString()) { | |
| 186 if (iIndex == 0) | |
| 187 pFindObj = pFields; | |
| 188 } else if (CPDF_Array* pArray = pFields->AsArray()) { | |
| 189 pFindObj = pArray->GetDirectObjectAt(iIndex); | |
| 190 } | |
| 191 return pFindObj; | |
| 192 } | |
| 193 | |
| 194 CFX_WideString CPDF_Action::GetJavaScript() const { | |
| 195 CFX_WideString csJS; | |
| 196 if (!m_pDict) { | |
| 197 return csJS; | |
| 198 } | |
| 199 CPDF_Object* pJS = m_pDict->GetDirectObjectBy("JS"); | |
| 200 return pJS ? pJS->GetUnicodeText() : csJS; | |
| 201 } | |
| 202 | |
| 203 size_t CPDF_Action::GetSubActionsCount() const { | |
| 204 if (!m_pDict || !m_pDict->KeyExist("Next")) | |
| 205 return 0; | |
| 206 | |
| 207 CPDF_Object* pNext = m_pDict->GetDirectObjectBy("Next"); | |
| 208 if (!pNext) | |
| 209 return 0; | |
| 210 if (pNext->IsDictionary()) | |
| 211 return 1; | |
| 212 if (CPDF_Array* pArray = pNext->AsArray()) | |
| 213 return pArray->GetCount(); | |
| 214 return 0; | |
| 215 } | |
| 216 CPDF_Action CPDF_Action::GetSubAction(size_t iIndex) const { | |
| 217 if (!m_pDict || !m_pDict->KeyExist("Next")) { | |
| 218 return CPDF_Action(); | |
| 219 } | |
| 220 CPDF_Object* pNext = m_pDict->GetDirectObjectBy("Next"); | |
| 221 if (CPDF_Dictionary* pDict = ToDictionary(pNext)) { | |
| 222 if (iIndex == 0) | |
| 223 return CPDF_Action(pDict); | |
| 224 } else if (CPDF_Array* pArray = ToArray(pNext)) { | |
| 225 return CPDF_Action(pArray->GetDictAt(iIndex)); | |
| 226 } | |
| 227 return CPDF_Action(); | |
| 228 } | |
| 229 | |
| 230 FX_BOOL CPDF_AAction::ActionExist(AActionType eType) const { | |
| 231 return m_pDict && m_pDict->KeyExist(g_sAATypes[eType]); | |
| 232 } | |
| 233 | |
| 234 CPDF_Action CPDF_AAction::GetAction(AActionType eType) const { | |
| 235 if (!m_pDict) | |
| 236 return CPDF_Action(); | |
| 237 | |
| 238 return CPDF_Action(m_pDict->GetDictBy(g_sAATypes[eType])); | |
| 239 } | |
| 240 | |
| 241 CPDF_DocJSActions::CPDF_DocJSActions(CPDF_Document* pDoc) : m_pDocument(pDoc) {} | |
| 242 | |
| 243 int CPDF_DocJSActions::CountJSActions() const { | |
| 244 ASSERT(m_pDocument); | |
| 245 CPDF_NameTree name_tree(m_pDocument, "JavaScript"); | |
| 246 return name_tree.GetCount(); | |
| 247 } | |
| 248 CPDF_Action CPDF_DocJSActions::GetJSAction(int index, | |
| 249 CFX_ByteString& csName) const { | |
| 250 ASSERT(m_pDocument); | |
| 251 CPDF_NameTree name_tree(m_pDocument, "JavaScript"); | |
| 252 CPDF_Object* pAction = name_tree.LookupValue(index, csName); | |
| 253 if (!ToDictionary(pAction)) { | |
| 254 return CPDF_Action(); | |
| 255 } | |
| 256 return CPDF_Action(pAction->GetDict()); | |
| 257 } | |
| 258 CPDF_Action CPDF_DocJSActions::GetJSAction(const CFX_ByteString& csName) const { | |
| 259 ASSERT(m_pDocument); | |
| 260 CPDF_NameTree name_tree(m_pDocument, "JavaScript"); | |
| 261 CPDF_Object* pAction = name_tree.LookupValue(csName); | |
| 262 if (!ToDictionary(pAction)) { | |
| 263 return CPDF_Action(); | |
| 264 } | |
| 265 return CPDF_Action(pAction->GetDict()); | |
| 266 } | |
| 267 int CPDF_DocJSActions::FindJSAction(const CFX_ByteString& csName) const { | |
| 268 ASSERT(m_pDocument); | |
| 269 CPDF_NameTree name_tree(m_pDocument, "JavaScript"); | |
| 270 return name_tree.GetIndex(csName); | |
| 271 } | |
| OLD | NEW |