7#include "public/fpdf_formfill.h"
12#include "constants/form_fields.h"
13#include "core/fpdfapi/page/cpdf_annotcontext.h"
14#include "core/fpdfapi/page/cpdf_occontext.h"
15#include "core/fpdfapi/page/cpdf_page.h"
16#include "core/fpdfapi/parser/cpdf_dictionary.h"
17#include "core/fpdfapi/parser/cpdf_document.h"
18#include "core/fpdfapi/parser/cpdf_stream.h"
19#include "core/fpdfapi/render/cpdf_renderoptions.h"
20#include "core/fpdfdoc/cpdf_formcontrol.h"
21#include "core/fpdfdoc/cpdf_formfield.h"
22#include "core/fpdfdoc/cpdf_interactiveform.h"
23#include "core/fxge/cfx_defaultrenderdevice.h"
24#include "core/fxge/dib/cfx_dibitmap.h"
25#include "fpdfsdk/cpdfsdk_annot.h"
26#include "fpdfsdk/cpdfsdk_formfillenvironment.h"
27#include "fpdfsdk/cpdfsdk_helpers.h"
28#include "fpdfsdk/cpdfsdk_interactiveform.h"
29#include "fpdfsdk/cpdfsdk_pageview.h"
30#include "public/fpdfview.h"
33#include "fpdfsdk/fpdfxfa/cpdfxfa_context.h"
34#include "fpdfsdk/fpdfxfa/cpdfxfa_page.h"
37#if defined(PDF_USE_SKIA)
42static_assert(
static_cast<
int>(AlertButton::kDefault) ==
43 JSPLATFORM_ALERT_BUTTON_DEFAULT,
44 "Default alert button types must match");
45static_assert(
static_cast<
int>(AlertButton::kOK) == JSPLATFORM_ALERT_BUTTON_OK,
46 "OK alert button types must match");
47static_assert(
static_cast<
int>(AlertButton::kOKCancel) ==
48 JSPLATFORM_ALERT_BUTTON_OKCANCEL,
49 "OKCancel alert button types must match");
50static_assert(
static_cast<
int>(AlertButton::kYesNo) ==
51 JSPLATFORM_ALERT_BUTTON_YESNO,
52 "YesNo alert button types must match");
53static_assert(
static_cast<
int>(AlertButton::kYesNoCancel) ==
54 JSPLATFORM_ALERT_BUTTON_YESNOCANCEL,
55 "YesNoCancel alert button types must match");
57static_assert(
static_cast<
int>(AlertIcon::kDefault) ==
58 JSPLATFORM_ALERT_ICON_DEFAULT,
59 "Default alert icon types must match");
60static_assert(
static_cast<
int>(AlertIcon::kError) ==
61 JSPLATFORM_ALERT_ICON_ERROR,
62 "Error alert icon types must match");
63static_assert(
static_cast<
int>(AlertIcon::kWarning) ==
64 JSPLATFORM_ALERT_ICON_WARNING,
65 "Warning alert icon types must match");
66static_assert(
static_cast<
int>(AlertIcon::kQuestion) ==
67 JSPLATFORM_ALERT_ICON_QUESTION,
68 "Question alert icon types must match");
69static_assert(
static_cast<
int>(AlertIcon::kStatus) ==
70 JSPLATFORM_ALERT_ICON_STATUS,
71 "Status alert icon types must match");
72static_assert(
static_cast<
int>(AlertIcon::kAsterisk) ==
73 JSPLATFORM_ALERT_ICON_ASTERISK,
74 "Asterisk alert icon types must match");
76static_assert(
static_cast<
int>(AlertReturn::kOK) == JSPLATFORM_ALERT_RETURN_OK,
77 "OK alert return types must match");
78static_assert(
static_cast<
int>(AlertReturn::kCancel) ==
79 JSPLATFORM_ALERT_RETURN_CANCEL,
80 "Cancel alert return types must match");
81static_assert(
static_cast<
int>(AlertReturn::kNo) == JSPLATFORM_ALERT_RETURN_NO,
82 "No alert return types must match");
83static_assert(
static_cast<
int>(AlertReturn::kYes) ==
84 JSPLATFORM_ALERT_RETURN_YES,
85 "Yes alert return types must match");
87static_assert(
static_cast<
int>(FormType::kNone) == FORMTYPE_NONE,
88 "None form types must match");
89static_assert(
static_cast<
int>(FormType::kAcroForm) == FORMTYPE_ACRO_FORM,
90 "AcroForm form types must match");
91static_assert(
static_cast<
int>(FormType::kXFAFull) == FORMTYPE_XFA_FULL,
92 "XFA full form types must match");
93static_assert(
static_cast<
int>(FormType::kXFAForeground) ==
94 FORMTYPE_XFA_FOREGROUND,
95 "XFA foreground form types must match");
100 "Unknown form field types must match");
103 "PushButton form field types must match");
106 "CheckBox form field types must match");
109 "RadioButton form field types must match");
112 "ComboBox form field types must match");
115 "ListBox form field types must match");
118 "TextField form field types must match");
121 "Signature form field types must match");
123static_assert(
static_cast<
int>(FormFieldType::kXFA) == FPDF_FORMFIELD_XFA,
124 "XFA form field types must match");
125static_assert(
static_cast<
int>(FormFieldType::kXFA_CheckBox) ==
126 FPDF_FORMFIELD_XFA_CHECKBOX,
127 "XFA CheckBox form field types must match");
128static_assert(
static_cast<
int>(FormFieldType::kXFA_ComboBox) ==
129 FPDF_FORMFIELD_XFA_COMBOBOX,
130 "XFA ComboBox form field types must match");
131static_assert(
static_cast<
int>(FormFieldType::kXFA_ImageField) ==
132 FPDF_FORMFIELD_XFA_IMAGEFIELD,
133 "XFA ImageField form field types must match");
134static_assert(
static_cast<
int>(FormFieldType::kXFA_ListBox) ==
135 FPDF_FORMFIELD_XFA_LISTBOX,
136 "XFA ListBox form field types must match");
137static_assert(
static_cast<
int>(FormFieldType::kXFA_PushButton) ==
138 FPDF_FORMFIELD_XFA_PUSHBUTTON,
139 "XFA PushButton form field types must match");
140static_assert(
static_cast<
int>(FormFieldType::kXFA_Signature) ==
141 FPDF_FORMFIELD_XFA_SIGNATURE,
142 "XFA Signature form field types must match");
143static_assert(
static_cast<
int>(FormFieldType::kXFA_TextField) ==
144 FPDF_FORMFIELD_XFA_TEXTFIELD,
145 "XFA TextField form field types must match");
148 "Number of form field types must match");
152 "CloseDocument action must match");
155 "SaveDocument action must match");
158 "DocumentSaved action must match");
161 "PrintDocument action must match");
164 "DocumentPrinted action must match");
168CPDFSDK_PageView* FormHandleToPageView(FPDF_FORMHANDLE hHandle,
169 FPDF_PAGE fpdf_page) {
174 CPDFSDK_FormFillEnvironment* pFormFillEnv =
179void FFLCommon(FPDF_FORMHANDLE hHandle,
181 FPDF_SKIA_CANVAS canvas,
197 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, fpdf_page);
199 const FX_RECT rect
(start_x
, start_y
, start_x + size_x
, start_y + size_y
);
202 auto pDevice =
std::make_unique<CFX_DefaultRenderDevice>();
203#if defined(PDF_USE_SKIA)
204 if (CFX_DefaultRenderDevice::UseSkiaRenderer() && canvas) {
205 pDevice->AttachCanvas(
reinterpret_cast<SkCanvas*>(canvas));
209 RetainPtr<CFX_DIBitmap> holder(CFXDIBitmapFromFPDFBitmap(bitmap));
213 pDevice->SetClip_Rect(rect);
224 pdfium::MakeRetain<CPDF_OCContext>(pPDFDoc, CPDF_OCContext
::kView)
);
233bool CheckFormfillVersion(FPDF_FORMFILLINFO* formInfo) {
238 if (formInfo->version != 2)
261 CFX_PointF(
static_cast<
float>(page_x),
static_cast<
float>(page_y))
,
270 const CPDFXFA_Page* pXFAPage = ToXFAPage(IPDFPageFromFPDFPage(page));
272 return pXFAPage->HasFormFieldAtPoint(
273 CFX_PointF(
static_cast<
float>(page_x),
static_cast<
float>(page_y)));
296 pPage
, CFX_PointF(
static_cast<
float>(page_x),
static_cast<
float>(page_y))
,
303 FPDF_FORMFILLINFO* formInfo) {
304 if (!CheckFormfillVersion(formInfo))
312 CPDFXFA_Context* pContext =
nullptr;
313 if (!formInfo->xfa_disabled) {
314 if (!pDocument->GetExtension()) {
315 pDocument->SetExtension(std::make_unique<CPDFXFA_Context>(pDocument));
321 pContext =
static_cast<CPDFXFA_Context*>(pDocument->GetExtension());
322 if (pContext->GetFormFillEnv()) {
323 return FPDFFormHandleFromCPDFSDKFormFillEnvironment(
324 pContext->GetFormFillEnv());
330 std::make_unique<CPDFSDK_FormFillEnvironment>(pDocument, formInfo);
334 pContext->SetFormFillEnv(pFormFillEnv.get());
339 return FPDFFormHandleFromCPDFSDKFormFillEnvironment(
340 pFormFillEnv.release());
350 std::unique_ptr<CPDFSDK_FormFillEnvironment> pFormFillEnv(
351 CPDFSDKFormFillEnvironmentFromFPDFFormHandle(hHandle));
356 pFormFillEnv->ClearAllFocusedAnnots();
360 static_cast<CPDFXFA_Context*>(pFormFillEnv->GetDocExtension());
362 pContext->SetFormFillEnv(
nullptr);
371 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
373 pPageView->OnMouseMove(
374 Mask<FWL_EVENTFLAG>::FromUnderlyingUnchecked(modifier),
375 CFX_PointF(page_x, page_y));
382 const FS_POINTF* page_coord,
388 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
390 pPageView->OnMouseWheel(
391 Mask<FWL_EVENTFLAG>::FromUnderlyingUnchecked(modifier),
392 CFXPointFFromFSPointF(*page_coord), CFX_Vector(delta_x, delta_y));
400 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
403 Mask<FWL_EVENTFLAG>::FromUnderlyingUnchecked(modifier),
404 CFX_PointF(page_x, page_y));
412#ifdef PDF_ENABLE_CLICK_LOGGING
413 fprintf(stderr,
"mousedown,left,%d,%d\n",
static_cast<
int>(round(page_x)),
414 static_cast<
int>(round(page_y)));
416 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
418 pPageView->OnLButtonDown(
419 Mask<FWL_EVENTFLAG>::FromUnderlyingUnchecked(modifier),
420 CFX_PointF(page_x, page_y));
428#ifdef PDF_ENABLE_CLICK_LOGGING
429 fprintf(stderr,
"mouseup,left,%d,%d\n",
static_cast<
int>(round(page_x)),
430 static_cast<
int>(round(page_y)));
432 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
434 pPageView->OnLButtonUp(
435 Mask<FWL_EVENTFLAG>::FromUnderlyingUnchecked(modifier),
436 CFX_PointF(page_x, page_y));
445#ifdef PDF_ENABLE_CLICK_LOGGING
446 fprintf(stderr,
"mousedown,doubleleft,%d,%d\n",
447 static_cast<
int>(round(page_x)),
static_cast<
int>(round(page_y)));
449 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
451 pPageView->OnLButtonDblClk(
452 Mask<FWL_EVENTFLAG>::FromUnderlyingUnchecked(modifier),
453 CFX_PointF(page_x, page_y));
461#ifdef PDF_ENABLE_CLICK_LOGGING
462 fprintf(stderr,
"mousedown,right,%d,%d\n",
static_cast<
int>(round(page_x)),
463 static_cast<
int>(round(page_y)));
465 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
467 pPageView->OnRButtonDown(
468 Mask<FWL_EVENTFLAG>::FromUnderlyingUnchecked(modifier),
469 CFX_PointF(page_x, page_y));
477#ifdef PDF_ENABLE_CLICK_LOGGING
478 fprintf(stderr,
"mouseup,right,%d,%d\n",
static_cast<
int>(round(page_x)),
479 static_cast<
int>(round(page_y)));
481 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
483 pPageView->OnRButtonUp(
484 Mask<FWL_EVENTFLAG>::FromUnderlyingUnchecked(modifier),
485 CFX_PointF(page_x, page_y));
492 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
494 pPageView->OnKeyDown(
495 static_cast<FWL_VKEYCODE>(nKeyCode),
496 Mask<FWL_EVENTFLAG>::FromUnderlyingUnchecked(modifier));
510 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
513 nChar, Mask<FWL_EVENTFLAG>::FromUnderlyingUnchecked(modifier));
520 unsigned long buflen) {
521 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
533 unsigned long buflen) {
534 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
545 FPDF_WIDESTRING wsText) {
546 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
555 FPDF_WIDESTRING wsText) {
556 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
565 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
571 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
579 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
587 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
595 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
603 CPDFSDK_FormFillEnvironment* pFormFillEnv =
613 FPDF_ANNOTATION* annot) {
614 if (!page_index || !annot)
617 CPDFSDK_FormFillEnvironment* form_fill_env =
644 cpdfsdk_annot->GetPDFAnnot()->GetMutableAnnotDict();
650 *annot = FPDFAnnotationFromCPDFAnnotContext(annot_context.release());
656 CPDFSDK_FormFillEnvironment* form_fill_env =
665 CPDFSDK_PageView* page_view =
670 RetainPtr<CPDF_Dictionary> annot_dict = annot_context->GetMutableAnnotDict();
672 page_view->GetAnnotByDict(annot_dict.Get()));
688 FFLCommon(hHandle, bitmap,
nullptr, page, start_x, start_y, size_x, size_y,
692#if defined(PDF_USE_SKIA)
693FPDF_EXPORT
void FPDF_CALLCONV FPDF_FFLDrawSkia(FPDF_FORMHANDLE hHandle,
694 FPDF_SKIA_CANVAS canvas,
702 FFLCommon(hHandle,
nullptr, canvas, page, start_x, start_y, size_x, size_y,
710 unsigned long color) {
715 absl::optional<FormFieldType> cast_input =
716 CPDF_FormField::IntToFormFieldType(fieldType);
717 if (!cast_input.has_value())
720 if (cast_input.value() == FormFieldType::kUnknown) {
723 pForm->SetHighlightColor(
static_cast<FX_COLORREF>(color),
741 FPDF_FORMHANDLE hHandle) {
742 if (CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page))
747 FPDF_FORMHANDLE hHandle) {
748 CPDFSDK_FormFillEnvironment* pFormFillEnv =
767 CPDFSDK_FormFillEnvironment* pFormFillEnv =
775 CPDFSDK_FormFillEnvironment* pFormFillEnv =
783 CPDFSDK_FormFillEnvironment* pFormFillEnv =
800 FPDF_FORMHANDLE hHandle,
802 CPDFSDK_FormFillEnvironment* pFormFillEnv =
827 FPDF_BOOL selected) {
828 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
834 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
CPDFSDK_PageView * GetPageView() const
virtual CPDFXFA_Widget * AsXFAWidget()
void SetValid(bool bValid)
void ReplaceSelection(const WideString &text)
void PageView_OnDraw(CFX_RenderDevice *pDevice, const CFX_Matrix &mtUser2Device, CPDF_RenderOptions *pOptions, const FX_RECT &pClip)
WideString GetFocusedFormText()
WideString GetSelectedText()
void ReplaceAndKeepSelection(const WideString &text)
bool IsIndexSelected(int index)
bool SetIndexSelected(int index, bool selected)
bool ActionExist(AActionType eType) const
CPDF_Action GetAction(AActionType eType) const
IPDF_Page * GetPage() const
RetainPtr< const CPDF_Dictionary > GetDictFor(const ByteString &key) const
const CPDF_Dictionary * GetRoot() const
void SetOCContext(RetainPtr< CPDF_OCContext > context)
void SetDrawAnnots(bool draw)
void SetColorMode(Type mode)
virtual CPDF_Document * GetDocument() const =0
virtual CFX_Matrix GetDisplayMatrix(const FX_RECT &rect, int iRotate) const =0
WideString WideStringFromFPDFWideString(FPDF_WIDESTRING wide_string)
IPDF_Page * IPDFPageFromFPDFPage(FPDF_PAGE page)
unsigned long Utf16EncodeMaybeCopyAndReturnLength(const WideString &text, void *buffer, unsigned long buflen)
CPDFSDK_FormFillEnvironment * CPDFSDKFormFillEnvironmentFromFPDFFormHandle(FPDF_FORMHANDLE handle)
CPDF_AnnotContext * CPDFAnnotContextFromFPDFAnnotation(FPDF_ANNOTATION annot)
void ReportUnsupportedXFA(const CPDF_Document *pDoc)
CPDF_Page * CPDFPageFromFPDFPage(FPDF_PAGE page)
CPDF_Document * CPDFDocumentFromFPDFDocument(FPDF_DOCUMENT doc)
CPDFSDK_InteractiveForm * FormHandleToInteractiveForm(FPDF_FORMHANDLE hHandle)
#define FPDF_REVERSE_BYTE_ORDER
constexpr FX_RECT(int l, int t, int r, int b)