summaryrefslogtreecommitdiff
path: root/core/fpdfdoc
diff options
context:
space:
mode:
authordsinclair <dsinclair@chromium.org>2016-11-08 06:55:40 -0800
committerCommit bot <commit-bot@chromium.org>2016-11-08 06:55:40 -0800
commitc59fa8895fa6fa8428b9b278eee6f05478ab8f56 (patch)
tree3216ee06e894144d578c07e5c655b7637bb6c59c /core/fpdfdoc
parent3c669a7fb05dfb602992a5d2333081daef6f002f (diff)
downloadpdfium-c59fa8895fa6fa8428b9b278eee6f05478ab8f56.tar.xz
Add FPDFDest_GetLocationInPage API
Add an API to get the value of the /XYZ destination parameter. This CL was originally from https://codereview.chromium.org/1960193003/ by halcanary@. Review-Url: https://codereview.chromium.org/2481743004
Diffstat (limited to 'core/fpdfdoc')
-rw-r--r--core/fpdfdoc/cpdf_dest.cpp49
-rw-r--r--core/fpdfdoc/cpdf_dest.h7
-rw-r--r--core/fpdfdoc/cpdf_dest_unittest.cpp60
3 files changed, 116 insertions, 0 deletions
diff --git a/core/fpdfdoc/cpdf_dest.cpp b/core/fpdfdoc/cpdf_dest.cpp
index 312ef87346..51e2d0b534 100644
--- a/core/fpdfdoc/cpdf_dest.cpp
+++ b/core/fpdfdoc/cpdf_dest.cpp
@@ -8,6 +8,8 @@
#include "core/fpdfapi/parser/cpdf_array.h"
#include "core/fpdfapi/parser/cpdf_document.h"
+#include "core/fpdfapi/parser/cpdf_name.h"
+#include "core/fpdfapi/parser/cpdf_number.h"
namespace {
@@ -64,6 +66,53 @@ int CPDF_Dest::GetZoomMode() {
return 0;
}
+bool CPDF_Dest::GetXYZ(bool* pHasX,
+ bool* pHasY,
+ bool* pHasZoom,
+ float* pX,
+ float* pY,
+ float* pZoom) const {
+ *pHasX = false;
+ *pHasY = false;
+ *pHasZoom = false;
+
+ CPDF_Array* pArray = ToArray(m_pObj);
+ if (!pArray)
+ return false;
+
+ if (pArray->GetCount() < 5)
+ return false;
+
+ const CPDF_Name* xyz = ToName(pArray->GetDirectObjectAt(1));
+ if (!xyz || xyz->GetString() != "XYZ")
+ return false;
+
+ const CPDF_Number* numX = ToNumber(pArray->GetDirectObjectAt(2));
+ const CPDF_Number* numY = ToNumber(pArray->GetDirectObjectAt(3));
+ const CPDF_Number* numZoom = ToNumber(pArray->GetDirectObjectAt(4));
+
+ // If the value is a CPDF_Null then ToNumber will return nullptr.
+ *pHasX = !!numX;
+ *pHasY = !!numY;
+ *pHasZoom = !!numZoom;
+
+ if (numX)
+ *pX = numX->GetNumber();
+ if (numY)
+ *pY = numY->GetNumber();
+
+ // A zoom value of 0 is equivalent to a null value, so treat it as a null.
+ if (numZoom) {
+ float num = numZoom->GetNumber();
+ if (num == 0.0)
+ *pHasZoom = false;
+ else
+ *pZoom = num;
+ }
+
+ return true;
+}
+
FX_FLOAT CPDF_Dest::GetParam(int index) {
CPDF_Array* pArray = ToArray(m_pObj);
return pArray ? pArray->GetNumberAt(2 + index) : 0;
diff --git a/core/fpdfdoc/cpdf_dest.h b/core/fpdfdoc/cpdf_dest.h
index 8f38192fd8..527d1dcf8b 100644
--- a/core/fpdfdoc/cpdf_dest.h
+++ b/core/fpdfdoc/cpdf_dest.h
@@ -25,6 +25,13 @@ class CPDF_Dest {
int GetZoomMode();
FX_FLOAT GetParam(int index);
+ bool GetXYZ(bool* pHasX,
+ bool* pHasY,
+ bool* pHasZoom,
+ float* pX,
+ float* pY,
+ float* pZoom) const;
+
private:
CPDF_Object* m_pObj;
};
diff --git a/core/fpdfdoc/cpdf_dest_unittest.cpp b/core/fpdfdoc/cpdf_dest_unittest.cpp
new file mode 100644
index 0000000000..d427ab66a0
--- /dev/null
+++ b/core/fpdfdoc/cpdf_dest_unittest.cpp
@@ -0,0 +1,60 @@
+// Copyright 2016 PDFium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "core/fpdfapi/parser/cpdf_array.h"
+#include "core/fpdfapi/parser/cpdf_null.h"
+#include "core/fpdfapi/parser/cpdf_number.h"
+#include "core/fpdfdoc/cpdf_dest.h"
+#include "testing/gtest/include/gtest/gtest.h"
+#include "testing/test_support.h"
+#include "third_party/base/ptr_util.h"
+
+TEST(cpdf_dest, GetXYZ) {
+ bool hasX;
+ bool hasY;
+ bool hasZoom;
+ float x;
+ float y;
+ float zoom;
+
+ auto dest = pdfium::MakeUnique<CPDF_Dest>();
+ EXPECT_FALSE(dest->GetXYZ(&hasX, &hasY, &hasZoom, &x, &y, &zoom));
+
+ auto array = pdfium::MakeUnique<CPDF_Array>();
+ array->AddInteger(0); // Page Index.
+ array->AddName("XYZ");
+ array->AddNumber(4); // X
+
+ // Not enough entries.
+ dest = pdfium::MakeUnique<CPDF_Dest>(array.get());
+ EXPECT_FALSE(dest->GetXYZ(&hasX, &hasY, &hasZoom, &x, &y, &zoom));
+
+ array->AddNumber(5); // Y
+ array->AddNumber(6); // Zoom.
+
+ dest = pdfium::MakeUnique<CPDF_Dest>(array.get());
+ EXPECT_TRUE(dest->GetXYZ(&hasX, &hasY, &hasZoom, &x, &y, &zoom));
+ EXPECT_TRUE(hasX);
+ EXPECT_TRUE(hasY);
+ EXPECT_TRUE(hasZoom);
+ EXPECT_EQ(4, x);
+ EXPECT_EQ(5, y);
+ EXPECT_EQ(6, zoom);
+
+ // Set zoom to 0.
+ array->SetAt(4, new CPDF_Number(0));
+ dest = pdfium::MakeUnique<CPDF_Dest>(array.get());
+ EXPECT_TRUE(dest->GetXYZ(&hasX, &hasY, &hasZoom, &x, &y, &zoom));
+ EXPECT_FALSE(hasZoom);
+
+ // Set values to null.
+ array->SetAt(2, new CPDF_Null);
+ array->SetAt(3, new CPDF_Null);
+ array->SetAt(4, new CPDF_Null);
+ dest = pdfium::MakeUnique<CPDF_Dest>(array.get());
+ EXPECT_TRUE(dest->GetXYZ(&hasX, &hasY, &hasZoom, &x, &y, &zoom));
+ EXPECT_FALSE(hasX);
+ EXPECT_FALSE(hasY);
+ EXPECT_FALSE(hasZoom);
+}