Remove FX_Alloc() null checks now that it can't return NULL.
[pdfium.git] / core / src / fpdfapi / fpdf_page / fpdf_page_doc.cpp
index 8e578f6..241b589 100644 (file)
@@ -21,7 +21,7 @@ public:
     }
     virtual CPDF_DocPageData*  CreateDocData(CPDF_Document* pDoc)
     {
-        return FX_NEW CPDF_DocPageData(pDoc);
+        return new CPDF_DocPageData(pDoc);
     }
     virtual void               ReleaseDoc(CPDF_Document* pDoc);
     virtual void               ClearDoc(CPDF_Document* pDoc);
@@ -59,11 +59,8 @@ CPDF_ColorSpace* CPDF_PageModule::GetStockCS(int family)
 }
 void CPDF_ModuleMgr::InitPageModule()
 {
-    if (m_pPageModule) {
-        delete m_pPageModule;
-    }
-    CPDF_PageModule* pPageModule = FX_NEW CPDF_PageModule;
-    m_pPageModule = pPageModule;
+    delete m_pPageModule;
+    m_pPageModule = new CPDF_PageModule;
 }
 void CPDF_PageModule::ReleaseDoc(CPDF_Document* pDoc)
 {
@@ -147,15 +144,52 @@ CPDF_DocPageData::~CPDF_DocPageData()
 {
     Clear(FALSE);
     Clear(TRUE);
+    FX_POSITION pos = m_PatternMap.GetStartPosition();
+    while (pos)
+    {
+        CPDF_Object* ptObj;
+        CPDF_CountedObject<CPDF_Pattern*>* ptData;
+        m_PatternMap.GetNextAssoc(pos, ptObj, ptData);
+        delete ptData;
+    }
+    m_PatternMap.RemoveAll();
+    pos = m_FontMap.GetStartPosition();
+    while (pos)
+    {
+        CPDF_Dictionary* fontDict;
+        CPDF_CountedObject<CPDF_Font*>* fontData;
+        m_FontMap.GetNextAssoc(pos, fontDict, fontData);
+        delete fontData;
+    }
+    m_FontMap.RemoveAll();
+    pos = m_ColorSpaceMap.GetStartPosition();
+    while (pos)
+    {
+        CPDF_Object* csKey;
+        CPDF_CountedObject<CPDF_ColorSpace*>* csData;
+        m_ColorSpaceMap.GetNextAssoc(pos, csKey, csData);
+        delete csData;
+    }
+    m_ColorSpaceMap.RemoveAll();
 }
 void CPDF_DocPageData::Clear(FX_BOOL bForceRelease)
 {
     FX_POSITION pos;
-
     m_bForceClear = bForceRelease;
-
-    // Release objects saved in the resource maps like font map and color space map.
-    // The compound objects shall be released before simple ones.
+    pos = m_PatternMap.GetStartPosition();
+    while (pos) {
+        CPDF_Object* ptObj;
+        CPDF_CountedObject<CPDF_Pattern*>* ptData;
+        m_PatternMap.GetNextAssoc(pos, ptObj, ptData);
+        if (!ptData->m_Obj) {
+            continue;
+        }
+        if (bForceRelease || ptData->m_nCount < 2) {
+            ptData->m_Obj->SetForceClear(bForceRelease);
+            delete ptData->m_Obj;
+            ptData->m_Obj = NULL;
+        }
+    }
     pos = m_FontMap.GetStartPosition();
     while (pos) {
         CPDF_Dictionary* fontDict;
@@ -203,7 +237,6 @@ void CPDF_DocPageData::Clear(FX_BOOL bForceRelease)
                 }
             }
             delete ipData->m_Obj;
-            ipData->m_Obj = NULL;
             delete ipData;
             m_IccProfileMap.RemoveKey(ipKey);
         }
@@ -218,25 +251,10 @@ void CPDF_DocPageData::Clear(FX_BOOL bForceRelease)
         }
         if (bForceRelease || ftData->m_nCount < 2) {
             delete ftData->m_Obj;
-            ftData->m_Obj = NULL;
             delete ftData;
             m_FontFileMap.RemoveKey(ftKey);
         }
     }
