From b137e75343cada31ddcf905fc581a3a86a63b5d5 Mon Sep 17 00:00:00 2001 From: Jane Liu Date: Wed, 5 Jul 2017 15:04:33 -0400 Subject: Added APIs for setting annotation flag values 1. Added APIs for getting/setting/unsetting annotation flag values in annotation dictionaries. * Added an embedder test testing all the new functions. Bug=pdfium:737 Change-Id: Ib6bbcf05d6e93c43ec4dcd7120db71bc244afdbf Reviewed-on: https://pdfium-review.googlesource.com/7154 Reviewed-by: dsinclair Commit-Queue: dsinclair --- fpdfsdk/fpdfannot.cpp | 26 ++++++++++++++++++++ fpdfsdk/fpdfannot_embeddertest.cpp | 50 ++++++++++++++++++++++++++++++++++++++ fpdfsdk/fpdfview_c_api_test.c | 2 ++ public/fpdf_annot.h | 30 +++++++++++++++++++++++ 4 files changed, 108 insertions(+) diff --git a/fpdfsdk/fpdfannot.cpp b/fpdfsdk/fpdfannot.cpp index 4d75e6e236..d7ab8abac1 100644 --- a/fpdfsdk/fpdfannot.cpp +++ b/fpdfsdk/fpdfannot.cpp @@ -705,3 +705,29 @@ DLLEXPORT unsigned long STDCALL FPDFAnnot_GetText(FPDF_ANNOTATION annot, return len; } + +DLLEXPORT int STDCALL FPDFAnnot_GetFlags(FPDF_ANNOTATION annot) { + if (!annot) + return FPDF_ANNOT_FLAG_NONE; + + CPDF_Dictionary* pAnnotDict = + CPDFAnnotContextFromFPDFAnnotation(annot)->GetAnnotDict(); + if (!pAnnotDict) + return FPDF_ANNOT_FLAG_NONE; + + return pAnnotDict->GetIntegerFor("F"); +} + +DLLEXPORT FPDF_BOOL STDCALL FPDFAnnot_SetFlags(FPDF_ANNOTATION annot, + int flags) { + if (!annot) + return false; + + CPDF_Dictionary* pAnnotDict = + CPDFAnnotContextFromFPDFAnnotation(annot)->GetAnnotDict(); + if (!pAnnotDict) + return false; + + pAnnotDict->SetNewFor("F", flags); + return true; +} diff --git a/fpdfsdk/fpdfannot_embeddertest.cpp b/fpdfsdk/fpdfannot_embeddertest.cpp index 6c56c06396..beaedf91af 100644 --- a/fpdfsdk/fpdfannot_embeddertest.cpp +++ b/fpdfsdk/fpdfannot_embeddertest.cpp @@ -555,3 +555,53 @@ TEST_F(FPDFAnnotEmbeddertest, AddAndModifyPath) { FPDFPage_CloseAnnot(annot); CloseSaved(); } + +TEST_F(FPDFAnnotEmbeddertest, ModifyAnnotationFlags) { + // Open a file with an annotation and load its first page. + ASSERT_TRUE(OpenDocument("annotation_highlight_rollover_ap.pdf")); + FPDF_PAGE page = FPDF_LoadPage(document(), 0); + ASSERT_TRUE(page); + + // Check that the page renders correctly. + FPDF_BITMAP bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT); + CompareBitmap(bitmap, 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e"); + FPDFBitmap_Destroy(bitmap); + + // Retrieve the annotation. + FPDF_ANNOTATION annot = FPDFPage_GetAnnot(page, 0); + ASSERT_TRUE(annot); + + // Check that the original flag values are as expected. + int flags = FPDFAnnot_GetFlags(annot); + EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN); + EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT); + + // Set the HIDDEN flag. + flags |= FPDF_ANNOT_FLAG_HIDDEN; + EXPECT_TRUE(FPDFAnnot_SetFlags(annot, flags)); + flags = FPDFAnnot_GetFlags(annot); + EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_HIDDEN); + EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT); + + // Check that the page renders correctly without rendering the annotation. + bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT); + CompareBitmap(bitmap, 612, 792, "1940568c9ba33bac5d0b1ee9558c76b3"); + FPDFBitmap_Destroy(bitmap); + + // Unset the HIDDEN flag. + EXPECT_TRUE(FPDFAnnot_SetFlags(annot, FPDF_ANNOT_FLAG_NONE)); + EXPECT_FALSE(FPDFAnnot_GetFlags(annot)); + flags &= ~FPDF_ANNOT_FLAG_HIDDEN; + EXPECT_TRUE(FPDFAnnot_SetFlags(annot, flags)); + flags = FPDFAnnot_GetFlags(annot); + EXPECT_FALSE(flags & FPDF_ANNOT_FLAG_HIDDEN); + EXPECT_TRUE(flags & FPDF_ANNOT_FLAG_PRINT); + + // Check that the page renders correctly as before. + bitmap = RenderPageWithFlags(page, form_handle_, FPDF_ANNOT); + CompareBitmap(bitmap, 612, 792, "dc98f06da047bd8aabfa99562d2cbd1e"); + FPDFBitmap_Destroy(bitmap); + + FPDFPage_CloseAnnot(annot); + UnloadPage(page); +} diff --git a/fpdfsdk/fpdfview_c_api_test.c b/fpdfsdk/fpdfview_c_api_test.c index 94f4051bcd..01c318912f 100644 --- a/fpdfsdk/fpdfview_c_api_test.c +++ b/fpdfsdk/fpdfview_c_api_test.c @@ -55,6 +55,8 @@ int CheckPDFiumCApi() { CHK(FPDFAnnot_GetRect); CHK(FPDFAnnot_SetText); CHK(FPDFAnnot_GetText); + CHK(FPDFAnnot_GetFlags); + CHK(FPDFAnnot_SetFlags); // fpdf_dataavail.h CHK(FPDFAvail_Create); diff --git a/public/fpdf_annot.h b/public/fpdf_annot.h index 89edd01613..971662f8e8 100644 --- a/public/fpdf_annot.h +++ b/public/fpdf_annot.h @@ -45,6 +45,18 @@ extern "C" { #define FPDF_ANNOT_RICHMEDIA 26 #define FPDF_ANNOT_XFAWIDGET 27 +// Refer to PDF Reference (6th edition) table 8.16 for all annotation flags. +#define FPDF_ANNOT_FLAG_NONE 0 +#define FPDF_ANNOT_FLAG_INVISIBLE (1 << 0) +#define FPDF_ANNOT_FLAG_HIDDEN (1 << 1) +#define FPDF_ANNOT_FLAG_PRINT (1 << 2) +#define FPDF_ANNOT_FLAG_NOZOOM (1 << 3) +#define FPDF_ANNOT_FLAG_NOROTATE (1 << 4) +#define FPDF_ANNOT_FLAG_NOVIEW (1 << 5) +#define FPDF_ANNOT_FLAG_READONLY (1 << 6) +#define FPDF_ANNOT_FLAG_LOCKED (1 << 7) +#define FPDF_ANNOT_FLAG_TOGGLENOVIEW (1 << 8) + typedef enum FPDFANNOT_COLORTYPE { FPDFANNOT_COLORTYPE_Color = 0, FPDFANNOT_COLORTYPE_InteriorColor @@ -275,6 +287,24 @@ DLLEXPORT unsigned long STDCALL FPDFAnnot_GetText(FPDF_ANNOTATION annot, void* buffer, unsigned long buflen); +// Experimental API. +// Get the annotation flags of |annot|. +// +// annot - handle to an annotation. +// +// Returns the annotation flags. +DLLEXPORT int STDCALL FPDFAnnot_GetFlags(FPDF_ANNOTATION annot); + +// Experimental API. +// Set the |annot|'s flags to be of the value |flags|. +// +// annot - handle to an annotation. +// flags - the flag values to be set. +// +// Returns true if successful. +DLLEXPORT FPDF_BOOL STDCALL FPDFAnnot_SetFlags(FPDF_ANNOTATION annot, + int flags); + #ifdef __cplusplus } // extern "C" #endif // __cplusplus -- cgit v1.2.3