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_dest.h" 8 9 #include "core/fpdfapi/parser/cpdf_array.h" 10 #include "core/fpdfapi/parser/cpdf_document.h" 11 #include "core/fpdfapi/parser/cpdf_name.h" 12 #include "core/fpdfapi/parser/cpdf_number.h" 13 14 namespace { 15 16 const FX_CHAR* const g_sZoomModes[] = {"XYZ", "Fit", "FitH", "FitV", "FitR", 17 "FitB", "FitBH", "FitBV", nullptr}; 18 19 } // namespace 20 21 int CPDF_Dest::GetPageIndex(CPDF_Document* pDoc) { 22 CPDF_Array* pArray = ToArray(m_pObj); 23 if (!pArray) 24 return 0; 25 26 CPDF_Object* pPage = pArray->GetDirectObjectAt(0); 27 if (!pPage) 28 return 0; 29 if (pPage->IsNumber()) 30 return pPage->GetInteger(); 31 if (!pPage->IsDictionary()) 32 return 0; 33 return pDoc->GetPageIndex(pPage->GetObjNum()); 34 } 35 36 uint32_t CPDF_Dest::GetPageObjNum() { 37 CPDF_Array* pArray = ToArray(m_pObj); 38 if (!pArray) 39 return 0; 40 41 CPDF_Object* pPage = pArray->GetDirectObjectAt(0); 42 if (!pPage) 43 return 0; 44 if (pPage->IsNumber()) 45 return pPage->GetInteger(); 46 if (pPage->IsDictionary()) 47 return pPage->GetObjNum(); 48 return 0; 49 } 50 51 int CPDF_Dest::GetZoomMode() { 52 CPDF_Array* pArray = ToArray(m_pObj); 53 if (!pArray) 54 return 0; 55 56 CPDF_Object* pObj = pArray->GetDirectObjectAt(1); 57 if (!pObj) 58 return 0; 59 60 CFX_ByteString mode = pObj->GetString(); 61 for (int i = 0; g_sZoomModes[i]; ++i) { 62 if (mode == g_sZoomModes[i]) 63 return i + 1; 64 } 65 66 return 0; 67 } 68 69 bool CPDF_Dest::GetXYZ(bool* pHasX, 70 bool* pHasY, 71 bool* pHasZoom, 72 float* pX, 73 float* pY, 74 float* pZoom) const { 75 *pHasX = false; 76 *pHasY = false; 77 *pHasZoom = false; 78 79 CPDF_Array* pArray = ToArray(m_pObj); 80 if (!pArray) 81 return false; 82 83 if (pArray->GetCount() < 5) 84 return false; 85 86 const CPDF_Name* xyz = ToName(pArray->GetDirectObjectAt(1)); 87 if (!xyz || xyz->GetString() != "XYZ") 88 return false; 89 90 const CPDF_Number* numX = ToNumber(pArray->GetDirectObjectAt(2)); 91 const CPDF_Number* numY = ToNumber(pArray->GetDirectObjectAt(3)); 92 const CPDF_Number* numZoom = ToNumber(pArray->GetDirectObjectAt(4)); 93 94 // If the value is a CPDF_Null then ToNumber will return nullptr. 95 *pHasX = !!numX; 96 *pHasY = !!numY; 97 *pHasZoom = !!numZoom; 98 99 if (numX) 100 *pX = numX->GetNumber(); 101 if (numY) 102 *pY = numY->GetNumber(); 103 104 // A zoom value of 0 is equivalent to a null value, so treat it as a null. 105 if (numZoom) { 106 float num = numZoom->GetNumber(); 107 if (num == 0.0) 108 *pHasZoom = false; 109 else 110 *pZoom = num; 111 } 112 113 return true; 114 } 115 116 FX_FLOAT CPDF_Dest::GetParam(int index) { 117 CPDF_Array* pArray = ToArray(m_pObj); 118 return pArray ? pArray->GetNumberAt(2 + index) : 0; 119 } 120 121 CFX_ByteString CPDF_Dest::GetRemoteName() { 122 return m_pObj ? m_pObj->GetString() : CFX_ByteString(); 123 } 124