Merge to XFA: Introduce CPDFPageFromFPFDPage()
authorTom Sepez <tsepez@chromium.org>
Fri, 16 Oct 2015 21:00:21 +0000 (14:00 -0700)
committerTom Sepez <tsepez@chromium.org>
Fri, 16 Oct 2015 21:00:21 +0000 (14:00 -0700)
Original Review URL: https://codereview.chromium.org/1406543004 .
(cherry picked from commit 943ea142b6a7e6edc91fe3c307013129e6eb0ef3)

R=thestig@chromium.org

Review URL: https://codereview.chromium.org/1411623002 .

fpdfsdk/include/fsdk_define.h
fpdfsdk/src/fpdf_flatten.cpp
fpdfsdk/src/fpdf_progressive.cpp
fpdfsdk/src/fpdf_transformpage.cpp
fpdfsdk/src/fpdfdoc.cpp
fpdfsdk/src/fpdfeditimg.cpp
fpdfsdk/src/fpdfeditpage.cpp
fpdfsdk/src/fpdfformfill.cpp
fpdfsdk/src/fpdfview.cpp

index c2aa0d9..cc8c12d 100644 (file)
@@ -93,6 +93,7 @@ class CFPDF_FileStream : public IFX_FileStream {
 
 // Conversions from FPDF_ types.
 CPDF_Document* CPDFDocumentFromFPDFDocument(FPDF_DOCUMENT doc);
+CPDF_Page* CPDFPageFromFPDFPage(FPDF_PAGE page);
 
 void DropContext(void* data);
 void FSDK_SetSandBoxPolicy(FPDF_DWORD policy, FPDF_BOOL enable);
index 73410b6..9d5a0d4 100644 (file)
@@ -312,14 +312,11 @@ void GetOffset(FX_FLOAT& fa,
 }
 
 DLLEXPORT int STDCALL FPDFPage_Flatten(FPDF_PAGE page, int nFlag) {
+  CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
   if (!page) {
     return FLATTEN_FAIL;
   }
 
-  CPDF_Page* pPage = ((CPDFXFA_Page*)(page))->GetPDFPage();
-  if (!pPage)
-    return FLATTEN_FAIL;
-
   CPDF_Document* pDocument = pPage->m_pDocument;
   CPDF_Dictionary* pPageDict = pPage->m_pFormDict;
 
index f5e11e8..4412648 100644 (file)
@@ -20,16 +20,10 @@ DLLEXPORT int STDCALL FPDF_RenderPageBitmap_Start(FPDF_BITMAP bitmap,
                                                   int rotate,
                                                   int flags,
                                                   IFSDK_PAUSE* pause) {
-  if (bitmap == NULL || page == NULL)
+  if (!bitmap || !pause || pause->version != 1)
     return FPDF_RENDER_FAILED;
 
-  if (!pause)
-    return FPDF_RENDER_FAILED;
-
-  if (pause->version != 1)
-    return FPDF_RENDER_FAILED;
-
-  CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
+  CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
   if (!pPage)
     return FPDF_RENDER_FAILED;
 
@@ -66,16 +60,10 @@ DLLEXPORT int STDCALL FPDF_RenderPageBitmap_Start(FPDF_BITMAP bitmap,
 
 DLLEXPORT int STDCALL FPDF_RenderPage_Continue(FPDF_PAGE page,
                                                IFSDK_PAUSE* pause) {
-  if (page == NULL)
+  if (!pause || pause->version != 1)
     return FPDF_RENDER_FAILED;
 
-  if (!pause)
-    return FPDF_RENDER_FAILED;
-
-  if (pause->version != 1)
-    return FPDF_RENDER_FAILED;
-
-  CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
+  CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
   if (!pPage)
     return FPDF_RENDER_FAILED;
 
@@ -93,9 +81,7 @@ DLLEXPORT int STDCALL FPDF_RenderPage_Continue(FPDF_PAGE page,
 }
 
 DLLEXPORT void STDCALL FPDF_RenderPage_Close(FPDF_PAGE page) {
-  if (!page)
-    return;
-  CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
+  CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
   if (!pPage)
     return;
 
index dbf97aa..132f668 100644 (file)
 
 namespace {
 
-CPDF_Page* GetPageFromFPDFPage(FPDF_PAGE page) {
-  return page ? static_cast<CPDFXFA_Page*>(page)->GetPDFPage() : nullptr;
-}
-
 void SetBoundingBox(CPDF_Page* page,
                     const CFX_ByteStringC& key,
                     float left,
@@ -55,7 +51,7 @@ DLLEXPORT void STDCALL FPDFPage_SetMediaBox(FPDF_PAGE page,
                                             float bottom,
                                             float right,
                                             float top) {
-  CPDF_Page* pPage = GetPageFromFPDFPage(page);
+  CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
   if (!pPage)
     return;
 
@@ -67,7 +63,7 @@ DLLEXPORT void STDCALL FPDFPage_SetCropBox(FPDF_PAGE page,
                                            float bottom,
                                            float right,
                                            float top) {
-  CPDF_Page* pPage = GetPageFromFPDFPage(page);
+  CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
   if (!pPage)
     return;
 
@@ -79,7 +75,7 @@ DLLEXPORT FPDF_BOOL STDCALL FPDFPage_GetMediaBox(FPDF_PAGE page,
                                                  float* bottom,
                                                  float* right,
                                                  float* top) {
-  CPDF_Page* pPage = GetPageFromFPDFPage(page);
+  CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
   return pPage && GetBoundingBox(pPage, "MediaBox", left, bottom, right, top);
 }
 
@@ -88,14 +84,14 @@ DLLEXPORT FPDF_BOOL STDCALL FPDFPage_GetCropBox(FPDF_PAGE page,
                                                 float* bottom,
                                                 float* right,
                                                 float* top) {
-  CPDF_Page* pPage = GetPageFromFPDFPage(page);
+  CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
   return pPage && GetBoundingBox(pPage, "CropBox", left, bottom, right, top);
 }
 
 DLLEXPORT FPDF_BOOL STDCALL FPDFPage_TransFormWithClip(FPDF_PAGE page,
                                                        FS_MATRIX* matrix,
                                                        FS_RECTF* clipRect) {
-  CPDF_Page* pPage = GetPageFromFPDFPage(page);
+  CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
   if (!pPage)
     return FALSE;
 
@@ -268,7 +264,7 @@ void OutputPath(CFX_ByteTextBuf& buf, CPDF_Path path) {
 
 DLLEXPORT void STDCALL FPDFPage_InsertClipPath(FPDF_PAGE page,
                                                FPDF_CLIPPATH clipPath) {
-  CPDF_Page* pPage = GetPageFromFPDFPage(page);
+  CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
   if (!pPage)
     return;
 
index d4b6b08..d1b71ac 100644 (file)
@@ -203,8 +203,7 @@ DLLEXPORT unsigned long STDCALL FPDFDest_GetPageIndex(FPDF_DOCUMENT document,
 
 DLLEXPORT FPDF_LINK STDCALL
 FPDFLink_GetLinkAtPoint(FPDF_PAGE page, double x, double y) {
-  CPDFXFA_Page* pXFAPage = (CPDFXFA_Page*)page;
-  CPDF_Page* pPage = pXFAPage->GetPDFPage();
+  CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
   if (!pPage)
     return nullptr;
 
@@ -218,8 +217,7 @@ FPDFLink_GetLinkAtPoint(FPDF_PAGE page, double x, double y) {
 
 DLLEXPORT int STDCALL
 FPDFLink_GetLinkZOrderAtPoint(FPDF_PAGE page, double x, double y) {
-  CPDFXFA_Page* pXFAPage = (CPDFXFA_Page*)page;
-  CPDF_Page* pPage = pXFAPage->GetPDFPage();
+  CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
   if (!pPage)
     return -1;
 
@@ -261,10 +259,10 @@ DLLEXPORT FPDF_ACTION STDCALL FPDFLink_GetAction(FPDF_LINK pDict) {
 DLLEXPORT FPDF_BOOL STDCALL FPDFLink_Enumerate(FPDF_PAGE page,
                                                int* startPos,
                                                FPDF_LINK* linkAnnot) {
-  if (!page || !startPos || !linkAnnot)
+  if (!startPos || !linkAnnot)
     return FALSE;
-  CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
-  if (!pPage->m_pFormDict)
+  CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
+  if (!pPage || !pPage->m_pFormDict)
     return FALSE;
   CPDF_Array* pAnnots = pPage->m_pFormDict->GetArray("Annots");
   if (!pAnnots)
index 09295aa..cd4618e 100644 (file)
@@ -32,7 +32,7 @@ FPDFImageObj_LoadJpegFile(FPDF_PAGE* pages,
   CPDF_ImageObject* pImgObj = (CPDF_ImageObject*)image_object;
   pImgObj->m_GeneralState.GetModify();
   for (int index = 0; index < nCount; index++) {
-    CPDF_Page* pPage = ((CPDFXFA_Page*)pages[index])->GetPDFPage();
+    CPDF_Page* pPage = CPDFPageFromFPDFPage(pages[index]);
     if (!pPage)
       continue;
     pImgObj->m_pImage->ResetCache(pPage, NULL);
@@ -73,7 +73,7 @@ DLLEXPORT FPDF_BOOL STDCALL FPDFImageObj_SetBitmap(FPDF_PAGE* pages,
   CPDF_ImageObject* pImgObj = (CPDF_ImageObject*)image_object;
   pImgObj->m_GeneralState.GetModify();
   for (int index = 0; index < nCount; index++) {
-    CPDF_Page* pPage = ((CPDFXFA_Page*)pages[index])->GetPDFPage();
+    CPDF_Page* pPage = CPDFPageFromFPDFPage(pages[index]);
     if (!pPage)
       continue;
     pImgObj->m_pImage->ResetCache(pPage, NULL);
index e7bbf05..0cf7c88 100644 (file)
@@ -89,7 +89,7 @@ DLLEXPORT FPDF_PAGE STDCALL FPDFPage_New(FPDF_DOCUMENT document,
 }
 
 DLLEXPORT int STDCALL FPDFPage_GetRotation(FPDF_PAGE page) {
-  CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
+  CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
   if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") ||
       !pPage->m_pFormDict->GetElement("Type")->GetDirect() ||
       pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare(
@@ -133,7 +133,7 @@ DLLEXPORT int STDCALL FPDFPage_GetRotation(FPDF_PAGE page) {
 
 DLLEXPORT void STDCALL FPDFPage_InsertObject(FPDF_PAGE page,
                                              FPDF_PAGEOBJECT page_obj) {
-  CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
+  CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
   if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") ||
       !pPage->m_pFormDict->GetElement("Type")->GetDirect() ||
       pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare(
@@ -175,9 +175,6 @@ DLLEXPORT void STDCALL FPDFPage_InsertObject(FPDF_PAGE page,
     default:
       break;
   }
-
-  //   pPage->ParseContent();
-  // pPage->GenerateContent();
 }
 
 DLLEXPORT int STDCALL FPDFPage_CountObject(FPDF_PAGE page) {
@@ -189,29 +186,22 @@ DLLEXPORT int STDCALL FPDFPage_CountObject(FPDF_PAGE page) {
     return -1;
   }
   return pPage->CountObjects();
-  //   return 0;
 }
 
 DLLEXPORT FPDF_PAGEOBJECT STDCALL FPDFPage_GetObject(FPDF_PAGE page,
                                                      int index) {
-  CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
+  CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
   if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") ||
       pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare(
           "Page")) {
     return NULL;
   }
   return pPage->GetObjectByIndex(index);
-  //   return NULL;
 }
 
 DLLEXPORT FPDF_BOOL STDCALL FPDFPage_HasTransparency(FPDF_PAGE page) {
-  if (!page)
-    return FALSE;
-  CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
-  if (!pPage)
-    return FALSE;
-
-  return pPage->BackgroundAlphaNeeded();
+  CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
+  return pPage && pPage->BackgroundAlphaNeeded();
 }
 
 DLLEXPORT FPDF_BOOL STDCALL
@@ -253,7 +243,7 @@ FPDFPageObj_HasTransparency(FPDF_PAGEOBJECT pageObject) {
 }
 
 DLLEXPORT FPDF_BOOL STDCALL FPDFPage_GenerateContent(FPDF_PAGE page) {
-  CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
+  CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
   if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") ||
       !pPage->m_pFormDict->GetElement("Type")->GetDirect() ||
       pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare(
@@ -288,9 +278,7 @@ DLLEXPORT void STDCALL FPDFPage_TransformAnnots(FPDF_PAGE page,
                                                 double d,
                                                 double e,
                                                 double f) {
-  if (page == NULL)
-    return;
-  CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
+  CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
   if (!pPage)
     return;
   CPDF_AnnotList AnnotList(pPage);
@@ -318,12 +306,7 @@ 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;
-
+  CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
   if (!pPage || !pPage->m_pFormDict || !pPage->m_pFormDict->KeyExist("Type") ||
       !pPage->m_pFormDict->GetElement("Type")->GetDirect() ||
       pPage->m_pFormDict->GetElement("Type")->GetDirect()->GetString().Compare(
index e86a47b..9fe024c 100644 (file)
@@ -42,10 +42,9 @@ DLLEXPORT int STDCALL FPDFPage_HasFormFieldAtPoint(FPDF_FORMHANDLE hHandle,
                                                    FPDF_PAGE page,
                                                    double page_x,
                                                    double page_y) {
-  if (!page || !hHandle)
+  if (!hHandle)
     return -1;
-
-  CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
+  CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
   if (pPage) {
     CPDF_InterForm interform(pPage->m_pDocument, FALSE);
     CPDF_FormControl* pFormCtrl = interform.GetControlAtPoint(
@@ -115,10 +114,11 @@ DLLEXPORT int STDCALL FPDFPage_FormFieldZOrderAtPoint(FPDF_FORMHANDLE hHandle,
                                                       FPDF_PAGE page,
                                                       double page_x,
                                                       double page_y) {
-  if (!page || !hHandle)
+  if (!hHandle)
+    return -1;
+  CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
+  if (!pPage)
     return -1;
-
-  CPDF_Page* pPage = (CPDF_Page*)page;
   CPDF_InterForm interform(pPage->m_pDocument, FALSE);
   int z_order = -1;
   (void)interform.GetControlAtPoint(pPage, (FX_FLOAT)page_x, (FX_FLOAT)page_y,
index 5d9bf4b..6254eb2 100644 (file)
@@ -26,6 +26,10 @@ CPDF_Document* CPDFDocumentFromFPDFDocument(FPDF_DOCUMENT doc) {
   return doc ? static_cast<CPDFXFA_Document*>(doc)->GetPDFDoc() : nullptr;
 }
 
+CPDF_Page* CPDFPageFromFPDFPage(FPDF_PAGE page) {
+  return page ? static_cast<CPDFXFA_Page*>(page)->GetPDFPage() : nullptr;
+}
+
 CFPDF_FileStream::CFPDF_FileStream(FPDF_FILEHANDLER* pFS) {
   m_pFS = pFS;
   m_nCurPos = 0;
@@ -436,13 +440,11 @@ DLLEXPORT double STDCALL FPDF_GetPageWidth(FPDF_PAGE page) {
   if (!page)
     return 0.0;
   return ((CPDFXFA_Page*)page)->GetPageWidth();
-  //   return ((CPDF_Page*)page)->GetPageWidth();
 }
 
 DLLEXPORT double STDCALL FPDF_GetPageHeight(FPDF_PAGE page) {
   if (!page)
     return 0.0;
-  //   return ((CPDF_Page*)page)->GetPageHeight();
   return ((CPDFXFA_Page*)page)->GetPageHeight();
 }
 
@@ -463,9 +465,7 @@ DLLEXPORT void STDCALL FPDF_RenderPage(HDC dc,
                                        int size_y,
                                        int rotate,
                                        int flags) {
-  if (page == NULL)
-    return;
-  CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
+  CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
   if (!pPage)
     return;
 
@@ -625,12 +625,11 @@ DLLEXPORT void STDCALL FPDF_RenderPageBitmap(FPDF_BITMAP bitmap,
                                              int size_y,
                                              int rotate,
                                              int flags) {
-  if (bitmap == NULL || page == NULL)
+  if (!bitmap)
     return;
-  CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
+  CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
   if (!pPage)
     return;
-
   CRenderContext* pContext = new CRenderContext;
   pPage->SetPrivateData((void*)1, pContext, DropContext);
 #ifdef _SKIA_SUPPORT_
@@ -702,9 +701,11 @@ DLLEXPORT void STDCALL FPDF_PageToDevice(FPDF_PAGE page,
                                          double page_y,
                                          int* device_x,
                                          int* device_y) {
-  if (page == NULL || device_x == NULL || device_y == NULL)
+  if (!device_x || !device_y)
     return;
   CPDFXFA_Page* pPage = (CPDFXFA_Page*)page;
+  if (!pPage)
+    return;
   pPage->PageToDevice(start_x, start_y, size_x, size_y, rotate, page_x, page_y,
                       device_x, device_y);
 }
@@ -804,8 +805,8 @@ void FPDF_RenderPage_Retail(CRenderContext* pContext,
                             int flags,
                             FX_BOOL bNeedToRestore,
                             IFSDK_PAUSE_Adapter* pause) {
-  CPDF_Page* pPage = ((CPDFXFA_Page*)page)->GetPDFPage();
-  if (pPage == NULL)
+  CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
+  if (!pPage)
     return;
 
   if (!pContext->m_pOptions)