-    pos = m_PatternMap.GetStartPosition();
-    while (pos) {
-        CPDF_Object* ptObj;
-        CPDF_CountedObject<CPDF_Pattern*>* ptData;
-        m_PatternMap.GetNextAssoc(pos, ptObj, ptData);
-        if (!ptData->m_Obj) {
-            continue;
-        }
-        if (bForceRelease || ptData->m_nCount < 2) {
-            ptData->m_Obj->SetForceClear(bForceRelease);
-            delete ptData->m_Obj;
-            ptData->m_Obj = NULL;
-        }
-    }
     pos = m_ImageMap.GetStartPosition();
     while (pos) {
         FX_DWORD objNum;
@@ -247,7 +265,6 @@ void CPDF_DocPageData::Clear(FX_BOOL bForceRelease)
         }
         if (bForceRelease || imageData->m_nCount < 2) {
             delete imageData->m_Obj;
-            imageData->m_Obj = NULL;
             delete imageData;
             m_ImageMap.RemoveKey(objNum);
         }
@@ -278,11 +295,8 @@ CPDF_Font* CPDF_DocPageData::GetFont(CPDF_Dictionary* pFontDict, FX_BOOL findOnl
     }
     FX_BOOL bNew = FALSE;
     if (!fontData) {
-        fontData = FX_NEW CPDF_CountedObject<CPDF_Font*>;
+        fontData = new CPDF_CountedObject<CPDF_Font*>;
         bNew = TRUE;
-        if (!fontData) {
-            return NULL;
-        }
     }
     CPDF_Font* pFont = CPDF_Font::CreateFontF(m_pPDFDoc, pFontDict);
     if (!pFont) {
@@ -329,7 +343,7 @@ CPDF_Font* CPDF_DocPageData::GetStandardFont(FX_BSTR fontName, CPDF_FontEncoding
         fontData->m_nCount ++;
         return pFont;
     }
-    CPDF_Dictionary* pDict = FX_NEW CPDF_Dictionary;
+    CPDF_Dictionary* pDict = new CPDF_Dictionary;
     pDict->SetAtName(FX_BSTRC("Type"), FX_BSTRC("Font"));
     pDict->SetAtName(FX_BSTRC("Subtype"), FX_BSTRC("Type1"));
     pDict->SetAtName(FX_BSTRC("BaseFont"), fontName);
@@ -337,10 +351,7 @@ CPDF_Font* CPDF_DocPageData::GetStandardFont(FX_BSTR fontName, CPDF_FontEncoding
         pDict->SetAt(FX_BSTRC("Encoding"), pEncoding->Realize());
     }
     m_pPDFDoc->AddIndirectObject(pDict);
-    CPDF_CountedObject<CPDF_Font*>* fontData = FX_NEW CPDF_CountedObject<CPDF_Font*>;
-    if (!fontData) {
-        return NULL;
-    }
+    CPDF_CountedObject<CPDF_Font*>* fontData = new CPDF_CountedObject<CPDF_Font*>;
     CPDF_Font* pFont = CPDF_Font::CreateFontF(m_pPDFDoc, pDict);
     if (!pFont) {
         delete fontData;
@@ -423,10 +434,7 @@ CPDF_ColorSpace* CPDF_DocPageData::GetColorSpace(CPDF_Object* pCSObj, CPDF_Dicti
     }
     FX_BOOL bNew = FALSE;
     if (!csData) {
-        csData = FX_NEW CPDF_CountedObject<CPDF_ColorSpace*>;
-        if (!csData) {
-            return NULL;
-        }
+        csData = new CPDF_CountedObject<CPDF_ColorSpace*>;
         bNew = TRUE;
     }
     CPDF_ColorSpace* pCS = CPDF_ColorSpace::Load(m_pPDFDoc, pArray);
@@ -484,23 +492,20 @@ CPDF_Pattern* CPDF_DocPageData::GetPattern(CPDF_Object* pPatternObj, FX_BOOL bSh
     }
     FX_BOOL bNew = FALSE;
     if (!ptData) {
-        ptData = FX_NEW CPDF_CountedObject<CPDF_Pattern*>;
+        ptData = new CPDF_CountedObject<CPDF_Pattern*>;
         bNew = TRUE;
-        if (!ptData) {
-            return NULL;
-        }
     }
     CPDF_Pattern* pPattern = NULL;
     if (bShading) {
-        pPattern = FX_NEW CPDF_ShadingPattern(m_pPDFDoc, pPatternObj, bShading, matrix);
+        pPattern = new CPDF_ShadingPattern(m_pPDFDoc, pPatternObj, bShading, matrix);
     } else {
         CPDF_Dictionary* pDict = pPatternObj ? pPatternObj->GetDict() : NULL;
         if (pDict) {
             int type = pDict->GetInteger(FX_BSTRC("PatternType"));
             if (type == 1) {
-                pPattern = FX_NEW CPDF_TilingPattern(m_pPDFDoc, pPatternObj, matrix);
+                pPattern = new CPDF_TilingPattern(m_pPDFDoc, pPatternObj, matrix);
             } else if (type == 2) {
-                pPattern = FX_NEW CPDF_ShadingPattern(m_pPDFDoc, pPatternObj, FALSE, matrix);
+                pPattern = new CPDF_ShadingPattern(m_pPDFDoc, pPatternObj, FALSE, matrix);
             }
         }
     }
@@ -540,15 +545,8 @@ CPDF_Image* CPDF_DocPageData::GetImage(CPDF_Object* pImageStream)
         imageData->m_nCount ++;
         return imageData->m_Obj;
     }
-    imageData = FX_NEW CPDF_CountedObject<CPDF_Image*>;
-    if (!imageData) {
-        return NULL;
-    }
-    CPDF_Image* pImage = FX_NEW CPDF_Image(m_pPDFDoc);
-    if (!pImage) {
-        delete imageData;
-        return NULL;
-    }
+    imageData = new CPDF_CountedObject<CPDF_Image*>;
+    CPDF_Image* pImage = new CPDF_Image(m_pPDFDoc);
     pImage->LoadImageF((CPDF_Stream*)pImageStream, FALSE);
     imageData->m_nCount = 2;
     imageData->m_Obj = pImage;
@@ -582,15 +580,8 @@ CPDF_IccProfile* CPDF_DocPageData::GetIccProfile(CPDF_Stream* pIccProfileStream)
         ipData->m_nCount++;
         return ipData->m_Obj;
     }
