From 56b272e9cb6c680c1fcda2984c23c47b6cc4b421 Mon Sep 17 00:00:00 2001 From: Lei Zhang Date: Wed, 9 May 2018 19:20:43 +0000 Subject: Break backcolor code out of CPDF_RenderStatus::LoadSMask(). Fix some nits in the code in the process. Change-Id: I660b3bf547823a11bc291ed2dd1ec19a76b24b5b Reviewed-on: https://pdfium-review.googlesource.com/32178 Reviewed-by: dsinclair Commit-Queue: Lei Zhang --- core/fpdfapi/render/cpdf_renderstatus.cpp | 71 ++++++++++++++++++------------- core/fpdfapi/render/cpdf_renderstatus.h | 4 ++ 2 files changed, 46 insertions(+), 29 deletions(-) diff --git a/core/fpdfapi/render/cpdf_renderstatus.cpp b/core/fpdfapi/render/cpdf_renderstatus.cpp index 708d7076c1..3672729f2e 100644 --- a/core/fpdfapi/render/cpdf_renderstatus.cpp +++ b/core/fpdfapi/render/cpdf_renderstatus.cpp @@ -2561,35 +2561,10 @@ RetainPtr CPDF_RenderStatus::LoadSMask( return nullptr; CFX_DIBitmap& bitmap = *bitmap_device.GetBitmap(); - int color_space_family = 0; + int nCSFamily = 0; if (bLuminosity) { - CPDF_Array* pBC = pSMaskDict->GetArrayFor("BC"); - FX_ARGB back_color = 0xff000000; - if (pBC) { - CPDF_Object* pCSObj = nullptr; - CPDF_Dictionary* pDict = pGroup->GetDict(); - if (pDict && pDict->GetDictFor("Group")) { - pCSObj = pDict->GetDictFor("Group")->GetDirectObjectFor("CS"); - } - const CPDF_ColorSpace* pCS = - m_pContext->GetDocument()->LoadColorSpace(pCSObj); - if (pCS) { - // Store Color Space Family to use in CPDF_RenderStatus::Initialize. - color_space_family = pCS->GetFamily(); - - float R, G, B; - uint32_t comps = std::max(8u, pCS->CountComponents()); - std::vector floats(comps); - size_t count = std::min(8, pBC->GetCount()); - for (size_t i = 0; i < count; i++) { - floats[i] = pBC->GetNumberAt(i); - } - pCS->GetRGB(floats.data(), &R, &G, &B); - back_color = 0xff000000 | ((int32_t)(R * 255) << 16) | - ((int32_t)(G * 255) << 8) | (int32_t)(B * 255); - m_pContext->GetDocument()->GetPageData()->ReleaseColorSpace(pCSObj); - } - } + FX_ARGB back_color = + GetBackColor(pSMaskDict, pGroup->GetDict(), &nCSFamily); bitmap.Clear(back_color); } else { bitmap.Clear(0); @@ -2604,7 +2579,7 @@ RetainPtr CPDF_RenderStatus::LoadSMask( CPDF_RenderStatus status; status.Initialize(m_pContext.Get(), &bitmap_device, nullptr, nullptr, nullptr, nullptr, &options, 0, m_bDropObjects, pFormResource, true, - nullptr, 0, color_space_family, bLuminosity); + nullptr, 0, nCSFamily, bLuminosity); status.RenderObjectList(&form, &matrix); auto pMask = pdfium::MakeRetain(); @@ -2649,3 +2624,41 @@ RetainPtr CPDF_RenderStatus::LoadSMask( } return pMask; } + +FX_ARGB CPDF_RenderStatus::GetBackColor(const CPDF_Dictionary* pSMaskDict, + const CPDF_Dictionary* pGroupDict, + int* pCSFamily) { + static constexpr FX_ARGB kDefaultColor = ArgbEncode(255, 0, 0, 0); + CPDF_Array* pBC = pSMaskDict->GetArrayFor("BC"); + if (!pBC) + return kDefaultColor; + + CPDF_Object* pCSObj = nullptr; + const CPDF_Dictionary* pGroup = + pGroupDict ? pGroupDict->GetDictFor("Group") : nullptr; + if (pGroup) { + // TODO(thestig): Check if "CS" is from PDF spec 1.7, table 13. + pCSObj = pGroup->GetDirectObjectFor("CS"); + } + const CPDF_ColorSpace* pCS = + m_pContext->GetDocument()->LoadColorSpace(pCSObj); + if (!pCS) + return kDefaultColor; + + // Store Color Space Family to use in CPDF_RenderStatus::Initialize(). + *pCSFamily = pCS->GetFamily(); + + uint32_t comps = std::max(8u, pCS->CountComponents()); + std::vector floats(comps); + size_t count = std::min(8, pBC->GetCount()); + for (size_t i = 0; i < count; i++) + floats[i] = pBC->GetNumberAt(i); + + float R; + float G; + float B; + pCS->GetRGB(floats.data(), &R, &G, &B); + m_pContext->GetDocument()->GetPageData()->ReleaseColorSpace(pCSObj); + return ArgbEncode(255, static_cast(R * 255), static_cast(G * 255), + static_cast(B * 255)); +} diff --git a/core/fpdfapi/render/cpdf_renderstatus.h b/core/fpdfapi/render/cpdf_renderstatus.h index c6be9883d2..7058c0f9b9 100644 --- a/core/fpdfapi/render/cpdf_renderstatus.h +++ b/core/fpdfapi/render/cpdf_renderstatus.h @@ -148,6 +148,10 @@ class CPDF_RenderStatus { RetainPtr LoadSMask(CPDF_Dictionary* pSMaskDict, FX_RECT* pClipRect, const CFX_Matrix* pMatrix); + // Optionally write the colorspace family value into |pCSFamily|. + FX_ARGB GetBackColor(const CPDF_Dictionary* pSMaskDict, + const CPDF_Dictionary* pGroupDict, + int* pCSFamily); static RetainPtr GetCachedType3(CPDF_Type3Font* pFont); static std::unique_ptr CloneObjStates( const CPDF_GraphicStates* pPathObj, -- cgit v1.2.3