From e10509a3d2a1641984845d579963d574305ea6cc Mon Sep 17 00:00:00 2001 From: Jane Liu Date: Tue, 20 Jun 2017 16:47:41 -0400 Subject: Wrapper class for annotation dictionary + CPDF_Form Simple wrapper class for annotation + its path parsing form object. This will be used in APIs for extracting and setting annotation paths. Bug=pdfium:737 Change-Id: I2e8131672d087613213735295c6d01e377b956e7 Reviewed-on: https://pdfium-review.googlesource.com/6730 Reviewed-by: dsinclair Commit-Queue: dsinclair --- fpdfsdk/fpdfannot.cpp | 86 +++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 73 insertions(+), 13 deletions(-) (limited to 'fpdfsdk/fpdfannot.cpp') diff --git a/fpdfsdk/fpdfannot.cpp b/fpdfsdk/fpdfannot.cpp index 90927fd396..6d47b04be2 100644 --- a/fpdfsdk/fpdfannot.cpp +++ b/fpdfsdk/fpdfannot.cpp @@ -8,6 +8,7 @@ #include +#include "core/fpdfapi/page/cpdf_form.h" #include "core/fpdfapi/page/cpdf_page.h" #include "core/fpdfapi/parser/cpdf_array.h" #include "core/fpdfapi/parser/cpdf_dictionary.h" @@ -20,6 +21,8 @@ #include "core/fpdfdoc/cpvt_generateap.h" #include "fpdfsdk/fsdk_define.h" +namespace { + // These checks ensure the consistency of annotation subtype values across core/ // and public. static_assert(static_cast(CPDF_Annot::Subtype::UNKNOWN) == @@ -98,6 +101,46 @@ static_assert(static_cast(CPDF_Annot::Subtype::XFAWIDGET) == FPDF_ANNOT_XFAWIDGET, "CPDF_Annot::XFAWIDGET value mismatch"); +class CPDF_AnnotContext { + public: + CPDF_AnnotContext(CPDF_Dictionary* pAnnotDict, + CPDF_Page* pPage, + CPDF_Stream* pStream) + : m_pAnnotDict(pAnnotDict), m_pPage(pPage) { + SetForm(pStream); + } + ~CPDF_AnnotContext() {} + + bool HasForm() const { return !!m_pAnnotForm; } + + void SetForm(CPDF_Stream* pStream) { + if (!pStream) + return; + + m_pAnnotForm = pdfium::MakeUnique( + m_pPage->m_pDocument.Get(), m_pPage->m_pResources.Get(), pStream); + m_pAnnotForm->ParseContent(nullptr, nullptr, nullptr); + } + + CPDF_Form* GetForm() const { return m_pAnnotForm.get(); } + CPDF_Dictionary* GetAnnotDict() const { return m_pAnnotDict.Get(); } + + private: + std::unique_ptr m_pAnnotForm; + CFX_UnownedPtr m_pAnnotDict; + CFX_UnownedPtr m_pPage; +}; + +FPDF_ANNOTATION FPDFAnnotationFromCPDFAnnotContext(CPDF_AnnotContext* pAnnot) { + return static_cast(pAnnot); +} + +CPDF_AnnotContext* CPDFAnnotContextFromFPDFAnnotation(FPDF_ANNOTATION annot) { + return static_cast(annot); +} + +} // namespace + DLLEXPORT FPDF_BOOL STDCALL FPDFAnnot_IsSupportedSubtype(FPDF_ANNOTATION_SUBTYPE subtype) { return subtype == FPDF_ANNOT_CIRCLE || subtype == FPDF_ANNOT_HIGHLIGHT || @@ -121,8 +164,11 @@ FPDFPage_CreateAnnot(FPDF_PAGE page, pDict->SetNewFor("Subtype", CPDF_Annot::AnnotSubtypeToString( static_cast(subtype))); - if (annot) - *annot = FPDFAnnotationFromCPDFDictionary(pDict.get()); + if (annot) { + auto pNewAnnot = + pdfium::MakeUnique(pDict.get(), pPage, nullptr); + *annot = FPDFAnnotationFromCPDFAnnotContext(pNewAnnot.release()); + } CPDF_Array* pAnnotList = pPage->m_pFormDict->GetArrayFor("Annots"); if (!pAnnotList) @@ -153,13 +199,19 @@ DLLEXPORT FPDF_BOOL STDCALL FPDFPage_GetAnnot(FPDF_PAGE page, return false; CPDF_Dictionary* pDict = ToDictionary(pAnnots->GetDirectObjectAt(index)); - *annot = FPDFAnnotationFromCPDFDictionary(pDict); - return *annot ? true : false; + auto pNewAnnot = pdfium::MakeUnique(pDict, pPage, nullptr); + *annot = FPDFAnnotationFromCPDFAnnotContext(pNewAnnot.release()); + return true; +} + +DLLEXPORT void STDCALL FPDFPage_CloseAnnot(FPDF_ANNOTATION annot) { + delete CPDFAnnotContextFromFPDFAnnotation(annot); } DLLEXPORT FPDF_ANNOTATION_SUBTYPE STDCALL FPDFAnnot_GetSubtype(FPDF_ANNOTATION annot) { - CPDF_Dictionary* pAnnotDict = CPDFDictionaryFromFPDFAnnotation(annot); + CPDF_Dictionary* pAnnotDict = + CPDFAnnotContextFromFPDFAnnotation(annot)->GetAnnotDict(); if (!pAnnotDict) return FPDF_ANNOT_UNKNOWN; @@ -173,7 +225,8 @@ DLLEXPORT FPDF_BOOL STDCALL FPDFAnnot_SetColor(FPDF_ANNOTATION annot, unsigned int G, unsigned int B, unsigned int A) { - CPDF_Dictionary* pAnnotDict = CPDFDictionaryFromFPDFAnnotation(annot); + CPDF_Dictionary* pAnnotDict = + CPDFAnnotContextFromFPDFAnnotation(annot)->GetAnnotDict(); if (!pAnnotDict || R > 255 || G > 255 || B > 255 || A > 255) return false; @@ -201,7 +254,8 @@ DLLEXPORT FPDF_BOOL STDCALL FPDFAnnot_GetColor(FPDF_ANNOTATION annot, unsigned int* G, unsigned int* B, unsigned int* A) { - CPDF_Dictionary* pAnnotDict = CPDFDictionaryFromFPDFAnnotation(annot); + CPDF_Dictionary* pAnnotDict = + CPDFAnnotContextFromFPDFAnnotation(annot)->GetAnnotDict(); if (!pAnnotDict || !R || !G || !B || !A) return false; @@ -267,7 +321,8 @@ FPDFAnnot_SetAttachmentPoints(FPDF_ANNOTATION annot, if (!annot || !FPDFAnnot_HasAttachmentPoints(annot)) return false; - CPDF_Dictionary* pAnnotDict = CPDFDictionaryFromFPDFAnnotation(annot); + CPDF_Dictionary* pAnnotDict = + CPDFAnnotContextFromFPDFAnnotation(annot)->GetAnnotDict(); CPDF_Array* pQuadPoints = pAnnotDict->GetArrayFor("QuadPoints"); if (pQuadPoints) pQuadPoints->Clear(); @@ -292,7 +347,8 @@ FPDFAnnot_GetAttachmentPoints(FPDF_ANNOTATION annot, return false; CPDF_Array* pArray = - CPDFDictionaryFromFPDFAnnotation(annot)->GetArrayFor("QuadPoints"); + CPDFAnnotContextFromFPDFAnnotation(annot)->GetAnnotDict()->GetArrayFor( + "QuadPoints"); if (!pArray) return false; @@ -309,7 +365,8 @@ FPDFAnnot_GetAttachmentPoints(FPDF_ANNOTATION annot, DLLEXPORT FPDF_BOOL STDCALL FPDFAnnot_SetRect(FPDF_ANNOTATION annot, FS_RECTF rect) { - CPDF_Dictionary* pAnnotDict = CPDFDictionaryFromFPDFAnnotation(annot); + CPDF_Dictionary* pAnnotDict = + CPDFAnnotContextFromFPDFAnnotation(annot)->GetAnnotDict(); if (!pAnnotDict) return false; @@ -328,7 +385,8 @@ DLLEXPORT FPDF_BOOL STDCALL FPDFAnnot_SetRect(FPDF_ANNOTATION annot, DLLEXPORT FPDF_BOOL STDCALL FPDFAnnot_GetRect(FPDF_ANNOTATION annot, FS_RECTF* rect) { - CPDF_Dictionary* pAnnotDict = CPDFDictionaryFromFPDFAnnotation(annot); + CPDF_Dictionary* pAnnotDict = + CPDFAnnotContextFromFPDFAnnotation(annot)->GetAnnotDict(); if (!rect || !pAnnotDict) return false; @@ -346,7 +404,8 @@ DLLEXPORT FPDF_BOOL STDCALL FPDFAnnot_GetRect(FPDF_ANNOTATION annot, DLLEXPORT FPDF_BOOL STDCALL FPDFAnnot_SetText(FPDF_ANNOTATION annot, FPDFANNOT_TEXTTYPE type, FPDF_WIDESTRING text) { - CPDF_Dictionary* pAnnotDict = CPDFDictionaryFromFPDFAnnotation(annot); + CPDF_Dictionary* pAnnotDict = + CPDFAnnotContextFromFPDFAnnotation(annot)->GetAnnotDict(); if (!pAnnotDict) return false; @@ -361,7 +420,8 @@ DLLEXPORT unsigned long STDCALL FPDFAnnot_GetText(FPDF_ANNOTATION annot, FPDFANNOT_TEXTTYPE type, void* buffer, unsigned long buflen) { - CPDF_Dictionary* pAnnotDict = CPDFDictionaryFromFPDFAnnotation(annot); + CPDF_Dictionary* pAnnotDict = + CPDFAnnotContextFromFPDFAnnotation(annot)->GetAnnotDict(); if (!pAnnotDict) return 0; -- cgit v1.2.3