• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 The PDFium Authors
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 <algorithm>
10 #include <iterator>
11 #include <utility>
12 
13 #include "core/fpdfapi/parser/cpdf_array.h"
14 #include "core/fpdfapi/parser/cpdf_document.h"
15 #include "core/fpdfapi/parser/cpdf_name.h"
16 #include "core/fpdfapi/parser/cpdf_number.h"
17 #include "core/fpdfdoc/cpdf_nametree.h"
18 
19 namespace {
20 
21 // These arrays are indexed by the PDFDEST_VIEW_* constants.
22 
23 // Last element is a sentinel.
24 const char* const kZoomModes[] = {"Unknown", "XYZ",  "Fit",   "FitH",  "FitV",
25                                   "FitR",    "FitB", "FitBH", "FitBV", nullptr};
26 
27 constexpr uint8_t kZoomModeMaxParamCount[] = {0, 3, 0, 1, 1, 4, 0, 1, 1, 0};
28 
29 static_assert(std::size(kZoomModes) == std::size(kZoomModeMaxParamCount),
30               "Zoom mode count Mismatch");
31 
32 }  // namespace
33 
CPDF_Dest(RetainPtr<const CPDF_Array> pArray)34 CPDF_Dest::CPDF_Dest(RetainPtr<const CPDF_Array> pArray)
35     : m_pArray(std::move(pArray)) {}
36 
37 CPDF_Dest::CPDF_Dest(const CPDF_Dest& that) = default;
38 
39 CPDF_Dest::~CPDF_Dest() = default;
40 
41 // static
Create(CPDF_Document * pDoc,RetainPtr<const CPDF_Object> pDest)42 CPDF_Dest CPDF_Dest::Create(CPDF_Document* pDoc,
43                             RetainPtr<const CPDF_Object> pDest) {
44   if (!pDest)
45     return CPDF_Dest(nullptr);
46 
47   if (pDest->IsString() || pDest->IsName())
48     return CPDF_Dest(CPDF_NameTree::LookupNamedDest(pDoc, pDest->GetString()));
49 
50   return CPDF_Dest(ToArray(pDest));
51 }
52 
GetDestPageIndex(CPDF_Document * pDoc) const53 int CPDF_Dest::GetDestPageIndex(CPDF_Document* pDoc) const {
54   if (!m_pArray)
55     return -1;
56 
57   RetainPtr<const CPDF_Object> pPage = m_pArray->GetDirectObjectAt(0);
58   if (!pPage)
59     return -1;
60 
61   if (pPage->IsNumber())
62     return pPage->GetInteger();
63 
64   if (!pPage->IsDictionary())
65     return -1;
66 
67   return pDoc->GetPageIndex(pPage->GetObjNum());
68 }
69 
GetScrollPositionArray() const70 std::vector<float> CPDF_Dest::GetScrollPositionArray() const {
71   std::vector<float> result;
72   if (m_pArray) {
73     // Skip over index 0 which contains destination page details, and index 1
74     // which contains a parameter that describes the rest of the array.
75     for (size_t i = 2; i < m_pArray->size(); i++)
76       result.push_back(m_pArray->GetFloatAt(i));
77   }
78   return result;
79 }
80 
GetZoomMode() const81 int CPDF_Dest::GetZoomMode() const {
82   if (!m_pArray)
83     return 0;
84 
85   RetainPtr<const CPDF_Object> pArray = m_pArray->GetDirectObjectAt(1);
86   if (!pArray)
87     return 0;
88 
89   ByteString mode = pArray->GetString();
90   for (int i = 1; kZoomModes[i]; ++i) {
91     if (mode == kZoomModes[i])
92       return i;
93   }
94 
95   return 0;
96 }
97 
GetXYZ(bool * pHasX,bool * pHasY,bool * pHasZoom,float * pX,float * pY,float * pZoom) const98 bool CPDF_Dest::GetXYZ(bool* pHasX,
99                        bool* pHasY,
100                        bool* pHasZoom,
101                        float* pX,
102                        float* pY,
103                        float* pZoom) const {
104   *pHasX = false;
105   *pHasY = false;
106   *pHasZoom = false;
107 
108   if (!m_pArray)
109     return false;
110 
111   if (m_pArray->size() < 5)
112     return false;
113 
114   RetainPtr<const CPDF_Name> xyz = ToName(m_pArray->GetDirectObjectAt(1));
115   if (!xyz || xyz->GetString() != "XYZ")
116     return false;
117 
118   RetainPtr<const CPDF_Number> numX = ToNumber(m_pArray->GetDirectObjectAt(2));
119   RetainPtr<const CPDF_Number> numY = ToNumber(m_pArray->GetDirectObjectAt(3));
120   RetainPtr<const CPDF_Number> numZoom =
121       ToNumber(m_pArray->GetDirectObjectAt(4));
122 
123   // If the value is a CPDF_Null then ToNumber will return nullptr.
124   *pHasX = !!numX;
125   *pHasY = !!numY;
126   *pHasZoom = !!numZoom;
127 
128   if (numX)
129     *pX = numX->GetNumber();
130   if (numY)
131     *pY = numY->GetNumber();
132 
133   // A zoom value of 0 is equivalent to a null value, so treat it as a null.
134   if (numZoom) {
135     float num = numZoom->GetNumber();
136     if (num == 0.0)
137       *pHasZoom = false;
138     else
139       *pZoom = num;
140   }
141 
142   return true;
143 }
144 
GetNumParams() const145 size_t CPDF_Dest::GetNumParams() const {
146   if (!m_pArray || m_pArray->size() < 2)
147     return 0;
148 
149   size_t maxParamsForFitType = kZoomModeMaxParamCount[GetZoomMode()];
150   size_t numParamsInArray = m_pArray->size() - 2;
151   return std::min(maxParamsForFitType, numParamsInArray);
152 }
153 
GetParam(size_t index) const154 float CPDF_Dest::GetParam(size_t index) const {
155   return m_pArray ? m_pArray->GetFloatAt(2 + index) : 0;
156 }
157