Remove FX_BSTR and FX_WSTR typedefs.
[pdfium.git] / core / src / fpdfapi / fpdf_page / fpdf_page_doc.cpp
index 097bd61..1c0629f 100644 (file)
@@ -1,7 +1,7 @@
 // Copyright 2014 PDFium Authors. All rights reserved.
 // Use of this source code is governed by a BSD-style license that can be
 // found in the LICENSE file.
+
 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
 
 #include "../../../include/fpdfapi/fpdf_page.h"
@@ -9,6 +9,78 @@
 #include "../../../include/fdrm/fx_crypt.h"
 #include "../fpdf_font/font_int.h"
 #include "pageint.h"
+
+namespace {
+
+template <class KeyType, class ValueType>
+KeyType PDF_DocPageData_FindValue(
+    const CFX_MapPtrTemplate<KeyType, CPDF_CountedObject<ValueType>*>& map,
+    ValueType findValue,
+    CPDF_CountedObject<ValueType>*& findData)
+{
+    FX_POSITION pos = map.GetStartPosition();
+    while (pos) {
+        KeyType findKey;
+        map.GetNextAssoc(pos, findKey, findData);
+        if (findData->m_Obj == findValue) {
+            return findKey;
+        }
+    }
+    findData = nullptr;
+    return (KeyType)nullptr;
+}
+
+template <class KeyType, class ValueType>
+void PDF_DocPageData_Release(
+    CFX_MapPtrTemplate<KeyType, CPDF_CountedObject<ValueType>*>& map,
+    KeyType findKey,
+    ValueType findValue,
+    FX_BOOL bForce)
+{
+    if (!findKey && !findValue)
+        return;
+
+    CPDF_CountedObject<ValueType>* findData = nullptr;
+    if (!findKey) {
+        findKey = PDF_DocPageData_FindValue<KeyType, ValueType>(map, findValue, findData);
+    } else if (!map.Lookup(findKey, findData)) {
+        return;
+    }
+    if (!findData)
+        return;
+
+    if ((-- findData->m_nCount) == 0 || bForce) {
+        delete findData->m_Obj;
+        delete findData;
+        map.RemoveKey(findKey);
+    }
+}
+
+template <class KeyType, class ValueType>
+void PDF_DocPageData_Release_Key(
+    KeyType findKey,
+    FX_BOOL bForce,
+    std::map<KeyType, CPDF_CountedObject<ValueType>*>* map)
+{
+    if (!findKey)
+        return;
+
+    CPDF_CountedObject<ValueType>* findData = nullptr;
+    auto it = map->find(findKey);
+    if (it != map->end())
+        findData = it->second;
+    if (!findData)
+        return;
+
+    if ((--findData->m_nCount) == 0 || bForce) {
+        delete findData->m_Obj;
+        delete findData;
+        map->erase(it);
+    }
+}
+
+}  // namespace
+
 class CPDF_PageModule : public CPDF_PageModuleDef
 {
 public:
@@ -21,7 +93,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 +131,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)
 {
@@ -107,9 +176,9 @@ CPDF_Pattern* CPDF_Document::LoadPattern(CPDF_Object* pPatternObj, FX_BOOL bShad
 {
     return GetValidatePageData()->GetPattern(pPatternObj, bShading, matrix);
 }
-CPDF_IccProfile* CPDF_Document::LoadIccProfile(CPDF_Stream* pStream, int nComponents)
+CPDF_IccProfile* CPDF_Document::LoadIccProfile(CPDF_Stream* pStream)
 {
-    return GetValidatePageData()->GetIccProfile(pStream, nComponents);
+    return GetValidatePageData()->GetIccProfile(pStream);
 }
 CPDF_Image* CPDF_Document::LoadImageF(CPDF_Object* pObj)
 {
@@ -127,199 +196,198 @@ void CPDF_Document::RemoveColorSpaceFromPageData(CPDF_Object* pCSObj)
     GetPageData()->ReleaseColorSpace(pCSObj);
 }
 CPDF_DocPageData::CPDF_DocPageData(CPDF_Document *pPDFDoc)
-    : m_pPDFDoc(pPDFDoc)
-    , m_FontMap()
-    , m_ColorSpaceMap()
-    , m_PatternMap()
-    , m_ImageMap()
-    , m_IccProfileMap()
-    , m_FontFileMap()
-{
-    m_FontMap.InitHashTable(64);
-    m_ColorSpaceMap.InitHashTable(32);
-    m_PatternMap.InitHashTable(16);
-    m_ImageMap.InitHashTable(64);
+    : m_pPDFDoc(pPDFDoc),
+      m_IccProfileMap(),
+      m_FontFileMap(),
+      m_bForceClear(FALSE)
+{
     m_IccProfileMap.InitHashTable(16);
     m_FontFileMap.InitHashTable(32);
 }
+
 CPDF_DocPageData::~CPDF_DocPageData()
 {
     Clear(FALSE);
     Clear(TRUE);
+
+    for (auto& it : m_PatternMap)
+        delete it.second;
+    m_PatternMap.clear();
+
+    for (auto& it : m_FontMap)
+        delete it.second;
+    m_FontMap.clear();
+
+    for (auto& it : m_ColorSpaceMap)
+        delete it.second;
+    m_ColorSpaceMap.clear();
 }
-void CPDF_DocPageData::Clear(FX_BOOL bRelease)
+
+void CPDF_DocPageData::Clear(FX_BOOL bForceRelease)
 {
-    FX_POSITION pos;
-    FX_DWORD   nCount;
-    {
-        pos = m_PatternMap.GetStartPosition();
-        while (pos) {
-            CPDF_Object* ptObj;
-            CPDF_CountedObject<CPDF_Pattern*>* ptData;
-            m_PatternMap.GetNextAssoc(pos, ptObj, ptData);
-            nCount = ptData->m_nCount;
-            if (bRelease || nCount < 2) {
-                delete ptData->m_Obj;
-                ptData->m_Obj = NULL;
-            }
+    m_bForceClear = bForceRelease;
+
+    for (auto& it : m_PatternMap) {
+        CPDF_CountedPattern* ptData = it.second;
+        if (!ptData->m_Obj)
+            continue;
+
+        if (bForceRelease || ptData->m_nCount < 2) {
+            ptData->m_Obj->SetForceClear(bForceRelease);
+            delete ptData->m_Obj;
+            ptData->m_Obj = nullptr;
         }
     }
-    {
-        pos = m_FontMap.GetStartPosition();
-        while (pos) {
-            CPDF_Dictionary* fontDict;
-            CPDF_CountedObject<CPDF_Font*>* fontData;
-            m_FontMap.GetNextAssoc(pos, fontDict, fontData);
-            nCount = fontData->m_nCount;
-            if (bRelease || nCount < 2) {
-                delete fontData->m_Obj;
-                fontData->m_Obj = NULL;
-            }
+
+    for (auto& it : m_FontMap) {
+        CPDF_CountedFont* fontData = it.second;
+        if (!fontData->m_Obj)
+            continue;
+
+        if (bForceRelease || fontData->m_nCount < 2) {
+            delete fontData->m_Obj;
+            fontData->m_Obj = nullptr;
         }
     }
-    {
-        pos = m_ImageMap.GetStartPosition();
-        while (pos) {
-            FX_DWORD objNum;
-            CPDF_CountedObject<CPDF_Image*>* imageData;
-            m_ImageMap.GetNextAssoc(pos, objNum, imageData);
-            nCount = imageData->m_nCount;
-            if (bRelease || nCount < 2) {
-                delete imageData->m_Obj;
-                delete imageData;
-                m_ImageMap.RemoveKey(objNum);
-            }
+
+    for (auto& it : m_ColorSpaceMap) {
+        CPDF_CountedColorSpace* csData = it.second;
+        if (!csData->m_Obj)
+            continue;
+
+        if (bForceRelease || csData->m_nCount < 2) {
+            csData->m_Obj->ReleaseCS();
+            csData->m_Obj = nullptr;
         }
     }
-    {
-        pos = m_ColorSpaceMap.GetStartPosition();
-        while (pos) {
-            CPDF_Object* csKey;
-            CPDF_CountedObject<CPDF_ColorSpace*>* csData;
-            m_ColorSpaceMap.GetNextAssoc(pos, csKey, csData);
-            nCount = csData->m_nCount;
-            if (bRelease || nCount < 2) {
-                csData->m_Obj->ReleaseCS();
-                csData->m_Obj = NULL;
-            }
+
+    FX_POSITION pos = m_IccProfileMap.GetStartPosition();
+    while (pos) {
+        CPDF_Stream* ipKey;
+        CPDF_CountedObject<CPDF_IccProfile*>* ipData;
+        m_IccProfileMap.GetNextAssoc(pos, ipKey, ipData);
+        if (!ipData->m_Obj) {
+            continue;
         }
-    }
-    {
-        pos = m_IccProfileMap.GetStartPosition();
-        while (pos) {
-            CPDF_Stream* ipKey;
-            CPDF_CountedObject<CPDF_IccProfile*>* ipData;
-            m_IccProfileMap.GetNextAssoc(pos, ipKey, ipData);
-            nCount = ipData->m_nCount;
-            if (bRelease || nCount < 2) {
-                FX_POSITION pos2 = m_HashProfileMap.GetStartPosition();
-                while (pos2) {
-                    CFX_ByteString bsKey;
-                    CPDF_Stream* pFindStream = NULL;
-                    m_HashProfileMap.GetNextAssoc(pos2, bsKey, (void*&)pFindStream);
-                    if (ipKey == pFindStream) {
-                        m_HashProfileMap.RemoveKey(bsKey);
-                        break;
-                    }
+        if (bForceRelease || ipData->m_nCount < 2) {
+            FX_POSITION pos2 = m_HashProfileMap.GetStartPosition();
+            while (pos2) {
+                CFX_ByteString bsKey;
+                CPDF_Stream* pFindStream = NULL;
+                m_HashProfileMap.GetNextAssoc(pos2, bsKey, (void*&)pFindStream);
+                if (ipKey == pFindStream) {
+                    m_HashProfileMap.RemoveKey(bsKey);
+                    break;
                 }
-                delete ipData->m_Obj;
-                delete ipData;
-                m_IccProfileMap.RemoveKey(ipKey);
             }
+            delete ipData->m_Obj;
+            delete ipData;
+            m_IccProfileMap.RemoveKey(ipKey);
         }
     }
-    {
-        pos = m_FontFileMap.GetStartPosition();
-        while (pos) {
-            CPDF_Stream* ftKey;
-            CPDF_CountedObject<CPDF_StreamAcc*>* ftData;
-            m_FontFileMap.GetNextAssoc(pos, ftKey, ftData);
-            nCount = ftData->m_nCount;
-            if (bRelease || nCount < 2) {
-                delete ftData->m_Obj;
-                delete ftData;
-                m_FontFileMap.RemoveKey(ftKey);
-            }
+    pos = m_FontFileMap.GetStartPosition();
+    while (pos) {
+        CPDF_Stream* ftKey;
+        CPDF_CountedObject<CPDF_StreamAcc*>* ftData;
+        m_FontFileMap.GetNextAssoc(pos, ftKey, ftData);
+        if (!ftData->m_Obj) {
+            continue;
+        }
+        if (bForceRelease || ftData->m_nCount < 2) {
+            delete ftData->m_Obj;
+            delete ftData;
+            m_FontFileMap.RemoveKey(ftKey);
+        }
+    }
+
+    for (auto it = m_ImageMap.begin(); it != m_ImageMap.end();) {
+        auto curr_it = it++;
+        CPDF_CountedImage* imageData = curr_it->second;
+        if (!imageData->m_Obj)
+            continue;
+
+        if (bForceRelease || imageData->m_nCount < 2) {
+            delete imageData->m_Obj;
+            delete imageData;
+            m_ImageMap.erase(curr_it);
         }
     }
 }
+
 CPDF_Font* CPDF_DocPageData::GetFont(CPDF_Dictionary* pFontDict, FX_BOOL findOnly)
 {
     if (!pFontDict) {
         return NULL;
     }
     if (findOnly) {
-        CPDF_CountedObject<CPDF_Font*>* fontData;
-        if (m_FontMap.Lookup(pFontDict, fontData)) {
-            if (!fontData->m_Obj) {
-                return NULL;
-            }
-            fontData->m_nCount ++;
+        auto it = m_FontMap.find(pFontDict);
+        if (it != m_FontMap.end()) {
+            CPDF_CountedFont* fontData = it->second;
+            if (!fontData->m_Obj)
+                return nullptr;
+
+            fontData->m_nCount++;
             return fontData->m_Obj;
         }
-        return NULL;
+        return nullptr;
     }
-    CPDF_CountedObject<CPDF_Font*>* fontData = NULL;
-    if (m_FontMap.Lookup(pFontDict, fontData)) {
+
+    CPDF_CountedFont* fontData = nullptr;
+    auto it = m_FontMap.find(pFontDict);
+    if (it != m_FontMap.end()) {
+        fontData = it->second;
         if (fontData->m_Obj) {
-            fontData->m_nCount ++;
+            fontData->m_nCount++;
             return fontData->m_Obj;
         }
     }
+
     FX_BOOL bNew = FALSE;
     if (!fontData) {
-        fontData = FX_NEW CPDF_CountedObject<CPDF_Font*>;
+        fontData = new CPDF_CountedFont;
         bNew = TRUE;
-        if (!fontData) {
-            return NULL;
-        }
     }
     CPDF_Font* pFont = CPDF_Font::CreateFontF(m_pPDFDoc, pFontDict);
     if (!pFont) {
-        if (bNew) {
+        if (bNew)
             delete fontData;
-        }
-        return NULL;
+        return nullptr;
     }
     fontData->m_nCount = 2;
     fontData->m_Obj = pFont;
-    m_FontMap.SetAt(pFontDict, fontData);
+    if (bNew)
+        m_FontMap[pFontDict] = fontData;
     return pFont;
 }
-CPDF_Font* CPDF_DocPageData::GetStandardFont(FX_BSTR fontName, CPDF_FontEncoding* pEncoding)
+
+CPDF_Font* CPDF_DocPageData::GetStandardFont(const CFX_ByteStringC& fontName, CPDF_FontEncoding* pEncoding)
 {
-    if (fontName.IsEmpty()) {
-        return NULL;
-    }
-    FX_POSITION pos = m_FontMap.GetStartPosition();
-    while (pos) {
-        CPDF_Dictionary* fontDict;
-        CPDF_CountedObject<CPDF_Font*>* fontData;
-        m_FontMap.GetNextAssoc(pos, fontDict, fontData);
+    if (fontName.IsEmpty())
+        return nullptr;
+
+    for (auto& it : m_FontMap) {
+        CPDF_CountedFont* fontData = it.second;
         CPDF_Font* pFont = fontData->m_Obj;
-        if (!pFont) {
+        if (!pFont)
             continue;
-        }
-        if (pFont->GetBaseFont() != fontName) {
+        if (pFont->GetBaseFont() != fontName)
             continue;
-        }
-        if (pFont->IsEmbedded()) {
+        if (pFont->IsEmbedded())
             continue;
-        }
-        if (pFont->GetFontType() != PDFFONT_TYPE1) {
+        if (pFont->GetFontType() != PDFFONT_TYPE1)
             continue;
-        }
-        if (pFont->GetFontDict()->KeyExist(FX_BSTRC("Widths"))) {
+        if (pFont->GetFontDict()->KeyExist(FX_BSTRC("Widths")))
             continue;
-        }
+
         CPDF_Type1Font* pT1Font = pFont->GetType1Font();
-        if (pEncoding && !pT1Font->GetEncoding()->IsIdentical(pEncoding)) {
+        if (pEncoding && !pT1Font->GetEncoding()->IsIdentical(pEncoding))
             continue;
-        }
-        fontData->m_nCount ++;
+
+        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);
@@ -327,34 +395,34 @@ 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_CountedFont* fontData = new CPDF_CountedFont;
     CPDF_Font* pFont = CPDF_Font::CreateFontF(m_pPDFDoc, pDict);
     if (!pFont) {
         delete fontData;
-        return NULL;
+        return nullptr;
     }
     fontData->m_nCount = 2;
     fontData->m_Obj = pFont;
-    m_FontMap.SetAt(pDict, fontData);
+    m_FontMap[pDict] = fontData;
     return pFont;
 }
+
 void CPDF_DocPageData::ReleaseFont(CPDF_Dictionary* pFontDict)
 {
-    if (!pFontDict) {
+    if (!pFontDict)
         return;
-    }
-    CPDF_CountedObject<CPDF_Font*>* fontData;
-    if (!m_FontMap.Lookup(pFontDict, fontData)) {
+
+    auto it = m_FontMap.find(pFontDict);
+    if (it == m_FontMap.end())
         return;
-    }
+
+    CPDF_CountedFont* fontData = it->second;
     if (fontData->m_Obj && --fontData->m_nCount == 0) {
         delete fontData->m_Obj;
-        fontData->m_Obj = NULL;
+        fontData->m_Obj = nullptr;
     }
 }
+
 CPDF_ColorSpace* CPDF_DocPageData::GetColorSpace(CPDF_Object* pCSObj, CPDF_Dictionary* pResources)
 {
     if (!pCSObj) {
@@ -394,165 +462,158 @@ CPDF_ColorSpace* CPDF_DocPageData::GetColorSpace(CPDF_Object* pCSObj, CPDF_Dicti
         }
         return GetColorSpace(pDefaultCS, NULL);
     }
-    if (pCSObj->GetType() != PDFOBJ_ARRAY) {
-        return NULL;
-    }
+
+    if (pCSObj->GetType() != PDFOBJ_ARRAY)
+        return nullptr;
     CPDF_Array* pArray = (CPDF_Array*)pCSObj;
-    if (pArray->GetCount() == 0) {
-        return NULL;
-    }
-    if (pArray->GetCount() == 1) {
+    if (pArray->GetCount() == 0)
+        return nullptr;
+    if (pArray->GetCount() == 1)
         return GetColorSpace(pArray->GetElementValue(0), pResources);
-    }
-    CPDF_CountedObject<CPDF_ColorSpace*>* csData = NULL;
-    if (m_ColorSpaceMap.Lookup(pCSObj, csData)) {
+
+    CPDF_CountedColorSpace* csData = nullptr;
+    auto it = m_ColorSpaceMap.find(pCSObj);
+    if (it != m_ColorSpaceMap.end()) {
+        csData = it->second;
         if (csData->m_Obj) {
             csData->m_nCount++;
             return csData->m_Obj;
         }
     }
-    FX_BOOL bNew = FALSE;
-    if (!csData) {
-        csData = FX_NEW CPDF_CountedObject<CPDF_ColorSpace*>;
-        if (!csData) {
-            return NULL;
-        }
-        bNew = TRUE;
-    }
+
     CPDF_ColorSpace* pCS = CPDF_ColorSpace::Load(m_pPDFDoc, pArray);
-    if (!pCS) {
-        if (bNew) {
-            delete csData;
-        }
-        return NULL;
+    if (!pCS)
+        return nullptr;
+
+    if (!csData) {
+        csData = new CPDF_CountedColorSpace;
+        m_ColorSpaceMap[pCSObj] = csData;
     }
     csData->m_nCount = 2;
     csData->m_Obj = pCS;
-    m_ColorSpaceMap.SetAt(pCSObj, csData);
     return pCS;
 }
+
 CPDF_ColorSpace* CPDF_DocPageData::GetCopiedColorSpace(CPDF_Object* pCSObj)
 {
-    if (!pCSObj) {
-        return NULL;
-    }
-    CPDF_CountedObject<CPDF_ColorSpace*>* csData;
-    if (!m_ColorSpaceMap.Lookup(pCSObj, csData)) {
-        return NULL;
-    }
-    if (!csData->m_Obj) {
-        return NULL;
-    }
-    csData->m_nCount ++;
+    if (!pCSObj)
+        return nullptr;
+
+    auto it = m_ColorSpaceMap.find(pCSObj);
+    if (it == m_ColorSpaceMap.end())
+        return nullptr;
+
+    CPDF_CountedColorSpace* csData = it->second;
+    if (!csData->m_Obj)
+        return nullptr;
+
+    csData->m_nCount++;
     return csData->m_Obj;
 }
+
 void CPDF_DocPageData::ReleaseColorSpace(CPDF_Object* pColorSpace)
 {
-    if (!pColorSpace) {
+    if (!pColorSpace)
         return;
-    }
-    CPDF_CountedObject<CPDF_ColorSpace*>* csData;
-    if (!m_ColorSpaceMap.Lookup(pColorSpace, csData)) {
+
+    auto it = m_ColorSpaceMap.find(pColorSpace);
+    if (it == m_ColorSpaceMap.end())
         return;
-    }
+
+    CPDF_CountedColorSpace* csData = it->second;
     if (csData->m_Obj && --csData->m_nCount == 0) {
         csData->m_Obj->ReleaseCS();
-        csData->m_Obj = NULL;
+        csData->m_Obj = nullptr;
     }
 }
+
 CPDF_Pattern* CPDF_DocPageData::GetPattern(CPDF_Object* pPatternObj, FX_BOOL bShading, const CFX_AffineMatrix* matrix)
 {
-    if (!pPatternObj) {
-        return NULL;
-    }
-    CPDF_CountedObject<CPDF_Pattern*>* ptData = NULL;
-    if (m_PatternMap.Lookup(pPatternObj, ptData)) {
+    if (!pPatternObj)
+        return nullptr;
+
+    CPDF_CountedPattern* ptData = nullptr;
+    auto it = m_PatternMap.find(pPatternObj);
+    if (it != m_PatternMap.end()) {
+        ptData = it->second;
         if (ptData->m_Obj) {
             ptData->m_nCount++;
             return ptData->m_Obj;
         }
     }
-    FX_BOOL bNew = FALSE;
-    if (!ptData) {
-        ptData = FX_NEW CPDF_CountedObject<CPDF_Pattern*>;
-        bNew = TRUE;
-        if (!ptData) {
-            return NULL;
-        }
-    }
-    CPDF_Pattern* pPattern = NULL;
+    CPDF_Pattern* pPattern = nullptr;
     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;
+        CPDF_Dictionary* pDict = pPatternObj ? pPatternObj->GetDict() : nullptr;
         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);
             }
         }
     }
-    if (!pPattern) {
-        if (bNew) {
-            delete ptData;
-        }
-        return NULL;
+    if (!pPattern)
+        return nullptr;
+
+    if (!ptData) {
+        ptData = new CPDF_CountedPattern;
+        m_PatternMap[pPatternObj] = ptData;
     }
     ptData->m_nCount = 2;
     ptData->m_Obj = pPattern;
-    m_PatternMap.SetAt(pPatternObj, ptData);
     return pPattern;
 }
+
 void CPDF_DocPageData::ReleasePattern(CPDF_Object* pPatternObj)
 {
-    if (!pPatternObj) {
+    if (!pPatternObj)
         return;
-    }
-    CPDF_CountedObject<CPDF_Pattern*>* ptData;
-    if (!m_PatternMap.Lookup(pPatternObj, ptData)) {
+
+    auto it = m_PatternMap.find(pPatternObj);
+    if (it == m_PatternMap.end())
         return;
-    }
+
+    CPDF_CountedPattern* ptData = it->second;
     if (ptData->m_Obj && --ptData->m_nCount == 0) {
         delete ptData->m_Obj;
-        ptData->m_Obj = NULL;
+        ptData->m_Obj = nullptr;
     }
 }
+
 CPDF_Image* CPDF_DocPageData::GetImage(CPDF_Object* pImageStream)
 {
-    if (!pImageStream) {
-        return NULL;
-    }
-    FX_DWORD dwImageObjNum = pImageStream->GetObjNum();
-    CPDF_CountedObject<CPDF_Image*>* imageData;
-    if (m_ImageMap.Lookup(dwImageObjNum, imageData)) {
-        imageData->m_nCount ++;
+    if (!pImageStream)
+        return nullptr;
+
+    const FX_DWORD dwImageObjNum = pImageStream->GetObjNum();
+    auto it = m_ImageMap.find(dwImageObjNum);
+    if (it != m_ImageMap.end()) {
+        CPDF_CountedImage* imageData = it->second;
+        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;
-    }
+    CPDF_CountedImage* imageData = new CPDF_CountedImage;
+    CPDF_Image* pImage = new CPDF_Image(m_pPDFDoc);
     pImage->LoadImageF((CPDF_Stream*)pImageStream, FALSE);
     imageData->m_nCount = 2;
     imageData->m_Obj = pImage;
-    m_ImageMap.SetAt(dwImageObjNum, imageData);
+    m_ImageMap[dwImageObjNum] = imageData;
     return pImage;
 }
+
 void CPDF_DocPageData::ReleaseImage(CPDF_Object* pImageStream)
 {
-    if (!pImageStream) {
+    if (!pImageStream)
         return;
-    }
-    PDF_DocPageData_Release<FX_DWORD, CPDF_Image*>(m_ImageMap, pImageStream->GetObjNum(), NULL);
+
+    PDF_DocPageData_Release_Key<FX_DWORD, CPDF_Image*>(
+        pImageStream->GetObjNum(), FALSE, &m_ImageMap);
 }
-CPDF_IccProfile* CPDF_DocPageData::GetIccProfile(CPDF_Stream* pIccProfileStream, FX_INT32 nComponents)
+
+CPDF_IccProfile* CPDF_DocPageData::GetIccProfile(CPDF_Stream* pIccProfileStream)
 {
     if (!pIccProfileStream) {
         return NULL;
@@ -564,7 +625,7 @@ CPDF_IccProfile* CPDF_DocPageData::GetIccProfile(CPDF_Stream* pIccProfileStream,
     }
     CPDF_StreamAcc stream;
     stream.LoadAllData(pIccProfileStream, FALSE);
-    FX_BYTE digest[20];
+    uint8_t digest[20];
     CPDF_Stream* pCopiedStream = NULL;
     CRYPT_SHA1Generate(stream.GetData(), stream.GetSize(), digest);
     if (m_HashProfileMap.Lookup(CFX_ByteStringC(digest, 20), (void*&)pCopiedStream)) {
@@ -572,41 +633,22 @@ 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(), nComponents);
-    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);
     m_HashProfileMap.SetAt(CFX_ByteStringC(digest, 20), pIccProfileStream);
     return pProfile;
 }
-void CPDF_DocPageData::ReleaseIccProfile(CPDF_Stream* pIccProfileStream, CPDF_IccProfile* pIccProfile)
+
+void CPDF_DocPageData::ReleaseIccProfile(CPDF_IccProfile* pIccProfile)
 {
-    if (!pIccProfileStream && !pIccProfile) {
-        return;
-    }
-    CPDF_CountedObject<CPDF_IccProfile*>* ipData = NULL;
-    if (m_IccProfileMap.Lookup(pIccProfileStream, ipData) && ipData->m_nCount < 2) {
-        FX_POSITION pos = m_HashProfileMap.GetStartPosition();
-        while (pos) {
-            CFX_ByteString key;
-            CPDF_Stream* pFindStream = NULL;
-            m_HashProfileMap.GetNextAssoc(pos, key, (void*&)pFindStream);
-            if (pIccProfileStream == pFindStream) {
-                m_HashProfileMap.RemoveKey(key);
-                break;
-            }
-        }
-    }
-    PDF_DocPageData_Release<CPDF_Stream*, CPDF_IccProfile*>(m_IccProfileMap, pIccProfileStream, pIccProfile);
+    ASSERT(pIccProfile);
+    PDF_DocPageData_Release<CPDF_Stream*, CPDF_IccProfile*>(
+        m_IccProfileMap, nullptr, pIccProfile, FALSE);
 }
+
 CPDF_StreamAcc* CPDF_DocPageData::GetFontFileStreamAcc(CPDF_Stream* pFontStream)
 {
     if (!pFontStream) {
@@ -617,17 +659,10 @@ 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"));
+    int32_t org_size = pFontDict->GetInteger(FX_BSTRC("Length1")) + pFontDict->GetInteger(FX_BSTRC("Length2")) + pFontDict->GetInteger(FX_BSTRC("Length3"));
     if (org_size < 0) {
         org_size = 0;
     }
@@ -644,3 +679,21 @@ 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 nullptr;
+
+    auto it = m_ColorSpaceMap.find(pCSObj);
+    return it != m_ColorSpaceMap.end() ? it->second : nullptr;
+}
+
+CPDF_CountedPattern* CPDF_DocPageData::FindPatternPtr(CPDF_Object* pPatternObj) const
+{
+    if (!pPatternObj)
+        return nullptr;
+
+    auto it = m_PatternMap.find(pPatternObj);
+    return it != m_PatternMap.end() ? it->second : nullptr;
+}