Revert "Revert "Add type cast definitions for CPDF_Dictionary.""
[pdfium.git] / fpdfsdk / src / fpdf_transformpage.cpp
index 71ba48e..1e24b68 100644 (file)
@@ -7,22 +7,53 @@
 #include "../../public/fpdf_transformpage.h"
 #include "../include/fsdk_define.h"
 
+namespace {
+
+void SetBoundingBox(CPDF_Page* page,
+                    const CFX_ByteStringC& key,
+                    float left,
+                    float bottom,
+                    float right,
+                    float top) {
+  CPDF_Dictionary* pPageDict = page->m_pFormDict;
+  CPDF_Array* pBoundingBoxArray = new CPDF_Array;
+  pBoundingBoxArray->Add(new CPDF_Number(left));
+  pBoundingBoxArray->Add(new CPDF_Number(bottom));
+  pBoundingBoxArray->Add(new CPDF_Number(right));
+  pBoundingBoxArray->Add(new CPDF_Number(top));
+  pPageDict->SetAt(key, pBoundingBoxArray);
+}
+
+FPDF_BOOL GetBoundingBox(CPDF_Page* page,
+                         const CFX_ByteStringC& key,
+                         float* left,
+                         float* bottom,
+                         float* right,
+                         float* top) {
+  CPDF_Dictionary* pPageDict = page->m_pFormDict;
+  CPDF_Array* pArray = pPageDict->GetArray(key);
+  if (!pArray)
+    return FALSE;
+
+  *left = pArray->GetFloat(0);
+  *bottom = pArray->GetFloat(1);
+  *right = pArray->GetFloat(2);
+  *top = pArray->GetFloat(3);
+  return TRUE;
+}
+
+}  // namespace
+
 DLLEXPORT void STDCALL FPDFPage_SetMediaBox(FPDF_PAGE page,
                                             float left,
                                             float bottom,
                                             float right,
                                             float top) {
-  if (!page)
+  CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
+  if (!pPage)
     return;
 
-  CPDF_Page* pPage = (CPDF_Page*)page;
-  CPDF_Dictionary* pPageDict = pPage->m_pFormDict;
-  CPDF_Array* pMediaBoxArray = new CPDF_Array;
-  pMediaBoxArray->Add(new CPDF_Number(left));
-  pMediaBoxArray->Add(new CPDF_Number(bottom));
-  pMediaBoxArray->Add(new CPDF_Number(FX_FLOAT(right)));
-  pMediaBoxArray->Add(new CPDF_Number(FX_FLOAT(top)));
-  pPageDict->SetAt("MediaBox", pMediaBoxArray);
+  SetBoundingBox(pPage, "MediaBox", left, bottom, right, top);
 }
 
 DLLEXPORT void STDCALL FPDFPage_SetCropBox(FPDF_PAGE page,
@@ -30,17 +61,11 @@ DLLEXPORT void STDCALL FPDFPage_SetCropBox(FPDF_PAGE page,
                                            float bottom,
                                            float right,
                                            float top) {
-  if (!page)
+  CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
+  if (!pPage)
     return;
 
-  CPDF_Page* pPage = (CPDF_Page*)page;
-  CPDF_Dictionary* pPageDict = pPage->m_pFormDict;
-  CPDF_Array* pCropBoxArray = new CPDF_Array;
-  pCropBoxArray->Add(new CPDF_Number(left));
-  pCropBoxArray->Add(new CPDF_Number(bottom));
-  pCropBoxArray->Add(new CPDF_Number(FX_FLOAT(right)));
-  pCropBoxArray->Add(new CPDF_Number(FX_FLOAT(top)));
-  pPageDict->SetAt("CropBox", pCropBoxArray);
+  SetBoundingBox(pPage, "CropBox", left, bottom, right, top);
 }
 
 DLLEXPORT FPDF_BOOL STDCALL FPDFPage_GetMediaBox(FPDF_PAGE page,
@@ -48,19 +73,8 @@ DLLEXPORT FPDF_BOOL STDCALL FPDFPage_GetMediaBox(FPDF_PAGE page,
                                                  float* bottom,
                                                  float* right,
                                                  float* top) {
-  if (!page)
-    return FALSE;
-  CPDF_Page* pPage = (CPDF_Page*)page;
-  CPDF_Dictionary* pPageDict = pPage->m_pFormDict;
-  CPDF_Array* pArray = pPageDict->GetArray("MediaBox");
-  if (pArray) {
-    *left = pArray->GetFloat(0);
-    *bottom = pArray->GetFloat(1);
-    *right = pArray->GetFloat(2);
-    *top = pArray->GetFloat(3);
-    return TRUE;
-  }
-  return FALSE;
+  CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
+  return pPage && GetBoundingBox(pPage, "MediaBox", left, bottom, right, top);
 }
 
 DLLEXPORT FPDF_BOOL STDCALL FPDFPage_GetCropBox(FPDF_PAGE page,
@@ -68,25 +82,15 @@ DLLEXPORT FPDF_BOOL STDCALL FPDFPage_GetCropBox(FPDF_PAGE page,
                                                 float* bottom,
                                                 float* right,
                                                 float* top) {
-  if (!page)
-    return FALSE;
-  CPDF_Page* pPage = (CPDF_Page*)page;
-  CPDF_Dictionary* pPageDict = pPage->m_pFormDict;
-  CPDF_Array* pArray = pPageDict->GetArray("CropBox");
-  if (pArray) {
-    *left = pArray->GetFloat(0);
-    *bottom = pArray->GetFloat(1);
-    *right = pArray->GetFloat(2);
-    *top = pArray->GetFloat(3);
-    return TRUE;
-  }
-  return FALSE;
+  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) {
-  if (!page)
+  CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
+  if (!pPage)
     return FALSE;
 
   CFX_ByteTextBuf textBuf;
@@ -104,7 +108,6 @@ DLLEXPORT FPDF_BOOL STDCALL FPDFPage_TransFormWithClip(FPDF_PAGE page,
                  matrix->d, matrix->e, matrix->f);
   textBuf << bsMatix;
 
-  CPDF_Page* pPage = (CPDF_Page*)page;
   CPDF_Dictionary* pPageDic = pPage->m_pFormDict;
   CPDF_Object* pContentObj = pPageDic ? pPageDic->GetElement("Contents") : NULL;
   if (!pContentObj)
@@ -131,7 +134,6 @@ DLLEXPORT FPDF_BOOL STDCALL FPDFPage_TransFormWithClip(FPDF_PAGE page,
     CPDF_Reference* pRef = new CPDF_Reference(pDoc, pStream->GetObjNum());
     pContentArray->InsertAt(0, pRef);
     pContentArray->AddReference(pDoc, pEndStream);
-
   } else if (pContentObj && pContentObj->GetType() == PDFOBJ_REFERENCE) {
     CPDF_Reference* pReference = (CPDF_Reference*)pContentObj;
     CPDF_Object* pDirectObj = pReference->GetDirect();
@@ -164,8 +166,8 @@ DLLEXPORT FPDF_BOOL STDCALL FPDFPage_TransFormWithClip(FPDF_PAGE page,
         CPDF_Object* pObj = pPattenDict->GetNextElement(pos, key);
         if (pObj->GetType() == PDFOBJ_REFERENCE)
           pObj = pObj->GetDirect();
-        if (pObj->GetType() == PDFOBJ_DICTIONARY) {
-          pDict = (CPDF_Dictionary*)pObj;
+        if (pObj->IsDictionary()) {
+          pDict = pObj->AsDictionary();
         } else if (pObj->GetType() == PDFOBJ_STREAM) {
           pDict = ((CPDF_Stream*)pObj)->GetDict();
         } else
@@ -260,9 +262,10 @@ void OutputPath(CFX_ByteTextBuf& buf, CPDF_Path path) {
 
 DLLEXPORT void STDCALL FPDFPage_InsertClipPath(FPDF_PAGE page,
                                                FPDF_CLIPPATH clipPath) {
-  if (!page)
+  CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
+  if (!pPage)
     return;
-  CPDF_Page* pPage = (CPDF_Page*)page;
+
   CPDF_Dictionary* pPageDic = pPage->m_pFormDict;
   CPDF_Object* pContentObj = pPageDic ? pPageDic->GetElement("Contents") : NULL;
   if (!pContentObj)