From 9fa95efa11f211a6faf9b2adb27211f784aee89a Mon Sep 17 00:00:00 2001 From: tsepez Date: Tue, 11 Oct 2016 12:29:38 -0700 Subject: Rename fpdf_page_doc.cpp -> cpdf_docpagedata.h Review-Url: https://codereview.chromium.org/2409283002 --- BUILD.gn | 3 +- core/fpdfapi/cpdf_modulemgr.cpp | 4 + core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp | 1 + core/fpdfapi/font/cpdf_font.cpp | 2 +- core/fpdfapi/page/cpdf_color.cpp | 1 + core/fpdfapi/page/cpdf_colorspace.cpp | 2 +- core/fpdfapi/page/cpdf_docpagedata.cpp | 545 +++++++++++++++++++++++ core/fpdfapi/page/cpdf_docpagedata.h | 85 ++++ core/fpdfapi/page/cpdf_image.cpp | 2 +- core/fpdfapi/page/cpdf_imageobject.cpp | 2 +- core/fpdfapi/page/cpdf_shadingobject.cpp | 2 +- core/fpdfapi/page/cpdf_shadingpattern.cpp | 1 + core/fpdfapi/page/cpdf_textstate.cpp | 2 +- core/fpdfapi/page/fpdf_page_doc.cpp | 549 ------------------------ core/fpdfapi/page/fpdf_page_parser.cpp | 1 + core/fpdfapi/page/fpdf_page_parser_old.cpp | 1 + core/fpdfapi/page/pageint.h | 58 --- core/fpdfapi/parser/cpdf_document.cpp | 1 + core/fpdfapi/render/fpdf_render.cpp | 1 + core/fpdfapi/render/fpdf_render_image.cpp | 1 + core/fpdfapi/render/fpdf_render_loadimage.cpp | 2 +- core/fpdfapi/render/fpdf_render_text.cpp | 2 +- 22 files changed, 652 insertions(+), 616 deletions(-) create mode 100644 core/fpdfapi/page/cpdf_docpagedata.cpp create mode 100644 core/fpdfapi/page/cpdf_docpagedata.h delete mode 100644 core/fpdfapi/page/fpdf_page_doc.cpp diff --git a/BUILD.gn b/BUILD.gn index b6b33f186b..a2a79d7f9b 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -435,6 +435,8 @@ static_library("fpdfapi") { "core/fpdfapi/page/cpdf_contentmarkitem.cpp", "core/fpdfapi/page/cpdf_contentmarkitem.h", "core/fpdfapi/page/cpdf_countedobject.h", + "core/fpdfapi/page/cpdf_docpagedata.cpp", + "core/fpdfapi/page/cpdf_docpagedata.h", "core/fpdfapi/page/cpdf_form.cpp", "core/fpdfapi/page/cpdf_form.h", "core/fpdfapi/page/cpdf_formobject.cpp", @@ -476,7 +478,6 @@ static_library("fpdfapi") { "core/fpdfapi/page/cpdf_tilingpattern.cpp", "core/fpdfapi/page/cpdf_tilingpattern.h", "core/fpdfapi/page/fpdf_page_colors.cpp", - "core/fpdfapi/page/fpdf_page_doc.cpp", "core/fpdfapi/page/fpdf_page_func.cpp", "core/fpdfapi/page/fpdf_page_parser.cpp", "core/fpdfapi/page/fpdf_page_parser_old.cpp", diff --git a/core/fpdfapi/cpdf_modulemgr.cpp b/core/fpdfapi/cpdf_modulemgr.cpp index ff7d95a68e..8ae8464ef5 100644 --- a/core/fpdfapi/cpdf_modulemgr.cpp +++ b/core/fpdfapi/cpdf_modulemgr.cpp @@ -32,6 +32,10 @@ CPDF_ModuleMgr::CPDF_ModuleMgr() : m_pCodecModule(nullptr) {} CPDF_ModuleMgr::~CPDF_ModuleMgr() {} +void CPDF_ModuleMgr::InitPageModule() { + m_pPageModule.reset(new CPDF_PageModule); +} + CCodec_FaxModule* CPDF_ModuleMgr::GetFaxModule() { return m_pCodecModule ? m_pCodecModule->GetFaxModule() : nullptr; } diff --git a/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp b/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp index 8697598756..66a47406f5 100644 --- a/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp +++ b/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp @@ -7,6 +7,7 @@ #include "core/fpdfapi/edit/cpdf_pagecontentgenerator.h" #include "core/fpdfapi/edit/cpdf_creator.h" +#include "core/fpdfapi/page/cpdf_docpagedata.h" #include "core/fpdfapi/page/cpdf_image.h" #include "core/fpdfapi/page/cpdf_imageobject.h" #include "core/fpdfapi/page/cpdf_page.h" diff --git a/core/fpdfapi/font/cpdf_font.cpp b/core/fpdfapi/font/cpdf_font.cpp index 1cfecb8ded..d39f752a8b 100644 --- a/core/fpdfapi/font/cpdf_font.cpp +++ b/core/fpdfapi/font/cpdf_font.cpp @@ -15,8 +15,8 @@ #include "core/fpdfapi/font/cpdf_type1font.h" #include "core/fpdfapi/font/cpdf_type3font.h" #include "core/fpdfapi/font/font_int.h" +#include "core/fpdfapi/page/cpdf_docpagedata.h" #include "core/fpdfapi/page/cpdf_pagemodule.h" -#include "core/fpdfapi/page/pageint.h" #include "core/fpdfapi/parser/cpdf_array.h" #include "core/fpdfapi/parser/cpdf_dictionary.h" #include "core/fpdfapi/parser/cpdf_document.h" diff --git a/core/fpdfapi/page/cpdf_color.cpp b/core/fpdfapi/page/cpdf_color.cpp index d06d445adf..68fa0edacf 100644 --- a/core/fpdfapi/page/cpdf_color.cpp +++ b/core/fpdfapi/page/cpdf_color.cpp @@ -6,6 +6,7 @@ #include "core/fpdfapi/page/cpdf_color.h" +#include "core/fpdfapi/page/cpdf_docpagedata.h" #include "core/fpdfapi/page/pageint.h" #include "core/fpdfapi/parser/cpdf_array.h" #include "core/fpdfapi/parser/cpdf_document.h" diff --git a/core/fpdfapi/page/cpdf_colorspace.cpp b/core/fpdfapi/page/cpdf_colorspace.cpp index 6925f691c9..fe840d10df 100644 --- a/core/fpdfapi/page/cpdf_colorspace.cpp +++ b/core/fpdfapi/page/cpdf_colorspace.cpp @@ -9,8 +9,8 @@ #include #include "core/fpdfapi/cpdf_modulemgr.h" +#include "core/fpdfapi/page/cpdf_docpagedata.h" #include "core/fpdfapi/page/cpdf_pagemodule.h" -#include "core/fpdfapi/page/pageint.h" #include "core/fpdfapi/parser/cpdf_array.h" #include "core/fpdfapi/parser/cpdf_dictionary.h" #include "core/fpdfapi/parser/cpdf_document.h" diff --git a/core/fpdfapi/page/cpdf_docpagedata.cpp b/core/fpdfapi/page/cpdf_docpagedata.cpp new file mode 100644 index 0000000000..7a85e60da2 --- /dev/null +++ b/core/fpdfapi/page/cpdf_docpagedata.cpp @@ -0,0 +1,545 @@ +// 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 "core/fpdfapi/page/cpdf_docpagedata.h" + +#include +#include + +#include "core/fdrm/crypto/fx_crypt.h" +#include "core/fpdfapi/cpdf_modulemgr.h" +#include "core/fpdfapi/font/cpdf_type1font.h" +#include "core/fpdfapi/font/font_int.h" +#include "core/fpdfapi/page/cpdf_image.h" +#include "core/fpdfapi/page/cpdf_pagemodule.h" +#include "core/fpdfapi/page/cpdf_pattern.h" +#include "core/fpdfapi/page/cpdf_shadingpattern.h" +#include "core/fpdfapi/page/cpdf_tilingpattern.h" +#include "core/fpdfapi/parser/cpdf_array.h" +#include "core/fpdfapi/parser/cpdf_dictionary.h" +#include "core/fpdfapi/parser/cpdf_document.h" +#include "core/fpdfapi/parser/cpdf_stream_acc.h" +#include "third_party/base/stl_util.h" + +CPDF_DocPageData::CPDF_DocPageData(CPDF_Document* pPDFDoc) + : m_pPDFDoc(pPDFDoc), m_bForceClear(false) {} + +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(bool bForceRelease) { + m_bForceClear = bForceRelease; + + for (auto& it : m_PatternMap) { + CPDF_CountedPattern* ptData = it.second; + if (!ptData->get()) + continue; + + if (bForceRelease || ptData->use_count() < 2) + ptData->clear(); + } + + for (auto& it : m_FontMap) { + CPDF_CountedFont* fontData = it.second; + if (!fontData->get()) + continue; + + if (bForceRelease || fontData->use_count() < 2) { + fontData->clear(); + } + } + + for (auto& it : m_ColorSpaceMap) { + CPDF_CountedColorSpace* csData = it.second; + if (!csData->get()) + continue; + + if (bForceRelease || csData->use_count() < 2) { + csData->get()->Release(); + csData->reset(nullptr); + } + } + + for (auto it = m_IccProfileMap.begin(); it != m_IccProfileMap.end();) { + auto curr_it = it++; + CPDF_CountedIccProfile* ipData = curr_it->second; + if (!ipData->get()) + continue; + + if (bForceRelease || ipData->use_count() < 2) { + for (auto hash_it = m_HashProfileMap.begin(); + hash_it != m_HashProfileMap.end(); ++hash_it) { + if (curr_it->first == hash_it->second) { + m_HashProfileMap.erase(hash_it); + break; + } + } + delete ipData->get(); + delete ipData; + m_IccProfileMap.erase(curr_it); + } + } + + for (auto it = m_FontFileMap.begin(); it != m_FontFileMap.end();) { + auto curr_it = it++; + CPDF_CountedStreamAcc* pCountedFont = curr_it->second; + if (!pCountedFont->get()) + continue; + + if (bForceRelease || pCountedFont->use_count() < 2) { + delete pCountedFont->get(); + delete pCountedFont; + m_FontFileMap.erase(curr_it); + } + } + + for (auto it = m_ImageMap.begin(); it != m_ImageMap.end();) { + auto curr_it = it++; + CPDF_CountedImage* pCountedImage = curr_it->second; + if (!pCountedImage->get()) + continue; + + if (bForceRelease || pCountedImage->use_count() < 2) { + delete pCountedImage->get(); + delete pCountedImage; + m_ImageMap.erase(curr_it); + } + } +} + +CPDF_Font* CPDF_DocPageData::GetFont(CPDF_Dictionary* pFontDict) { + if (!pFontDict) + return nullptr; + + CPDF_CountedFont* pFontData = nullptr; + auto it = m_FontMap.find(pFontDict); + if (it != m_FontMap.end()) { + pFontData = it->second; + if (pFontData->get()) { + return pFontData->AddRef(); + } + } + std::unique_ptr pFont = CPDF_Font::Create(m_pPDFDoc, pFontDict); + if (!pFont) + return nullptr; + + if (pFontData) { + pFontData->reset(pFont.release()); + } else { + pFontData = new CPDF_CountedFont(pFont.release()); + m_FontMap[pFontDict] = pFontData; + } + return pFontData->AddRef(); +} + +CPDF_Font* CPDF_DocPageData::GetStandardFont(const CFX_ByteString& fontName, + CPDF_FontEncoding* pEncoding) { + if (fontName.IsEmpty()) + return nullptr; + + for (auto& it : m_FontMap) { + CPDF_CountedFont* fontData = it.second; + CPDF_Font* pFont = fontData->get(); + if (!pFont) + continue; + if (pFont->GetBaseFont() != fontName) + continue; + if (pFont->IsEmbedded()) + continue; + if (!pFont->IsType1Font()) + continue; + if (pFont->GetFontDict()->KeyExist("Widths")) + continue; + + CPDF_Type1Font* pT1Font = pFont->AsType1Font(); + if (pEncoding && !pT1Font->GetEncoding()->IsIdentical(pEncoding)) + continue; + + return fontData->AddRef(); + } + + CPDF_Dictionary* pDict = new CPDF_Dictionary(m_pPDFDoc->GetByteStringPool()); + pDict->SetNameFor("Type", "Font"); + pDict->SetNameFor("Subtype", "Type1"); + pDict->SetNameFor("BaseFont", fontName); + if (pEncoding) { + pDict->SetFor("Encoding", + pEncoding->Realize(m_pPDFDoc->GetByteStringPool())); + } + m_pPDFDoc->AddIndirectObject(pDict); + std::unique_ptr pFont = CPDF_Font::Create(m_pPDFDoc, pDict); + if (!pFont) + return nullptr; + + CPDF_CountedFont* fontData = new CPDF_CountedFont(pFont.release()); + m_FontMap[pDict] = fontData; + return fontData->AddRef(); +} + +void CPDF_DocPageData::ReleaseFont(const CPDF_Dictionary* pFontDict) { + if (!pFontDict) + return; + + auto it = m_FontMap.find(pFontDict); + if (it == m_FontMap.end()) + return; + + CPDF_CountedFont* pFontData = it->second; + if (!pFontData->get()) + return; + + pFontData->RemoveRef(); + if (pFontData->use_count() > 1) + return; + + // We have font data only in m_FontMap cache. Clean it. + pFontData->clear(); +} + +CPDF_ColorSpace* CPDF_DocPageData::GetColorSpace( + CPDF_Object* pCSObj, + const CPDF_Dictionary* pResources) { + std::set visited; + return GetColorSpaceImpl(pCSObj, pResources, &visited); +} + +CPDF_ColorSpace* CPDF_DocPageData::GetColorSpaceImpl( + CPDF_Object* pCSObj, + const CPDF_Dictionary* pResources, + std::set* pVisited) { + if (!pCSObj) + return nullptr; + + if (pdfium::ContainsKey(*pVisited, pCSObj)) + return nullptr; + + if (pCSObj->IsName()) { + CFX_ByteString name = pCSObj->GetString(); + CPDF_ColorSpace* pCS = CPDF_ColorSpace::ColorspaceFromName(name); + if (!pCS && pResources) { + CPDF_Dictionary* pList = pResources->GetDictFor("ColorSpace"); + if (pList) { + pdfium::ScopedSetInsertion insertion(pVisited, pCSObj); + return GetColorSpaceImpl(pList->GetDirectObjectFor(name), nullptr, + pVisited); + } + } + if (!pCS || !pResources) + return pCS; + + CPDF_Dictionary* pColorSpaces = pResources->GetDictFor("ColorSpace"); + if (!pColorSpaces) + return pCS; + + CPDF_Object* pDefaultCS = nullptr; + switch (pCS->GetFamily()) { + case PDFCS_DEVICERGB: + pDefaultCS = pColorSpaces->GetDirectObjectFor("DefaultRGB"); + break; + case PDFCS_DEVICEGRAY: + pDefaultCS = pColorSpaces->GetDirectObjectFor("DefaultGray"); + break; + case PDFCS_DEVICECMYK: + pDefaultCS = pColorSpaces->GetDirectObjectFor("DefaultCMYK"); + break; + } + if (!pDefaultCS) + return pCS; + + pdfium::ScopedSetInsertion insertion(pVisited, pCSObj); + return GetColorSpaceImpl(pDefaultCS, nullptr, pVisited); + } + + CPDF_Array* pArray = pCSObj->AsArray(); + if (!pArray || pArray->IsEmpty()) + return nullptr; + + if (pArray->GetCount() == 1) { + pdfium::ScopedSetInsertion insertion(pVisited, pCSObj); + return GetColorSpaceImpl(pArray->GetDirectObjectAt(0), pResources, + pVisited); + } + + CPDF_CountedColorSpace* csData = nullptr; + auto it = m_ColorSpaceMap.find(pCSObj); + if (it != m_ColorSpaceMap.end()) { + csData = it->second; + if (csData->get()) { + return csData->AddRef(); + } + } + + std::unique_ptr pCS = + CPDF_ColorSpace::Load(m_pPDFDoc, pArray); + if (!pCS) + return nullptr; + + if (!csData) { + csData = new CPDF_CountedColorSpace(pCS.release()); + m_ColorSpaceMap[pCSObj] = csData; + } else { + csData->reset(pCS.release()); + } + return csData->AddRef(); +} + +CPDF_ColorSpace* CPDF_DocPageData::GetCopiedColorSpace(CPDF_Object* pCSObj) { + if (!pCSObj) + return nullptr; + + auto it = m_ColorSpaceMap.find(pCSObj); + if (it != m_ColorSpaceMap.end()) + return it->second->AddRef(); + + return nullptr; +} + +void CPDF_DocPageData::ReleaseColorSpace(const CPDF_Object* pColorSpace) { + if (!pColorSpace) + return; + + auto it = m_ColorSpaceMap.find(pColorSpace); + if (it == m_ColorSpaceMap.end()) + return; + + CPDF_CountedColorSpace* pCountedColorSpace = it->second; + if (!pCountedColorSpace->get()) + return; + + pCountedColorSpace->RemoveRef(); + if (pCountedColorSpace->use_count() > 1) + return; + + // We have item only in m_ColorSpaceMap cache. Clean it. + pCountedColorSpace->get()->Release(); + pCountedColorSpace->reset(nullptr); +} + +CPDF_Pattern* CPDF_DocPageData::GetPattern(CPDF_Object* pPatternObj, + bool bShading, + const CFX_Matrix& matrix) { + if (!pPatternObj) + return nullptr; + + CPDF_CountedPattern* ptData = nullptr; + auto it = m_PatternMap.find(pPatternObj); + if (it != m_PatternMap.end()) { + ptData = it->second; + if (ptData->get()) { + return ptData->AddRef(); + } + } + CPDF_Pattern* pPattern = nullptr; + if (bShading) { + pPattern = new CPDF_ShadingPattern(m_pPDFDoc, pPatternObj, true, matrix); + } else { + CPDF_Dictionary* pDict = pPatternObj ? pPatternObj->GetDict() : nullptr; + if (pDict) { + int type = pDict->GetIntegerFor("PatternType"); + if (type == CPDF_Pattern::TILING) { + pPattern = new CPDF_TilingPattern(m_pPDFDoc, pPatternObj, matrix); + } else if (type == CPDF_Pattern::SHADING) { + pPattern = + new CPDF_ShadingPattern(m_pPDFDoc, pPatternObj, false, matrix); + } + } + } + if (!pPattern) + return nullptr; + + if (!ptData) { + ptData = new CPDF_CountedPattern(pPattern); + m_PatternMap[pPatternObj] = ptData; + } else { + ptData->reset(pPattern); + } + return ptData->AddRef(); +} + +void CPDF_DocPageData::ReleasePattern(const CPDF_Object* pPatternObj) { + if (!pPatternObj) + return; + + auto it = m_PatternMap.find(pPatternObj); + if (it == m_PatternMap.end()) + return; + + CPDF_CountedPattern* pPattern = it->second; + if (!pPattern->get()) + return; + + pPattern->RemoveRef(); + if (pPattern->use_count() > 1) + return; + + // We have item only in m_PatternMap cache. Clean it. + pPattern->clear(); +} + +CPDF_Image* CPDF_DocPageData::GetImage(CPDF_Object* pImageStream) { + if (!pImageStream) + return nullptr; + + const uint32_t dwImageObjNum = pImageStream->GetObjNum(); + auto it = m_ImageMap.find(dwImageObjNum); + if (it != m_ImageMap.end()) + return it->second->AddRef(); + + CPDF_CountedImage* pCountedImage = new CPDF_CountedImage( + new CPDF_Image(m_pPDFDoc, pImageStream->AsStream(), false)); + m_ImageMap[dwImageObjNum] = pCountedImage; + return pCountedImage->AddRef(); +} + +void CPDF_DocPageData::ReleaseImage(const CPDF_Object* pImageStream) { + if (!pImageStream) + return; + + uint32_t dwObjNum = pImageStream->GetObjNum(); + if (!dwObjNum) + return; + + auto it = m_ImageMap.find(dwObjNum); + if (it == m_ImageMap.end()) + return; + + CPDF_CountedImage* pCountedImage = it->second; + if (!pCountedImage) + return; + + pCountedImage->RemoveRef(); + if (pCountedImage->use_count() > 1) + return; + + // We have item only in m_ImageMap cache. Clean it. + delete pCountedImage->get(); + delete pCountedImage; + m_ImageMap.erase(it); +} + +CPDF_IccProfile* CPDF_DocPageData::GetIccProfile( + CPDF_Stream* pIccProfileStream) { + if (!pIccProfileStream) + return nullptr; + + auto it = m_IccProfileMap.find(pIccProfileStream); + if (it != m_IccProfileMap.end()) + return it->second->AddRef(); + + CPDF_StreamAcc stream; + stream.LoadAllData(pIccProfileStream, FALSE); + uint8_t digest[20]; + CRYPT_SHA1Generate(stream.GetData(), stream.GetSize(), digest); + CFX_ByteString bsDigest(digest, 20); + auto hash_it = m_HashProfileMap.find(bsDigest); + if (hash_it != m_HashProfileMap.end()) { + auto it_copied_stream = m_IccProfileMap.find(hash_it->second); + if (it_copied_stream != m_IccProfileMap.end()) + return it_copied_stream->second->AddRef(); + } + CPDF_IccProfile* pProfile = + new CPDF_IccProfile(stream.GetData(), stream.GetSize()); + CPDF_CountedIccProfile* ipData = new CPDF_CountedIccProfile(pProfile); + m_IccProfileMap[pIccProfileStream] = ipData; + m_HashProfileMap[bsDigest] = pIccProfileStream; + return ipData->AddRef(); +} + +void CPDF_DocPageData::ReleaseIccProfile(const CPDF_IccProfile* pIccProfile) { + ASSERT(pIccProfile); + + for (auto it = m_IccProfileMap.begin(); it != m_IccProfileMap.end(); ++it) { + CPDF_CountedIccProfile* profile = it->second; + if (profile->get() != pIccProfile) + continue; + + profile->RemoveRef(); + if (profile->use_count() > 1) + continue; + // We have item only in m_IccProfileMap cache. Clean it. + delete profile->get(); + delete profile; + m_IccProfileMap.erase(it); + return; + } +} + +CPDF_StreamAcc* CPDF_DocPageData::GetFontFileStreamAcc( + CPDF_Stream* pFontStream) { + ASSERT(pFontStream); + + auto it = m_FontFileMap.find(pFontStream); + if (it != m_FontFileMap.end()) + return it->second->AddRef(); + + CPDF_Dictionary* pFontDict = pFontStream->GetDict(); + int32_t org_size = pFontDict->GetIntegerFor("Length1") + + pFontDict->GetIntegerFor("Length2") + + pFontDict->GetIntegerFor("Length3"); + org_size = std::max(org_size, 0); + + CPDF_StreamAcc* pFontFile = new CPDF_StreamAcc; + pFontFile->LoadAllData(pFontStream, FALSE, org_size); + + CPDF_CountedStreamAcc* pCountedFont = new CPDF_CountedStreamAcc(pFontFile); + m_FontFileMap[pFontStream] = pCountedFont; + return pCountedFont->AddRef(); +} + +void CPDF_DocPageData::ReleaseFontFileStreamAcc( + const CPDF_Stream* pFontStream) { + if (!pFontStream) + return; + + auto it = m_FontFileMap.find(pFontStream); + if (it == m_FontFileMap.end()) + return; + + CPDF_CountedStreamAcc* pCountedStream = it->second; + if (!pCountedStream) + return; + + pCountedStream->RemoveRef(); + if (pCountedStream->use_count() > 1) + return; + + // We have item only in m_FontFileMap cache. Clean it. + delete pCountedStream->get(); + delete pCountedStream; + m_FontFileMap.erase(it); +} + +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; +} diff --git a/core/fpdfapi/page/cpdf_docpagedata.h b/core/fpdfapi/page/cpdf_docpagedata.h new file mode 100644 index 0000000000..64c0444a5e --- /dev/null +++ b/core/fpdfapi/page/cpdf_docpagedata.h @@ -0,0 +1,85 @@ +// Copyright 2016 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 + +#ifndef CORE_FPDFAPI_PAGE_CPDF_DOCPAGEDATA_H_ +#define CORE_FPDFAPI_PAGE_CPDF_DOCPAGEDATA_H_ + +#include +#include + +#include "core/fpdfapi/page/cpdf_countedobject.h" +#include "core/fxcrt/fx_coordinates.h" +#include "core/fxcrt/fx_string.h" + +class CPDF_Dictionary; +class CPDF_Document; +class CPDF_Font; +class CPDF_FontEncoding; +class CPDF_IccProfile; +class CPDF_Image; +class CPDF_Object; +class CPDF_Stream; +class CPDF_StreamAcc; + +class CPDF_DocPageData { + public: + explicit CPDF_DocPageData(CPDF_Document* pPDFDoc); + ~CPDF_DocPageData(); + + void Clear(bool bRelease = FALSE); + CPDF_Font* GetFont(CPDF_Dictionary* pFontDict); + CPDF_Font* GetStandardFont(const CFX_ByteString& fontName, + CPDF_FontEncoding* pEncoding); + void ReleaseFont(const CPDF_Dictionary* pFontDict); + CPDF_ColorSpace* GetColorSpace(CPDF_Object* pCSObj, + const CPDF_Dictionary* pResources); + CPDF_ColorSpace* GetCopiedColorSpace(CPDF_Object* pCSObj); + void ReleaseColorSpace(const CPDF_Object* pColorSpace); + CPDF_Pattern* GetPattern(CPDF_Object* pPatternObj, + bool bShading, + const CFX_Matrix& matrix); + void ReleasePattern(const CPDF_Object* pPatternObj); + CPDF_Image* GetImage(CPDF_Object* pImageStream); + void ReleaseImage(const CPDF_Object* pImageStream); + CPDF_IccProfile* GetIccProfile(CPDF_Stream* pIccProfileStream); + void ReleaseIccProfile(const CPDF_IccProfile* pIccProfile); + CPDF_StreamAcc* GetFontFileStreamAcc(CPDF_Stream* pFontStream); + void ReleaseFontFileStreamAcc(const CPDF_Stream* pFontStream); + bool IsForceClear() const { return m_bForceClear; } + CPDF_CountedColorSpace* FindColorSpacePtr(CPDF_Object* pCSObj) const; + CPDF_CountedPattern* FindPatternPtr(CPDF_Object* pPatternObj) const; + + private: + using CPDF_CountedFont = CPDF_CountedObject; + using CPDF_CountedIccProfile = CPDF_CountedObject; + using CPDF_CountedImage = CPDF_CountedObject; + using CPDF_CountedStreamAcc = CPDF_CountedObject; + + using CPDF_ColorSpaceMap = + std::map; + using CPDF_FontFileMap = std::map; + using CPDF_FontMap = std::map; + using CPDF_IccProfileMap = + std::map; + using CPDF_ImageMap = std::map; + using CPDF_PatternMap = std::map; + + CPDF_ColorSpace* GetColorSpaceImpl(CPDF_Object* pCSObj, + const CPDF_Dictionary* pResources, + std::set* pVisited); + + CPDF_Document* const m_pPDFDoc; + bool m_bForceClear; + std::map m_HashProfileMap; + CPDF_ColorSpaceMap m_ColorSpaceMap; + CPDF_FontFileMap m_FontFileMap; + CPDF_FontMap m_FontMap; + CPDF_IccProfileMap m_IccProfileMap; + CPDF_ImageMap m_ImageMap; + CPDF_PatternMap m_PatternMap; +}; + +#endif // CORE_FPDFAPI_PAGE_CPDF_DOCPAGEDATA_H_ diff --git a/core/fpdfapi/page/cpdf_image.cpp b/core/fpdfapi/page/cpdf_image.cpp index ae962e8b98..64d0114eb9 100644 --- a/core/fpdfapi/page/cpdf_image.cpp +++ b/core/fpdfapi/page/cpdf_image.cpp @@ -11,8 +11,8 @@ #include #include "core/fpdfapi/cpdf_modulemgr.h" +#include "core/fpdfapi/page/cpdf_docpagedata.h" #include "core/fpdfapi/page/cpdf_page.h" -#include "core/fpdfapi/page/pageint.h" #include "core/fpdfapi/parser/cpdf_array.h" #include "core/fpdfapi/parser/cpdf_boolean.h" #include "core/fpdfapi/parser/cpdf_document.h" diff --git a/core/fpdfapi/page/cpdf_imageobject.cpp b/core/fpdfapi/page/cpdf_imageobject.cpp index 9fbebfd8bf..c51f648b6f 100644 --- a/core/fpdfapi/page/cpdf_imageobject.cpp +++ b/core/fpdfapi/page/cpdf_imageobject.cpp @@ -8,8 +8,8 @@ #include +#include "core/fpdfapi/page/cpdf_docpagedata.h" #include "core/fpdfapi/page/cpdf_image.h" -#include "core/fpdfapi/page/pageint.h" #include "core/fpdfapi/parser/cpdf_document.h" CPDF_ImageObject::CPDF_ImageObject() diff --git a/core/fpdfapi/page/cpdf_shadingobject.cpp b/core/fpdfapi/page/cpdf_shadingobject.cpp index 8050fc75b9..84b6aa7cd5 100644 --- a/core/fpdfapi/page/cpdf_shadingobject.cpp +++ b/core/fpdfapi/page/cpdf_shadingobject.cpp @@ -6,8 +6,8 @@ #include "core/fpdfapi/page/cpdf_shadingobject.h" +#include "core/fpdfapi/page/cpdf_docpagedata.h" #include "core/fpdfapi/page/cpdf_shadingpattern.h" -#include "core/fpdfapi/page/pageint.h" #include "core/fpdfapi/parser/cpdf_document.h" CPDF_ShadingObject::CPDF_ShadingObject() : m_pShading(nullptr) {} diff --git a/core/fpdfapi/page/cpdf_shadingpattern.cpp b/core/fpdfapi/page/cpdf_shadingpattern.cpp index dbe35b4714..d2736870e1 100644 --- a/core/fpdfapi/page/cpdf_shadingpattern.cpp +++ b/core/fpdfapi/page/cpdf_shadingpattern.cpp @@ -6,6 +6,7 @@ #include "core/fpdfapi/page/cpdf_shadingpattern.h" +#include "core/fpdfapi/page/cpdf_docpagedata.h" #include "core/fpdfapi/page/pageint.h" #include "core/fpdfapi/parser/cpdf_array.h" #include "core/fpdfapi/parser/cpdf_dictionary.h" diff --git a/core/fpdfapi/page/cpdf_textstate.cpp b/core/fpdfapi/page/cpdf_textstate.cpp index 6fc8ed29cc..cce036c41a 100644 --- a/core/fpdfapi/page/cpdf_textstate.cpp +++ b/core/fpdfapi/page/cpdf_textstate.cpp @@ -6,8 +6,8 @@ #include "core/fpdfapi/page/cpdf_textstate.h" +#include "core/fpdfapi/page/cpdf_docpagedata.h" #include "core/fpdfapi/font/cpdf_font.h" -#include "core/fpdfapi/page/pageint.h" #include "core/fpdfapi/parser/cpdf_document.h" CPDF_TextState::CPDF_TextState() {} diff --git a/core/fpdfapi/page/fpdf_page_doc.cpp b/core/fpdfapi/page/fpdf_page_doc.cpp deleted file mode 100644 index 6fa64cfa8e..0000000000 --- a/core/fpdfapi/page/fpdf_page_doc.cpp +++ /dev/null @@ -1,549 +0,0 @@ -// 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 "core/fpdfapi/page/pageint.h" - -#include -#include - -#include "core/fdrm/crypto/fx_crypt.h" -#include "core/fpdfapi/cpdf_modulemgr.h" -#include "core/fpdfapi/font/cpdf_type1font.h" -#include "core/fpdfapi/font/font_int.h" -#include "core/fpdfapi/page/cpdf_image.h" -#include "core/fpdfapi/page/cpdf_pagemodule.h" -#include "core/fpdfapi/page/cpdf_pattern.h" -#include "core/fpdfapi/page/cpdf_shadingpattern.h" -#include "core/fpdfapi/page/cpdf_tilingpattern.h" -#include "core/fpdfapi/parser/cpdf_array.h" -#include "core/fpdfapi/parser/cpdf_dictionary.h" -#include "core/fpdfapi/parser/cpdf_document.h" -#include "core/fpdfapi/parser/cpdf_stream_acc.h" -#include "third_party/base/stl_util.h" - -void CPDF_ModuleMgr::InitPageModule() { - m_pPageModule.reset(new CPDF_PageModule); -} - -CPDF_DocPageData::CPDF_DocPageData(CPDF_Document* pPDFDoc) - : m_pPDFDoc(pPDFDoc), m_bForceClear(false) {} - -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(bool bForceRelease) { - m_bForceClear = bForceRelease; - - for (auto& it : m_PatternMap) { - CPDF_CountedPattern* ptData = it.second; - if (!ptData->get()) - continue; - - if (bForceRelease || ptData->use_count() < 2) - ptData->clear(); - } - - for (auto& it : m_FontMap) { - CPDF_CountedFont* fontData = it.second; - if (!fontData->get()) - continue; - - if (bForceRelease || fontData->use_count() < 2) { - fontData->clear(); - } - } - - for (auto& it : m_ColorSpaceMap) { - CPDF_CountedColorSpace* csData = it.second; - if (!csData->get()) - continue; - - if (bForceRelease || csData->use_count() < 2) { - csData->get()->Release(); - csData->reset(nullptr); - } - } - - for (auto it = m_IccProfileMap.begin(); it != m_IccProfileMap.end();) { - auto curr_it = it++; - CPDF_CountedIccProfile* ipData = curr_it->second; - if (!ipData->get()) - continue; - - if (bForceRelease || ipData->use_count() < 2) { - for (auto hash_it = m_HashProfileMap.begin(); - hash_it != m_HashProfileMap.end(); ++hash_it) { - if (curr_it->first == hash_it->second) { - m_HashProfileMap.erase(hash_it); - break; - } - } - delete ipData->get(); - delete ipData; - m_IccProfileMap.erase(curr_it); - } - } - - for (auto it = m_FontFileMap.begin(); it != m_FontFileMap.end();) { - auto curr_it = it++; - CPDF_CountedStreamAcc* pCountedFont = curr_it->second; - if (!pCountedFont->get()) - continue; - - if (bForceRelease || pCountedFont->use_count() < 2) { - delete pCountedFont->get(); - delete pCountedFont; - m_FontFileMap.erase(curr_it); - } - } - - for (auto it = m_ImageMap.begin(); it != m_ImageMap.end();) { - auto curr_it = it++; - CPDF_CountedImage* pCountedImage = curr_it->second; - if (!pCountedImage->get()) - continue; - - if (bForceRelease || pCountedImage->use_count() < 2) { - delete pCountedImage->get(); - delete pCountedImage; - m_ImageMap.erase(curr_it); - } - } -} - -CPDF_Font* CPDF_DocPageData::GetFont(CPDF_Dictionary* pFontDict) { - if (!pFontDict) - return nullptr; - - CPDF_CountedFont* pFontData = nullptr; - auto it = m_FontMap.find(pFontDict); - if (it != m_FontMap.end()) { - pFontData = it->second; - if (pFontData->get()) { - return pFontData->AddRef(); - } - } - std::unique_ptr pFont = CPDF_Font::Create(m_pPDFDoc, pFontDict); - if (!pFont) - return nullptr; - - if (pFontData) { - pFontData->reset(pFont.release()); - } else { - pFontData = new CPDF_CountedFont(pFont.release()); - m_FontMap[pFontDict] = pFontData; - } - return pFontData->AddRef(); -} - -CPDF_Font* CPDF_DocPageData::GetStandardFont(const CFX_ByteString& fontName, - CPDF_FontEncoding* pEncoding) { - if (fontName.IsEmpty()) - return nullptr; - - for (auto& it : m_FontMap) { - CPDF_CountedFont* fontData = it.second; - CPDF_Font* pFont = fontData->get(); - if (!pFont) - continue; - if (pFont->GetBaseFont() != fontName) - continue; - if (pFont->IsEmbedded()) - continue; - if (!pFont->IsType1Font()) - continue; - if (pFont->GetFontDict()->KeyExist("Widths")) - continue; - - CPDF_Type1Font* pT1Font = pFont->AsType1Font(); - if (pEncoding && !pT1Font->GetEncoding()->IsIdentical(pEncoding)) - continue; - - return fontData->AddRef(); - } - - CPDF_Dictionary* pDict = new CPDF_Dictionary(m_pPDFDoc->GetByteStringPool()); - pDict->SetNameFor("Type", "Font"); - pDict->SetNameFor("Subtype", "Type1"); - pDict->SetNameFor("BaseFont", fontName); - if (pEncoding) { - pDict->SetFor("Encoding", - pEncoding->Realize(m_pPDFDoc->GetByteStringPool())); - } - m_pPDFDoc->AddIndirectObject(pDict); - std::unique_ptr pFont = CPDF_Font::Create(m_pPDFDoc, pDict); - if (!pFont) - return nullptr; - - CPDF_CountedFont* fontData = new CPDF_CountedFont(pFont.release()); - m_FontMap[pDict] = fontData; - return fontData->AddRef(); -} - -void CPDF_DocPageData::ReleaseFont(const CPDF_Dictionary* pFontDict) { - if (!pFontDict) - return; - - auto it = m_FontMap.find(pFontDict); - if (it == m_FontMap.end()) - return; - - CPDF_CountedFont* pFontData = it->second; - if (!pFontData->get()) - return; - - pFontData->RemoveRef(); - if (pFontData->use_count() > 1) - return; - - // We have font data only in m_FontMap cache. Clean it. - pFontData->clear(); -} - -CPDF_ColorSpace* CPDF_DocPageData::GetColorSpace( - CPDF_Object* pCSObj, - const CPDF_Dictionary* pResources) { - std::set visited; - return GetColorSpaceImpl(pCSObj, pResources, &visited); -} - -CPDF_ColorSpace* CPDF_DocPageData::GetColorSpaceImpl( - CPDF_Object* pCSObj, - const CPDF_Dictionary* pResources, - std::set* pVisited) { - if (!pCSObj) - return nullptr; - - if (pdfium::ContainsKey(*pVisited, pCSObj)) - return nullptr; - - if (pCSObj->IsName()) { - CFX_ByteString name = pCSObj->GetString(); - CPDF_ColorSpace* pCS = CPDF_ColorSpace::ColorspaceFromName(name); - if (!pCS && pResources) { - CPDF_Dictionary* pList = pResources->GetDictFor("ColorSpace"); - if (pList) { - pdfium::ScopedSetInsertion insertion(pVisited, pCSObj); - return GetColorSpaceImpl(pList->GetDirectObjectFor(name), nullptr, - pVisited); - } - } - if (!pCS || !pResources) - return pCS; - - CPDF_Dictionary* pColorSpaces = pResources->GetDictFor("ColorSpace"); - if (!pColorSpaces) - return pCS; - - CPDF_Object* pDefaultCS = nullptr; - switch (pCS->GetFamily()) { - case PDFCS_DEVICERGB: - pDefaultCS = pColorSpaces->GetDirectObjectFor("DefaultRGB"); - break; - case PDFCS_DEVICEGRAY: - pDefaultCS = pColorSpaces->GetDirectObjectFor("DefaultGray"); - break; - case PDFCS_DEVICECMYK: - pDefaultCS = pColorSpaces->GetDirectObjectFor("DefaultCMYK"); - break; - } - if (!pDefaultCS) - return pCS; - - pdfium::ScopedSetInsertion insertion(pVisited, pCSObj); - return GetColorSpaceImpl(pDefaultCS, nullptr, pVisited); - } - - CPDF_Array* pArray = pCSObj->AsArray(); - if (!pArray || pArray->IsEmpty()) - return nullptr; - - if (pArray->GetCount() == 1) { - pdfium::ScopedSetInsertion insertion(pVisited, pCSObj); - return GetColorSpaceImpl(pArray->GetDirectObjectAt(0), pResources, - pVisited); - } - - CPDF_CountedColorSpace* csData = nullptr; - auto it = m_ColorSpaceMap.find(pCSObj); - if (it != m_ColorSpaceMap.end()) { - csData = it->second; - if (csData->get()) { - return csData->AddRef(); - } - } - - std::unique_ptr pCS = - CPDF_ColorSpace::Load(m_pPDFDoc, pArray); - if (!pCS) - return nullptr; - - if (!csData) { - csData = new CPDF_CountedColorSpace(pCS.release()); - m_ColorSpaceMap[pCSObj] = csData; - } else { - csData->reset(pCS.release()); - } - return csData->AddRef(); -} - -CPDF_ColorSpace* CPDF_DocPageData::GetCopiedColorSpace(CPDF_Object* pCSObj) { - if (!pCSObj) - return nullptr; - - auto it = m_ColorSpaceMap.find(pCSObj); - if (it != m_ColorSpaceMap.end()) - return it->second->AddRef(); - - return nullptr; -} - -void CPDF_DocPageData::ReleaseColorSpace(const CPDF_Object* pColorSpace) { - if (!pColorSpace) - return; - - auto it = m_ColorSpaceMap.find(pColorSpace); - if (it == m_ColorSpaceMap.end()) - return; - - CPDF_CountedColorSpace* pCountedColorSpace = it->second; - if (!pCountedColorSpace->get()) - return; - - pCountedColorSpace->RemoveRef(); - if (pCountedColorSpace->use_count() > 1) - return; - - // We have item only in m_ColorSpaceMap cache. Clean it. - pCountedColorSpace->get()->Release(); - pCountedColorSpace->reset(nullptr); -} - -CPDF_Pattern* CPDF_DocPageData::GetPattern(CPDF_Object* pPatternObj, - bool bShading, - const CFX_Matrix& matrix) { - if (!pPatternObj) - return nullptr; - - CPDF_CountedPattern* ptData = nullptr; - auto it = m_PatternMap.find(pPatternObj); - if (it != m_PatternMap.end()) { - ptData = it->second; - if (ptData->get()) { - return ptData->AddRef(); - } - } - CPDF_Pattern* pPattern = nullptr; - if (bShading) { - pPattern = new CPDF_ShadingPattern(m_pPDFDoc, pPatternObj, true, matrix); - } else { - CPDF_Dictionary* pDict = pPatternObj ? pPatternObj->GetDict() : nullptr; - if (pDict) { - int type = pDict->GetIntegerFor("PatternType"); - if (type == CPDF_Pattern::TILING) { - pPattern = new CPDF_TilingPattern(m_pPDFDoc, pPatternObj, matrix); - } else if (type == CPDF_Pattern::SHADING) { - pPattern = - new CPDF_ShadingPattern(m_pPDFDoc, pPatternObj, false, matrix); - } - } - } - if (!pPattern) - return nullptr; - - if (!ptData) { - ptData = new CPDF_CountedPattern(pPattern); - m_PatternMap[pPatternObj] = ptData; - } else { - ptData->reset(pPattern); - } - return ptData->AddRef(); -} - -void CPDF_DocPageData::ReleasePattern(const CPDF_Object* pPatternObj) { - if (!pPatternObj) - return; - - auto it = m_PatternMap.find(pPatternObj); - if (it == m_PatternMap.end()) - return; - - CPDF_CountedPattern* pPattern = it->second; - if (!pPattern->get()) - return; - - pPattern->RemoveRef(); - if (pPattern->use_count() > 1) - return; - - // We have item only in m_PatternMap cache. Clean it. - pPattern->clear(); -} - -CPDF_Image* CPDF_DocPageData::GetImage(CPDF_Object* pImageStream) { - if (!pImageStream) - return nullptr; - - const uint32_t dwImageObjNum = pImageStream->GetObjNum(); - auto it = m_ImageMap.find(dwImageObjNum); - if (it != m_ImageMap.end()) - return it->second->AddRef(); - - CPDF_CountedImage* pCountedImage = new CPDF_CountedImage( - new CPDF_Image(m_pPDFDoc, pImageStream->AsStream(), false)); - m_ImageMap[dwImageObjNum] = pCountedImage; - return pCountedImage->AddRef(); -} - -void CPDF_DocPageData::ReleaseImage(const CPDF_Object* pImageStream) { - if (!pImageStream) - return; - - uint32_t dwObjNum = pImageStream->GetObjNum(); - if (!dwObjNum) - return; - - auto it = m_ImageMap.find(dwObjNum); - if (it == m_ImageMap.end()) - return; - - CPDF_CountedImage* pCountedImage = it->second; - if (!pCountedImage) - return; - - pCountedImage->RemoveRef(); - if (pCountedImage->use_count() > 1) - return; - - // We have item only in m_ImageMap cache. Clean it. - delete pCountedImage->get(); - delete pCountedImage; - m_ImageMap.erase(it); -} - -CPDF_IccProfile* CPDF_DocPageData::GetIccProfile( - CPDF_Stream* pIccProfileStream) { - if (!pIccProfileStream) - return nullptr; - - auto it = m_IccProfileMap.find(pIccProfileStream); - if (it != m_IccProfileMap.end()) - return it->second->AddRef(); - - CPDF_StreamAcc stream; - stream.LoadAllData(pIccProfileStream, FALSE); - uint8_t digest[20]; - CRYPT_SHA1Generate(stream.GetData(), stream.GetSize(), digest); - CFX_ByteString bsDigest(digest, 20); - auto hash_it = m_HashProfileMap.find(bsDigest); - if (hash_it != m_HashProfileMap.end()) { - auto it_copied_stream = m_IccProfileMap.find(hash_it->second); - if (it_copied_stream != m_IccProfileMap.end()) - return it_copied_stream->second->AddRef(); - } - CPDF_IccProfile* pProfile = - new CPDF_IccProfile(stream.GetData(), stream.GetSize()); - CPDF_CountedIccProfile* ipData = new CPDF_CountedIccProfile(pProfile); - m_IccProfileMap[pIccProfileStream] = ipData; - m_HashProfileMap[bsDigest] = pIccProfileStream; - return ipData->AddRef(); -} - -void CPDF_DocPageData::ReleaseIccProfile(const CPDF_IccProfile* pIccProfile) { - ASSERT(pIccProfile); - - for (auto it = m_IccProfileMap.begin(); it != m_IccProfileMap.end(); ++it) { - CPDF_CountedIccProfile* profile = it->second; - if (profile->get() != pIccProfile) - continue; - - profile->RemoveRef(); - if (profile->use_count() > 1) - continue; - // We have item only in m_IccProfileMap cache. Clean it. - delete profile->get(); - delete profile; - m_IccProfileMap.erase(it); - return; - } -} - -CPDF_StreamAcc* CPDF_DocPageData::GetFontFileStreamAcc( - CPDF_Stream* pFontStream) { - ASSERT(pFontStream); - - auto it = m_FontFileMap.find(pFontStream); - if (it != m_FontFileMap.end()) - return it->second->AddRef(); - - CPDF_Dictionary* pFontDict = pFontStream->GetDict(); - int32_t org_size = pFontDict->GetIntegerFor("Length1") + - pFontDict->GetIntegerFor("Length2") + - pFontDict->GetIntegerFor("Length3"); - org_size = std::max(org_size, 0); - - CPDF_StreamAcc* pFontFile = new CPDF_StreamAcc; - pFontFile->LoadAllData(pFontStream, FALSE, org_size); - - CPDF_CountedStreamAcc* pCountedFont = new CPDF_CountedStreamAcc(pFontFile); - m_FontFileMap[pFontStream] = pCountedFont; - return pCountedFont->AddRef(); -} - -void CPDF_DocPageData::ReleaseFontFileStreamAcc( - const CPDF_Stream* pFontStream) { - if (!pFontStream) - return; - - auto it = m_FontFileMap.find(pFontStream); - if (it == m_FontFileMap.end()) - return; - - CPDF_CountedStreamAcc* pCountedStream = it->second; - if (!pCountedStream) - return; - - pCountedStream->RemoveRef(); - if (pCountedStream->use_count() > 1) - return; - - // We have item only in m_FontFileMap cache. Clean it. - delete pCountedStream->get(); - delete pCountedStream; - m_FontFileMap.erase(it); -} - -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; -} diff --git a/core/fpdfapi/page/fpdf_page_parser.cpp b/core/fpdfapi/page/fpdf_page_parser.cpp index 124bf71189..60cb3b4bfa 100644 --- a/core/fpdfapi/page/fpdf_page_parser.cpp +++ b/core/fpdfapi/page/fpdf_page_parser.cpp @@ -14,6 +14,7 @@ #include "core/fpdfapi/font/cpdf_font.h" #include "core/fpdfapi/font/cpdf_type3font.h" #include "core/fpdfapi/page/cpdf_allstates.h" +#include "core/fpdfapi/page/cpdf_docpagedata.h" #include "core/fpdfapi/page/cpdf_form.h" #include "core/fpdfapi/page/cpdf_formobject.h" #include "core/fpdfapi/page/cpdf_image.h" diff --git a/core/fpdfapi/page/fpdf_page_parser_old.cpp b/core/fpdfapi/page/fpdf_page_parser_old.cpp index f073b51b27..8c69903b2c 100644 --- a/core/fpdfapi/page/fpdf_page_parser_old.cpp +++ b/core/fpdfapi/page/fpdf_page_parser_old.cpp @@ -11,6 +11,7 @@ #include "core/fpdfapi/cpdf_modulemgr.h" #include "core/fpdfapi/font/cpdf_type3char.h" #include "core/fpdfapi/page/cpdf_allstates.h" +#include "core/fpdfapi/page/cpdf_docpagedata.h" #include "core/fpdfapi/page/cpdf_form.h" #include "core/fpdfapi/page/cpdf_page.h" #include "core/fpdfapi/page/cpdf_pageobject.h" diff --git a/core/fpdfapi/page/pageint.h b/core/fpdfapi/page/pageint.h index 79cd46b819..5f8351e261 100644 --- a/core/fpdfapi/page/pageint.h +++ b/core/fpdfapi/page/pageint.h @@ -307,64 +307,6 @@ class CPDF_ContentParser { std::unique_ptr m_pParser; }; -class CPDF_DocPageData { - public: - explicit CPDF_DocPageData(CPDF_Document* pPDFDoc); - ~CPDF_DocPageData(); - - void Clear(bool bRelease = FALSE); - CPDF_Font* GetFont(CPDF_Dictionary* pFontDict); - CPDF_Font* GetStandardFont(const CFX_ByteString& fontName, - CPDF_FontEncoding* pEncoding); - void ReleaseFont(const CPDF_Dictionary* pFontDict); - CPDF_ColorSpace* GetColorSpace(CPDF_Object* pCSObj, - const CPDF_Dictionary* pResources); - CPDF_ColorSpace* GetCopiedColorSpace(CPDF_Object* pCSObj); - void ReleaseColorSpace(const CPDF_Object* pColorSpace); - CPDF_Pattern* GetPattern(CPDF_Object* pPatternObj, - bool bShading, - const CFX_Matrix& matrix); - void ReleasePattern(const CPDF_Object* pPatternObj); - CPDF_Image* GetImage(CPDF_Object* pImageStream); - void ReleaseImage(const CPDF_Object* pImageStream); - CPDF_IccProfile* GetIccProfile(CPDF_Stream* pIccProfileStream); - void ReleaseIccProfile(const CPDF_IccProfile* pIccProfile); - CPDF_StreamAcc* GetFontFileStreamAcc(CPDF_Stream* pFontStream); - void ReleaseFontFileStreamAcc(const CPDF_Stream* pFontStream); - bool IsForceClear() const { return m_bForceClear; } - CPDF_CountedColorSpace* FindColorSpacePtr(CPDF_Object* pCSObj) const; - CPDF_CountedPattern* FindPatternPtr(CPDF_Object* pPatternObj) const; - - private: - using CPDF_CountedFont = CPDF_CountedObject; - using CPDF_CountedIccProfile = CPDF_CountedObject; - using CPDF_CountedImage = CPDF_CountedObject; - using CPDF_CountedStreamAcc = CPDF_CountedObject; - - using CPDF_ColorSpaceMap = - std::map; - using CPDF_FontFileMap = std::map; - using CPDF_FontMap = std::map; - using CPDF_IccProfileMap = - std::map; - using CPDF_ImageMap = std::map; - using CPDF_PatternMap = std::map; - - CPDF_ColorSpace* GetColorSpaceImpl(CPDF_Object* pCSObj, - const CPDF_Dictionary* pResources, - std::set* pVisited); - - CPDF_Document* const m_pPDFDoc; - bool m_bForceClear; - std::map m_HashProfileMap; - CPDF_ColorSpaceMap m_ColorSpaceMap; - CPDF_FontFileMap m_FontFileMap; - CPDF_FontMap m_FontMap; - CPDF_IccProfileMap m_IccProfileMap; - CPDF_ImageMap m_ImageMap; - CPDF_PatternMap m_PatternMap; -}; - class CPDF_Function { public: enum class Type { diff --git a/core/fpdfapi/parser/cpdf_document.cpp b/core/fpdfapi/parser/cpdf_document.cpp index a5bf39d438..c5f64a790c 100644 --- a/core/fpdfapi/parser/cpdf_document.cpp +++ b/core/fpdfapi/parser/cpdf_document.cpp @@ -12,6 +12,7 @@ #include "core/fpdfapi/cpdf_modulemgr.h" #include "core/fpdfapi/font/cpdf_fontencoding.h" +#include "core/fpdfapi/page/cpdf_docpagedata.h" #include "core/fpdfapi/page/cpdf_pagemodule.h" #include "core/fpdfapi/page/pageint.h" #include "core/fpdfapi/parser/cpdf_array.h" diff --git a/core/fpdfapi/render/fpdf_render.cpp b/core/fpdfapi/render/fpdf_render.cpp index 5feeae8f53..4dcad15949 100644 --- a/core/fpdfapi/render/fpdf_render.cpp +++ b/core/fpdfapi/render/fpdf_render.cpp @@ -11,6 +11,7 @@ #include "core/fpdfapi/cpdf_modulemgr.h" #include "core/fpdfapi/font/cpdf_type3char.h" #include "core/fpdfapi/font/cpdf_type3font.h" +#include "core/fpdfapi/page/cpdf_docpagedata.h" #include "core/fpdfapi/page/cpdf_form.h" #include "core/fpdfapi/page/cpdf_formobject.h" #include "core/fpdfapi/page/cpdf_graphicstates.h" diff --git a/core/fpdfapi/render/fpdf_render_image.cpp b/core/fpdfapi/render/fpdf_render_image.cpp index 766eb950fe..049eff30c0 100644 --- a/core/fpdfapi/render/fpdf_render_image.cpp +++ b/core/fpdfapi/render/fpdf_render_image.cpp @@ -10,6 +10,7 @@ #include #include +#include "core/fpdfapi/page/cpdf_docpagedata.h" #include "core/fpdfapi/page/cpdf_form.h" #include "core/fpdfapi/page/cpdf_image.h" #include "core/fpdfapi/page/cpdf_imageobject.h" diff --git a/core/fpdfapi/render/fpdf_render_loadimage.cpp b/core/fpdfapi/render/fpdf_render_loadimage.cpp index 0a4579e45e..940aa37479 100644 --- a/core/fpdfapi/render/fpdf_render_loadimage.cpp +++ b/core/fpdfapi/render/fpdf_render_loadimage.cpp @@ -11,9 +11,9 @@ #include #include "core/fpdfapi/cpdf_modulemgr.h" +#include "core/fpdfapi/page/cpdf_docpagedata.h" #include "core/fpdfapi/page/cpdf_image.h" #include "core/fpdfapi/page/cpdf_imageobject.h" -#include "core/fpdfapi/page/pageint.h" #include "core/fpdfapi/parser/cpdf_array.h" #include "core/fpdfapi/parser/cpdf_dictionary.h" #include "core/fpdfapi/parser/cpdf_document.h" diff --git a/core/fpdfapi/render/fpdf_render_text.cpp b/core/fpdfapi/render/fpdf_render_text.cpp index 26edc1b0f4..528e11dc44 100644 --- a/core/fpdfapi/render/fpdf_render_text.cpp +++ b/core/fpdfapi/render/fpdf_render_text.cpp @@ -12,12 +12,12 @@ #include "core/fpdfapi/font/cpdf_font.h" #include "core/fpdfapi/font/cpdf_type3char.h" #include "core/fpdfapi/font/cpdf_type3font.h" +#include "core/fpdfapi/page/cpdf_docpagedata.h" #include "core/fpdfapi/page/cpdf_form.h" #include "core/fpdfapi/page/cpdf_imageobject.h" #include "core/fpdfapi/page/cpdf_pageobject.h" #include "core/fpdfapi/page/cpdf_pathobject.h" #include "core/fpdfapi/page/cpdf_textobject.h" -#include "core/fpdfapi/page/pageint.h" #include "core/fpdfapi/parser/cpdf_dictionary.h" #include "core/fpdfapi/parser/cpdf_document.h" #include "core/fpdfapi/render/cpdf_renderoptions.h" -- cgit v1.2.3