Merge to XFA: Kill remaining sprintfs
[pdfium.git] / fpdfsdk / src / fpdfeditpage.cpp
index 3f22b56..43d74aa 100644 (file)
@@ -4,9 +4,12 @@
  
 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
 
-// #include "x:/pdf/fpdfapi5/include/fpdfapi.h"
 #include "../include/fsdk_define.h"
 #include "../include/fpdfedit.h"
+#include "../include/fpdfformfill.h"
+#include "../include/fpdfxfa/fpdfxfa_doc.h"
+#include "../include/fpdfxfa/fpdfxfa_app.h"
+#include "../include/fpdfxfa/fpdfxfa_page.h"
 
 
 #if _FX_OS_ == _FX_ANDROID_
@@ -44,19 +47,18 @@ DLLEXPORT FPDF_DOCUMENT STDCALL FPDF_CreateNewDocument()
        {
                if(FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS))
                        pInfoDict->SetAt("CreationDate", new CPDF_String(DateStr));
-#ifdef FOXIT_CHROME_BUILD
-               pInfoDict->SetAt("Creator",FX_NEW CPDF_String(L"Google"));
-#else
-               pInfoDict->SetAt("Creator",FX_NEW CPDF_String(L"Foxit PDF SDK DLL 2.0 - Foxit Software"));
-#endif
+               pInfoDict->SetAt("Creator",FX_NEW CPDF_String(L"PDFium"));
        }
 
