Introduce CPDF_Dest::GetScrollPositionsArray().
This de-duplicates code from two places, and avoids exposing
the internal array for these cases.
Change-Id: Ibc1679177afb35be85e9d00e997aa0fbedcd8925
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/99070
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
diff --git a/core/fpdfdoc/cpdf_dest.cpp b/core/fpdfdoc/cpdf_dest.cpp
index 9d555e8..0b68aad 100644
--- a/core/fpdfdoc/cpdf_dest.cpp
+++ b/core/fpdfdoc/cpdf_dest.cpp
@@ -67,6 +67,17 @@
return pDoc->GetPageIndex(pPage->GetObjNum());
}
+std::vector<float> CPDF_Dest::GetScrollPositionArray() const {
+ std::vector<float> result;
+ if (m_pArray) {
+ // Skip over index 0 which contains destination page details, and index 1
+ // which contains a parameter that describes the rest of the array.
+ for (size_t i = 2; i < m_pArray->size(); i++)
+ result.push_back(m_pArray->GetFloatAt(i));
+ }
+ return result;
+}
+
int CPDF_Dest::GetZoomMode() const {
if (!m_pArray)
return 0;
diff --git a/core/fpdfdoc/cpdf_dest.h b/core/fpdfdoc/cpdf_dest.h
index 5c5e7fc..4f13992 100644
--- a/core/fpdfdoc/cpdf_dest.h
+++ b/core/fpdfdoc/cpdf_dest.h
@@ -7,6 +7,8 @@
#ifndef CORE_FPDFDOC_CPDF_DEST_H_
#define CORE_FPDFDOC_CPDF_DEST_H_
+#include <vector>
+
#include "core/fpdfapi/parser/cpdf_array.h"
#include "core/fxcrt/retain_ptr.h"
@@ -24,14 +26,15 @@
RetainPtr<const CPDF_Object> pDest);
const CPDF_Array* GetArray() const { return m_pArray.Get(); }
+
int GetDestPageIndex(CPDF_Document* pDoc) const;
+ std::vector<float> GetScrollPositionArray() const;
// Returns the zoom mode, as one of the PDFDEST_VIEW_* values in fpdf_doc.h.
int GetZoomMode() const;
size_t GetNumParams() const;
float GetParam(size_t index) const;
-
bool GetXYZ(bool* pHasX,
bool* pHasY,
bool* pHasZoom,
diff --git a/fpdfsdk/cpdfsdk_formfillenvironment.cpp b/fpdfsdk/cpdfsdk_formfillenvironment.cpp
index adbe03a..0178e9c 100644
--- a/fpdfsdk/cpdfsdk_formfillenvironment.cpp
+++ b/fpdfsdk/cpdfsdk_formfillenvironment.cpp
@@ -888,17 +888,8 @@
CPDF_Document* document = GetPDFDocument();
DCHECK(document);
- const CPDF_Array* dest_array = dest.GetArray();
- std::vector<float> dest_positions;
- // |dest_array| index 0 contains destination page details and index 1 contains
- // parameter that explains about the rest of |dest_array|.
- if (dest_array) {
- for (size_t i = 2; i < dest_array->size(); i++)
- dest_positions.push_back(dest_array->GetFloatAt(i));
- }
-
- DoGoToAction(dest.GetDestPageIndex(document), dest.GetZoomMode(),
- dest_positions);
+ std::vector<float> positions = dest.GetScrollPositionArray();
+ DoGoToAction(dest.GetDestPageIndex(document), dest.GetZoomMode(), positions);
return true;
}
diff --git a/fxjs/cjs_document.cpp b/fxjs/cjs_document.cpp
index 673ead2..3abb515 100644
--- a/fxjs/cjs_document.cpp
+++ b/fxjs/cjs_document.cpp
@@ -1389,15 +1389,10 @@
return CJS_Result::Failure(JSMessage::kBadObjectError);
CPDF_Dest dest(std::move(dest_array));
- const CPDF_Array* arrayObject = dest.GetArray();
- std::vector<float> scrollPositionArray;
- if (arrayObject) {
- for (size_t i = 2; i < arrayObject->size(); i++)
- scrollPositionArray.push_back(arrayObject->GetFloatAt(i));
- }
+ std::vector<float> positions = dest.GetScrollPositionArray();
pRuntime->BeginBlock();
m_pFormFillEnv->DoGoToAction(dest.GetDestPageIndex(pDocument),
- dest.GetZoomMode(), scrollPositionArray);
+ dest.GetZoomMode(), positions);
pRuntime->EndBlock();
return CJS_Result::Success();
}