-    CPDF_IccProfile* pProfile = FX_NEW CPDF_IccProfile(stream.GetData(), stream.GetSize());
-    if (!pProfile) {
-        return NULL;
-    }
-    ipData = FX_NEW CPDF_CountedObject<CPDF_IccProfile*>;
-    if (!ipData) {
-        delete pProfile;
-        return NULL;
-    }
+    CPDF_IccProfile* pProfile = new CPDF_IccProfile(stream.GetData(), stream.GetSize());
+    ipData = new CPDF_CountedObject<CPDF_IccProfile*>;
     ipData->m_nCount = 2;
     ipData->m_Obj = pProfile;
     m_IccProfileMap.SetAt(pIccProfileStream, ipData);
@@ -627,15 +618,8 @@ CPDF_StreamAcc* CPDF_DocPageData::GetFontFileStreamAcc(CPDF_Stream* pFontStream)
         ftData->m_nCount ++;
         return ftData->m_Obj;
     }
-    ftData = FX_NEW CPDF_CountedObject<CPDF_StreamAcc*>;
-    if (!ftData) {
-        return NULL;
-    }
-    CPDF_StreamAcc* pFontFile = FX_NEW CPDF_StreamAcc;
-    if (!pFontFile) {
-        delete ftData;
-        return NULL;
-    }
+    ftData = new CPDF_CountedObject<CPDF_StreamAcc*>;
+    CPDF_StreamAcc* pFontFile = new CPDF_StreamAcc;
     CPDF_Dictionary* pFontDict = pFontStream->GetDict();
     FX_INT32 org_size = pFontDict->GetInteger(FX_BSTRC("Length1")) + pFontDict->GetInteger(FX_BSTRC("Length2")) + pFontDict->GetInteger(FX_BSTRC("Length3"));
     if (org_size < 0) {
@@ -654,3 +638,23 @@ void CPDF_DocPageData::ReleaseFontFileStreamAcc(CPDF_Stream* pFontStream, FX_BOO
     }
     PDF_DocPageData_Release<CPDF_Stream*, CPDF_StreamAcc*>(m_FontFileMap, pFontStream, NULL, bForce);
 }
+CPDF_CountedColorSpace* CPDF_DocPageData::FindColorSpacePtr(CPDF_Object* pCSObj) const
+{
+    if (!pCSObj) return NULL;
+    CPDF_CountedObject<CPDF_ColorSpace*>* csData;
+    if (m_ColorSpaceMap.Lookup(pCSObj, csData))
+    {
+        return csData;
+    }
+    return NULL;
+}
+CPDF_CountedPattern* CPDF_DocPageData::FindPatternPtr(CPDF_Object* pPatternObj) const
+{
+    if (!pPatternObj) return NULL;
+    CPDF_CountedObject<CPDF_Pattern*>* ptData;
+    if (m_PatternMap.Lookup(pPatternObj, ptData))
+    {
+        return ptData;
+    }
+    return NULL;
+}