1 // Copyright 2016 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/cpdf_actionfields.h" 8 9 #include "core/fpdfapi/parser/cpdf_array.h" 10 #include "core/fpdfapi/parser/cpdf_dictionary.h" 11 #include "core/fpdfdoc/cpdf_action.h" 12 CPDF_ActionFields(const CPDF_Action * pAction)13CPDF_ActionFields::CPDF_ActionFields(const CPDF_Action* pAction) 14 : m_pAction(pAction) {} 15 ~CPDF_ActionFields()16CPDF_ActionFields::~CPDF_ActionFields() {} 17 GetFieldsCount() const18size_t CPDF_ActionFields::GetFieldsCount() const { 19 if (!m_pAction) 20 return 0; 21 22 CPDF_Dictionary* pDict = m_pAction->GetDict(); 23 if (!pDict) 24 return 0; 25 26 ByteString csType = pDict->GetStringFor("S"); 27 CPDF_Object* pFields = nullptr; 28 if (csType == "Hide") 29 pFields = pDict->GetDirectObjectFor("T"); 30 else 31 pFields = pDict->GetArrayFor("Fields"); 32 33 if (!pFields) 34 return 0; 35 if (pFields->IsDictionary()) 36 return 1; 37 if (pFields->IsString()) 38 return 1; 39 if (CPDF_Array* pArray = pFields->AsArray()) 40 return pArray->GetCount(); 41 return 0; 42 } 43 GetAllFields() const44std::vector<CPDF_Object*> CPDF_ActionFields::GetAllFields() const { 45 std::vector<CPDF_Object*> fields; 46 if (!m_pAction) 47 return fields; 48 49 CPDF_Dictionary* pDict = m_pAction->GetDict(); 50 if (!pDict) 51 return fields; 52 53 ByteString csType = pDict->GetStringFor("S"); 54 CPDF_Object* pFields; 55 if (csType == "Hide") 56 pFields = pDict->GetDirectObjectFor("T"); 57 else 58 pFields = pDict->GetArrayFor("Fields"); 59 60 if (!pFields) 61 return fields; 62 63 if (pFields->IsDictionary() || pFields->IsString()) { 64 fields.push_back(pFields); 65 } else if (CPDF_Array* pArray = pFields->AsArray()) { 66 for (size_t i = 0; i < pArray->GetCount(); ++i) { 67 CPDF_Object* pObj = pArray->GetDirectObjectAt(i); 68 if (pObj) 69 fields.push_back(pObj); 70 } 71 } 72 return fields; 73 } 74 GetField(size_t iIndex) const75CPDF_Object* CPDF_ActionFields::GetField(size_t iIndex) const { 76 if (!m_pAction) 77 return nullptr; 78 79 CPDF_Dictionary* pDict = m_pAction->GetDict(); 80 if (!pDict) 81 return nullptr; 82 83 ByteString csType = pDict->GetStringFor("S"); 84 CPDF_Object* pFields = nullptr; 85 if (csType == "Hide") 86 pFields = pDict->GetDirectObjectFor("T"); 87 else 88 pFields = pDict->GetArrayFor("Fields"); 89 90 if (!pFields) 91 return nullptr; 92 93 CPDF_Object* pFindObj = nullptr; 94 if (pFields->IsDictionary() || pFields->IsString()) { 95 if (iIndex == 0) 96 pFindObj = pFields; 97 } else if (CPDF_Array* pArray = pFields->AsArray()) { 98 pFindObj = pArray->GetDirectObjectAt(iIndex); 99 } 100 return pFindObj; 101 } 102