From 935c6310adacf2f4a4265e831e6cb60dc7a35803 Mon Sep 17 00:00:00 2001 From: npm Date: Thu, 17 Nov 2016 10:47:43 -0800 Subject: Move CPDF_DocRenderData from render_int First CL in an attempt to reduce the class cluttering in fpdf_render_text, fpdf_render_image, and fpdf_render. Review-Url: https://codereview.chromium.org/2507023006 --- core/fpdfapi/page/cpdf_generalstate.cpp | 1 + core/fpdfapi/parser/cpdf_document.cpp | 1 + core/fpdfapi/render/cpdf_docrenderdata.cpp | 153 +++++++++++++++++++++++++++++ core/fpdfapi/render/cpdf_docrenderdata.h | 43 ++++++++ core/fpdfapi/render/fpdf_render.cpp | 134 +------------------------ core/fpdfapi/render/fpdf_render_text.cpp | 1 + core/fpdfapi/render/render_int.h | 21 ---- 7 files changed, 200 insertions(+), 154 deletions(-) create mode 100644 core/fpdfapi/render/cpdf_docrenderdata.cpp create mode 100644 core/fpdfapi/render/cpdf_docrenderdata.h (limited to 'core') diff --git a/core/fpdfapi/page/cpdf_generalstate.cpp b/core/fpdfapi/page/cpdf_generalstate.cpp index e8be18e2fa..33e8e5ecb4 100644 --- a/core/fpdfapi/page/cpdf_generalstate.cpp +++ b/core/fpdfapi/page/cpdf_generalstate.cpp @@ -7,6 +7,7 @@ #include "core/fpdfapi/page/cpdf_generalstate.h" #include "core/fpdfapi/parser/cpdf_document.h" +#include "core/fpdfapi/render/cpdf_docrenderdata.h" #include "core/fpdfapi/render/render_int.h" namespace { diff --git a/core/fpdfapi/parser/cpdf_document.cpp b/core/fpdfapi/parser/cpdf_document.cpp index c7f99fa6ef..bafeefe6fc 100644 --- a/core/fpdfapi/parser/cpdf_document.cpp +++ b/core/fpdfapi/parser/cpdf_document.cpp @@ -23,6 +23,7 @@ #include "core/fpdfapi/parser/cpdf_parser.h" #include "core/fpdfapi/parser/cpdf_reference.h" #include "core/fpdfapi/parser/cpdf_stream.h" +#include "core/fpdfapi/render/cpdf_docrenderdata.h" #include "core/fpdfapi/render/render_int.h" #include "core/fxcodec/JBig2_DocumentContext.h" #include "core/fxge/cfx_unicodeencoding.h" diff --git a/core/fpdfapi/render/cpdf_docrenderdata.cpp b/core/fpdfapi/render/cpdf_docrenderdata.cpp new file mode 100644 index 0000000000..9a1691711b --- /dev/null +++ b/core/fpdfapi/render/cpdf_docrenderdata.cpp @@ -0,0 +1,153 @@ +// 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 + +#include "core/fpdfapi/render/cpdf_docrenderdata.h" + +#include + +#include "core/fpdfapi/font/cpdf_type3font.h" +#include "core/fpdfapi/page/pageint.h" +#include "core/fpdfapi/parser/cpdf_array.h" +#include "core/fpdfapi/parser/cpdf_document.h" +#include "core/fpdfapi/render/cpdf_type3cache.h" +#include "core/fpdfapi/render/render_int.h" + +namespace { + +const int kMaxOutputs = 16; + +} // namespace + +CPDF_DocRenderData::CPDF_DocRenderData(CPDF_Document* pPDFDoc) + : m_pPDFDoc(pPDFDoc) {} + +CPDF_DocRenderData::~CPDF_DocRenderData() { + Clear(true); +} + +void CPDF_DocRenderData::Clear(bool bRelease) { + for (auto it = m_Type3FaceMap.begin(); it != m_Type3FaceMap.end();) { + auto curr_it = it++; + CPDF_CountedObject* cache = curr_it->second; + if (bRelease || cache->use_count() < 2) { + delete cache->get(); + delete cache; + m_Type3FaceMap.erase(curr_it); + } + } + + for (auto it = m_TransferFuncMap.begin(); it != m_TransferFuncMap.end();) { + auto curr_it = it++; + CPDF_CountedObject* value = curr_it->second; + if (bRelease || value->use_count() < 2) { + delete value->get(); + delete value; + m_TransferFuncMap.erase(curr_it); + } + } +} + +CPDF_Type3Cache* CPDF_DocRenderData::GetCachedType3(CPDF_Type3Font* pFont) { + CPDF_CountedObject* pCache; + auto it = m_Type3FaceMap.find(pFont); + if (it == m_Type3FaceMap.end()) { + CPDF_Type3Cache* pType3 = new CPDF_Type3Cache(pFont); + pCache = new CPDF_CountedObject(pType3); + m_Type3FaceMap[pFont] = pCache; + } else { + pCache = it->second; + } + return pCache->AddRef(); +} + +void CPDF_DocRenderData::ReleaseCachedType3(CPDF_Type3Font* pFont) { + auto it = m_Type3FaceMap.find(pFont); + if (it != m_Type3FaceMap.end()) { + it->second->RemoveRef(); + if (it->second->use_count() < 2) { + delete it->second->get(); + delete it->second; + m_Type3FaceMap.erase(it); + } + } +} + +CPDF_TransferFunc* CPDF_DocRenderData::GetTransferFunc(CPDF_Object* pObj) { + if (!pObj) + return nullptr; + + auto it = m_TransferFuncMap.find(pObj); + if (it != m_TransferFuncMap.end()) { + CPDF_CountedObject* pTransferCounter = it->second; + return pTransferCounter->AddRef(); + } + + std::unique_ptr pFuncs[3]; + bool bUniTransfer = true; + bool bIdentity = true; + if (CPDF_Array* pArray = pObj->AsArray()) { + bUniTransfer = false; + if (pArray->GetCount() < 3) + return nullptr; + + for (uint32_t i = 0; i < 3; ++i) { + pFuncs[2 - i] = CPDF_Function::Load(pArray->GetDirectObjectAt(i)); + if (!pFuncs[2 - i]) + return nullptr; + } + } else { + pFuncs[0] = CPDF_Function::Load(pObj); + if (!pFuncs[0]) + return nullptr; + } + CPDF_TransferFunc* pTransfer = new CPDF_TransferFunc(m_pPDFDoc); + CPDF_CountedObject* pTransferCounter = + new CPDF_CountedObject(pTransfer); + m_TransferFuncMap[pObj] = pTransferCounter; + FX_FLOAT output[kMaxOutputs]; + FXSYS_memset(output, 0, sizeof(output)); + FX_FLOAT input; + int noutput; + for (int v = 0; v < 256; ++v) { + input = (FX_FLOAT)v / 255.0f; + if (bUniTransfer) { + if (pFuncs[0] && pFuncs[0]->CountOutputs() <= kMaxOutputs) + pFuncs[0]->Call(&input, 1, output, noutput); + int o = FXSYS_round(output[0] * 255); + if (o != v) + bIdentity = false; + for (int i = 0; i < 3; ++i) + pTransfer->m_Samples[i * 256 + v] = o; + continue; + } + for (int i = 0; i < 3; ++i) { + if (!pFuncs[i] || pFuncs[i]->CountOutputs() > kMaxOutputs) { + pTransfer->m_Samples[i * 256 + v] = v; + continue; + } + pFuncs[i]->Call(&input, 1, output, noutput); + int o = FXSYS_round(output[0] * 255); + if (o != v) + bIdentity = false; + pTransfer->m_Samples[i * 256 + v] = o; + } + } + + pTransfer->m_bIdentity = bIdentity; + return pTransferCounter->AddRef(); +} + +void CPDF_DocRenderData::ReleaseTransferFunc(CPDF_Object* pObj) { + auto it = m_TransferFuncMap.find(pObj); + if (it != m_TransferFuncMap.end()) { + it->second->RemoveRef(); + if (it->second->use_count() < 2) { + delete it->second->get(); + delete it->second; + m_TransferFuncMap.erase(it); + } + } +} diff --git a/core/fpdfapi/render/cpdf_docrenderdata.h b/core/fpdfapi/render/cpdf_docrenderdata.h new file mode 100644 index 0000000000..a8f4167a8d --- /dev/null +++ b/core/fpdfapi/render/cpdf_docrenderdata.h @@ -0,0 +1,43 @@ +// 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_RENDER_CPDF_DOCRENDERDATA_H_ +#define CORE_FPDFAPI_RENDER_CPDF_DOCRENDERDATA_H_ + +#include + +#include "core/fpdfapi/page/cpdf_countedobject.h" + +class CPDF_Document; +class CPDF_Font; +class CPDF_Object; +class CPDF_TransferFunc; +class CPDF_Type3Cache; +class CPDF_Type3Font; + +class CPDF_DocRenderData { + public: + explicit CPDF_DocRenderData(CPDF_Document* pPDFDoc); + ~CPDF_DocRenderData(); + + CPDF_Type3Cache* GetCachedType3(CPDF_Type3Font* pFont); + void ReleaseCachedType3(CPDF_Type3Font* pFont); + CPDF_TransferFunc* GetTransferFunc(CPDF_Object* pObj); + void ReleaseTransferFunc(CPDF_Object* pObj); + void Clear(bool bRelease); + + private: + using CPDF_Type3CacheMap = + std::map*>; + using CPDF_TransferFuncMap = + std::map*>; + + CPDF_Document* m_pPDFDoc; // Not Owned + CPDF_Type3CacheMap m_Type3FaceMap; + CPDF_TransferFuncMap m_TransferFuncMap; +}; + +#endif // CORE_FPDFAPI_RENDER_CPDF_DOCRENDERDATA_H_ diff --git a/core/fpdfapi/render/fpdf_render.cpp b/core/fpdfapi/render/fpdf_render.cpp index e4abb7e832..ca9e059171 100644 --- a/core/fpdfapi/render/fpdf_render.cpp +++ b/core/fpdfapi/render/fpdf_render.cpp @@ -25,6 +25,7 @@ #include "core/fpdfapi/parser/cpdf_array.h" #include "core/fpdfapi/parser/cpdf_dictionary.h" #include "core/fpdfapi/parser/cpdf_document.h" +#include "core/fpdfapi/render/cpdf_docrenderdata.h" #include "core/fpdfapi/render/cpdf_pagerendercache.h" #include "core/fpdfapi/render/cpdf_progressiverenderer.h" #include "core/fpdfapi/render/cpdf_renderoptions.h" @@ -36,60 +37,6 @@ #include "core/fxge/cfx_pathdata.h" #include "core/fxge/cfx_renderdevice.h" -CPDF_DocRenderData::CPDF_DocRenderData(CPDF_Document* pPDFDoc) - : m_pPDFDoc(pPDFDoc) {} - -CPDF_DocRenderData::~CPDF_DocRenderData() { - Clear(true); -} - -void CPDF_DocRenderData::Clear(bool bRelease) { - for (auto it = m_Type3FaceMap.begin(); it != m_Type3FaceMap.end();) { - auto curr_it = it++; - CPDF_CountedObject* cache = curr_it->second; - if (bRelease || cache->use_count() < 2) { - delete cache->get(); - delete cache; - m_Type3FaceMap.erase(curr_it); - } - } - - for (auto it = m_TransferFuncMap.begin(); it != m_TransferFuncMap.end();) { - auto curr_it = it++; - CPDF_CountedObject* value = curr_it->second; - if (bRelease || value->use_count() < 2) { - delete value->get(); - delete value; - m_TransferFuncMap.erase(curr_it); - } - } -} - -CPDF_Type3Cache* CPDF_DocRenderData::GetCachedType3(CPDF_Type3Font* pFont) { - CPDF_CountedObject* pCache; - auto it = m_Type3FaceMap.find(pFont); - if (it == m_Type3FaceMap.end()) { - CPDF_Type3Cache* pType3 = new CPDF_Type3Cache(pFont); - pCache = new CPDF_CountedObject(pType3); - m_Type3FaceMap[pFont] = pCache; - } else { - pCache = it->second; - } - return pCache->AddRef(); -} - -void CPDF_DocRenderData::ReleaseCachedType3(CPDF_Type3Font* pFont) { - auto it = m_Type3FaceMap.find(pFont); - if (it != m_Type3FaceMap.end()) { - it->second->RemoveRef(); - if (it->second->use_count() < 2) { - delete it->second->get(); - delete it->second; - m_Type3FaceMap.erase(it); - } - } -} - CPDF_RenderOptions::CPDF_RenderOptions() : m_ColorMode(RENDER_COLOR_NORMAL), m_Flags(RENDER_CLEARTYPE), @@ -1087,85 +1034,6 @@ void CPDF_ProgressiveRenderer::Continue(IFX_Pause* pPause) { } } -CPDF_TransferFunc* CPDF_DocRenderData::GetTransferFunc(CPDF_Object* pObj) { - if (!pObj) - return nullptr; - - auto it = m_TransferFuncMap.find(pObj); - if (it != m_TransferFuncMap.end()) { - CPDF_CountedObject* pTransferCounter = it->second; - return pTransferCounter->AddRef(); - } - - std::unique_ptr pFuncs[3]; - bool bUniTransfer = true; - bool bIdentity = true; - if (CPDF_Array* pArray = pObj->AsArray()) { - bUniTransfer = false; - if (pArray->GetCount() < 3) - return nullptr; - - for (uint32_t i = 0; i < 3; ++i) { - pFuncs[2 - i] = CPDF_Function::Load(pArray->GetDirectObjectAt(i)); - if (!pFuncs[2 - i]) - return nullptr; - } - } else { - pFuncs[0] = CPDF_Function::Load(pObj); - if (!pFuncs[0]) - return nullptr; - } - CPDF_TransferFunc* pTransfer = new CPDF_TransferFunc(m_pPDFDoc); - CPDF_CountedObject* pTransferCounter = - new CPDF_CountedObject(pTransfer); - m_TransferFuncMap[pObj] = pTransferCounter; - static const int kMaxOutputs = 16; - FX_FLOAT output[kMaxOutputs]; - FXSYS_memset(output, 0, sizeof(output)); - FX_FLOAT input; - int noutput; - for (int v = 0; v < 256; ++v) { - input = (FX_FLOAT)v / 255.0f; - if (bUniTransfer) { - if (pFuncs[0] && pFuncs[0]->CountOutputs() <= kMaxOutputs) - pFuncs[0]->Call(&input, 1, output, noutput); - int o = FXSYS_round(output[0] * 255); - if (o != v) - bIdentity = false; - for (int i = 0; i < 3; ++i) { - pTransfer->m_Samples[i * 256 + v] = o; - } - } else { - for (int i = 0; i < 3; ++i) { - if (pFuncs[i] && pFuncs[i]->CountOutputs() <= kMaxOutputs) { - pFuncs[i]->Call(&input, 1, output, noutput); - int o = FXSYS_round(output[0] * 255); - if (o != v) - bIdentity = false; - pTransfer->m_Samples[i * 256 + v] = o; - } else { - pTransfer->m_Samples[i * 256 + v] = v; - } - } - } - } - - pTransfer->m_bIdentity = bIdentity; - return pTransferCounter->AddRef(); -} - -void CPDF_DocRenderData::ReleaseTransferFunc(CPDF_Object* pObj) { - auto it = m_TransferFuncMap.find(pObj); - if (it != m_TransferFuncMap.end()) { - it->second->RemoveRef(); - if (it->second->use_count() < 2) { - delete it->second->get(); - delete it->second; - m_TransferFuncMap.erase(it); - } - } -} - CPDF_DeviceBuffer::CPDF_DeviceBuffer() : m_pDevice(nullptr), m_pContext(nullptr), m_pObject(nullptr) {} diff --git a/core/fpdfapi/render/fpdf_render_text.cpp b/core/fpdfapi/render/fpdf_render_text.cpp index 86fac2d90f..bf1d95ff74 100644 --- a/core/fpdfapi/render/fpdf_render_text.cpp +++ b/core/fpdfapi/render/fpdf_render_text.cpp @@ -20,6 +20,7 @@ #include "core/fpdfapi/page/cpdf_textobject.h" #include "core/fpdfapi/parser/cpdf_dictionary.h" #include "core/fpdfapi/parser/cpdf_document.h" +#include "core/fpdfapi/render/cpdf_docrenderdata.h" #include "core/fpdfapi/render/cpdf_renderoptions.h" #include "core/fpdfapi/render/cpdf_textrenderer.h" #include "core/fpdfapi/render/cpdf_type3cache.h" diff --git a/core/fpdfapi/render/render_int.h b/core/fpdfapi/render/render_int.h index e6090e8ebe..a8c8a580de 100644 --- a/core/fpdfapi/render/render_int.h +++ b/core/fpdfapi/render/render_int.h @@ -63,27 +63,6 @@ class CPDF_TransferFunc { uint8_t m_Samples[256 * 3]; }; -class CPDF_DocRenderData { - public: - explicit CPDF_DocRenderData(CPDF_Document* pPDFDoc); - ~CPDF_DocRenderData(); - CPDF_Type3Cache* GetCachedType3(CPDF_Type3Font* pFont); - CPDF_TransferFunc* GetTransferFunc(CPDF_Object* pObj); - void Clear(bool bRelease = false); - void ReleaseCachedType3(CPDF_Type3Font* pFont); - void ReleaseTransferFunc(CPDF_Object* pObj); - - private: - using CPDF_Type3CacheMap = - std::map*>; - using CPDF_TransferFuncMap = - std::map*>; - - CPDF_Document* m_pPDFDoc; - CPDF_Type3CacheMap m_Type3FaceMap; - CPDF_TransferFuncMap m_TransferFuncMap; -}; - class CPDF_RenderStatus { public: CPDF_RenderStatus(); -- cgit v1.2.3