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"
31#include "third_party/abseil-cpp/absl/types/variant.h"
34#include "fpdfsdk/fpdfxfa/cpdfxfa_context.h"
35#include "fpdfsdk/fpdfxfa/cpdfxfa_page.h"
38#if defined(PDF_USE_SKIA)
43static_assert(
static_cast<
int>(AlertButton::kDefault) ==
44 JSPLATFORM_ALERT_BUTTON_DEFAULT,
45 "Default alert button types must match");
46static_assert(
static_cast<
int>(AlertButton::kOK) == JSPLATFORM_ALERT_BUTTON_OK,
47 "OK alert button types must match");
48static_assert(
static_cast<
int>(AlertButton::kOKCancel) ==
49 JSPLATFORM_ALERT_BUTTON_OKCANCEL,
50 "OKCancel alert button types must match");
51static_assert(
static_cast<
int>(AlertButton::kYesNo) ==
52 JSPLATFORM_ALERT_BUTTON_YESNO,
53 "YesNo alert button types must match");
54static_assert(
static_cast<
int>(AlertButton::kYesNoCancel) ==
55 JSPLATFORM_ALERT_BUTTON_YESNOCANCEL,
56 "YesNoCancel alert button types must match");
58static_assert(
static_cast<
int>(AlertIcon::kDefault) ==
59 JSPLATFORM_ALERT_ICON_DEFAULT,
60 "Default alert icon types must match");
61static_assert(
static_cast<
int>(AlertIcon::kError) ==
62 JSPLATFORM_ALERT_ICON_ERROR,
63 "Error alert icon types must match");
64static_assert(
static_cast<
int>(AlertIcon::kWarning) ==
65 JSPLATFORM_ALERT_ICON_WARNING,
66 "Warning alert icon types must match");
67static_assert(
static_cast<
int>(AlertIcon::kQuestion) ==
68 JSPLATFORM_ALERT_ICON_QUESTION,
69 "Question alert icon types must match");
70static_assert(
static_cast<
int>(AlertIcon::kStatus) ==
71 JSPLATFORM_ALERT_ICON_STATUS,
72 "Status alert icon types must match");
73static_assert(
static_cast<
int>(AlertIcon::kAsterisk) ==
74 JSPLATFORM_ALERT_ICON_ASTERISK,
75 "Asterisk alert icon types must match");
77static_assert(
static_cast<
int>(AlertReturn::kOK) == JSPLATFORM_ALERT_RETURN_OK,
78 "OK alert return types must match");
79static_assert(
static_cast<
int>(AlertReturn::kCancel) ==
80 JSPLATFORM_ALERT_RETURN_CANCEL,
81 "Cancel alert return types must match");
82static_assert(
static_cast<
int>(AlertReturn::kNo) == JSPLATFORM_ALERT_RETURN_NO,
83 "No alert return types must match");
84static_assert(
static_cast<
int>(AlertReturn::kYes) ==
85 JSPLATFORM_ALERT_RETURN_YES,
86 "Yes alert return types must match");
88static_assert(
static_cast<
int>(FormType::kNone) == FORMTYPE_NONE,
89 "None form types must match");
90static_assert(
static_cast<
int>(FormType::kAcroForm) == FORMTYPE_ACRO_FORM,
91 "AcroForm form types must match");
92static_assert(
static_cast<
int>(FormType::kXFAFull) == FORMTYPE_XFA_FULL,
93 "XFA full form types must match");
94static_assert(
static_cast<
int>(FormType::kXFAForeground) ==
95 FORMTYPE_XFA_FOREGROUND,
96 "XFA foreground form types must match");
101 "Unknown form field types must match");
104 "PushButton form field types must match");
107 "CheckBox form field types must match");
110 "RadioButton form field types must match");
113 "ComboBox form field types must match");
116 "ListBox form field types must match");
119 "TextField form field types must match");
122 "Signature form field types must match");
124static_assert(
static_cast<
int>(FormFieldType::kXFA) == FPDF_FORMFIELD_XFA,
125 "XFA form field types must match");
126static_assert(
static_cast<
int>(FormFieldType::kXFA_CheckBox) ==
127 FPDF_FORMFIELD_XFA_CHECKBOX,
128 "XFA CheckBox form field types must match");
129static_assert(
static_cast<
int>(FormFieldType::kXFA_ComboBox) ==
130 FPDF_FORMFIELD_XFA_COMBOBOX,
131 "XFA ComboBox form field types must match");
132static_assert(
static_cast<
int>(FormFieldType::kXFA_ImageField) ==
133 FPDF_FORMFIELD_XFA_IMAGEFIELD,
134 "XFA ImageField form field types must match");
135static_assert(
static_cast<
int>(FormFieldType::kXFA_ListBox) ==
136 FPDF_FORMFIELD_XFA_LISTBOX,
137 "XFA ListBox form field types must match");
138static_assert(
static_cast<
int>(FormFieldType::kXFA_PushButton) ==
139 FPDF_FORMFIELD_XFA_PUSHBUTTON,
140 "XFA PushButton form field types must match");
141static_assert(
static_cast<
int>(FormFieldType::kXFA_Signature) ==
142 FPDF_FORMFIELD_XFA_SIGNATURE,
143 "XFA Signature form field types must match");
144static_assert(
static_cast<
int>(FormFieldType::kXFA_TextField) ==
145 FPDF_FORMFIELD_XFA_TEXTFIELD,
146 "XFA TextField form field types must match");
149 "Number of form field types must match");
153 "CloseDocument action must match");
156 "SaveDocument action must match");
159 "DocumentSaved action must match");
162 "PrintDocument action must match");
165 "DocumentPrinted action must match");
169CPDFSDK_PageView* FormHandleToPageView(FPDF_FORMHANDLE hHandle,
170 FPDF_PAGE fpdf_page) {
175 CPDFSDK_FormFillEnvironment* pFormFillEnv =
180#if defined(PDF_USE_SKIA)
181using BitmapOrCanvas = absl::variant<CFX_DIBitmap*, SkCanvas*>;
183using BitmapOrCanvas = absl::variant<CFX_DIBitmap*>;
187void FFLCommon(FPDF_FORMHANDLE hHandle,
206#if defined(PDF_USE_SKIA)
207 SkCanvas* canvas =
nullptr;
210 const bool dest_is_bitmap = absl::holds_alternative<CFX_DIBitmap*>(dest);
211 if (dest_is_bitmap) {
212 holder.Reset(absl::get<CFX_DIBitmap*>(dest));
215#if defined(PDF_USE_SKIA)
216 if (!CFX_DefaultRenderDevice::UseSkiaRenderer()) {
220 canvas = absl::get<SkCanvas*>(dest);
226 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, fpdf_page);
228 const FX_RECT rect
(start_x
, start_y
, start_x + size_x
, start_y + size_y
);
231 auto pDevice =
std::make_unique<CFX_DefaultRenderDevice>();
232 if (dest_is_bitmap) {
233 if (!pDevice->AttachWithRgbByteOrder(holder,
238#if defined(PDF_USE_SKIA)
239 if (!pDevice->AttachCanvas(*canvas)) {
247 pDevice->SetClip_Rect(rect);
258 pdfium::MakeRetain<CPDF_OCContext>(pPDFDoc, CPDF_OCContext
::kView)
);
267bool CheckFormfillVersion(FPDF_FORMFILLINFO* formInfo) {
272 if (formInfo->version != 2)
295 CFX_PointF(
static_cast<
float>(page_x),
static_cast<
float>(page_y))
,
304 const CPDFXFA_Page* pXFAPage = ToXFAPage(IPDFPageFromFPDFPage(page));
306 return pXFAPage->HasFormFieldAtPoint(
307 CFX_PointF(
static_cast<
float>(page_x),
static_cast<
float>(page_y)));
330 pPage
, CFX_PointF(
static_cast<
float>(page_x),
static_cast<
float>(page_y))
,
337 FPDF_FORMFILLINFO* formInfo) {
338 if (!CheckFormfillVersion(formInfo))
346 CPDFXFA_Context* pContext =
nullptr;
347 if (!formInfo->xfa_disabled) {
348 if (!pDocument->GetExtension()) {
349 pDocument->SetExtension(std::make_unique<CPDFXFA_Context>(pDocument));
355 pContext =
static_cast<CPDFXFA_Context*>(pDocument->GetExtension());
356 if (pContext->GetFormFillEnv()) {
357 return FPDFFormHandleFromCPDFSDKFormFillEnvironment(
358 pContext->GetFormFillEnv());
364 std::make_unique<CPDFSDK_FormFillEnvironment>(pDocument, formInfo);
368 pContext->SetFormFillEnv(pFormFillEnv.get());
373 return FPDFFormHandleFromCPDFSDKFormFillEnvironment(
374 pFormFillEnv.release());
384 std::unique_ptr<CPDFSDK_FormFillEnvironment> pFormFillEnv(
390 pFormFillEnv->ClearAllFocusedAnnots();
394 static_cast<CPDFXFA_Context*>(pFormFillEnv->GetDocExtension());
396 pContext->SetFormFillEnv(
nullptr);
405 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
407 pPageView->OnMouseMove(
408 Mask<FWL_EVENTFLAG>::FromUnderlyingUnchecked(modifier),
409 CFX_PointF(page_x, page_y));
416 const FS_POINTF* page_coord,
422 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
424 pPageView->OnMouseWheel(
425 Mask<FWL_EVENTFLAG>::FromUnderlyingUnchecked(modifier),
426 CFXPointFFromFSPointF(*page_coord), CFX_Vector(delta_x, delta_y));
434 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
437 Mask<FWL_EVENTFLAG>::FromUnderlyingUnchecked(modifier),
438 CFX_PointF(page_x, page_y));
446#ifdef PDF_ENABLE_CLICK_LOGGING
447 fprintf(stderr,
"mousedown,left,%d,%d\n",
static_cast<
int>(round(page_x)),
448 static_cast<
int>(round(page_y)));
450 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
452 pPageView->OnLButtonDown(
453 Mask<FWL_EVENTFLAG>::FromUnderlyingUnchecked(modifier),
454 CFX_PointF(page_x, page_y));
462#ifdef PDF_ENABLE_CLICK_LOGGING
463 fprintf(stderr,
"mouseup,left,%d,%d\n",
static_cast<
int>(round(page_x)),
464 static_cast<
int>(round(page_y)));
466 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
468 pPageView->OnLButtonUp(
469 Mask<FWL_EVENTFLAG>::FromUnderlyingUnchecked(modifier),
470 CFX_PointF(page_x, page_y));
479#ifdef PDF_ENABLE_CLICK_LOGGING
480 fprintf(stderr,
"mousedown,doubleleft,%d,%d\n",
481 static_cast<
int>(round(page_x)),
static_cast<
int>(round(page_y)));
483 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
485 pPageView->OnLButtonDblClk(
486 Mask<FWL_EVENTFLAG>::FromUnderlyingUnchecked(modifier),
487 CFX_PointF(page_x, page_y));
495#ifdef PDF_ENABLE_CLICK_LOGGING
496 fprintf(stderr,
"mousedown,right,%d,%d\n",
static_cast<
int>(round(page_x)),
497 static_cast<
int>(round(page_y)));
499 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
501 pPageView->OnRButtonDown(
502 Mask<FWL_EVENTFLAG>::FromUnderlyingUnchecked(modifier),
503 CFX_PointF(page_x, page_y));
511#ifdef PDF_ENABLE_CLICK_LOGGING
512 fprintf(stderr,
"mouseup,right,%d,%d\n",
static_cast<
int>(round(page_x)),
513 static_cast<
int>(round(page_y)));
515 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
517 pPageView->OnRButtonUp(
518 Mask<FWL_EVENTFLAG>::FromUnderlyingUnchecked(modifier),
519 CFX_PointF(page_x, page_y));
526 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
528 pPageView->OnKeyDown(
529 static_cast<FWL_VKEYCODE>(nKeyCode),
530 Mask<FWL_EVENTFLAG>::FromUnderlyingUnchecked(modifier));
544 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
547 nChar, Mask<FWL_EVENTFLAG>::FromUnderlyingUnchecked(modifier));
554 unsigned long buflen) {
555 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
560 return Utf16EncodeMaybeCopyAndReturnLength(
561 pPageView->GetFocusedFormText(),
569 unsigned long buflen) {
570 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
575 return Utf16EncodeMaybeCopyAndReturnLength(
576 pPageView->GetSelectedText(),
583 FPDF_WIDESTRING wsText) {
584 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
595 FPDF_WIDESTRING wsText) {
596 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
607 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
613 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
621 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
629 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
637 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
645 CPDFSDK_FormFillEnvironment* pFormFillEnv =
655 FPDF_ANNOTATION* annot) {
656 if (!page_index || !annot)
659 CPDFSDK_FormFillEnvironment* form_fill_env =
686 cpdfsdk_annot->GetPDFAnnot()->GetMutableAnnotDict();
692 *annot = FPDFAnnotationFromCPDFAnnotContext(annot_context.release());
698 CPDFSDK_FormFillEnvironment* form_fill_env =
707 CPDFSDK_PageView* page_view =
714 page_view->GetAnnotByDict(annot_dict.Get()));
735#if defined(PDF_USE_SKIA)
736 CFX_DIBitmap::ScopedPremultiplier scoped_premultiplier(
737 pdfium::WrapRetain(cbitmap), CFX_DefaultRenderDevice::UseSkiaRenderer());
739 FFLCommon(hHandle, page, cbitmap, start_x, start_y, size_x, size_y, rotate,
743#if defined(PDF_USE_SKIA)
744FPDF_EXPORT
void FPDF_CALLCONV FPDF_FFLDrawSkia(FPDF_FORMHANDLE hHandle,
745 FPDF_SKIA_CANVAS canvas,
753 SkCanvas* sk_canvas = SkCanvasFromFPDFSkiaCanvas(canvas);
758 FFLCommon(hHandle, page, sk_canvas, start_x, start_y, size_x, size_y, rotate,
766 unsigned long color) {
772 CPDF_FormField::IntToFormFieldType(fieldType);
773 if (!cast_input.has_value())
797 FPDF_FORMHANDLE hHandle) {
798 if (CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page))
803 FPDF_FORMHANDLE hHandle) {
804 CPDFSDK_FormFillEnvironment* pFormFillEnv =
823 CPDFSDK_FormFillEnvironment* pFormFillEnv =
831 CPDFSDK_FormFillEnvironment* pFormFillEnv =
839 CPDFSDK_FormFillEnvironment* pFormFillEnv =
856 FPDF_FORMHANDLE hHandle,
858 CPDFSDK_FormFillEnvironment* pFormFillEnv =
883 FPDF_BOOL selected) {
884 CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page);
890 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)
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
std::map< ByteString, RetainPtr< CPDF_Object >, std::less<> > DictMap
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
#define UNSAFE_BUFFERS(...)
UNSAFE_BUFFER_USAGE WideString WideStringFromFPDFWideString(FPDF_WIDESTRING wide_string)
IPDF_Page * IPDFPageFromFPDFPage(FPDF_PAGE page)
CFX_DIBitmap * CFXDIBitmapFromFPDFBitmap(FPDF_BITMAP bitmap)
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)