summaryrefslogtreecommitdiff
path: root/fpdfsdk
diff options
context:
space:
mode:
authorthestig <thestig@chromium.org>2016-11-04 16:07:25 -0700
committerCommit bot <commit-bot@chromium.org>2016-11-04 16:07:25 -0700
commit04bebfe590d6d27a825b881fd234e31501843edc (patch)
tree127477b084005457579c599b2ec9eef0668a1479 /fpdfsdk
parent931d087b991a986cc8bfa72131fe8eda0d987fc4 (diff)
downloadpdfium-04bebfe590d6d27a825b881fd234e31501843edc.tar.xz
Implement FPDF_VIEWERREF_GetName() API.chromium/2910
This is a generic API function to retrieve any viewer preference of type name. BUG=pdfium:414 Review-Url: https://codereview.chromium.org/2475923003
Diffstat (limited to 'fpdfsdk')
-rw-r--r--fpdfsdk/fpdfview.cpp19
-rw-r--r--fpdfsdk/fpdfview_c_api_test.c1
-rw-r--r--fpdfsdk/fpdfview_embeddertest.cpp48
3 files changed, 67 insertions, 1 deletions
diff --git a/fpdfsdk/fpdfview.cpp b/fpdfsdk/fpdfview.cpp
index cde15dff9b..9142dc78c9 100644
--- a/fpdfsdk/fpdfview.cpp
+++ b/fpdfsdk/fpdfview.cpp
@@ -942,6 +942,25 @@ FPDF_VIEWERREF_GetDuplex(FPDF_DOCUMENT document) {
return DuplexUndefined;
}
+DLLEXPORT unsigned long STDCALL FPDF_VIEWERREF_GetName(FPDF_DOCUMENT document,
+ FPDF_BYTESTRING key,
+ char* buffer,
+ unsigned long length) {
+ CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
+ if (!pDoc)
+ return 0;
+
+ CPDF_ViewerPreferences viewRef(pDoc);
+ CFX_ByteString bsVal;
+ if (!viewRef.GenericName(key, &bsVal))
+ return 0;
+
+ unsigned long dwStringLen = bsVal.GetLength() + 1;
+ if (buffer && length >= dwStringLen)
+ memcpy(buffer, bsVal.c_str(), dwStringLen);
+ return dwStringLen;
+}
+
DLLEXPORT FPDF_DWORD STDCALL FPDF_CountNamedDests(FPDF_DOCUMENT document) {
CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document);
if (!pDoc)
diff --git a/fpdfsdk/fpdfview_c_api_test.c b/fpdfsdk/fpdfview_c_api_test.c
index 2f0cb49fc2..4847180e46 100644
--- a/fpdfsdk/fpdfview_c_api_test.c
+++ b/fpdfsdk/fpdfview_c_api_test.c
@@ -228,6 +228,7 @@ int CheckPDFiumCApi() {
CHK(FPDF_VIEWERREF_GetNumCopies);
CHK(FPDF_VIEWERREF_GetPrintPageRange);
CHK(FPDF_VIEWERREF_GetDuplex);
+ CHK(FPDF_VIEWERREF_GetName);
CHK(FPDF_CountNamedDests);
CHK(FPDF_GetNamedDestByName);
CHK(FPDF_GetNamedDest);
diff --git a/fpdfsdk/fpdfview_embeddertest.cpp b/fpdfsdk/fpdfview_embeddertest.cpp
index 820d496bdc..e712edb89a 100644
--- a/fpdfsdk/fpdfview_embeddertest.cpp
+++ b/fpdfsdk/fpdfview_embeddertest.cpp
@@ -56,6 +56,10 @@ TEST_F(FPDFViewEmbeddertest, EmptyDocument) {
EXPECT_EQ(1, FPDF_VIEWERREF_GetNumCopies(document()));
EXPECT_EQ(DuplexUndefined, FPDF_VIEWERREF_GetDuplex(document()));
+ char buf[100];
+ EXPECT_EQ(0U, FPDF_VIEWERREF_GetName(document(), "foo", nullptr, 0));
+ EXPECT_EQ(0U, FPDF_VIEWERREF_GetName(document(), "foo", buf, sizeof(buf)));
+
EXPECT_EQ(0u, FPDF_CountNamedDests(document()));
}
@@ -69,11 +73,53 @@ TEST_F(FPDFViewEmbeddertest, Page) {
EXPECT_EQ(nullptr, LoadPage(1));
}
-TEST_F(FPDFViewEmbeddertest, ViewerRef) {
+TEST_F(FPDFViewEmbeddertest, ViewerRefDummy) {
EXPECT_TRUE(OpenDocument("about_blank.pdf"));
EXPECT_TRUE(FPDF_VIEWERREF_GetPrintScaling(document()));
EXPECT_EQ(1, FPDF_VIEWERREF_GetNumCopies(document()));
EXPECT_EQ(DuplexUndefined, FPDF_VIEWERREF_GetDuplex(document()));
+
+ char buf[100];
+ EXPECT_EQ(0U, FPDF_VIEWERREF_GetName(document(), "foo", nullptr, 0));
+ EXPECT_EQ(0U, FPDF_VIEWERREF_GetName(document(), "foo", buf, sizeof(buf)));
+}
+
+TEST_F(FPDFViewEmbeddertest, ViewerRef) {
+ EXPECT_TRUE(OpenDocument("viewer_ref.pdf"));
+ EXPECT_TRUE(FPDF_VIEWERREF_GetPrintScaling(document()));
+ EXPECT_EQ(5, FPDF_VIEWERREF_GetNumCopies(document()));
+ EXPECT_EQ(DuplexUndefined, FPDF_VIEWERREF_GetDuplex(document()));
+
+ // Test some corner cases.
+ char buf[100];
+ EXPECT_EQ(0U, FPDF_VIEWERREF_GetName(document(), "", buf, sizeof(buf)));
+ EXPECT_EQ(0U, FPDF_VIEWERREF_GetName(document(), "foo", nullptr, 0));
+ EXPECT_EQ(0U, FPDF_VIEWERREF_GetName(document(), "foo", buf, sizeof(buf)));
+
+ // Make sure |buf| does not get written into when it appears to be too small.
+ strcpy(buf, "ABCD");
+ EXPECT_EQ(4U, FPDF_VIEWERREF_GetName(document(), "Foo", buf, 1));
+ EXPECT_STREQ("ABCD", buf);
+
+ // Note "Foo" is a different key from "foo".
+ EXPECT_EQ(4U,
+ FPDF_VIEWERREF_GetName(document(), "Foo", nullptr, sizeof(buf)));
+ ASSERT_EQ(4U, FPDF_VIEWERREF_GetName(document(), "Foo", buf, sizeof(buf)));
+ EXPECT_STREQ("foo", buf);
+
+ // Try to retrieve a boolean and an integer.
+ EXPECT_EQ(
+ 0U, FPDF_VIEWERREF_GetName(document(), "HideToolbar", buf, sizeof(buf)));
+ EXPECT_EQ(0U,
+ FPDF_VIEWERREF_GetName(document(), "NumCopies", buf, sizeof(buf)));
+
+ // Try more valid cases.
+ ASSERT_EQ(4U,
+ FPDF_VIEWERREF_GetName(document(), "Direction", buf, sizeof(buf)));
+ EXPECT_STREQ("R2L", buf);
+ ASSERT_EQ(8U,
+ FPDF_VIEWERREF_GetName(document(), "ViewArea", buf, sizeof(buf)));
+ EXPECT_STREQ("CropBox", buf);
}
TEST_F(FPDFViewEmbeddertest, NamedDests) {