From 132c38e7bdd0bdd1aa526b35c7bcb79c286fb061 Mon Sep 17 00:00:00 2001 From: Henrique Nakashima Date: Mon, 23 Apr 2018 16:35:56 +0000 Subject: Create API to get PageObject mark parameters. New calls added in this cl: - FPDFPageObjMark_GetParamKey - FPDFPageObjMark_GetParamValueType - FPDFPageObjMark_GetParamIntValue Bug: pdfium:1037 Change-Id: Iedb74ddbf8a5483de62094ec295dadd6367d5175 Reviewed-on: https://pdfium-review.googlesource.com/30912 Reviewed-by: dsinclair Commit-Queue: Henrique Nakashima --- fpdfsdk/fpdf_edit_embeddertest.cpp | 8 +++++ fpdfsdk/fpdf_editpage.cpp | 55 +++++++++++++++++++++++++++++++ fpdfsdk/fpdf_view_c_api_test.c | 3 ++ public/fpdf_edit.h | 43 ++++++++++++++++++++++++ testing/resources/text_in_page_marked.in | 2 +- testing/resources/text_in_page_marked.pdf | 2 +- 6 files changed, 111 insertions(+), 2 deletions(-) diff --git a/fpdfsdk/fpdf_edit_embeddertest.cpp b/fpdfsdk/fpdf_edit_embeddertest.cpp index 8f54bf8eab..ec6b893530 100644 --- a/fpdfsdk/fpdf_edit_embeddertest.cpp +++ b/fpdfsdk/fpdf_edit_embeddertest.cpp @@ -473,6 +473,14 @@ TEST_F(FPDFEditEmbeddertest, RemoveMarkedObjectsPrime) { } else if (name == L"Square") { square_count++; EXPECT_EQ(1, FPDFPageObjMark_CountParams(mark)); + ASSERT_GT(FPDFPageObjMark_GetParamKey(mark, 0, buffer, 256), 0u); + std::wstring key = + GetPlatformWString(reinterpret_cast(buffer)); + EXPECT_EQ(L"Factor", key); + EXPECT_EQ(FPDF_OBJECT_NUMBER, + FPDFPageObjMark_GetParamValueType(mark, 0)); + int square_root = FPDFPageObjMark_GetParamIntValue(mark, 0); + EXPECT_EQ(i + 1, square_root * square_root); } else if (name == L"GreaterThanTen") { greater_than_ten_count++; EXPECT_EQ(0, FPDFPageObjMark_CountParams(mark)); diff --git a/fpdfsdk/fpdf_editpage.cpp b/fpdfsdk/fpdf_editpage.cpp index 7a8bbabf2d..241025f3a6 100644 --- a/fpdfsdk/fpdf_editpage.cpp +++ b/fpdfsdk/fpdf_editpage.cpp @@ -99,6 +99,28 @@ void CalcBoundingBox(CPDF_PageObject* pPageObj) { } } +const std::pair>* +GetMarkParamPairAtIndex(FPDF_PAGEOBJECTMARK mark, unsigned long index) { + if (!mark) + return nullptr; + + const CPDF_ContentMarkItem* pMarkItem = + CPDFContentMarkItemFromFPDFPageObjectMark(mark); + + const CPDF_Dictionary* pParams = pMarkItem->GetParam(); + if (!pParams) + return nullptr; + + for (auto& it : *pParams) { + if (index == 0) + return ⁢ + + --index; + } + + return nullptr; +} + } // namespace FPDF_EXPORT FPDF_DOCUMENT FPDF_CALLCONV FPDF_CreateNewDocument() { @@ -284,6 +306,39 @@ FPDFPageObjMark_CountParams(FPDF_PAGEOBJECTMARK mark) { return pParams->GetCount(); } +FPDF_EXPORT unsigned long FPDF_CALLCONV +FPDFPageObjMark_GetParamKey(FPDF_PAGEOBJECTMARK mark, + unsigned long index, + void* buffer, + unsigned long buflen) { + auto* param_pair = GetMarkParamPairAtIndex(mark, index); + if (!param_pair) + return 0; + + return Utf16EncodeMaybeCopyAndReturnLength( + WideString::FromUTF8(param_pair->first.AsStringView()), buffer, buflen); +} + +FPDF_EXPORT FPDF_OBJECT_TYPE FPDF_CALLCONV +FPDFPageObjMark_GetParamValueType(FPDF_PAGEOBJECTMARK mark, + unsigned long index) { + auto* param_pair = GetMarkParamPairAtIndex(mark, index); + if (!param_pair) + return FPDF_OBJECT_UNKNOWN; + + return param_pair->second->GetType(); +} + +FPDF_EXPORT int FPDF_CALLCONV +FPDFPageObjMark_GetParamIntValue(FPDF_PAGEOBJECTMARK mark, + unsigned long index) { + auto* param_pair = GetMarkParamPairAtIndex(mark, index); + if (!param_pair) + return 0; + + return param_pair->second->GetInteger(); +} + FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFPageObj_HasTransparency(FPDF_PAGEOBJECT pageObject) { if (!pageObject) diff --git a/fpdfsdk/fpdf_view_c_api_test.c b/fpdfsdk/fpdf_view_c_api_test.c index 017487246b..dd93095550 100644 --- a/fpdfsdk/fpdf_view_c_api_test.c +++ b/fpdfsdk/fpdf_view_c_api_test.c @@ -149,6 +149,9 @@ int CheckPDFiumCApi() { CHK(FPDFPageObj_GetMark); CHK(FPDFPageObjMark_GetName); CHK(FPDFPageObjMark_CountParams); + CHK(FPDFPageObjMark_GetParamKey); + CHK(FPDFPageObjMark_GetParamValueType); + CHK(FPDFPageObjMark_GetParamIntValue); CHK(FPDFImageObj_LoadJpegFile); CHK(FPDFImageObj_LoadJpegFileInline); CHK(FPDFImageObj_SetMatrix); diff --git a/public/fpdf_edit.h b/public/fpdf_edit.h index 4968a7f003..8228bb759a 100644 --- a/public/fpdf_edit.h +++ b/public/fpdf_edit.h @@ -281,6 +281,7 @@ FPDF_EXPORT void FPDF_CALLCONV FPDFPage_TransformAnnots(FPDF_PAGE page, FPDF_EXPORT FPDF_PAGEOBJECT FPDF_CALLCONV FPDFPageObj_NewImageObj(FPDF_DOCUMENT document); +// Experimental API. // Get number of content marks in |page_object|. // // page_object - handle to a page object. @@ -290,6 +291,7 @@ FPDFPageObj_NewImageObj(FPDF_DOCUMENT document); FPDF_EXPORT int FPDF_CALLCONV FPDFPageObj_CountMarks(FPDF_PAGEOBJECT page_object); +// Experimental API. // Get content mark in |page_object| at |index|. // // page_object - handle to a page object. @@ -302,6 +304,7 @@ FPDFPageObj_CountMarks(FPDF_PAGEOBJECT page_object); FPDF_EXPORT FPDF_PAGEOBJECTMARK FPDF_CALLCONV FPDFPageObj_GetMark(FPDF_PAGEOBJECT page_object, unsigned long index); +// Experimental API. // Get name of a content mark. |buffer| is only modified if |buflen| is longer // than the length of the name. // @@ -315,6 +318,7 @@ FPDFPageObjMark_GetName(FPDF_PAGEOBJECTMARK mark, void* buffer, unsigned long buflen); +// Experimental API. // Get number of key/value pair parameters in |mark|. // // mark - handle to a content mark. @@ -324,6 +328,45 @@ FPDFPageObjMark_GetName(FPDF_PAGEOBJECTMARK mark, FPDF_EXPORT int FPDF_CALLCONV FPDFPageObjMark_CountParams(FPDF_PAGEOBJECTMARK mark); +// Experimental API. +// Get the key of a property in a content mark. |buffer| is only modified if +// |buflen| is longer than the length of the key. +// +// mark - handle to a content mark. +// index - index of the property. +// buffer - buffer for holding the returned key in UTF16-LE. +// buflen - length of the buffer. +// +// Returns the length of the key. +FPDF_EXPORT unsigned long FPDF_CALLCONV +FPDFPageObjMark_GetParamKey(FPDF_PAGEOBJECTMARK mark, + unsigned long index, + void* buffer, + unsigned long buflen); + +// Experimental API. +// Get type of the value of a property in a content mark. +// +// mark - handle to a content mark. +// index - index of the property. +// +// Returns the type of the value, or FPDF_OBJECT_UNKNOWN in case of failure. +FPDF_EXPORT FPDF_OBJECT_TYPE FPDF_CALLCONV +FPDFPageObjMark_GetParamValueType(FPDF_PAGEOBJECTMARK mark, + unsigned long index); + +// Experimental API. +// Get value of an int property in a content mark. +// FPDFPageObjMark_GetParamValueType() should have returned FPDF_OBJECT_NUMBER +// for this property. +// +// mark - handle to a content mark. +// index - index of the property. +// +// Returns the int value, 0 in case of failure. +FPDF_EXPORT int FPDF_CALLCONV +FPDFPageObjMark_GetParamIntValue(FPDF_PAGEOBJECTMARK mark, unsigned long index); + // Load an image from a JPEG image file and then set it into |image_object|. // // pages - pointer to the start of all loaded pages, may be NULL. diff --git a/testing/resources/text_in_page_marked.in b/testing/resources/text_in_page_marked.in index e2387a440c..982fc7be7c 100644 --- a/testing/resources/text_in_page_marked.in +++ b/testing/resources/text_in_page_marked.in @@ -103,7 +103,7 @@ Tm /FXF2 9 Tf (Test 14) Tj ET Q q 0 0 0.733333 rg /FXE2 gs BT -0.143427 1.73091 -1.73091 -0.143427 97.1315 134.618 Tm /FXF1 9 Tf (Test 15) Tj ET Q -/Square <> BDC +/Square <> BDC q 0 0 0.788235 rg /FXE2 gs BT 0.43929 1.73472 -1.73472 0.43929 108.786 134.694 Tm /FXF2 9 Tf (Test 16) Tj ET Q diff --git a/testing/resources/text_in_page_marked.pdf b/testing/resources/text_in_page_marked.pdf index 5f9467f61c..1f9e54f575 100644 --- a/testing/resources/text_in_page_marked.pdf +++ b/testing/resources/text_in_page_marked.pdf @@ -104,7 +104,7 @@ Tm /FXF2 9 Tf (Test 14) Tj ET Q q 0 0 0.733333 rg /FXE2 gs BT -0.143427 1.73091 -1.73091 -0.143427 97.1315 134.618 Tm /FXF1 9 Tf (Test 15) Tj ET Q -/Square <> BDC +/Square <> BDC q 0 0 0.788235 rg /FXE2 gs BT 0.43929 1.73472 -1.73472 0.43929 108.786 134.694 Tm /FXF2 9 Tf (Test 16) Tj ET Q -- cgit v1.2.3