summaryrefslogtreecommitdiff
path: root/fpdfsdk/fpdfannot.cpp
diff options
context:
space:
mode:
authorJane Liu <janeliulwq@google.com>2017-06-20 16:47:41 -0400
committerChromium commit bot <commit-bot@chromium.org>2017-06-20 21:04:49 +0000
commite10509a3d2a1641984845d579963d574305ea6cc (patch)
tree4a18e88dfd833279e2bd22590f16a317f4c6e812 /fpdfsdk/fpdfannot.cpp
parentd5a13afe9265463a497db40d8c66a8a736a86bc2 (diff)
downloadpdfium-e10509a3d2a1641984845d579963d574305ea6cc.tar.xz
Wrapper class for annotation dictionary + CPDF_Formchromium/3137
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 <dsinclair@chromium.org> Commit-Queue: dsinclair <dsinclair@chromium.org>
Diffstat (limited to 'fpdfsdk/fpdfannot.cpp')
-rw-r--r--fpdfsdk/fpdfannot.cpp86
1 files changed, 73 insertions, 13 deletions
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 <utility>
+#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<int>(CPDF_Annot::Subtype::UNKNOWN) ==
@@ -98,6 +101,46 @@ static_assert(static_cast<int>(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<CPDF_Form>(
+ 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<CPDF_Form> m_pAnnotForm;
+ CFX_UnownedPtr<CPDF_Dictionary> m_pAnnotDict;
+ CFX_UnownedPtr<CPDF_Page> m_pPage;
+};
+
+FPDF_ANNOTATION FPDFAnnotationFromCPDFAnnotContext(CPDF_AnnotContext* pAnnot) {
+ return static_cast<FPDF_ANNOTATION>(pAnnot);
+}
+
+CPDF_AnnotContext* CPDFAnnotContextFromFPDFAnnotation(FPDF_ANNOTATION annot) {
+ return static_cast<CPDF_AnnotContext*>(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<CPDF_Name>("Subtype",
CPDF_Annot::AnnotSubtypeToString(
static_cast<CPDF_Annot::Subtype>(subtype)));
- if (annot)
- *annot = FPDFAnnotationFromCPDFDictionary(pDict.get());
+ if (annot) {
+ auto pNewAnnot =
+ pdfium::MakeUnique<CPDF_AnnotContext>(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<CPDF_AnnotContext>(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;