Clean up CPDF_AnnotList.
[pdfium.git] / fpdfsdk / src / fpdf_transformpage.cpp
index d0dee59..8a835fb 100644 (file)
@@ -9,10 +9,6 @@
 
 namespace {
 
-CPDF_Page* GetPageFromFPDFPage(FPDF_PAGE page) {
-  return static_cast<CPDF_Page*>(page);
-}
-
 void SetBoundingBox(CPDF_Page* page,
                     const CFX_ByteStringC& key,
                     float left,
@@ -53,7 +49,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;
 
@@ -65,7 +61,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;
 
@@ -77,7 +73,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);
 }
 
@@ -86,14 +82,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;
 
@@ -113,14 +109,15 @@ DLLEXPORT FPDF_BOOL STDCALL FPDFPage_TransFormWithClip(FPDF_PAGE page,
   textBuf << bsMatix;
 
   CPDF_Dictionary* pPageDic = pPage->m_pFormDict;
-  CPDF_Object* pContentObj = pPageDic ? pPageDic->GetElement("Contents") : NULL;
+  CPDF_Object* pContentObj =
+      pPageDic ? pPageDic->GetElement("Contents") : nullptr;
   if (!pContentObj)
-    pContentObj = pPageDic ? pPageDic->GetArray("Contents") : NULL;
+    pContentObj = pPageDic ? pPageDic->GetArray("Contents") : nullptr;
   if (!pContentObj)
     return FALSE;
 
   CPDF_Dictionary* pDic = new CPDF_Dictionary;
-  CPDF_Stream* pStream = new CPDF_Stream(NULL, 0, pDic);
+  CPDF_Stream* pStream = new CPDF_Stream(nullptr, 0, pDic);
   pStream->SetData(textBuf.GetBuffer(), textBuf.GetSize(), FALSE, FALSE);
   CPDF_Document* pDoc = pPage->m_pDocument;
   if (!pDoc)
@@ -128,27 +125,25 @@ DLLEXPORT FPDF_BOOL STDCALL FPDFPage_TransFormWithClip(FPDF_PAGE page,
   pDoc->AddIndirectObject(pStream);
 
   pDic = new CPDF_Dictionary;
-  CPDF_Stream* pEndStream = new CPDF_Stream(NULL, 0, pDic);
+  CPDF_Stream* pEndStream = new CPDF_Stream(nullptr, 0, pDic);
   pEndStream->SetData((const uint8_t*)" Q", 2, FALSE, FALSE);
   pDoc->AddIndirectObject(pEndStream);
 
-  CPDF_Array* pContentArray = NULL;
-  if (pContentObj && pContentObj->GetType() == PDFOBJ_ARRAY) {
-    pContentArray = (CPDF_Array*)pContentObj;
+  CPDF_Array* pContentArray = nullptr;
+  if (CPDF_Array* pArray = ToArray(pContentObj)) {
+    pContentArray = pArray;
     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;
+  } else if (CPDF_Reference* pReference = ToReference(pContentObj)) {
     CPDF_Object* pDirectObj = pReference->GetDirect();
-    if (pDirectObj != NULL) {
-      if (pDirectObj->GetType() == PDFOBJ_ARRAY) {
-        pContentArray = (CPDF_Array*)pDirectObj;
+    if (pDirectObj) {
+      if (CPDF_Array* pArray = pDirectObj->AsArray()) {
+        pContentArray = pArray;
         CPDF_Reference* pRef = new CPDF_Reference(pDoc, pStream->GetObjNum());
         pContentArray->InsertAt(0, pRef);
         pContentArray->AddReference(pDoc, pEndStream);
-      } else if (pDirectObj->GetType() == PDFOBJ_STREAM) {
+      } else if (pDirectObj->IsStream()) {
         pContentArray = new CPDF_Array();
         pContentArray->AddReference(pDoc, pStream->GetObjNum());
         pContentArray->AddReference(pDoc, pDirectObj->GetObjNum());
@@ -166,16 +161,17 @@ DLLEXPORT FPDF_BOOL STDCALL FPDFPage_TransFormWithClip(FPDF_PAGE page,
     if (pPattenDict) {
       FX_POSITION pos = pPattenDict->GetStartPos();
       while (pos) {
-        CPDF_Dictionary* pDict = NULL;
+        CPDF_Dictionary* pDict = nullptr;
         CFX_ByteString key;
         CPDF_Object* pObj = pPattenDict->GetNextElement(pos, key);
-        if (pObj->GetType() == PDFOBJ_REFERENCE)
+        if (pObj->IsReference())
           pObj = pObj->GetDirect();
-        if (pObj->GetType() == PDFOBJ_DICTIONARY) {
-          pDict = (CPDF_Dictionary*)pObj;
-        } else if (pObj->GetType() == PDFOBJ_STREAM) {
-          pDict = ((CPDF_Stream*)pObj)->GetDict();
-        } else
+
+        if (pObj->IsDictionary())
+          pDict = pObj->AsDictionary();
+        else if (CPDF_Stream* pStream = pObj->AsStream())
+          pDict = pStream->GetDict();
+        else
           continue;
 
         CFX_AffineMatrix m = pDict->GetMatrix(FX_BSTRC("Matrix"));
@@ -267,14 +263,15 @@ 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;
 
   CPDF_Dictionary* pPageDic = pPage->m_pFormDict;
-  CPDF_Object* pContentObj = pPageDic ? pPageDic->GetElement("Contents") : NULL;
+  CPDF_Object* pContentObj =
+      pPageDic ? pPageDic->GetElement("Contents") : nullptr;
   if (!pContentObj)
-    pContentObj = pPageDic ? pPageDic->GetArray("Contents") : NULL;
+    pContentObj = pPageDic ? pPageDic->GetArray("Contents") : nullptr;
   if (!pContentObj)
     return;
 
@@ -296,27 +293,26 @@ DLLEXPORT void STDCALL FPDFPage_InsertClipPath(FPDF_PAGE page,
     }
   }
   CPDF_Dictionary* pDic = new CPDF_Dictionary;
-  CPDF_Stream* pStream = new CPDF_Stream(NULL, 0, pDic);
+  CPDF_Stream* pStream = new CPDF_Stream(nullptr, 0, pDic);
   pStream->SetData(strClip.GetBuffer(), strClip.GetSize(), FALSE, FALSE);
   CPDF_Document* pDoc = pPage->m_pDocument;
   if (!pDoc)
     return;
   pDoc->AddIndirectObject(pStream);
 
-  CPDF_Array* pContentArray = NULL;
-  if (pContentObj && pContentObj->GetType() == PDFOBJ_ARRAY) {
-    pContentArray = (CPDF_Array*)pContentObj;
+  CPDF_Array* pContentArray = nullptr;
+  if (CPDF_Array* pArray = ToArray(pContentObj)) {
+    pContentArray = pArray;
     CPDF_Reference* pRef = new CPDF_Reference(pDoc, pStream->GetObjNum());
     pContentArray->InsertAt(0, pRef);
-  } else if (pContentObj && pContentObj->GetType() == PDFOBJ_REFERENCE) {
-    CPDF_Reference* pReference = (CPDF_Reference*)pContentObj;
+  } else if (CPDF_Reference* pReference = ToReference(pContentObj)) {
     CPDF_Object* pDirectObj = pReference->GetDirect();
-    if (pDirectObj != NULL) {
-      if (pDirectObj->GetType() == PDFOBJ_ARRAY) {
-        pContentArray = (CPDF_Array*)pDirectObj;
+    if (pDirectObj) {
+      if (CPDF_Array* pArray = pDirectObj->AsArray()) {
+        pContentArray = pArray;
         CPDF_Reference* pRef = new CPDF_Reference(pDoc, pStream->GetObjNum());
         pContentArray->InsertAt(0, pRef);
-      } else if (pDirectObj->GetType() == PDFOBJ_STREAM) {
+      } else if (pDirectObj->IsStream()) {
         pContentArray = new CPDF_Array();
         pContentArray->AddReference(pDoc, pStream->GetObjNum());
         pContentArray->AddReference(pDoc, pDirectObj->GetObjNum());