diff options
Diffstat (limited to 'fpdfsdk/src/fpdfformfill.cpp')
-rw-r--r-- | fpdfsdk/src/fpdfformfill.cpp | 399 |
1 files changed, 395 insertions, 4 deletions
diff --git a/fpdfsdk/src/fpdfformfill.cpp b/fpdfsdk/src/fpdfformfill.cpp index 9dc5b70372..5825456b19 100644 --- a/fpdfsdk/src/fpdfformfill.cpp +++ b/fpdfsdk/src/fpdfformfill.cpp @@ -12,6 +12,12 @@ #include "fpdfsdk/include/fsdk_mgr.h" #include "public/fpdfview.h" +#ifdef PDF_ENABLE_XFA +#include "fpdfsdk/include/fpdfxfa/fpdfxfa_app.h" +#include "fpdfsdk/include/fpdfxfa/fpdfxfa_doc.h" +#include "fpdfsdk/include/fpdfxfa/fpdfxfa_page.h" +#endif // PDF_ENABLE_XFA + namespace { CPDFSDK_InterForm* FormHandleToInterForm(FPDF_FORMHANDLE hHandle) { @@ -38,6 +44,63 @@ DLLEXPORT int STDCALL FPDFPage_HasFormFieldAtPoint(FPDF_FORMHANDLE hHandle, if (!hHandle) return -1; CPDF_Page* pPage = CPDFPageFromFPDFPage(page); +#ifdef PDF_ENABLE_XFA + if (pPage) { + CPDF_InterForm interform(pPage->m_pDocument, FALSE); + CPDF_FormControl* pFormCtrl = interform.GetControlAtPoint( + pPage, (FX_FLOAT)page_x, (FX_FLOAT)page_y, nullptr); + if (!pFormCtrl) + return -1; + + CPDF_FormField* pFormField = pFormCtrl->GetField(); + if (!pFormField) + return -1; + + int nType = pFormField->GetFieldType(); + return nType; + } + + IXFA_PageView* pPageView = ((CPDFXFA_Page*)page)->GetXFAPageView(); + if (pPageView) { + IXFA_WidgetHandler* pWidgetHandler = NULL; + IXFA_DocView* pDocView = pPageView->GetDocView(); + if (!pDocView) + return -1; + + pWidgetHandler = pDocView->GetWidgetHandler(); + if (!pWidgetHandler) + return -1; + + IXFA_Widget* pXFAAnnot = NULL; + IXFA_WidgetIterator* pWidgetIterator = pPageView->CreateWidgetIterator( + XFA_TRAVERSEWAY_Form, + XFA_WIDGETFILTER_Viewable | XFA_WIDGETFILTER_AllType); + if (!pWidgetIterator) + return -1; + pXFAAnnot = pWidgetIterator->MoveToNext(); + while (pXFAAnnot) { + CFX_RectF rcBBox; + pWidgetHandler->GetBBox(pXFAAnnot, rcBBox, 0); + CFX_FloatRect rcWidget(rcBBox.left, rcBBox.top, + rcBBox.left + rcBBox.width, + rcBBox.top + rcBBox.height); + rcWidget.left -= 1.0f; + rcWidget.right += 1.0f; + rcWidget.bottom -= 1.0f; + rcWidget.top += 1.0f; + + if (rcWidget.Contains(static_cast<FX_FLOAT>(page_x), + static_cast<FX_FLOAT>(page_y))) { + pWidgetIterator->Release(); + return FPDF_FORMFIELD_XFA; + } + pXFAAnnot = pWidgetIterator->MoveToNext(); + } + + pWidgetIterator->Release(); + } + return -1; +#else // PDF_ENABLE_XFA if (!pPage) return -1; CPDF_InterForm interform(pPage->m_pDocument, FALSE); @@ -45,9 +108,9 @@ DLLEXPORT int STDCALL FPDFPage_HasFormFieldAtPoint(FPDF_FORMHANDLE hHandle, pPage, (FX_FLOAT)page_x, (FX_FLOAT)page_y, nullptr); if (!pFormCtrl) return -1; - CPDF_FormField* pFormField = pFormCtrl->GetField(); return pFormField ? pFormField->GetFieldType() : -1; +#endif // PDF_ENABLE_XFA } DLLEXPORT int STDCALL FPDPage_HasFormFieldAtPoint(FPDF_FORMHANDLE hHandle, @@ -76,7 +139,11 @@ DLLEXPORT int STDCALL FPDFPage_FormFieldZOrderAtPoint(FPDF_FORMHANDLE hHandle, DLLEXPORT FPDF_FORMHANDLE STDCALL FPDFDOC_InitFormFillEnvironment(FPDF_DOCUMENT document, FPDF_FORMFILLINFO* formInfo) { +#ifdef PDF_ENABLE_XFA + const int kRequiredVersion = 2; +#else // PDF_ENABLE_XFA const int kRequiredVersion = 1; +#endif // PDF_ENABLE_XFA if (!formInfo || formInfo->version != kRequiredVersion) return nullptr; @@ -85,7 +152,13 @@ FPDFDOC_InitFormFillEnvironment(FPDF_DOCUMENT document, return nullptr; CPDFDoc_Environment* pEnv = new CPDFDoc_Environment(pDocument, formInfo); +#ifdef PDF_ENABLE_XFA + pEnv->SetSDKDocument(pDocument->GetSDKDocument(pEnv)); + CPDFXFA_App* pApp = CPDFXFA_App::GetInstance(); + pApp->AddFormFillEnv(pEnv); +#else // PDF_ENABLE_XFA pEnv->SetSDKDocument(new CPDFSDK_Document(pDocument, pEnv)); +#endif // PDF_ENABLE_XFA return pEnv; } @@ -93,12 +166,16 @@ DLLEXPORT void STDCALL FPDFDOC_ExitFormFillEnvironment(FPDF_FORMHANDLE hHandle) { if (!hHandle) return; - CPDFDoc_Environment* pEnv = (CPDFDoc_Environment*)hHandle; +#ifdef PDF_ENABLE_XFA + CPDFXFA_App* pApp = CPDFXFA_App::GetInstance(); + pApp->RemoveFormFillEnv(pEnv); +#else // PDF_ENABLE_XFA if (CPDFSDK_Document* pSDKDoc = pEnv->GetSDKDocument()) { pEnv->SetSDKDocument(NULL); delete pSDKDoc; } +#endif // PDF_ENABLE_XFA delete pEnv; } @@ -141,6 +218,34 @@ DLLEXPORT FPDF_BOOL STDCALL FORM_OnLButtonUp(FPDF_FORMHANDLE hHandle, return pPageView->OnLButtonUp(pt, modifier); } +#ifdef PDF_ENABLE_XFA +DLLEXPORT FPDF_BOOL STDCALL FORM_OnRButtonDown(FPDF_FORMHANDLE hHandle, + FPDF_PAGE page, + int modifier, + double page_x, + double page_y) { + CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page); + if (!pPageView) + return FALSE; + + CPDF_Point pt((FX_FLOAT)page_x, (FX_FLOAT)page_y); + return pPageView->OnRButtonDown(pt, modifier); +} + +DLLEXPORT FPDF_BOOL STDCALL FORM_OnRButtonUp(FPDF_FORMHANDLE hHandle, + FPDF_PAGE page, + int modifier, + double page_x, + double page_y) { + CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, page); + if (!pPageView) + return FALSE; + + CPDF_Point pt((FX_FLOAT)page_x, (FX_FLOAT)page_y); + return pPageView->OnRButtonUp(pt, modifier); +} +#endif // PDF_ENABLE_XFA + DLLEXPORT FPDF_BOOL STDCALL FORM_OnKeyDown(FPDF_FORMHANDLE hHandle, FPDF_PAGE page, int nKeyCode, @@ -198,21 +303,32 @@ DLLEXPORT void STDCALL FPDF_FFLDraw(FPDF_FORMHANDLE hHandle, if (!pPage) return; +#ifndef PDF_ENABLE_XFA CPDF_RenderOptions options; if (flags & FPDF_LCD_TEXT) options.m_Flags |= RENDER_CLEARTYPE; else options.m_Flags &= ~RENDER_CLEARTYPE; - // Grayscale output if (flags & FPDF_GRAYSCALE) { options.m_ColorMode = RENDER_COLOR_GRAY; options.m_ForeColor = 0; options.m_BackColor = 0xffffff; } - options.m_AddFlags = flags >> 8; options.m_pOCContext = new CPDF_OCContext(pPage->m_pDocument); +#else // PDF_ENABLE_XFA + CPDFXFA_Document* pDocument = pPage->GetDocument(); + if (!pDocument) + return; + CPDF_Document* pPDFDoc = pDocument->GetPDFDoc(); + if (!pPDFDoc) + return; + CPDFDoc_Environment* pEnv = (CPDFDoc_Environment*)hHandle; + CPDFSDK_Document* pFXDoc = pEnv->GetSDKDocument(); + if (!pFXDoc) + return; +#endif // PDF_ENABLE_XFA CFX_Matrix matrix; pPage->GetDisplayMatrix(matrix, start_x, start_y, size_x, size_y, rotate); @@ -232,12 +348,287 @@ DLLEXPORT void STDCALL FPDF_FFLDraw(FPDF_FORMHANDLE hHandle, pDevice->SaveState(); pDevice->SetClip_Rect(&clip); +#ifndef PDF_ENABLE_XFA if (CPDFSDK_PageView* pPageView = FormHandleToPageView(hHandle, pPage)) pPageView->PageView_OnDraw(pDevice.get(), &matrix, &options); +#else // PDF_ENABLE_XFA + CPDF_RenderOptions options; + if (flags & FPDF_LCD_TEXT) + options.m_Flags |= RENDER_CLEARTYPE; + else + options.m_Flags &= ~RENDER_CLEARTYPE; + + // Grayscale output + if (flags & FPDF_GRAYSCALE) { + options.m_ColorMode = RENDER_COLOR_GRAY; + options.m_ForeColor = 0; + options.m_BackColor = 0xffffff; + } + options.m_AddFlags = flags >> 8; + options.m_pOCContext = new CPDF_OCContext(pPDFDoc); + + if (CPDFSDK_PageView* pPageView = pFXDoc->GetPageView(pPage)) + pPageView->PageView_OnDraw(pDevice.get(), &matrix, &options, clip); +#endif // PDF_ENABLE_XFA pDevice->RestoreState(); delete options.m_pOCContext; +#ifdef PDF_ENABLE_XFA + options.m_pOCContext = NULL; +#endif // PDF_ENABLE_XFA +} + +#ifdef PDF_ENABLE_XFA +DLLEXPORT void STDCALL FPDF_Widget_Undo(FPDF_DOCUMENT document, + FPDF_WIDGET hWidget) { + if (NULL == hWidget || NULL == document) + return; + + CPDFXFA_Document* pDocument = (CPDFXFA_Document*)document; + if (pDocument->GetDocType() != XFA_DOCTYPE_Dynamic && + pDocument->GetDocType() != XFA_DOCTYPE_Static) + return; + + IXFA_MenuHandler* pXFAMenuHander = + CPDFXFA_App::GetInstance()->GetXFAApp()->GetMenuHandler(); + if (pXFAMenuHander == NULL) + return; + + pXFAMenuHander->Undo((IXFA_Widget*)hWidget); +} +DLLEXPORT void STDCALL FPDF_Widget_Redo(FPDF_DOCUMENT document, + FPDF_WIDGET hWidget) { + if (NULL == hWidget || NULL == document) + return; + + CPDFXFA_Document* pDocument = (CPDFXFA_Document*)document; + if (pDocument->GetDocType() != XFA_DOCTYPE_Dynamic && + pDocument->GetDocType() != XFA_DOCTYPE_Static) + return; + + IXFA_MenuHandler* pXFAMenuHander = + CPDFXFA_App::GetInstance()->GetXFAApp()->GetMenuHandler(); + if (pXFAMenuHander == NULL) + return; + + pXFAMenuHander->Redo((IXFA_Widget*)hWidget); +} + +DLLEXPORT void STDCALL FPDF_Widget_SelectAll(FPDF_DOCUMENT document, + FPDF_WIDGET hWidget) { + if (NULL == hWidget || NULL == document) + return; + + CPDFXFA_Document* pDocument = (CPDFXFA_Document*)document; + if (pDocument->GetDocType() != XFA_DOCTYPE_Dynamic && + pDocument->GetDocType() != XFA_DOCTYPE_Static) + return; + + IXFA_MenuHandler* pXFAMenuHander = + CPDFXFA_App::GetInstance()->GetXFAApp()->GetMenuHandler(); + if (pXFAMenuHander == NULL) + return; + + pXFAMenuHander->SelectAll((IXFA_Widget*)hWidget); +} +DLLEXPORT void STDCALL FPDF_Widget_Copy(FPDF_DOCUMENT document, + FPDF_WIDGET hWidget, + FPDF_WIDESTRING wsText, + FPDF_DWORD* size) { + if (NULL == hWidget || NULL == document) + return; + + CPDFXFA_Document* pDocument = (CPDFXFA_Document*)document; + if (pDocument->GetDocType() != XFA_DOCTYPE_Dynamic && + pDocument->GetDocType() != XFA_DOCTYPE_Static) + return; + + IXFA_MenuHandler* pXFAMenuHander = + CPDFXFA_App::GetInstance()->GetXFAApp()->GetMenuHandler(); + if (pXFAMenuHander == NULL) + return; + + CFX_WideString wsCpText; + pXFAMenuHander->Copy((IXFA_Widget*)hWidget, wsCpText); + + CFX_ByteString bsCpText = wsCpText.UTF16LE_Encode(); + int len = bsCpText.GetLength() / sizeof(unsigned short); + if (wsText == NULL) { + *size = len; + return; + } + + int real_size = len < *size ? len : *size; + if (real_size > 0) { + FXSYS_memcpy((void*)wsText, + bsCpText.GetBuffer(real_size * sizeof(unsigned short)), + real_size * sizeof(unsigned short)); + bsCpText.ReleaseBuffer(real_size * sizeof(unsigned short)); + } + *size = real_size; +} +DLLEXPORT void STDCALL FPDF_Widget_Cut(FPDF_DOCUMENT document, + FPDF_WIDGET hWidget, + FPDF_WIDESTRING wsText, + FPDF_DWORD* size) { + if (NULL == hWidget || NULL == document) + return; + CPDFXFA_Document* pDocument = (CPDFXFA_Document*)document; + if (pDocument->GetDocType() != XFA_DOCTYPE_Dynamic && + pDocument->GetDocType() != XFA_DOCTYPE_Static) + return; + + IXFA_MenuHandler* pXFAMenuHander = + CPDFXFA_App::GetInstance()->GetXFAApp()->GetMenuHandler(); + if (pXFAMenuHander == NULL) + return; + + CFX_WideString wsCpText; + pXFAMenuHander->Cut((IXFA_Widget*)hWidget, wsCpText); + + CFX_ByteString bsCpText = wsCpText.UTF16LE_Encode(); + int len = bsCpText.GetLength() / sizeof(unsigned short); + if (wsText == NULL) { + *size = len; + return; + } + + int real_size = len < *size ? len : *size; + if (real_size > 0) { + FXSYS_memcpy((void*)wsText, + bsCpText.GetBuffer(real_size * sizeof(unsigned short)), + real_size * sizeof(unsigned short)); + bsCpText.ReleaseBuffer(real_size * sizeof(unsigned short)); + } + *size = real_size; +} +DLLEXPORT void STDCALL FPDF_Widget_Paste(FPDF_DOCUMENT document, + FPDF_WIDGET hWidget, + FPDF_WIDESTRING wsText, + FPDF_DWORD size) { + if (NULL == hWidget || NULL == document) + return; + + CPDFXFA_Document* pDocument = (CPDFXFA_Document*)document; + if (pDocument->GetDocType() != XFA_DOCTYPE_Dynamic && + pDocument->GetDocType() != XFA_DOCTYPE_Static) + return; + + IXFA_MenuHandler* pXFAMenuHander = + CPDFXFA_App::GetInstance()->GetXFAApp()->GetMenuHandler(); + if (pXFAMenuHander == NULL) + return; + + CFX_WideString wstr = CFX_WideString::FromUTF16LE(wsText, size); + pXFAMenuHander->Paste((IXFA_Widget*)hWidget, wstr); +} +DLLEXPORT void STDCALL +FPDF_Widget_ReplaceSpellCheckWord(FPDF_DOCUMENT document, + FPDF_WIDGET hWidget, + float x, + float y, + FPDF_BYTESTRING bsText) { + if (NULL == hWidget || NULL == document) + return; + + CPDFXFA_Document* pDocument = (CPDFXFA_Document*)document; + if (pDocument->GetDocType() != XFA_DOCTYPE_Dynamic && + pDocument->GetDocType() != XFA_DOCTYPE_Static) + return; + + IXFA_MenuHandler* pXFAMenuHander = + CPDFXFA_App::GetInstance()->GetXFAApp()->GetMenuHandler(); + if (pXFAMenuHander == NULL) + return; + + CFX_PointF ptPopup; + ptPopup.x = x; + ptPopup.y = y; + CFX_ByteStringC bs(bsText); + pXFAMenuHander->ReplaceSpellCheckWord((IXFA_Widget*)hWidget, ptPopup, bs); +} +DLLEXPORT void STDCALL +FPDF_Widget_GetSpellCheckWords(FPDF_DOCUMENT document, + FPDF_WIDGET hWidget, + float x, + float y, + FPDF_STRINGHANDLE* stringHandle) { + if (NULL == hWidget || NULL == document) + return; + + CPDFXFA_Document* pDocument = (CPDFXFA_Document*)document; + if (pDocument->GetDocType() != XFA_DOCTYPE_Dynamic && + pDocument->GetDocType() != XFA_DOCTYPE_Static) + return; + + IXFA_MenuHandler* pXFAMenuHander = + CPDFXFA_App::GetInstance()->GetXFAApp()->GetMenuHandler(); + if (pXFAMenuHander == NULL) + return; + + CFX_ByteStringArray* sSuggestWords = new CFX_ByteStringArray; + CFX_PointF ptPopup; + ptPopup.x = x; + ptPopup.y = y; + pXFAMenuHander->GetSuggestWords((IXFA_Widget*)hWidget, ptPopup, + *sSuggestWords); + *stringHandle = (FPDF_STRINGHANDLE)sSuggestWords; +} +DLLEXPORT int STDCALL FPDF_StringHandleCounts(FPDF_STRINGHANDLE stringHandle) { + if (stringHandle == NULL) + return -1; + CFX_ByteStringArray* sSuggestWords = (CFX_ByteStringArray*)stringHandle; + return sSuggestWords->GetSize(); +} +DLLEXPORT FPDF_BOOL STDCALL +FPDF_StringHandleGetStringByIndex(FPDF_STRINGHANDLE stringHandle, + int index, + FPDF_BYTESTRING bsText, + FPDF_DWORD* size) { + if (stringHandle == NULL || size == NULL) + return FALSE; + int count = FPDF_StringHandleCounts(stringHandle); + if (index < 0 || index >= count) + return FALSE; + + CFX_ByteStringArray sSuggestWords = *(CFX_ByteStringArray*)stringHandle; + int len = sSuggestWords[index].GetLength(); + + if (bsText == NULL) { + *size = len; + return TRUE; + } + + int real_size = len < *size ? len : *size; + if (real_size > 0) + FXSYS_memcpy((void*)bsText, (const FX_CHAR*)(sSuggestWords[index]), + real_size); + *size = real_size; + + return TRUE; +} +DLLEXPORT void STDCALL +FPDF_StringHandleRelease(FPDF_STRINGHANDLE stringHandle) { + if (stringHandle == NULL) + return; + CFX_ByteStringArray* sSuggestWords = (CFX_ByteStringArray*)stringHandle; + delete sSuggestWords; +} + +DLLEXPORT FPDF_BOOL STDCALL +FPDF_StringHandleAddString(FPDF_STRINGHANDLE stringHandle, + FPDF_BYTESTRING bsText, + FPDF_DWORD size) { + if (stringHandle == NULL || bsText == NULL || size <= 0) + return FALSE; + + CFX_ByteStringArray* stringArr = (CFX_ByteStringArray*)stringHandle; + CFX_ByteString bsStr(bsText, size); + + stringArr->Add(bsStr); + return TRUE; } +#endif // PDF_ENABLE_XFA DLLEXPORT void STDCALL FPDF_SetFormFieldHighlightColor(FPDF_FORMHANDLE hHandle, int fieldType, |