-       return pDoc;
+       CPDFXFA_App* pApp = FPDFXFA_GetApp();
+       CPDFXFA_Document* document = FX_NEW CPDFXFA_Document(pDoc, pApp);
+
+       return document;
 }
 
 DLLEXPORT void STDCALL FPDFPage_Delete(FPDF_DOCUMENT document, int page_index)
 {
-       CPDF_Document* pDoc = (CPDF_Document*)document;
+       CPDF_Document* pDoc = ((CPDFXFA_Document*)document)->GetPDFDoc();
        if (pDoc == NULL) 
                return;
        if (page_index < 0 || page_index >= pDoc->GetPageCount()) 
@@ -71,7 +73,7 @@ DLLEXPORT FPDF_PAGE STDCALL FPDFPage_New(FPDF_DOCUMENT document, int page_index,
                return NULL;
 
 //     CPDF_Parser* pParser = (CPDF_Parser*)document;
-       CPDF_Document* pDoc = (CPDF_Document*)document;
+       CPDF_Document* pDoc = ((CPDFXFA_Document*)document)->GetPDFDoc();
        if(page_index < 0)
                page_index = 0;
        if(pDoc->GetPageCount()<page_index)
@@ -92,17 +94,20 @@ DLLEXPORT FPDF_PAGE STDCALL FPDFPage_New(FPDF_DOCUMENT document, int page_index,
        pPageDict->SetAt("Rotate", FX_NEW CPDF_Number(0));
        pPageDict->SetAt("Resources", FX_NEW CPDF_Dictionary);
 
-       CPDF_Page* pPage = FX_NEW CPDF_Page;
-       pPage->Load(pDoc,pPageDict);
-       pPage->ParseContent();
+//     CPDF_Page* pPage = FX_NEW CPDF_Page;
+//     pPage->Load(pDoc,pPageDict);
+//     pPage->ParseContent();
+
+       CPDFXFA_Page* pPage = FX_NEW CPDFXFA_Page((CPDFXFA_Document*)document, page_index);
+       pPage->LoadPDFPage(pPageDict);
 
        return pPage;
 }
 
 DLLEXPORT int STDCALL FPDFPage_GetRotation(FPDF_PAGE page)
 {
-       CPDF_Page* pPage = (CPDF_Page*)page;
-       if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type")
+       CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
+       if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") || !pPage->m_pFormDict->GetElement("Type")->GetDirect()
                || pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare("Page"))
        {
                return -1;
@@ -112,8 +117,8 @@ DLLEXPORT int STDCALL FPDFPage_GetRotation(FPDF_PAGE page)
        int rotate = 0;
        if(pDict != NULL)
        {
-               if(pDict->KeyExist("Rotate"))
-                       rotate = pDict->GetElement("Rotate")->GetDirect()->GetInteger() / 90;
+               if (pDict->KeyExist("Rotate"))
+                       rotate = pDict->GetElement("Rotate")->GetDirect()? pDict->GetElement("Rotate")->GetDirect()->GetInteger() / 90 : 0;
                else
                {
                        if(pDict->KeyExist("Parent"))
@@ -123,7 +128,7 @@ DLLEXPORT int STDCALL FPDFPage_GetRotation(FPDF_PAGE page)
                                {
                                        if(pPages->KeyExist("Rotate"))
                                        {
-                                               rotate = pPages->GetElement("Rotate")->GetDirect()->GetInteger() / 90;
+                                               rotate = pPages->GetElement("Rotate")->GetDirect()? pPages->GetElement("Rotate")->GetDirect()->GetInteger() / 90 : 0;
                                                break;
                                        }
                                        else if(pPages->KeyExist("Parent"))
@@ -143,8 +148,8 @@ DLLEXPORT int STDCALL FPDFPage_GetRotation(FPDF_PAGE page)
 
 DLLEXPORT void STDCALL FPDFPage_InsertObject(FPDF_PAGE page, FPDF_PAGEOBJECT page_obj)
 {
-       CPDF_Page* pPage = (CPDF_Page*)page;
-       if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type")
+       CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
+       if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") || !pPage->m_pFormDict->GetElement("Type")->GetDirect()
                || pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare("Page"))
        {
                return;
@@ -198,8 +203,8 @@ DLLEXPORT void STDCALL FPDFPage_InsertObject(FPDF_PAGE page, FPDF_PAGEOBJECT pag
 
 DLLEXPORT int STDCALL FPDFPage_CountObject(FPDF_PAGE page)
 {
-       CPDF_Page* pPage = (CPDF_Page*)page;
-       if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type")
+       CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
+       if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") || !pPage->m_pFormDict->GetElement("Type")->GetDirect()
                || pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare("Page"))
        {
                return -1;
@@ -210,7 +215,7 @@ DLLEXPORT int STDCALL FPDFPage_CountObject(FPDF_PAGE page)
 
 DLLEXPORT FPDF_PAGEOBJECT STDCALL FPDFPage_GetObject(FPDF_PAGE page, int index)
 {
-       CPDF_Page* pPage = (CPDF_Page*)page;
+       CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
        if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type")
                || pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare("Page"))
        {
@@ -223,7 +228,8 @@ DLLEXPORT FPDF_PAGEOBJECT STDCALL FPDFPage_GetObject(FPDF_PAGE page, int index)
 DLLEXPORT FPDF_BOOL STDCALL FPDFPage_HasTransparency(FPDF_PAGE page)
 {
        if(!page) return FALSE;
-       CPDF_Page* pPage = (CPDF_Page*)page;
+       CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
+       if (!pPage) return FALSE;
 
        return pPage->BackgroundAlphaNeeded();
 }
@@ -262,8 +268,8 @@ DLLEXPORT FPDF_BOOL STDCALL FPDFPageObj_HasTransparency(FPDF_PAGEOBJECT pageObje
 
 DLLEXPORT FPDF_BOOL STDCALL FPDFPage_GenerateContent(FPDF_PAGE page)
 {
-       CPDF_Page* pPage = (CPDF_Page*)page;
-       if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type")
+       CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
+       if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") || !pPage->m_pFormDict->GetElement("Type")->GetDirect()
                || pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare("Page"))
        {
                return FALSE;
@@ -289,7 +295,8 @@ DLLEXPORT void STDCALL FPDFPage_TransformAnnots(FPDF_PAGE page,
 {
        if(page == NULL)
                return;
-       CPDF_Page* pPage = (CPDF_Page*)page;
+       CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
+       if (!pPage) return;
        CPDF_AnnotList AnnotList(pPage);
        for (int i=0; i<AnnotList.Count();i++)
        {
@@ -314,3 +321,21 @@ DLLEXPORT void STDCALL FPDFPage_TransformAnnots(FPDF_PAGE page,
        }
 
 }
+
+DLLEXPORT void STDCALL FPDFPage_SetRotation(FPDF_PAGE page, int rotate)
+{
+       if (page == NULL)
+               return;
+       CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
+       if (!pPage) return;
+
+       if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") || !pPage->m_pFormDict->GetElement("Type")->GetDirect()
+               || pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare("Page"))
+       {
+               return;
+       }
+       CPDF_Dictionary* pDict = pPage->m_pFormDict;
+       rotate %=4;
+
+       pDict->SetAt("Rotate", FX_NEW CPDF_Number(rotate * 90));
+}