Move get form field logic to common method in fpdf_annot.h
Multiple APIs in fpdf_annot.h uses FPDF_FORMHANDLE and FPDF_ANNOTATION
to get the reference of CPDF_FormField. Moving this common logic to a
separate method GetFormField() to avoid duplication.
Change-Id: Ie983b328e7710e7220e429b88cb14e74ee295b19
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/65470
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
diff --git a/fpdfsdk/fpdf_annot.cpp b/fpdfsdk/fpdf_annot.cpp
index 2ef525b..923bcff 100644
--- a/fpdfsdk/fpdf_annot.cpp
+++ b/fpdfsdk/fpdf_annot.cpp
@@ -256,6 +256,19 @@
return pResourceDict;
}
+CPDF_FormField* GetFormField(FPDF_FORMHANDLE hHandle, FPDF_ANNOTATION annot) {
+ CPDF_Dictionary* pAnnotDict = GetAnnotDictFromFPDFAnnotation(annot);
+ if (!pAnnotDict)
+ return nullptr;
+
+ CPDFSDK_InteractiveForm* pForm = FormHandleToInteractiveForm(hHandle);
+ if (!pForm)
+ return nullptr;
+
+ CPDF_InteractiveForm* pPDFForm = pForm->GetInteractiveForm();
+ return pPDFForm->GetFieldByDict(pAnnotDict);
+}
+
} // namespace
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
@@ -916,16 +929,7 @@
FPDF_EXPORT int FPDF_CALLCONV
FPDFAnnot_GetFormFieldFlags(FPDF_FORMHANDLE hHandle, FPDF_ANNOTATION annot) {
- CPDFSDK_InteractiveForm* pForm = FormHandleToInteractiveForm(hHandle);
- if (!pForm)
- return FPDF_FORMFLAG_NONE;
-
- CPDF_Dictionary* pAnnotDict = GetAnnotDictFromFPDFAnnotation(annot);
- if (!pAnnotDict)
- return FPDF_FORMFLAG_NONE;
-
- CPDF_InteractiveForm* pPDFForm = pForm->GetInteractiveForm();
- CPDF_FormField* pFormField = pPDFForm->GetFieldByDict(pAnnotDict);
+ CPDF_FormField* pFormField = GetFormField(hHandle, annot);
return pFormField ? pFormField->GetFieldFlags() : FPDF_FORMFLAG_NONE;
}
@@ -955,16 +959,7 @@
FPDF_EXPORT int FPDF_CALLCONV FPDFAnnot_GetOptionCount(FPDF_FORMHANDLE hHandle,
FPDF_ANNOTATION annot) {
- CPDFSDK_InteractiveForm* pForm = FormHandleToInteractiveForm(hHandle);
- if (!pForm)
- return -1;
-
- CPDF_Dictionary* pAnnotDict = GetAnnotDictFromFPDFAnnotation(annot);
- if (!pAnnotDict)
- return -1;
-
- CPDF_InteractiveForm* pPDFForm = pForm->GetInteractiveForm();
- CPDF_FormField* pFormField = pPDFForm->GetFieldByDict(pAnnotDict);
+ CPDF_FormField* pFormField = GetFormField(hHandle, annot);
return pFormField ? pFormField->CountOptions() : -1;
}
@@ -974,16 +969,10 @@
int index,
FPDF_WCHAR* buffer,
unsigned long buflen) {
- CPDFSDK_InteractiveForm* pForm = FormHandleToInteractiveForm(hHandle);
- if (!pForm)
+ if (index < 0)
return 0;
- CPDF_Dictionary* pAnnotDict = GetAnnotDictFromFPDFAnnotation(annot);
- if (!pAnnotDict || index < 0)
- return 0;
-
- CPDF_InteractiveForm* pPDFForm = pForm->GetInteractiveForm();
- CPDF_FormField* pFormField = pPDFForm->GetFieldByDict(pAnnotDict);
+ CPDF_FormField* pFormField = GetFormField(hHandle, annot);
if (!pFormField || index >= pFormField->CountOptions())
return 0;