From a8a718c974210a919f2af0b099d540143cf330c6 Mon Sep 17 00:00:00 2001 From: Lei Zhang Date: Wed, 10 Jun 2015 15:25:22 -0700 Subject: Convert CPDF_ImageMap to std::map. Also simplify CPDF_DocPageData::ReleaseIccProfile() since its first argument is always null. R=tsepez@chromium.org Review URL: https://codereview.chromium.org/1149273004. --- core/include/fpdfapi/fpdf_resource.h | 2 - core/src/fpdfapi/fpdf_page/fpdf_page_colors.cpp | 2 +- core/src/fpdfapi/fpdf_page/fpdf_page_doc.cpp | 140 +++++++++++++++++------- core/src/fpdfapi/fpdf_page/pageint.h | 41 +------ 4 files changed, 105 insertions(+), 80 deletions(-) (limited to 'core') diff --git a/core/include/fpdfapi/fpdf_resource.h b/core/include/fpdfapi/fpdf_resource.h index cd190b9d30..501e3be2f0 100644 --- a/core/include/fpdfapi/fpdf_resource.h +++ b/core/include/fpdfapi/fpdf_resource.h @@ -43,14 +43,12 @@ public: ObjClass m_Obj; FX_DWORD m_nCount; }; -typedef CPDF_CountedObject CPDF_CountedImage; typedef CPDF_CountedObject CPDF_CountedICCProfile; typedef CPDF_CountedObject CPDF_CountedStreamAcc; using CPDF_CountedColorSpace = CPDF_CountedObject; using CPDF_CountedPattern = CPDF_CountedObject; -typedef CFX_MapPtrTemplate CPDF_ImageMap; typedef CFX_MapPtrTemplate CPDF_IccProfileMap; typedef CFX_MapPtrTemplate CPDF_FontFileMap; diff --git a/core/src/fpdfapi/fpdf_page/fpdf_page_colors.cpp b/core/src/fpdfapi/fpdf_page/fpdf_page_colors.cpp index 1b989be040..dc3d454656 100644 --- a/core/src/fpdfapi/fpdf_page/fpdf_page_colors.cpp +++ b/core/src/fpdfapi/fpdf_page/fpdf_page_colors.cpp @@ -583,7 +583,7 @@ CPDF_ICCBasedCS::~CPDF_ICCBasedCS() m_pAlterCS->ReleaseCS(); } if (m_pProfile && m_pDocument) { - m_pDocument->GetPageData()->ReleaseIccProfile(NULL, m_pProfile); + m_pDocument->GetPageData()->ReleaseIccProfile(m_pProfile); } } FX_BOOL CPDF_ICCBasedCS::v_Load(CPDF_Document* pDoc, CPDF_Array* pArray) diff --git a/core/src/fpdfapi/fpdf_page/fpdf_page_doc.cpp b/core/src/fpdfapi/fpdf_page/fpdf_page_doc.cpp index b277b3c9a0..6c3d8c6a29 100644 --- a/core/src/fpdfapi/fpdf_page/fpdf_page_doc.cpp +++ b/core/src/fpdfapi/fpdf_page/fpdf_page_doc.cpp @@ -10,6 +10,77 @@ #include "../fpdf_font/font_int.h" #include "pageint.h" +namespace { + +template +KeyType PDF_DocPageData_FindValue( + const CFX_MapPtrTemplate*>& map, + ValueType findValue, + CPDF_CountedObject*& 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 +void PDF_DocPageData_Release( + CFX_MapPtrTemplate*>& map, + KeyType findKey, + ValueType findValue, + FX_BOOL bForce) +{ + if (!findKey && !findValue) + return; + + CPDF_CountedObject* findData = nullptr; + if (!findKey) { + findKey = PDF_DocPageData_FindValue(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 +void PDF_DocPageData_Release_Key( + KeyType findKey, + FX_BOOL bForce, + std::map*>* map) +{ + if (!findKey) + return; + + CPDF_CountedObject* 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: @@ -126,12 +197,10 @@ void CPDF_Document::RemoveColorSpaceFromPageData(CPDF_Object* pCSObj) } CPDF_DocPageData::CPDF_DocPageData(CPDF_Document *pPDFDoc) : m_pPDFDoc(pPDFDoc), - m_ImageMap(), m_IccProfileMap(), m_FontFileMap(), m_bForceClear(FALSE) { - m_ImageMap.InitHashTable(64); m_IccProfileMap.InitHashTable(16); m_FontFileMap.InitHashTable(32); } @@ -230,18 +299,17 @@ void CPDF_DocPageData::Clear(FX_BOOL bForceRelease) m_FontFileMap.RemoveKey(ftKey); } } - pos = m_ImageMap.GetStartPosition(); - while (pos) { - FX_DWORD objNum; - CPDF_CountedObject* imageData; - m_ImageMap.GetNextAssoc(pos, objNum, imageData); - if (!imageData->m_Obj) { + + 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.RemoveKey(objNum); + m_ImageMap.erase(curr_it); } } } @@ -517,30 +585,34 @@ void CPDF_DocPageData::ReleasePattern(CPDF_Object* pPatternObj) CPDF_Image* CPDF_DocPageData::GetImage(CPDF_Object* pImageStream) { - if (!pImageStream) { - return NULL; - } - FX_DWORD dwImageObjNum = pImageStream->GetObjNum(); - CPDF_CountedObject* 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 = new CPDF_CountedObject; + 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(m_ImageMap, pImageStream->GetObjNum(), NULL); + + PDF_DocPageData_Release_Key( + pImageStream->GetObjNum(), FALSE, &m_ImageMap); } + CPDF_IccProfile* CPDF_DocPageData::GetIccProfile(CPDF_Stream* pIccProfileStream) { if (!pIccProfileStream) { @@ -569,26 +641,14 @@ CPDF_IccProfile* CPDF_DocPageData::GetIccProfile(CPDF_Stream* pIccProfileStream) 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* 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(m_IccProfileMap, pIccProfileStream, pIccProfile); + ASSERT(pIccProfile); + PDF_DocPageData_Release( + m_IccProfileMap, nullptr, pIccProfile, FALSE); } + CPDF_StreamAcc* CPDF_DocPageData::GetFontFileStreamAcc(CPDF_Stream* pFontStream) { if (!pFontStream) { diff --git a/core/src/fpdfapi/fpdf_page/pageint.h b/core/src/fpdfapi/fpdf_page/pageint.h index 2855969e46..3e6abc2059 100644 --- a/core/src/fpdfapi/fpdf_page/pageint.h +++ b/core/src/fpdfapi/fpdf_page/pageint.h @@ -12,7 +12,6 @@ #include "../../../include/fpdfapi/fpdf_pageobj.h" #define PARSE_STEP_LIMIT 100 -#define STREAM_PARSE_BUFSIZE 20480 class CPDF_StreamParser { @@ -328,40 +327,6 @@ public: FX_FLOAT m_TextLeading, m_TextRise, m_TextHorzScale; }; -template -KeyType PDF_DocPageData_FindValue(const CFX_MapPtrTemplate*> &map, ValueType findValue, CPDF_CountedObject*& findData) -{ - FX_POSITION pos = map.GetStartPosition(); - while (pos) { - KeyType findKey; - map.GetNextAssoc(pos, findKey, findData); - if (findData->m_Obj == findValue) { - return findKey; - } - } - findData = NULL; - return (KeyType)(uintptr_t)NULL; -} -template -FX_BOOL PDF_DocPageData_Release(CFX_MapPtrTemplate*> &map, KeyType findKey, ValueType findValue, FX_BOOL bForce = FALSE) -{ - if (!findKey && !findValue) { - return FALSE; - } - CPDF_CountedObject* findData = NULL; - if (!findKey) { - findKey = PDF_DocPageData_FindValue(map, findValue, findData); - } else if (!map.Lookup(findKey, findData)) { - return FALSE; - } - if (findData && ((-- findData->m_nCount) == 0 || bForce)) { - delete findData->m_Obj; - delete findData; - map.RemoveKey(findKey); - return TRUE; - } - return FALSE; -} class CPDF_DocPageData { public: @@ -380,7 +345,7 @@ class CPDF_DocPageData CPDF_Image* GetImage(CPDF_Object* pImageStream); void ReleaseImage(CPDF_Object* pImageStream); CPDF_IccProfile* GetIccProfile(CPDF_Stream* pIccProfileStream); - void ReleaseIccProfile(CPDF_Stream* pIccProfileStream, CPDF_IccProfile* pIccProfile); + void ReleaseIccProfile(CPDF_IccProfile* pIccProfile); CPDF_StreamAcc* GetFontFileStreamAcc(CPDF_Stream* pFontStream); void ReleaseFontFileStreamAcc(CPDF_Stream* pFontStream, FX_BOOL bForce = FALSE); FX_BOOL IsForceClear() const {return m_bForceClear;} @@ -388,7 +353,6 @@ class CPDF_DocPageData CPDF_CountedPattern* FindPatternPtr(CPDF_Object* pPatternObj) const; CPDF_Document* m_pPDFDoc; - CPDF_ImageMap m_ImageMap; CPDF_IccProfileMap m_IccProfileMap; CFX_MapByteStringToPtr m_HashProfileMap; CPDF_FontFileMap m_FontFileMap; @@ -396,13 +360,16 @@ class CPDF_DocPageData private: using CPDF_CountedFont = CPDF_CountedObject; + using CPDF_CountedImage = CPDF_CountedObject; using CPDF_ColorSpaceMap = std::map; using CPDF_FontMap = std::map; + using CPDF_ImageMap = std::map; using CPDF_PatternMap = std::map; CPDF_ColorSpaceMap m_ColorSpaceMap; CPDF_FontMap m_FontMap; + CPDF_ImageMap m_ImageMap; CPDF_PatternMap m_PatternMap; }; -- cgit v1.2.3